Chromium Code Reviews| Index: content/browser/navigator_connect/navigator_connect_context_impl.cc |
| diff --git a/content/browser/navigator_connect/navigator_connect_context.cc b/content/browser/navigator_connect/navigator_connect_context_impl.cc |
| similarity index 19% |
| copy from content/browser/navigator_connect/navigator_connect_context.cc |
| copy to content/browser/navigator_connect/navigator_connect_context_impl.cc |
| index 45d6a6f6cf74f17072ed984b4038c4d921e8f806..849028ec00404409c2b7d2484668a8701fbfc1ea 100644 |
| --- a/content/browser/navigator_connect/navigator_connect_context.cc |
| +++ b/content/browser/navigator_connect/navigator_connect_context_impl.cc |
| @@ -2,90 +2,92 @@ |
| // 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_context.h" |
| +#include "content/browser/navigator_connect/navigator_connect_context_impl.h" |
| #include "content/browser/message_port_service.h" |
| -#include "content/browser/service_worker/service_worker_context_wrapper.h" |
| -#include "content/browser/service_worker/service_worker_utils.h" |
| -#include "content/common/navigator_connect_types.h" |
| +#include "content/public/browser/navigator_connect_service.h" |
| +#include "content/public/browser/navigator_connect_service_factory.h" |
| +#include "content/public/common/navigator_connect_client.h" |
| namespace content { |
| -struct NavigatorConnectContext::Connection { |
| - CrossOriginServiceWorkerClient client; |
| - int64 service_worker_registration_id; |
| - GURL service_worker_registration_origin; |
| -}; |
| +NavigatorConnectContextImpl::NavigatorConnectContextImpl() { |
| +} |
| + |
| +NavigatorConnectContextImpl::~NavigatorConnectContextImpl() { |
| +} |
| -NavigatorConnectContext::NavigatorConnectContext( |
| - const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
| - : service_worker_context_(service_worker_context) { |
| +void NavigatorConnectContextImpl::AddFactory( |
| + scoped_ptr<NavigatorConnectServiceFactory> factory) { |
| + service_factories_.push_back(factory.release()); |
| } |
| -NavigatorConnectContext::~NavigatorConnectContext() { |
| +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 factory to handle request, more recently added factories should take |
| + // priority. |
|
scheib
2015/01/23 22:03:58
take priority as per comment at NavigatorConnectCo
Marijn Kruisselbrink
2015/01/26 21:34:26
Done.
|
| + NavigatorConnectServiceFactory* factory = nullptr; |
| + for (auto it = service_factories_.rbegin(); it != service_factories_.rend(); |
| + ++it) { |
| + if ((*it)->HandlesUrl(client.target_url)) { |
| + factory = *it; |
| + break; |
| + } |
| + } |
| + |
| + if (!factory) { |
| + // No factories found. |
| + // Close port since connection failed. |
| + MessagePortService::GetInstance()->ClosePort(client.message_port_id); |
| + callback.Run(false); |
| + return; |
| + } |
| + |
| + // Actually initiate connection. |
| + factory->Connect( |
| + client, base::Bind(&NavigatorConnectContextImpl::OnConnectResult, this, |
| + client, callback)); |
| } |
| -void NavigatorConnectContext::RegisterConnection( |
| - const CrossOriginServiceWorkerClient& client, |
| - const scoped_refptr<ServiceWorkerRegistration>& |
| - service_worker_registration) { |
| - MessagePortService::GetInstance()->UpdateMessagePort( |
| - client.message_port_id, this, client.message_port_id); |
| - MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); |
| - Connection& connection = connections_[client.message_port_id]; |
| - connection.client = client; |
| - connection.service_worker_registration_id = service_worker_registration->id(); |
| - connection.service_worker_registration_origin = |
| - service_worker_registration->pattern().GetOrigin(); |
| +void NavigatorConnectContextImpl::OnConnectResult( |
| + const NavigatorConnectClient& client, |
| + const ConnectCallback& callback, |
| + scoped_ptr<NavigatorConnectService> service) { |
| + if (service) { |
| + MessagePortService::GetInstance()->UpdateMessagePort( |
| + client.message_port_id, this, client.message_port_id); |
| + MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); |
| + connected_services_[client.message_port_id] = service.release(); |
| + callback.Run(true); |
| + } else { |
| + // Close port since connection failed. |
| + MessagePortService::GetInstance()->ClosePort(client.message_port_id); |
| + callback.Run(false); |
| + } |
| } |
| -void NavigatorConnectContext::SendMessage( |
| +void NavigatorConnectContextImpl::SendMessageToPort( |
| int route_id, |
| const base::string16& message, |
| const std::vector<int>& sent_message_port_ids) { |
| - DCHECK(connections_.find(route_id) != connections_.end()); |
| - const Connection& connection = connections_[route_id]; |
| + DCHECK(connected_services_.find(route_id) != connected_services_.end()); |
| + NavigatorConnectService* service = connected_services_[route_id]; |
| - // Hold messages while service worker is found, activated, and message sent |
| - // causing ServiceWorkerScriptContext::OnCrossOriginMessageToWorker to |
| - // construct WebMessagePortChannelImpl instances which send |
| - // MessagePortHostMsg_ReleaseMessages. |
| + // Hold messages on transferred message ports. Actual delivery of the message |
| + // by the service can be asynchronous. When a message is delivered, |
| + // WebMessagePortChannelImpl instances will be constructed which send |
| + // MessagePortHostMsg_ReleaseMessages to release messages. |
| for (int sent_message_port_id : sent_message_port_ids) |
| MessagePortService::GetInstance()->HoldMessages(sent_message_port_id); |
| - service_worker_context_->context()->storage()->FindRegistrationForId( |
| - connection.service_worker_registration_id, |
| - connection.service_worker_registration_origin, |
| - base::Bind(&NavigatorConnectContext::DoSendMessage, this, |
| - connection.client, message, sent_message_port_ids)); |
| + service->OnMessage(message, sent_message_port_ids); |
| } |
| -void NavigatorConnectContext::SendMessagesAreQueued(int route_id) { |
| +void NavigatorConnectContextImpl::SendMessagesAreQueued(int route_id) { |
| NOTREACHED() << "navigator.connect endpoints should never queue messages."; |
| } |
| -void NavigatorConnectContext::DoSendMessage( |
| - const CrossOriginServiceWorkerClient& client, |
| - const base::string16& message, |
| - const std::vector<int>& sent_message_port_ids, |
| - ServiceWorkerStatusCode service_worker_status, |
| - const scoped_refptr<ServiceWorkerRegistration>& |
| - service_worker_registration) { |
| - if (service_worker_status != SERVICE_WORKER_OK) { |
| - // TODO(mek): Do something when no service worker was found. |
| - return; |
| - } |
| - |
| - ServiceWorkerVersion* active_version = |
| - service_worker_registration->active_version(); |
| - if (!active_version) { |
| - // TODO(mek): Do something when no active version exists. |
| - return; |
| - } |
| - |
| - active_version->DispatchCrossOriginMessageEvent( |
| - client, message, sent_message_port_ids, |
| - base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| -} |
| - |
| } // namespace content |