electron_api_service_worker_main.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2025 Salesforce, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_MAIN_H_
  5. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_MAIN_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/process/process.h"
  9. #include "content/public/browser/global_routing_id.h"
  10. #include "content/public/browser/service_worker_context.h"
  11. #include "content/public/browser/service_worker_version_base_info.h"
  12. #include "gin/wrappable.h"
  13. #include "mojo/public/cpp/bindings/associated_receiver.h"
  14. #include "mojo/public/cpp/bindings/associated_remote.h"
  15. #include "mojo/public/cpp/bindings/pending_receiver.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. #include "shell/browser/event_emitter_mixin.h"
  18. #include "shell/common/api/api.mojom.h"
  19. #include "shell/common/gin_helper/constructible.h"
  20. #include "shell/common/gin_helper/pinnable.h"
  21. #include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
  22. class GURL;
  23. namespace content {
  24. class StoragePartition;
  25. }
  26. namespace gin {
  27. class Arguments;
  28. } // namespace gin
  29. namespace gin_helper {
  30. class Dictionary;
  31. template <typename T>
  32. class Handle;
  33. template <typename T>
  34. class Promise;
  35. } // namespace gin_helper
  36. namespace electron::api {
  37. // Key to uniquely identify a ServiceWorkerMain by its Version ID within the
  38. // associated StoragePartition.
  39. struct ServiceWorkerKey {
  40. int64_t version_id;
  41. raw_ptr<const content::StoragePartition> storage_partition;
  42. ServiceWorkerKey(int64_t id, const content::StoragePartition* partition)
  43. : version_id(id), storage_partition(partition) {}
  44. bool operator<(const ServiceWorkerKey& other) const {
  45. return std::tie(version_id, storage_partition) <
  46. std::tie(other.version_id, other.storage_partition);
  47. }
  48. bool operator==(const ServiceWorkerKey& other) const {
  49. return version_id == other.version_id &&
  50. storage_partition == other.storage_partition;
  51. }
  52. struct Hasher {
  53. std::size_t operator()(const ServiceWorkerKey& key) const {
  54. return std::hash<const content::StoragePartition*>()(
  55. key.storage_partition) ^
  56. std::hash<int64_t>()(key.version_id);
  57. }
  58. };
  59. };
  60. // Creates a wrapper to align with the lifecycle of the non-public
  61. // content::ServiceWorkerVersion. Object instances are pinned for the lifetime
  62. // of the underlying SW such that registered IPC handlers continue to dispatch.
  63. //
  64. // Instances are uniquely identified by pairing their version ID and the
  65. // StoragePartition in which they're registered. In Electron, this is always
  66. // the default StoragePartition for the associated BrowserContext.
  67. class ServiceWorkerMain final
  68. : public gin::Wrappable<ServiceWorkerMain>,
  69. public gin_helper::EventEmitterMixin<ServiceWorkerMain>,
  70. public gin_helper::Pinnable<ServiceWorkerMain>,
  71. public gin_helper::Constructible<ServiceWorkerMain> {
  72. public:
  73. // Create a new ServiceWorkerMain and return the V8 wrapper of it.
  74. static gin::Handle<ServiceWorkerMain> New(v8::Isolate* isolate);
  75. static gin::Handle<ServiceWorkerMain> From(
  76. v8::Isolate* isolate,
  77. content::ServiceWorkerContext* sw_context,
  78. const content::StoragePartition* storage_partition,
  79. int64_t version_id);
  80. static ServiceWorkerMain* FromVersionID(
  81. int64_t version_id,
  82. const content::StoragePartition* storage_partition);
  83. // gin_helper::Constructible
  84. static void FillObjectTemplate(v8::Isolate*, v8::Local<v8::ObjectTemplate>);
  85. static const char* GetClassName() { return "ServiceWorkerMain"; }
  86. // gin::Wrappable
  87. static gin::WrapperInfo kWrapperInfo;
  88. const char* GetTypeName() override;
  89. // disable copy
  90. ServiceWorkerMain(const ServiceWorkerMain&) = delete;
  91. ServiceWorkerMain& operator=(const ServiceWorkerMain&) = delete;
  92. void OnRunningStatusChanged(blink::EmbeddedWorkerStatus running_status);
  93. void OnVersionRedundant();
  94. protected:
  95. explicit ServiceWorkerMain(content::ServiceWorkerContext* sw_context,
  96. int64_t version_id,
  97. const ServiceWorkerKey& key);
  98. ~ServiceWorkerMain() override;
  99. private:
  100. void Destroy();
  101. void MaybeDisconnectRemote();
  102. const blink::StorageKey GetStorageKey();
  103. // Increments external requests for the service worker to keep it alive.
  104. gin_helper::Dictionary StartExternalRequest(v8::Isolate* isolate,
  105. bool has_timeout);
  106. void FinishExternalRequest(v8::Isolate* isolate, std::string uuid);
  107. size_t CountExternalRequestsForTest();
  108. // Get or create a Mojo connection to the renderer process.
  109. mojom::ElectronRenderer* GetRendererApi();
  110. // Send a message to the renderer process.
  111. void Send(v8::Isolate* isolate,
  112. bool internal,
  113. const std::string& channel,
  114. v8::Local<v8::Value> args);
  115. void InvalidateVersionInfo();
  116. const content::ServiceWorkerVersionBaseInfo* version_info() const {
  117. return version_info_.get();
  118. }
  119. bool IsDestroyed() const;
  120. int64_t VersionID() const;
  121. GURL ScopeURL() const;
  122. GURL ScriptURL() const;
  123. // Version ID unique only to the StoragePartition.
  124. int64_t version_id_;
  125. // Unique identifier pairing the Version ID and StoragePartition.
  126. ServiceWorkerKey key_;
  127. // Whether the Service Worker version has been destroyed.
  128. bool version_destroyed_ = false;
  129. // Whether the Service Worker version's state is redundant.
  130. bool redundant_ = false;
  131. // Store copy of version info so it's accessible when not running.
  132. std::unique_ptr<content::ServiceWorkerVersionBaseInfo> version_info_;
  133. raw_ptr<content::ServiceWorkerContext> service_worker_context_;
  134. mojo::AssociatedRemote<mojom::ElectronRenderer> remote_;
  135. std::unique_ptr<gin_helper::Promise<void>> start_worker_promise_;
  136. };
  137. } // namespace electron::api
  138. #endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_MAIN_H_