| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/child/navigator_connect/navigator_connect_provider.h" | |
| 6 | |
| 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" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 base::LazyInstance<base::ThreadLocalPointer<NavigatorConnectProvider>>::Leaky | |
| 21 g_provider_tls = LAZY_INSTANCE_INITIALIZER; | |
| 22 | |
| 23 NavigatorConnectProvider* const kHasBeenDeleted = | |
| 24 reinterpret_cast<NavigatorConnectProvider*>(0x1); | |
| 25 | |
| 26 int CurrentWorkerId() { | |
| 27 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 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); | |
| 37 } | |
| 38 | |
| 39 NavigatorConnectProvider::~NavigatorConnectProvider() { | |
| 40 g_provider_tls.Pointer()->Set(kHasBeenDeleted); | |
| 41 } | |
| 42 | |
| 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); | |
| 48 | |
| 49 thread_safe_sender_->Send(new NavigatorConnectHostMsg_Connect( | |
| 50 CurrentWorkerId(), request_id, | |
| 51 NavigatorConnectClient(target_url, GURL(origin), MSG_ROUTING_NONE))); | |
| 52 } | |
| 53 | |
| 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(); | |
| 61 } | |
| 62 | |
| 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); | |
| 69 } | |
| 70 if (g_provider_tls.Pointer()->Get()) | |
| 71 return g_provider_tls.Pointer()->Get(); | |
| 72 | |
| 73 NavigatorConnectProvider* provider = | |
| 74 new NavigatorConnectProvider(thread_safe_sender, main_loop); | |
| 75 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
| 76 WorkerTaskRunner::Instance()->AddStopObserver(provider); | |
| 77 return provider; | |
| 78 } | |
| 79 | |
| 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); | |
| 89 | |
| 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(); | |
| 96 } | |
| 97 requests_.Remove(request_id); | |
| 98 } | |
| 99 | |
| 100 void NavigatorConnectProvider::OnWorkerRunLoopStopped() { | |
| 101 delete this; | |
| 102 } | |
| 103 | |
| 104 } // namespace content | |
| OLD | NEW |