| 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/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 146 } |
| 147 | 147 |
| 148 void RunErrorFetchCallback(const ServiceWorkerVersion::FetchCallback& callback, | 148 void RunErrorFetchCallback(const ServiceWorkerVersion::FetchCallback& callback, |
| 149 ServiceWorkerStatusCode status) { | 149 ServiceWorkerStatusCode status) { |
| 150 callback.Run(status, | 150 callback.Run(status, |
| 151 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, | 151 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, |
| 152 ServiceWorkerResponse()); | 152 ServiceWorkerResponse()); |
| 153 } | 153 } |
| 154 | 154 |
| 155 void RunErrorMessageCallback( | 155 void RunErrorMessageCallback( |
| 156 const std::vector<int>& sent_message_port_ids, | 156 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 157 const ServiceWorkerVersion::StatusCallback& callback, | 157 const ServiceWorkerVersion::StatusCallback& callback, |
| 158 ServiceWorkerStatusCode status) { | 158 ServiceWorkerStatusCode status) { |
| 159 // Transfering the message ports failed, so destroy the ports. | 159 // Transfering the message ports failed, so destroy the ports. |
| 160 for (int message_port_id : sent_message_port_ids) { | 160 for (const TransferredMessagePort& port : sent_message_ports) { |
| 161 MessagePortService::GetInstance()->ClosePort(message_port_id); | 161 MessagePortService::GetInstance()->ClosePort(port.id); |
| 162 } | 162 } |
| 163 callback.Run(status); | 163 callback.Run(status); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void RunErrorCrossOriginConnectCallback( | 166 void RunErrorCrossOriginConnectCallback( |
| 167 const ServiceWorkerVersion::CrossOriginConnectCallback& callback, | 167 const ServiceWorkerVersion::CrossOriginConnectCallback& callback, |
| 168 ServiceWorkerStatusCode status) { | 168 ServiceWorkerStatusCode status) { |
| 169 callback.Run(status, false); | 169 callback.Run(status, false); |
| 170 } | 170 } |
| 171 | 171 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 message, callback))); | 437 message, callback))); |
| 438 return; | 438 return; |
| 439 } | 439 } |
| 440 | 440 |
| 441 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); | 441 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); |
| 442 RunSoon(base::Bind(callback, status)); | 442 RunSoon(base::Bind(callback, status)); |
| 443 } | 443 } |
| 444 | 444 |
| 445 void ServiceWorkerVersion::DispatchMessageEvent( | 445 void ServiceWorkerVersion::DispatchMessageEvent( |
| 446 const base::string16& message, | 446 const base::string16& message, |
| 447 const std::vector<int>& sent_message_port_ids, | 447 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 448 const StatusCallback& callback) { | 448 const StatusCallback& callback) { |
| 449 for (int message_port_id : sent_message_port_ids) { | 449 for (const TransferredMessagePort& port : sent_message_ports) { |
| 450 MessagePortService::GetInstance()->HoldMessages(message_port_id); | 450 MessagePortService::GetInstance()->HoldMessages(port.id); |
| 451 } | 451 } |
| 452 | 452 |
| 453 DispatchMessageEventInternal(message, sent_message_port_ids, callback); | 453 DispatchMessageEventInternal(message, sent_message_ports, callback); |
| 454 } | 454 } |
| 455 | 455 |
| 456 void ServiceWorkerVersion::DispatchMessageEventInternal( | 456 void ServiceWorkerVersion::DispatchMessageEventInternal( |
| 457 const base::string16& message, | 457 const base::string16& message, |
| 458 const std::vector<int>& sent_message_port_ids, | 458 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 459 const StatusCallback& callback) { | 459 const StatusCallback& callback) { |
| 460 if (running_status() != RUNNING) { | 460 if (running_status() != RUNNING) { |
| 461 // Schedule calling this method after starting the worker. | 461 // Schedule calling this method after starting the worker. |
| 462 StartWorker(base::Bind( | 462 StartWorker(base::Bind( |
| 463 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), | 463 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 464 base::Bind(&RunErrorMessageCallback, sent_message_port_ids, callback), | 464 base::Bind(&RunErrorMessageCallback, sent_message_ports, callback), |
| 465 base::Bind(&self::DispatchMessageEventInternal, | 465 base::Bind(&self::DispatchMessageEventInternal, |
| 466 weak_factory_.GetWeakPtr(), message, sent_message_port_ids, | 466 weak_factory_.GetWeakPtr(), message, sent_message_ports, |
| 467 callback))); | 467 callback))); |
| 468 return; | 468 return; |
| 469 } | 469 } |
| 470 | 470 |
| 471 MessagePortMessageFilter* filter = | 471 MessagePortMessageFilter* filter = |
| 472 embedded_worker_->message_port_message_filter(); | 472 embedded_worker_->message_port_message_filter(); |
| 473 std::vector<int> new_routing_ids; | 473 std::vector<int> new_routing_ids; |
| 474 filter->UpdateMessagePortsWithNewRoutes(sent_message_port_ids, | 474 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); |
| 475 &new_routing_ids); | |
| 476 ServiceWorkerStatusCode status = | 475 ServiceWorkerStatusCode status = |
| 477 embedded_worker_->SendMessage(ServiceWorkerMsg_MessageToWorker( | 476 embedded_worker_->SendMessage(ServiceWorkerMsg_MessageToWorker( |
| 478 message, sent_message_port_ids, new_routing_ids)); | 477 message, sent_message_ports, new_routing_ids)); |
| 479 RunSoon(base::Bind(callback, status)); | 478 RunSoon(base::Bind(callback, status)); |
| 480 } | 479 } |
| 481 | 480 |
| 482 void ServiceWorkerVersion::DispatchInstallEvent( | 481 void ServiceWorkerVersion::DispatchInstallEvent( |
| 483 int active_version_id, | 482 int active_version_id, |
| 484 const StatusCallback& callback) { | 483 const StatusCallback& callback) { |
| 485 DCHECK_EQ(INSTALLING, status()) << status(); | 484 DCHECK_EQ(INSTALLING, status()) << status(); |
| 486 | 485 |
| 487 if (running_status() != RUNNING) { | 486 if (running_status() != RUNNING) { |
| 488 // Schedule calling this method after starting the worker. | 487 // Schedule calling this method after starting the worker. |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 ServiceWorkerMsg_CrossOriginConnectEvent(request_id, client)); | 690 ServiceWorkerMsg_CrossOriginConnectEvent(request_id, client)); |
| 692 if (status != SERVICE_WORKER_OK) { | 691 if (status != SERVICE_WORKER_OK) { |
| 693 cross_origin_connect_callbacks_.Remove(request_id); | 692 cross_origin_connect_callbacks_.Remove(request_id); |
| 694 RunSoon(base::Bind(callback, status, false)); | 693 RunSoon(base::Bind(callback, status, false)); |
| 695 } | 694 } |
| 696 } | 695 } |
| 697 | 696 |
| 698 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( | 697 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( |
| 699 const NavigatorConnectClient& client, | 698 const NavigatorConnectClient& client, |
| 700 const base::string16& message, | 699 const base::string16& message, |
| 701 const std::vector<int>& sent_message_port_ids, | 700 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 702 const StatusCallback& callback) { | 701 const StatusCallback& callback) { |
| 703 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to | 702 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to |
| 704 // have already put all the sent message ports on hold. So no need to do that | 703 // have already put all the sent message ports on hold. So no need to do that |
| 705 // here again. | 704 // here again. |
| 706 | 705 |
| 707 if (running_status() != RUNNING) { | 706 if (running_status() != RUNNING) { |
| 708 // Schedule calling this method after starting the worker. | 707 // Schedule calling this method after starting the worker. |
| 709 StartWorker(base::Bind( | 708 StartWorker(base::Bind( |
| 710 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), callback, | 709 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 710 base::Bind(&RunErrorMessageCallback, sent_message_ports, callback), |
| 711 base::Bind(&self::DispatchCrossOriginMessageEvent, | 711 base::Bind(&self::DispatchCrossOriginMessageEvent, |
| 712 weak_factory_.GetWeakPtr(), client, message, | 712 weak_factory_.GetWeakPtr(), client, message, |
| 713 sent_message_port_ids, callback))); | 713 sent_message_ports, callback))); |
| 714 return; | 714 return; |
| 715 } | 715 } |
| 716 | 716 |
| 717 MessagePortMessageFilter* filter = | 717 MessagePortMessageFilter* filter = |
| 718 embedded_worker_->message_port_message_filter(); | 718 embedded_worker_->message_port_message_filter(); |
| 719 std::vector<int> new_routing_ids; | 719 std::vector<int> new_routing_ids; |
| 720 filter->UpdateMessagePortsWithNewRoutes(sent_message_port_ids, | 720 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); |
| 721 &new_routing_ids); | |
| 722 ServiceWorkerStatusCode status = | 721 ServiceWorkerStatusCode status = |
| 723 embedded_worker_->SendMessage(ServiceWorkerMsg_CrossOriginMessageToWorker( | 722 embedded_worker_->SendMessage(ServiceWorkerMsg_CrossOriginMessageToWorker( |
| 724 client, message, sent_message_port_ids, new_routing_ids)); | 723 client, message, sent_message_ports, new_routing_ids)); |
| 725 RunSoon(base::Bind(callback, status)); | 724 RunSoon(base::Bind(callback, status)); |
| 726 } | 725 } |
| 726 |
| 727 void ServiceWorkerVersion::AddControllee( | 727 void ServiceWorkerVersion::AddControllee( |
| 728 ServiceWorkerProviderHost* provider_host) { | 728 ServiceWorkerProviderHost* provider_host) { |
| 729 DCHECK(!ContainsKey(controllee_map_, provider_host)); | 729 DCHECK(!ContainsKey(controllee_map_, provider_host)); |
| 730 int controllee_id = controllee_by_id_.Add(provider_host); | 730 int controllee_id = controllee_by_id_.Add(provider_host); |
| 731 // IDMap<>'s last index is kInvalidServiceWorkerClientId. | 731 // IDMap<>'s last index is kInvalidServiceWorkerClientId. |
| 732 CHECK(controllee_id != kInvalidServiceWorkerClientId); | 732 CHECK(controllee_id != kInvalidServiceWorkerClientId); |
| 733 controllee_map_[provider_host] = controllee_id; | 733 controllee_map_[provider_host] = controllee_id; |
| 734 // Reset the timer if it's running (so that it's kept alive a bit longer | 734 // Reset the timer if it's running (so that it's kept alive a bit longer |
| 735 // right after a new controllee is added). | 735 // right after a new controllee is added). |
| 736 ScheduleStopWorker(); | 736 ScheduleStopWorker(); |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1278 int result) { | 1278 int result) { |
| 1279 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 1279 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
| 1280 "ServiceWorkerVersion::OnClearCachedMetadata", | 1280 "ServiceWorkerVersion::OnClearCachedMetadata", |
| 1281 callback_id, "result", result); | 1281 callback_id, "result", result); |
| 1282 FOR_EACH_OBSERVER(Listener, listeners_, OnCachedMetadataUpdated(this)); | 1282 FOR_EACH_OBSERVER(Listener, listeners_, OnCachedMetadataUpdated(this)); |
| 1283 } | 1283 } |
| 1284 | 1284 |
| 1285 void ServiceWorkerVersion::OnPostMessageToDocument( | 1285 void ServiceWorkerVersion::OnPostMessageToDocument( |
| 1286 int client_id, | 1286 int client_id, |
| 1287 const base::string16& message, | 1287 const base::string16& message, |
| 1288 const std::vector<int>& sent_message_port_ids) { | 1288 const std::vector<TransferredMessagePort>& sent_message_ports) { |
| 1289 TRACE_EVENT1("ServiceWorker", | 1289 TRACE_EVENT1("ServiceWorker", |
| 1290 "ServiceWorkerVersion::OnPostMessageToDocument", | 1290 "ServiceWorkerVersion::OnPostMessageToDocument", |
| 1291 "Client id", client_id); | 1291 "Client id", client_id); |
| 1292 ServiceWorkerProviderHost* provider_host = | 1292 ServiceWorkerProviderHost* provider_host = |
| 1293 controllee_by_id_.Lookup(client_id); | 1293 controllee_by_id_.Lookup(client_id); |
| 1294 if (!provider_host) { | 1294 if (!provider_host) { |
| 1295 // The client may already have been closed, just ignore. | 1295 // The client may already have been closed, just ignore. |
| 1296 return; | 1296 return; |
| 1297 } | 1297 } |
| 1298 provider_host->PostMessage(message, sent_message_port_ids); | 1298 provider_host->PostMessage(message, sent_message_ports); |
| 1299 } | 1299 } |
| 1300 | 1300 |
| 1301 void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) { | 1301 void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) { |
| 1302 TRACE_EVENT2("ServiceWorker", | 1302 TRACE_EVENT2("ServiceWorker", |
| 1303 "ServiceWorkerVersion::OnFocusClient", | 1303 "ServiceWorkerVersion::OnFocusClient", |
| 1304 "Request id", request_id, | 1304 "Request id", request_id, |
| 1305 "Client id", client_id); | 1305 "Client id", client_id); |
| 1306 ServiceWorkerProviderHost* provider_host = | 1306 ServiceWorkerProviderHost* provider_host = |
| 1307 controllee_by_id_.Lookup(client_id); | 1307 controllee_by_id_.Lookup(client_id); |
| 1308 if (!provider_host) { | 1308 if (!provider_host) { |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1517 int request_id) { | 1517 int request_id) { |
| 1518 callbacks->Remove(request_id); | 1518 callbacks->Remove(request_id); |
| 1519 if (is_doomed_) { | 1519 if (is_doomed_) { |
| 1520 // The stop should be already scheduled, but try to stop immediately, in | 1520 // The stop should be already scheduled, but try to stop immediately, in |
| 1521 // order to release worker resources soon. | 1521 // order to release worker resources soon. |
| 1522 StopWorkerIfIdle(); | 1522 StopWorkerIfIdle(); |
| 1523 } | 1523 } |
| 1524 } | 1524 } |
| 1525 | 1525 |
| 1526 } // namespace content | 1526 } // namespace content |
| OLD | NEW |