feat: add API for receiving logs from service workers (#20624)
* feat: add API for receiving logs from service workers * feat: add new serviceWorkerContext APIs * chore: add missing #include's * refactor: rename serviceWorkerContext to serviceWorkers * chore: clean up based on review * chore: remove native_mate * chore: add tests for the service worker module * Update spec-main/api-service-workers-spec.ts Co-Authored-By: Jeremy Apthorp <jeremya@chromium.org> * chore: fix linting * chore: handle renames Co-authored-by: Jeremy Apthorp <nornagon@nornagon.net>
This commit is contained in:
parent
2e6fff885d
commit
e7b0a9ca8f
19 changed files with 456 additions and 324 deletions
154
shell/browser/api/electron_api_service_worker_context.cc
Normal file
154
shell/browser/api/electron_api_service_worker_context.cc
Normal file
|
@ -0,0 +1,154 @@
|
|||
// Copyright (c) 2019 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/api/electron_api_service_worker_context.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "chrome/browser/browser_process.h"
|
||||
#include "content/public/browser/console_message.h"
|
||||
#include "content/public/browser/storage_partition.h"
|
||||
#include "gin/data_object_builder.h"
|
||||
#include "gin/handle.h"
|
||||
#include "shell/browser/electron_browser_context.h"
|
||||
#include "shell/common/gin_converters/value_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/object_template_builder.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace api {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string MessageSourceToString(
|
||||
const blink::mojom::ConsoleMessageSource source) {
|
||||
if (source == blink::mojom::ConsoleMessageSource::kXml)
|
||||
return "xml";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kJavaScript)
|
||||
return "javascript";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kNetwork)
|
||||
return "network";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kConsoleApi)
|
||||
return "console-api";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kStorage)
|
||||
return "storage";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kAppCache)
|
||||
return "app-cache";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kRendering)
|
||||
return "rendering";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kSecurity)
|
||||
return "security";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kDeprecation)
|
||||
return "deprecation";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kWorker)
|
||||
return "worker";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kViolation)
|
||||
return "violation";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kIntervention)
|
||||
return "intervention";
|
||||
if (source == blink::mojom::ConsoleMessageSource::kRecommendation)
|
||||
return "recommendation";
|
||||
return "other";
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> ServiceWorkerRunningInfoToDict(
|
||||
v8::Isolate* isolate,
|
||||
const content::ServiceWorkerRunningInfo& info) {
|
||||
return gin::DataObjectBuilder(isolate)
|
||||
.Set("scriptUrl", info.script_url.spec())
|
||||
.Set("scope", info.scope.spec())
|
||||
.Set("renderProcessId", info.render_process_id)
|
||||
.Build();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ServiceWorkerContext::ServiceWorkerContext(
|
||||
v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context)
|
||||
: browser_context_(browser_context), weak_ptr_factory_(this) {
|
||||
Init(isolate);
|
||||
service_worker_context_ =
|
||||
content::BrowserContext::GetDefaultStoragePartition(browser_context_)
|
||||
->GetServiceWorkerContext();
|
||||
service_worker_context_->AddObserver(this);
|
||||
}
|
||||
|
||||
ServiceWorkerContext::~ServiceWorkerContext() {
|
||||
service_worker_context_->RemoveObserver(this);
|
||||
}
|
||||
|
||||
void ServiceWorkerContext::OnReportConsoleMessage(
|
||||
int64_t version_id,
|
||||
const content::ConsoleMessage& message) {
|
||||
Emit("console-message",
|
||||
gin::DataObjectBuilder(v8::Isolate::GetCurrent())
|
||||
.Set("versionId", version_id)
|
||||
.Set("source", MessageSourceToString(message.source))
|
||||
.Set("level", static_cast<int32_t>(message.message_level))
|
||||
.Set("message", message.message)
|
||||
.Set("lineNumber", message.line_number)
|
||||
.Set("sourceUrl", message.source_url.spec())
|
||||
.Build());
|
||||
}
|
||||
|
||||
void ServiceWorkerContext::OnDestruct(content::ServiceWorkerContext* context) {
|
||||
if (context == service_worker_context_) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> ServiceWorkerContext::GetAllRunningWorkerInfo(
|
||||
v8::Isolate* isolate) {
|
||||
gin::DataObjectBuilder builder(isolate);
|
||||
const base::flat_map<int64_t, content::ServiceWorkerRunningInfo>& info_map =
|
||||
service_worker_context_->GetRunningServiceWorkerInfos();
|
||||
for (auto iter = info_map.begin(); iter != info_map.end(); ++iter) {
|
||||
builder.Set(
|
||||
std::to_string(iter->first),
|
||||
ServiceWorkerRunningInfoToDict(isolate, std::move(iter->second)));
|
||||
}
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> ServiceWorkerContext::GetWorkerInfoFromID(
|
||||
gin_helper::ErrorThrower thrower,
|
||||
int64_t version_id) {
|
||||
const base::flat_map<int64_t, content::ServiceWorkerRunningInfo>& info_map =
|
||||
service_worker_context_->GetRunningServiceWorkerInfos();
|
||||
auto iter = info_map.find(version_id);
|
||||
if (iter == info_map.end()) {
|
||||
thrower.ThrowError("Could not find service worker with that version_id");
|
||||
return v8::Local<v8::Value>();
|
||||
}
|
||||
return ServiceWorkerRunningInfoToDict(thrower.isolate(),
|
||||
std::move(iter->second));
|
||||
}
|
||||
|
||||
// static
|
||||
gin::Handle<ServiceWorkerContext> ServiceWorkerContext::Create(
|
||||
v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context) {
|
||||
return gin::CreateHandle(isolate,
|
||||
new ServiceWorkerContext(isolate, browser_context));
|
||||
}
|
||||
|
||||
// static
|
||||
void ServiceWorkerContext::BuildPrototype(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
prototype->SetClassName(gin::StringToV8(isolate, "ServiceWorkerContext"));
|
||||
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||
.SetMethod("getAllRunning",
|
||||
&ServiceWorkerContext::GetAllRunningWorkerInfo)
|
||||
.SetMethod("getFromVersionID",
|
||||
&ServiceWorkerContext::GetWorkerInfoFromID);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace electron
|
58
shell/browser/api/electron_api_service_worker_context.h
Normal file
58
shell/browser/api/electron_api_service_worker_context.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Copyright (c) 2019 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_CONTEXT_H_
|
||||
#define SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_CONTEXT_H_
|
||||
|
||||
#include "content/public/browser/service_worker_context.h"
|
||||
#include "content/public/browser/service_worker_context_observer.h"
|
||||
#include "gin/handle.h"
|
||||
#include "shell/common/gin_helper/trackable_object.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
class ElectronBrowserContext;
|
||||
|
||||
namespace api {
|
||||
|
||||
class ServiceWorkerContext
|
||||
: public gin_helper::TrackableObject<ServiceWorkerContext>,
|
||||
public content::ServiceWorkerContextObserver {
|
||||
public:
|
||||
static gin::Handle<ServiceWorkerContext> Create(
|
||||
v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context);
|
||||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype);
|
||||
|
||||
v8::Local<v8::Value> GetAllRunningWorkerInfo(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> GetWorkerInfoFromID(gin_helper::ErrorThrower thrower,
|
||||
int64_t version_id);
|
||||
|
||||
// content::ServiceWorkerContextObserver
|
||||
void OnReportConsoleMessage(int64_t version_id,
|
||||
const content::ConsoleMessage& message) override;
|
||||
void OnDestruct(content::ServiceWorkerContext* context) override;
|
||||
|
||||
protected:
|
||||
explicit ServiceWorkerContext(v8::Isolate* isolate,
|
||||
ElectronBrowserContext* browser_context);
|
||||
~ServiceWorkerContext() override;
|
||||
|
||||
private:
|
||||
ElectronBrowserContext* browser_context_;
|
||||
|
||||
content::ServiceWorkerContext* service_worker_context_;
|
||||
|
||||
base::WeakPtrFactory<ServiceWorkerContext> weak_ptr_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContext);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_CONTEXT_H_
|
|
@ -46,6 +46,7 @@
|
|||
#include "shell/browser/api/electron_api_download_item.h"
|
||||
#include "shell/browser/api/electron_api_net_log.h"
|
||||
#include "shell/browser/api/electron_api_protocol.h"
|
||||
#include "shell/browser/api/electron_api_service_worker_context.h"
|
||||
#include "shell/browser/api/electron_api_web_request.h"
|
||||
#include "shell/browser/browser.h"
|
||||
#include "shell/browser/electron_browser_context.h"
|
||||
|
@ -721,6 +722,15 @@ v8::Local<v8::Value> Session::Protocol(v8::Isolate* isolate) {
|
|||
return v8::Local<v8::Value>::New(isolate, protocol_);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::ServiceWorkerContext(v8::Isolate* isolate) {
|
||||
if (service_worker_context_.IsEmpty()) {
|
||||
v8::Local<v8::Value> handle;
|
||||
handle = ServiceWorkerContext::Create(isolate, browser_context()).ToV8();
|
||||
service_worker_context_.Reset(isolate, handle);
|
||||
}
|
||||
return v8::Local<v8::Value>::New(isolate, service_worker_context_);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Session::WebRequest(v8::Isolate* isolate) {
|
||||
if (web_request_.IsEmpty()) {
|
||||
auto handle = WebRequest::Create(isolate, browser_context());
|
||||
|
@ -958,6 +968,7 @@ void Session::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetProperty("cookies", &Session::Cookies)
|
||||
.SetProperty("netLog", &Session::NetLog)
|
||||
.SetProperty("protocol", &Session::Protocol)
|
||||
.SetProperty("serviceWorkers", &Session::ServiceWorkerContext)
|
||||
.SetProperty("webRequest", &Session::WebRequest);
|
||||
}
|
||||
|
||||
|
@ -970,6 +981,7 @@ namespace {
|
|||
using electron::api::Cookies;
|
||||
using electron::api::NetLog;
|
||||
using electron::api::Protocol;
|
||||
using electron::api::ServiceWorkerContext;
|
||||
using electron::api::Session;
|
||||
|
||||
v8::Local<v8::Value> FromPartition(const std::string& partition,
|
||||
|
@ -1002,6 +1014,9 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
dict.Set(
|
||||
"Protocol",
|
||||
Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
|
||||
dict.Set("ServiceWorkerContext", ServiceWorkerContext::GetConstructor(isolate)
|
||||
->GetFunction(context)
|
||||
.ToLocalChecked());
|
||||
dict.SetMethod("fromPartition", &FromPartition);
|
||||
}
|
||||
|
||||
|
|
|
@ -87,6 +87,7 @@ class Session : public gin_helper::TrackableObject<Session>,
|
|||
std::vector<base::FilePath::StringType> GetPreloads() const;
|
||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> Protocol(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> ServiceWorkerContext(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
|
||||
v8::Local<v8::Value> NetLog(v8::Isolate* isolate);
|
||||
void Preconnect(const gin_helper::Dictionary& options,
|
||||
|
@ -120,6 +121,7 @@ class Session : public gin_helper::TrackableObject<Session>,
|
|||
v8::Global<v8::Value> cookies_;
|
||||
v8::Global<v8::Value> protocol_;
|
||||
v8::Global<v8::Value> net_log_;
|
||||
v8::Global<v8::Value> service_worker_context_;
|
||||
|
||||
// Cached object.
|
||||
v8::Global<v8::Value> web_request_;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue