|
| 1 | +// Copyright (c) 2019 Slack Technologies, Inc. |
| 2 | +// Use of this source code is governed by the MIT license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "shell/browser/api/electron_api_service_worker_context.h" |
| 6 | + |
| 7 | +#include <string> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +#include "chrome/browser/browser_process.h" |
| 11 | +#include "content/public/browser/console_message.h" |
| 12 | +#include "content/public/browser/storage_partition.h" |
| 13 | +#include "gin/data_object_builder.h" |
| 14 | +#include "gin/handle.h" |
| 15 | +#include "shell/browser/electron_browser_context.h" |
| 16 | +#include "shell/common/gin_converters/value_converter.h" |
| 17 | +#include "shell/common/gin_helper/dictionary.h" |
| 18 | +#include "shell/common/gin_helper/object_template_builder.h" |
| 19 | +#include "shell/common/node_includes.h" |
| 20 | + |
| 21 | +namespace electron { |
| 22 | + |
| 23 | +namespace api { |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +std::string MessageSourceToString( |
| 28 | + const blink::mojom::ConsoleMessageSource source) { |
| 29 | + if (source == blink::mojom::ConsoleMessageSource::kXml) |
| 30 | + return "xml"; |
| 31 | + if (source == blink::mojom::ConsoleMessageSource::kJavaScript) |
| 32 | + return "javascript"; |
| 33 | + if (source == blink::mojom::ConsoleMessageSource::kNetwork) |
| 34 | + return "network"; |
| 35 | + if (source == blink::mojom::ConsoleMessageSource::kConsoleApi) |
| 36 | + return "console-api"; |
| 37 | + if (source == blink::mojom::ConsoleMessageSource::kStorage) |
| 38 | + return "storage"; |
| 39 | + if (source == blink::mojom::ConsoleMessageSource::kAppCache) |
| 40 | + return "app-cache"; |
| 41 | + if (source == blink::mojom::ConsoleMessageSource::kRendering) |
| 42 | + return "rendering"; |
| 43 | + if (source == blink::mojom::ConsoleMessageSource::kSecurity) |
| 44 | + return "security"; |
| 45 | + if (source == blink::mojom::ConsoleMessageSource::kDeprecation) |
| 46 | + return "deprecation"; |
| 47 | + if (source == blink::mojom::ConsoleMessageSource::kWorker) |
| 48 | + return "worker"; |
| 49 | + if (source == blink::mojom::ConsoleMessageSource::kViolation) |
| 50 | + return "violation"; |
| 51 | + if (source == blink::mojom::ConsoleMessageSource::kIntervention) |
| 52 | + return "intervention"; |
| 53 | + if (source == blink::mojom::ConsoleMessageSource::kRecommendation) |
| 54 | + return "recommendation"; |
| 55 | + return "other"; |
| 56 | +} |
| 57 | + |
| 58 | +v8::Local<v8::Value> ServiceWorkerRunningInfoToDict( |
| 59 | + v8::Isolate* isolate, |
| 60 | + const content::ServiceWorkerRunningInfo& info) { |
| 61 | + return gin::DataObjectBuilder(isolate) |
| 62 | + .Set("scriptUrl", info.script_url.spec()) |
| 63 | + .Set("scope", info.scope.spec()) |
| 64 | + .Set("renderProcessId", info.render_process_id) |
| 65 | + .Build(); |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +ServiceWorkerContext::ServiceWorkerContext( |
| 71 | + v8::Isolate* isolate, |
| 72 | + ElectronBrowserContext* browser_context) |
| 73 | + : browser_context_(browser_context), weak_ptr_factory_(this) { |
| 74 | + Init(isolate); |
| 75 | + service_worker_context_ = |
| 76 | + content::BrowserContext::GetDefaultStoragePartition(browser_context_) |
| 77 | + ->GetServiceWorkerContext(); |
| 78 | + service_worker_context_->AddObserver(this); |
| 79 | +} |
| 80 | + |
| 81 | +ServiceWorkerContext::~ServiceWorkerContext() { |
| 82 | + service_worker_context_->RemoveObserver(this); |
| 83 | +} |
| 84 | + |
| 85 | +void ServiceWorkerContext::OnReportConsoleMessage( |
| 86 | + int64_t version_id, |
| 87 | + const content::ConsoleMessage& message) { |
| 88 | + Emit("console-message", |
| 89 | + gin::DataObjectBuilder(v8::Isolate::GetCurrent()) |
| 90 | + .Set("versionId", version_id) |
| 91 | + .Set("source", MessageSourceToString(message.source)) |
| 92 | + .Set("level", static_cast<int32_t>(message.message_level)) |
| 93 | + .Set("message", message.message) |
| 94 | + .Set("lineNumber", message.line_number) |
| 95 | + .Set("sourceUrl", message.source_url.spec()) |
| 96 | + .Build()); |
| 97 | +} |
| 98 | + |
| 99 | +void ServiceWorkerContext::OnDestruct(content::ServiceWorkerContext* context) { |
| 100 | + if (context == service_worker_context_) { |
| 101 | + delete this; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +v8::Local<v8::Value> ServiceWorkerContext::GetAllRunningWorkerInfo( |
| 106 | + v8::Isolate* isolate) { |
| 107 | + gin::DataObjectBuilder builder(isolate); |
| 108 | + const base::flat_map<int64_t, content::ServiceWorkerRunningInfo>& info_map = |
| 109 | + service_worker_context_->GetRunningServiceWorkerInfos(); |
| 110 | + for (auto iter = info_map.begin(); iter != info_map.end(); ++iter) { |
| 111 | + builder.Set( |
| 112 | + std::to_string(iter->first), |
| 113 | + ServiceWorkerRunningInfoToDict(isolate, std::move(iter->second))); |
| 114 | + } |
| 115 | + return builder.Build(); |
| 116 | +} |
| 117 | + |
| 118 | +v8::Local<v8::Value> ServiceWorkerContext::GetWorkerInfoFromID( |
| 119 | + gin_helper::ErrorThrower thrower, |
| 120 | + int64_t version_id) { |
| 121 | + const base::flat_map<int64_t, content::ServiceWorkerRunningInfo>& info_map = |
| 122 | + service_worker_context_->GetRunningServiceWorkerInfos(); |
| 123 | + auto iter = info_map.find(version_id); |
| 124 | + if (iter == info_map.end()) { |
| 125 | + thrower.ThrowError("Could not find service worker with that version_id"); |
| 126 | + return v8::Local<v8::Value>(); |
| 127 | + } |
| 128 | + return ServiceWorkerRunningInfoToDict(thrower.isolate(), |
| 129 | + std::move(iter->second)); |
| 130 | +} |
| 131 | + |
| 132 | +// static |
| 133 | +gin::Handle<ServiceWorkerContext> ServiceWorkerContext::Create( |
| 134 | + v8::Isolate* isolate, |
| 135 | + ElectronBrowserContext* browser_context) { |
| 136 | + return gin::CreateHandle(isolate, |
| 137 | + new ServiceWorkerContext(isolate, browser_context)); |
| 138 | +} |
| 139 | + |
| 140 | +// static |
| 141 | +void ServiceWorkerContext::BuildPrototype( |
| 142 | + v8::Isolate* isolate, |
| 143 | + v8::Local<v8::FunctionTemplate> prototype) { |
| 144 | + prototype->SetClassName(gin::StringToV8(isolate, "ServiceWorkerContext")); |
| 145 | + gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) |
| 146 | + .SetMethod("getAllRunning", |
| 147 | + &ServiceWorkerContext::GetAllRunningWorkerInfo) |
| 148 | + .SetMethod("getFromVersionID", |
| 149 | + &ServiceWorkerContext::GetWorkerInfoFromID); |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace api |
| 153 | + |
| 154 | +} // namespace electron |
0 commit comments