Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: content/browser/service_worker/service_worker_dispatcher_host.cc

Issue 1800953003: ServiceWorker: Move ExtendableMessageEvent dispatching out of ServiceWorkerVersion (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove unnecessary code Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_dispatcher_host.h" 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/profiler/scoped_tracker.h" 11 #include "base/profiler/scoped_tracker.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
13 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
14 #include "content/browser/bad_message.h" 15 #include "content/browser/bad_message.h"
15 #include "content/browser/message_port_message_filter.h" 16 #include "content/browser/message_port_message_filter.h"
16 #include "content/browser/message_port_service.h" 17 #include "content/browser/message_port_service.h"
17 #include "content/browser/service_worker/embedded_worker_registry.h" 18 #include "content/browser/service_worker/embedded_worker_registry.h"
19 #include "content/browser/service_worker/service_worker_client_utils.h"
18 #include "content/browser/service_worker/service_worker_context_core.h" 20 #include "content/browser/service_worker/service_worker_context_core.h"
19 #include "content/browser/service_worker/service_worker_context_wrapper.h" 21 #include "content/browser/service_worker/service_worker_context_wrapper.h"
20 #include "content/browser/service_worker/service_worker_handle.h" 22 #include "content/browser/service_worker/service_worker_handle.h"
21 #include "content/browser/service_worker/service_worker_navigation_handle_core.h " 23 #include "content/browser/service_worker/service_worker_navigation_handle_core.h "
22 #include "content/browser/service_worker/service_worker_registration.h" 24 #include "content/browser/service_worker/service_worker_registration.h"
23 #include "content/browser/service_worker/service_worker_registration_handle.h" 25 #include "content/browser/service_worker/service_worker_registration_handle.h"
24 #include "content/common/service_worker/embedded_worker_messages.h" 26 #include "content/common/service_worker/embedded_worker_messages.h"
25 #include "content/common/service_worker/service_worker_messages.h" 27 #include "content/common/service_worker/service_worker_messages.h"
26 #include "content/common/service_worker/service_worker_types.h" 28 #include "content/common/service_worker/service_worker_types.h"
27 #include "content/common/service_worker/service_worker_utils.h" 29 #include "content/common/service_worker/service_worker_utils.h"
(...skipping 16 matching lines...) Expand all
44 const char kShutdownErrorMessage[] = 46 const char kShutdownErrorMessage[] =
45 "The Service Worker system has shutdown."; 47 "The Service Worker system has shutdown.";
46 const char kUserDeniedPermissionMessage[] = 48 const char kUserDeniedPermissionMessage[] =
47 "The user denied permission to use Service Worker."; 49 "The user denied permission to use Service Worker.";
48 const char kInvalidStateErrorMessage[] = "The object is in an invalid state."; 50 const char kInvalidStateErrorMessage[] = "The object is in an invalid state.";
49 51
50 const uint32_t kFilteredMessageClasses[] = { 52 const uint32_t kFilteredMessageClasses[] = {
51 ServiceWorkerMsgStart, EmbeddedWorkerMsgStart, 53 ServiceWorkerMsgStart, EmbeddedWorkerMsgStart,
52 }; 54 };
53 55
56 void RunSoon(const base::Closure& callback) {
57 if (!callback.is_null())
58 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
59 }
60
54 bool CanUnregisterServiceWorker(const GURL& document_url, 61 bool CanUnregisterServiceWorker(const GURL& document_url,
55 const GURL& pattern) { 62 const GURL& pattern) {
56 DCHECK(document_url.is_valid()); 63 DCHECK(document_url.is_valid());
57 DCHECK(pattern.is_valid()); 64 DCHECK(pattern.is_valid());
58 return document_url.GetOrigin() == pattern.GetOrigin() && 65 return document_url.GetOrigin() == pattern.GetOrigin() &&
59 OriginCanAccessServiceWorkers(document_url) && 66 OriginCanAccessServiceWorkers(document_url) &&
60 OriginCanAccessServiceWorkers(pattern); 67 OriginCanAccessServiceWorkers(pattern);
61 } 68 }
62 69
63 bool CanUpdateServiceWorker(const GURL& document_url, const GURL& pattern) { 70 bool CanUpdateServiceWorker(const GURL& document_url, const GURL& pattern) {
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 return; 694 return;
688 } 695 }
689 696
690 ServiceWorkerProviderHost* sender_provider_host = 697 ServiceWorkerProviderHost* sender_provider_host =
691 GetContext()->GetProviderHost(render_process_id_, provider_id); 698 GetContext()->GetProviderHost(render_process_id_, provider_id);
692 if (!sender_provider_host) { 699 if (!sender_provider_host) {
693 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); 700 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE);
694 return; 701 return;
695 } 702 }
696 703
697 handle->version()->DispatchExtendableMessageEvent( 704 for (const TransferredMessagePort& port : sent_message_ports)
698 sender_provider_host, message, source_origin, sent_message_ports, 705 MessagePortService::GetInstance()->HoldMessages(port.id);
699 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 706
707 switch (sender_provider_host->provider_type()) {
708 case SERVICE_WORKER_PROVIDER_FOR_WINDOW:
709 case SERVICE_WORKER_PROVIDER_FOR_WORKER:
710 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER:
711 service_worker_client_utils::GetClient(
712 sender_provider_host,
713 base::Bind(
714 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent<
715 ServiceWorkerClientInfo>,
716 this, make_scoped_refptr(handle->version()), message,
717 source_origin, sent_message_ports));
718 break;
719 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER:
720 // TODO(nhiroki): Decrement a reference to ServiceWorkerHandle if starting
721 // worker fails (http://crbug.com/543198).
722 RunSoon(base::Bind(
723 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent<
724 ServiceWorkerObjectInfo>,
725 this, make_scoped_refptr(handle->version()), message, source_origin,
726 sent_message_ports,
727 sender_provider_host->GetOrCreateServiceWorkerHandle(
728 sender_provider_host->running_hosted_version())));
729 break;
730 case SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME:
731 case SERVICE_WORKER_PROVIDER_UNKNOWN:
732 NOTREACHED() << sender_provider_host->provider_type();
733 break;
734 }
700 } 735 }
701 736
702 void ServiceWorkerDispatcherHost::OnDeprecatedPostMessageToWorker( 737 void ServiceWorkerDispatcherHost::OnDeprecatedPostMessageToWorker(
703 int handle_id, 738 int handle_id,
704 const base::string16& message, 739 const base::string16& message,
705 const std::vector<TransferredMessagePort>& sent_message_ports) { 740 const std::vector<TransferredMessagePort>& sent_message_ports) {
706 TRACE_EVENT0("ServiceWorker", 741 TRACE_EVENT0("ServiceWorker",
707 "ServiceWorkerDispatcherHost::OnDeprecatedPostMessageToWorker"); 742 "ServiceWorkerDispatcherHost::OnDeprecatedPostMessageToWorker");
708 if (!GetContext()) 743 if (!GetContext())
709 return; 744 return;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 858
824 ServiceWorkerRegistrationObjectInfo info; 859 ServiceWorkerRegistrationObjectInfo info;
825 ServiceWorkerVersionAttributes attrs; 860 ServiceWorkerVersionAttributes attrs;
826 GetRegistrationObjectInfoAndVersionAttributes( 861 GetRegistrationObjectInfoAndVersionAttributes(
827 provider_host->AsWeakPtr(), registration, &info, &attrs); 862 provider_host->AsWeakPtr(), registration, &info, &attrs);
828 863
829 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, 864 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId,
830 provider_id, info, attrs)); 865 provider_id, info, attrs));
831 } 866 }
832 867
868 template <typename SourceInfo>
869 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent(
870 scoped_refptr<ServiceWorkerVersion> worker,
871 const base::string16& message,
872 const url::Origin& source_origin,
873 const std::vector<TransferredMessagePort>& sent_message_ports,
874 const SourceInfo& source_info) {
875 if (!source_info.IsValid()) {
876 DidFailToDispatchExtendableMessageEvent(sent_message_ports,
877 SERVICE_WORKER_ERROR_FAILED);
878 return;
879 }
880 worker->RunAfterStartWorker(
881 base::Bind(&ServiceWorkerDispatcherHost::
882 DispatchExtendableMessageEventAfterStartWorker,
883 this, worker, message, source_origin, sent_message_ports,
884 ExtendableMessageEventSource(source_info)),
885 base::Bind(
886 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent,
887 this, sent_message_ports));
888 }
889
890 void ServiceWorkerDispatcherHost::
891 DispatchExtendableMessageEventAfterStartWorker(
892 scoped_refptr<ServiceWorkerVersion> worker,
893 const base::string16& message,
894 const url::Origin& source_origin,
895 const std::vector<TransferredMessagePort>& sent_message_ports,
896 const ExtendableMessageEventSource& source) {
897 int request_id =
898 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE,
899 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
900
901 MessagePortMessageFilter* filter =
902 worker->embedded_worker()->message_port_message_filter();
903 std::vector<int> new_routing_ids;
904 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids);
905
906 ServiceWorkerMsg_ExtendableMessageEvent_Params params;
907 params.message = message;
908 params.source_origin = source_origin;
909 params.message_ports = sent_message_ports;
910 params.new_routing_ids = new_routing_ids;
911 params.source = source;
912
913 // Hide the client url if the client has a unique origin.
914 if (source_origin.unique()) {
915 if (params.source.client_info.IsValid())
916 params.source.client_info.url = GURL();
917 else
918 params.source.service_worker_info.url = GURL();
919 }
920
921 worker->DispatchSimpleEvent<
922 ServiceWorkerHostMsg_ExtendableMessageEventFinished>(
923 request_id, ServiceWorkerMsg_ExtendableMessageEvent(request_id, params));
924 }
925
926 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent(
927 const std::vector<TransferredMessagePort>& sent_message_ports,
928 ServiceWorkerStatusCode status) {
929 // Transfering the message ports failed, so destroy the ports.
930 for (const TransferredMessagePort& port : sent_message_ports)
931 MessagePortService::GetInstance()->ClosePort(port.id);
932 }
933
833 ServiceWorkerRegistrationHandle* 934 ServiceWorkerRegistrationHandle*
834 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, 935 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id,
835 int64_t registration_id) { 936 int64_t registration_id) {
836 for (RegistrationHandleMap::iterator iter(&registration_handles_); 937 for (RegistrationHandleMap::iterator iter(&registration_handles_);
837 !iter.IsAtEnd(); iter.Advance()) { 938 !iter.IsAtEnd(); iter.Advance()) {
838 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); 939 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue();
839 if (handle->provider_id() == provider_id && 940 if (handle->provider_id() == provider_id &&
840 handle->registration()->id() == registration_id) { 941 handle->registration()->id() == registration_id) {
841 return handle; 942 return handle;
842 } 943 }
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
1288 if (!handle) { 1389 if (!handle) {
1289 bad_message::ReceivedBadMessage(this, 1390 bad_message::ReceivedBadMessage(this,
1290 bad_message::SWDH_TERMINATE_BAD_HANDLE); 1391 bad_message::SWDH_TERMINATE_BAD_HANDLE);
1291 return; 1392 return;
1292 } 1393 }
1293 handle->version()->StopWorker( 1394 handle->version()->StopWorker(
1294 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1395 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1295 } 1396 }
1296 1397
1297 } // namespace content 1398 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698