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

Unified Diff: chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc

Issue 7672009: Lazy creating of background pages --enable-lazy-background-pages) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 4 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
Index: chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc
diff --git a/chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc b/chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc
index 5611b0cee7e841f02e5531e04b904c7bd1393bba..cc01309dc3aee8bac76c5e64b859794113853916 100644
--- a/chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc
+++ b/chrome/browser/ui/gtk/browser_actions_toolbar_gtk.cc
@@ -7,6 +7,7 @@
#include <algorithm>
#include <vector>
+#include "base/command_line.h"
#include "base/i18n/rtl.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
@@ -27,6 +28,7 @@
#include "chrome/browser/ui/gtk/view_id_util.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/chrome_notification_types.h"
+#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_action.h"
#include "chrome/common/extensions/extension_resource.h"
@@ -94,7 +96,8 @@ class BrowserActionButton : public NotificationObserver,
image_(NULL),
tracker_(this),
tab_specific_icon_(NULL),
- default_icon_(NULL) {
+ default_icon_(NULL),
+ waiting_for_background_page_(false) {
button_.reset(new CustomDrawButton(
theme_provider,
IDR_BROWSER_ACTION,
@@ -153,10 +156,17 @@ class BrowserActionButton : public NotificationObserver,
void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
- if (type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED)
+ if (type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED) {
UpdateState();
- else
+ } else if (type == chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING) {
+ BrowserActionExecuted();
+ registrar_.Remove(this,
+ chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
+ Source<Profile>(toolbar_->browser()->profile()));
+ waiting_for_background_page_ = false;
+ } else {
NOTREACHED();
+ }
}
// ImageLoadingTracker::Observer implementation.
@@ -169,6 +179,34 @@ class BrowserActionButton : public NotificationObserver,
UpdateState();
}
+ bool IsBackgroundPageMissing() {
+ if (extension_->background_url().is_valid()) {
+ ExtensionProcessManager* pm =
+ toolbar_->browser()->profile()->GetExtensionProcessManager();
+ return pm->GetBackgroundHostForExtension(extension_) == NULL;
+ }
+
+ return false; // Extension doesn't have a background page.
+ }
+
+
+ void AddObserverForBackgroundPageLoaded() {
+ if (waiting_for_background_page_)
+ return; // NotificationRegistrar doesn't like dups.
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
+ Source<Profile>(toolbar_->browser()->profile()));
+ waiting_for_background_page_ = true;
+ }
+
+ void BrowserActionExecuted() {
+ ExtensionService* service =
+ toolbar_->browser()->profile()->GetExtensionService();
+ service->browser_event_router()->BrowserActionExecuted(
+ toolbar_->browser()->profile(), extension_->id(),
+ toolbar_->browser());
+ }
+
// Updates the button based on the latest state from the associated
// browser action.
void UpdateState() {
@@ -289,11 +327,20 @@ class BrowserActionButton : public NotificationObserver,
if (action->ShowPopup(false))
return;
- ExtensionService* service =
- action->toolbar_->browser()->profile()->GetExtensionService();
- service->browser_event_router()->BrowserActionExecuted(
- action->toolbar_->browser()->profile(), action->extension_->id(),
- action->toolbar_->browser());
+ // Time to create the background page!
+ if (CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableLazyBackgroundPages) &&
+ action->IsBackgroundPageMissing()) {
+ // Dispatch the OnClicked event when the background page is loaded.
+ action->AddObserverForBackgroundPageLoaded();
+
+ ExtensionProcessManager* pm =
+ action->toolbar_->browser()->profile()->GetExtensionProcessManager();
+ pm->CreateBackgroundHost(action->extension_,
+ action->extension_->background_url());
+ } else {
+ action->BrowserActionExecuted();
+ }
}
static gboolean OnExposeEvent(GtkWidget* widget,
@@ -359,6 +406,8 @@ class BrowserActionButton : public NotificationObserver,
scoped_ptr<MenuGtk> context_menu_;
scoped_refptr<ExtensionContextMenuModel> context_menu_model_;
+ bool waiting_for_background_page_;
+
friend class BrowserActionsToolbarGtk;
};

Powered by Google App Engine
This is Rietveld 408576698