Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / extension / XWalkExtension.java
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.
4
5 package org.xwalk.core.extension;
6
7 import android.content.Context;
8 import android.content.Intent;
9
10 /**
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.
15  */
16 public abstract class XWalkExtension {
17     // The unique name for this extension.
18     protected String mName;
19
20     // The JavaScript code stub. Will be injected to JS engine.
21     protected String mJsApi;
22
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;
26
27     // The context used by extensions.
28     protected XWalkExtensionContext mExtensionContext;
29
30     /**
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.
36      */
37     public XWalkExtension(String name, String jsApi, XWalkExtensionContext context) {
38         mName = name;
39         mJsApi = jsApi;
40         mEntryPoints = null;
41         mExtensionContext = context;
42         mExtensionContext.registerExtension(this);
43     }
44
45     /**
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.
52      */
53     public XWalkExtension(String name, String jsApi, String[] entryPoints, XWalkExtensionContext context) {
54         mName = name;
55         mJsApi = jsApi;
56         mEntryPoints = entryPoints;
57         mExtensionContext = context;
58         mExtensionContext.registerExtension(this);
59     }
60
61     /**
62      * Get the unique name of extension.
63      * @return the name of extension set from constructor.
64      */
65     public String getExtensionName() {
66         return mName;
67     }
68
69     /**
70      * Get the JavaScript code stub.
71      * @return the JavaScript code stub.
72      */
73     public String getJsApi() {
74         return mJsApi;
75     }
76
77     /**
78      * Get the entry points of extension.
79      * @return the entry points.
80      */
81     public String[] getEntryPoints() {
82         return mEntryPoints;
83     }
84
85     /**
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.
91      */
92     public abstract void onMessage(int instanceID, String message);
93
94     /**
95      * Synchronized JavaScript calls into Java code. Similar to
96      * onMessage. The only difference is it's a synchronized
97      * message.
98      * @param instanceID the ID of extension instance where the message came from.
99      * @param message the message from JavaScript code.
100      */
101     public String onSyncMessage(int instanceID, String message) {
102         return null;
103     }
104
105     /**
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.
111      */
112     public final void postMessage(int instanceID, String message) {
113         mExtensionContext.postMessage(this, instanceID, message);
114     }
115
116     /**
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.
121      */
122     public final void broadcastMessage(String message) {
123         mExtensionContext.broadcastMessage(this, message);
124     }
125
126     /**
127      * Called when this app is onResume.
128      */
129     public void onResume() {
130     }
131
132     /**
133      * Called when this app is onPause.
134      */
135     public void onPause() {
136     }
137
138     /**
139      * Called when this app is onDestroy.
140      */
141     public void onDestroy() {
142     }
143
144     /**
145      * Tell extension that one activity exists so that it can know the result
146      * of the exit code.
147      */
148     public void onActivityResult(int requestCode, int resultCode, Intent data) {
149     }
150 }