| 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" |
| 8 #include "base/strings/string_number_conversions.h" |
| 7 #include "content/browser/devtools/service_worker_devtools_manager.h" | 9 #include "content/browser/devtools/service_worker_devtools_manager.h" |
| 10 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 8 #include "content/public/browser/devtools_agent_host.h" | 14 #include "content/public/browser/devtools_agent_host.h" |
| 15 #include "content/public/browser/storage_partition.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 // Windows headers will redefine SendMessage. |
| 19 #ifdef SendMessage |
| 20 #undef SendMessage |
| 21 #endif |
| 9 | 22 |
| 10 namespace content { | 23 namespace content { |
| 11 namespace devtools { | 24 namespace devtools { |
| 12 namespace service_worker { | 25 namespace service_worker { |
| 13 | 26 |
| 27 namespace { |
| 28 |
| 29 void GetRegistrationsOnIOThread( |
| 30 scoped_refptr<ServiceWorkerContextWrapper> context, |
| 31 const std::string& origin, |
| 32 base::Callback<void(const std::vector<ServiceWorkerRegistrationInfo>&)> |
| 33 callback) { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 35 if (origin.empty()) |
| 36 context->context()->storage()->GetAllRegistrations(callback); |
| 37 else |
| 38 context->context()->storage()->GetRegistrationsForOrigin(GURL(origin), |
| 39 callback); |
| 40 } |
| 41 |
| 42 void OnRegistrationsOnIOThread( |
| 43 base::Callback<void(const std::vector<ServiceWorkerRegistrationInfo>&)> |
| 44 callback, |
| 45 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 47 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 48 base::Bind(callback, registrations)); |
| 49 } |
| 50 |
| 51 const char* GetVersionRunningStatusString( |
| 52 content::ServiceWorkerVersion::RunningStatus running_status) { |
| 53 switch (running_status) { |
| 54 case content::ServiceWorkerVersion::STOPPED: |
| 55 return service_worker_version::kRunningStatusStopped; |
| 56 case content::ServiceWorkerVersion::STARTING: |
| 57 return service_worker_version::kRunningStatusStarting; |
| 58 case content::ServiceWorkerVersion::RUNNING: |
| 59 return service_worker_version::kRunningStatusRunning; |
| 60 case content::ServiceWorkerVersion::STOPPING: |
| 61 return service_worker_version::kRunningStatusStopping; |
| 62 } |
| 63 return ""; |
| 64 } |
| 65 |
| 66 const char* GetVersionStatusString( |
| 67 content::ServiceWorkerVersion::Status status) { |
| 68 switch (status) { |
| 69 case content::ServiceWorkerVersion::NEW: |
| 70 return service_worker_version::kStatusNew; |
| 71 case content::ServiceWorkerVersion::INSTALLING: |
| 72 return service_worker_version::kStatusInstalling; |
| 73 case content::ServiceWorkerVersion::INSTALLED: |
| 74 return service_worker_version::kStatusInstalled; |
| 75 case content::ServiceWorkerVersion::ACTIVATING: |
| 76 return service_worker_version::kStatusActivating; |
| 77 case content::ServiceWorkerVersion::ACTIVATED: |
| 78 return service_worker_version::kStatusActivated; |
| 79 case content::ServiceWorkerVersion::REDUNDANT: |
| 80 return service_worker_version::kStatusRedundant; |
| 81 } |
| 82 return ""; |
| 83 } |
| 84 |
| 85 scoped_refptr<ServiceWorkerVersion> CreateVersionDictionaryValue( |
| 86 const ServiceWorkerVersionInfo& version_info) { |
| 87 scoped_refptr<ServiceWorkerVersion> version( |
| 88 ServiceWorkerVersion::Create() |
| 89 ->set_version_id(base::Int64ToString(version_info.version_id)) |
| 90 ->set_script_url(version_info.script_url.spec()) |
| 91 ->set_running_status( |
| 92 GetVersionRunningStatusString(version_info.running_status)) |
| 93 ->set_status(GetVersionStatusString(version_info.status))); |
| 94 return version; |
| 95 } |
| 96 |
| 97 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
| 98 const ServiceWorkerRegistrationInfo& registration_info) { |
| 99 scoped_refptr<ServiceWorkerRegistration> registration( |
| 100 ServiceWorkerRegistration::Create() |
| 101 ->set_registration_id( |
| 102 base::Int64ToString(registration_info.registration_id)) |
| 103 ->set_scope_url(registration_info.pattern.spec())); |
| 104 if (registration_info.active_version.version_id != |
| 105 kInvalidServiceWorkerVersionId) { |
| 106 registration->set_active_version( |
| 107 CreateVersionDictionaryValue(registration_info.active_version)); |
| 108 } |
| 109 if (registration_info.waiting_version.version_id != |
| 110 kInvalidServiceWorkerVersionId) { |
| 111 registration->set_waiting_version( |
| 112 CreateVersionDictionaryValue(registration_info.waiting_version)); |
| 113 } |
| 114 if (registration_info.installing_version.version_id != |
| 115 kInvalidServiceWorkerVersionId) { |
| 116 registration->set_installing_version( |
| 117 CreateVersionDictionaryValue(registration_info.installing_version)); |
| 118 } |
| 119 return registration; |
| 120 } |
| 121 |
| 122 } // namespace |
| 123 |
| 14 using Response = DevToolsProtocolClient::Response; | 124 using Response = DevToolsProtocolClient::Response; |
| 15 | 125 |
| 16 ServiceWorkerHandler::ServiceWorkerHandler() { | 126 ServiceWorkerHandler::ServiceWorkerHandler() |
| 127 : render_view_host_(nullptr), weak_factory_(this) { |
| 17 } | 128 } |
| 18 | 129 |
| 19 ServiceWorkerHandler::~ServiceWorkerHandler() { | 130 ServiceWorkerHandler::~ServiceWorkerHandler() { |
| 20 Disable(); | 131 Disable(); |
| 21 } | 132 } |
| 22 | 133 |
| 23 void ServiceWorkerHandler::SetClient( | 134 void ServiceWorkerHandler::SetRenderViewHost( |
| 24 scoped_ptr<Client> client) { | 135 RenderViewHostImpl* render_view_host) { |
| 136 render_view_host_ = render_view_host; |
| 137 } |
| 138 |
| 139 void ServiceWorkerHandler::SetClient(scoped_ptr<Client> client) { |
| 25 client_.swap(client); | 140 client_.swap(client); |
| 26 } | 141 } |
| 27 | 142 |
| 28 void ServiceWorkerHandler::Detached() { | 143 void ServiceWorkerHandler::Detached() { |
| 29 Disable(); | 144 Disable(); |
| 30 } | 145 } |
| 31 | 146 |
| 32 Response ServiceWorkerHandler::Enable() { | 147 Response ServiceWorkerHandler::Enable() { |
| 33 DevToolsAgentHost::List agent_hosts; | 148 DevToolsAgentHost::List agent_hosts; |
| 34 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); | 149 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if (it == attached_hosts_.end()) | 191 if (it == attached_hosts_.end()) |
| 77 return Response::InternalError("Not connected to the worker"); | 192 return Response::InternalError("Not connected to the worker"); |
| 78 | 193 |
| 79 attached_hosts_.erase(worker_id); | 194 attached_hosts_.erase(worker_id); |
| 80 it->second->DetachClient(); | 195 it->second->DetachClient(); |
| 81 return Response::OK(); | 196 return Response::OK(); |
| 82 } | 197 } |
| 83 | 198 |
| 84 Response ServiceWorkerHandler::GetRegistrations(DevToolsCommandId command_id, | 199 Response ServiceWorkerHandler::GetRegistrations(DevToolsCommandId command_id, |
| 85 const std::string& origin) { | 200 const std::string& origin) { |
| 86 return Response::InternalError("Not yet implemented"); | 201 if (!render_view_host_) |
| 202 return Response::ServerError("Could not connect to view"); |
| 203 StoragePartition* partition = BrowserContext::GetStoragePartition( |
| 204 render_view_host_->GetProcess()->GetBrowserContext(), |
| 205 render_view_host_->GetSiteInstance()); |
| 206 scoped_refptr<ServiceWorkerContextWrapper> context = |
| 207 static_cast<ServiceWorkerContextWrapper*>( |
| 208 partition->GetServiceWorkerContext()); |
| 209 BrowserThread::PostTask( |
| 210 BrowserThread::IO, FROM_HERE, |
| 211 base::Bind( |
| 212 GetRegistrationsOnIOThread, context, origin, |
| 213 base::Bind(&OnRegistrationsOnIOThread, |
| 214 base::Bind(&ServiceWorkerHandler::OnGetRegistrations, |
| 215 weak_factory_.GetWeakPtr(), command_id)))); |
| 216 return Response::OK(); |
| 217 } |
| 218 |
| 219 void ServiceWorkerHandler::OnGetRegistrations( |
| 220 DevToolsCommandId command_id, |
| 221 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { |
| 222 std::vector<scoped_refptr<ServiceWorkerRegistration>> registration_values; |
| 223 for (const auto& registration : registrations) { |
| 224 registration_values.push_back( |
| 225 CreateRegistrationDictionaryValue(registration)); |
| 226 } |
| 227 |
| 228 client_->SendGetRegistrationsResponse( |
| 229 command_id, GetRegistrationsResponse::Create()->set_registrations( |
| 230 registration_values)); |
| 87 } | 231 } |
| 88 | 232 |
| 89 void ServiceWorkerHandler::DispatchProtocolMessage( | 233 void ServiceWorkerHandler::DispatchProtocolMessage( |
| 90 DevToolsAgentHost* host, | 234 DevToolsAgentHost* host, |
| 91 const std::string& message) { | 235 const std::string& message) { |
| 92 auto it = attached_hosts_.find(host->GetId()); | 236 auto it = attached_hosts_.find(host->GetId()); |
| 93 if (it == attached_hosts_.end()) | 237 if (it == attached_hosts_.end()) |
| 94 return; // Already disconnected. | 238 return; // Already disconnected. |
| 95 | 239 |
| 96 client_->DispatchMessage( | 240 client_->DispatchMessage( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 118 if (it == attached_hosts_.end()) | 262 if (it == attached_hosts_.end()) |
| 119 return; | 263 return; |
| 120 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 264 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 121 set_worker_id(host->GetId())); | 265 set_worker_id(host->GetId())); |
| 122 attached_hosts_.erase(it); | 266 attached_hosts_.erase(it); |
| 123 } | 267 } |
| 124 | 268 |
| 125 } // namespace service_worker | 269 } // namespace service_worker |
| 126 } // namespace devtools | 270 } // namespace devtools |
| 127 } // namespace content | 271 } // namespace content |
| OLD | NEW |