Upstream version 8.36.161.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.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10
11 import java.lang.reflect.Method;
12 import java.util.StringTokenizer;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16 import org.xwalk.app.runtime.CrossPackageWrapper;
17
18 /**
19  * This class is to encapsulate the reflection detail of
20  * invoking XWalkExtension class in the shared library APK.
21  *
22  * Each external extension should inherit this class and implements
23  * below methods. It's created and registered by runtime side via the
24  * configuration information in extensions-config.json.
25  */
26 public class XWalkExtensionClient extends CrossPackageWrapper {
27
28     private final static String EXTENSION_CLASS_NAME = "org.xwalk.core.internal.extension.XWalkExtensionClientImpl";
29     private Object mInstance;
30     private Method mGetExtensionName;
31     private Method mGetJsApi;
32     private Method mPostMessage;
33     private Method mBroadcastMessage;
34
35     protected XWalkExtensionContextClient mContext;
36
37     public XWalkExtensionClient(String name, String jsApi, XWalkExtensionContextClient context) {
38         super(context.getActivity(), EXTENSION_CLASS_NAME, null /* ExceptionHalder */, String.class, String.class,
39                 context.getInstance().getClass(), Object.class);
40         mContext = context;
41         mInstance = this.createInstance(name, jsApi, context.getInstance(), this);
42
43         mGetExtensionName = lookupMethod("getExtensionName");
44         mGetJsApi = lookupMethod("getJsApi");
45         mPostMessage = lookupMethod("postMessage", int.class, String.class);
46         mBroadcastMessage = lookupMethod("broadcastMessage", String.class);
47     }
48
49     /**
50      * Get the extension name which is set when it's created.
51      */
52     public final String getExtensionName() {
53         return (String)invokeMethod(mGetExtensionName, mInstance);
54     }
55
56     /**
57      * Get the JavaScript stub code which is set when it's created.
58      */
59     public final String getJsApi() {
60         return (String)invokeMethod(mGetJsApi, mInstance);
61     }
62
63     /**
64      * Called when this app is onResume.
65      */
66     public void onResume() {
67     }
68
69     /**
70      * Called when this app is onPause.
71      */
72     public void onPause() {
73     }
74
75     /**
76      * Called when this app is onDestroy.
77      */
78     public void onDestroy() {
79     }
80
81     /**
82      * Tell extension that one activity exists so that it can know the result
83      * of the exit code.
84      */
85     public void onActivityResult(int requestCode, int resultCode, Intent data) {
86     }
87
88     /**
89      * JavaScript calls into Java code. The message is handled by
90      * the extension implementation. The inherited classes should
91      * override and add its implementation.
92      * @param extensionInstanceID the ID of extension instance where the message came from.
93      * @param message the message from JavaScript code.
94      */
95     public void onMessage(int extensionInstanceID, String message) {
96     }
97
98     /**
99      * Synchronized JavaScript calls into Java code. Similar to
100      * onMessage. The only difference is it's a synchronized
101      * message.
102      * @param extensionInstanceID the ID of extension instance where the message came from.
103      * @param message the message from JavaScript code.
104      */
105     public String onSyncMessage(int extensionInstanceID, String message) {
106         return "";
107     }
108
109     /**
110      * Post messages to JavaScript via extension's context.
111      * It's used by child classes to post message from Java side
112      * to JavaScript side.
113      * @param extensionInstanceID the ID of extension instance where the message came from.
114      * @param message the message to be passed to Javascript.
115      */
116     public final void postMessage(int extensionInstanceID, String message) {
117         invokeMethod(mPostMessage, mInstance, extensionInstanceID, message);
118     }
119
120     /**
121      * Broadcast messages to JavaScript via extension's context.
122      * It's used by child classes to broadcast message from Java side
123      * to all JavaScript side instances of the extension.
124      * @param message the message to be passed to Javascript.
125      */
126     public final void broadcastMessage(String message) {
127         invokeMethod(mBroadcastMessage, mInstance, message);
128     }
129 }