OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h" | 5 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager .h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_weak_ref.h" | 8 #include "base/android/jni_weak_ref.h" |
9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "content/browser/renderer_host/java/java_bound_object.h" | 13 #include "content/browser/renderer_host/java/java_bound_object.h" |
14 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host.h" | 14 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host.h" |
15 #include "content/common/android/hash_set.h" | 15 #include "content/common/android/hash_set.h" |
16 #include "content/common/java_bridge_messages.h" | |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
18 #include "third_party/WebKit/public/web/WebBindings.h" | 19 #include "third_party/WebKit/public/web/WebBindings.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 JavaBridgeDispatcherHostManager::JavaBridgeDispatcherHostManager( | 23 JavaBridgeDispatcherHostManager::JavaBridgeDispatcherHostManager( |
23 WebContents* web_contents) | 24 WebContents* web_contents, |
25 jobject retained_object_set) | |
24 : WebContentsObserver(web_contents), | 26 : WebContentsObserver(web_contents), |
27 retained_object_set_(base::android::AttachCurrentThread(), | |
28 retained_object_set), | |
25 allow_object_contents_inspection_(true) { | 29 allow_object_contents_inspection_(true) { |
30 DCHECK(retained_object_set); | |
benm (inactive)
2014/05/07 13:51:43
should this be retained_object_set_ (i.e. the memb
mnaganov (inactive)
2014/05/07 15:12:35
Here we are checking that we have received an actu
| |
26 } | 31 } |
27 | 32 |
28 JavaBridgeDispatcherHostManager::~JavaBridgeDispatcherHostManager() { | 33 JavaBridgeDispatcherHostManager::~JavaBridgeDispatcherHostManager() { |
29 for (ObjectMap::iterator iter = objects_.begin(); iter != objects_.end(); | 34 for (ObjectMap::iterator iter = objects_.begin(); iter != objects_.end(); |
30 ++iter) { | 35 ++iter) { |
31 blink::WebBindings::releaseObject(iter->second); | 36 blink::WebBindings::releaseObject(iter->second); |
32 } | 37 } |
33 DCHECK_EQ(0U, instances_.size()); | 38 DCHECK_EQ(0U, instances_.size()); |
34 } | 39 } |
35 | 40 |
36 void JavaBridgeDispatcherHostManager::AddNamedObject(const base::string16& name, | 41 void JavaBridgeDispatcherHostManager::AddNamedObject(const base::string16& name, |
37 NPObject* object) { | 42 NPObject* object) { |
38 // Record this object in a map so that we can add it into RenderViewHosts | 43 // Record this object in a map so that we can add it into RenderViewHosts |
39 // created later. The JavaBridgeDispatcherHost instances will take a | 44 // created later. The JavaBridgeDispatcherHost instances will take a |
40 // reference to the object, but we take one too, because this method can be | 45 // reference to the object, but we take one too, because this method can be |
41 // called before there are any such instances. | 46 // called before there are any such instances. |
42 blink::WebBindings::retainObject(object); | 47 blink::WebBindings::retainObject(object); |
43 objects_[name] = object; | 48 objects_[name] = object; |
44 | 49 |
45 for (InstanceMap::iterator iter = instances_.begin(); | 50 for (InstanceMap::iterator iter = instances_.begin(); |
46 iter != instances_.end(); ++iter) { | 51 iter != instances_.end(); ++iter) { |
47 iter->second->AddNamedObject(name, object); | 52 iter->second->AddNamedObject(name, object); |
48 } | 53 } |
49 } | 54 } |
50 | 55 |
51 void JavaBridgeDispatcherHostManager::SetRetainedObjectSet( | |
52 const JavaObjectWeakGlobalRef& retained_object_set) { | |
53 // It's an error to replace the retained_object_set_ after it's been set, | |
54 // so we check that it hasn't already been here. | |
55 // TODO(benm): It'd be better to pass the set in the constructor to avoid | |
56 // the chance of this happening; but that's tricky as this get's constructed | |
57 // before ContentViewCore (which owns the set). Best solution may be to move | |
58 // ownership of the JavaBridgerDispatchHostManager from WebContents to | |
59 // ContentViewCore? | |
60 JNIEnv* env = base::android::AttachCurrentThread(); | |
61 base::android::ScopedJavaLocalRef<jobject> new_retained_object_set = | |
62 retained_object_set.get(env); | |
63 base::android::ScopedJavaLocalRef<jobject> current_retained_object_set = | |
64 retained_object_set_.get(env); | |
65 if (!env->IsSameObject(new_retained_object_set.obj(), | |
66 current_retained_object_set.obj())) { | |
67 DCHECK(current_retained_object_set.is_null()); | |
68 retained_object_set_ = retained_object_set; | |
69 } | |
70 } | |
71 | |
72 void JavaBridgeDispatcherHostManager::RemoveNamedObject( | 56 void JavaBridgeDispatcherHostManager::RemoveNamedObject( |
73 const base::string16& name) { | 57 const base::string16& name) { |
74 ObjectMap::iterator iter = objects_.find(name); | 58 ObjectMap::iterator iter = objects_.find(name); |
75 if (iter == objects_.end()) { | 59 if (iter == objects_.end()) { |
76 return; | 60 return; |
77 } | 61 } |
78 | 62 |
79 blink::WebBindings::releaseObject(iter->second); | 63 blink::WebBindings::releaseObject(iter->second); |
80 objects_.erase(iter); | 64 objects_.erase(iter); |
81 | 65 |
82 for (InstanceMap::iterator iter = instances_.begin(); | 66 for (InstanceMap::iterator iter = instances_.begin(); |
83 iter != instances_.end(); ++iter) { | 67 iter != instances_.end(); ++iter) { |
84 iter->second->RemoveNamedObject(name); | 68 iter->second->RemoveNamedObject(name); |
85 } | 69 } |
86 } | 70 } |
87 | 71 |
88 void JavaBridgeDispatcherHostManager::OnGetChannelHandle( | |
89 RenderFrameHost* render_frame_host, IPC::Message* reply_msg) { | |
90 instances_[render_frame_host]->OnGetChannelHandle(reply_msg); | |
91 } | |
92 | |
93 void JavaBridgeDispatcherHostManager::RenderFrameCreated( | 72 void JavaBridgeDispatcherHostManager::RenderFrameCreated( |
94 RenderFrameHost* render_frame_host) { | 73 RenderFrameHost* render_frame_host) { |
95 // Creates a JavaBridgeDispatcherHost for the specified RenderViewHost and | 74 // Creates a JavaBridgeDispatcherHost for the specified RenderViewHost and |
96 // adds all currently registered named objects to the new instance. | 75 // adds all currently registered named objects to the new instance. |
97 scoped_refptr<JavaBridgeDispatcherHost> instance = | 76 scoped_refptr<JavaBridgeDispatcherHost> instance = |
98 new JavaBridgeDispatcherHost(render_frame_host); | 77 new JavaBridgeDispatcherHost(render_frame_host); |
99 | 78 |
100 for (ObjectMap::const_iterator iter = objects_.begin(); | 79 for (ObjectMap::const_iterator iter = objects_.begin(); |
101 iter != objects_.end(); ++iter) { | 80 iter != objects_.end(); ++iter) { |
102 instance->AddNamedObject(iter->first, iter->second); | 81 instance->AddNamedObject(iter->first, iter->second); |
(...skipping 22 matching lines...) Expand all Loading... | |
125 // We also need to add back the named objects we have so far as they | 104 // We also need to add back the named objects we have so far as they |
126 // should survive navigations. | 105 // should survive navigations. |
127 ObjectMap::iterator it = objects_.begin(); | 106 ObjectMap::iterator it = objects_.begin(); |
128 for (; it != objects_.end(); ++it) { | 107 for (; it != objects_.end(); ++it) { |
129 JNI_Java_HashSet_add(env, retained_object_set, | 108 JNI_Java_HashSet_add(env, retained_object_set, |
130 JavaBoundObject::GetJavaObject(it->second)); | 109 JavaBoundObject::GetJavaObject(it->second)); |
131 } | 110 } |
132 } | 111 } |
133 } | 112 } |
134 | 113 |
114 bool JavaBridgeDispatcherHostManager::OnMessageReceived( | |
115 const IPC::Message& message, | |
116 RenderFrameHost* render_frame_host) { | |
117 DCHECK(render_frame_host); | |
118 scoped_refptr<JavaBridgeDispatcherHost> instance = | |
119 instances_[render_frame_host]; | |
120 bool handled = true; | |
121 IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcherHostManager, message) | |
122 IPC_MESSAGE_FORWARD_DELAY_REPLY( | |
123 JavaBridgeHostMsg_GetChannelHandle, | |
124 instance.get(), | |
125 JavaBridgeDispatcherHost::OnGetChannelHandle) | |
126 IPC_MESSAGE_UNHANDLED(handled = false) | |
127 IPC_END_MESSAGE_MAP() | |
128 return handled; | |
129 } | |
130 | |
135 void JavaBridgeDispatcherHostManager::JavaBoundObjectCreated( | 131 void JavaBridgeDispatcherHostManager::JavaBoundObjectCreated( |
136 const base::android::JavaRef<jobject>& object) { | 132 const base::android::JavaRef<jobject>& object) { |
137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
138 | 134 |
139 JNIEnv* env = base::android::AttachCurrentThread(); | 135 JNIEnv* env = base::android::AttachCurrentThread(); |
140 base::android::ScopedJavaLocalRef<jobject> retained_object_set = | 136 base::android::ScopedJavaLocalRef<jobject> retained_object_set = |
141 retained_object_set_.get(env); | 137 retained_object_set_.get(env); |
142 if (!retained_object_set.is_null()) { | 138 if (!retained_object_set.is_null()) { |
143 JNI_Java_HashSet_add(env, retained_object_set, object); | 139 JNI_Java_HashSet_add(env, retained_object_set, object); |
144 } | 140 } |
(...skipping 10 matching lines...) Expand all Loading... | |
155 JNI_Java_HashSet_remove(env, retained_object_set, object); | 151 JNI_Java_HashSet_remove(env, retained_object_set, object); |
156 } | 152 } |
157 } | 153 } |
158 | 154 |
159 void JavaBridgeDispatcherHostManager::SetAllowObjectContentsInspection( | 155 void JavaBridgeDispatcherHostManager::SetAllowObjectContentsInspection( |
160 bool allow) { | 156 bool allow) { |
161 allow_object_contents_inspection_ = allow; | 157 allow_object_contents_inspection_ = allow; |
162 } | 158 } |
163 | 159 |
164 } // namespace content | 160 } // namespace content |
OLD | NEW |