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> |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 9 | #include <string> |
leon.han | b4d9ea9 | 2017-01-24 04:28:32 | [diff] [blame] | 10 | #include <vector> |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 11 | |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 12 | #include "base/macros.h" |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 13 | #include "base/threading/thread_local.h" |
John Abd-El-Malek | 383fc8f6 | 2017-10-23 00:08:42 | [diff] [blame] | 14 | #include "content/public/renderer/worker_thread.h" |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 15 | #include "extensions/renderer/script_context_set_iterable.h" |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 16 | #include "url/gurl.h" |
| 17 | #include "v8/include/v8.h" |
| 18 | |
| 19 | namespace extensions { |
| 20 | |
| 21 | class ScriptContext; |
| 22 | |
| 23 | // A set of ScriptContexts owned by worker threads. Thread safe. |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 24 | class WorkerScriptContextSet : public ScriptContextSetIterable, |
| 25 | public content::WorkerThread::Observer { |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 26 | public: |
| 27 | WorkerScriptContextSet(); |
| 28 | |
| 29 | ~WorkerScriptContextSet() override; |
| 30 | |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 31 | // ScriptContextSetIterable: |
| 32 | void ForEach( |
| 33 | const std::string& extension_id, |
| 34 | content::RenderFrame* render_frame, |
| 35 | const base::RepeatingCallback<void(ScriptContext*)>& callback) override; |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 36 | // Inserts |context| into the set. Contexts are stored in TLS. |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 37 | void Insert(std::unique_ptr<ScriptContext> context); |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 38 | |
| 39 | // Removes the ScriptContext associated with |v8_context| which was added for |
| 40 | // |url| (used for sanity checking). |
| 41 | void Remove(v8::Local<v8::Context> v8_context, const GURL& url); |
| 42 | |
| 43 | private: |
| 44 | // WorkerThread::Observer: |
| 45 | void WillStopCurrentWorkerThread() override; |
| 46 | |
| 47 | // Implement thread safety by storing each ScriptContext in TLS. |
leon.han | b4d9ea9 | 2017-01-24 04:28:32 | [diff] [blame] | 48 | base::ThreadLocalPointer<std::vector<std::unique_ptr<ScriptContext>>> |
| 49 | contexts_tls_; |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(WorkerScriptContextSet); |
| 52 | }; |
| 53 | |
| 54 | } // namespace extensions |
| 55 | |
| 56 | #endif // EXTENSIONS_RENDERER_WORKER_SCRIPT_CONTEXT_SET_H_ |