| 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_action_api.h" | 5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 ExtensionActionFunction::ExtensionActionFunction() | 316 ExtensionActionFunction::ExtensionActionFunction() |
| 317 : details_(NULL), | 317 : details_(NULL), |
| 318 tab_id_(ExtensionAction::kDefaultTabId), | 318 tab_id_(ExtensionAction::kDefaultTabId), |
| 319 contents_(NULL), | 319 contents_(NULL), |
| 320 extension_action_(NULL) { | 320 extension_action_(NULL) { |
| 321 } | 321 } |
| 322 | 322 |
| 323 ExtensionActionFunction::~ExtensionActionFunction() { | 323 ExtensionActionFunction::~ExtensionActionFunction() { |
| 324 } | 324 } |
| 325 | 325 |
| 326 bool ExtensionActionFunction::RunSync() { | 326 ExtensionFunction::ResponseAction ExtensionActionFunction::Run() { |
| 327 ExtensionActionManager* manager = ExtensionActionManager::Get(GetProfile()); | 327 ExtensionActionManager* manager = |
| 328 ExtensionActionManager::Get(browser_context()); |
| 328 if (base::StartsWith(name(), "systemIndicator.", | 329 if (base::StartsWith(name(), "systemIndicator.", |
| 329 base::CompareCase::INSENSITIVE_ASCII)) { | 330 base::CompareCase::INSENSITIVE_ASCII)) { |
| 330 extension_action_ = manager->GetSystemIndicator(*extension()); | 331 extension_action_ = manager->GetSystemIndicator(*extension()); |
| 331 } else { | 332 } else { |
| 332 extension_action_ = manager->GetBrowserAction(*extension()); | 333 extension_action_ = manager->GetBrowserAction(*extension()); |
| 333 if (!extension_action_) { | 334 if (!extension_action_) { |
| 334 extension_action_ = manager->GetPageAction(*extension()); | 335 extension_action_ = manager->GetPageAction(*extension()); |
| 335 } | 336 } |
| 336 } | 337 } |
| 337 if (!extension_action_) { | 338 if (!extension_action_) { |
| 338 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event | 339 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event |
| 339 // exist for extensions that don't have one declared. This should come as | 340 // exist for extensions that don't have one declared. This should come as |
| 340 // part of the Feature system. | 341 // part of the Feature system. |
| 341 error_ = kNoExtensionActionError; | 342 return RespondNow(Error(kNoExtensionActionError)); |
| 342 return false; | |
| 343 } | 343 } |
| 344 | 344 |
| 345 // Populates the tab_id_ and details_ members. | 345 // Populates the tab_id_ and details_ members. |
| 346 EXTENSION_FUNCTION_VALIDATE(ExtractDataFromArguments()); | 346 EXTENSION_FUNCTION_VALIDATE(ExtractDataFromArguments()); |
| 347 | 347 |
| 348 // Find the WebContents that contains this tab id if one is required. | 348 // Find the WebContents that contains this tab id if one is required. |
| 349 if (tab_id_ != ExtensionAction::kDefaultTabId) { | 349 if (tab_id_ != ExtensionAction::kDefaultTabId) { |
| 350 ExtensionTabUtil::GetTabById(tab_id_, | 350 ExtensionTabUtil::GetTabById(tab_id_, browser_context(), |
| 351 GetProfile(), | 351 include_incognito(), nullptr, nullptr, |
| 352 include_incognito(), | 352 &contents_, nullptr); |
| 353 NULL, | 353 if (!contents_) |
| 354 NULL, | 354 return RespondNow(Error(kNoTabError, base::IntToString(tab_id_))); |
| 355 &contents_, | |
| 356 NULL); | |
| 357 if (!contents_) { | |
| 358 error_ = ErrorUtils::FormatErrorMessage( | |
| 359 kNoTabError, base::IntToString(tab_id_)); | |
| 360 return false; | |
| 361 } | |
| 362 } else { | 355 } else { |
| 363 // Only browser actions and system indicators have a default tabId. | 356 // Only browser actions and system indicators have a default tabId. |
| 364 ActionInfo::Type action_type = extension_action_->action_type(); | 357 ActionInfo::Type action_type = extension_action_->action_type(); |
| 365 EXTENSION_FUNCTION_VALIDATE( | 358 EXTENSION_FUNCTION_VALIDATE( |
| 366 action_type == ActionInfo::TYPE_BROWSER || | 359 action_type == ActionInfo::TYPE_BROWSER || |
| 367 action_type == ActionInfo::TYPE_SYSTEM_INDICATOR); | 360 action_type == ActionInfo::TYPE_SYSTEM_INDICATOR); |
| 368 } | 361 } |
| 369 return RunExtensionAction(); | 362 return RunExtensionAction(); |
| 370 } | 363 } |
| 371 | 364 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 break; | 403 break; |
| 411 | 404 |
| 412 default: | 405 default: |
| 413 return false; | 406 return false; |
| 414 } | 407 } |
| 415 | 408 |
| 416 return true; | 409 return true; |
| 417 } | 410 } |
| 418 | 411 |
| 419 void ExtensionActionFunction::NotifyChange() { | 412 void ExtensionActionFunction::NotifyChange() { |
| 420 ExtensionActionAPI::Get(GetProfile())->NotifyChange( | 413 ExtensionActionAPI::Get(browser_context()) |
| 421 extension_action_, contents_, GetProfile()); | 414 ->NotifyChange(extension_action_, contents_, browser_context()); |
| 422 } | 415 } |
| 423 | 416 |
| 424 bool ExtensionActionFunction::SetVisible(bool visible) { | 417 void ExtensionActionFunction::SetVisible(bool visible) { |
| 425 if (extension_action_->GetIsVisible(tab_id_) == visible) | 418 if (extension_action_->GetIsVisible(tab_id_) == visible) |
| 426 return true; | 419 return; |
| 427 extension_action_->SetIsVisible(tab_id_, visible); | 420 extension_action_->SetIsVisible(tab_id_, visible); |
| 428 NotifyChange(); | 421 NotifyChange(); |
| 429 return true; | |
| 430 } | 422 } |
| 431 | 423 |
| 432 bool ExtensionActionShowFunction::RunExtensionAction() { | 424 ExtensionFunction::ResponseAction |
| 433 return SetVisible(true); | 425 ExtensionActionShowFunction::RunExtensionAction() { |
| 426 SetVisible(true); |
| 427 return RespondNow(NoArguments()); |
| 434 } | 428 } |
| 435 | 429 |
| 436 bool ExtensionActionHideFunction::RunExtensionAction() { | 430 ExtensionFunction::ResponseAction |
| 437 return SetVisible(false); | 431 ExtensionActionHideFunction::RunExtensionAction() { |
| 432 SetVisible(false); |
| 433 return RespondNow(NoArguments()); |
| 438 } | 434 } |
| 439 | 435 |
| 440 bool ExtensionActionSetIconFunction::RunExtensionAction() { | 436 ExtensionFunction::ResponseAction |
| 437 ExtensionActionSetIconFunction::RunExtensionAction() { |
| 441 EXTENSION_FUNCTION_VALIDATE(details_); | 438 EXTENSION_FUNCTION_VALIDATE(details_); |
| 442 | 439 |
| 443 // setIcon can take a variant argument: either a dictionary of canvas | 440 // setIcon can take a variant argument: either a dictionary of canvas |
| 444 // ImageData, or an icon index. | 441 // ImageData, or an icon index. |
| 445 base::DictionaryValue* canvas_set = NULL; | 442 base::DictionaryValue* canvas_set = NULL; |
| 446 int icon_index; | 443 int icon_index; |
| 447 if (details_->GetDictionary("imageData", &canvas_set)) { | 444 if (details_->GetDictionary("imageData", &canvas_set)) { |
| 448 gfx::ImageSkia icon; | 445 gfx::ImageSkia icon; |
| 449 | 446 |
| 450 EXTENSION_FUNCTION_VALIDATE( | 447 EXTENSION_FUNCTION_VALIDATE( |
| 451 ExtensionAction::ParseIconFromCanvasDictionary(*canvas_set, &icon)); | 448 ExtensionAction::ParseIconFromCanvasDictionary(*canvas_set, &icon)); |
| 452 | 449 |
| 453 if (icon.isNull()) { | 450 if (icon.isNull()) |
| 454 error_ = "Icon invalid."; | 451 return RespondNow(Error("Icon invalid.")); |
| 455 return false; | |
| 456 } | |
| 457 | 452 |
| 458 extension_action_->SetIcon(tab_id_, gfx::Image(icon)); | 453 extension_action_->SetIcon(tab_id_, gfx::Image(icon)); |
| 459 } else if (details_->GetInteger("iconIndex", &icon_index)) { | 454 } else if (details_->GetInteger("iconIndex", &icon_index)) { |
| 460 // Obsolete argument: ignore it. | 455 // Obsolete argument: ignore it. |
| 461 return true; | 456 return RespondNow(NoArguments()); |
| 462 } else { | 457 } else { |
| 463 EXTENSION_FUNCTION_VALIDATE(false); | 458 EXTENSION_FUNCTION_VALIDATE(false); |
| 464 } | 459 } |
| 465 NotifyChange(); | 460 NotifyChange(); |
| 466 return true; | 461 return RespondNow(NoArguments()); |
| 467 } | 462 } |
| 468 | 463 |
| 469 bool ExtensionActionSetTitleFunction::RunExtensionAction() { | 464 ExtensionFunction::ResponseAction |
| 465 ExtensionActionSetTitleFunction::RunExtensionAction() { |
| 470 EXTENSION_FUNCTION_VALIDATE(details_); | 466 EXTENSION_FUNCTION_VALIDATE(details_); |
| 471 std::string title; | 467 std::string title; |
| 472 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title)); | 468 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title)); |
| 473 extension_action_->SetTitle(tab_id_, title); | 469 extension_action_->SetTitle(tab_id_, title); |
| 474 NotifyChange(); | 470 NotifyChange(); |
| 475 return true; | 471 return RespondNow(NoArguments()); |
| 476 } | 472 } |
| 477 | 473 |
| 478 bool ExtensionActionSetPopupFunction::RunExtensionAction() { | 474 ExtensionFunction::ResponseAction |
| 475 ExtensionActionSetPopupFunction::RunExtensionAction() { |
| 479 EXTENSION_FUNCTION_VALIDATE(details_); | 476 EXTENSION_FUNCTION_VALIDATE(details_); |
| 480 std::string popup_string; | 477 std::string popup_string; |
| 481 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string)); | 478 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string)); |
| 482 | 479 |
| 483 GURL popup_url; | 480 GURL popup_url; |
| 484 if (!popup_string.empty()) | 481 if (!popup_string.empty()) |
| 485 popup_url = extension()->GetResourceURL(popup_string); | 482 popup_url = extension()->GetResourceURL(popup_string); |
| 486 | 483 |
| 487 extension_action_->SetPopupUrl(tab_id_, popup_url); | 484 extension_action_->SetPopupUrl(tab_id_, popup_url); |
| 488 NotifyChange(); | 485 NotifyChange(); |
| 489 return true; | 486 return RespondNow(NoArguments()); |
| 490 } | 487 } |
| 491 | 488 |
| 492 bool ExtensionActionSetBadgeTextFunction::RunExtensionAction() { | 489 ExtensionFunction::ResponseAction |
| 490 ExtensionActionSetBadgeTextFunction::RunExtensionAction() { |
| 493 EXTENSION_FUNCTION_VALIDATE(details_); | 491 EXTENSION_FUNCTION_VALIDATE(details_); |
| 494 std::string badge_text; | 492 std::string badge_text; |
| 495 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text)); | 493 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text)); |
| 496 extension_action_->SetBadgeText(tab_id_, badge_text); | 494 extension_action_->SetBadgeText(tab_id_, badge_text); |
| 497 NotifyChange(); | 495 NotifyChange(); |
| 498 return true; | 496 return RespondNow(NoArguments()); |
| 499 } | 497 } |
| 500 | 498 |
| 501 bool ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() { | 499 ExtensionFunction::ResponseAction |
| 500 ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() { |
| 502 EXTENSION_FUNCTION_VALIDATE(details_); | 501 EXTENSION_FUNCTION_VALIDATE(details_); |
| 503 base::Value* color_value = NULL; | 502 base::Value* color_value = NULL; |
| 504 EXTENSION_FUNCTION_VALIDATE(details_->Get("color", &color_value)); | 503 EXTENSION_FUNCTION_VALIDATE(details_->Get("color", &color_value)); |
| 505 SkColor color = 0; | 504 SkColor color = 0; |
| 506 if (color_value->IsType(base::Value::TYPE_LIST)) { | 505 if (color_value->IsType(base::Value::TYPE_LIST)) { |
| 507 base::ListValue* list = NULL; | 506 base::ListValue* list = NULL; |
| 508 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list)); | 507 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list)); |
| 509 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); | 508 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); |
| 510 | 509 |
| 511 int color_array[4] = {0}; | 510 int color_array[4] = {0}; |
| 512 for (size_t i = 0; i < arraysize(color_array); ++i) { | 511 for (size_t i = 0; i < arraysize(color_array); ++i) { |
| 513 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); | 512 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); |
| 514 } | 513 } |
| 515 | 514 |
| 516 color = SkColorSetARGB(color_array[3], color_array[0], | 515 color = SkColorSetARGB(color_array[3], color_array[0], |
| 517 color_array[1], color_array[2]); | 516 color_array[1], color_array[2]); |
| 518 } else if (color_value->IsType(base::Value::TYPE_STRING)) { | 517 } else if (color_value->IsType(base::Value::TYPE_STRING)) { |
| 519 std::string color_string; | 518 std::string color_string; |
| 520 EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string)); | 519 EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string)); |
| 521 if (!image_util::ParseCssColorString(color_string, &color)) { | 520 if (!image_util::ParseCssColorString(color_string, &color)) |
| 522 error_ = kInvalidColorError; | 521 return RespondNow(Error(kInvalidColorError)); |
| 523 return false; | |
| 524 } | |
| 525 } | 522 } |
| 526 | 523 |
| 527 extension_action_->SetBadgeBackgroundColor(tab_id_, color); | 524 extension_action_->SetBadgeBackgroundColor(tab_id_, color); |
| 528 NotifyChange(); | 525 NotifyChange(); |
| 529 return true; | 526 return RespondNow(NoArguments()); |
| 530 } | 527 } |
| 531 | 528 |
| 532 bool ExtensionActionGetTitleFunction::RunExtensionAction() { | 529 ExtensionFunction::ResponseAction |
| 533 SetResult(base::MakeUnique<base::StringValue>( | 530 ExtensionActionGetTitleFunction::RunExtensionAction() { |
| 534 extension_action_->GetTitle(tab_id_))); | 531 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>( |
| 535 return true; | 532 extension_action_->GetTitle(tab_id_)))); |
| 536 } | 533 } |
| 537 | 534 |
| 538 bool ExtensionActionGetPopupFunction::RunExtensionAction() { | 535 ExtensionFunction::ResponseAction |
| 539 SetResult(base::MakeUnique<base::StringValue>( | 536 ExtensionActionGetPopupFunction::RunExtensionAction() { |
| 540 extension_action_->GetPopupUrl(tab_id_).spec())); | 537 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>( |
| 541 return true; | 538 extension_action_->GetPopupUrl(tab_id_).spec()))); |
| 542 } | 539 } |
| 543 | 540 |
| 544 bool ExtensionActionGetBadgeTextFunction::RunExtensionAction() { | 541 ExtensionFunction::ResponseAction |
| 545 SetResult(base::MakeUnique<base::StringValue>( | 542 ExtensionActionGetBadgeTextFunction::RunExtensionAction() { |
| 546 extension_action_->GetBadgeText(tab_id_))); | 543 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>( |
| 547 return true; | 544 extension_action_->GetBadgeText(tab_id_)))); |
| 548 } | 545 } |
| 549 | 546 |
| 550 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | 547 ExtensionFunction::ResponseAction |
| 548 ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { |
| 551 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 549 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
| 552 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); | 550 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); |
| 553 list->AppendInteger(static_cast<int>(SkColorGetR(color))); | 551 list->AppendInteger(static_cast<int>(SkColorGetR(color))); |
| 554 list->AppendInteger(static_cast<int>(SkColorGetG(color))); | 552 list->AppendInteger(static_cast<int>(SkColorGetG(color))); |
| 555 list->AppendInteger(static_cast<int>(SkColorGetB(color))); | 553 list->AppendInteger(static_cast<int>(SkColorGetB(color))); |
| 556 list->AppendInteger(static_cast<int>(SkColorGetA(color))); | 554 list->AppendInteger(static_cast<int>(SkColorGetA(color))); |
| 557 SetResult(std::move(list)); | 555 return RespondNow(OneArgument(std::move(list))); |
| 558 return true; | |
| 559 } | 556 } |
| 560 | 557 |
| 561 BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction() | 558 BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction() |
| 562 : response_sent_(false) { | 559 : response_sent_(false) { |
| 563 } | 560 } |
| 564 | 561 |
| 565 bool BrowserActionOpenPopupFunction::RunAsync() { | 562 bool BrowserActionOpenPopupFunction::RunAsync() { |
| 566 // We only allow the popup in the active window. | 563 // We only allow the popup in the active window. |
| 567 Profile* profile = GetProfile(); | 564 Profile* profile = GetProfile(); |
| 568 Browser* browser = chrome::FindLastActiveWithProfile(profile); | 565 Browser* browser = chrome::FindLastActiveWithProfile(profile); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP || | 627 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP || |
| 631 host->extension()->id() != extension_->id()) | 628 host->extension()->id() != extension_->id()) |
| 632 return; | 629 return; |
| 633 | 630 |
| 634 SendResponse(true); | 631 SendResponse(true); |
| 635 response_sent_ = true; | 632 response_sent_ = true; |
| 636 registrar_.RemoveAll(); | 633 registrar_.RemoveAll(); |
| 637 } | 634 } |
| 638 | 635 |
| 639 } // namespace extensions | 636 } // namespace extensions |
| OLD | NEW |