Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / script_context_set.h
blobbe58fe5d68f7c0e68d4b34de49d50f91319547ca
1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_
6 #define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_
8 #include <set>
9 #include <string>
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/features/feature.h"
15 #include "extensions/renderer/renderer_extension_registry.h"
16 #include "url/gurl.h"
17 #include "v8/include/v8.h"
19 class GURL;
21 namespace base {
22 class ListValue;
25 namespace blink {
26 class WebLocalFrame;
27 class WebSecurityOrigin;
30 namespace content {
31 class RenderFrame;
34 namespace extensions {
35 class ScriptContext;
37 // A container of ScriptContexts, responsible for both creating and managing
38 // them.
40 // Since calling JavaScript within a context can cause any number of contexts
41 // to be created or destroyed, this has additional smarts to help with the set
42 // changing underneath callers.
43 class ScriptContextSet {
44 public:
45 ScriptContextSet(
46 // Set of the IDs of extensions that are active in this process.
47 // Must outlive this. TODO(kalman): Combine this and |extensions|.
48 ExtensionIdSet* active_extension_ids);
50 ~ScriptContextSet();
52 // Returns the number of contexts being tracked by this set.
53 // This may also include invalid contexts. TODO(kalman): Useful?
54 size_t size() const { return contexts_.size(); }
56 // Creates and starts managing a new ScriptContext. Ownership is held.
57 // Returns a weak reference to the new ScriptContext.
58 ScriptContext* Register(blink::WebLocalFrame* frame,
59 const v8::Local<v8::Context>& v8_context,
60 int extension_group,
61 int world_id);
63 // If the specified context is contained in this set, remove it, then delete
64 // it asynchronously. After this call returns the context object will still
65 // be valid, but its frame() pointer will be cleared.
66 void Remove(ScriptContext* context);
68 // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or
69 // NULL if no such context exists.
70 ScriptContext* GetCurrent() const;
72 // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or
73 // NULL if no such context exists.
74 ScriptContext* GetCalling() const;
76 // Gets the ScriptContext corresponding to the specified
77 // v8::Context or NULL if no such context exists.
78 ScriptContext* GetByV8Context(const v8::Local<v8::Context>& context) const;
79 // Static equivalent of the above.
80 static ScriptContext* GetContextByV8Context(
81 const v8::Local<v8::Context>& context);
83 // Synchronously runs |callback| with each ScriptContext that belongs to
84 // |extension_id| in |render_frame|.
86 // An empty |extension_id| will match all extensions, and a null
87 // |render_frame| will match all render views, but try to use the inline
88 // variants of these methods instead.
89 void ForEach(const std::string& extension_id,
90 content::RenderFrame* render_frame,
91 const base::Callback<void(ScriptContext*)>& callback) const;
92 // ForEach which matches all extensions.
93 void ForEach(content::RenderFrame* render_frame,
94 const base::Callback<void(ScriptContext*)>& callback) const {
95 ForEach(std::string(), render_frame, callback);
97 // ForEach which matches all render views.
98 void ForEach(const std::string& extension_id,
99 const base::Callback<void(ScriptContext*)>& callback) const {
100 ForEach(extension_id, nullptr, callback);
103 // Cleans up contexts belonging to an unloaded extension.
105 // Returns the set of ScriptContexts that were removed as a result. These
106 // are safe to interact with until the end of the current event loop, since
107 // they're deleted asynchronously.
108 std::set<ScriptContext*> OnExtensionUnloaded(const std::string& extension_id);
110 private:
111 // Finds the extension for the JavaScript context associated with the
112 // specified |frame| and isolated world. If |world_id| is zero, finds the
113 // extension ID associated with the main world's JavaScript context. If the
114 // JavaScript context isn't from an extension, returns empty string.
115 const Extension* GetExtensionFromFrameAndWorld(
116 const blink::WebLocalFrame* frame,
117 int world_id,
118 bool use_effective_url);
120 // Returns the Feature::Context type of context for a JavaScript context.
121 Feature::Context ClassifyJavaScriptContext(
122 const Extension* extension,
123 int extension_group,
124 const GURL& url,
125 const blink::WebSecurityOrigin& origin);
127 // Calls Remove on |context| then appends |context| to |out|.
128 // This is a helper designed to be used by OnExtensionUnloaded with ForEach.
129 void DispatchOnUnloadEventAndRemove(std::set<ScriptContext*>* out,
130 ScriptContext* context);
132 // Weak reference to all installed Extensions that are also active in this
133 // process.
134 ExtensionIdSet* active_extension_ids_;
136 // The set of all ScriptContexts we own.
137 std::set<ScriptContext*> contexts_;
139 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet);
142 } // namespace extensions
144 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_