| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" | 5 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; | 33 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; |
| 34 const char kNoIconSpecified[] = "Page action has no icons to show."; | 34 const char kNoIconSpecified[] = "Page action has no icons to show."; |
| 35 } | 35 } |
| 36 | 36 |
| 37 PageActionsFunction::PageActionsFunction() { | 37 PageActionsFunction::PageActionsFunction() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 PageActionsFunction::~PageActionsFunction() { | 40 PageActionsFunction::~PageActionsFunction() { |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool PageActionFunction::RunImpl() { | |
| 44 ExtensionActionFunction::RunImpl(); | |
| 45 | |
| 46 // If the first argument is an integer, it is the tabId. | |
| 47 base::Value* arg = NULL; | |
| 48 args_->Get(0, &arg); | |
| 49 if (arg->IsType(base::Value::TYPE_INTEGER)) | |
| 50 arg->GetAsInteger(&tab_id_); | |
| 51 | |
| 52 extension_action_ = GetExtension()->page_action(); | |
| 53 if (!extension_action_) { | |
| 54 error_ = kNoPageActionError; | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 // Find the TabContentsWrapper that contains this tab id. | |
| 59 contents_ = NULL; | |
| 60 TabContentsWrapper* wrapper = NULL; | |
| 61 bool result = ExtensionTabUtil::GetTabById( | |
| 62 tab_id_, profile(), include_incognito(), NULL, NULL, &wrapper, NULL); | |
| 63 if (!result || !wrapper) { | |
| 64 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
| 65 kNoTabError, base::IntToString(tab_id_)); | |
| 66 return false; | |
| 67 } | |
| 68 contents_ = wrapper; | |
| 69 | |
| 70 if (!RunExtensionAction()) | |
| 71 return false; | |
| 72 | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 bool PageActionsFunction::SetPageActionEnabled(bool enable) { | 43 bool PageActionsFunction::SetPageActionEnabled(bool enable) { |
| 77 std::string extension_action_id; | 44 std::string extension_action_id; |
| 78 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); | 45 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); |
| 79 DictionaryValue* action = NULL; | 46 DictionaryValue* action = NULL; |
| 80 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); | 47 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); |
| 81 | 48 |
| 82 int tab_id; | 49 int tab_id; |
| 83 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); | 50 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 84 std::string url; | 51 std::string url; |
| 85 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | 52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 95 |
| 129 // Set visibility and broadcast notifications that the UI should be updated. | 96 // Set visibility and broadcast notifications that the UI should be updated. |
| 130 page_action->SetIsVisible(tab_id, enable); | 97 page_action->SetIsVisible(tab_id, enable); |
| 131 page_action->SetTitle(tab_id, title); | 98 page_action->SetTitle(tab_id, title); |
| 132 page_action->SetIconIndex(tab_id, icon_id); | 99 page_action->SetIconIndex(tab_id, icon_id); |
| 133 contents->extension_tab_helper()->PageActionStateChanged(); | 100 contents->extension_tab_helper()->PageActionStateChanged(); |
| 134 | 101 |
| 135 return true; | 102 return true; |
| 136 } | 103 } |
| 137 | 104 |
| 138 bool PageActionFunction::SetVisible(bool visible) { | |
| 139 extension_action_->SetIsVisible(tab_id_, visible); | |
| 140 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 bool EnablePageActionsFunction::RunImpl() { | 105 bool EnablePageActionsFunction::RunImpl() { |
| 145 return SetPageActionEnabled(true); | 106 return SetPageActionEnabled(true); |
| 146 } | 107 } |
| 147 | 108 |
| 148 bool DisablePageActionsFunction::RunImpl() { | 109 bool DisablePageActionsFunction::RunImpl() { |
| 149 return SetPageActionEnabled(false); | 110 return SetPageActionEnabled(false); |
| 150 } | 111 } |
| 151 | |
| 152 bool PageActionShowFunction::RunExtensionAction() { | |
| 153 return SetVisible(true); | |
| 154 } | |
| 155 | |
| 156 bool PageActionHideFunction::RunExtensionAction() { | |
| 157 return SetVisible(false); | |
| 158 } | |
| 159 | |
| 160 bool PageActionSetIconFunction::RunExtensionAction() { | |
| 161 // setIcon can take a variant argument: either a canvas ImageData, or an | |
| 162 // icon index. | |
| 163 base::BinaryValue* binary = NULL; | |
| 164 int icon_index; | |
| 165 if (details_->GetBinary("imageData", &binary)) { | |
| 166 SetIcon(); | |
| 167 } else if (details_->GetInteger("iconIndex", &icon_index)) { | |
| 168 if (icon_index < 0 || static_cast<size_t>(icon_index) >= | |
| 169 extension_action_->icon_paths()->size()) { | |
| 170 error_ = kIconIndexOutOfBounds; | |
| 171 return false; | |
| 172 } | |
| 173 extension_action_->SetIcon(tab_id_, SkBitmap()); | |
| 174 extension_action_->SetIconIndex(tab_id_, icon_index); | |
| 175 } else { | |
| 176 EXTENSION_FUNCTION_VALIDATE(false); | |
| 177 } | |
| 178 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 bool PageActionSetTitleFunction::RunExtensionAction() { | |
| 183 if (!SetTitle()) | |
| 184 return false; | |
| 185 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 bool PageActionSetPopupFunction::RunExtensionAction() { | |
| 190 // TODO(skerner): Consider allowing null and undefined to mean the popup | |
| 191 // should be removed. | |
| 192 if (!SetPopup()) | |
| 193 return false; | |
| 194 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 // Not currently exposed to extensions. To re-enable, add mapping in | |
| 199 // extension_function_dispatcher. | |
| 200 bool PageActionSetBadgeBackgroundColorFunction::RunExtensionAction() { | |
| 201 if (!SetBadgeBackgroundColor()) | |
| 202 return false; | |
| 203 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 // Not currently exposed to extensions. To re-enable, add mapping in | |
| 208 // extension_function_dispatcher. | |
| 209 bool PageActionSetBadgeTextColorFunction::RunExtensionAction() { | |
| 210 ListValue* color_value = NULL; | |
| 211 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &color_value)); | |
| 212 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); | |
| 213 | |
| 214 int color_array[4] = {0}; | |
| 215 for (size_t i = 0; i < arraysize(color_array); ++i) | |
| 216 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); | |
| 217 | |
| 218 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], | |
| 219 color_array[2]); | |
| 220 extension_action_->SetBadgeTextColor(tab_id_, color); | |
| 221 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 225 // Not currently exposed to extensions. To re-enable, add mapping in | |
| 226 // extension_function_dispatcher. | |
| 227 bool PageActionSetBadgeTextFunction::RunExtensionAction() { | |
| 228 if (!SetBadgeText()) | |
| 229 return false; | |
| 230 contents_->extension_tab_helper()->PageActionStateChanged(); | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 bool PageActionGetTitleFunction::RunExtensionAction() { | |
| 235 return GetTitle(); | |
| 236 } | |
| 237 | |
| 238 bool PageActionGetPopupFunction::RunExtensionAction() { | |
| 239 return GetPopup(); | |
| 240 } | |
| 241 | |
| 242 // Not currently exposed to extensions. To re-enable, add mapping in | |
| 243 // extension_function_dispatcher. | |
| 244 bool PageActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | |
| 245 return GetBadgeBackgroundColor(); | |
| 246 } | |
| 247 | |
| 248 // Not currently exposed to extensions. To re-enable, add mapping in | |
| 249 // extension_function_dispatcher. | |
| 250 bool PageActionGetBadgeTextFunction::RunExtensionAction() { | |
| 251 return GetBadgeText(); | |
| 252 } | |
| OLD | NEW |