diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-16 21:53:46 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-16 21:53:46 +0000 |
commit | 744ef17706b3c77d087809ec119bbc2f7bd17c89 (patch) | |
tree | 24eb889c256f49dd3d24610b68c5e09b90a5c21d /chrome/browser/extensions/extension_page_actions_module.cc | |
parent | 0db22afeba278540d2457ebfead171a8a54feb3c (diff) | |
download | chromium_src-744ef17706b3c77d087809ec119bbc2f7bd17c89.zip chromium_src-744ef17706b3c77d087809ec119bbc2f7bd17c89.tar.gz chromium_src-744ef17706b3c77d087809ec119bbc2f7bd17c89.tar.bz2 |
Implement new page action API.
Still no actual drawing code for badges, though.
BUG=24635
TEST=Load the sample test_page_action extension and click the page action to cycle through various states.
Review URL: http://codereview.chromium.org/269079
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29335 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_page_actions_module.cc')
-rw-r--r-- | chrome/browser/extensions/extension_page_actions_module.cc | 178 |
1 files changed, 171 insertions, 7 deletions
diff --git a/chrome/browser/extensions/extension_page_actions_module.cc b/chrome/browser/extensions/extension_page_actions_module.cc index 51866e3..60eb86a 100644 --- a/chrome/browser/extensions/extension_page_actions_module.cc +++ b/chrome/browser/extensions/extension_page_actions_module.cc @@ -15,9 +15,21 @@ #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_error_utils.h" +#include "chrome/common/render_messages.h" namespace keys = extension_page_actions_module_constants; +namespace { +// Errors. +const char kNoExtensionError[] = "No extension with id: *."; +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 kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; +} + +// TODO(EXTENSIONS_DEPRECATED): obsolete API. bool PageActionFunction::SetPageActionEnabled(bool enable) { EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); const ListValue* args = static_cast<const ListValue*>(args_); @@ -48,7 +60,7 @@ bool PageActionFunction::SetPageActionEnabled(bool enable) { TabContents* contents = NULL; ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL); if (!contents) { - error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoTabError, + error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, IntToString(tab_id)); return false; } @@ -56,8 +68,7 @@ bool PageActionFunction::SetPageActionEnabled(bool enable) { // Make sure the URL hasn't changed. NavigationEntry* entry = contents->controller().GetActiveEntry(); if (!entry || url != entry->url().spec()) { - error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kUrlNotActiveError, - url); + error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); return false; } @@ -66,22 +77,52 @@ bool PageActionFunction::SetPageActionEnabled(bool enable) { ExtensionsService* service = profile()->GetExtensionsService(); extension = service->GetExtensionById(extension_id()); if (!extension) { - error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, + error_ = ExtensionErrorUtils::FormatErrorMessage(kNoExtensionError, extension_id()); return false; } const ExtensionAction* page_action = extension->page_action(); if (!page_action) { - error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoPageActionError, - page_action_id); + error_ = kNoPageActionError; return false; } // Set visibility and broadcast notifications that the UI should be updated. contents->SetPageActionEnabled(page_action, enable, title, icon_id); - contents->NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS); + contents->PageActionStateChanged(); + + return true; +} + +bool PageActionFunction::InitCommon(int tab_id) { + page_action_ = dispatcher()->GetExtension()->page_action(); + if (!page_action_) { + error_ = kNoPageActionError; + return false; + } + + // Find the TabContents that contains this tab id. + contents_ = NULL; + ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents_, NULL); + if (!contents_) { + error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, + IntToString(tab_id)); + return false; + } + + state_ = contents_->GetOrCreatePageActionState(page_action_); + return true; +} + +bool PageActionFunction::SetHidden(bool hidden) { + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); + if (!InitCommon(tab_id)) + return false; + state_->set_hidden(hidden); + contents_->PageActionStateChanged(); return true; } @@ -92,3 +133,126 @@ bool EnablePageActionFunction::RunImpl() { bool DisablePageActionFunction::RunImpl() { return SetPageActionEnabled(false); } + +bool PageActionShowFunction::RunImpl() { + return SetHidden(false); +} + +bool PageActionHideFunction::RunImpl() { + return SetHidden(true); +} + +bool PageActionSetIconFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); + + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); + if (!InitCommon(tab_id)) + return false; + + // setIcon can take a variant argument: either a canvas ImageData, or an + // icon index. + BinaryValue* binary; + int icon_index; + if (args->GetBinary(L"imageData", &binary)) { + IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); + void* iter = NULL; + scoped_ptr<SkBitmap> bitmap(new SkBitmap); + EXTENSION_FUNCTION_VALIDATE( + IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); + state_->set_icon(bitmap.release()); + } else if (args->GetInteger(L"iconIndex", &icon_index)) { + if (icon_index < 0 || + static_cast<size_t>(icon_index) >= page_action_->icon_paths().size()) { + error_ = kIconIndexOutOfBounds; + return false; + } + state_->set_icon(NULL); + state_->set_icon_index(icon_index); + } else { + EXTENSION_FUNCTION_VALIDATE(false); + } + + contents_->PageActionStateChanged(); + return true; +} + +bool PageActionSetTitleFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); + + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); + if (!InitCommon(tab_id)) + return false; + + std::string title; + EXTENSION_FUNCTION_VALIDATE(args->GetString(L"title", &title)); + + state_->set_title(title); + contents_->PageActionStateChanged(); + return true; +} + +bool PageActionSetBadgeBackgroundColorFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); + + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); + if (!InitCommon(tab_id)) + return false; + + ListValue* color_value; + EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); + EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); + + int color_array[4] = {0}; + for (size_t i = 0; i < arraysize(color_array); ++i) + EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); + + SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], + color_array[3]); + state_->set_badge_background_color(color); + contents_->PageActionStateChanged(); + return true; +} + +bool PageActionSetBadgeTextColorFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); + + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); + if (!InitCommon(tab_id)) + return false; + + ListValue* color_value; + EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); + EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); + + int color_array[4] = {0}; + for (size_t i = 0; i < arraysize(color_array); ++i) + EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); + + // TODO(mpcomplete): implement text coloring. + return true; +} + +bool PageActionSetBadgeTextFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); + + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); + if (!InitCommon(tab_id)) + return false; + + std::string text; + EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); + + state_->set_badge_text(text); + contents_->PageActionStateChanged(); + return true; +} |