OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/renderer_host/browser_render_process_host.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/threading/thread.h" |
9 #include "content/browser/renderer_host/java/java_bridge_channel_host.h" | 10 #include "content/browser/renderer_host/java/java_bridge_channel_host.h" |
10 #include "content/browser/renderer_host/render_view_host.h" | 11 #include "content/browser/renderer_host/render_view_host.h" |
11 #include "content/common/child_process.h" | 12 #include "content/common/child_process.h" |
12 #include "content/common/java_bridge_messages.h" | 13 #include "content/common/java_bridge_messages.h" |
13 #include "content/common/npobject_stub.h" | 14 #include "content/common/npobject_stub.h" |
14 #include "content/common/npobject_util.h" // For CreateNPVariantParam() | 15 #include "content/common/npobject_util.h" // For CreateNPVariantParam() |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
17 | 18 |
18 using content::BrowserThread; | 19 using content::BrowserThread; |
19 | 20 |
| 21 namespace { |
| 22 class JavaBridgeThread : public base::Thread { |
| 23 public: |
| 24 JavaBridgeThread() : base::Thread("JavaBridge") { |
| 25 Start(); |
| 26 } |
| 27 virtual ~JavaBridgeThread() { |
| 28 Stop(); |
| 29 } |
| 30 }; |
| 31 base::LazyInstance<JavaBridgeThread> g_background_thread = |
| 32 LAZY_INSTANCE_INITIALIZER; |
| 33 } // namespace |
| 34 |
20 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( | 35 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( |
21 RenderViewHost* render_view_host) | 36 RenderViewHost* render_view_host) |
22 : RenderViewHostObserver(render_view_host), | 37 : RenderViewHostObserver(render_view_host), |
23 is_renderer_initialized_(false) { | 38 is_renderer_initialized_(false) { |
24 } | 39 } |
25 | 40 |
26 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { | 41 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { |
27 } | 42 } |
28 | 43 |
29 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, | 44 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, |
(...skipping 25 matching lines...) Expand all Loading... |
55 bool handled = true; | 70 bool handled = true; |
56 IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcherHost, msg) | 71 IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcherHost, msg) |
57 IPC_MESSAGE_HANDLER_DELAY_REPLY(JavaBridgeHostMsg_GetChannelHandle, | 72 IPC_MESSAGE_HANDLER_DELAY_REPLY(JavaBridgeHostMsg_GetChannelHandle, |
58 OnGetChannelHandle) | 73 OnGetChannelHandle) |
59 IPC_MESSAGE_UNHANDLED(handled = false) | 74 IPC_MESSAGE_UNHANDLED(handled = false) |
60 IPC_END_MESSAGE_MAP() | 75 IPC_END_MESSAGE_MAP() |
61 return handled; | 76 return handled; |
62 } | 77 } |
63 | 78 |
64 void JavaBridgeDispatcherHost::OnGetChannelHandle(IPC::Message* reply_msg) { | 79 void JavaBridgeDispatcherHost::OnGetChannelHandle(IPC::Message* reply_msg) { |
65 if (RenderProcessHost::run_renderer_in_process()) { | 80 g_background_thread.Get().message_loop()->PostTask( |
66 // TODO(steveblock): Fix Java Bridge with in-process renderer. See | |
67 // http://code.google.com/p/chromium/issues/detail?id=106838 | |
68 CHECK(false) << "Java Bridge does not support in-process renderer"; | |
69 } | |
70 BrowserThread::PostTask( | |
71 BrowserThread::WEBKIT, | |
72 FROM_HERE, | 81 FROM_HERE, |
73 base::Bind(&JavaBridgeDispatcherHost::GetChannelHandle, this, reply_msg)); | 82 base::Bind(&JavaBridgeDispatcherHost::GetChannelHandle, this, reply_msg)); |
74 } | 83 } |
75 | 84 |
76 void JavaBridgeDispatcherHost::GetChannelHandle(IPC::Message* reply_msg) { | 85 void JavaBridgeDispatcherHost::GetChannelHandle(IPC::Message* reply_msg) { |
77 // The channel creates the channel handle based on the renderer ID we passed | 86 // The channel creates the channel handle based on the renderer ID we passed |
78 // to GetJavaBridgeChannelHost() and, on POSIX, the file descriptor used by | 87 // to GetJavaBridgeChannelHost() and, on POSIX, the file descriptor used by |
79 // the underlying channel. | 88 // the underlying channel. |
80 JavaBridgeHostMsg_GetChannelHandle::WriteReplyParams( | 89 JavaBridgeHostMsg_GetChannelHandle::WriteReplyParams( |
81 reply_msg, | 90 reply_msg, |
82 channel_->channel_handle()); | 91 channel_->channel_handle()); |
83 Send(reply_msg); | 92 Send(reply_msg); |
84 } | 93 } |
85 | 94 |
86 void JavaBridgeDispatcherHost::CreateNPVariantParam(NPObject* object, | 95 void JavaBridgeDispatcherHost::CreateNPVariantParam(NPObject* object, |
87 NPVariant_Param* param) { | 96 NPVariant_Param* param) { |
88 // The JavaBridgeChannelHost needs to be created on the WEBKIT thread, as | 97 // The JavaBridgeChannelHost needs to be created on the background thread, as |
89 // that is where Java objects will live, and CreateNPVariantParam() needs the | 98 // that is where Java objects will live, and CreateNPVariantParam() needs the |
90 // channel to create the NPObjectStub. To avoid blocking here until the | 99 // channel to create the NPObjectStub. To avoid blocking here until the |
91 // channel is ready, create the NPVariant_Param by hand, then post a message | 100 // channel is ready, create the NPVariant_Param by hand, then post a message |
92 // to the WEBKIT thread to set up the channel and create the corresponding | 101 // to the background thread to set up the channel and create the corresponding |
93 // NPObjectStub. Post that message before doing any IPC, to make sure that | 102 // NPObjectStub. Post that message before doing any IPC, to make sure that |
94 // the channel and object proxies are ready before responses are received | 103 // the channel and object proxies are ready before responses are received |
95 // from the renderer. | 104 // from the renderer. |
96 | 105 |
97 // Create an NPVariantParam suitable for serialization over IPC from our | 106 // Create an NPVariantParam suitable for serialization over IPC from our |
98 // NPVariant. See CreateNPVariantParam() in npobject_utils. | 107 // NPVariant. See CreateNPVariantParam() in npobject_utils. |
99 param->type = NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID; | 108 param->type = NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID; |
100 int route_id = JavaBridgeChannelHost::ThreadsafeGenerateRouteID(); | 109 int route_id = JavaBridgeChannelHost::ThreadsafeGenerateRouteID(); |
101 param->npobject_routing_id = route_id; | 110 param->npobject_routing_id = route_id; |
102 | 111 |
103 WebKit::WebBindings::retainObject(object); | 112 WebKit::WebBindings::retainObject(object); |
104 BrowserThread::PostTask( | 113 g_background_thread.Get().message_loop()->PostTask( |
105 BrowserThread::WEBKIT, | |
106 FROM_HERE, | 114 FROM_HERE, |
107 base::Bind(&JavaBridgeDispatcherHost::CreateObjectStub, this, object, | 115 base::Bind(&JavaBridgeDispatcherHost::CreateObjectStub, this, object, |
108 route_id)); | 116 route_id)); |
109 } | 117 } |
110 | 118 |
111 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, | 119 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, |
112 int route_id) { | 120 int route_id) { |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 121 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); |
114 | |
115 if (!channel_) { | 122 if (!channel_) { |
116 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( | 123 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( |
117 render_view_host()->process()->id(), | 124 render_view_host()->process()->id(), |
118 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 125 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
119 } | 126 } |
120 | 127 |
| 128 // NPObjectStub takes a ref to the NPObject. The lifetime of the NPObjectStub |
| 129 // is governed by that of the NPObjectProxy in the renderer, via the channel. |
121 // We don't need the containing window or the page URL, as we don't do | 130 // We don't need the containing window or the page URL, as we don't do |
122 // re-entrant sync IPC. | 131 // re-entrant sync IPC. |
123 new NPObjectStub(object, channel_, route_id, 0, GURL()); | 132 new NPObjectStub(object, channel_, route_id, 0, GURL()); |
124 // The NPObjectStub takes a reference to the NPObject. Release the ref added | 133 // The NPObjectStub takes a reference to the NPObject. Release the ref added |
125 // in CreateNPVariantParam(). | 134 // in CreateNPVariantParam(). |
126 WebKit::WebBindings::releaseObject(object); | 135 WebKit::WebBindings::releaseObject(object); |
127 } | 136 } |
OLD | NEW |