| 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_actions_api.h
" | 5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/lazy_instance.h" |
| 10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 14 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
| 14 #include "chrome/browser/extensions/extension_action.h" | 15 #include "chrome/browser/extensions/extension_action.h" |
| 15 #include "chrome/browser/extensions/extension_action_manager.h" | 16 #include "chrome/browser/extensions/extension_action_manager.h" |
| 17 #include "chrome/browser/extensions/extension_function_registry.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
| 18 #include "chrome/browser/extensions/extension_tab_util.h" | 20 #include "chrome/browser/extensions/extension_tab_util.h" |
| 19 #include "chrome/browser/extensions/location_bar_controller.h" | 21 #include "chrome/browser/extensions/location_bar_controller.h" |
| 20 #include "chrome/browser/extensions/state_store.h" | 22 #include "chrome/browser/extensions/state_store.h" |
| 21 #include "chrome/browser/extensions/tab_helper.h" | 23 #include "chrome/browser/extensions/tab_helper.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/extensions/api/extension_action/action_info.h" | 26 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 27 #include "chrome/common/extensions/api/extension_action/script_badge_handler.h" |
| 28 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 29 #include "chrome/common/extensions/manifest_handler.h" |
| 25 #include "chrome/common/render_messages.h" | 30 #include "chrome/common/render_messages.h" |
| 26 #include "content/public/browser/navigation_entry.h" | 31 #include "content/public/browser/navigation_entry.h" |
| 27 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
| 28 #include "extensions/common/error_utils.h" | 33 #include "extensions/common/error_utils.h" |
| 29 | 34 |
| 35 namespace extensions { |
| 36 |
| 30 namespace { | 37 namespace { |
| 31 | 38 |
| 32 const char kBrowserActionStorageKey[] = "browser_action"; | 39 const char kBrowserActionStorageKey[] = "browser_action"; |
| 33 const char kPopupUrlStorageKey[] = "poupup_url"; | 40 const char kPopupUrlStorageKey[] = "poupup_url"; |
| 34 const char kTitleStorageKey[] = "title"; | 41 const char kTitleStorageKey[] = "title"; |
| 35 const char kIconStorageKey[] = "icon"; | 42 const char kIconStorageKey[] = "icon"; |
| 36 const char kBadgeTextStorageKey[] = "badge_text"; | 43 const char kBadgeTextStorageKey[] = "badge_text"; |
| 37 const char kBadgeBackgroundColorStorageKey[] = "badge_background_color"; | 44 const char kBadgeBackgroundColorStorageKey[] = "badge_background_color"; |
| 38 const char kBadgeTextColorStorageKey[] = "badge_text_color"; | 45 const char kBadgeTextColorStorageKey[] = "badge_text_color"; |
| 39 const char kAppearanceStorageKey[] = "appearance"; | 46 const char kAppearanceStorageKey[] = "appearance"; |
| 40 | 47 |
| 41 // Errors. | 48 // Errors. |
| 42 const char kNoExtensionActionError[] = | 49 const char kNoExtensionActionError[] = |
| 43 "This extension has no action specified."; | 50 "This extension has no action specified."; |
| 44 const char kNoTabError[] = "No tab with id: *."; | 51 const char kNoTabError[] = "No tab with id: *."; |
| 52 const char kNoPageActionError[] = |
| 53 "This extension has no page action specified."; |
| 54 const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| 45 | 55 |
| 46 struct IconRepresentationInfo { | 56 struct IconRepresentationInfo { |
| 47 // Size as a string that will be used to retrieve representation value from | 57 // Size as a string that will be used to retrieve representation value from |
| 48 // SetIcon function arguments. | 58 // SetIcon function arguments. |
| 49 const char* size_string; | 59 const char* size_string; |
| 50 // Scale factor for which the represantion should be used. | 60 // Scale factor for which the represantion should be used. |
| 51 ui::ScaleFactor scale; | 61 ui::ScaleFactor scale; |
| 52 }; | 62 }; |
| 53 | 63 |
| 54 | |
| 55 const IconRepresentationInfo kIconSizes[] = { | 64 const IconRepresentationInfo kIconSizes[] = { |
| 56 { "19", ui::SCALE_FACTOR_100P }, | 65 { "19", ui::SCALE_FACTOR_100P }, |
| 57 { "38", ui::SCALE_FACTOR_200P } | 66 { "38", ui::SCALE_FACTOR_200P } |
| 58 }; | 67 }; |
| 59 | 68 |
| 60 // Conversion function for reading/writing to storage. | 69 // Conversion function for reading/writing to storage. |
| 61 SkColor RawStringToSkColor(const std::string& str) { | 70 SkColor RawStringToSkColor(const std::string& str) { |
| 62 uint64 value = 0; | 71 uint64 value = 0; |
| 63 base::StringToUint64(str, &value); | 72 base::StringToUint64(str, &value); |
| 64 SkColor color = static_cast<SkColor>(value); | 73 SkColor color = static_cast<SkColor>(value); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 RepresentationToString(icon, kIconSizes[i].scale)); | 171 RepresentationToString(icon, kIconSizes[i].scale)); |
| 163 } | 172 } |
| 164 } | 173 } |
| 165 dict->Set(kIconStorageKey, icon_value); | 174 dict->Set(kIconStorageKey, icon_value); |
| 166 } | 175 } |
| 167 return dict.Pass(); | 176 return dict.Pass(); |
| 168 } | 177 } |
| 169 | 178 |
| 170 } // namespace | 179 } // namespace |
| 171 | 180 |
| 172 namespace extensions { | 181 // |
| 182 // ExtensionActionAPI |
| 183 // |
| 184 |
| 185 static base::LazyInstance<ProfileKeyedAPIFactory<ExtensionActionAPI> > |
| 186 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 187 |
| 188 ExtensionActionAPI::ExtensionActionAPI(Profile* profile) { |
| 189 ManifestHandler::Register(extension_manifest_keys::kScriptBadge, |
| 190 new ScriptBadgeHandler); |
| 191 ExtensionFunctionRegistry* registry = |
| 192 ExtensionFunctionRegistry::GetInstance(); |
| 193 registry->RegisterFunction<ScriptBadgeGetAttentionFunction>(); |
| 194 registry->RegisterFunction<ScriptBadgeGetPopupFunction>(); |
| 195 registry->RegisterFunction<ScriptBadgeSetPopupFunction>(); |
| 196 } |
| 197 |
| 198 ExtensionActionAPI::~ExtensionActionAPI() { |
| 199 } |
| 200 |
| 201 // static |
| 202 ProfileKeyedAPIFactory<ExtensionActionAPI>* |
| 203 ExtensionActionAPI::GetFactoryInstance() { |
| 204 return &g_factory.Get(); |
| 205 } |
| 173 | 206 |
| 174 // | 207 // |
| 175 // ExtensionActionStorageManager | 208 // ExtensionActionStorageManager |
| 176 // | 209 // |
| 177 | 210 |
| 178 ExtensionActionStorageManager::ExtensionActionStorageManager(Profile* profile) | 211 ExtensionActionStorageManager::ExtensionActionStorageManager(Profile* profile) |
| 179 : profile_(profile) { | 212 : profile_(profile) { |
| 180 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 213 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 181 content::Source<Profile>(profile_)); | 214 content::Source<Profile>(profile_)); |
| 182 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 215 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 183 content::NotificationService::AllBrowserContextsAndSources()); | 216 content::NotificationService::AllBrowserContextsAndSources()); |
| 184 | 217 |
| 185 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 218 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); |
| 186 if (storage) | 219 if (storage) |
| 187 storage->RegisterKey(kBrowserActionStorageKey); | 220 storage->RegisterKey(kBrowserActionStorageKey); |
| 188 } | 221 } |
| 189 | 222 |
| 190 ExtensionActionStorageManager::~ExtensionActionStorageManager() { | 223 ExtensionActionStorageManager::~ExtensionActionStorageManager() { |
| 191 } | 224 } |
| 192 | 225 |
| 193 void ExtensionActionStorageManager::Observe( | 226 void ExtensionActionStorageManager::Observe( |
| 194 int type, | 227 int type, |
| 195 const content::NotificationSource& source, | 228 const content::NotificationSource& source, |
| 196 const content::NotificationDetails& details) { | 229 const content::NotificationDetails& details) { |
| 197 switch (type) { | 230 switch (type) { |
| 198 case chrome::NOTIFICATION_EXTENSION_LOADED: { | 231 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 199 const Extension* extension = | 232 const Extension* extension = |
| 200 content::Details<const Extension>(details).ptr(); | 233 content::Details<const Extension>(details).ptr(); |
| 201 if (!extensions::ExtensionActionManager::Get(profile_)-> | 234 if (!ExtensionActionManager::Get(profile_)-> |
| 202 GetBrowserAction(*extension)) { | 235 GetBrowserAction(*extension)) { |
| 203 break; | 236 break; |
| 204 } | 237 } |
| 205 | 238 |
| 206 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); | 239 StateStore* storage = ExtensionSystem::Get(profile_)->state_store(); |
| 207 if (storage) { | 240 if (storage) { |
| 208 storage->GetExtensionValue(extension->id(), kBrowserActionStorageKey, | 241 storage->GetExtensionValue(extension->id(), kBrowserActionStorageKey, |
| 209 base::Bind(&ExtensionActionStorageManager::ReadFromStorage, | 242 base::Bind(&ExtensionActionStorageManager::ReadFromStorage, |
| 210 AsWeakPtr(), extension->id())); | 243 AsWeakPtr(), extension->id())); |
| 211 } | 244 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 if (browser_action->has_changed()) | 293 if (browser_action->has_changed()) |
| 261 return; | 294 return; |
| 262 | 295 |
| 263 const base::DictionaryValue* dict = NULL; | 296 const base::DictionaryValue* dict = NULL; |
| 264 if (!value.get() || !value->GetAsDictionary(&dict)) | 297 if (!value.get() || !value->GetAsDictionary(&dict)) |
| 265 return; | 298 return; |
| 266 | 299 |
| 267 SetDefaultsFromValue(dict, browser_action); | 300 SetDefaultsFromValue(dict, browser_action); |
| 268 } | 301 } |
| 269 | 302 |
| 270 } // namespace extensions | |
| 271 | |
| 272 | |
| 273 // | 303 // |
| 274 // ExtensionActionFunction | 304 // ExtensionActionFunction |
| 275 // | 305 // |
| 276 | 306 |
| 277 ExtensionActionFunction::ExtensionActionFunction() | 307 ExtensionActionFunction::ExtensionActionFunction() |
| 278 : details_(NULL), | 308 : details_(NULL), |
| 279 tab_id_(ExtensionAction::kDefaultTabId), | 309 tab_id_(ExtensionAction::kDefaultTabId), |
| 280 contents_(NULL), | 310 contents_(NULL), |
| 281 extension_action_(NULL) { | 311 extension_action_(NULL) { |
| 282 } | 312 } |
| 283 | 313 |
| 284 ExtensionActionFunction::~ExtensionActionFunction() { | 314 ExtensionActionFunction::~ExtensionActionFunction() { |
| 285 } | 315 } |
| 286 | 316 |
| 287 bool ExtensionActionFunction::RunImpl() { | 317 bool ExtensionActionFunction::RunImpl() { |
| 288 extensions::ExtensionActionManager* manager = | 318 ExtensionActionManager* manager = ExtensionActionManager::Get(profile_); |
| 289 extensions::ExtensionActionManager::Get(profile_); | 319 const Extension* extension = GetExtension(); |
| 290 const extensions::Extension* extension = GetExtension(); | |
| 291 if (StartsWithASCII(name(), "scriptBadge.", false)) { | 320 if (StartsWithASCII(name(), "scriptBadge.", false)) { |
| 292 extension_action_ = manager->GetScriptBadge(*extension); | 321 extension_action_ = manager->GetScriptBadge(*extension); |
| 293 } else if (StartsWithASCII(name(), "systemIndicator.", false)) { | 322 } else if (StartsWithASCII(name(), "systemIndicator.", false)) { |
| 294 extension_action_ = manager->GetSystemIndicator(*extension); | 323 extension_action_ = manager->GetSystemIndicator(*extension); |
| 295 } else { | 324 } else { |
| 296 extension_action_ = manager->GetBrowserAction(*extension); | 325 extension_action_ = manager->GetBrowserAction(*extension); |
| 297 if (!extension_action_) { | 326 if (!extension_action_) { |
| 298 extension_action_ = manager->GetPageAction(*extension); | 327 extension_action_ = manager->GetPageAction(*extension); |
| 299 } | 328 } |
| 300 } | 329 } |
| 301 if (!extension_action_) { | 330 if (!extension_action_) { |
| 302 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event | 331 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event |
| 303 // exist for extensions that don't have one declared. This should come as | 332 // exist for extensions that don't have one declared. This should come as |
| 304 // part of the Feature system. | 333 // part of the Feature system. |
| 305 error_ = kNoExtensionActionError; | 334 error_ = kNoExtensionActionError; |
| 306 return false; | 335 return false; |
| 307 } | 336 } |
| 308 | 337 |
| 309 // Populates the tab_id_ and details_ members. | 338 // Populates the tab_id_ and details_ members. |
| 310 EXTENSION_FUNCTION_VALIDATE(ExtractDataFromArguments()); | 339 EXTENSION_FUNCTION_VALIDATE(ExtractDataFromArguments()); |
| 311 | 340 |
| 312 // Find the WebContents that contains this tab id if one is required. | 341 // Find the WebContents that contains this tab id if one is required. |
| 313 if (tab_id_ != ExtensionAction::kDefaultTabId) { | 342 if (tab_id_ != ExtensionAction::kDefaultTabId) { |
| 314 ExtensionTabUtil::GetTabById( | 343 ExtensionTabUtil::GetTabById( |
| 315 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL); | 344 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL); |
| 316 if (!contents_) { | 345 if (!contents_) { |
| 317 error_ = extensions::ErrorUtils::FormatErrorMessage( | 346 error_ = ErrorUtils::FormatErrorMessage( |
| 318 kNoTabError, base::IntToString(tab_id_)); | 347 kNoTabError, base::IntToString(tab_id_)); |
| 319 return false; | 348 return false; |
| 320 } | 349 } |
| 321 } else { | 350 } else { |
| 322 // Only browser actions and system indicators have a default tabId. | 351 // Only browser actions and system indicators have a default tabId. |
| 323 extensions::ActionInfo::Type action_type = extension_action_->action_type(); | 352 ActionInfo::Type action_type = extension_action_->action_type(); |
| 324 EXTENSION_FUNCTION_VALIDATE( | 353 EXTENSION_FUNCTION_VALIDATE( |
| 325 action_type == extensions::ActionInfo::TYPE_BROWSER || | 354 action_type == ActionInfo::TYPE_BROWSER || |
| 326 action_type == extensions::ActionInfo::TYPE_SYSTEM_INDICATOR); | 355 action_type == ActionInfo::TYPE_SYSTEM_INDICATOR); |
| 327 } | 356 } |
| 328 return RunExtensionAction(); | 357 return RunExtensionAction(); |
| 329 } | 358 } |
| 330 | 359 |
| 331 bool ExtensionActionFunction::ExtractDataFromArguments() { | 360 bool ExtensionActionFunction::ExtractDataFromArguments() { |
| 332 // There may or may not be details (depends on the function). | 361 // There may or may not be details (depends on the function). |
| 333 // The tabId might appear in details (if it exists), as the first | 362 // The tabId might appear in details (if it exists), as the first |
| 334 // argument besides the action type (depends on the function), or be omitted | 363 // argument besides the action type (depends on the function), or be omitted |
| 335 // entirely. | 364 // entirely. |
| 336 base::Value* first_arg = NULL; | 365 base::Value* first_arg = NULL; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 | 399 |
| 371 default: | 400 default: |
| 372 return false; | 401 return false; |
| 373 } | 402 } |
| 374 | 403 |
| 375 return true; | 404 return true; |
| 376 } | 405 } |
| 377 | 406 |
| 378 void ExtensionActionFunction::NotifyChange() { | 407 void ExtensionActionFunction::NotifyChange() { |
| 379 switch (extension_action_->action_type()) { | 408 switch (extension_action_->action_type()) { |
| 380 case extensions::ActionInfo::TYPE_BROWSER: | 409 case ActionInfo::TYPE_BROWSER: |
| 381 case extensions::ActionInfo::TYPE_PAGE: | 410 case ActionInfo::TYPE_PAGE: |
| 382 if (extensions::ExtensionActionManager::Get(profile_)-> | 411 if (ExtensionActionManager::Get(profile_)-> |
| 383 GetBrowserAction(*extension_)) { | 412 GetBrowserAction(*extension_)) { |
| 384 NotifyBrowserActionChange(); | 413 NotifyBrowserActionChange(); |
| 385 } else if (extensions::ExtensionActionManager::Get(profile_)-> | 414 } else if (ExtensionActionManager::Get(profile_)-> |
| 386 GetPageAction(*extension_)) { | 415 GetPageAction(*extension_)) { |
| 387 NotifyLocationBarChange(); | 416 NotifyLocationBarChange(); |
| 388 } | 417 } |
| 389 return; | 418 return; |
| 390 case extensions::ActionInfo::TYPE_SCRIPT_BADGE: | 419 case ActionInfo::TYPE_SCRIPT_BADGE: |
| 391 NotifyLocationBarChange(); | 420 NotifyLocationBarChange(); |
| 392 return; | 421 return; |
| 393 case extensions::ActionInfo::TYPE_SYSTEM_INDICATOR: | 422 case ActionInfo::TYPE_SYSTEM_INDICATOR: |
| 394 NotifySystemIndicatorChange(); | 423 NotifySystemIndicatorChange(); |
| 395 return; | 424 return; |
| 396 } | 425 } |
| 397 NOTREACHED(); | 426 NOTREACHED(); |
| 398 } | 427 } |
| 399 | 428 |
| 400 void ExtensionActionFunction::NotifyBrowserActionChange() { | 429 void ExtensionActionFunction::NotifyBrowserActionChange() { |
| 401 content::NotificationService::current()->Notify( | 430 content::NotificationService::current()->Notify( |
| 402 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 431 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 403 content::Source<ExtensionAction>(extension_action_), | 432 content::Source<ExtensionAction>(extension_action_), |
| 404 content::Details<Profile>(profile())); | 433 content::Details<Profile>(profile())); |
| 405 } | 434 } |
| 406 | 435 |
| 407 void ExtensionActionFunction::NotifyLocationBarChange() { | 436 void ExtensionActionFunction::NotifyLocationBarChange() { |
| 408 extensions::TabHelper::FromWebContents(contents_)-> | 437 TabHelper::FromWebContents(contents_)-> |
| 409 location_bar_controller()->NotifyChange(); | 438 location_bar_controller()->NotifyChange(); |
| 410 } | 439 } |
| 411 | 440 |
| 412 void ExtensionActionFunction::NotifySystemIndicatorChange() { | 441 void ExtensionActionFunction::NotifySystemIndicatorChange() { |
| 413 content::NotificationService::current()->Notify( | 442 content::NotificationService::current()->Notify( |
| 414 chrome::NOTIFICATION_EXTENSION_SYSTEM_INDICATOR_UPDATED, | 443 chrome::NOTIFICATION_EXTENSION_SYSTEM_INDICATOR_UPDATED, |
| 415 content::Source<Profile>(profile()), | 444 content::Source<Profile>(profile()), |
| 416 content::Details<ExtensionAction>(extension_action_)); | 445 content::Details<ExtensionAction>(extension_action_)); |
| 417 } | 446 } |
| 418 | 447 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 | 484 |
| 456 bool ExtensionActionFunction::SetVisible(bool visible) { | 485 bool ExtensionActionFunction::SetVisible(bool visible) { |
| 457 if (extension_action_->GetIsVisible(tab_id_) == visible) | 486 if (extension_action_->GetIsVisible(tab_id_) == visible) |
| 458 return true; | 487 return true; |
| 459 extension_action_->SetAppearance( | 488 extension_action_->SetAppearance( |
| 460 tab_id_, visible ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); | 489 tab_id_, visible ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); |
| 461 NotifyChange(); | 490 NotifyChange(); |
| 462 return true; | 491 return true; |
| 463 } | 492 } |
| 464 | 493 |
| 465 extensions::TabHelper& ExtensionActionFunction::tab_helper() const { | 494 TabHelper& ExtensionActionFunction::tab_helper() const { |
| 466 CHECK(contents_); | 495 CHECK(contents_); |
| 467 return *extensions::TabHelper::FromWebContents(contents_); | 496 return *TabHelper::FromWebContents(contents_); |
| 468 } | 497 } |
| 469 | 498 |
| 470 bool ExtensionActionShowFunction::RunExtensionAction() { | 499 bool ExtensionActionShowFunction::RunExtensionAction() { |
| 471 return SetVisible(true); | 500 return SetVisible(true); |
| 472 } | 501 } |
| 473 | 502 |
| 474 bool ExtensionActionHideFunction::RunExtensionAction() { | 503 bool ExtensionActionHideFunction::RunExtensionAction() { |
| 475 return SetVisible(false); | 504 return SetVisible(false); |
| 476 } | 505 } |
| 477 | 506 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | 611 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { |
| 583 ListValue* list = new ListValue(); | 612 ListValue* list = new ListValue(); |
| 584 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); | 613 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); |
| 585 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); | 614 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); |
| 586 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); | 615 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); |
| 587 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); | 616 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); |
| 588 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); | 617 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); |
| 589 SetResult(list); | 618 SetResult(list); |
| 590 return true; | 619 return true; |
| 591 } | 620 } |
| 621 |
| 622 // |
| 623 // ScriptBadgeGetAttentionFunction |
| 624 // |
| 625 |
| 626 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} |
| 627 |
| 628 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { |
| 629 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); |
| 630 return true; |
| 631 } |
| 632 |
| 633 } // namespace extensions |
| 634 |
| 635 // |
| 636 // PageActionsFunction (deprecated) |
| 637 // |
| 638 |
| 639 namespace keys = extension_page_actions_api_constants; |
| 640 |
| 641 PageActionsFunction::PageActionsFunction() { |
| 642 } |
| 643 |
| 644 PageActionsFunction::~PageActionsFunction() { |
| 645 } |
| 646 |
| 647 bool PageActionsFunction::SetPageActionEnabled(bool enable) { |
| 648 std::string extension_action_id; |
| 649 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); |
| 650 DictionaryValue* action = NULL; |
| 651 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); |
| 652 |
| 653 int tab_id; |
| 654 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 655 std::string url; |
| 656 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| 657 |
| 658 std::string title; |
| 659 if (enable) { |
| 660 if (action->HasKey(keys::kTitleKey)) |
| 661 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); |
| 662 } |
| 663 |
| 664 ExtensionAction* page_action = |
| 665 extensions::ExtensionActionManager::Get(profile())-> |
| 666 GetPageAction(*GetExtension()); |
| 667 if (!page_action) { |
| 668 error_ = extensions::kNoPageActionError; |
| 669 return false; |
| 670 } |
| 671 |
| 672 // Find the WebContents that contains this tab id. |
| 673 content::WebContents* contents = NULL; |
| 674 bool result = ExtensionTabUtil::GetTabById( |
| 675 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); |
| 676 if (!result || !contents) { |
| 677 error_ = extensions::ErrorUtils::FormatErrorMessage( |
| 678 extensions::kNoTabError, base::IntToString(tab_id)); |
| 679 return false; |
| 680 } |
| 681 |
| 682 // Make sure the URL hasn't changed. |
| 683 content::NavigationEntry* entry = contents->GetController().GetActiveEntry(); |
| 684 if (!entry || url != entry->GetURL().spec()) { |
| 685 error_ = extensions::ErrorUtils::FormatErrorMessage( |
| 686 extensions::kUrlNotActiveError, url); |
| 687 return false; |
| 688 } |
| 689 |
| 690 // Set visibility and broadcast notifications that the UI should be updated. |
| 691 page_action->SetAppearance( |
| 692 tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); |
| 693 page_action->SetTitle(tab_id, title); |
| 694 extensions::TabHelper::FromWebContents(contents)-> |
| 695 location_bar_controller()->NotifyChange(); |
| 696 |
| 697 return true; |
| 698 } |
| 699 |
| 700 bool EnablePageActionsFunction::RunImpl() { |
| 701 return SetPageActionEnabled(true); |
| 702 } |
| 703 |
| 704 bool DisablePageActionsFunction::RunImpl() { |
| 705 return SetPageActionEnabled(false); |
| 706 } |
| OLD | NEW |