| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/devtools/protocol/service_worker_handler.h" | 5 #include "content/browser/devtools/protocol/service_worker_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/containers/scoped_ptr_hash_map.h" |
| 8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 9 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 10 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| 10 #include "content/browser/devtools/service_worker_devtools_manager.h" | 11 #include "content/browser/devtools/service_worker_devtools_manager.h" |
| 11 #include "content/browser/service_worker/service_worker_context_observer.h" | 12 #include "content/browser/service_worker/service_worker_context_watcher.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 14 #include "content/browser/service_worker/service_worker_version.h" |
| 13 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/devtools_agent_host.h" | 17 #include "content/public/browser/devtools_agent_host.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/storage_partition.h" | 20 #include "content/public/browser/storage_partition.h" |
| 19 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 20 | 22 |
| 21 // Windows headers will redefine SendMessage. | 23 // Windows headers will redefine SendMessage. |
| 22 #ifdef SendMessage | 24 #ifdef SendMessage |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 ->set_status(GetVersionStatusString(version_info.status))); | 78 ->set_status(GetVersionStatusString(version_info.status))); |
| 77 return version; | 79 return version; |
| 78 } | 80 } |
| 79 | 81 |
| 80 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( | 82 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
| 81 const ServiceWorkerRegistrationInfo& registration_info) { | 83 const ServiceWorkerRegistrationInfo& registration_info) { |
| 82 scoped_refptr<ServiceWorkerRegistration> registration( | 84 scoped_refptr<ServiceWorkerRegistration> registration( |
| 83 ServiceWorkerRegistration::Create() | 85 ServiceWorkerRegistration::Create() |
| 84 ->set_registration_id( | 86 ->set_registration_id( |
| 85 base::Int64ToString(registration_info.registration_id)) | 87 base::Int64ToString(registration_info.registration_id)) |
| 86 ->set_scope_url(registration_info.pattern.spec())); | 88 ->set_scope_url(registration_info.pattern.spec()) |
| 87 if (registration_info.active_version.version_id != | 89 ->set_is_deleted(registration_info.delete_flag == |
| 88 kInvalidServiceWorkerVersionId) { | 90 ServiceWorkerRegistrationInfo::IsDeleted)); |
| 89 registration->set_active_version( | |
| 90 CreateVersionDictionaryValue(registration_info.active_version)); | |
| 91 } | |
| 92 if (registration_info.waiting_version.version_id != | |
| 93 kInvalidServiceWorkerVersionId) { | |
| 94 registration->set_waiting_version( | |
| 95 CreateVersionDictionaryValue(registration_info.waiting_version)); | |
| 96 } | |
| 97 if (registration_info.installing_version.version_id != | |
| 98 kInvalidServiceWorkerVersionId) { | |
| 99 registration->set_installing_version( | |
| 100 CreateVersionDictionaryValue(registration_info.installing_version)); | |
| 101 } | |
| 102 return registration; | 91 return registration; |
| 103 } | 92 } |
| 104 | 93 |
| 105 } // namespace | 94 } // namespace |
| 106 | 95 |
| 107 using Response = DevToolsProtocolClient::Response; | 96 using Response = DevToolsProtocolClient::Response; |
| 108 | 97 |
| 109 class ServiceWorkerHandler::ContextObserver | |
| 110 : public ServiceWorkerContextObserver, | |
| 111 public base::RefCountedThreadSafe<ContextObserver> { | |
| 112 public: | |
| 113 ContextObserver(scoped_refptr<ServiceWorkerContextWrapper> context, | |
| 114 base::WeakPtr<ServiceWorkerHandler> handler); | |
| 115 void Start(); | |
| 116 void Stop(); | |
| 117 | |
| 118 private: | |
| 119 friend class base::RefCountedThreadSafe<ContextObserver>; | |
| 120 ~ContextObserver() override; | |
| 121 void GetStoredRegistrationsOnIOThread(); | |
| 122 void OnStoredRegistrationsOnIOThread( | |
| 123 const std::vector<ServiceWorkerRegistrationInfo>& registrations); | |
| 124 void StopOnIOThread(); | |
| 125 | |
| 126 void OnVersionUpdated(int64 version_id); | |
| 127 void OnRegistrationUpdated(int64 registration_id); | |
| 128 | |
| 129 // ServiceWorkerContextObserver implements | |
| 130 void OnRunningStateChanged(int64 version_id) override; | |
| 131 void OnVersionStateChanged(int64 version_id) override; | |
| 132 void OnRegistrationStored(int64 registration_id, | |
| 133 const GURL& pattern) override; | |
| 134 void OnRegistrationDeleted(int64 registration_id, | |
| 135 const GURL& pattern) override; | |
| 136 | |
| 137 scoped_refptr<ServiceWorkerContextWrapper> context_; | |
| 138 base::WeakPtr<ServiceWorkerHandler> handler_; | |
| 139 }; | |
| 140 | |
| 141 ServiceWorkerHandler::ContextObserver::ContextObserver( | |
| 142 scoped_refptr<ServiceWorkerContextWrapper> context, | |
| 143 base::WeakPtr<ServiceWorkerHandler> handler) | |
| 144 : context_(context), handler_(handler) { | |
| 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 146 } | |
| 147 | |
| 148 void ServiceWorkerHandler::ContextObserver::Start() { | |
| 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 150 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 151 base::Bind(&ServiceWorkerHandler::ContextObserver:: | |
| 152 GetStoredRegistrationsOnIOThread, | |
| 153 this)); | |
| 154 } | |
| 155 | |
| 156 void ServiceWorkerHandler::ContextObserver::Stop() { | |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 158 BrowserThread::PostTask( | |
| 159 BrowserThread::IO, FROM_HERE, | |
| 160 base::Bind(&ServiceWorkerHandler::ContextObserver::StopOnIOThread, this)); | |
| 161 } | |
| 162 | |
| 163 void ServiceWorkerHandler::ContextObserver::GetStoredRegistrationsOnIOThread() { | |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 165 context_->context()->storage()->GetAllRegistrations(base::Bind( | |
| 166 &ServiceWorkerHandler::ContextObserver::OnStoredRegistrationsOnIOThread, | |
| 167 this)); | |
| 168 } | |
| 169 | |
| 170 void ServiceWorkerHandler::ContextObserver::OnStoredRegistrationsOnIOThread( | |
| 171 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 173 context_->AddObserver(this); | |
| 174 BrowserThread::PostTask( | |
| 175 BrowserThread::UI, FROM_HERE, | |
| 176 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_, | |
| 177 registrations)); | |
| 178 BrowserThread::PostTask( | |
| 179 BrowserThread::UI, FROM_HERE, | |
| 180 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_, | |
| 181 context_->context()->GetAllLiveRegistrationInfo())); | |
| 182 BrowserThread::PostTask( | |
| 183 BrowserThread::UI, FROM_HERE, | |
| 184 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, handler_, | |
| 185 context_->context()->GetAllLiveVersionInfo())); | |
| 186 } | |
| 187 | |
| 188 void ServiceWorkerHandler::ContextObserver::StopOnIOThread() { | |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 190 context_->RemoveObserver(this); | |
| 191 } | |
| 192 | |
| 193 ServiceWorkerHandler::ContextObserver::~ContextObserver() { | |
| 194 } | |
| 195 | |
| 196 void ServiceWorkerHandler::ContextObserver::OnVersionUpdated(int64 version_id) { | |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 198 content::ServiceWorkerVersion* version = | |
| 199 context_->context()->GetLiveVersion(version_id); | |
| 200 if (!version) | |
| 201 return; | |
| 202 OnRegistrationUpdated(version->registration_id()); | |
| 203 std::vector<ServiceWorkerVersionInfo> versions; | |
| 204 versions.push_back(version->GetInfo()); | |
| 205 BrowserThread::PostTask( | |
| 206 BrowserThread::UI, FROM_HERE, | |
| 207 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, handler_, | |
| 208 versions)); | |
| 209 } | |
| 210 | |
| 211 void ServiceWorkerHandler::ContextObserver::OnRegistrationUpdated( | |
| 212 int64 registration_id) { | |
| 213 content::ServiceWorkerRegistration* registration = | |
| 214 context_->context()->GetLiveRegistration(registration_id); | |
| 215 if (!registration) | |
| 216 return; | |
| 217 std::vector<ServiceWorkerRegistrationInfo> registrations; | |
| 218 registrations.push_back(registration->GetInfo()); | |
| 219 BrowserThread::PostTask( | |
| 220 BrowserThread::UI, FROM_HERE, | |
| 221 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_, | |
| 222 registrations)); | |
| 223 } | |
| 224 | |
| 225 void ServiceWorkerHandler::ContextObserver::OnRunningStateChanged( | |
| 226 int64 version_id) { | |
| 227 OnVersionUpdated(version_id); | |
| 228 } | |
| 229 | |
| 230 void ServiceWorkerHandler::ContextObserver::OnVersionStateChanged( | |
| 231 int64 version_id) { | |
| 232 OnVersionUpdated(version_id); | |
| 233 } | |
| 234 | |
| 235 void ServiceWorkerHandler::ContextObserver::OnRegistrationStored( | |
| 236 int64 registration_id, | |
| 237 const GURL& pattern) { | |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 239 content::ServiceWorkerRegistration* registration = | |
| 240 context_->context()->GetLiveRegistration(registration_id); | |
| 241 DCHECK(registration); | |
| 242 std::vector<ServiceWorkerRegistrationInfo> registrations; | |
| 243 registrations.push_back(registration->GetInfo()); | |
| 244 BrowserThread::PostTask( | |
| 245 BrowserThread::UI, FROM_HERE, | |
| 246 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, handler_, | |
| 247 registrations)); | |
| 248 } | |
| 249 | |
| 250 void ServiceWorkerHandler::ContextObserver::OnRegistrationDeleted( | |
| 251 int64 registration_id, | |
| 252 const GURL& pattern) { | |
| 253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 254 BrowserThread::PostTask( | |
| 255 BrowserThread::UI, FROM_HERE, | |
| 256 base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationDeleted, handler_, | |
| 257 registration_id)); | |
| 258 } | |
| 259 | |
| 260 ServiceWorkerHandler::ServiceWorkerHandler() | 98 ServiceWorkerHandler::ServiceWorkerHandler() |
| 261 : enabled_(false), weak_factory_(this) { | 99 : enabled_(false), weak_factory_(this) { |
| 262 } | 100 } |
| 263 | 101 |
| 264 ServiceWorkerHandler::~ServiceWorkerHandler() { | 102 ServiceWorkerHandler::~ServiceWorkerHandler() { |
| 265 Disable(); | 103 Disable(); |
| 266 } | 104 } |
| 267 | 105 |
| 268 void ServiceWorkerHandler::SetRenderFrameHost( | 106 void ServiceWorkerHandler::SetRenderFrameHost( |
| 269 RenderFrameHost* render_frame_host) { | 107 RenderFrameHost* render_frame_host) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 return Response::InternalError("Could not connect to the context"); | 156 return Response::InternalError("Could not connect to the context"); |
| 319 enabled_ = true; | 157 enabled_ = true; |
| 320 | 158 |
| 321 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | 159 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| 322 | 160 |
| 323 ServiceWorkerDevToolsAgentHost::List agent_hosts; | 161 ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| 324 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); | 162 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| 325 for (auto host : agent_hosts) | 163 for (auto host : agent_hosts) |
| 326 WorkerReadyForInspection(host.get()); | 164 WorkerReadyForInspection(host.get()); |
| 327 | 165 |
| 328 context_observer_ = new ContextObserver(context_, weak_factory_.GetWeakPtr()); | 166 context_watcher_ = new ServiceWorkerContextWatcher( |
| 329 context_observer_->Start(); | 167 context_, base::Bind(&ServiceWorkerHandler::OnWorkerRegistrationUpdated, |
| 168 weak_factory_.GetWeakPtr()), |
| 169 base::Bind(&ServiceWorkerHandler::OnWorkerVersionUpdated, |
| 170 weak_factory_.GetWeakPtr())); |
| 171 context_watcher_->Start(); |
| 330 return Response::OK(); | 172 return Response::OK(); |
| 331 } | 173 } |
| 332 | 174 |
| 333 Response ServiceWorkerHandler::Disable() { | 175 Response ServiceWorkerHandler::Disable() { |
| 334 if (!enabled_) | 176 if (!enabled_) |
| 335 return Response::OK(); | 177 return Response::OK(); |
| 336 enabled_ = false; | 178 enabled_ = false; |
| 337 | 179 |
| 338 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | 180 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| 339 for (const auto& pair : attached_hosts_) | 181 for (const auto& pair : attached_hosts_) |
| 340 pair.second->DetachClient(); | 182 pair.second->DetachClient(); |
| 341 attached_hosts_.clear(); | 183 attached_hosts_.clear(); |
| 342 DCHECK(context_observer_); | 184 DCHECK(context_watcher_); |
| 343 context_observer_->Stop(); | 185 context_watcher_->Stop(); |
| 344 context_observer_ = nullptr; | 186 context_watcher_ = nullptr; |
| 345 return Response::OK(); | 187 return Response::OK(); |
| 346 } | 188 } |
| 347 | 189 |
| 348 Response ServiceWorkerHandler::SendMessage( | 190 Response ServiceWorkerHandler::SendMessage( |
| 349 const std::string& worker_id, | 191 const std::string& worker_id, |
| 350 const std::string& message) { | 192 const std::string& message) { |
| 351 auto it = attached_hosts_.find(worker_id); | 193 auto it = attached_hosts_.find(worker_id); |
| 352 if (it == attached_hosts_.end()) | 194 if (it == attached_hosts_.end()) |
| 353 return Response::InternalError("Not connected to the worker"); | 195 return Response::InternalError("Not connected to the worker"); |
| 354 it->second->DispatchProtocolMessage(message); | 196 it->second->DispatchProtocolMessage(message); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 379 void ServiceWorkerHandler::OnWorkerVersionUpdated( | 221 void ServiceWorkerHandler::OnWorkerVersionUpdated( |
| 380 const std::vector<ServiceWorkerVersionInfo>& versions) { | 222 const std::vector<ServiceWorkerVersionInfo>& versions) { |
| 381 std::vector<scoped_refptr<ServiceWorkerVersion>> version_values; | 223 std::vector<scoped_refptr<ServiceWorkerVersion>> version_values; |
| 382 for (const auto& version : versions) { | 224 for (const auto& version : versions) { |
| 383 version_values.push_back(CreateVersionDictionaryValue(version)); | 225 version_values.push_back(CreateVersionDictionaryValue(version)); |
| 384 } | 226 } |
| 385 client_->WorkerVersionUpdated( | 227 client_->WorkerVersionUpdated( |
| 386 WorkerVersionUpdatedParams::Create()->set_versions(version_values)); | 228 WorkerVersionUpdatedParams::Create()->set_versions(version_values)); |
| 387 } | 229 } |
| 388 | 230 |
| 389 void ServiceWorkerHandler::OnWorkerRegistrationDeleted(int64 registration_id) { | |
| 390 client_->WorkerRegistrationDeleted( | |
| 391 WorkerRegistrationDeletedParams::Create()->set_registration_id( | |
| 392 base::Int64ToString(registration_id))); | |
| 393 } | |
| 394 | |
| 395 void ServiceWorkerHandler::DispatchProtocolMessage( | 231 void ServiceWorkerHandler::DispatchProtocolMessage( |
| 396 DevToolsAgentHost* host, | 232 DevToolsAgentHost* host, |
| 397 const std::string& message) { | 233 const std::string& message) { |
| 398 | 234 |
| 399 auto it = attached_hosts_.find(host->GetId()); | 235 auto it = attached_hosts_.find(host->GetId()); |
| 400 if (it == attached_hosts_.end()) | 236 if (it == attached_hosts_.end()) |
| 401 return; // Already disconnected. | 237 return; // Already disconnected. |
| 402 | 238 |
| 403 client_->DispatchMessage( | 239 client_->DispatchMessage( |
| 404 DispatchMessageParams::Create()-> | 240 DispatchMessageParams::Create()-> |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 bool ServiceWorkerHandler::MatchesInspectedPage( | 281 bool ServiceWorkerHandler::MatchesInspectedPage( |
| 446 ServiceWorkerDevToolsAgentHost* host) { | 282 ServiceWorkerDevToolsAgentHost* host) { |
| 447 // TODO(pfeldman): match based on scope. | 283 // TODO(pfeldman): match based on scope. |
| 448 // TODO(pfeldman): match iframes. | 284 // TODO(pfeldman): match iframes. |
| 449 return host->GetURL().host() == url_.host(); | 285 return host->GetURL().host() == url_.host(); |
| 450 } | 286 } |
| 451 | 287 |
| 452 } // namespace service_worker | 288 } // namespace service_worker |
| 453 } // namespace devtools | 289 } // namespace devtools |
| 454 } // namespace content | 290 } // namespace content |
| OLD | NEW |