Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / common / android / xwalk_extension_android.h
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 #ifndef XWALK_EXTENSIONS_COMMON_ANDROID_XWALK_EXTENSION_ANDROID_H_
6 #define XWALK_EXTENSIONS_COMMON_ANDROID_XWALK_EXTENSION_ANDROID_H_
7
8 #include <map>
9 #include <string>
10
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"
18
19 namespace xwalk {
20 namespace extensions {
21
22 class XWalkExtensionAndroidInstance;
23
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.
27 //
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
31 // to Java side.
32 //
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.
38 //
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.
42 //
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 {
48  public:
49   XWalkExtensionAndroid(JNIEnv* env, jobject obj, jstring name,
50                         jstring js_api, jobjectArray js_entry_ports);
51   virtual ~XWalkExtensionAndroid();
52
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);
56
57   void DestroyExtension(JNIEnv* env, jobject obj);
58
59   virtual XWalkExtensionInstance* CreateInstance() OVERRIDE;
60
61   void RemoveInstance(int instance);
62
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);
69
70  private:
71   bool is_valid();
72
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_;
78
79   DISALLOW_COPY_AND_ASSIGN(XWalkExtensionAndroid);
80 };
81
82 class XWalkExtensionAndroidInstance : public XWalkExtensionInstance {
83  public:
84   explicit XWalkExtensionAndroidInstance(
85       XWalkExtensionAndroid* extension,
86       const JavaObjectWeakGlobalRef& java_ref,
87       int id);
88   ~XWalkExtensionAndroidInstance();
89
90   void PostMessageWrapper(const char* msg) {
91     PostMessageToJS(scoped_ptr<base::Value>(new base::StringValue(msg)));
92   }
93
94   int getID() {
95       return id_;
96   }
97
98  private:
99   virtual void HandleMessage(scoped_ptr<base::Value> msg) OVERRIDE;
100   virtual void HandleSyncMessage(scoped_ptr<base::Value> msg) OVERRIDE;
101
102   XWalkExtensionAndroid* extension_;
103   // Hold a refenerence to Java-side XWalkExtensionAndroid object.
104   JavaObjectWeakGlobalRef java_ref_;
105   int id_;
106
107   DISALLOW_COPY_AND_ASSIGN(XWalkExtensionAndroidInstance);
108 };
109
110 bool RegisterXWalkExtensionAndroid(JNIEnv* env);
111
112 }  // namespace extensions
113 }  // namespace xwalk
114
115 #endif  // XWALK_EXTENSIONS_COMMON_ANDROID_XWALK_EXTENSION_ANDROID_H_