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/macros.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/bind.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" | 13 #include "v8/include/v8.h" |
18 | 14 |
19 class GURL; | 15 class GURL; |
20 | 16 |
21 namespace base { | 17 namespace base { |
22 class ListValue; | 18 class ListValue; |
23 } | 19 } |
24 | 20 |
25 namespace blink { | |
26 class WebLocalFrame; | |
27 class WebSecurityOrigin; | |
28 } | |
29 | |
30 namespace content { | 21 namespace content { |
31 class RenderView; | 22 class RenderView; |
32 } | 23 } |
33 | 24 |
| 25 namespace v8 { |
| 26 class Context; |
| 27 } |
| 28 |
34 namespace extensions { | 29 namespace extensions { |
35 class ScriptContext; | 30 class ScriptContext; |
36 | 31 |
37 // A container of ScriptContexts, responsible for both creating and managing | 32 // A container of ExtensionBindingsContext. Since calling JavaScript within a |
38 // them. | 33 // context can cause any number of contexts to be created or destroyed, this |
39 // | 34 // has additional smarts to help with the set changing underneath callers. |
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 { | 35 class ScriptContextSet { |
44 public: | 36 public: |
45 ScriptContextSet( | 37 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); | |
50 | |
51 ~ScriptContextSet(); | 38 ~ScriptContextSet(); |
52 | 39 |
53 // Returns the number of contexts being tracked by this set. | 40 int size() const; |
54 // This may also include invalid contexts. TODO(kalman): Useful? | |
55 size_t size() const { return contexts_.size(); } | |
56 | 41 |
57 // Creates and starts managing a new ScriptContext. Ownership is held. | 42 // Takes ownership of |context|. |
58 // Returns a weak reference to the new ScriptContext. | 43 void Add(ScriptContext* context); |
59 ScriptContext* Register(blink::WebLocalFrame* frame, | |
60 const v8::Handle<v8::Context>& v8_context, | |
61 int extension_group, | |
62 int world_id); | |
63 | 44 |
64 // If the specified context is contained in this set, remove it, then delete | 45 // 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 | 46 // it asynchronously. After this call returns the context object will still |
66 // be valid, but its frame() pointer will be cleared. | 47 // be valid, but its frame() pointer will be cleared. |
67 void Remove(ScriptContext* context); | 48 void Remove(ScriptContext* context); |
68 | 49 |
| 50 // Returns a copy to protect against changes. |
| 51 typedef std::set<ScriptContext*> ContextSet; |
| 52 ContextSet GetAll() const; |
| 53 |
69 // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or | 54 // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or |
70 // NULL if no such context exists. | 55 // NULL if no such context exists. |
71 ScriptContext* GetCurrent() const; | 56 ScriptContext* GetCurrent() const; |
72 | 57 |
73 // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or | 58 // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or |
74 // NULL if no such context exists. | 59 // NULL if no such context exists. |
75 ScriptContext* GetCalling() const; | 60 ScriptContext* GetCalling() const; |
76 | 61 |
77 // Gets the ScriptContext corresponding to the specified | 62 // Gets the ScriptContext corresponding to the specified |
78 // v8::Context or NULL if no such context exists. | 63 // v8::Context or NULL if no such context exists. |
79 ScriptContext* GetByV8Context(const v8::Handle<v8::Context>& context) const; | 64 ScriptContext* GetByV8Context(v8::Handle<v8::Context> context) const; |
80 | 65 |
81 // Synchronously runs |callback| with each ScriptContext that belongs to | 66 // Synchronously runs |callback| with each ScriptContext that belongs to |
82 // |extension_id| in |render_view|. | 67 // |extension_id| in |render_view|. |
83 // | 68 // |
84 // An empty |extension_id| will match all extensions, and a NULL |render_view| | 69 // 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 | 70 // will match all render views, but try to use the inline variants of these |
86 // methods instead. | 71 // methods instead. |
87 void ForEach(const std::string& extension_id, | 72 void ForEach(const std::string& extension_id, |
88 content::RenderView* render_view, | 73 content::RenderView* render_view, |
89 const base::Callback<void(ScriptContext*)>& callback) const; | 74 const base::Callback<void(ScriptContext*)>& callback) const; |
90 // ForEach which matches all extensions. | 75 // ForEach which matches all extensions. |
91 void ForEach(content::RenderView* render_view, | 76 void ForEach(content::RenderView* render_view, |
92 const base::Callback<void(ScriptContext*)>& callback) const { | 77 const base::Callback<void(ScriptContext*)>& callback) const { |
93 ForEach("", render_view, callback); | 78 ForEach("", render_view, callback); |
94 } | 79 } |
95 // ForEach which matches all render views. | 80 // ForEach which matches all render views. |
96 void ForEach(const std::string& extension_id, | 81 void ForEach(const std::string& extension_id, |
97 const base::Callback<void(ScriptContext*)>& callback) const { | 82 const base::Callback<void(ScriptContext*)>& callback) const { |
98 ForEach(extension_id, NULL, callback); | 83 ForEach(extension_id, NULL, callback); |
99 } | 84 } |
100 | 85 |
101 // Cleans up contexts belonging to an unloaded extension. | 86 // Cleans up contexts belonging to an unloaded extension. |
102 // | 87 // |
103 // Returns the set of ScriptContexts that were removed as a result. These | 88 // 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 | 89 // are safe to interact with until the end of the current event loop, since |
105 // they're deleted asynchronously. | 90 // they're deleted asynchronously. |
106 std::set<ScriptContext*> OnExtensionUnloaded(const std::string& extension_id); | 91 ContextSet OnExtensionUnloaded(const std::string& extension_id); |
107 | 92 |
108 private: | 93 private: |
109 // Finds the extension for the JavaScript context associated with the | 94 ContextSet contexts_; |
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_; | |
139 | 95 |
140 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet); | 96 DISALLOW_COPY_AND_ASSIGN(ScriptContextSet); |
141 }; | 97 }; |
142 | 98 |
143 } // namespace extensions | 99 } // namespace extensions |
144 | 100 |
145 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ | 101 #endif // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_ |
OLD | NEW |