Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Shared mode for Crosswalk Core Library on Android

wang16 edited this page Sep 24, 2014 · 3 revisions

General

Shared mode allows multiple xwalk applications to share one xwalk core. There will be a library APK to be installed on device for other xwalk apps to use. Via [createPackageContext](http://developer.android.com/reference/android/content/Context.html#createPackageContext(java.lang.String, int)), app can load library package’s classloader and invoking xwalk core via reflection. So a reflection layer will be created between org.xwalk.core and org.xwalk.core.internal to support shared mode. Note that, the reflection layer is always there no matter whether shared mode is used or not. The difference is whether to load target classes from classloader within package or from library package.

Package structure:

  • org.xwalk.core.internal: Package that all real implementations are in, as well as bridge classes, which extends internal classes with reflection capability. Will be included in library APK.
  • org.xwalk.core: Package that will be exposed as Embedding API. It’s a wrapper of internal object, each wrapper holds an instance of bridge object and talk to it via reflection.

Reflection layer design:

Reflection layer will be created between org.xwalk.core and org.xwalk.core.internal. The class model differs based on the usage of each class.

  • Basic class model: The basic idea of how bridge/wrapper work with internal class to keep the possibility to be inherited as well as internal functionality if using super();
  • Multiple class model: Some APIs core exposes touch multiple internal classes. This model shows how its handled. (e.g. XWalkView and XWalkUIClient are coupled closely)
  • Internally create object: Some class is not designed to be overriden. It's created internally inside org.xwalk.core.internal and exposed to embedder just for method calling. (e.g. XWalkNavigationHistory.)

Basic class model:

Basic class model

  • bridge and wrapper will talk with each other via reflection.
  • bridge inherits internal class, bridge's foo() calls wrapper's foo() and wrapper's foo() calls bridge's fooSuper().
  • If method with Internal classes as parameter, will have an override method in bridge, which chooses super or wrapper to invoke by detecting the class type of input params.
  • For Enum type, will use enumInternal = EnumInternal.valueOf(enum.toString()) to convert between EnumInternal and Enum
  • following charts shows how the function call works when view.foo() is invoked by other components either in internal package or wrapper package.

Basic class process

Multiple class model:

Multiple class model

  • When wrapper class uses another wrapper class, it pass its bridge object to its own bridge.
  • Bridge always take bridge class type as method parameters.

Multiple class process

Internally Created Object

Internal created class model

Some object are created inside internal package and needs to be exposed to wrapper to be used.

  • Bridge class uses the default constructor of internal class.
  • Bridge keeps an instance of internal object as well.
  • If the instance of internal object is not null, will use that instance for all method call.

Internal created class process

Reflection layer generating

Annotation will be added to each class XWalk Embedding API exposes to indicate the property of the class. A script is used to analysis the source code with annotation and generate source code for bridge and wrapper class.

Debug Core Library

First following wiki to create project and link necessary source code in eclipse. To debug the reflection layer, link the generated source code for bridge and wrapper class as well. They are generated in out/Release(Debug)/gen/xwalk_core_reflection_layer/wrapper(bridge).

Clone this wiki locally