1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.app.runtime.extension;
7 import android.content.Intent;
10 * This class is to encapsulate the reflection detail of
11 * invoking XWalkExtension class in the shared library APK.
13 * Each external extension should inherit this class and implements
14 * below methods. It's created and registered by runtime side via the
15 * configuration information in extensions-config.json.
17 public class XWalkExtensionClient {
18 // The unique name for this extension.
19 protected String mName;
21 // The JavaScript code stub. Will be injected to JS engine.
22 protected String mJsApi;
24 // The context used by extensions.
25 protected XWalkExtensionContextClient mExtensionContext;
28 * Constructor with the information of an extension.
29 * @param name the extension name.
30 * @param apiVersion the version of API.
31 * @param jsApi the code stub of JavaScript for this extension.
32 * @param context the extension context.
34 public XWalkExtensionClient(String name, String jsApi, XWalkExtensionContextClient context) {
35 assert (context != null);
38 mExtensionContext = context;
39 mExtensionContext.registerExtension(this);
43 * Get the unique name of extension.
44 * @return the name of extension set from constructor.
46 public final String getExtensionName() {
51 * Get the JavaScript code stub.
52 * @return the JavaScript code stub.
54 public final String getJsApi() {
59 * Called when this app is onStart.
61 public void onStart() {
65 * Called when this app is onResume.
67 public void onResume() {
71 * Called when this app is onPause.
73 public void onPause() {
77 * Called when this app is onStop.
79 public void onStop() {
83 * Called when this app is onDestroy.
85 public void onDestroy() {
89 * Tell extension that one activity exists so that it can know the result
92 public void onActivityResult(int requestCode, int resultCode, Intent data) {
96 * JavaScript calls into Java code. The message is handled by
97 * the extension implementation. The inherited classes should
98 * override and add its implementation.
99 * @param extensionInstanceID the ID of extension instance where the message came from.
100 * @param message the message from JavaScript code.
102 public void onMessage(int extensionInstanceID, String message) {
106 * Synchronized JavaScript calls into Java code. Similar to
107 * onMessage. The only difference is it's a synchronized
109 * @param extensionInstanceID the ID of extension instance where the message came from.
110 * @param message the message from JavaScript code.
112 public String onSyncMessage(int extensionInstanceID, String message) {
118 * Post messages to JavaScript via extension's context.
119 * It's used by child classes to post message from Java side
120 * to JavaScript side.
121 * @param instanceID the ID of target extension instance.
122 * @param message the message to be passed to Javascript.
124 public final void postMessage(int instanceID, String message) {
125 mExtensionContext.postMessage(this, instanceID, message);
129 * Broadcast messages to JavaScript via extension's context.
130 * It's used by child classes to broadcast message from Java side
131 * to all JavaScript side instances of the extension.
132 * @param message the message to be passed to Javascript.
134 public final void broadcastMessage(String message) {
135 mExtensionContext.broadcastMessage(this, message);