kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef EXTENSIONS_RENDERER_WORKER_SCRIPT_CONTEXT_SET_H_ |
| 6 | #define EXTENSIONS_RENDERER_WORKER_SCRIPT_CONTEXT_SET_H_ |
| 7 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 8 | #include <memory> |
leon.han | b4d9ea9 | 2017-01-24 04:28:32 | [diff] [blame^] | 9 | #include <vector> |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 10 | |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 11 | #include "base/macros.h" |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 12 | #include "base/threading/thread_local.h" |
| 13 | #include "content/public/child/worker_thread.h" |
| 14 | #include "url/gurl.h" |
| 15 | #include "v8/include/v8.h" |
| 16 | |
| 17 | namespace extensions { |
| 18 | |
| 19 | class ScriptContext; |
| 20 | |
| 21 | // A set of ScriptContexts owned by worker threads. Thread safe. |
| 22 | class WorkerScriptContextSet : public content::WorkerThread::Observer { |
| 23 | public: |
| 24 | WorkerScriptContextSet(); |
| 25 | |
| 26 | ~WorkerScriptContextSet() override; |
| 27 | |
| 28 | // Inserts |context| into the set. Contexts are stored in TLS. |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 29 | void Insert(std::unique_ptr<ScriptContext> context); |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 30 | |
| 31 | // Removes the ScriptContext associated with |v8_context| which was added for |
| 32 | // |url| (used for sanity checking). |
| 33 | void Remove(v8::Local<v8::Context> v8_context, const GURL& url); |
| 34 | |
| 35 | private: |
| 36 | // WorkerThread::Observer: |
| 37 | void WillStopCurrentWorkerThread() override; |
| 38 | |
| 39 | // Implement thread safety by storing each ScriptContext in TLS. |
leon.han | b4d9ea9 | 2017-01-24 04:28:32 | [diff] [blame^] | 40 | base::ThreadLocalPointer<std::vector<std::unique_ptr<ScriptContext>>> |
| 41 | contexts_tls_; |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 42 | |
| 43 | DISALLOW_COPY_AND_ASSIGN(WorkerScriptContextSet); |
| 44 | }; |
| 45 | |
| 46 | } // namespace extensions |
| 47 | |
| 48 | #endif // EXTENSIONS_RENDERER_WORKER_SCRIPT_CONTEXT_SET_H_ |