| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extension_page_actions_module.h" | 5 #include "chrome/browser/extensions/extension_page_actions_module.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const char kNoPageActionError[] = | 25 const char kNoPageActionError[] = |
| 26 "This extension has no page action specified."; | 26 "This extension has no page action specified."; |
| 27 const char kUrlNotActiveError[] = "This url is no longer active: *."; | 27 const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| 28 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; | 28 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; |
| 29 const char kNoIconSpecified[] = "Page action has no icons to show."; | 29 const char kNoIconSpecified[] = "Page action has no icons to show."; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // TODO(EXTENSIONS_DEPRECATED): obsolete API. | 32 // TODO(EXTENSIONS_DEPRECATED): obsolete API. |
| 33 bool PageActionFunction::SetPageActionEnabled(bool enable) { | 33 bool PageActionFunction::SetPageActionEnabled(bool enable) { |
| 34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 35 const ListValue* args = static_cast<const ListValue*>(args_); | 35 const ListValue* args = args_as_list(); |
| 36 | 36 |
| 37 std::string page_action_id; | 37 std::string page_action_id; |
| 38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id)); | 38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id)); |
| 39 DictionaryValue* action; | 39 DictionaryValue* action; |
| 40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action)); | 40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action)); |
| 41 | 41 |
| 42 int tab_id; | 42 int tab_id; |
| 43 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); | 43 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 44 std::string url; | 44 std::string url; |
| 45 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | 45 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 bool PageActionShowFunction::RunImpl() { | 134 bool PageActionShowFunction::RunImpl() { |
| 135 return SetVisible(true); | 135 return SetVisible(true); |
| 136 } | 136 } |
| 137 | 137 |
| 138 bool PageActionHideFunction::RunImpl() { | 138 bool PageActionHideFunction::RunImpl() { |
| 139 return SetVisible(false); | 139 return SetVisible(false); |
| 140 } | 140 } |
| 141 | 141 |
| 142 bool PageActionSetIconFunction::RunImpl() { | 142 bool PageActionSetIconFunction::RunImpl() { |
| 143 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 143 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 144 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 144 const DictionaryValue* args = args_as_dictionary(); |
| 145 | 145 |
| 146 int tab_id; | 146 int tab_id; |
| 147 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); | 147 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); |
| 148 if (!InitCommon(tab_id)) | 148 if (!InitCommon(tab_id)) |
| 149 return false; | 149 return false; |
| 150 | 150 |
| 151 // setIcon can take a variant argument: either a canvas ImageData, or an | 151 // setIcon can take a variant argument: either a canvas ImageData, or an |
| 152 // icon index. | 152 // icon index. |
| 153 BinaryValue* binary; | 153 BinaryValue* binary; |
| 154 int icon_index; | 154 int icon_index; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 170 } else { | 170 } else { |
| 171 EXTENSION_FUNCTION_VALIDATE(false); | 171 EXTENSION_FUNCTION_VALIDATE(false); |
| 172 } | 172 } |
| 173 | 173 |
| 174 contents_->PageActionStateChanged(); | 174 contents_->PageActionStateChanged(); |
| 175 return true; | 175 return true; |
| 176 } | 176 } |
| 177 | 177 |
| 178 bool PageActionSetTitleFunction::RunImpl() { | 178 bool PageActionSetTitleFunction::RunImpl() { |
| 179 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 179 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 180 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 180 const DictionaryValue* args = args_as_dictionary(); |
| 181 | 181 |
| 182 int tab_id; | 182 int tab_id; |
| 183 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); | 183 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); |
| 184 if (!InitCommon(tab_id)) | 184 if (!InitCommon(tab_id)) |
| 185 return false; | 185 return false; |
| 186 | 186 |
| 187 std::string title; | 187 std::string title; |
| 188 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"title", &title)); | 188 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"title", &title)); |
| 189 | 189 |
| 190 page_action_->SetTitle(tab_id, title); | 190 page_action_->SetTitle(tab_id, title); |
| 191 contents_->PageActionStateChanged(); | 191 contents_->PageActionStateChanged(); |
| 192 return true; | 192 return true; |
| 193 } | 193 } |
| 194 | 194 |
| 195 // Not currently exposed to extensions. To re-enable, add mapping in | 195 // Not currently exposed to extensions. To re-enable, add mapping in |
| 196 // extension_function_dispatcher. | 196 // extension_function_dispatcher. |
| 197 bool PageActionSetBadgeBackgroundColorFunction::RunImpl() { | 197 bool PageActionSetBadgeBackgroundColorFunction::RunImpl() { |
| 198 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 198 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 199 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 199 const DictionaryValue* args = args_as_dictionary(); |
| 200 | 200 |
| 201 int tab_id; | 201 int tab_id; |
| 202 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); | 202 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); |
| 203 if (!InitCommon(tab_id)) | 203 if (!InitCommon(tab_id)) |
| 204 return false; | 204 return false; |
| 205 | 205 |
| 206 ListValue* color_value; | 206 ListValue* color_value; |
| 207 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); | 207 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); |
| 208 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); | 208 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); |
| 209 | 209 |
| 210 int color_array[4] = {0}; | 210 int color_array[4] = {0}; |
| 211 for (size_t i = 0; i < arraysize(color_array); ++i) | 211 for (size_t i = 0; i < arraysize(color_array); ++i) |
| 212 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); | 212 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); |
| 213 | 213 |
| 214 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], | 214 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], |
| 215 color_array[2]); | 215 color_array[2]); |
| 216 page_action_->SetBadgeBackgroundColor(tab_id, color); | 216 page_action_->SetBadgeBackgroundColor(tab_id, color); |
| 217 contents_->PageActionStateChanged(); | 217 contents_->PageActionStateChanged(); |
| 218 return true; | 218 return true; |
| 219 } | 219 } |
| 220 | 220 |
| 221 // Not currently exposed to extensions. To re-enable, add mapping in | 221 // Not currently exposed to extensions. To re-enable, add mapping in |
| 222 // extension_function_dispatcher. | 222 // extension_function_dispatcher. |
| 223 bool PageActionSetBadgeTextColorFunction::RunImpl() { | 223 bool PageActionSetBadgeTextColorFunction::RunImpl() { |
| 224 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 224 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 225 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 225 const DictionaryValue* args = args_as_dictionary(); |
| 226 | 226 |
| 227 int tab_id; | 227 int tab_id; |
| 228 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); | 228 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); |
| 229 if (!InitCommon(tab_id)) | 229 if (!InitCommon(tab_id)) |
| 230 return false; | 230 return false; |
| 231 | 231 |
| 232 ListValue* color_value; | 232 ListValue* color_value; |
| 233 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); | 233 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); |
| 234 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); | 234 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); |
| 235 | 235 |
| 236 int color_array[4] = {0}; | 236 int color_array[4] = {0}; |
| 237 for (size_t i = 0; i < arraysize(color_array); ++i) | 237 for (size_t i = 0; i < arraysize(color_array); ++i) |
| 238 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); | 238 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); |
| 239 | 239 |
| 240 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], | 240 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], |
| 241 color_array[2]); | 241 color_array[2]); |
| 242 page_action_->SetBadgeTextColor(tab_id, color); | 242 page_action_->SetBadgeTextColor(tab_id, color); |
| 243 contents_->PageActionStateChanged(); | 243 contents_->PageActionStateChanged(); |
| 244 return true; | 244 return true; |
| 245 } | 245 } |
| 246 | 246 |
| 247 // Not currently exposed to extensions. To re-enable, add mapping in | 247 // Not currently exposed to extensions. To re-enable, add mapping in |
| 248 // extension_function_dispatcher. | 248 // extension_function_dispatcher. |
| 249 bool PageActionSetBadgeTextFunction::RunImpl() { | 249 bool PageActionSetBadgeTextFunction::RunImpl() { |
| 250 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 250 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 251 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 251 const DictionaryValue* args = args_as_dictionary(); |
| 252 | 252 |
| 253 int tab_id; | 253 int tab_id; |
| 254 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); | 254 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); |
| 255 if (!InitCommon(tab_id)) | 255 if (!InitCommon(tab_id)) |
| 256 return false; | 256 return false; |
| 257 | 257 |
| 258 std::string text; | 258 std::string text; |
| 259 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); | 259 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); |
| 260 | 260 |
| 261 page_action_->SetBadgeText(tab_id, text); | 261 page_action_->SetBadgeText(tab_id, text); |
| 262 contents_->PageActionStateChanged(); | 262 contents_->PageActionStateChanged(); |
| 263 return true; | 263 return true; |
| 264 } | 264 } |
| OLD | NEW |