Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / android / runtime_client / src / org / xwalk / app / runtime / extension / XWalkExtensionClient.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.app.runtime.extension;
6
7 import android.content.Intent;
8
9 /**
10  * This class is to encapsulate the reflection detail of
11  * invoking XWalkExtension class in the shared library APK.
12  *
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.
16  */
17 public class XWalkExtensionClient {
18     // The unique name for this extension.
19     protected String mName;
20
21     // The JavaScript code stub. Will be injected to JS engine.
22     protected String mJsApi;
23
24     // The context used by extensions.
25     protected XWalkExtensionContextClient mExtensionContext;
26
27     /**
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.
33      */
34     public XWalkExtensionClient(String name, String jsApi, XWalkExtensionContextClient context) {
35         assert (context != null);
36         mName = name;
37         mJsApi = jsApi;
38         mExtensionContext = context;
39         mExtensionContext.registerExtension(this);
40     }
41
42     /**
43      * Get the unique name of extension.
44      * @return the name of extension set from constructor.
45      */
46     public final String getExtensionName() {
47         return mName;
48     }
49
50     /**
51      * Get the JavaScript code stub.
52      * @return the JavaScript code stub.
53      */
54     public final String getJsApi() {
55         return mJsApi;
56     }
57
58     /**
59      * Called when this app is onStart.
60      */
61     public void onStart() {
62     }
63
64     /**
65      * Called when this app is onResume.
66      */
67     public void onResume() {
68     }
69
70     /**
71      * Called when this app is onPause.
72      */
73     public void onPause() {
74     }
75
76     /**
77      * Called when this app is onStop.
78      */
79     public void onStop() {
80     }
81
82     /**
83      * Called when this app is onDestroy.
84      */
85     public void onDestroy() {
86     }
87
88     /**
89      * Called when this app is onNewIntent.
90      */
91     public void onNewIntent(Intent intent) {
92     }
93
94     /**
95      * Tell extension that one activity exists so that it can know the result
96      * of the exit code.
97      */
98     public void onActivityResult(int requestCode, int resultCode, Intent data) {
99     }
100
101     /**
102      * JavaScript calls into Java code. The message is handled by
103      * the extension implementation. The inherited classes should
104      * override and add its implementation.
105      * @param extensionInstanceID the ID of extension instance where the message came from.
106      * @param message the message from JavaScript code.
107      */
108     public void onMessage(int extensionInstanceID, String message) {
109     }
110
111     /**
112      * Synchronized JavaScript calls into Java code. Similar to
113      * onMessage. The only difference is it's a synchronized
114      * message.
115      * @param extensionInstanceID the ID of extension instance where the message came from.
116      * @param message the message from JavaScript code.
117      */
118     public String onSyncMessage(int extensionInstanceID, String message) {
119         return "";
120     }
121
122
123     /**
124      * Post messages to JavaScript via extension's context.
125      * It's used by child classes to post message from Java side
126      * to JavaScript side.
127      * @param instanceID the ID of target extension instance.
128      * @param message the message to be passed to Javascript.
129      */
130     public final void postMessage(int instanceID, String message) {
131         mExtensionContext.postMessage(this, instanceID, message);
132     }
133
134     /**
135      * Broadcast messages to JavaScript via extension's context.
136      * It's used by child classes to broadcast message from Java side
137      * to all JavaScript side instances of the extension.
138      * @param message the message to be passed to Javascript.
139      */
140     public final void broadcastMessage(String message) {
141         mExtensionContext.broadcastMessage(this, message);
142     }
143 }