Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: chrome/renderer/extensions/event_bindings.cc

Issue 16032015: Extensions: pass ChromeV8Context around instead of v8::Handle. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/renderer/extensions/event_bindings.h" 5 #include "chrome/renderer/extensions/event_bindings.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 // A map of extension IDs to filtered listener counts for that extension. 64 // A map of extension IDs to filtered listener counts for that extension.
65 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > 65 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
66 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; 66 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER;
67 67
68 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; 68 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER;
69 69
70 // TODO(koz): Merge this into EventBindings. 70 // TODO(koz): Merge this into EventBindings.
71 class ExtensionImpl : public ChromeV8Extension { 71 class ExtensionImpl : public ChromeV8Extension {
72 public: 72 public:
73 explicit ExtensionImpl(Dispatcher* dispatcher, 73 explicit ExtensionImpl(Dispatcher* dispatcher, ChromeV8Context* context)
74 v8::Handle<v8::Context> v8_context) 74 : ChromeV8Extension(dispatcher, context) {
75 : ChromeV8Extension(dispatcher, v8_context) {
76 RouteFunction("AttachEvent", 75 RouteFunction("AttachEvent",
77 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this))); 76 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this)));
78 RouteFunction("DetachEvent", 77 RouteFunction("DetachEvent",
79 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this))); 78 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this)));
80 RouteFunction("AttachFilteredEvent", 79 RouteFunction("AttachFilteredEvent",
81 base::Bind(&ExtensionImpl::AttachFilteredEvent, 80 base::Bind(&ExtensionImpl::AttachFilteredEvent,
82 base::Unretained(this))); 81 base::Unretained(this)));
83 RouteFunction("DetachFilteredEvent", 82 RouteFunction("DetachFilteredEvent",
84 base::Bind(&ExtensionImpl::DetachFilteredEvent, 83 base::Bind(&ExtensionImpl::DetachFilteredEvent,
85 base::Unretained(this))); 84 base::Unretained(this)));
86 RouteFunction("MatchAgainstEventFilter", 85 RouteFunction("MatchAgainstEventFilter",
87 base::Bind(&ExtensionImpl::MatchAgainstEventFilter, 86 base::Bind(&ExtensionImpl::MatchAgainstEventFilter,
88 base::Unretained(this))); 87 base::Unretained(this)));
89 } 88 }
90 89
91 virtual ~ExtensionImpl() {} 90 virtual ~ExtensionImpl() {}
92 91
93 // Attach an event name to an object. 92 // Attach an event name to an object.
94 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { 93 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
95 DCHECK(args.Length() == 1); 94 DCHECK(args.Length() == 1);
96 // TODO(erikkay) should enforce that event name is a string in the bindings 95 // TODO(erikkay) should enforce that event name is a string in the bindings
97 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 96 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
98 97
99 if (args[0]->IsString()) { 98 if (args[0]->IsString()) {
100 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); 99 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
101 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); 100 CHECK(context());
102 ChromeV8Context* context = context_set.GetByV8Context(v8_context());
103 CHECK(context);
104 101
105 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context)) 102 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
106 return v8::Undefined(); 103 return v8::Undefined();
107 104
108 std::string extension_id = context->GetExtensionID(); 105 std::string extension_id = context()->GetExtensionID();
109 EventListenerCounts& listener_counts = 106 EventListenerCounts& listener_counts =
110 g_listener_counts.Get()[extension_id]; 107 g_listener_counts.Get()[extension_id];
111 if (++listener_counts[event_name] == 1) { 108 if (++listener_counts[event_name] == 1) {
112 content::RenderThread::Get()->Send( 109 content::RenderThread::Get()->Send(
113 new ExtensionHostMsg_AddListener(extension_id, event_name)); 110 new ExtensionHostMsg_AddListener(extension_id, event_name));
114 } 111 }
115 112
116 // This is called the first time the page has added a listener. Since 113 // This is called the first time the page has added a listener. Since
117 // the background page is the only lazy page, we know this is the first 114 // the background page is the only lazy page, we know this is the first
118 // time this listener has been registered. 115 // time this listener has been registered.
119 if (IsLazyBackgroundPage(GetRenderView(), context->extension())) { 116 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) {
120 content::RenderThread::Get()->Send( 117 content::RenderThread::Get()->Send(
121 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); 118 new ExtensionHostMsg_AddLazyListener(extension_id, event_name));
122 } 119 }
123 } 120 }
124 return v8::Undefined(); 121 return v8::Undefined();
125 } 122 }
126 123
127 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { 124 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) {
128 DCHECK(args.Length() == 2); 125 DCHECK(args.Length() == 2);
129 // TODO(erikkay) should enforce that event name is a string in the bindings 126 // TODO(erikkay) should enforce that event name is a string in the bindings
130 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); 127 DCHECK(args[0]->IsString() || args[0]->IsUndefined());
131 128
132 if (args[0]->IsString() && args[1]->IsBoolean()) { 129 if (args[0]->IsString() && args[1]->IsBoolean()) {
133 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); 130 std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
134 bool is_manual = args[1]->BooleanValue(); 131 bool is_manual = args[1]->BooleanValue();
135 132
136 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); 133 if (!context())
137 ChromeV8Context* context = context_set.GetByV8Context(v8_context());
138 if (!context)
139 return v8::Undefined(); 134 return v8::Undefined();
140 135
141 std::string extension_id = context->GetExtensionID(); 136 std::string extension_id = context()->GetExtensionID();
142 EventListenerCounts& listener_counts = 137 EventListenerCounts& listener_counts =
143 g_listener_counts.Get()[extension_id]; 138 g_listener_counts.Get()[extension_id];
144 139
145 if (--listener_counts[event_name] == 0) { 140 if (--listener_counts[event_name] == 0) {
146 content::RenderThread::Get()->Send( 141 content::RenderThread::Get()->Send(
147 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); 142 new ExtensionHostMsg_RemoveListener(extension_id, event_name));
148 } 143 }
149 144
150 // DetachEvent is called when the last listener for the context is 145 // DetachEvent is called when the last listener for the context is
151 // removed. If the context is the background page, and it removes the 146 // removed. If the context is the background page, and it removes the
152 // last listener manually, then we assume that it is no longer interested 147 // last listener manually, then we assume that it is no longer interested
153 // in being awakened for this event. 148 // in being awakened for this event.
154 if (is_manual && IsLazyBackgroundPage(GetRenderView(), 149 if (is_manual && IsLazyBackgroundPage(GetRenderView(),
155 context->extension())) { 150 context()->extension())) {
156 content::RenderThread::Get()->Send( 151 content::RenderThread::Get()->Send(
157 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); 152 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name));
158 } 153 }
159 } 154 }
160 return v8::Undefined(); 155 return v8::Undefined();
161 } 156 }
162 157
163 // MatcherID AttachFilteredEvent(string event_name, object filter) 158 // MatcherID AttachFilteredEvent(string event_name, object filter)
164 // event_name - Name of the event to attach. 159 // event_name - Name of the event to attach.
165 // filter - Which instances of the named event are we interested in. 160 // filter - Which instances of the named event are we interested in.
166 // returns the id assigned to the listener, which will be returned from calls 161 // returns the id assigned to the listener, which will be returned from calls
167 // to MatchAgainstEventFilter where this listener matches. 162 // to MatchAgainstEventFilter where this listener matches.
168 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) { 163 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) {
169 DCHECK_EQ(2, args.Length()); 164 DCHECK_EQ(2, args.Length());
170 DCHECK(args[0]->IsString()); 165 DCHECK(args[0]->IsString());
171 DCHECK(args[1]->IsObject()); 166 DCHECK(args[1]->IsObject());
172 167
173 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); 168 DCHECK(context());
174 ChromeV8Context* context = context_set.GetByV8Context(v8_context()); 169 if (!context())
175 DCHECK(context);
176 if (!context)
177 return v8::Integer::New(-1); 170 return v8::Integer::New(-1);
178 171
179 std::string event_name = *v8::String::AsciiValue(args[0]); 172 std::string event_name = *v8::String::AsciiValue(args[0]);
180 // This method throws an exception if it returns false. 173 // This method throws an exception if it returns false.
181 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context)) 174 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
182 return v8::Undefined(); 175 return v8::Undefined();
183 176
184 std::string extension_id = context->GetExtensionID(); 177 std::string extension_id = context()->GetExtensionID();
185 if (extension_id.empty()) 178 if (extension_id.empty())
186 return v8::Integer::New(-1); 179 return v8::Integer::New(-1);
187 180
188 scoped_ptr<base::DictionaryValue> filter; 181 scoped_ptr<base::DictionaryValue> filter;
189 scoped_ptr<content::V8ValueConverter> converter( 182 scoped_ptr<content::V8ValueConverter> converter(
190 content::V8ValueConverter::create()); 183 content::V8ValueConverter::create());
191 184
192 base::DictionaryValue* filter_dict = NULL; 185 base::DictionaryValue* filter_dict = NULL;
193 base::Value* filter_value = 186 base::Value* filter_value =
194 converter->FromV8Value(args[1]->ToObject(), context->v8_context()); 187 converter->FromV8Value(args[1]->ToObject(), context()->v8_context());
195 if (!filter_value) 188 if (!filter_value)
196 return v8::Integer::New(-1); 189 return v8::Integer::New(-1);
197 if (!filter_value->GetAsDictionary(&filter_dict)) { 190 if (!filter_value->GetAsDictionary(&filter_dict)) {
198 delete filter_value; 191 delete filter_value;
199 return v8::Integer::New(-1); 192 return v8::Integer::New(-1);
200 } 193 }
201 194
202 filter.reset(filter_dict); 195 filter.reset(filter_dict);
203 EventFilter& event_filter = g_event_filter.Get(); 196 EventFilter& event_filter = g_event_filter.Get();
204 int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher( 197 int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher(
205 filter.get())); 198 filter.get()));
206 199
207 // Only send IPCs the first time a filter gets added. 200 // Only send IPCs the first time a filter gets added.
208 if (AddFilter(event_name, extension_id, filter.get())) { 201 if (AddFilter(event_name, extension_id, filter.get())) {
209 bool lazy = IsLazyBackgroundPage(GetRenderView(), context->extension()); 202 bool lazy = IsLazyBackgroundPage(GetRenderView(), context()->extension());
210 content::RenderThread::Get()->Send( 203 content::RenderThread::Get()->Send(
211 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name, 204 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name,
212 *filter, lazy)); 205 *filter, lazy));
213 } 206 }
214 207
215 return v8::Integer::New(id); 208 return v8::Integer::New(id);
216 } 209 }
217 210
218 // Add a filter to |event_name| in |extension_id|, returning true if it 211 // Add a filter to |event_name| in |extension_id|, returning true if it
219 // was the first filter for that event in that extension. 212 // was the first filter for that event in that extension.
(...skipping 25 matching lines...) Expand all
245 238
246 // void DetachFilteredEvent(int id, bool manual) 239 // void DetachFilteredEvent(int id, bool manual)
247 // id - Id of the event to detach. 240 // id - Id of the event to detach.
248 // manual - false if this is part of the extension unload process where all 241 // manual - false if this is part of the extension unload process where all
249 // listeners are automatically detached. 242 // listeners are automatically detached.
250 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) { 243 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) {
251 DCHECK_EQ(2, args.Length()); 244 DCHECK_EQ(2, args.Length());
252 DCHECK(args[0]->IsInt32()); 245 DCHECK(args[0]->IsInt32());
253 DCHECK(args[1]->IsBoolean()); 246 DCHECK(args[1]->IsBoolean());
254 bool is_manual = args[1]->BooleanValue(); 247 bool is_manual = args[1]->BooleanValue();
255 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); 248 if (!context())
256 ChromeV8Context* context = context_set.GetByV8Context(v8_context());
257 if (!context)
258 return v8::Undefined(); 249 return v8::Undefined();
259 250
260 std::string extension_id = context->GetExtensionID(); 251 std::string extension_id = context()->GetExtensionID();
261 if (extension_id.empty()) 252 if (extension_id.empty())
262 return v8::Undefined(); 253 return v8::Undefined();
263 254
264 int matcher_id = args[0]->Int32Value(); 255 int matcher_id = args[0]->Int32Value();
265 EventFilter& event_filter = g_event_filter.Get(); 256 EventFilter& event_filter = g_event_filter.Get();
266 EventMatcher* event_matcher = 257 EventMatcher* event_matcher =
267 event_filter.GetEventMatcher(matcher_id); 258 event_filter.GetEventMatcher(matcher_id);
268 259
269 const std::string& event_name = event_filter.GetEventName(matcher_id); 260 const std::string& event_name = event_filter.GetEventName(matcher_id);
270 261
271 // Only send IPCs the last time a filter gets removed. 262 // Only send IPCs the last time a filter gets removed.
272 if (RemoveFilter(event_name, extension_id, event_matcher->value())) { 263 if (RemoveFilter(event_name, extension_id, event_matcher->value())) {
273 bool lazy = is_manual && IsLazyBackgroundPage(GetRenderView(), 264 bool lazy = is_manual && IsLazyBackgroundPage(GetRenderView(),
274 context->extension()); 265 context()->extension());
275 content::RenderThread::Get()->Send( 266 content::RenderThread::Get()->Send(
276 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name, 267 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name,
277 *event_matcher->value(), 268 *event_matcher->value(),
278 lazy)); 269 lazy));
279 } 270 }
280 271
281 event_filter.RemoveEventMatcher(matcher_id); 272 event_filter.RemoveEventMatcher(matcher_id);
282 273
283 return v8::Undefined(); 274 return v8::Undefined();
284 } 275 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 base::DictionaryValue* filter_dict) { 315 base::DictionaryValue* filter_dict) {
325 return scoped_ptr<EventMatcher>(new EventMatcher( 316 return scoped_ptr<EventMatcher>(new EventMatcher(
326 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()))); 317 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy())));
327 } 318 }
328 }; 319 };
329 320
330 } // namespace 321 } // namespace
331 322
332 // static 323 // static
333 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, 324 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher,
334 v8::Handle<v8::Context> context) { 325 ChromeV8Context* context) {
335 return new ExtensionImpl(dispatcher, context); 326 return new ExtensionImpl(dispatcher, context);
336 } 327 }
337 328
338 } // namespace extensions 329 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/event_bindings.h ('k') | chrome/renderer/extensions/extension_custom_bindings.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698