blob: ac699a9ec124acf05227c2d0c7120c6eb8a215aa [file] [log] [blame]
kalman04755302015-09-14 18:52:111// 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
dchengf6f80662016-04-20 20:26:048#include <memory>
Istiaque Ahmedee72f4952019-03-06 21:18:369#include <string>
leon.hanb4d9ea92017-01-24 04:28:3210#include <vector>
dchengf6f80662016-04-20 20:26:0411
kalman04755302015-09-14 18:52:1112#include "base/macros.h"
kalman04755302015-09-14 18:52:1113#include "base/threading/thread_local.h"
John Abd-El-Malek383fc8f62017-10-23 00:08:4214#include "content/public/renderer/worker_thread.h"
Istiaque Ahmedee72f4952019-03-06 21:18:3615#include "extensions/renderer/script_context_set_iterable.h"
kalman04755302015-09-14 18:52:1116#include "url/gurl.h"
17#include "v8/include/v8.h"
18
19namespace extensions {
20
21class ScriptContext;
22
23// A set of ScriptContexts owned by worker threads. Thread safe.
Istiaque Ahmedee72f4952019-03-06 21:18:3624class WorkerScriptContextSet : public ScriptContextSetIterable,
25 public content::WorkerThread::Observer {
kalman04755302015-09-14 18:52:1126 public:
27 WorkerScriptContextSet();
28
29 ~WorkerScriptContextSet() override;
30
Istiaque Ahmedee72f4952019-03-06 21:18:3631 // ScriptContextSetIterable:
32 void ForEach(
33 const std::string& extension_id,
34 content::RenderFrame* render_frame,
35 const base::RepeatingCallback<void(ScriptContext*)>& callback) override;
kalman04755302015-09-14 18:52:1136 // Inserts |context| into the set. Contexts are stored in TLS.
dchengf6f80662016-04-20 20:26:0437 void Insert(std::unique_ptr<ScriptContext> context);
kalman04755302015-09-14 18:52:1138
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.hanb4d9ea92017-01-24 04:28:3248 base::ThreadLocalPointer<std::vector<std::unique_ptr<ScriptContext>>>
49 contexts_tls_;
kalman04755302015-09-14 18:52:1150
51 DISALLOW_COPY_AND_ASSIGN(WorkerScriptContextSet);
52};
53
54} // namespace extensions
55
56#endif // EXTENSIONS_RENDERER_WORKER_SCRIPT_CONTEXT_SET_H_