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

Unified Diff: chrome/browser/extensions/api/extension_action/extension_action_api.cc

Issue 11638022: Consolidate [BrowserAction,PageAction,ScriptBadge]API into ExtensionActionAPI (Closed) Base URL: http://git.chromium.org/chromium/src.git@dc_unref_script_badge
Patch Set: Created 8 years 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/extensions/api/extension_action/extension_action_api.cc
diff --git a/chrome/browser/extensions/api/extension_action/extension_actions_api.cc b/chrome/browser/extensions/api/extension_action/extension_action_api.cc
similarity index 82%
rename from chrome/browser/extensions/api/extension_action/extension_actions_api.cc
rename to chrome/browser/extensions/api/extension_action/extension_action_api.cc
index d40fddc91acda556e7506a0456af97cadf6e75b0..aa4c6fbde7e4f7201d3bfed6ed8902d726ddaf45 100644
--- a/chrome/browser/extensions/api/extension_action/extension_actions_api.cc
+++ b/chrome/browser/extensions/api/extension_action/extension_action_api.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/extensions/api/extension_action/extension_actions_api.h"
+#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
#include <string>
@@ -10,7 +10,7 @@
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/values.h"
-#include "chrome/browser/extensions/api/extension_action/extension_page_actions_api_constants.h"
+#include "chrome/browser/extensions/api/extension_action/extension_action_api_constants.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_service.h"
@@ -22,6 +22,9 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/api/extension_action/action_info.h"
+#include "chrome/common/extensions/api/extension_action/script_badge_handler.h"
+#include "chrome/common/extensions/extension_manifest_constants.h"
+#include "chrome/common/extensions/manifest_handler.h"
#include "chrome/common/render_messages.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
@@ -42,6 +45,10 @@ const char kAppearanceStorageKey[] = "appearance";
const char kNoExtensionActionError[] =
"This extension has no action specified.";
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: *.";
+
struct IconRepresentationInfo {
// Size as a string that will be used to retrieve representation value from
@@ -171,6 +178,23 @@ scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) {
namespace extensions {
+namespace keys = extension_action_api_constants;
+
+//
+// ExtensionActionAPI
+//
+
+ExtensionActionAPI::ExtensionActionAPI(Profile* profile) {
+ ManifestHandler::Register(extension_manifest_keys::kScriptBadge,
+ new ScriptBadgeHandler);
+}
+
+ExtensionActionAPI::~ExtensionActionAPI() {
+}
+
+void ExtensionActionAPI::Shutdown() {
+}
+
//
// ExtensionActionStorageManager
//
@@ -198,7 +222,7 @@ void ExtensionActionStorageManager::Observe(
case chrome::NOTIFICATION_EXTENSION_LOADED: {
const Extension* extension =
content::Details<const Extension>(details).ptr();
- if (!extensions::ExtensionActionManager::Get(profile_)->
+ if (!ExtensionActionManager::Get(profile_)->
GetBrowserAction(*extension)) {
break;
}
@@ -267,9 +291,6 @@ void ExtensionActionStorageManager::ReadFromStorage(
SetDefaultsFromValue(dict, browser_action);
}
-} // namespace extensions
-
-
//
// ExtensionActionFunction
//
@@ -285,9 +306,8 @@ ExtensionActionFunction::~ExtensionActionFunction() {
}
bool ExtensionActionFunction::RunImpl() {
- extensions::ExtensionActionManager* manager =
- extensions::ExtensionActionManager::Get(profile_);
- const extensions::Extension* extension = GetExtension();
+ ExtensionActionManager* manager = ExtensionActionManager::Get(profile_);
+ const Extension* extension = GetExtension();
if (StartsWithASCII(name(), "scriptBadge.", false)) {
extension_action_ = manager->GetScriptBadge(*extension);
} else if (StartsWithASCII(name(), "systemIndicator.", false)) {
@@ -314,16 +334,16 @@ bool ExtensionActionFunction::RunImpl() {
ExtensionTabUtil::GetTabById(
tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL);
if (!contents_) {
- error_ = extensions::ErrorUtils::FormatErrorMessage(
+ error_ = ErrorUtils::FormatErrorMessage(
kNoTabError, base::IntToString(tab_id_));
return false;
}
} else {
// Only browser actions and system indicators have a default tabId.
- extensions::ActionInfo::Type action_type = extension_action_->action_type();
+ ActionInfo::Type action_type = extension_action_->action_type();
EXTENSION_FUNCTION_VALIDATE(
- action_type == extensions::ActionInfo::TYPE_BROWSER ||
- action_type == extensions::ActionInfo::TYPE_SYSTEM_INDICATOR);
+ action_type == ActionInfo::TYPE_BROWSER ||
+ action_type == ActionInfo::TYPE_SYSTEM_INDICATOR);
}
return RunExtensionAction();
}
@@ -377,20 +397,20 @@ bool ExtensionActionFunction::ExtractDataFromArguments() {
void ExtensionActionFunction::NotifyChange() {
switch (extension_action_->action_type()) {
- case extensions::ActionInfo::TYPE_BROWSER:
- case extensions::ActionInfo::TYPE_PAGE:
- if (extensions::ExtensionActionManager::Get(profile_)->
- GetBrowserAction(*extension_)) {
+ case ActionInfo::TYPE_BROWSER:
+ case ActionInfo::TYPE_PAGE:
+ if (ExtensionActionManager::Get(profile_)->
+ GetBrowserAction(*extension_)) {
NotifyBrowserActionChange();
- } else if (extensions::ExtensionActionManager::Get(profile_)->
- GetPageAction(*extension_)) {
+ } else if (ExtensionActionManager::Get(profile_)->
+ GetPageAction(*extension_)) {
NotifyLocationBarChange();
}
return;
- case extensions::ActionInfo::TYPE_SCRIPT_BADGE:
+ case ActionInfo::TYPE_SCRIPT_BADGE:
NotifyLocationBarChange();
return;
- case extensions::ActionInfo::TYPE_SYSTEM_INDICATOR:
+ case ActionInfo::TYPE_SYSTEM_INDICATOR:
NotifySystemIndicatorChange();
return;
}
@@ -405,7 +425,7 @@ void ExtensionActionFunction::NotifyBrowserActionChange() {
}
void ExtensionActionFunction::NotifyLocationBarChange() {
- extensions::TabHelper::FromWebContents(contents_)->
+ TabHelper::FromWebContents(contents_)->
location_bar_controller()->NotifyChange();
}
@@ -462,9 +482,9 @@ bool ExtensionActionFunction::SetVisible(bool visible) {
return true;
}
-extensions::TabHelper& ExtensionActionFunction::tab_helper() const {
+TabHelper& ExtensionActionFunction::tab_helper() const {
CHECK(contents_);
- return *extensions::TabHelper::FromWebContents(contents_);
+ return *TabHelper::FromWebContents(contents_);
}
bool ExtensionActionShowFunction::RunExtensionAction() {
@@ -589,3 +609,86 @@ bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() {
SetResult(list);
return true;
}
+
+//
+// PageActionFunction
Yoyo Zhou 2012/12/19 23:30:52 Comment here that this is deprecated.
Devlin 2012/12/20 19:43:20 Done.
+//
+
+PageActionFunction::PageActionFunction() {
+}
+
+PageActionFunction::~PageActionFunction() {
+}
+
+bool PageActionFunction::SetPageActionEnabled(bool enable) {
+ std::string extension_action_id;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id));
+ DictionaryValue* action = NULL;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action));
+
+ int tab_id;
+ EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id));
+ std::string url;
+ EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url));
+
+ std::string title;
+ if (enable) {
+ if (action->HasKey(keys::kTitleKey))
+ EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title));
+ }
+
+ ExtensionAction* page_action =
+ extensions::ExtensionActionManager::Get(profile())->
+ GetPageAction(*GetExtension());
+ if (!page_action) {
+ error_ = kNoPageActionError;
+ return false;
+ }
+
+ // Find the WebContents that contains this tab id.
+ content::WebContents* contents = NULL;
+ bool result = ExtensionTabUtil::GetTabById(
+ tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL);
+ if (!result || !contents) {
+ error_ = ErrorUtils::FormatErrorMessage(
+ kNoTabError, base::IntToString(tab_id));
+ return false;
+ }
+
+ // Make sure the URL hasn't changed.
+ content::NavigationEntry* entry = contents->GetController().GetActiveEntry();
+ if (!entry || url != entry->GetURL().spec()) {
+ error_ = ErrorUtils::FormatErrorMessage(kUrlNotActiveError, url);
+ return false;
+ }
+
+ // Set visibility and broadcast notifications that the UI should be updated.
+ page_action->SetAppearance(
+ tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE);
+ page_action->SetTitle(tab_id, title);
+ extensions::TabHelper::FromWebContents(contents)->
+ location_bar_controller()->NotifyChange();
+
+ return true;
+}
+
+bool EnablePageActionFunction::RunImpl() {
+ return SetPageActionEnabled(true);
+}
+
+bool DisablePageActionFunction::RunImpl() {
+ return SetPageActionEnabled(false);
+}
+
+//
+// ScriptBadgeGetAttentionFunction
Yoyo Zhou 2012/12/19 23:30:52 Put this above the deprecated code.
Devlin 2012/12/20 19:43:20 Done.
+//
+
+ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {}
+
+bool ScriptBadgeGetAttentionFunction::RunExtensionAction() {
+ tab_helper().location_bar_controller()->GetAttentionFor(extension_id());
+ return true;
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698