Skip to content

Commit

Permalink
chore: clean up based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jan 9, 2020
1 parent ff6c525 commit 5357c7d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/api/service-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For example:
const { session } = require('electron')

// Get all service workers.
console.log(session.defaultSession.serviceWorkers.getAll())
console.log(session.defaultSession.serviceWorkers.getAllRunning())

// Handle logs and get service worker info
session.defaultSession.serviceWorkers.on('console-message', (event, messageDetails) => {
Expand Down Expand Up @@ -47,7 +47,7 @@ Emitted when a service worker logs something to the console.

The following methods are available on instances of `ServiceWorkers`:

#### `serviceWorkers.getAll()`
#### `serviceWorkers.getAllRunning()`

Returns `Record<Number, ServiceWorkerInfo>` - A [ServiceWorkerInfo](structures/service-worker-info.md) object where the keys are the service worker version ID and the values are the information about that service worker.

Expand Down
61 changes: 37 additions & 24 deletions shell/browser/api/atom_api_service_worker_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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 "native_mate/converter.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
Expand Down Expand Up @@ -55,13 +56,14 @@ std::string MessageSourceToString(
return "other";
}

base::DictionaryValue ServiceWorkerRunningInfoToDict(
v8::Local<v8::Value> ServiceWorkerRunningInfoToDict(
v8::Isolate* isolate,
const content::ServiceWorkerRunningInfo& info) {
base::DictionaryValue dict;
dict.SetStringPath("scriptUrl", info.script_url.spec());
dict.SetStringPath("scope", info.scope.spec());
dict.SetIntPath("renderProcessId", info.render_process_id);
return dict;
return gin::DataObjectBuilder(isolate)
.Set("scriptUrl", info.script_url.spec())
.Set("scope", info.scope.spec())
.Set("renderProcessId", info.render_process_id)
.Build();
}

} // namespace
Expand All @@ -83,39 +85,49 @@ ServiceWorkerContext::~ServiceWorkerContext() {
void ServiceWorkerContext::OnReportConsoleMessage(
int64_t version_id,
const content::ConsoleMessage& message) {
base::DictionaryValue details;
details.SetDoublePath("versionId", static_cast<double>(version_id));
details.SetStringPath("source", MessageSourceToString(message.source));
details.SetIntPath("level", static_cast<int32_t>(message.message_level));
details.SetStringPath("message", message.message);
details.SetIntPath("lineNumber", message.line_number);
details.SetStringPath("sourceUrl", message.source_url.spec());
Emit("console-message", details);
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;
}
}

base::DictionaryValue ServiceWorkerContext::GetAllWorkerInfo() {
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();
base::DictionaryValue dict;
for (auto iter = info_map.begin(); iter != info_map.end(); ++iter) {
dict.Set(std::to_string(iter->first),
base::Value::ToUniquePtrValue(
ServiceWorkerRunningInfoToDict(std::move(iter->second))));
builder.Set(
std::to_string(iter->first),
ServiceWorkerRunningInfoToDict(isolate, std::move(iter->second)));
}
return dict;
return builder.Build();
;
}

base::DictionaryValue ServiceWorkerContext::GetWorkerInfoFromID(
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 base::DictionaryValue();
return v8::Local<v8::Value>();
}
return ServiceWorkerRunningInfoToDict(std::move(iter->second));
return ServiceWorkerRunningInfoToDict(thrower.isolate(),
std::move(iter->second));
}

// static
Expand All @@ -132,7 +144,8 @@ void ServiceWorkerContext::BuildPrototype(
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "ServiceWorkerContext"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("getAll", &ServiceWorkerContext::GetAllWorkerInfo)
.SetMethod("getAllRunning",
&ServiceWorkerContext::GetAllRunningWorkerInfo)
.SetMethod("getFromVersionID",
&ServiceWorkerContext::GetWorkerInfoFromID);
}
Expand Down
7 changes: 4 additions & 3 deletions shell/browser/api/atom_api_service_worker_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class ServiceWorkerContext
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

base::DictionaryValue GetAllWorkerInfo();
base::DictionaryValue GetWorkerInfoFromID(gin_helper::ErrorThrower thrower,
int64_t version_id);
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,
Expand Down

0 comments on commit 5357c7d

Please sign in to comment.