| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 const StatusCallback& error_callback) { | 518 const StatusCallback& error_callback) { |
| 519 if (running_status() == RUNNING) { | 519 if (running_status() == RUNNING) { |
| 520 DCHECK(start_callbacks_.empty()); | 520 DCHECK(start_callbacks_.empty()); |
| 521 task.Run(); | 521 task.Run(); |
| 522 return; | 522 return; |
| 523 } | 523 } |
| 524 StartWorker(base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), | 524 StartWorker(base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 525 error_callback, task)); | 525 error_callback, task)); |
| 526 } | 526 } |
| 527 | 527 |
| 528 void ServiceWorkerVersion::DispatchExtendableMessageEvent( | |
| 529 ServiceWorkerProviderHost* sender_provider_host, | |
| 530 const base::string16& message, | |
| 531 const url::Origin& source_origin, | |
| 532 const std::vector<TransferredMessagePort>& sent_message_ports, | |
| 533 const StatusCallback& callback) { | |
| 534 for (const TransferredMessagePort& port : sent_message_ports) | |
| 535 MessagePortService::GetInstance()->HoldMessages(port.id); | |
| 536 | |
| 537 switch (sender_provider_host->provider_type()) { | |
| 538 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | |
| 539 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | |
| 540 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | |
| 541 service_worker_client_utils::GetClient( | |
| 542 sender_provider_host, | |
| 543 base::Bind( | |
| 544 &ServiceWorkerVersion::DispatchExtendableMessageEventInternal< | |
| 545 ServiceWorkerClientInfo>, | |
| 546 weak_factory_.GetWeakPtr(), message, source_origin, | |
| 547 sent_message_ports, callback)); | |
| 548 break; | |
| 549 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: | |
| 550 // TODO(nhiroki): Decrement a reference to ServiceWorkerHandle if starting | |
| 551 // worker fails. Handles are managed by ServiceWorkerDispatcherHost, and | |
| 552 // we might need to make a new path to ask the dispatcher host to release | |
| 553 // the handle from ServiceWorkerVersion (http://crbug.com/543198). | |
| 554 RunSoon(base::Bind( | |
| 555 &ServiceWorkerVersion::DispatchExtendableMessageEventInternal< | |
| 556 ServiceWorkerObjectInfo>, | |
| 557 weak_factory_.GetWeakPtr(), message, source_origin, | |
| 558 sent_message_ports, callback, | |
| 559 sender_provider_host->GetOrCreateServiceWorkerHandle( | |
| 560 sender_provider_host->running_hosted_version()))); | |
| 561 break; | |
| 562 case SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME: | |
| 563 case SERVICE_WORKER_PROVIDER_UNKNOWN: | |
| 564 NOTREACHED() << sender_provider_host->provider_type(); | |
| 565 RunSoon(base::Bind(&RunErrorMessageCallback, sent_message_ports, callback, | |
| 566 SERVICE_WORKER_ERROR_FAILED)); | |
| 567 break; | |
| 568 } | |
| 569 } | |
| 570 | |
| 571 void ServiceWorkerVersion::DispatchMessageEvent( | 528 void ServiceWorkerVersion::DispatchMessageEvent( |
| 572 const base::string16& message, | 529 const base::string16& message, |
| 573 const std::vector<TransferredMessagePort>& sent_message_ports, | 530 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 574 const StatusCallback& callback) { | 531 const StatusCallback& callback) { |
| 575 for (const TransferredMessagePort& port : sent_message_ports) { | 532 for (const TransferredMessagePort& port : sent_message_ports) { |
| 576 MessagePortService::GetInstance()->HoldMessages(port.id); | 533 MessagePortService::GetInstance()->HoldMessages(port.id); |
| 577 } | 534 } |
| 578 | 535 |
| 579 DispatchMessageEventInternal(message, sent_message_ports, callback); | 536 DispatchMessageEventInternal(message, sent_message_ports, callback); |
| 580 } | 537 } |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 | 888 |
| 932 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( | 889 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( |
| 933 ServiceWorkerStatusCode status) { | 890 ServiceWorkerStatusCode status) { |
| 934 if (status != SERVICE_WORKER_OK) { | 891 if (status != SERVICE_WORKER_OK) { |
| 935 scoped_refptr<ServiceWorkerVersion> protect(this); | 892 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 936 RunCallbacks(this, &start_callbacks_, | 893 RunCallbacks(this, &start_callbacks_, |
| 937 DeduceStartWorkerFailureReason(status)); | 894 DeduceStartWorkerFailureReason(status)); |
| 938 } | 895 } |
| 939 } | 896 } |
| 940 | 897 |
| 941 template <typename SourceInfo> | |
| 942 void ServiceWorkerVersion::DispatchExtendableMessageEventInternal( | |
| 943 const base::string16& message, | |
| 944 const url::Origin& source_origin, | |
| 945 const std::vector<TransferredMessagePort>& sent_message_ports, | |
| 946 const StatusCallback& callback, | |
| 947 const SourceInfo& source_info) { | |
| 948 if (!source_info.IsValid()) { | |
| 949 RunErrorMessageCallback(sent_message_ports, callback, | |
| 950 SERVICE_WORKER_ERROR_FAILED); | |
| 951 return; | |
| 952 } | |
| 953 RunAfterStartWorker( | |
| 954 base::Bind( | |
| 955 &ServiceWorkerVersion::DispatchExtendableMessageEventAfterStartWorker, | |
| 956 weak_factory_.GetWeakPtr(), message, source_origin, | |
| 957 sent_message_ports, ExtendableMessageEventSource(source_info), | |
| 958 callback), | |
| 959 base::Bind(&RunErrorMessageCallback, sent_message_ports, callback)); | |
| 960 } | |
| 961 | |
| 962 void ServiceWorkerVersion::DispatchExtendableMessageEventAfterStartWorker( | |
| 963 const base::string16& message, | |
| 964 const url::Origin& source_origin, | |
| 965 const std::vector<TransferredMessagePort>& sent_message_ports, | |
| 966 const ExtendableMessageEventSource& source, | |
| 967 const StatusCallback& callback) { | |
| 968 int request_id = | |
| 969 StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, callback); | |
| 970 | |
| 971 MessagePortMessageFilter* filter = | |
| 972 embedded_worker_->message_port_message_filter(); | |
| 973 std::vector<int> new_routing_ids; | |
| 974 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | |
| 975 | |
| 976 ServiceWorkerMsg_ExtendableMessageEvent_Params params; | |
| 977 params.message = message; | |
| 978 params.source_origin = source_origin; | |
| 979 params.message_ports = sent_message_ports; | |
| 980 params.new_routing_ids = new_routing_ids; | |
| 981 params.source = source; | |
| 982 | |
| 983 // Hide the client url if the client has a unique origin. | |
| 984 if (source_origin.unique()) { | |
| 985 if (params.source.client_info.IsValid()) | |
| 986 params.source.client_info.url = GURL(); | |
| 987 else | |
| 988 params.source.service_worker_info.url = GURL(); | |
| 989 } | |
| 990 | |
| 991 DispatchSimpleEvent<ServiceWorkerHostMsg_ExtendableMessageEventFinished>( | |
| 992 request_id, ServiceWorkerMsg_ExtendableMessageEvent(request_id, params)); | |
| 993 } | |
| 994 | |
| 995 void ServiceWorkerVersion::OnGetClient(int request_id, | 898 void ServiceWorkerVersion::OnGetClient(int request_id, |
| 996 const std::string& client_uuid) { | 899 const std::string& client_uuid) { |
| 997 if (!context_) | 900 if (!context_) |
| 998 return; | 901 return; |
| 999 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", "ServiceWorkerVersion::OnGetClient", | 902 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", "ServiceWorkerVersion::OnGetClient", |
| 1000 request_id, "client_uuid", client_uuid); | 903 request_id, "client_uuid", client_uuid); |
| 1001 ServiceWorkerProviderHost* provider_host = | 904 ServiceWorkerProviderHost* provider_host = |
| 1002 context_->GetProviderHostByClientID(client_uuid); | 905 context_->GetProviderHostByClientID(client_uuid); |
| 1003 if (!provider_host || | 906 if (!provider_host || |
| 1004 provider_host->document_url().GetOrigin() != script_url_.GetOrigin()) { | 907 provider_host->document_url().GetOrigin() != script_url_.GetOrigin()) { |
| (...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1790 void ServiceWorkerVersion::OnBeginEvent() { | 1693 void ServiceWorkerVersion::OnBeginEvent() { |
| 1791 if (should_exclude_from_uma_ || running_status() != RUNNING || | 1694 if (should_exclude_from_uma_ || running_status() != RUNNING || |
| 1792 idle_time_.is_null()) { | 1695 idle_time_.is_null()) { |
| 1793 return; | 1696 return; |
| 1794 } | 1697 } |
| 1795 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 1698 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - |
| 1796 idle_time_); | 1699 idle_time_); |
| 1797 } | 1700 } |
| 1798 | 1701 |
| 1799 } // namespace content | 1702 } // namespace content |
| OLD | NEW |