• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager.h"
6 
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_helper.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/browser/renderer_host/java/java_bound_object.h"
15 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host.h"
16 #include "content/common/android/hash_set.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_delegate.h"
20 #include "third_party/WebKit/public/web/WebBindings.h"
21 
22 namespace content {
23 
JavaBridgeDispatcherHostManager(WebContents * web_contents)24 JavaBridgeDispatcherHostManager::JavaBridgeDispatcherHostManager(
25     WebContents* web_contents)
26     : WebContentsObserver(web_contents) {
27 }
28 
~JavaBridgeDispatcherHostManager()29 JavaBridgeDispatcherHostManager::~JavaBridgeDispatcherHostManager() {
30   for (ObjectMap::iterator iter = objects_.begin(); iter != objects_.end();
31       ++iter) {
32     blink::WebBindings::releaseObject(iter->second);
33   }
34   DCHECK_EQ(0U, instances_.size());
35 }
36 
AddNamedObject(const base::string16 & name,NPObject * object)37 void JavaBridgeDispatcherHostManager::AddNamedObject(const base::string16& name,
38                                                      NPObject* object) {
39   // Record this object in a map so that we can add it into RenderViewHosts
40   // created later. The JavaBridgeDispatcherHost instances will take a
41   // reference to the object, but we take one too, because this method can be
42   // called before there are any such instances.
43   blink::WebBindings::retainObject(object);
44   objects_[name] = object;
45 
46   for (InstanceMap::iterator iter = instances_.begin();
47       iter != instances_.end(); ++iter) {
48     iter->second->AddNamedObject(name, object);
49   }
50 }
51 
SetRetainedObjectSet(const JavaObjectWeakGlobalRef & retained_object_set)52 void JavaBridgeDispatcherHostManager::SetRetainedObjectSet(
53     const JavaObjectWeakGlobalRef& retained_object_set) {
54   // It's an error to replace the retained_object_set_ after it's been set,
55   // so we check that it hasn't already been here.
56   // TODO(benm): It'd be better to pass the set in the constructor to avoid
57   // the chance of this happening; but that's tricky as this get's constructed
58   // before ContentViewCore (which owns the set). Best solution may be to move
59   // ownership of the JavaBridgerDispatchHostManager from WebContents to
60   // ContentViewCore?
61   JNIEnv* env = base::android::AttachCurrentThread();
62   base::android::ScopedJavaLocalRef<jobject> new_retained_object_set =
63       retained_object_set.get(env);
64   base::android::ScopedJavaLocalRef<jobject> current_retained_object_set =
65       retained_object_set_.get(env);
66   if (!env->IsSameObject(new_retained_object_set.obj(),
67                          current_retained_object_set.obj())) {
68     DCHECK(current_retained_object_set.is_null());
69     retained_object_set_ = retained_object_set;
70   }
71 }
72 
RemoveNamedObject(const base::string16 & name)73 void JavaBridgeDispatcherHostManager::RemoveNamedObject(
74     const base::string16& name) {
75   ObjectMap::iterator iter = objects_.find(name);
76   if (iter == objects_.end()) {
77     return;
78   }
79 
80   blink::WebBindings::releaseObject(iter->second);
81   objects_.erase(iter);
82 
83   for (InstanceMap::iterator iter = instances_.begin();
84       iter != instances_.end(); ++iter) {
85     iter->second->RemoveNamedObject(name);
86   }
87 }
88 
OnGetChannelHandle(RenderViewHost * render_view_host,IPC::Message * reply_msg)89 void JavaBridgeDispatcherHostManager::OnGetChannelHandle(
90     RenderViewHost* render_view_host, IPC::Message* reply_msg) {
91   instances_[render_view_host]->OnGetChannelHandle(reply_msg);
92 }
93 
RenderViewCreated(RenderViewHost * render_view_host)94 void JavaBridgeDispatcherHostManager::RenderViewCreated(
95     RenderViewHost* render_view_host) {
96   // Creates a JavaBridgeDispatcherHost for the specified RenderViewHost and
97   // adds all currently registered named objects to the new instance.
98   scoped_refptr<JavaBridgeDispatcherHost> instance =
99       new JavaBridgeDispatcherHost(render_view_host);
100 
101   for (ObjectMap::const_iterator iter = objects_.begin();
102       iter != objects_.end(); ++iter) {
103     instance->AddNamedObject(iter->first, iter->second);
104   }
105 
106   instances_[render_view_host] = instance;
107 }
108 
RenderViewDeleted(RenderViewHost * render_view_host)109 void JavaBridgeDispatcherHostManager::RenderViewDeleted(
110     RenderViewHost* render_view_host) {
111   if (!instances_.count(render_view_host))  // Needed for tests.
112     return;
113   instances_[render_view_host]->RenderViewDeleted();
114   instances_.erase(render_view_host);
115 }
116 
DocumentAvailableInMainFrame()117 void JavaBridgeDispatcherHostManager::DocumentAvailableInMainFrame() {
118   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
119   // Called when the window object has been cleared in the main frame.
120   JNIEnv* env = base::android::AttachCurrentThread();
121   base::android::ScopedJavaLocalRef<jobject> retained_object_set =
122       retained_object_set_.get(env);
123   if (!retained_object_set.is_null()) {
124     JNI_Java_HashSet_clear(env, retained_object_set);
125 
126     // We also need to add back the named objects we have so far as they
127     // should survive navigations.
128     ObjectMap::iterator it = objects_.begin();
129     for (; it != objects_.end(); ++it) {
130       JNI_Java_HashSet_add(env, retained_object_set,
131                            JavaBoundObject::GetJavaObject(it->second));
132     }
133   }
134 }
135 
JavaBoundObjectCreated(const base::android::JavaRef<jobject> & object)136 void JavaBridgeDispatcherHostManager::JavaBoundObjectCreated(
137     const base::android::JavaRef<jobject>& object) {
138   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
139 
140   JNIEnv* env = base::android::AttachCurrentThread();
141   base::android::ScopedJavaLocalRef<jobject> retained_object_set =
142       retained_object_set_.get(env);
143   if (!retained_object_set.is_null()) {
144     JNI_Java_HashSet_add(env, retained_object_set, object);
145   }
146 }
147 
JavaBoundObjectDestroyed(const base::android::JavaRef<jobject> & object)148 void JavaBridgeDispatcherHostManager::JavaBoundObjectDestroyed(
149     const base::android::JavaRef<jobject>& object) {
150   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 
152   JNIEnv* env = base::android::AttachCurrentThread();
153   base::android::ScopedJavaLocalRef<jobject> retained_object_set =
154       retained_object_set_.get(env);
155   if (!retained_object_set.is_null()) {
156     JNI_Java_HashSet_remove(env, retained_object_set, object);
157   }
158 }
159 
AddMessageToConsole(int32 level,const char * message)160 void JavaBridgeDispatcherHostManager::AddMessageToConsole(
161     int32 level,
162     const char* message) {
163   WebContentsDelegate* delegate = web_contents()->GetDelegate();
164   if (delegate)
165     delegate->AddMessageToConsole(
166         web_contents(), level, ASCIIToUTF16(message), 0, ASCIIToUTF16(""));
167 }
168 
169 }  // namespace content
170