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.app.runtime.extension;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
11 import java.lang.reflect.Method;
12 import java.util.StringTokenizer;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
16 import org.xwalk.app.runtime.CrossPackageWrapper;
19 * This class is to encapsulate the reflection detail of
20 * invoking XWalkExtension class in the shared library APK.
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.
26 public class XWalkExtensionClient extends CrossPackageWrapper {
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;
35 protected XWalkExtensionContextClient mContext;
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);
41 mInstance = this.createInstance(name, jsApi, context.getInstance(), this);
43 mGetExtensionName = lookupMethod("getExtensionName");
44 mGetJsApi = lookupMethod("getJsApi");
45 mPostMessage = lookupMethod("postMessage", int.class, String.class);
46 mBroadcastMessage = lookupMethod("broadcastMessage", String.class);
50 * Get the extension name which is set when it's created.
52 public final String getExtensionName() {
53 return (String)invokeMethod(mGetExtensionName, mInstance);
57 * Get the JavaScript stub code which is set when it's created.
59 public final String getJsApi() {
60 return (String)invokeMethod(mGetJsApi, mInstance);
64 * Called when this app is onResume.
66 public void onResume() {
70 * Called when this app is onPause.
72 public void onPause() {
76 * Called when this app is onDestroy.
78 public void onDestroy() {
82 * Tell extension that one activity exists so that it can know the result
85 public void onActivityResult(int requestCode, int resultCode, Intent data) {
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.
95 public void onMessage(int extensionInstanceID, String message) {
99 * Synchronized JavaScript calls into Java code. Similar to
100 * onMessage. The only difference is it's a synchronized
102 * @param extensionInstanceID the ID of extension instance where the message came from.
103 * @param message the message from JavaScript code.
105 public String onSyncMessage(int extensionInstanceID, String message) {
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.
116 public final void postMessage(int extensionInstanceID, String message) {
117 invokeMethod(mPostMessage, mInstance, extensionInstanceID, message);
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.
126 public final void broadcastMessage(String message) {
127 invokeMethod(mBroadcastMessage, mInstance, message);