Chromium Code Reviews| Index: chrome/browser/extensions/api/extension_action/extension_action_api.cc |
| diff --git a/chrome/browser/extensions/api/extension_action/extension_action_api.cc b/chrome/browser/extensions/api/extension_action/extension_action_api.cc |
| index 98e73ee428297faf8f31e47801af00e6872994d5..dd1a98280cef343fe74801d296f1d142629cebee 100644 |
| --- a/chrome/browser/extensions/api/extension_action/extension_action_api.cc |
| +++ b/chrome/browser/extensions/api/extension_action/extension_action_api.cc |
| @@ -4,8 +4,6 @@ |
| #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| -#include <string> |
| - |
| #include "base/base64.h" |
| #include "base/lazy_instance.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -17,6 +15,7 @@ |
| #include "chrome/browser/extensions/extension_action.h" |
| #include "chrome/browser/extensions/extension_action_manager.h" |
| #include "chrome/browser/extensions/extension_function_registry.h" |
| +#include "chrome/browser/extensions/extension_host.h" |
| #include "chrome/browser/extensions/extension_service.h" |
| #include "chrome/browser/extensions/extension_system.h" |
| #include "chrome/browser/extensions/extension_tab_util.h" |
| @@ -59,6 +58,7 @@ const char kNoTabError[] = "No tab with id: *."; |
| const char kNoPageActionError[] = |
| "This extension has no page action specified."; |
| const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| +const char kInternalError[] = "Internal error."; |
| struct IconRepresentationInfo { |
| // Size as a string that will be used to retrieve representation value from |
| @@ -209,6 +209,7 @@ ExtensionActionAPI::ExtensionActionAPI(Profile* profile) { |
| registry->RegisterFunction<BrowserActionGetPopupFunction>(); |
| registry->RegisterFunction<BrowserActionEnableFunction>(); |
| registry->RegisterFunction<BrowserActionDisableFunction>(); |
| + registry->RegisterFunction<BrowserActionOpenPopupFunction>(); |
| // Page Actions |
| registry->RegisterFunction<EnablePageActionsFunction>(); |
| @@ -805,6 +806,59 @@ bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { |
| return true; |
| } |
| +BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction() |
| + : response_sent_(false) { |
| +} |
| + |
| +bool BrowserActionOpenPopupFunction::RunImpl() { |
| + ExtensionToolbarModel* model = extensions::ExtensionSystem::Get(profile_)-> |
| + extension_service()->toolbar_model(); |
| + if (!model) { |
| + error_ = kInternalError; |
| + return false; |
| + } |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| + content::Source<Profile>(profile_)); |
| + model->ShowBrowserActionPopup(extension_); |
| + |
| + // Set a timeout for waiting for the notification that the popup is loaded. |
| + // Waiting is required so that the popup view can be retrieved by the custom |
| + // bindings for response callback. |
| + base::MessageLoopForUI::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&BrowserActionOpenPopupFunction::OpenPopupTimedOut, this), |
| + base::TimeDelta::FromSeconds(1)); |
| + return true; |
| +} |
| + |
| +void BrowserActionOpenPopupFunction::OpenPopupTimedOut() { |
| + if (response_sent_) |
| + return; |
| + |
| + DVLOG(1) << "chrome.browserAction.openPopup did not show a popup."; |
| + // Custom binding will still try to get the popup, but may fail. |
| + SendResponse(new base::ListValue()); |
| + response_sent_ = true; |
| +} |
| + |
| +void BrowserActionOpenPopupFunction::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING); |
| + if (response_sent_) |
| + return; |
| + |
| + ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| + if (host->extension()->id() != extension_->id()) |
| + return; |
| + |
| + SendResponse(new base::ListValue()); |
|
mark a. foltz
2013/10/16 19:00:53
Is this what you intended? The signature for Exte
justinlin
2013/10/16 19:31:29
Oh good catch. So, apparently base::Value* can be
|
| + response_sent_ = true; |
|
mark a. foltz
2013/10/16 19:00:53
You could also check for NULL-ness of results_, bu
justinlin
2013/10/16 19:31:29
I think it's okay not to set result here since we
|
| + // Don't need to de-register notification listener since the last ref for this |
|
mark a. foltz
2013/10/16 19:00:53
Would it hurt to de-register it anyway?
justinlin
2013/10/16 19:31:29
Done.
|
| + // object will go away after OpenPopupTimedOut is called. |
| +} |
| + |
| // |
| // ScriptBadgeGetAttentionFunction |
| // |