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