| 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.h" | 5 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "content/browser/renderer_host/java/java_bridge_channel_host.h" | 10 #include "content/browser/renderer_host/java/java_bridge_channel_host.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 namespace { | 22 namespace { |
| 23 class JavaBridgeThread : public base::Thread { | 23 class JavaBridgeThread : public base::Thread { |
| 24 public: | 24 public: |
| 25 JavaBridgeThread() : base::Thread("JavaBridge") { | 25 JavaBridgeThread() : base::Thread("JavaBridge") { |
| 26 Start(); | 26 Start(); |
| 27 } | 27 } |
| 28 virtual ~JavaBridgeThread() { | 28 virtual ~JavaBridgeThread() { |
| 29 Stop(); | 29 Stop(); |
| 30 } | 30 } |
| 31 }; | 31 }; |
| 32 | |
| 33 void CleanUpStubs(const std::vector<base::WeakPtr<NPObjectStub> > & stubs) { | |
| 34 for (size_t i = 0; i < stubs.size(); ++i) { | |
| 35 if (stubs[i]) { | |
| 36 stubs[i]->DeleteSoon(); | |
| 37 } | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 base::LazyInstance<JavaBridgeThread> g_background_thread = | 32 base::LazyInstance<JavaBridgeThread> g_background_thread = |
| 42 LAZY_INSTANCE_INITIALIZER; | 33 LAZY_INSTANCE_INITIALIZER; |
| 43 } // namespace | 34 } // namespace |
| 44 | 35 |
| 45 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( | 36 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( |
| 46 RenderViewHost* render_view_host) | 37 RenderViewHost* render_view_host) |
| 47 : RenderViewHostObserver(render_view_host), | 38 : RenderViewHostObserver(render_view_host), |
| 48 is_renderer_initialized_(false) { | 39 is_renderer_initialized_(false) { |
| 49 } | 40 } |
| 50 | 41 |
| 51 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { | 42 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { |
| 52 g_background_thread.Get().message_loop()->PostTask( | |
| 53 FROM_HERE, | |
| 54 base::Bind(&CleanUpStubs, stubs_)); | |
| 55 } | 43 } |
| 56 | 44 |
| 57 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, | 45 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, |
| 58 NPObject* object) { | 46 NPObject* object) { |
| 59 NPVariant_Param variant_param; | 47 NPVariant_Param variant_param; |
| 60 CreateNPVariantParam(object, &variant_param); | 48 CreateNPVariantParam(object, &variant_param); |
| 61 | 49 |
| 62 if (!is_renderer_initialized_) { | 50 if (!is_renderer_initialized_) { |
| 63 is_renderer_initialized_ = true; | 51 is_renderer_initialized_ = true; |
| 64 Send(new JavaBridgeMsg_Init(routing_id())); | 52 Send(new JavaBridgeMsg_Init(routing_id())); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 126 |
| 139 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, | 127 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, |
| 140 int route_id) { | 128 int route_id) { |
| 141 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); | 129 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); |
| 142 if (!channel_) { | 130 if (!channel_) { |
| 143 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( | 131 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( |
| 144 render_view_host()->GetProcess()->GetID(), | 132 render_view_host()->GetProcess()->GetID(), |
| 145 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 133 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 146 } | 134 } |
| 147 | 135 |
| 148 // In a typical scenario, the lifetime of each NPObjectStub is governed by | 136 // NPObjectStub takes a ref to the NPObject. The lifetime of the NPObjectStub |
| 149 // that of the NPObjectProxy in the renderer, via the channel. However, | 137 // is governed by that of the NPObjectProxy in the renderer, via the channel. |
| 150 // we cannot guaranteed that the renderer always terminates cleanly | |
| 151 // (crashes / sometimes just unavoidable). We keep a weak reference to | |
| 152 // it now and schedule a delete on it when this host is getting deleted. | |
| 153 | |
| 154 // Pass 0 for the containing window, as it's only used by plugins to pump the | 138 // Pass 0 for the containing window, as it's only used by plugins to pump the |
| 155 // window message queue when a method on a renderer-side object causes a | 139 // window message queue when a method on a renderer-side object causes a |
| 156 // dialog to be displayed, and the Java Bridge does not need this | 140 // dialog to be displayed, and the Java Bridge does not need this |
| 157 // functionality. The page URL is also not required. | 141 // functionality. The page URL is also not required. |
| 158 stubs_.push_back( | 142 new NPObjectStub(object, channel_, route_id, 0, GURL()); |
| 159 (new NPObjectStub(object, channel_, route_id, 0, GURL()))->AsWeakPtr()); | |
| 160 | |
| 161 // The NPObjectStub takes a reference to the NPObject. Release the ref added | 143 // The NPObjectStub takes a reference to the NPObject. Release the ref added |
| 162 // in CreateNPVariantParam(). | 144 // in CreateNPVariantParam(). |
| 163 WebKit::WebBindings::releaseObject(object); | 145 WebKit::WebBindings::releaseObject(object); |
| 164 } | 146 } |
| 165 | 147 |
| 166 } // namespace content | 148 } // namespace content |
| OLD | NEW |