| Index: content/browser/devtools/protocol/service_worker_handler.cc
|
| diff --git a/content/browser/devtools/protocol/service_worker_handler.cc b/content/browser/devtools/protocol/service_worker_handler.cc
|
| index 8ff8a62c0b66497ae396662ee48e37a311962edb..93919986cfc4d7c68aceb1e3640e47e4f12f8edd 100644
|
| --- a/content/browser/devtools/protocol/service_worker_handler.cc
|
| +++ b/content/browser/devtools/protocol/service_worker_handler.cc
|
| @@ -4,24 +4,139 @@
|
|
|
| #include "content/browser/devtools/protocol/service_worker_handler.h"
|
|
|
| +#include "base/bind.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| #include "content/browser/devtools/service_worker_devtools_manager.h"
|
| +#include "content/browser/renderer_host/render_view_host_impl.h"
|
| +#include "content/browser/service_worker/service_worker_context_wrapper.h"
|
| +#include "content/public/browser/browser_context.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| #include "content/public/browser/devtools_agent_host.h"
|
| +#include "content/public/browser/storage_partition.h"
|
| +#include "url/gurl.h"
|
| +
|
| +// Windows headers will redefine SendMessage.
|
| +#ifdef SendMessage
|
| +#undef SendMessage
|
| +#endif
|
|
|
| namespace content {
|
| namespace devtools {
|
| namespace service_worker {
|
|
|
| +namespace {
|
| +
|
| +void GetRegistrationsOnIOThread(
|
| + scoped_refptr<ServiceWorkerContextWrapper> context,
|
| + const std::string& origin,
|
| + base::Callback<void(const std::vector<ServiceWorkerRegistrationInfo>&)>
|
| + callback) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + if (origin.empty())
|
| + context->context()->storage()->GetAllRegistrations(callback);
|
| + else
|
| + context->context()->storage()->GetRegistrationsForOrigin(GURL(origin),
|
| + callback);
|
| +}
|
| +
|
| +void OnRegistrationsOnIOThread(
|
| + base::Callback<void(const std::vector<ServiceWorkerRegistrationInfo>&)>
|
| + callback,
|
| + const std::vector<ServiceWorkerRegistrationInfo>& registrations) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
| + base::Bind(callback, registrations));
|
| +}
|
| +
|
| +const char* GetVersionRunningStatusString(
|
| + content::ServiceWorkerVersion::RunningStatus running_status) {
|
| + switch (running_status) {
|
| + case content::ServiceWorkerVersion::STOPPED:
|
| + return service_worker_version::kRunningStatusStopped;
|
| + case content::ServiceWorkerVersion::STARTING:
|
| + return service_worker_version::kRunningStatusStarting;
|
| + case content::ServiceWorkerVersion::RUNNING:
|
| + return service_worker_version::kRunningStatusRunning;
|
| + case content::ServiceWorkerVersion::STOPPING:
|
| + return service_worker_version::kRunningStatusStopping;
|
| + }
|
| + return "";
|
| +}
|
| +
|
| +const char* GetVersionStatusString(
|
| + content::ServiceWorkerVersion::Status status) {
|
| + switch (status) {
|
| + case content::ServiceWorkerVersion::NEW:
|
| + return service_worker_version::kStatusNew;
|
| + case content::ServiceWorkerVersion::INSTALLING:
|
| + return service_worker_version::kStatusInstalling;
|
| + case content::ServiceWorkerVersion::INSTALLED:
|
| + return service_worker_version::kStatusInstalled;
|
| + case content::ServiceWorkerVersion::ACTIVATING:
|
| + return service_worker_version::kStatusActivating;
|
| + case content::ServiceWorkerVersion::ACTIVATED:
|
| + return service_worker_version::kStatusActivated;
|
| + case content::ServiceWorkerVersion::REDUNDANT:
|
| + return service_worker_version::kStatusRedundant;
|
| + }
|
| + return "";
|
| +}
|
| +
|
| +scoped_refptr<ServiceWorkerVersion> CreateVersionDictionaryValue(
|
| + const ServiceWorkerVersionInfo& version_info) {
|
| + scoped_refptr<ServiceWorkerVersion> version(
|
| + ServiceWorkerVersion::Create()
|
| + ->set_version_id(base::Int64ToString(version_info.version_id))
|
| + ->set_script_url(version_info.script_url.spec())
|
| + ->set_running_status(
|
| + GetVersionRunningStatusString(version_info.running_status))
|
| + ->set_status(GetVersionStatusString(version_info.status)));
|
| + return version;
|
| +}
|
| +
|
| +scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue(
|
| + const ServiceWorkerRegistrationInfo& registration_info) {
|
| + scoped_refptr<ServiceWorkerRegistration> registration(
|
| + ServiceWorkerRegistration::Create()
|
| + ->set_registration_id(
|
| + base::Int64ToString(registration_info.registration_id))
|
| + ->set_scope_url(registration_info.pattern.spec()));
|
| + if (registration_info.active_version.version_id !=
|
| + kInvalidServiceWorkerVersionId) {
|
| + registration->set_active_version(
|
| + CreateVersionDictionaryValue(registration_info.active_version));
|
| + }
|
| + if (registration_info.waiting_version.version_id !=
|
| + kInvalidServiceWorkerVersionId) {
|
| + registration->set_waiting_version(
|
| + CreateVersionDictionaryValue(registration_info.waiting_version));
|
| + }
|
| + if (registration_info.installing_version.version_id !=
|
| + kInvalidServiceWorkerVersionId) {
|
| + registration->set_installing_version(
|
| + CreateVersionDictionaryValue(registration_info.installing_version));
|
| + }
|
| + return registration;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| using Response = DevToolsProtocolClient::Response;
|
|
|
| -ServiceWorkerHandler::ServiceWorkerHandler() {
|
| +ServiceWorkerHandler::ServiceWorkerHandler()
|
| + : render_view_host_(nullptr), weak_factory_(this) {
|
| }
|
|
|
| ServiceWorkerHandler::~ServiceWorkerHandler() {
|
| Disable();
|
| }
|
|
|
| -void ServiceWorkerHandler::SetClient(
|
| - scoped_ptr<Client> client) {
|
| +void ServiceWorkerHandler::SetRenderViewHost(
|
| + RenderViewHostImpl* render_view_host) {
|
| + render_view_host_ = render_view_host;
|
| +}
|
| +
|
| +void ServiceWorkerHandler::SetClient(scoped_ptr<Client> client) {
|
| client_.swap(client);
|
| }
|
|
|
| @@ -83,7 +198,36 @@ Response ServiceWorkerHandler::Detach(const std::string& worker_id) {
|
|
|
| Response ServiceWorkerHandler::GetRegistrations(DevToolsCommandId command_id,
|
| const std::string& origin) {
|
| - return Response::InternalError("Not yet implemented");
|
| + if (!render_view_host_)
|
| + return Response::ServerError("Could not connect to view");
|
| + StoragePartition* partition = BrowserContext::GetStoragePartition(
|
| + render_view_host_->GetProcess()->GetBrowserContext(),
|
| + render_view_host_->GetSiteInstance());
|
| + scoped_refptr<ServiceWorkerContextWrapper> context =
|
| + static_cast<ServiceWorkerContextWrapper*>(
|
| + partition->GetServiceWorkerContext());
|
| + BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + base::Bind(
|
| + GetRegistrationsOnIOThread, context, origin,
|
| + base::Bind(&OnRegistrationsOnIOThread,
|
| + base::Bind(&ServiceWorkerHandler::OnGetRegistrations,
|
| + weak_factory_.GetWeakPtr(), command_id))));
|
| + return Response::OK();
|
| +}
|
| +
|
| +void ServiceWorkerHandler::OnGetRegistrations(
|
| + DevToolsCommandId command_id,
|
| + const std::vector<ServiceWorkerRegistrationInfo>& registrations) {
|
| + std::vector<scoped_refptr<ServiceWorkerRegistration>> registration_values;
|
| + for (const auto& registration : registrations) {
|
| + registration_values.push_back(
|
| + CreateRegistrationDictionaryValue(registration));
|
| + }
|
| +
|
| + client_->SendGetRegistrationsResponse(
|
| + command_id, GetRegistrationsResponse::Create()->set_registrations(
|
| + registration_values));
|
| }
|
|
|
| void ServiceWorkerHandler::DispatchProtocolMessage(
|
|
|