| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/service_worker/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "content/browser/message_port_message_filter.h" | 10 #include "content/browser/message_port_message_filter.h" |
| 11 #include "content/browser/message_port_service.h" |
| 11 #include "content/browser/service_worker/embedded_worker_instance.h" | 12 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 12 #include "content/browser/service_worker/embedded_worker_registry.h" | 13 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 13 #include "content/browser/service_worker/service_worker_context_core.h" | 14 #include "content/browser/service_worker/service_worker_context_core.h" |
| 14 #include "content/browser/service_worker/service_worker_registration.h" | 15 #include "content/browser/service_worker/service_worker_registration.h" |
| 15 #include "content/browser/service_worker/service_worker_utils.h" | 16 #include "content/browser/service_worker/service_worker_utils.h" |
| 16 #include "content/common/service_worker/service_worker_messages.h" | 17 #include "content/common/service_worker/service_worker_messages.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 task.Run(); | 82 task.Run(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void RunErrorFetchCallback(const ServiceWorkerVersion::FetchCallback& callback, | 85 void RunErrorFetchCallback(const ServiceWorkerVersion::FetchCallback& callback, |
| 85 ServiceWorkerStatusCode status) { | 86 ServiceWorkerStatusCode status) { |
| 86 callback.Run(status, | 87 callback.Run(status, |
| 87 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, | 88 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, |
| 88 ServiceWorkerResponse()); | 89 ServiceWorkerResponse()); |
| 89 } | 90 } |
| 90 | 91 |
| 92 void RunErrorMessageCallback( |
| 93 const std::vector<int>& sent_message_port_ids, |
| 94 const ServiceWorkerVersion::StatusCallback& callback, |
| 95 ServiceWorkerStatusCode status) { |
| 96 // Transfering the message ports failed, so destroy the ports. |
| 97 for (int message_port_id : sent_message_port_ids) { |
| 98 MessagePortService::GetInstance()->ClosePort(message_port_id); |
| 99 } |
| 100 callback.Run(status); |
| 101 } |
| 102 |
| 91 } // namespace | 103 } // namespace |
| 92 | 104 |
| 93 ServiceWorkerVersion::ServiceWorkerVersion( | 105 ServiceWorkerVersion::ServiceWorkerVersion( |
| 94 ServiceWorkerRegistration* registration, | 106 ServiceWorkerRegistration* registration, |
| 95 const GURL& script_url, | 107 const GURL& script_url, |
| 96 int64 version_id, | 108 int64 version_id, |
| 97 base::WeakPtr<ServiceWorkerContextCore> context) | 109 base::WeakPtr<ServiceWorkerContextCore> context) |
| 98 : version_id_(version_id), | 110 : version_id_(version_id), |
| 99 registration_id_(kInvalidServiceWorkerVersionId), | 111 registration_id_(kInvalidServiceWorkerVersionId), |
| 100 script_url_(script_url), | 112 script_url_(script_url), |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 257 } |
| 246 | 258 |
| 247 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); | 259 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); |
| 248 RunSoon(base::Bind(callback, status)); | 260 RunSoon(base::Bind(callback, status)); |
| 249 } | 261 } |
| 250 | 262 |
| 251 void ServiceWorkerVersion::DispatchMessageEvent( | 263 void ServiceWorkerVersion::DispatchMessageEvent( |
| 252 const base::string16& message, | 264 const base::string16& message, |
| 253 const std::vector<int>& sent_message_port_ids, | 265 const std::vector<int>& sent_message_port_ids, |
| 254 const StatusCallback& callback) { | 266 const StatusCallback& callback) { |
| 267 for (int message_port_id : sent_message_port_ids) { |
| 268 MessagePortService::GetInstance()->HoldMessages(message_port_id); |
| 269 } |
| 270 |
| 271 DispatchMessageEventInternal(message, sent_message_port_ids, callback); |
| 272 } |
| 273 |
| 274 void ServiceWorkerVersion::DispatchMessageEventInternal( |
| 275 const base::string16& message, |
| 276 const std::vector<int>& sent_message_port_ids, |
| 277 const StatusCallback& callback) { |
| 255 if (running_status() != RUNNING) { | 278 if (running_status() != RUNNING) { |
| 256 // Schedule calling this method after starting the worker. | 279 // Schedule calling this method after starting the worker. |
| 257 StartWorker(base::Bind( | 280 StartWorker(base::Bind( |
| 258 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), callback, | 281 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 259 base::Bind(&self::DispatchMessageEvent, weak_factory_.GetWeakPtr(), | 282 base::Bind(&RunErrorMessageCallback, sent_message_port_ids, callback), |
| 260 message, sent_message_port_ids, callback))); | 283 base::Bind(&self::DispatchMessageEventInternal, |
| 284 weak_factory_.GetWeakPtr(), message, sent_message_port_ids, |
| 285 callback))); |
| 261 return; | 286 return; |
| 262 } | 287 } |
| 263 | 288 |
| 264 MessagePortMessageFilter* filter = | 289 MessagePortMessageFilter* filter = |
| 265 embedded_worker_->message_port_message_filter(); | 290 embedded_worker_->message_port_message_filter(); |
| 266 std::vector<int> new_routing_ids; | 291 std::vector<int> new_routing_ids; |
| 267 filter->UpdateMessagePortsWithNewRoutes(sent_message_port_ids, | 292 filter->UpdateMessagePortsWithNewRoutes(sent_message_port_ids, |
| 268 &new_routing_ids); | 293 &new_routing_ids); |
| 269 ServiceWorkerStatusCode status = | 294 ServiceWorkerStatusCode status = |
| 270 embedded_worker_->SendMessage(ServiceWorkerMsg_MessageToWorker( | 295 embedded_worker_->SendMessage(ServiceWorkerMsg_MessageToWorker( |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 SetStatus(REDUNDANT); | 803 SetStatus(REDUNDANT); |
| 779 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 804 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 780 if (!context_) | 805 if (!context_) |
| 781 return; | 806 return; |
| 782 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 807 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 783 script_cache_map_.GetResources(&resources); | 808 script_cache_map_.GetResources(&resources); |
| 784 context_->storage()->PurgeResources(resources); | 809 context_->storage()->PurgeResources(resources); |
| 785 } | 810 } |
| 786 | 811 |
| 787 } // namespace content | 812 } // namespace content |
| OLD | NEW |