Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / renderer / script_context_set.h
blobbe2621477e556d94cfcde7e652f3b4eb69270c1c
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/extension_set.h"
15 #include "extensions/common/features/feature.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 ExtensionSet* extensions,
47 // Set of the IDs of extensions that are active in this process.
48 // Must outlive this. TODO(kalman): Combine this and |extensions|.
49 ExtensionIdSet* active_extension_ids);
51 ~ScriptContextSet();
53 // Returns the number of contexts being tracked by this set.
54 // This may also include invalid contexts. TODO(kalman): Useful?
55 size_t size() const { return contexts_.size(); }
57 // Creates and starts managing a new ScriptContext. Ownership is held.
58 // Returns a weak reference to the new ScriptContext.
59 ScriptContext* Register(blink::WebLocalFrame* frame,
60 const v8::Local<v8::Context>& v8_context,
61 int extension_group,
62 int world_id);
64 // If the specified context is contained in this set, remove it, then delete
65 // it asynchronously. After this call returns the context object will still
66 // be valid, but its frame() pointer will be cleared.
67 void Remove(ScriptContext* context);
69 // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or
70 // NULL if no such context exists.
71 ScriptContext* GetCurrent() const;
73 // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or
74 // NULL if no such context exists.
75 ScriptContext* GetCalling() const;
77 // Gets the ScriptContext corresponding to the specified
78 // v8::Context or NULL if no such context exists.
79 ScriptContext* GetByV8Context(const v8::Local<v8::Context>& context) const;
80 // Static equivalent of the above.
81 static ScriptContext* GetContextByV8Context(
82 const v8::Local<v8::Context>& context);
84 // Synchronously runs |callback| with each ScriptContext that belongs to
85 // |extension_id| in |render_frame|.
87 // An empty |extension_id| will match all extensions, and a null
88 // |render_frame| will match all render views, but try to use the inline
89 // variants of these methods instead.
90 void ForEach(const std::string& extension_id,
91 content::RenderFrame* render_frame,
92 const base::Callback<void(ScriptContext*)>& callback) const;
93 // ForEach which matches all extensions.
94 void ForEach(content::RenderFrame* render_frame,
95 const base::Callback<void(ScriptContext*)>& callback) const {
96 ForEach(std::string(), render_frame, callback);
98 // ForEach which matches all render views.
99 void ForEach(const std::string& extension_id,
100 const base::Callback<void(ScriptContext*)>& callback) const {
101 ForEach(extension_id, nullptr, callback);
104 // Cleans up contexts belonging to an unloaded extension.
106 // Returns the set of ScriptContexts that were removed as a result. These
107 // are safe to interact with until the end of the current event loop, since
108 // they're deleted asynchronously.
109 std::set<ScriptContext*> OnExtensionUnloaded(const std::string& extension_id);
111 private:
112 // Finds the extension for the JavaScript context associated with the
113 // specified |frame| and isolated world. If |world_id| is zero, finds the
114 // extension ID associated with the main world's JavaScript context. If the
115 // JavaScript context isn't from an extension, returns empty string.
116 const Extension* GetExtensionFromFrameAndWorld(
117 const blink::WebLocalFrame* frame,
118 int world_id,
119 bool use_effective_url);
121 // Returns the Feature::Context type of context for a JavaScript context.
122 Feature::Context ClassifyJavaScriptContext(
123 const Extension* extension,
124 int extension_group,
125 const GURL& url,
126 const blink::WebSecurityOrigin& origin);
128 // Calls Remove on |context| then appends |context| to |out|.
129 // This is a helper designed to be used by OnExtensionUnloaded with ForEach.
130 void DispatchOnUnloadEventAndRemove(std::set<ScriptContext*>* out,
131 ScriptContext* context);
133 // Weak reference to all installed Extensions.
134 ExtensionSet* extensions_;
136 // Weak reference to all installed Extensions that are also active in this
137 // process.
138 ExtensionIdSet* active_extension_ids_;
140 // The set of all ScriptContexts we own.
141 std::set<ScriptContext*> contexts_;
143 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet);
146 } // namespace extensions
148 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_