OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef WorkerClients_h | 31 #ifndef WorkerClients_h |
32 #define WorkerClients_h | 32 #define WorkerClients_h |
33 | 33 |
34 #include "core/CoreExport.h" | 34 #include "core/CoreExport.h" |
35 #include "platform/Supplementable.h" | 35 #include "platform/Supplementable.h" |
36 #include "platform/wtf/Forward.h" | 36 #include "platform/wtf/Forward.h" |
| 37 #include "platform/wtf/Noncopyable.h" |
37 | 38 |
38 namespace blink { | 39 namespace blink { |
39 | 40 |
40 class WorkerClients; | |
41 | |
42 // This is created on the main thread, passed to the worker thread and | 41 // This is created on the main thread, passed to the worker thread and |
43 // attached to WorkerOrWorkletGlobalScope when it is created. | 42 // attached to WorkerOrWorkletGlobalScope when it is created. |
44 // This class can be used to provide "client" implementations to workers or | 43 // This class can be used to provide "client" implementations to workers or |
45 // worklets. | 44 // worklets. |
46 class CORE_EXPORT WorkerClients final : public GarbageCollected<WorkerClients>, | 45 class CORE_EXPORT WorkerClients final : public GarbageCollected<WorkerClients>, |
47 public Supplementable<WorkerClients> { | 46 public Supplementable<WorkerClients> { |
48 USING_GARBAGE_COLLECTED_MIXIN(WorkerClients); | 47 USING_GARBAGE_COLLECTED_MIXIN(WorkerClients); |
49 WTF_MAKE_NONCOPYABLE(WorkerClients); | 48 WTF_MAKE_NONCOPYABLE(WorkerClients); |
50 | 49 |
51 public: | 50 public: |
52 static WorkerClients* Create() { return new WorkerClients; } | 51 static WorkerClients* Create() { return new WorkerClients; } |
53 | 52 |
54 DEFINE_INLINE_VIRTUAL_TRACE() { | 53 DEFINE_INLINE_VIRTUAL_TRACE() { |
55 Supplementable<WorkerClients>::Trace(visitor); | 54 Supplementable<WorkerClients>::Trace(visitor); |
56 } | 55 } |
57 | 56 |
58 private: | 57 private: |
59 WorkerClients() {} | 58 WorkerClients() {} |
60 }; | 59 }; |
61 | 60 |
62 extern template class CORE_EXTERN_TEMPLATE_EXPORT Supplement<WorkerClients>; | 61 extern template class CORE_EXTERN_TEMPLATE_EXPORT Supplement<WorkerClients>; |
63 | 62 |
| 63 // Allows for the registration of a callback that is invoked whenever a new |
| 64 // worker starts. Callbacks are expected to provide module clients to a given |
| 65 // WorkerClients. All functions must be called on the main thread. |
| 66 // |
| 67 // Example: |
| 68 // // In ModulesInitializer.cpp. |
| 69 // WorkerClientsInitializer<CoolWorker>::Register( |
| 70 // [](WorkerClients* worker_clients) { |
| 71 // // Provides module clients to |worker_clients| here. |
| 72 // }); |
| 73 // |
| 74 // // In CoolWorker.cpp. |
| 75 // WorkerClients* worker_clients = WorkerClients::Create(); |
| 76 // WorkerClients<CoolWorker>::Run(worker_clients); |
| 77 // |
| 78 template <class WorkerType> |
| 79 class WorkerClientsInitializer { |
| 80 WTF_MAKE_NONCOPYABLE(WorkerClientsInitializer); |
| 81 static_assert(sizeof(WorkerType), "WorkerType must be a complete type."); |
| 82 |
| 83 public: |
| 84 using Callback = void (*)(WorkerClients*); |
| 85 |
| 86 WorkerClientsInitializer() = default; |
| 87 |
| 88 static void Register(Callback callback) { |
| 89 DCHECK(IsMainThread()); |
| 90 if (!instance_) |
| 91 instance_ = new WorkerClientsInitializer<WorkerType>; |
| 92 instance_->RegisterInternal(callback); |
| 93 } |
| 94 |
| 95 static void Run(WorkerClients* worker_clients) { |
| 96 DCHECK(IsMainThread()); |
| 97 DCHECK(instance_); |
| 98 instance_->RunInternal(worker_clients); |
| 99 } |
| 100 |
| 101 private: |
| 102 void RegisterInternal(Callback callback) { callbacks_.push_back(callback); } |
| 103 |
| 104 void RunInternal(WorkerClients* worker_clients) { |
| 105 DCHECK(!callbacks_.IsEmpty()); |
| 106 for (auto& callback : callbacks_) |
| 107 callback(worker_clients); |
| 108 } |
| 109 |
| 110 Vector<Callback> callbacks_; |
| 111 |
| 112 static WorkerClientsInitializer<WorkerType>* instance_; |
| 113 }; |
| 114 |
| 115 template <class WorkerType> |
| 116 WorkerClientsInitializer<WorkerType>* |
| 117 WorkerClientsInitializer<WorkerType>::instance_ = nullptr; |
| 118 |
64 } // namespace blink | 119 } // namespace blink |
65 | 120 |
66 #endif // WorkerClients_h | 121 #endif // WorkerClients_h |
OLD | NEW |