1 // Copyright 2014 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/child/navigator_connect/navigator_connect_provider.h"
7 #include "base/lazy_instance.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/task_runner_util.h"
10 #include "content/child/thread_safe_sender.h"
11 #include "content/child/webmessageportchannel_impl.h"
12 #include "content/common/navigator_connect_messages.h"
13 #include "content/public/common/navigator_connect_client.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
20 base::LazyInstance
<base::ThreadLocalPointer
<NavigatorConnectProvider
>>::Leaky
21 g_provider_tls
= LAZY_INSTANCE_INITIALIZER
;
23 NavigatorConnectProvider
* const kHasBeenDeleted
=
24 reinterpret_cast<NavigatorConnectProvider
*>(0x1);
26 int CurrentWorkerId() {
27 return WorkerTaskRunner::Instance()->CurrentWorkerId();
32 NavigatorConnectProvider::NavigatorConnectProvider(
33 ThreadSafeSender
* thread_safe_sender
,
34 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_loop
)
35 : thread_safe_sender_(thread_safe_sender
), main_loop_(main_loop
) {
36 g_provider_tls
.Pointer()->Set(this);
39 NavigatorConnectProvider::~NavigatorConnectProvider() {
40 g_provider_tls
.Pointer()->Set(kHasBeenDeleted
);
43 void NavigatorConnectProvider::connect(
44 const blink::WebURL
& target_url
,
45 const blink::WebString
& origin
,
46 blink::WebNavigatorConnectPortCallbacks
* callbacks
) {
47 int request_id
= requests_
.Add(callbacks
);
49 thread_safe_sender_
->Send(new NavigatorConnectHostMsg_Connect(
50 CurrentWorkerId(), request_id
,
51 NavigatorConnectClient(target_url
, GURL(origin
), MSG_ROUTING_NONE
)));
54 void NavigatorConnectProvider::OnMessageReceived(const IPC::Message
& msg
) {
56 IPC_BEGIN_MESSAGE_MAP(NavigatorConnectProvider
, msg
)
57 IPC_MESSAGE_HANDLER(NavigatorConnectMsg_ConnectResult
, OnConnectResult
)
58 IPC_MESSAGE_UNHANDLED(handled
= false)
60 DCHECK(handled
) << "Unhandled message:" << msg
.type();
63 NavigatorConnectProvider
* NavigatorConnectProvider::ThreadSpecificInstance(
64 ThreadSafeSender
* thread_safe_sender
,
65 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_loop
) {
66 if (g_provider_tls
.Pointer()->Get() == kHasBeenDeleted
) {
67 NOTREACHED() << "Re-instantiating TLS NavigatorConnectProvider.";
68 g_provider_tls
.Pointer()->Set(NULL
);
70 if (g_provider_tls
.Pointer()->Get())
71 return g_provider_tls
.Pointer()->Get();
73 NavigatorConnectProvider
* provider
=
74 new NavigatorConnectProvider(thread_safe_sender
, main_loop
);
75 if (WorkerTaskRunner::Instance()->CurrentWorkerId())
76 WorkerTaskRunner::Instance()->AddStopObserver(provider
);
80 void NavigatorConnectProvider::OnConnectResult(
83 const TransferredMessagePort
& message_port
,
84 int message_port_route_id
,
86 blink::WebNavigatorConnectPortCallbacks
* callbacks
=
87 requests_
.Lookup(request_id
);
91 WebMessagePortChannelImpl
* channel
= new WebMessagePortChannelImpl(
92 message_port_route_id
, message_port
, main_loop_
);
93 callbacks
->onSuccess(channel
);
97 requests_
.Remove(request_id
);
100 void NavigatorConnectProvider::OnWorkerRunLoopStopped() {
104 } // namespace content