Chromium Code Reviews| 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 "content/child/thread_safe_sender.h" | |
| 9 #include "content/child/webmessageportchannel_impl.h" | |
| 10 #include "content/common/navigator_connect_messages.h" | |
| 11 #include "third_party/WebKit/public/platform/WebCallbacks.h" | |
| 12 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 base::LazyInstance<base::ThreadLocalPointer<NavigatorConnectProvider>>::Leaky | |
| 19 g_provider_tls = LAZY_INSTANCE_INITIALIZER; | |
| 20 | |
| 21 NavigatorConnectProvider* const kHasBeenDeleted = | |
| 22 reinterpret_cast<NavigatorConnectProvider*>(0x1); | |
| 23 | |
| 24 int CurrentWorkerId() { | |
| 25 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 NavigatorConnectProvider::NavigatorConnectProvider( | |
| 31 ThreadSafeSender* thread_safe_sender) | |
| 32 : thread_safe_sender_(thread_safe_sender) { | |
| 33 g_provider_tls.Pointer()->Set(this); | |
| 34 } | |
| 35 | |
| 36 NavigatorConnectProvider::~NavigatorConnectProvider() { | |
| 37 g_provider_tls.Pointer()->Set(kHasBeenDeleted); | |
| 38 } | |
| 39 | |
| 40 void NavigatorConnectProvider::connect( | |
| 41 const blink::WebURL& targetUrl, | |
| 42 blink::WebMessagePortChannel* port, | |
| 43 blink::WebCallbacks<void, void>* callbacks) { | |
| 44 int request_id = requests_.Add(callbacks); | |
| 45 | |
| 46 // Extract the port ID. | |
| 47 WebMessagePortChannelImpl* webchannel = | |
| 48 static_cast<WebMessagePortChannelImpl*>(port); | |
| 49 int message_port_id = webchannel->message_port_id(); | |
| 50 DCHECK(message_port_id != MSG_ROUTING_NONE); | |
| 51 webchannel->QueueMessages(); | |
| 52 | |
| 53 thread_safe_sender_->Send(new NavigatorConnectHostMsg_Connect( | |
| 54 CurrentWorkerId(), request_id, targetUrl, message_port_id)); | |
| 55 } | |
| 56 | |
| 57 void NavigatorConnectProvider::OnMessageReceived(const IPC::Message& msg) { | |
| 58 bool handled = true; | |
| 59 IPC_BEGIN_MESSAGE_MAP(NavigatorConnectProvider, msg) | |
| 60 IPC_MESSAGE_HANDLER(NavigatorConnectMsg_ConnectResult, OnConnectResult) | |
| 61 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 62 IPC_END_MESSAGE_MAP() | |
| 63 DCHECK(handled) << "Unhandled message:" << msg.type(); | |
| 64 } | |
| 65 | |
| 66 void NavigatorConnectProvider::OnConnectResult(int thread_id, | |
| 67 int request_id, | |
| 68 bool allow_connect) { | |
| 69 ConnectCallback* callbacks = requests_.Lookup(request_id); | |
| 70 DCHECK(callbacks); | |
| 71 | |
| 72 if (allow_connect) { | |
| 73 callbacks->onSuccess(); | |
| 74 } else { | |
| 75 callbacks->onError(); | |
| 76 } | |
| 77 requests_.Remove(request_id); | |
| 78 } | |
| 79 | |
| 80 NavigatorConnectProvider* NavigatorConnectProvider::ThreadSpecificInstance( | |
|
scheib
2014/12/05 18:59:35
Keep definitions matching the declaration order.
Marijn Kruisselbrink
2014/12/09 23:23:31
Done.
| |
| 81 ThreadSafeSender* thread_safe_sender) { | |
| 82 if (g_provider_tls.Pointer()->Get() == kHasBeenDeleted) { | |
| 83 NOTREACHED() << "Re-instantiating TLS NavigatorConnectProvider."; | |
| 84 g_provider_tls.Pointer()->Set(NULL); | |
| 85 } | |
| 86 if (g_provider_tls.Pointer()->Get()) | |
| 87 return g_provider_tls.Pointer()->Get(); | |
| 88 | |
| 89 NavigatorConnectProvider* provider = | |
| 90 new NavigatorConnectProvider(thread_safe_sender); | |
| 91 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
| 92 WorkerTaskRunner::Instance()->AddStopObserver(provider); | |
| 93 return provider; | |
| 94 } | |
| 95 | |
| 96 void NavigatorConnectProvider::OnWorkerRunLoopStopped() { | |
| 97 delete this; | |
| 98 } | |
| 99 | |
| 100 } // namespace content | |
| OLD | NEW |