Remove unnecessary casts for WebContentsImpl::GetRenderViewHost.
[chromium-blink-merge.git] / content / child / navigator_connect / navigator_connect_provider.cc
blob78c3fdad7fd6a81886d1d7b5baa83ed9d288e159
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"
16 namespace content {
18 namespace {
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();
30 } // namespace
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) {
55 bool handled = true;
56 IPC_BEGIN_MESSAGE_MAP(NavigatorConnectProvider, msg)
57 IPC_MESSAGE_HANDLER(NavigatorConnectMsg_ConnectResult, OnConnectResult)
58 IPC_MESSAGE_UNHANDLED(handled = false)
59 IPC_END_MESSAGE_MAP()
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);
77 return provider;
80 void NavigatorConnectProvider::OnConnectResult(
81 int thread_id,
82 int request_id,
83 const TransferredMessagePort& message_port,
84 int message_port_route_id,
85 bool allow_connect) {
86 blink::WebNavigatorConnectPortCallbacks* callbacks =
87 requests_.Lookup(request_id);
88 DCHECK(callbacks);
90 if (allow_connect) {
91 WebMessagePortChannelImpl* channel = new WebMessagePortChannelImpl(
92 message_port_route_id, message_port, main_loop_);
93 callbacks->onSuccess(channel);
94 } else {
95 callbacks->onError();
97 requests_.Remove(request_id);
100 void NavigatorConnectProvider::OnWorkerRunLoopStopped() {
101 delete this;
104 } // namespace content