Chromium Code Reviews| 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_context.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/service_worker/service_worker_context_wrapper.h" | 8 #include "content/public/browser/navigator_connect_service.h" |
| 9 #include "content/browser/service_worker/service_worker_utils.h" | 9 #include "content/public/browser/navigator_connect_service_factory.h" |
| 10 #include "content/common/navigator_connect_types.h" | 10 #include "content/public/common/navigator_connect_client.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 struct NavigatorConnectContext::Connection { | 14 NavigatorConnectContextImpl::NavigatorConnectContextImpl() { |
| 15 CrossOriginServiceWorkerClient client; | |
| 16 int64 service_worker_registration_id; | |
| 17 GURL service_worker_registration_origin; | |
| 18 }; | |
| 19 | |
| 20 NavigatorConnectContext::NavigatorConnectContext( | |
| 21 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | |
| 22 : service_worker_context_(service_worker_context) { | |
| 23 } | 15 } |
| 24 | 16 |
| 25 NavigatorConnectContext::~NavigatorConnectContext() { | 17 NavigatorConnectContextImpl::~NavigatorConnectContextImpl() { |
| 26 } | 18 } |
| 27 | 19 |
| 28 void NavigatorConnectContext::RegisterConnection( | 20 void NavigatorConnectContextImpl::AddFactory( |
| 29 const CrossOriginServiceWorkerClient& client, | 21 scoped_ptr<NavigatorConnectServiceFactory> factory) { |
| 30 const scoped_refptr<ServiceWorkerRegistration>& | 22 service_factories_.push_back(factory.release()); |
| 31 service_worker_registration) { | |
| 32 MessagePortService::GetInstance()->UpdateMessagePort( | |
| 33 client.message_port_id, this, client.message_port_id); | |
| 34 MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); | |
| 35 Connection& connection = connections_[client.message_port_id]; | |
| 36 connection.client = client; | |
| 37 connection.service_worker_registration_id = service_worker_registration->id(); | |
| 38 connection.service_worker_registration_origin = | |
| 39 service_worker_registration->pattern().GetOrigin(); | |
| 40 } | 23 } |
| 41 | 24 |
| 42 void NavigatorConnectContext::SendMessage( | 25 void NavigatorConnectContextImpl::Connect(const NavigatorConnectClient& client, |
| 26 const ConnectCallback& callback) { | |
| 27 // Hold messages for port while setting up connection. | |
| 28 MessagePortService::GetInstance()->HoldMessages(client.message_port_id); | |
| 29 | |
| 30 // Find factory to handle request, more recently added factories should take | |
| 31 // priority. | |
|
scheib
2015/01/23 22:03:58
take priority as per comment at NavigatorConnectCo
Marijn Kruisselbrink
2015/01/26 21:34:26
Done.
| |
| 32 NavigatorConnectServiceFactory* factory = nullptr; | |
| 33 for (auto it = service_factories_.rbegin(); it != service_factories_.rend(); | |
| 34 ++it) { | |
| 35 if ((*it)->HandlesUrl(client.target_url)) { | |
| 36 factory = *it; | |
| 37 break; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 if (!factory) { | |
| 42 // No factories found. | |
| 43 // Close port since connection failed. | |
| 44 MessagePortService::GetInstance()->ClosePort(client.message_port_id); | |
| 45 callback.Run(false); | |
| 46 return; | |
| 47 } | |
| 48 | |
| 49 // Actually initiate connection. | |
| 50 factory->Connect( | |
| 51 client, base::Bind(&NavigatorConnectContextImpl::OnConnectResult, this, | |
| 52 client, callback)); | |
| 53 } | |
| 54 | |
| 55 void NavigatorConnectContextImpl::OnConnectResult( | |
| 56 const NavigatorConnectClient& client, | |
| 57 const ConnectCallback& callback, | |
| 58 scoped_ptr<NavigatorConnectService> service) { | |
| 59 if (service) { | |
| 60 MessagePortService::GetInstance()->UpdateMessagePort( | |
| 61 client.message_port_id, this, client.message_port_id); | |
| 62 MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); | |
| 63 connected_services_[client.message_port_id] = service.release(); | |
| 64 callback.Run(true); | |
| 65 } else { | |
| 66 // Close port since connection failed. | |
| 67 MessagePortService::GetInstance()->ClosePort(client.message_port_id); | |
| 68 callback.Run(false); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void NavigatorConnectContextImpl::SendMessageToPort( | |
| 43 int route_id, | 73 int route_id, |
| 44 const base::string16& message, | 74 const base::string16& message, |
| 45 const std::vector<int>& sent_message_port_ids) { | 75 const std::vector<int>& sent_message_port_ids) { |
| 46 DCHECK(connections_.find(route_id) != connections_.end()); | 76 DCHECK(connected_services_.find(route_id) != connected_services_.end()); |
| 47 const Connection& connection = connections_[route_id]; | 77 NavigatorConnectService* service = connected_services_[route_id]; |
| 48 | 78 |
| 49 // Hold messages while service worker is found, activated, and message sent | 79 // Hold messages on transferred message ports. Actual delivery of the message |
| 50 // causing ServiceWorkerScriptContext::OnCrossOriginMessageToWorker to | 80 // by the service can be asynchronous. When a message is delivered, |
| 51 // construct WebMessagePortChannelImpl instances which send | 81 // WebMessagePortChannelImpl instances will be constructed which send |
| 52 // MessagePortHostMsg_ReleaseMessages. | 82 // MessagePortHostMsg_ReleaseMessages to release messages. |
| 53 for (int sent_message_port_id : sent_message_port_ids) | 83 for (int sent_message_port_id : sent_message_port_ids) |
| 54 MessagePortService::GetInstance()->HoldMessages(sent_message_port_id); | 84 MessagePortService::GetInstance()->HoldMessages(sent_message_port_id); |
| 55 | 85 |
| 56 service_worker_context_->context()->storage()->FindRegistrationForId( | 86 service->OnMessage(message, sent_message_port_ids); |
| 57 connection.service_worker_registration_id, | |
| 58 connection.service_worker_registration_origin, | |
| 59 base::Bind(&NavigatorConnectContext::DoSendMessage, this, | |
| 60 connection.client, message, sent_message_port_ids)); | |
| 61 } | 87 } |
| 62 | 88 |
| 63 void NavigatorConnectContext::SendMessagesAreQueued(int route_id) { | 89 void NavigatorConnectContextImpl::SendMessagesAreQueued(int route_id) { |
| 64 NOTREACHED() << "navigator.connect endpoints should never queue messages."; | 90 NOTREACHED() << "navigator.connect endpoints should never queue messages."; |
| 65 } | 91 } |
| 66 | 92 |
| 67 void NavigatorConnectContext::DoSendMessage( | |
| 68 const CrossOriginServiceWorkerClient& client, | |
| 69 const base::string16& message, | |
| 70 const std::vector<int>& sent_message_port_ids, | |
| 71 ServiceWorkerStatusCode service_worker_status, | |
| 72 const scoped_refptr<ServiceWorkerRegistration>& | |
| 73 service_worker_registration) { | |
| 74 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 75 // TODO(mek): Do something when no service worker was found. | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 ServiceWorkerVersion* active_version = | |
| 80 service_worker_registration->active_version(); | |
| 81 if (!active_version) { | |
| 82 // TODO(mek): Do something when no active version exists. | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 active_version->DispatchCrossOriginMessageEvent( | |
| 87 client, message, sent_message_port_ids, | |
| 88 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 89 } | |
| 90 | |
| 91 } // namespace content | 93 } // namespace content |
| OLD | NEW |