electron_api_service_worker_context.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "shell/browser/api/electron_api_service_worker_context.h"
  5. #include <string_view>
  6. #include <utility>
  7. #include "base/strings/string_number_conversions.h"
  8. #include "chrome/browser/browser_process.h"
  9. #include "content/public/browser/console_message.h"
  10. #include "content/public/browser/storage_partition.h"
  11. #include "gin/data_object_builder.h"
  12. #include "gin/handle.h"
  13. #include "gin/object_template_builder.h"
  14. #include "shell/browser/electron_browser_context.h"
  15. #include "shell/browser/javascript_environment.h"
  16. #include "shell/common/gin_converters/gurl_converter.h"
  17. #include "shell/common/gin_converters/value_converter.h"
  18. #include "shell/common/gin_helper/dictionary.h"
  19. #include "shell/common/node_includes.h"
  20. namespace electron::api {
  21. namespace {
  22. constexpr std::string_view MessageSourceToString(
  23. const blink::mojom::ConsoleMessageSource source) {
  24. switch (source) {
  25. case blink::mojom::ConsoleMessageSource::kXml:
  26. return "xml";
  27. case blink::mojom::ConsoleMessageSource::kJavaScript:
  28. return "javascript";
  29. case blink::mojom::ConsoleMessageSource::kNetwork:
  30. return "network";
  31. case blink::mojom::ConsoleMessageSource::kConsoleApi:
  32. return "console-api";
  33. case blink::mojom::ConsoleMessageSource::kStorage:
  34. return "storage";
  35. case blink::mojom::ConsoleMessageSource::kRendering:
  36. return "rendering";
  37. case blink::mojom::ConsoleMessageSource::kSecurity:
  38. return "security";
  39. case blink::mojom::ConsoleMessageSource::kDeprecation:
  40. return "deprecation";
  41. case blink::mojom::ConsoleMessageSource::kWorker:
  42. return "worker";
  43. case blink::mojom::ConsoleMessageSource::kViolation:
  44. return "violation";
  45. case blink::mojom::ConsoleMessageSource::kIntervention:
  46. return "intervention";
  47. case blink::mojom::ConsoleMessageSource::kRecommendation:
  48. return "recommendation";
  49. default:
  50. return "other";
  51. }
  52. }
  53. v8::Local<v8::Value> ServiceWorkerRunningInfoToDict(
  54. v8::Isolate* isolate,
  55. const content::ServiceWorkerRunningInfo& info) {
  56. return gin::DataObjectBuilder(isolate)
  57. .Set("scriptUrl", info.script_url.spec())
  58. .Set("scope", info.scope.spec())
  59. .Set("renderProcessId", info.render_process_id)
  60. .Build();
  61. }
  62. } // namespace
  63. gin::WrapperInfo ServiceWorkerContext::kWrapperInfo = {gin::kEmbedderNativeGin};
  64. ServiceWorkerContext::ServiceWorkerContext(
  65. v8::Isolate* isolate,
  66. ElectronBrowserContext* browser_context) {
  67. service_worker_context_ =
  68. browser_context->GetDefaultStoragePartition()->GetServiceWorkerContext();
  69. service_worker_context_->AddObserver(this);
  70. }
  71. ServiceWorkerContext::~ServiceWorkerContext() {
  72. service_worker_context_->RemoveObserver(this);
  73. }
  74. void ServiceWorkerContext::OnReportConsoleMessage(
  75. int64_t version_id,
  76. const GURL& scope,
  77. const content::ConsoleMessage& message) {
  78. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  79. v8::HandleScope handle_scope(isolate);
  80. Emit("console-message",
  81. gin::DataObjectBuilder(isolate)
  82. .Set("versionId", version_id)
  83. .Set("source", MessageSourceToString(message.source))
  84. .Set("level", static_cast<int32_t>(message.message_level))
  85. .Set("message", message.message)
  86. .Set("lineNumber", message.line_number)
  87. .Set("sourceUrl", message.source_url.spec())
  88. .Build());
  89. }
  90. void ServiceWorkerContext::OnRegistrationCompleted(const GURL& scope) {
  91. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  92. v8::HandleScope handle_scope(isolate);
  93. Emit("registration-completed",
  94. gin::DataObjectBuilder(isolate).Set("scope", scope).Build());
  95. }
  96. void ServiceWorkerContext::OnDestruct(content::ServiceWorkerContext* context) {
  97. if (context == service_worker_context_) {
  98. delete this;
  99. }
  100. }
  101. v8::Local<v8::Value> ServiceWorkerContext::GetAllRunningWorkerInfo(
  102. v8::Isolate* isolate) {
  103. gin::DataObjectBuilder builder(isolate);
  104. const base::flat_map<int64_t, content::ServiceWorkerRunningInfo>& info_map =
  105. service_worker_context_->GetRunningServiceWorkerInfos();
  106. for (const auto& iter : info_map) {
  107. builder.Set(
  108. base::NumberToString(iter.first),
  109. ServiceWorkerRunningInfoToDict(isolate, std::move(iter.second)));
  110. }
  111. return builder.Build();
  112. }
  113. v8::Local<v8::Value> ServiceWorkerContext::GetWorkerInfoFromID(
  114. gin_helper::ErrorThrower thrower,
  115. int64_t version_id) {
  116. const base::flat_map<int64_t, content::ServiceWorkerRunningInfo>& info_map =
  117. service_worker_context_->GetRunningServiceWorkerInfos();
  118. auto iter = info_map.find(version_id);
  119. if (iter == info_map.end()) {
  120. thrower.ThrowError("Could not find service worker with that version_id");
  121. return v8::Local<v8::Value>();
  122. }
  123. return ServiceWorkerRunningInfoToDict(thrower.isolate(),
  124. std::move(iter->second));
  125. }
  126. // static
  127. gin::Handle<ServiceWorkerContext> ServiceWorkerContext::Create(
  128. v8::Isolate* isolate,
  129. ElectronBrowserContext* browser_context) {
  130. return gin::CreateHandle(isolate,
  131. new ServiceWorkerContext(isolate, browser_context));
  132. }
  133. // static
  134. gin::ObjectTemplateBuilder ServiceWorkerContext::GetObjectTemplateBuilder(
  135. v8::Isolate* isolate) {
  136. return gin_helper::EventEmitterMixin<
  137. ServiceWorkerContext>::GetObjectTemplateBuilder(isolate)
  138. .SetMethod("getAllRunning",
  139. &ServiceWorkerContext::GetAllRunningWorkerInfo)
  140. .SetMethod("getFromVersionID",
  141. &ServiceWorkerContext::GetWorkerInfoFromID);
  142. }
  143. const char* ServiceWorkerContext::GetTypeName() {
  144. return "ServiceWorkerContext";
  145. }
  146. } // namespace electron::api