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

Unified Diff: extensions/browser/event_router.cc

Issue 2887293003: Modernize some extensions code. (Closed)
Patch Set: address comments Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/browser/event_router.h ('k') | extensions/browser/event_router_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/browser/event_router.cc
diff --git a/extensions/browser/event_router.cc b/extensions/browser/event_router.cc
index 72121ead18dcbac98e7369b831b7bf6a4d8e5521..c39641884df439bfa7a110282b9ad3243d2e77da 100644
--- a/extensions/browser/event_router.cc
+++ b/extensions/browser/event_router.cc
@@ -10,8 +10,6 @@
#include "base/atomic_sequence_num.h"
#include "base/bind.h"
-#include "base/command_line.h"
-#include "base/lazy_instance.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_macros.h"
@@ -194,14 +192,12 @@ void EventRouter::RegisterObserver(Observer* observer,
}
void EventRouter::UnregisterObserver(Observer* observer) {
- std::vector<ObserverMap::iterator> iters_to_remove;
- for (ObserverMap::iterator iter = observers_.begin();
- iter != observers_.end(); ++iter) {
- if (iter->second == observer)
- iters_to_remove.push_back(iter);
+ for (ObserverMap::iterator it = observers_.begin(); it != observers_.end();) {
+ if (it->second == observer)
+ it = observers_.erase(it);
+ else
+ ++it;
}
- for (size_t i = 0; i < iters_to_remove.size(); ++i)
- observers_.erase(iters_to_remove[i]);
}
void EventRouter::OnListenerAdded(const EventListener* listener) {
@@ -283,14 +279,14 @@ void EventRouter::AddFilteredEventListener(const std::string& event_name,
event_name, extension_id, process,
std::unique_ptr<DictionaryValue>(filter.DeepCopy())));
- if (add_lazy_listener) {
- bool added = listeners_.AddListener(EventListener::ForExtension(
- event_name, extension_id, NULL,
- std::unique_ptr<DictionaryValue>(filter.DeepCopy())));
+ if (!add_lazy_listener)
+ return;
- if (added)
- AddFilterToEvent(event_name, extension_id, &filter);
- }
+ bool added = listeners_.AddListener(EventListener::ForExtension(
+ event_name, extension_id, nullptr,
+ std::unique_ptr<DictionaryValue>(filter.DeepCopy())));
+ if (added)
+ AddFilterToEvent(event_name, extension_id, &filter);
}
void EventRouter::RemoveFilteredEventListener(
@@ -314,12 +310,13 @@ void EventRouter::RemoveFilteredEventListener(
}
}
-bool EventRouter::HasEventListener(const std::string& event_name) {
+bool EventRouter::HasEventListener(const std::string& event_name) const {
return listeners_.HasListenerForEvent(event_name);
}
-bool EventRouter::ExtensionHasEventListener(const std::string& extension_id,
- const std::string& event_name) {
+bool EventRouter::ExtensionHasEventListener(
+ const std::string& extension_id,
+ const std::string& event_name) const {
return listeners_.HasListenerForExtension(extension_id, event_name);
}
@@ -576,30 +573,29 @@ bool EventRouter::MaybeLoadLazyBackgroundPageToDispatchEvent(
return false;
LazyBackgroundTaskQueue* queue = LazyBackgroundTaskQueue::Get(context);
- if (queue->ShouldEnqueueTask(context, extension)) {
- linked_ptr<Event> dispatched_event(event);
-
- // If there's a dispatch callback, call it now (rather than dispatch time)
- // to avoid lifetime issues. Use a separate copy of the event args, so they
- // last until the event is dispatched.
- if (!event->will_dispatch_callback.is_null()) {
- dispatched_event.reset(event->DeepCopy());
- if (!dispatched_event->will_dispatch_callback.Run(
- context, extension, dispatched_event.get(), listener_filter)) {
- // The event has been canceled.
- return true;
- }
- // Ensure we don't call it again at dispatch time.
- dispatched_event->will_dispatch_callback.Reset();
- }
+ if (!queue->ShouldEnqueueTask(context, extension))
+ return false;
- queue->AddPendingTask(context, extension->id(),
- base::Bind(&EventRouter::DispatchPendingEvent,
- base::Unretained(this), dispatched_event));
- return true;
+ linked_ptr<Event> dispatched_event(event);
+
+ // If there's a dispatch callback, call it now (rather than dispatch time)
+ // to avoid lifetime issues. Use a separate copy of the event args, so they
+ // last until the event is dispatched.
+ if (!event->will_dispatch_callback.is_null()) {
+ dispatched_event.reset(event->DeepCopy());
+ if (!dispatched_event->will_dispatch_callback.Run(
+ context, extension, dispatched_event.get(), listener_filter)) {
+ // The event has been canceled.
+ return true;
+ }
+ // Ensure we don't call it again at dispatch time.
+ dispatched_event->will_dispatch_callback.Reset();
}
- return false;
+ queue->AddPendingTask(context, extension->id(),
+ base::Bind(&EventRouter::DispatchPendingEvent,
+ base::Unretained(this), dispatched_event));
+ return true;
}
// static
@@ -756,22 +752,15 @@ void EventRouter::AddFilterToEvent(const std::string& event_name,
void EventRouter::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
- switch (type) {
- case extensions::NOTIFICATION_EXTENSION_ENABLED: {
- // If the extension has a lazy background page, make sure it gets loaded
- // to register the events the extension is interested in.
- const Extension* extension =
- content::Details<const Extension>(details).ptr();
- if (BackgroundInfo::HasLazyBackgroundPage(extension)) {
- LazyBackgroundTaskQueue* queue =
- LazyBackgroundTaskQueue::Get(browser_context_);
- queue->AddPendingTask(browser_context_, extension->id(),
- base::Bind(&DoNothing));
- }
- break;
- }
- default:
- NOTREACHED();
+ DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_ENABLED, type);
+ // If the extension has a lazy background page, make sure it gets loaded
+ // to register the events the extension is interested in.
+ const Extension* extension = content::Details<const Extension>(details).ptr();
+ if (BackgroundInfo::HasLazyBackgroundPage(extension)) {
+ LazyBackgroundTaskQueue* queue =
+ LazyBackgroundTaskQueue::Get(browser_context_);
+ queue->AddPendingTask(browser_context_, extension->id(),
+ base::Bind(&DoNothing));
}
}
« no previous file with comments | « extensions/browser/event_router.h ('k') | extensions/browser/event_router_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698