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 #ifndef XWALK_EXTENSIONS_COMMON_ANDROID_XWALK_EXTENSION_ANDROID_H_
6 #define XWALK_EXTENSIONS_COMMON_ANDROID_XWALK_EXTENSION_ANDROID_H_
11 #include "base/android/jni_weak_ref.h"
12 #include "base/android/scoped_java_ref.h"
13 #include "base/callback.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "xwalk/extensions/browser/xwalk_extension_service.h"
17 #include "xwalk/extensions/common/xwalk_extension.h"
20 namespace extensions {
22 class XWalkExtensionAndroidInstance;
24 // This class (together with XWalkExtensionAndroidInstance) is the native part
25 // of XWalkExtensionAndroid on Java side for message-passing based extension
26 // system works on Android platform.
28 // Once a XWalkExtensionAndroid-derived instance is created on Java side, the
29 // native part is also created accordingly. Meanwhile, the native part has to
30 // hold a reference to the Java-side object for routing message from native side
33 // The native-side XWalkExtensionAndroid/XWalkExtensionInstance objects are
34 // actually owned by XWalkExtensionServer, it means that XWalkExtensionServer
35 // is the right place to delete those objects. The native XWalkExtensionAndroid
36 // object is always alive in memory until the process is terminated after it is
37 // registered into extension server.
39 // The Java-side object may be destroyed due to the lifecycle of Activity, e.g.
40 // when pressing back button, the Activity will invoke onDestroyed callback and
41 // hence Java-side objects are treated to be invalid.
43 // Since the native part of XWalkExtensionAndroid is designed to be reused
44 // during the whole process lifecycle. For each native part, its referenced
45 // Java-side object needs to be re-assigned once a new Java extension object is
46 // created for the same extension identified the extension name.
47 class XWalkExtensionAndroid : public XWalkExtension {
49 XWalkExtensionAndroid(JNIEnv* env, jobject obj, jstring name,
50 jstring js_api, jobjectArray js_entry_ports);
51 virtual ~XWalkExtensionAndroid();
53 // JNI interface to post message from Java to JS
54 void PostMessage(JNIEnv* env, jobject obj, jint instance, jstring msg);
55 void BroadcastMessage(JNIEnv* env, jobject obj, jstring msg);
57 void DestroyExtension(JNIEnv* env, jobject obj);
59 XWalkExtensionInstance* CreateInstance() override;
61 void RemoveInstance(int instance);
63 // Each Extension object created on Java side is backed by this native object,
64 // and the native object also has a reference to Java-side object for message
65 // routing from native side to Java side. However, the Java extension object
66 // might be destroyed due to activity lifecycle. This method is used to
67 // re-bind the native object to an valid Java-side extension object.
68 void BindToJavaObject(JNIEnv* env, jobject obj);
73 typedef std::map<int, XWalkExtensionAndroidInstance*> InstanceMap;
74 InstanceMap instances_;
75 // Hold a reference to Java-side extension object for message routing.
76 JavaObjectWeakGlobalRef java_ref_;
77 int next_instance_id_;
79 DISALLOW_COPY_AND_ASSIGN(XWalkExtensionAndroid);
82 class XWalkExtensionAndroidInstance : public XWalkExtensionInstance {
84 explicit XWalkExtensionAndroidInstance(
85 XWalkExtensionAndroid* extension,
86 const JavaObjectWeakGlobalRef& java_ref,
88 ~XWalkExtensionAndroidInstance();
90 void PostMessageWrapper(const char* msg) {
91 PostMessageToJS(scoped_ptr<base::Value>(new base::StringValue(msg)));
99 void HandleMessage(scoped_ptr<base::Value> msg) override;
100 void HandleSyncMessage(scoped_ptr<base::Value> msg) override;
102 XWalkExtensionAndroid* extension_;
103 // Hold a refenerence to Java-side XWalkExtensionAndroid object.
104 JavaObjectWeakGlobalRef java_ref_;
107 DISALLOW_COPY_AND_ASSIGN(XWalkExtensionAndroidInstance);
110 bool RegisterXWalkExtensionAndroid(JNIEnv* env);
112 } // namespace extensions
115 #endif // XWALK_EXTENSIONS_COMMON_ANDROID_XWALK_EXTENSION_ANDROID_H_