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.core.extension;
7 import android.content.Context;
8 import android.content.Intent;
11 * The public base class of xwalk extensions. Each extension should inherit
12 * this class and implement its interfaces. Note that it's for every extensions.
13 * For internal extensions, each one should base on it directly. For external
14 * extensions, there'll be a bridge class in the runtime client side.
16 public abstract class XWalkExtension {
17 // The unique name for this extension.
18 protected String mName;
20 // The JavaScript code stub. Will be injected to JS engine.
21 protected String mJsApi;
23 // The entry points are used when extension needs to have objects outside
24 // the namespace that is implicitly created using its name.
25 protected String[] mEntryPoints;
27 // The context used by extensions.
28 protected XWalkExtensionContext mExtensionContext;
31 * Constructor with the information of an extension.
32 * @param name the extension name.
33 * @param apiVersion the version of API.
34 * @param jsApi the code stub of JavaScript for this extension.
35 * @param context the extension context.
37 public XWalkExtension(String name, String jsApi, XWalkExtensionContext context) {
41 mExtensionContext = context;
42 mExtensionContext.registerExtension(this);
46 * Constructor with the information of an extension.
47 * @param name the extension name.
48 * @param apiVersion the version of API.
49 * @param jsApi the code stub of JavaScript for this extension.
50 * @param entryPoints the entry points of JavaScript for this extension.
51 * @param context the extension context.
53 public XWalkExtension(String name, String jsApi, String[] entryPoints, XWalkExtensionContext context) {
56 mEntryPoints = entryPoints;
57 mExtensionContext = context;
58 mExtensionContext.registerExtension(this);
62 * Get the unique name of extension.
63 * @return the name of extension set from constructor.
65 public String getExtensionName() {
70 * Get the JavaScript code stub.
71 * @return the JavaScript code stub.
73 public String getJsApi() {
78 * Get the entry points of extension.
79 * @return the entry points.
81 public String[] getEntryPoints() {
86 * JavaScript calls into Java code. The message is handled by
87 * the extension implementation. The inherited classes should
88 * override and add its implementation.
89 * @param instanceID the ID of extension instance where the message came from.
90 * @param message the message from JavaScript code.
92 public abstract void onMessage(int instanceID, String message);
95 * Synchronized JavaScript calls into Java code. Similar to
96 * onMessage. The only difference is it's a synchronized
98 * @param instanceID the ID of extension instance where the message came from.
99 * @param message the message from JavaScript code.
101 public String onSyncMessage(int instanceID, String message) {
106 * Post messages to JavaScript via extension's context.
107 * It's used by child classes to post message from Java side
108 * to JavaScript side.
109 * @param instanceID the ID of target extension instance.
110 * @param message the message to be passed to Javascript.
112 public final void postMessage(int instanceID, String message) {
113 mExtensionContext.postMessage(this, instanceID, message);
117 * Broadcast messages to JavaScript via extension's context.
118 * It's used by child classes to broad message from Java side
119 * to all JavaScript side instances of the extension.
120 * @param message the message to be passed to Javascript.
122 public final void broadcastMessage(String message) {
123 mExtensionContext.broadcastMessage(this, message);
127 * Called when this app is onResume.
129 public void onResume() {
133 * Called when this app is onPause.
135 public void onPause() {
139 * Called when this app is onDestroy.
141 public void onDestroy() {
145 * Tell extension that one activity exists so that it can know the result
148 public void onActivityResult(int requestCode, int resultCode, Intent data) {