| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/navigator_connect/navigator_connect_dispatcher_host.h" | 5 #include "content/browser/navigator_connect/navigator_connect_context_impl.h" |
| 6 | 6 |
| 7 #include "content/browser/message_port_service.h" | 7 #include "content/browser/message_port_service.h" |
| 8 #include "content/browser/navigator_connect/navigator_connect_context.h" | 8 #include "content/public/browser/navigator_connect_service_factory.h" |
| 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 9 #include "content/public/common/navigator_connect_client.h" |
| 10 #include "content/common/navigator_connect_messages.h" | |
| 11 #include "content/common/navigator_connect_types.h" | |
| 12 | 10 |
| 13 namespace content { | 11 namespace content { |
| 14 | 12 |
| 15 NavigatorConnectDispatcherHost::NavigatorConnectDispatcherHost( | 13 NavigatorConnectContextImpl::NavigatorConnectContextImpl() { |
| 16 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, | |
| 17 const scoped_refptr<NavigatorConnectContext>& navigator_connect_context) | |
| 18 : BrowserMessageFilter(NavigatorConnectMsgStart), | |
| 19 service_worker_context_(service_worker_context), | |
| 20 navigator_connect_context_(navigator_connect_context) { | |
| 21 } | 14 } |
| 22 | 15 |
| 23 NavigatorConnectDispatcherHost::~NavigatorConnectDispatcherHost() { | 16 NavigatorConnectContextImpl::~NavigatorConnectContextImpl() { |
| 24 } | 17 } |
| 25 | 18 |
| 26 bool NavigatorConnectDispatcherHost::OnMessageReceived( | 19 void NavigatorConnectContextImpl::AddFactory( |
| 27 const IPC::Message& message) { | 20 scoped_ptr<NavigatorConnectServiceFactory> factory) { |
| 28 bool handled = true; | 21 service_factories_.push_back(factory.release()); |
| 29 IPC_BEGIN_MESSAGE_MAP(NavigatorConnectDispatcherHost, message) | |
| 30 IPC_MESSAGE_HANDLER(NavigatorConnectHostMsg_Connect, OnConnect) | |
| 31 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 32 IPC_END_MESSAGE_MAP() | |
| 33 return handled; | |
| 34 } | 22 } |
| 35 | 23 |
| 36 void NavigatorConnectDispatcherHost::OnConnect( | 24 void NavigatorConnectContextImpl::Connect(const NavigatorConnectClient& client, |
| 37 int thread_id, | 25 const ConnectCallback& callback) { |
| 38 int request_id, | |
| 39 const CrossOriginServiceWorkerClient& client) { | |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 41 | |
| 42 // Hold messages for port while setting up connection. | 26 // Hold messages for port while setting up connection. |
| 43 MessagePortService::GetInstance()->HoldMessages(client.message_port_id); | 27 MessagePortService::GetInstance()->HoldMessages(client.message_port_id); |
| 44 | 28 |
| 45 // Find the right service worker to service this connection. | 29 // Find factory to handle request, more recently added factories should take |
| 46 service_worker_context_->context()->storage()->FindRegistrationForDocument( | 30 // priority as per comment at NavigatorConnectContext::AddFactory.. |
| 47 client.target_url, | 31 NavigatorConnectServiceFactory* factory = nullptr; |
| 48 base::Bind(&NavigatorConnectDispatcherHost::GotServiceWorkerRegistration, | 32 for (auto it = service_factories_.rbegin(); it != service_factories_.rend(); |
| 49 this, thread_id, request_id, client)); | 33 ++it) { |
| 50 } | 34 if ((*it)->HandlesUrl(client.target_url)) { |
| 35 factory = *it; |
| 36 break; |
| 37 } |
| 38 } |
| 51 | 39 |
| 52 void NavigatorConnectDispatcherHost::GotServiceWorkerRegistration( | 40 if (!factory) { |
| 53 int thread_id, | 41 // No factories found. |
| 54 int request_id, | 42 // Close port since connection failed. |
| 55 const CrossOriginServiceWorkerClient& client, | 43 MessagePortService::GetInstance()->ClosePort(client.message_port_id); |
| 56 ServiceWorkerStatusCode status, | 44 callback.Run(false); |
| 57 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 59 | |
| 60 if (status != SERVICE_WORKER_OK) { | |
| 61 // No service worker found, reject connection attempt. | |
| 62 OnConnectResult(thread_id, request_id, client, registration, status, false); | |
| 63 return; | 45 return; |
| 64 } | 46 } |
| 65 | 47 |
| 66 ServiceWorkerVersion* active_version = registration->active_version(); | 48 // Actually initiate connection. |
| 67 if (!active_version) { | 49 factory->Connect( |
| 68 // No active version, reject connection attempt. | 50 client, base::Bind(&NavigatorConnectContextImpl::OnConnectResult, this, |
| 69 OnConnectResult(thread_id, request_id, client, registration, status, false); | 51 client, callback)); |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 active_version->DispatchCrossOriginConnectEvent( | |
| 74 base::Bind(&NavigatorConnectDispatcherHost::OnConnectResult, this, | |
| 75 thread_id, request_id, client, registration), | |
| 76 client); | |
| 77 } | 52 } |
| 78 | 53 |
| 79 void NavigatorConnectDispatcherHost::OnConnectResult( | 54 void NavigatorConnectContextImpl::OnConnectResult( |
| 80 int thread_id, | 55 const NavigatorConnectClient& client, |
| 81 int request_id, | 56 const ConnectCallback& callback, |
| 82 const CrossOriginServiceWorkerClient& client, | 57 MessagePortDelegate* delegate) { |
| 83 const scoped_refptr<ServiceWorkerRegistration>& registration, | 58 if (delegate) { |
| 84 ServiceWorkerStatusCode status, | 59 MessagePortService::GetInstance()->UpdateMessagePort( |
| 85 bool accept_connection) { | 60 client.message_port_id, delegate, client.message_port_id); |
| 86 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 61 MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); |
| 87 | 62 callback.Run(true); |
| 88 if (status != SERVICE_WORKER_OK || !accept_connection) { | 63 } else { |
| 89 // Close port since connection failed. | 64 // Close port since connection failed. |
| 90 MessagePortService::GetInstance()->ClosePort(client.message_port_id); | 65 MessagePortService::GetInstance()->ClosePort(client.message_port_id); |
| 91 Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, false)); | 66 callback.Run(false); |
| 92 return; | |
| 93 } | 67 } |
| 94 | |
| 95 // Register connection and post back result. | |
| 96 navigator_connect_context_->RegisterConnection(client, registration); | |
| 97 Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, true)); | |
| 98 } | 68 } |
| 99 | 69 |
| 100 } // namespace content | 70 } // namespace content |
| OLD | NEW |