Upstream version 9.38.204.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      * Tell extension that one activity exists so that it can know the result
90      * of the exit code.
91      */
92     public void onActivityResult(int requestCode, int resultCode, Intent data) {
93     }
94
95     /**
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.
101      */
102     public void onMessage(int extensionInstanceID, String message) {
103     }
104
105     /**
106      * Synchronized JavaScript calls into Java code. Similar to
107      * onMessage. The only difference is it's a synchronized
108      * message.
109      * @param extensionInstanceID the ID of extension instance where the message came from.
110      * @param message the message from JavaScript code.
111      */
112     public String onSyncMessage(int extensionInstanceID, String message) {
113         return "";
114     }
115
116
117     /**
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.
123      */
124     public final void postMessage(int instanceID, String message) {
125         mExtensionContext.postMessage(this, instanceID, message);
126     }
127
128     /**
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.
133      */
134     public final void broadcastMessage(String message) {
135         mExtensionContext.broadcastMessage(this, message);
136     }
137 }