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/browser/navigator_connect/navigator_connect_context.h" | |
| 6 | |
| 7 #include "content/browser/message_port_service.h" | |
| 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 9 #include "content/browser/service_worker/service_worker_utils.h" | |
| 10 #include "content/common/navigator_connect_types.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 struct NavigatorConnectContext::Connection { | |
| 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 } | |
| 24 | |
| 25 NavigatorConnectContext::~NavigatorConnectContext() { | |
| 26 } | |
| 27 | |
| 28 void NavigatorConnectContext::RegisterConnection( | |
| 29 const CrossOriginServiceWorkerClient& client, | |
| 30 const scoped_refptr<ServiceWorkerRegistration>& | |
| 31 service_worker_registration) { | |
| 32 // Use message_port_id as routing ID since there is no reason to have yet | |
|
scheib
2014/12/18 00:24:10
Perhaps this comment would be best at the definiti
Marijn Kruisselbrink
2014/12/19 00:09:21
Ah yes, good point.
| |
| 33 // another ID. | |
| 34 MessagePortService::GetInstance()->UpdateMessagePort( | |
| 35 client.message_port_id, this, client.message_port_id); | |
| 36 MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id); | |
| 37 Connection& connection = connections_[client.message_port_id]; | |
|
scheib
2014/12/18 00:24:10
See comment at NavigatorConnectDispatcherHost::OnC
| |
| 38 connection.client = client; | |
| 39 connection.service_worker_registration_id = service_worker_registration->id(); | |
| 40 connection.service_worker_registration_origin = | |
| 41 service_worker_registration->pattern().GetOrigin(); | |
| 42 } | |
| 43 | |
| 44 void NavigatorConnectContext::SendMessage( | |
| 45 int route_id, | |
| 46 const base::string16& message, | |
| 47 const std::vector<int>& sent_message_port_ids) { | |
| 48 DCHECK(connections_.find(route_id) != connections_.end()); | |
| 49 const Connection& connection = connections_[route_id]; | |
| 50 | |
| 51 // Hold messages on all transfered ports. | |
|
scheib
2014/12/18 00:24:10
comment suggestion:
Hold messages while service w
Marijn Kruisselbrink
2014/12/19 00:09:21
Done.
| |
| 52 for (int sent_message_port_id : sent_message_port_ids) | |
| 53 MessagePortService::GetInstance()->HoldMessages(sent_message_port_id); | |
| 54 | |
| 55 service_worker_context_->context()->storage()->FindRegistrationForId( | |
| 56 connection.service_worker_registration_id, | |
| 57 connection.service_worker_registration_origin, | |
| 58 base::Bind(&NavigatorConnectContext::DoSendMessage, this, | |
| 59 connection.client, message, sent_message_port_ids)); | |
| 60 } | |
| 61 | |
| 62 void NavigatorConnectContext::SendMessagesAreQueued(int route_id) { | |
| 63 NOTREACHED() << "navigator.connect endpoints should never queue messages."; | |
| 64 } | |
| 65 | |
| 66 void NavigatorConnectContext::DoSendMessage( | |
| 67 const CrossOriginServiceWorkerClient& client, | |
| 68 const base::string16& message, | |
| 69 const std::vector<int>& sent_message_port_ids, | |
| 70 ServiceWorkerStatusCode service_worker_status, | |
| 71 const scoped_refptr<ServiceWorkerRegistration>& | |
| 72 service_worker_registration) { | |
| 73 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 74 // TODO(mek): Do something when no service worker was found. | |
| 75 return; | |
| 76 } | |
| 77 | |
| 78 ServiceWorkerVersion* active_version = | |
| 79 service_worker_registration->active_version(); | |
| 80 if (!active_version) { | |
| 81 // TODO(mek): Do something when no active version exists. | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 active_version->DispatchCrossOriginMessageEvent( | |
| 86 client, message, sent_message_port_ids, | |
| 87 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 88 } | |
| 89 | |
| 90 } // namespace content | |
| OLD | NEW |