Index: content/browser/navigator_connect/navigator_connect_context_impl.cc |
diff --git a/content/browser/navigator_connect/navigator_connect_dispatcher_host.cc b/content/browser/navigator_connect/navigator_connect_context_impl.cc |
similarity index 16% |
copy from content/browser/navigator_connect/navigator_connect_dispatcher_host.cc |
copy to content/browser/navigator_connect/navigator_connect_context_impl.cc |
index 3a4ae29562e0197788be10a119cf2716f8c79424..91d8874647150eb5c6f37d656cbba3aba5df772c 100644 |
--- a/content/browser/navigator_connect/navigator_connect_dispatcher_host.cc |
+++ b/content/browser/navigator_connect/navigator_connect_context_impl.cc |
@@ -2,99 +2,69 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "content/browser/navigator_connect/navigator_connect_dispatcher_host.h" |
+#include "content/browser/navigator_connect/navigator_connect_context_impl.h" |
#include "content/browser/message_port_service.h" |
-#include "content/browser/navigator_connect/navigator_connect_context.h" |
-#include "content/browser/service_worker/service_worker_context_wrapper.h" |
-#include "content/common/navigator_connect_messages.h" |
-#include "content/common/navigator_connect_types.h" |
+#include "content/public/browser/navigator_connect_service_factory.h" |
+#include "content/public/common/navigator_connect_client.h" |
namespace content { |
-NavigatorConnectDispatcherHost::NavigatorConnectDispatcherHost( |
- const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, |
- const scoped_refptr<NavigatorConnectContext>& navigator_connect_context) |
- : BrowserMessageFilter(NavigatorConnectMsgStart), |
- service_worker_context_(service_worker_context), |
- navigator_connect_context_(navigator_connect_context) { |
+NavigatorConnectContextImpl::NavigatorConnectContextImpl() { |
} |
-NavigatorConnectDispatcherHost::~NavigatorConnectDispatcherHost() { |
+NavigatorConnectContextImpl::~NavigatorConnectContextImpl() { |
} |
-bool NavigatorConnectDispatcherHost::OnMessageReceived( |
- const IPC::Message& message) { |
- bool handled = true; |
- IPC_BEGIN_MESSAGE_MAP(NavigatorConnectDispatcherHost, message) |
- IPC_MESSAGE_HANDLER(NavigatorConnectHostMsg_Connect, OnConnect) |
- IPC_MESSAGE_UNHANDLED(handled = false) |
- IPC_END_MESSAGE_MAP() |
- return handled; |
+void NavigatorConnectContextImpl::AddFactory( |
+ scoped_ptr<NavigatorConnectServiceFactory> factory) { |
+ service_factories_.push_back(factory.release()); |
} |
-void NavigatorConnectDispatcherHost::OnConnect( |
- int thread_id, |
- int request_id, |
- const CrossOriginServiceWorkerClient& client) { |
- DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- |
+void NavigatorConnectContextImpl::Connect(const NavigatorConnectClient& client, |
+ const ConnectCallback& callback) { |
// Hold messages for port while setting up connection. |
MessagePortService::GetInstance()->HoldMessages(client.message_port_id); |
- // Find the right service worker to service this connection. |
- service_worker_context_->context()->storage()->FindRegistrationForDocument( |
- client.target_url, |
- base::Bind(&NavigatorConnectDispatcherHost::GotServiceWorkerRegistration, |
- this, thread_id, request_id, client)); |
-} |
- |
-void NavigatorConnectDispatcherHost::GotServiceWorkerRegistration( |
- int thread_id, |
- int request_id, |
- const CrossOriginServiceWorkerClient& client, |
- ServiceWorkerStatusCode status, |
- const scoped_refptr<ServiceWorkerRegistration>& registration) { |
- DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- |
- if (status != SERVICE_WORKER_OK) { |
- // No service worker found, reject connection attempt. |
- OnConnectResult(thread_id, request_id, client, registration, status, false); |
- return; |
+ // Find factory to handle request, more recently added factories should take |
+ // priority as per comment at NavigatorConnectContext::AddFactory.. |
+ NavigatorConnectServiceFactory* factory = nullptr; |
+ for (auto it = service_factories_.rbegin(); it != service_factories_.rend(); |
+ ++it) { |
+ if ((*it)->HandlesUrl(client.target_url)) { |
+ factory = *it; |
+ break; |
+ } |
} |
- ServiceWorkerVersion* active_version = registration->active_version(); |
- if (!active_version) { |
- // No active version, reject connection attempt. |
- OnConnectResult(thread_id, request_id, client, registration, status, false); |
+ if (!factory) { |
+ // No factories found. |
+ // Close port since connection failed. |
+ MessagePortService::GetInstance()->ClosePort(client.message_port_id); |
+ callback.Run(false); |
return; |
} |
- active_version->DispatchCrossOriginConnectEvent( |
- base::Bind(&NavigatorConnectDispatcherHost::OnConnectResult, this, |
- thread_id, request_id, client, registration), |
- client); |
+ // Actually initiate connection. |
+ factory->Connect( |
+ client, base::Bind(&NavigatorConnectContextImpl::OnConnectResult, this, |
+ client, callback)); |
} |
-void NavigatorConnectDispatcherHost::OnConnectResult( |
- int thread_id, |
- int request_id, |
- const CrossOriginServiceWorkerClient& client, |
- const scoped_refptr<ServiceWorkerRegistration>& registration, |
- ServiceWorkerStatusCode status, |
- bool accept_connection) { |
- DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- |
- if (status != SERVICE_WORKER_OK || !accept_connection) { |
+void NavigatorConnectContextImpl::OnConnectResult( |
+ const NavigatorConnectClient& client, |
+ const ConnectCallback& callback, |
+ MessagePortDelegate* delegate) { |
+ if (delegate) { |
+ MessagePortService::GetInstance()->UpdateMessagePort( |
+ client.message_port_id, delegate, client.message_port_id); |
+ MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); |
+ callback.Run(true); |
+ } else { |
// Close port since connection failed. |
MessagePortService::GetInstance()->ClosePort(client.message_port_id); |
- Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, false)); |
- return; |
+ callback.Run(false); |
} |
- |
- // Register connection and post back result. |
- navigator_connect_context_->RegisterConnection(client, registration); |
- Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, true)); |
} |
} // namespace content |