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_
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"
17 #include "v8/include/v8.h"
27 class WebSecurityOrigin
;
34 namespace extensions
{
37 // A container of ScriptContexts, responsible for both creating and managing
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
{
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
);
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::Handle
<v8::Context
>& v8_context
,
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::Handle
<v8::Context
>& context
) const;
81 // Synchronously runs |callback| with each ScriptContext that belongs to
82 // |extension_id| in |render_view|.
84 // An empty |extension_id| will match all extensions, and a NULL |render_view|
85 // will match all render views, but try to use the inline variants of these
87 void ForEach(const std::string
& extension_id
,
88 content::RenderView
* render_view
,
89 const base::Callback
<void(ScriptContext
*)>& callback
) const;
90 // ForEach which matches all extensions.
91 void ForEach(content::RenderView
* render_view
,
92 const base::Callback
<void(ScriptContext
*)>& callback
) const {
93 ForEach("", render_view
, callback
);
95 // ForEach which matches all render views.
96 void ForEach(const std::string
& extension_id
,
97 const base::Callback
<void(ScriptContext
*)>& callback
) const {
98 ForEach(extension_id
, NULL
, callback
);
101 // Cleans up contexts belonging to an unloaded extension.
103 // Returns the set of ScriptContexts that were removed as a result. These
104 // are safe to interact with until the end of the current event loop, since
105 // they're deleted asynchronously.
106 std::set
<ScriptContext
*> OnExtensionUnloaded(const std::string
& extension_id
);
109 // Finds the extension for the JavaScript context associated with the
110 // specified |frame| and isolated world. If |world_id| is zero, finds the
111 // extension ID associated with the main world's JavaScript context. If the
112 // JavaScript context isn't from an extension, returns empty string.
113 const Extension
* GetExtensionFromFrameAndWorld(
114 const blink::WebLocalFrame
* frame
,
116 bool use_effective_url
);
118 // Returns the Feature::Context type of context for a JavaScript context.
119 Feature::Context
ClassifyJavaScriptContext(
120 const Extension
* extension
,
123 const blink::WebSecurityOrigin
& origin
);
125 // Calls Remove on |context| then appends |context| to |out|.
126 // This is a helper designed to be used by OnExtensionUnloaded with ForEach.
127 void DispatchOnUnloadEventAndRemove(std::set
<ScriptContext
*>* out
,
128 ScriptContext
* context
);
130 // Weak reference to all installed Extensions.
131 ExtensionSet
* extensions_
;
133 // Weak reference to all installed Extensions that are also active in this
135 ExtensionIdSet
* active_extension_ids_
;
137 // The set of all ScriptContexts we own.
138 std::set
<ScriptContext
*> contexts_
;
140 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet
);
143 } // namespace extensions
145 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_