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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/java/java_bridge_channel_host.h"
7 #include "base/atomicops.h"
8 #include "base/lazy_instance.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "content/common/java_bridge_messages.h"
13 using base::WaitableEvent
;
17 struct WaitableEventLazyInstanceTraits
18 : public base::DefaultLazyInstanceTraits
<WaitableEvent
> {
19 static WaitableEvent
* New(void* instance
) {
20 // Use placement new to initialize our instance in our preallocated space.
21 // The parenthesis is very important here to force POD type initialization.
22 return new (instance
) WaitableEvent(false, false);
25 base::LazyInstance
<WaitableEvent
, WaitableEventLazyInstanceTraits
> dummy_event
=
26 LAZY_INSTANCE_INITIALIZER
;
28 base::subtle::AtomicWord g_last_id
= 0;
31 JavaBridgeChannelHost::~JavaBridgeChannelHost() {
33 if (channel_handle_
.socket
.fd
> 0) {
34 close(channel_handle_
.socket
.fd
);
39 JavaBridgeChannelHost
* JavaBridgeChannelHost::GetJavaBridgeChannelHost(
41 base::MessageLoopProxy
* ipc_message_loop
) {
42 std::string
channel_name(base::StringPrintf("r%d.javabridge", renderer_id
));
43 // There's no need for a shutdown event here. If the browser is terminated
44 // while the JavaBridgeChannelHost is blocked on a synchronous IPC call, the
45 // renderer's shutdown event will cause the underlying channel to shut down,
46 // thus terminating the IPC call.
47 return static_cast<JavaBridgeChannelHost
*>(NPChannelBase::GetChannel(
49 IPC::Channel::MODE_SERVER
,
53 dummy_event
.Pointer()));
56 int JavaBridgeChannelHost::ThreadsafeGenerateRouteID() {
57 return base::subtle::NoBarrier_AtomicIncrement(&g_last_id
, 1);
60 int JavaBridgeChannelHost::GenerateRouteID() {
61 return ThreadsafeGenerateRouteID();
64 bool JavaBridgeChannelHost::Init(base::MessageLoopProxy
* ipc_message_loop
,
66 WaitableEvent
* shutdown_event
) {
67 if (!NPChannelBase::Init(ipc_message_loop
, create_pipe_now
, shutdown_event
)) {
71 // Finish populating our ChannelHandle.
73 // We take control of the FD for all session between this host and
74 // the corresponding renderers. We keep it open until this object
76 channel_handle_
.socket
.fd
= channel_
->TakeClientFileDescriptor();
82 bool JavaBridgeChannelHost::OnControlMessageReceived(
83 const IPC::Message
& message
) {
85 IPC_BEGIN_MESSAGE_MAP(JavaBridgeChannelHost
, message
)
86 IPC_MESSAGE_HANDLER(JavaBridgeMsg_GenerateRouteID
, OnGenerateRouteID
)
91 void JavaBridgeChannelHost::OnGenerateRouteID(int* route_id
) {
92 *route_id
= GenerateRouteID();
95 } // namespace content