OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ | 5 #ifndef EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ |
6 #define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ | 6 #define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/macros.h" |
12 #include "base/bind.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" | |
13 #include "v8/include/v8.h" | 17 #include "v8/include/v8.h" |
14 | 18 |
15 class GURL; | 19 class GURL; |
16 | 20 |
17 namespace base { | 21 namespace base { |
18 class ListValue; | 22 class ListValue; |
19 } | 23 } |
20 | 24 |
25 namespace blink { | |
26 class WebLocalFrame; | |
27 class WebSecurityOrigin; | |
28 } | |
29 | |
21 namespace content { | 30 namespace content { |
22 class RenderView; | 31 class RenderView; |
23 } | 32 } |
24 | 33 |
25 namespace v8 { | |
26 class Context; | |
27 } | |
28 | |
29 namespace extensions { | 34 namespace extensions { |
30 class ScriptContext; | 35 class ScriptContext; |
31 | 36 |
32 // A container of ExtensionBindingsContext. Since calling JavaScript within a | 37 // A container of ScriptContexts, responsible for both creating and managing |
33 // context can cause any number of contexts to be created or destroyed, this | 38 // them. |
34 // has additional smarts to help with the set changing underneath callers. | 39 // |
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. | |
35 class ScriptContextSet { | 43 class ScriptContextSet { |
36 public: | 44 public: |
37 ScriptContextSet(); | 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|. | |
not at google - send to devlin
2015/03/12 16:59:57
FYI by "combine this" I mean that the renderer sid
| |
49 ExtensionIdSet* active_extension_ids); | |
50 | |
38 ~ScriptContextSet(); | 51 ~ScriptContextSet(); |
39 | 52 |
40 int size() const; | 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(); } | |
41 | 56 |
42 // Takes ownership of |context|. | 57 // Creates and starts managing a new ScriptContext. Ownership is held. |
43 void Add(ScriptContext* context); | 58 // Returns a weak reference to the new ScriptContext. |
59 ScriptContext* Register(blink::WebLocalFrame* frame, | |
not at google - send to devlin
2015/03/12 16:59:57
I'm not particularly proud of this function being
Ken Rockot(use gerrit already)
2015/03/12 22:45:16
Seems fine to me. Doubly so with the future callba
not at google - send to devlin
2015/03/12 22:48:34
Alrightee.
| |
60 const v8::Handle<v8::Context>& v8_context, | |
61 int extension_group, | |
62 int world_id); | |
44 | 63 |
45 // If the specified context is contained in this set, remove it, then delete | 64 // If the specified context is contained in this set, remove it, then delete |
46 // it asynchronously. After this call returns the context object will still | 65 // it asynchronously. After this call returns the context object will still |
47 // be valid, but its frame() pointer will be cleared. | 66 // be valid, but its frame() pointer will be cleared. |
48 void Remove(ScriptContext* context); | 67 void Remove(ScriptContext* context); |
49 | 68 |
50 // Returns a copy to protect against changes. | |
51 typedef std::set<ScriptContext*> ContextSet; | |
52 ContextSet GetAll() const; | |
53 | |
54 // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or | 69 // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or |
55 // NULL if no such context exists. | 70 // NULL if no such context exists. |
56 ScriptContext* GetCurrent() const; | 71 ScriptContext* GetCurrent() const; |
57 | 72 |
58 // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or | 73 // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or |
59 // NULL if no such context exists. | 74 // NULL if no such context exists. |
60 ScriptContext* GetCalling() const; | 75 ScriptContext* GetCalling() const; |
61 | 76 |
62 // Gets the ScriptContext corresponding to the specified | 77 // Gets the ScriptContext corresponding to the specified |
63 // v8::Context or NULL if no such context exists. | 78 // v8::Context or NULL if no such context exists. |
64 ScriptContext* GetByV8Context(v8::Handle<v8::Context> context) const; | 79 ScriptContext* GetByV8Context(const v8::Handle<v8::Context>& context) const; |
65 | 80 |
66 // Synchronously runs |callback| with each ScriptContext that belongs to | 81 // Synchronously runs |callback| with each ScriptContext that belongs to |
67 // |extension_id| in |render_view|. | 82 // |extension_id| in |render_view|. |
68 // | 83 // |
69 // An empty |extension_id| will match all extensions, and a NULL |render_view| | 84 // An empty |extension_id| will match all extensions, and a NULL |render_view| |
70 // will match all render views, but try to use the inline variants of these | 85 // will match all render views, but try to use the inline variants of these |
71 // methods instead. | 86 // methods instead. |
72 void ForEach(const std::string& extension_id, | 87 void ForEach(const std::string& extension_id, |
73 content::RenderView* render_view, | 88 content::RenderView* render_view, |
74 const base::Callback<void(ScriptContext*)>& callback) const; | 89 const base::Callback<void(ScriptContext*)>& callback) const; |
75 // ForEach which matches all extensions. | 90 // ForEach which matches all extensions. |
76 void ForEach(content::RenderView* render_view, | 91 void ForEach(content::RenderView* render_view, |
77 const base::Callback<void(ScriptContext*)>& callback) const { | 92 const base::Callback<void(ScriptContext*)>& callback) const { |
78 ForEach("", render_view, callback); | 93 ForEach("", render_view, callback); |
79 } | 94 } |
80 // ForEach which matches all render views. | 95 // ForEach which matches all render views. |
81 void ForEach(const std::string& extension_id, | 96 void ForEach(const std::string& extension_id, |
82 const base::Callback<void(ScriptContext*)>& callback) const { | 97 const base::Callback<void(ScriptContext*)>& callback) const { |
83 ForEach(extension_id, NULL, callback); | 98 ForEach(extension_id, NULL, callback); |
84 } | 99 } |
85 | 100 |
86 // Cleans up contexts belonging to an unloaded extension. | 101 // Cleans up contexts belonging to an unloaded extension. |
87 // | 102 // |
88 // Returns the set of ScriptContexts that were removed as a result. These | 103 // Returns the set of ScriptContexts that were removed as a result. These |
89 // are safe to interact with until the end of the current event loop, since | 104 // are safe to interact with until the end of the current event loop, since |
90 // they're deleted asynchronously. | 105 // they're deleted asynchronously. |
91 ContextSet OnExtensionUnloaded(const std::string& extension_id); | 106 std::set<ScriptContext*> OnExtensionUnloaded(const std::string& extension_id); |
92 | 107 |
93 private: | 108 private: |
94 ContextSet contexts_; | 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, | |
115 int world_id, | |
116 bool use_effective_url); | |
117 | |
118 // Returns the Feature::Context type of context for a JavaScript context. | |
119 Feature::Context ClassifyJavaScriptContext( | |
120 const Extension* extension, | |
121 int extension_group, | |
122 const GURL& url, | |
123 const blink::WebSecurityOrigin& origin); | |
124 | |
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); | |
129 | |
130 // Weak reference to all installed Extensions. | |
131 ExtensionSet* extensions_; | |
132 | |
133 // Weak reference to all installed Extensions that are also active in this | |
134 // process. | |
135 ExtensionIdSet* active_extension_ids_; | |
136 | |
137 // The set of all ScriptContexts we own. | |
138 std::set<ScriptContext*> contexts_; | |
95 | 139 |
96 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet); | 140 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet); |
97 }; | 141 }; |
98 | 142 |
99 } // namespace extensions | 143 } // namespace extensions |
100 | 144 |
101 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ | 145 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ |
OLD | NEW |