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