| 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_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/debug/crash_logging.h" | 9 #include "base/debug/crash_logging.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 resource_context_(resource_context), | 112 resource_context_(resource_context), |
| 113 channel_ready_(false), | 113 channel_ready_(false), |
| 114 weak_factory_(this) { | 114 weak_factory_(this) { |
| 115 AddAssociatedInterface( | 115 AddAssociatedInterface( |
| 116 mojom::ServiceWorkerDispatcherHost::Name_, | 116 mojom::ServiceWorkerDispatcherHost::Name_, |
| 117 base::Bind(&ServiceWorkerDispatcherHost::AddMojoBinding, | 117 base::Bind(&ServiceWorkerDispatcherHost::AddMojoBinding, |
| 118 base::Unretained(this))); | 118 base::Unretained(this))); |
| 119 } | 119 } |
| 120 | 120 |
| 121 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 121 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 122 if (GetContext()) { | 122 if (GetContext()) |
| 123 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_); | 123 GetContext()->RemoveDispatcherHost(render_process_id_); |
| 124 GetContext()->embedded_worker_registry()->RemoveChildProcessSender( | |
| 125 render_process_id_); | |
| 126 } | |
| 127 } | 124 } |
| 128 | 125 |
| 129 void ServiceWorkerDispatcherHost::Init( | 126 void ServiceWorkerDispatcherHost::Init( |
| 130 ServiceWorkerContextWrapper* context_wrapper) { | 127 ServiceWorkerContextWrapper* context_wrapper) { |
| 131 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 128 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 132 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 129 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 133 base::Bind(&ServiceWorkerDispatcherHost::Init, this, | 130 base::Bind(&ServiceWorkerDispatcherHost::Init, this, |
| 134 base::RetainedRef(context_wrapper))); | 131 base::RetainedRef(context_wrapper))); |
| 135 return; | 132 return; |
| 136 } | 133 } |
| 137 | 134 |
| 138 context_wrapper_ = context_wrapper; | 135 context_wrapper_ = context_wrapper; |
| 139 if (!GetContext()) | 136 if (!GetContext()) |
| 140 return; | 137 return; |
| 141 GetContext()->embedded_worker_registry()->AddChildProcessSender( | 138 GetContext()->AddDispatcherHost(render_process_id_, this); |
| 142 render_process_id_, this); | |
| 143 } | 139 } |
| 144 | 140 |
| 145 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Channel* channel) { | 141 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Channel* channel) { |
| 146 TRACE_EVENT0("ServiceWorker", | 142 TRACE_EVENT0("ServiceWorker", |
| 147 "ServiceWorkerDispatcherHost::OnFilterAdded"); | 143 "ServiceWorkerDispatcherHost::OnFilterAdded"); |
| 148 channel_ready_ = true; | 144 channel_ready_ = true; |
| 149 std::vector<std::unique_ptr<IPC::Message>> messages; | 145 std::vector<std::unique_ptr<IPC::Message>> messages; |
| 150 messages.swap(pending_messages_); | 146 messages.swap(pending_messages_); |
| 151 for (auto& message : messages) { | 147 for (auto& message : messages) { |
| 152 BrowserMessageFilter::Send(message.release()); | 148 BrowserMessageFilter::Send(message.release()); |
| 153 } | 149 } |
| 154 } | 150 } |
| 155 | 151 |
| 156 void ServiceWorkerDispatcherHost::OnFilterRemoved() { | 152 void ServiceWorkerDispatcherHost::OnFilterRemoved() { |
| 157 // Don't wait until the destructor to teardown since a new dispatcher host | 153 // Don't wait until the destructor to teardown since a new dispatcher host |
| 158 // for this process might be created before then. | 154 // for this process might be created before then. |
| 159 if (GetContext()) { | 155 if (GetContext()) |
| 160 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_); | 156 GetContext()->RemoveDispatcherHost(render_process_id_); |
| 161 GetContext()->embedded_worker_registry()->RemoveChildProcessSender( | |
| 162 render_process_id_); | |
| 163 } | |
| 164 context_wrapper_ = nullptr; | 157 context_wrapper_ = nullptr; |
| 165 channel_ready_ = false; | 158 channel_ready_ = false; |
| 166 } | 159 } |
| 167 | 160 |
| 168 void ServiceWorkerDispatcherHost::OnDestruct() const { | 161 void ServiceWorkerDispatcherHost::OnDestruct() const { |
| 169 BrowserThread::DeleteOnIOThread::Destruct(this); | 162 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 170 } | 163 } |
| 171 | 164 |
| 172 bool ServiceWorkerDispatcherHost::OnMessageReceived( | 165 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
| 173 const IPC::Message& message) { | 166 const IPC::Message& message) { |
| 174 bool handled = true; | 167 bool handled = true; |
| 175 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) | 168 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) |
| 176 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
| 177 OnRegisterServiceWorker) | 170 OnRegisterServiceWorker) |
| 178 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UpdateServiceWorker, | 171 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UpdateServiceWorker, |
| 179 OnUpdateServiceWorker) | 172 OnUpdateServiceWorker) |
| 180 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 173 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
| 181 OnUnregisterServiceWorker) | 174 OnUnregisterServiceWorker) |
| 182 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, | 175 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, |
| 183 OnGetRegistration) | 176 OnGetRegistration) |
| 184 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrations, | 177 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrations, |
| 185 OnGetRegistrations) | 178 OnGetRegistrations) |
| 186 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, | 179 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, |
| 187 OnGetRegistrationForReady) | 180 OnGetRegistrationForReady) |
| 188 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | |
| 189 OnProviderCreated) | |
| 190 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, | |
| 191 OnProviderDestroyed) | |
| 192 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetVersionId, | |
| 193 OnSetHostedVersionId) | |
| 194 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, | 181 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToWorker, |
| 195 OnPostMessageToWorker) | 182 OnPostMessageToWorker) |
| 196 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerReadyForInspection, | 183 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerReadyForInspection, |
| 197 OnWorkerReadyForInspection) | 184 OnWorkerReadyForInspection) |
| 198 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptLoaded, | 185 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptLoaded, |
| 199 OnWorkerScriptLoaded) | 186 OnWorkerScriptLoaded) |
| 200 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerThreadStarted, | 187 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerThreadStarted, |
| 201 OnWorkerThreadStarted) | 188 OnWorkerThreadStarted) |
| 202 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptLoadFailed, | 189 IPC_MESSAGE_HANDLER(EmbeddedWorkerHostMsg_WorkerScriptLoadFailed, |
| 203 OnWorkerScriptLoadFailed) | 190 OnWorkerScriptLoadFailed) |
| (...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 break; | 984 break; |
| 998 } | 985 } |
| 999 case SERVICE_WORKER_PROVIDER_UNKNOWN: | 986 case SERVICE_WORKER_PROVIDER_UNKNOWN: |
| 1000 default: | 987 default: |
| 1001 NOTREACHED() << sender_provider_host->provider_type(); | 988 NOTREACHED() << sender_provider_host->provider_type(); |
| 1002 break; | 989 break; |
| 1003 } | 990 } |
| 1004 } | 991 } |
| 1005 | 992 |
| 1006 void ServiceWorkerDispatcherHost::OnProviderCreated( | 993 void ServiceWorkerDispatcherHost::OnProviderCreated( |
| 1007 int provider_id, | 994 ServiceWorkerProviderHostInfo info) { |
| 1008 int route_id, | |
| 1009 ServiceWorkerProviderType provider_type, | |
| 1010 bool is_parent_frame_secure) { | |
| 1011 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. | 995 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. |
| 1012 tracked_objects::ScopedTracker tracking_profile( | 996 tracked_objects::ScopedTracker tracking_profile( |
| 1013 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 997 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1014 "477117 ServiceWorkerDispatcherHost::OnProviderCreated")); | 998 "477117 ServiceWorkerDispatcherHost::OnProviderCreated")); |
| 1015 TRACE_EVENT0("ServiceWorker", | 999 TRACE_EVENT0("ServiceWorker", |
| 1016 "ServiceWorkerDispatcherHost::OnProviderCreated"); | 1000 "ServiceWorkerDispatcherHost::OnProviderCreated"); |
| 1017 if (!GetContext()) | 1001 if (!GetContext()) |
| 1018 return; | 1002 return; |
| 1019 if (GetContext()->GetProviderHost(render_process_id_, provider_id)) { | 1003 if (GetContext()->GetProviderHost(render_process_id_, info.provider_id)) { |
| 1020 bad_message::ReceivedBadMessage(this, | 1004 bad_message::ReceivedBadMessage(this, |
| 1021 bad_message::SWDH_PROVIDER_CREATED_NO_HOST); | 1005 bad_message::SWDH_PROVIDER_CREATED_NO_HOST); |
| 1022 return; | 1006 return; |
| 1023 } | 1007 } |
| 1024 | 1008 |
| 1025 std::unique_ptr<ServiceWorkerProviderHost> provider_host; | |
| 1026 if (IsBrowserSideNavigationEnabled() && | 1009 if (IsBrowserSideNavigationEnabled() && |
| 1027 ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id)) { | 1010 ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { |
| 1011 std::unique_ptr<ServiceWorkerProviderHost> provider_host; |
| 1028 // PlzNavigate | 1012 // PlzNavigate |
| 1029 // Retrieve the provider host previously created for navigation requests. | 1013 // Retrieve the provider host previously created for navigation requests. |
| 1030 ServiceWorkerNavigationHandleCore* navigation_handle_core = | 1014 ServiceWorkerNavigationHandleCore* navigation_handle_core = |
| 1031 GetContext()->GetNavigationHandleCore(provider_id); | 1015 GetContext()->GetNavigationHandleCore(info.provider_id); |
| 1032 if (navigation_handle_core != nullptr) | 1016 if (navigation_handle_core != nullptr) |
| 1033 provider_host = navigation_handle_core->RetrievePreCreatedHost(); | 1017 provider_host = navigation_handle_core->RetrievePreCreatedHost(); |
| 1034 | 1018 |
| 1035 // If no host is found, the navigation has been cancelled in the meantime. | 1019 // If no host is found, the navigation has been cancelled in the meantime. |
| 1036 // Just return as the navigation will be stopped in the renderer as well. | 1020 // Just return as the navigation will be stopped in the renderer as well. |
| 1037 if (provider_host == nullptr) | 1021 if (provider_host == nullptr) |
| 1038 return; | 1022 return; |
| 1039 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_WINDOW, provider_type); | 1023 DCHECK_EQ(SERVICE_WORKER_PROVIDER_FOR_WINDOW, info.type); |
| 1040 provider_host->CompleteNavigationInitialized(render_process_id_, route_id, | 1024 provider_host->CompleteNavigationInitialized(render_process_id_, |
| 1041 this); | 1025 info.route_id, this); |
| 1026 GetContext()->AddProviderHost(std::move(provider_host)); |
| 1042 } else { | 1027 } else { |
| 1043 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(provider_id)) { | 1028 if (ServiceWorkerUtils::IsBrowserAssignedProviderId(info.provider_id)) { |
| 1044 bad_message::ReceivedBadMessage( | 1029 bad_message::ReceivedBadMessage( |
| 1045 this, bad_message::SWDH_PROVIDER_CREATED_NO_HOST); | 1030 this, bad_message::SWDH_PROVIDER_CREATED_NO_HOST); |
| 1046 return; | 1031 return; |
| 1047 } | 1032 } |
| 1048 ServiceWorkerProviderHost::FrameSecurityLevel parent_frame_security_level = | 1033 GetContext()->AddProviderHost(ServiceWorkerProviderHost::Create( |
| 1049 is_parent_frame_secure | 1034 render_process_id_, std::move(info), GetContext()->AsWeakPtr(), this)); |
| 1050 ? ServiceWorkerProviderHost::FrameSecurityLevel::SECURE | |
| 1051 : ServiceWorkerProviderHost::FrameSecurityLevel::INSECURE; | |
| 1052 provider_host = std::unique_ptr<ServiceWorkerProviderHost>( | |
| 1053 new ServiceWorkerProviderHost( | |
| 1054 render_process_id_, route_id, provider_id, provider_type, | |
| 1055 parent_frame_security_level, GetContext()->AsWeakPtr(), this)); | |
| 1056 } | 1035 } |
| 1057 GetContext()->AddProviderHost(std::move(provider_host)); | |
| 1058 } | 1036 } |
| 1059 | 1037 |
| 1060 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { | 1038 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { |
| 1061 TRACE_EVENT0("ServiceWorker", | 1039 TRACE_EVENT0("ServiceWorker", |
| 1062 "ServiceWorkerDispatcherHost::OnProviderDestroyed"); | 1040 "ServiceWorkerDispatcherHost::OnProviderDestroyed"); |
| 1063 if (!GetContext()) | 1041 if (!GetContext()) |
| 1064 return; | 1042 return; |
| 1065 if (!GetContext()->GetProviderHost(render_process_id_, provider_id)) { | 1043 if (!GetContext()->GetProviderHost(render_process_id_, provider_id)) { |
| 1066 // PlzNavigate: in some cancellation of navigation cases, it is possible | 1044 // PlzNavigate: in some cancellation of navigation cases, it is possible |
| 1067 // for the pre-created hoist to have been destroyed before being claimed by | 1045 // for the pre-created hoist to have been destroyed before being claimed by |
| (...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1797 if (!handle) { | 1775 if (!handle) { |
| 1798 bad_message::ReceivedBadMessage(this, | 1776 bad_message::ReceivedBadMessage(this, |
| 1799 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1777 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1800 return; | 1778 return; |
| 1801 } | 1779 } |
| 1802 handle->version()->StopWorker( | 1780 handle->version()->StopWorker( |
| 1803 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1781 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1804 } | 1782 } |
| 1805 | 1783 |
| 1806 } // namespace content | 1784 } // namespace content |
| OLD | NEW |