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 #include "extensions/renderer/event_bindings.h" | 5 #include "extensions/renderer/event_bindings.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 content::V8ValueConverter::create()); | 265 content::V8ValueConverter::create()); |
266 std::unique_ptr<base::Value> filter_value(converter->FromV8Value( | 266 std::unique_ptr<base::Value> filter_value(converter->FromV8Value( |
267 v8::Local<v8::Object>::Cast(args[1]), context()->v8_context())); | 267 v8::Local<v8::Object>::Cast(args[1]), context()->v8_context())); |
268 if (!filter_value || !filter_value->IsType(base::Value::TYPE_DICTIONARY)) { | 268 if (!filter_value || !filter_value->IsType(base::Value::TYPE_DICTIONARY)) { |
269 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | 269 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
270 return; | 270 return; |
271 } | 271 } |
272 filter = base::DictionaryValue::From(std::move(filter_value)); | 272 filter = base::DictionaryValue::From(std::move(filter_value)); |
273 } | 273 } |
274 | 274 |
275 // Hold onto a weak reference to |filter| so that it can be used after passing | |
276 // ownership to |event_filter|. | |
277 base::DictionaryValue* filter_weak = filter.get(); | |
278 int id = g_event_filter.Get().AddEventMatcher( | 275 int id = g_event_filter.Get().AddEventMatcher( |
279 event_name, ParseEventMatcher(std::move(filter))); | 276 event_name, ParseEventMatcher(std::move(filter))); |
| 277 if (id == -1) { |
| 278 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 279 return; |
| 280 } |
280 attached_matcher_ids_.insert(id); | 281 attached_matcher_ids_.insert(id); |
281 | 282 |
282 // Only send IPCs the first time a filter gets added. | 283 // Only send IPCs the first time a filter gets added. |
| 284 const EventMatcher* matcher = g_event_filter.Get().GetEventMatcher(id); |
| 285 DCHECK(matcher); |
| 286 base::DictionaryValue* filter_weak = matcher->value(); |
283 std::string extension_id = context()->GetExtensionID(); | 287 std::string extension_id = context()->GetExtensionID(); |
284 if (AddFilter(event_name, extension_id, *filter_weak)) { | 288 if (AddFilter(event_name, extension_id, *filter_weak)) { |
285 bool lazy = ExtensionFrameHelper::IsContextForEventPage(context()); | 289 bool lazy = ExtensionFrameHelper::IsContextForEventPage(context()); |
286 content::RenderThread::Get()->Send(new ExtensionHostMsg_AddFilteredListener( | 290 content::RenderThread::Get()->Send(new ExtensionHostMsg_AddFilteredListener( |
287 extension_id, event_name, *filter_weak, lazy)); | 291 extension_id, event_name, *filter_weak, lazy)); |
288 } | 292 } |
289 | 293 |
290 args.GetReturnValue().Set(static_cast<int32_t>(id)); | 294 args.GetReturnValue().Set(static_cast<int32_t>(id)); |
291 } | 295 } |
292 | 296 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 // Same for filtered events. | 364 // Same for filtered events. |
361 std::set<int> attached_matcher_ids_safe = attached_matcher_ids_; | 365 std::set<int> attached_matcher_ids_safe = attached_matcher_ids_; |
362 for (int matcher_id : attached_matcher_ids_safe) { | 366 for (int matcher_id : attached_matcher_ids_safe) { |
363 DetachFilteredEvent(matcher_id, false /* is_manual */); | 367 DetachFilteredEvent(matcher_id, false /* is_manual */); |
364 } | 368 } |
365 DCHECK(attached_matcher_ids_.empty()) | 369 DCHECK(attached_matcher_ids_.empty()) |
366 << "Filtered events cannot be attached during invalidation"; | 370 << "Filtered events cannot be attached during invalidation"; |
367 } | 371 } |
368 | 372 |
369 } // namespace extensions | 373 } // namespace extensions |
OLD | NEW |