1 // Copyright 2014 The Chromium Authors. 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 CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_ 6 #define CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_ 7 8 #include <map> 9 #include <set> 10 11 #include "base/android/jni_weak_ref.h" 12 #include "base/android/scoped_java_ref.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/weak_ptr.h" 15 #include "content/browser/android/java/gin_java_bound_object.h" 16 #include "content/browser/android/java/gin_java_method_invocation_helper.h" 17 #include "content/public/browser/web_contents_observer.h" 18 19 namespace base { 20 class ListValue; 21 } 22 23 namespace IPC { 24 class Message; 25 } 26 27 namespace content { 28 29 // This class handles injecting Java objects into a single RenderView. The Java 30 // object itself lives in the browser process on a background thread, while a 31 // proxy object is created in the renderer. An instance of this class exists 32 // for each RenderFrameHost. 33 class GinJavaBridgeDispatcherHost 34 : public base::SupportsWeakPtr<GinJavaBridgeDispatcherHost>, 35 public WebContentsObserver, 36 public GinJavaMethodInvocationHelper::DispatcherDelegate { 37 public: 38 39 GinJavaBridgeDispatcherHost(WebContents* web_contents, 40 jobject retained_object_set); 41 virtual ~GinJavaBridgeDispatcherHost(); 42 43 void AddNamedObject( 44 const std::string& name, 45 const base::android::JavaRef<jobject>& object, 46 const base::android::JavaRef<jclass>& safe_annotation_clazz); 47 void RemoveNamedObject(const std::string& name); 48 void SetAllowObjectContentsInspection(bool allow); 49 50 // WebContentsObserver 51 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) OVERRIDE; 52 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) OVERRIDE; 53 virtual void DocumentAvailableInMainFrame() OVERRIDE; 54 virtual bool OnMessageReceived(const IPC::Message& message, 55 RenderFrameHost* render_frame_host) OVERRIDE; 56 57 // GinJavaMethodInvocationHelper::DispatcherDelegate 58 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( 59 GinJavaBoundObject::ObjectID object_id) OVERRIDE; 60 61 void OnGetMethods(RenderFrameHost* render_frame_host, 62 GinJavaBoundObject::ObjectID object_id, 63 IPC::Message* reply_msg); 64 void OnHasMethod(RenderFrameHost* render_frame_host, 65 GinJavaBoundObject::ObjectID object_id, 66 const std::string& method_name, 67 IPC::Message* reply_msg); 68 void OnInvokeMethod(RenderFrameHost* render_frame_host, 69 GinJavaBoundObject::ObjectID object_id, 70 const std::string& method_name, 71 const base::ListValue& arguments, 72 IPC::Message* reply_msg); 73 74 private: 75 void OnObjectWrapperDeleted(RenderFrameHost* render_frame_host, 76 GinJavaBoundObject::ObjectID object_id); 77 78 bool IsValidRenderFrameHost(RenderFrameHost* render_frame_host); 79 void SendMethods(RenderFrameHost* render_frame_host, 80 const std::set<std::string>& method_names); 81 void SendHasMethodReply(RenderFrameHost* render_frame_host, 82 bool result); 83 void ProcessMethodInvocationResult( 84 RenderFrameHost* render_frame_host, 85 scoped_refptr<GinJavaMethodInvocationHelper> result); 86 void ProcessMethodInvocationObjectResult( 87 RenderFrameHost* render_frame_host, 88 scoped_refptr<GinJavaMethodInvocationHelper> result); 89 GinJavaBoundObject::ObjectID AddObject( 90 const base::android::JavaRef<jobject>& object, 91 const base::android::JavaRef<jclass>& safe_annotation_clazz, 92 bool is_named, 93 RenderFrameHost* holder); 94 bool FindObjectId(const base::android::JavaRef<jobject>& object, 95 GinJavaBoundObject::ObjectID* object_id); 96 void RemoveHolder(RenderFrameHost* holder, 97 const GinJavaBoundObject::ObjectMap::iterator& from, 98 size_t count); 99 bool HasPendingReply(RenderFrameHost* render_frame_host) const; 100 IPC::Message* TakePendingReply(RenderFrameHost* render_frame_host); 101 102 // Every time a GinJavaBoundObject backed by a real Java object is 103 // created/destroyed, we insert/remove a strong ref to that Java object into 104 // this set so that it doesn't get garbage collected while it's still 105 // potentially in use. Although the set is managed native side, it's owned 106 // and defined in Java so that pushing refs into it does not create new GC 107 // roots that would prevent ContentViewCore from being garbage collected. 108 JavaObjectWeakGlobalRef retained_object_set_; 109 bool allow_object_contents_inspection_; 110 GinJavaBoundObject::ObjectMap objects_; 111 typedef std::map<std::string, GinJavaBoundObject::ObjectID> NamedObjectMap; 112 NamedObjectMap named_objects_; 113 114 // Keep track of pending calls out to Java such that we can send a synchronous 115 // reply to the renderer waiting on the response should the RenderFrame be 116 // destroyed while the reply is pending. 117 // Only used on the UI thread. 118 typedef std::map<RenderFrameHost*, IPC::Message*> PendingReplyMap; 119 PendingReplyMap pending_replies_; 120 121 DISALLOW_COPY_AND_ASSIGN(GinJavaBridgeDispatcherHost); 122 }; 123 124 } // namespace content 125 126 #endif // CONTENT_BROWSER_ANDROID_JAVA_GIN_JAVA_BRIDGE_DISPATCHER_HOST_H_ 127