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; |
}; |