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/service_worker/navigator_connect_message_handler.h" |
| 6 |
| 7 #include "content/browser/message_port_service.h" |
| 8 #include "content/browser/service_worker/service_worker_context_core.h" |
| 9 #include "content/browser/service_worker/service_worker_registration.h" |
| 10 #include "content/browser/service_worker/service_worker_utils.h" |
| 11 #include "content/common/service_worker/service_worker_types.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 namespace { |
| 16 |
| 17 struct QueuedMessage { |
| 18 base::string16 message; |
| 19 std::vector<int> sent_message_port_ids; |
| 20 }; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 struct NavigatorConnectMessageHandler::MessagePort { |
| 25 CrossOriginServiceWorkerClient client; |
| 26 int64 service_worker_registration_id; |
| 27 GURL service_worker_registration_origin; |
| 28 }; |
| 29 |
| 30 NavigatorConnectMessageHandler::NavigatorConnectMessageHandler( |
| 31 base::WeakPtr<ServiceWorkerContextCore> context) |
| 32 : context_(context) { |
| 33 } |
| 34 |
| 35 NavigatorConnectMessageHandler::~NavigatorConnectMessageHandler() { |
| 36 } |
| 37 |
| 38 void NavigatorConnectMessageHandler::RegisterMessagePort( |
| 39 const CrossOriginServiceWorkerClient& client) { |
| 40 const int message_port_id = client.message_port_id; |
| 41 MessagePortService::GetInstance()->UpdateMessagePort(message_port_id, this, |
| 42 message_port_id); |
| 43 ports_[message_port_id].client = client; |
| 44 ports_[message_port_id].service_worker_registration_id = |
| 45 kInvalidServiceWorkerRegistrationId; |
| 46 } |
| 47 |
| 48 void NavigatorConnectMessageHandler::ClearRegistration(int message_port_id) { |
| 49 ports_.erase(message_port_id); |
| 50 MessagePortService::GetInstance()->ClosePort(message_port_id); |
| 51 } |
| 52 |
| 53 void NavigatorConnectMessageHandler::SetServiceWorkerRegistation( |
| 54 int message_port_id, |
| 55 const scoped_refptr<ServiceWorkerRegistration>& |
| 56 service_worker_registration) { |
| 57 DCHECK(ports_.find(message_port_id) != ports_.end()); |
| 58 ports_[message_port_id].service_worker_registration_id = |
| 59 service_worker_registration->id(); |
| 60 ports_[message_port_id].service_worker_registration_origin = |
| 61 service_worker_registration->pattern().GetOrigin(); |
| 62 } |
| 63 |
| 64 void NavigatorConnectMessageHandler::SendMessage( |
| 65 int route_id, |
| 66 const base::string16& message, |
| 67 const std::vector<int>& sent_message_port_ids) { |
| 68 LOG(INFO) << "Sending message " << message << " to route " << route_id; |
| 69 DCHECK(ports_.find(route_id) != ports_.end()); |
| 70 // There should be no situation where messages are sent to a port before it is |
| 71 // associated with a service worker. |
| 72 const MessagePort& port = ports_[route_id]; |
| 73 DCHECK(port.service_worker_registration_id != |
| 74 kInvalidServiceWorkerRegistrationId); |
| 75 |
| 76 if (!context_) { |
| 77 // TODO: should just clear everything with this handler |
| 78 ClearRegistration(route_id); |
| 79 return; |
| 80 } |
| 81 |
| 82 for (int sent_message_port_id: sent_message_port_ids) |
| 83 MessagePortService::GetInstance()->HoldMessages(sent_message_port_id); |
| 84 |
| 85 context_->storage()->FindRegistrationForId( |
| 86 port.service_worker_registration_id, |
| 87 port.service_worker_registration_origin, |
| 88 base::Bind(&NavigatorConnectMessageHandler::DoSendMessage, |
| 89 base::Unretained(this), port.client, message, |
| 90 sent_message_port_ids)); |
| 91 } |
| 92 |
| 93 void NavigatorConnectMessageHandler::SendMessagesQueued(int route_id) { |
| 94 LOG(INFO) << "SendMessagesQueued for route " << route_id; |
| 95 // Should never be called. |
| 96 } |
| 97 |
| 98 void NavigatorConnectMessageHandler::DoSendMessage( |
| 99 const CrossOriginServiceWorkerClient& client, |
| 100 const base::string16& message, |
| 101 const std::vector<int>& sent_message_port_ids, |
| 102 ServiceWorkerStatusCode service_worker_status, |
| 103 const scoped_refptr<ServiceWorkerRegistration>& |
| 104 service_worker_registration) { |
| 105 LOG(INFO) << "Actually sending message " << message << " to route " |
| 106 << client.message_port_id; |
| 107 if (service_worker_status != SERVICE_WORKER_OK) { |
| 108 ClearRegistration(client.message_port_id); |
| 109 return; |
| 110 } |
| 111 |
| 112 ServiceWorkerVersion* active_version = |
| 113 service_worker_registration->active_version(); |
| 114 if (!active_version) { |
| 115 // TODO(mek): Do something when no active version exists. |
| 116 return; |
| 117 } |
| 118 |
| 119 active_version->SendCrossOriginMessage( |
| 120 client, message, sent_message_port_ids, |
| 121 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 122 } |
| 123 |
| 124 } // namespace content |
OLD | NEW |