diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-22 20:55:46 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-22 20:55:46 +0000 |
commit | 005ef3e3012d878280648cf5b81fb568453ae961 (patch) | |
tree | a9674ab3aba4dbd52a4e9137fa640c10a1071ba2 | |
parent | 379ca24af3d07223af4f09f5e42f0200d3b268d3 (diff) | |
download | chromium_src-005ef3e3012d878280648cf5b81fb568453ae961.zip chromium_src-005ef3e3012d878280648cf5b81fb568453ae961.tar.gz chromium_src-005ef3e3012d878280648cf5b81fb568453ae961.tar.bz2 |
Use a NotificationRegistrar to listen for notifications.
BUG=2381
Review URL: http://codereview.chromium.org/114040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16792 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/metrics/metrics_service.cc | 77 | ||||
-rw-r--r-- | chrome/browser/metrics/metrics_service.h | 12 |
2 files changed, 37 insertions, 52 deletions
diff --git a/chrome/browser/metrics/metrics_service.cc b/chrome/browser/metrics/metrics_service.cc index 21adc52..0289dd8 100644 --- a/chrome/browser/metrics/metrics_service.cc +++ b/chrome/browser/metrics/metrics_service.cc @@ -443,10 +443,41 @@ void MetricsService::SetRecording(bool enabled) { } } StartRecording(); - ListenerRegistration(true); + + registrar_.Add(this, NotificationType::BROWSER_OPENED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::BROWSER_CLOSED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::USER_ACTION, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::TAB_PARENTED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::TAB_CLOSING, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::LOAD_START, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::LOAD_STOP, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_IN_SBOX, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_HANG, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::CHILD_PROCESS_HOST_CONNECTED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::CHILD_INSTANCE_CREATED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::CHILD_PROCESS_CRASHED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::TEMPLATE_URL_MODEL_LOADED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::OMNIBOX_OPENED_URL, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED, + NotificationService::AllSources()); } else { - // Turn off all observers. - ListenerRegistration(false); + registrar_.RemoveAll(); PushPendingLogsToUnsentLists(); DCHECK(!pending_log()); if (state_ > INITIAL_LOG_READY && unsent_logs()) @@ -787,46 +818,6 @@ void MetricsService::StopRecording(MetricsLog** log) { current_log_ = NULL; } -void MetricsService::ListenerRegistration(bool start_listening) { - AddOrRemoveObserver(this, NotificationType::BROWSER_OPENED, start_listening); - AddOrRemoveObserver(this, NotificationType::BROWSER_CLOSED, start_listening); - AddOrRemoveObserver(this, NotificationType::USER_ACTION, start_listening); - AddOrRemoveObserver(this, NotificationType::TAB_PARENTED, start_listening); - AddOrRemoveObserver(this, NotificationType::TAB_CLOSING, start_listening); - AddOrRemoveObserver(this, NotificationType::LOAD_START, start_listening); - AddOrRemoveObserver(this, NotificationType::LOAD_STOP, start_listening); - AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_IN_SBOX, - start_listening); - AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_CLOSED, - start_listening); - AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_HANG, - start_listening); - AddOrRemoveObserver(this, NotificationType::CHILD_PROCESS_HOST_CONNECTED, - start_listening); - AddOrRemoveObserver(this, NotificationType::CHILD_INSTANCE_CREATED, - start_listening); - AddOrRemoveObserver(this, NotificationType::CHILD_PROCESS_CRASHED, - start_listening); - AddOrRemoveObserver(this, NotificationType::TEMPLATE_URL_MODEL_LOADED, - start_listening); - AddOrRemoveObserver(this, NotificationType::OMNIBOX_OPENED_URL, - start_listening); - AddOrRemoveObserver(this, NotificationType::BOOKMARK_MODEL_LOADED, - start_listening); -} - -// static -void MetricsService::AddOrRemoveObserver(NotificationObserver* observer, - NotificationType type, - bool is_add) { - NotificationService* service = NotificationService::current(); - - if (is_add) - service->AddObserver(observer, type, NotificationService::AllSources()); - else - service->RemoveObserver(observer, type, NotificationService::AllSources()); -} - void MetricsService::PushPendingLogsToUnsentLists() { if (state_ < INITIAL_LOG_READY) return; // We didn't and still don't have time to get plugin list etc. diff --git a/chrome/browser/metrics/metrics_service.h b/chrome/browser/metrics/metrics_service.h index 426c66e..199e1b1 100644 --- a/chrome/browser/metrics/metrics_service.h +++ b/chrome/browser/metrics/metrics_service.h @@ -20,7 +20,7 @@ #include "base/values.h" #include "chrome/browser/metrics/metrics_log.h" #include "chrome/browser/net/url_fetcher.h" -#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" #include "webkit/glue/webplugin.h" class BookmarkModel; @@ -177,14 +177,6 @@ class MetricsService : public NotificationObserver, // or passes in NULL to indicate that the log should simply be deleted. void StopRecording(MetricsLog** log); - void ListenerRegistration(bool start_listening); - - // Adds or Removes (depending on the value of is_add) the given observer - // to the given notification type for all sources. - static void AddOrRemoveObserver(NotificationObserver* observer, - NotificationType type, - bool is_add); - // Deletes pending_log_ and current_log_, and pushes their text into the // appropriate unsent_log vectors. Called when Chrome shuts down. void PushPendingLogsToUnsentLists(); @@ -377,6 +369,8 @@ class MetricsService : public NotificationObserver, // Sets the value of the specified path in prefs and schedules a save. void RecordBooleanPrefValue(const wchar_t* path, bool value); + NotificationRegistrar registrar_; + // Indicate whether recording and reporting are currently happening. // These should not be set directly, but by calling SetRecording and // SetReporting. |