electron_api_service_worker_context.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_CONTEXT_H_
  5. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_CONTEXT_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "content/public/browser/service_worker_context.h"
  8. #include "content/public/browser/service_worker_context_observer.h"
  9. #include "gin/wrappable.h"
  10. #include "shell/browser/event_emitter_mixin.h"
  11. #include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
  12. namespace content {
  13. class StoragePartition;
  14. }
  15. namespace gin {
  16. template <typename T>
  17. class Handle;
  18. } // namespace gin
  19. namespace gin_helper {
  20. template <typename T>
  21. class Promise;
  22. } // namespace gin_helper
  23. namespace electron {
  24. class ElectronBrowserContext;
  25. namespace api {
  26. class ServiceWorkerMain;
  27. class ServiceWorkerContext final
  28. : public gin::Wrappable<ServiceWorkerContext>,
  29. public gin_helper::EventEmitterMixin<ServiceWorkerContext>,
  30. private content::ServiceWorkerContextObserver {
  31. public:
  32. static gin::Handle<ServiceWorkerContext> Create(
  33. v8::Isolate* isolate,
  34. ElectronBrowserContext* browser_context);
  35. v8::Local<v8::Value> GetAllRunningWorkerInfo(v8::Isolate* isolate);
  36. v8::Local<v8::Value> GetInfoFromVersionID(gin_helper::ErrorThrower thrower,
  37. int64_t version_id);
  38. v8::Local<v8::Value> GetFromVersionID(gin_helper::ErrorThrower thrower,
  39. int64_t version_id);
  40. v8::Local<v8::Value> GetWorkerFromVersionID(v8::Isolate* isolate,
  41. int64_t version_id);
  42. gin::Handle<ServiceWorkerMain> GetWorkerFromVersionIDIfExists(
  43. v8::Isolate* isolate,
  44. int64_t version_id);
  45. v8::Local<v8::Promise> StartWorkerForScope(v8::Isolate* isolate, GURL scope);
  46. void DidStartWorkerForScope(
  47. std::shared_ptr<gin_helper::Promise<v8::Local<v8::Value>>> shared_promise,
  48. int64_t version_id,
  49. int process_id,
  50. int thread_id);
  51. void DidFailToStartWorkerForScope(
  52. std::shared_ptr<gin_helper::Promise<v8::Local<v8::Value>>> shared_promise,
  53. content::StatusCodeResponse status);
  54. void StopWorkersForScope(GURL scope);
  55. v8::Local<v8::Promise> StopAllWorkers(v8::Isolate* isolate);
  56. // content::ServiceWorkerContextObserver
  57. void OnReportConsoleMessage(int64_t version_id,
  58. const GURL& scope,
  59. const content::ConsoleMessage& message) override;
  60. void OnRegistrationCompleted(const GURL& scope) override;
  61. void OnVersionStartingRunning(int64_t version_id) override;
  62. void OnVersionStartedRunning(
  63. int64_t version_id,
  64. const content::ServiceWorkerRunningInfo& running_info) override;
  65. void OnVersionStoppingRunning(int64_t version_id) override;
  66. void OnVersionStoppedRunning(int64_t version_id) override;
  67. void OnVersionRedundant(int64_t version_id, const GURL& scope) override;
  68. void OnDestruct(content::ServiceWorkerContext* context) override;
  69. // gin::Wrappable
  70. static gin::WrapperInfo kWrapperInfo;
  71. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  72. v8::Isolate* isolate) override;
  73. const char* GetTypeName() override;
  74. // disable copy
  75. ServiceWorkerContext(const ServiceWorkerContext&) = delete;
  76. ServiceWorkerContext& operator=(const ServiceWorkerContext&) = delete;
  77. protected:
  78. explicit ServiceWorkerContext(v8::Isolate* isolate,
  79. ElectronBrowserContext* browser_context);
  80. ~ServiceWorkerContext() override;
  81. private:
  82. void OnRunningStatusChanged(int64_t version_id,
  83. blink::EmbeddedWorkerStatus running_status);
  84. raw_ptr<content::ServiceWorkerContext> service_worker_context_;
  85. // Service worker registration and versions are unique to a storage partition.
  86. // Keep a reference to the storage partition to be used for lookups.
  87. raw_ptr<content::StoragePartition> storage_partition_;
  88. base::WeakPtrFactory<ServiceWorkerContext> weak_ptr_factory_{this};
  89. };
  90. } // namespace api
  91. } // namespace electron
  92. #endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SERVICE_WORKER_CONTEXT_H_