electron_api_service_worker_context.cc 5.7 KB

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