| 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); |
| 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 if (!instances_.count(render_frame_host)) |
| 119 return false; |
| 120 scoped_refptr<JavaBridgeDispatcherHost> instance = |
| 121 instances_[render_frame_host]; |
| 122 bool handled = true; |
| 123 IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcherHostManager, message) |
| 124 IPC_MESSAGE_FORWARD_DELAY_REPLY( |
| 125 JavaBridgeHostMsg_GetChannelHandle, |
| 126 instance.get(), |
| 127 JavaBridgeDispatcherHost::OnGetChannelHandle) |
| 128 IPC_MESSAGE_UNHANDLED(handled = false) |
| 129 IPC_END_MESSAGE_MAP() |
| 130 return handled; |
| 131 } |
| 132 |
| 135 void JavaBridgeDispatcherHostManager::JavaBoundObjectCreated( | 133 void JavaBridgeDispatcherHostManager::JavaBoundObjectCreated( |
| 136 const base::android::JavaRef<jobject>& object) { | 134 const base::android::JavaRef<jobject>& object) { |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 138 | 136 |
| 139 JNIEnv* env = base::android::AttachCurrentThread(); | 137 JNIEnv* env = base::android::AttachCurrentThread(); |
| 140 base::android::ScopedJavaLocalRef<jobject> retained_object_set = | 138 base::android::ScopedJavaLocalRef<jobject> retained_object_set = |
| 141 retained_object_set_.get(env); | 139 retained_object_set_.get(env); |
| 142 if (!retained_object_set.is_null()) { | 140 if (!retained_object_set.is_null()) { |
| 143 JNI_Java_HashSet_add(env, retained_object_set, object); | 141 JNI_Java_HashSet_add(env, retained_object_set, object); |
| 144 } | 142 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 155 JNI_Java_HashSet_remove(env, retained_object_set, object); | 153 JNI_Java_HashSet_remove(env, retained_object_set, object); |
| 156 } | 154 } |
| 157 } | 155 } |
| 158 | 156 |
| 159 void JavaBridgeDispatcherHostManager::SetAllowObjectContentsInspection( | 157 void JavaBridgeDispatcherHostManager::SetAllowObjectContentsInspection( |
| 160 bool allow) { | 158 bool allow) { |
| 161 allow_object_contents_inspection_ = allow; | 159 allow_object_contents_inspection_ = allow; |
| 162 } | 160 } |
| 163 | 161 |
| 164 } // namespace content | 162 } // namespace content |
| OLD | NEW |