summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_browser_event_router.cc
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-29 07:34:45 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-29 07:34:45 +0000
commitaeb53b34fe924159674c8f15a6a82d23e470e487 (patch)
treecb2378f4a8414dbd82969cf0b79ac571c0df0670 /chrome/browser/extensions/extension_browser_event_router.cc
parentb56fec5936adb2d3af47681f0cc7a053bd8db6da (diff)
downloadchromium_src-aeb53b34fe924159674c8f15a6a82d23e470e487.zip
chromium_src-aeb53b34fe924159674c8f15a6a82d23e470e487.tar.gz
chromium_src-aeb53b34fe924159674c8f15a6a82d23e470e487.tar.bz2
Only dispatch tab events when there is someone listening.
This should cut down on the number of occurrences of a crash that happens in JSON serialization. BUG=25558,26169 Review URL: http://codereview.chromium.org/341029 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30444 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_browser_event_router.cc')
-rw-r--r--chrome/browser/extensions/extension_browser_event_router.cc55
1 files changed, 45 insertions, 10 deletions
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc
index ad0d701..4e9bab3 100644
--- a/chrome/browser/extensions/extension_browser_event_router.cc
+++ b/chrome/browser/extensions/extension_browser_event_router.cc
@@ -111,6 +111,23 @@ void ExtensionBrowserEventRouter::Init() {
BrowserList::AddObserver(this);
+ // Init() can happen after the browser is running, so catch up with any
+ // windows that already exist.
+ for (BrowserList::const_iterator iter = BrowserList::begin();
+ iter != BrowserList::end(); ++iter) {
+ RegisterForBrowserNotifications(*iter);
+
+ // Also catch up our internal bookkeeping of tab entries.
+ Browser* browser = *iter;
+ if (browser->tabstrip_model()) {
+ for (int i = 0; i < browser->tabstrip_model()->count(); ++i) {
+ TabContents* contents = browser->tabstrip_model()->GetTabContentsAt(i);
+ int tab_id = ExtensionTabUtil::GetTabId(contents);
+ tab_entries_[tab_id] = TabEntry(contents);
+ }
+ }
+ }
+
initialized_ = true;
}
@@ -118,12 +135,38 @@ ExtensionBrowserEventRouter::ExtensionBrowserEventRouter()
: initialized_(false) { }
void ExtensionBrowserEventRouter::OnBrowserAdded(const Browser* browser) {
+ RegisterForBrowserNotifications(browser);
+}
+
+void ExtensionBrowserEventRouter::RegisterForBrowserNotifications(
+ const Browser* browser) {
// Start listening to TabStripModel events for this browser.
browser->tabstrip_model()->AddObserver(this);
- // The window isn't ready at this point, so we defer until it is.
+ // If this is a new window, it isn't ready at this point, so we register to be
+ // notified when it is. If this is an existing window, this is a no-op that we
+ // just do to reduce code complexity.
registrar_.Add(this, NotificationType::BROWSER_WINDOW_READY,
Source<const Browser>(browser));
+
+ if (browser->tabstrip_model()) {
+ for (int i = 0; i < browser->tabstrip_model()->count(); ++i)
+ RegisterForTabNotifications(
+ browser->tabstrip_model()->GetTabContentsAt(i));
+ }
+}
+
+void ExtensionBrowserEventRouter::RegisterForTabNotifications(
+ TabContents* contents) {
+ registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
+ Source<NavigationController>(&contents->controller()));
+
+ // Observing TAB_CONTENTS_DESTROYED is necessary because it's
+ // possible for tabs to be created, detached and then destroyed without
+ // ever having been re-attached and closed. This happens in the case of
+ // a devtools TabContents that is opened in window, docked, then closed.
+ registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
+ Source<TabContents>(contents));
}
void ExtensionBrowserEventRouter::OnBrowserWindowReady(const Browser* browser) {
@@ -168,15 +211,7 @@ void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents,
DispatchEvent(contents->profile(), events::kOnTabCreated, json_args);
- registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
- Source<NavigationController>(&contents->controller()));
-
- // Observing TAB_CONTENTS_DESTROYED is necessary because it's
- // possible for tabs to be created, detached and then destroyed without
- // ever having been re-attached and closed. This happens in the case of
- // a devtools TabContents that is opened in window, docked, then closed.
- registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
- Source<TabContents>(contents));
+ RegisterForTabNotifications(contents);
}
void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents,