Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1084)

Unified Diff: third_party/WebKit/Source/core/workers/WorkerClients.h

Issue 2939623002: [DONT COMMIT] WIP: WorkerClientsInitializer (Closed)
Patch Set: finalize2 Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/workers/WorkerClients.h
diff --git a/third_party/WebKit/Source/core/workers/WorkerClients.h b/third_party/WebKit/Source/core/workers/WorkerClients.h
index 35ecdff94ea2be52a5a613d5020d1de9add0ed8e..09abcb131680ff68446ecdd99837ca5fcff8f5c8 100644
--- a/third_party/WebKit/Source/core/workers/WorkerClients.h
+++ b/third_party/WebKit/Source/core/workers/WorkerClients.h
@@ -34,11 +34,10 @@
#include "core/CoreExport.h"
#include "platform/Supplementable.h"
#include "platform/wtf/Forward.h"
+#include "platform/wtf/Noncopyable.h"
namespace blink {
-class WorkerClients;
-
// This is created on the main thread, passed to the worker thread and
// attached to WorkerOrWorkletGlobalScope when it is created.
// This class can be used to provide "client" implementations to workers or
@@ -61,6 +60,62 @@ class CORE_EXPORT WorkerClients final : public GarbageCollected<WorkerClients>,
extern template class CORE_EXTERN_TEMPLATE_EXPORT Supplement<WorkerClients>;
+// Allows for the registration of a callback that is invoked whenever a new
+// worker starts. Callbacks are expected to provide module clients to a given
+// WorkerClients. All functions must be called on the main thread.
+//
+// Example:
+// // In ModulesInitializer.cpp.
+// WorkerClientsInitializer<CoolWorker>::Register(
+// [](WorkerClients* worker_clients) {
+// // Provides module clients to |worker_clients| here.
+// });
+//
+// // In CoolWorker.cpp.
+// WorkerClients* worker_clients = WorkerClients::Create();
+// WorkerClients<CoolWorker>::Run(worker_clients);
+//
+template <class WorkerType>
+class WorkerClientsInitializer {
+ WTF_MAKE_NONCOPYABLE(WorkerClientsInitializer);
+ static_assert(sizeof(WorkerType), "WorkerType must be a complete type.");
+
+ public:
+ using Callback = void (*)(WorkerClients*);
+
+ WorkerClientsInitializer() = default;
+
+ static void Register(Callback callback) {
+ DCHECK(IsMainThread());
+ if (!instance_)
+ instance_ = new WorkerClientsInitializer<WorkerType>;
+ instance_->RegisterInternal(callback);
+ }
+
+ static void Run(WorkerClients* worker_clients) {
+ DCHECK(IsMainThread());
+ DCHECK(instance_);
+ instance_->RunInternal(worker_clients);
+ }
+
+ private:
+ void RegisterInternal(Callback callback) { callbacks_.push_back(callback); }
+
+ void RunInternal(WorkerClients* worker_clients) {
+ DCHECK(!callbacks_.IsEmpty());
+ for (auto& callback : callbacks_)
+ callback(worker_clients);
+ }
+
+ Vector<Callback> callbacks_;
+
+ static WorkerClientsInitializer<WorkerType>* instance_;
+};
+
+template <class WorkerType>
+WorkerClientsInitializer<WorkerType>*
+ WorkerClientsInitializer<WorkerType>::instance_ = nullptr;
+
} // namespace blink
#endif // WorkerClients_h
« no previous file with comments | « third_party/WebKit/Source/core/workers/Worker.cpp ('k') | third_party/WebKit/Source/modules/ModulesInitializer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698