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_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 bool 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 true; |
| 427 extension_action_->SetIsVisible(tab_id_, visible); | 420 extension_action_->SetIsVisible(tab_id_, visible); |
| 428 NotifyChange(); | 421 NotifyChange(); |
| 429 return true; | 422 return true; |
| 430 } | 423 } |
| 431 | 424 |
| 432 bool ExtensionActionShowFunction::RunExtensionAction() { | 425 ExtensionFunction::ResponseAction |
| 433 return SetVisible(true); | 426 ExtensionActionShowFunction::RunExtensionAction() { |
| 427 SetVisible(true); | |
|
lazyboy
2016/10/07 23:32:51
maybe change SetVisible to return void?
Devlin
2016/10/10 16:29:01
Good call, done.
| |
| 428 return RespondNow(NoArguments()); | |
| 434 } | 429 } |
| 435 | 430 |
| 436 bool ExtensionActionHideFunction::RunExtensionAction() { | 431 ExtensionFunction::ResponseAction |
| 437 return SetVisible(false); | 432 ExtensionActionHideFunction::RunExtensionAction() { |
| 433 SetVisible(false); | |
| 434 return RespondNow(NoArguments()); | |
| 438 } | 435 } |
| 439 | 436 |
| 440 bool ExtensionActionSetIconFunction::RunExtensionAction() { | 437 ExtensionFunction::ResponseAction |
| 438 ExtensionActionSetIconFunction::RunExtensionAction() { | |
| 441 EXTENSION_FUNCTION_VALIDATE(details_); | 439 EXTENSION_FUNCTION_VALIDATE(details_); |
| 442 | 440 |
| 443 // setIcon can take a variant argument: either a dictionary of canvas | 441 // setIcon can take a variant argument: either a dictionary of canvas |
| 444 // ImageData, or an icon index. | 442 // ImageData, or an icon index. |
| 445 base::DictionaryValue* canvas_set = NULL; | 443 base::DictionaryValue* canvas_set = NULL; |
| 446 int icon_index; | 444 int icon_index; |
| 447 if (details_->GetDictionary("imageData", &canvas_set)) { | 445 if (details_->GetDictionary("imageData", &canvas_set)) { |
| 448 gfx::ImageSkia icon; | 446 gfx::ImageSkia icon; |
| 449 | 447 |
| 450 EXTENSION_FUNCTION_VALIDATE( | 448 EXTENSION_FUNCTION_VALIDATE( |
| 451 ExtensionAction::ParseIconFromCanvasDictionary(*canvas_set, &icon)); | 449 ExtensionAction::ParseIconFromCanvasDictionary(*canvas_set, &icon)); |
| 452 | 450 |
| 453 if (icon.isNull()) { | 451 if (icon.isNull()) |
| 454 error_ = "Icon invalid."; | 452 return RespondNow(Error("Icon invalid.")); |
| 455 return false; | |
| 456 } | |
| 457 | 453 |
| 458 extension_action_->SetIcon(tab_id_, gfx::Image(icon)); | 454 extension_action_->SetIcon(tab_id_, gfx::Image(icon)); |
| 459 } else if (details_->GetInteger("iconIndex", &icon_index)) { | 455 } else if (details_->GetInteger("iconIndex", &icon_index)) { |
| 460 // Obsolete argument: ignore it. | 456 // Obsolete argument: ignore it. |
| 461 return true; | 457 return RespondNow(NoArguments()); |
| 462 } else { | 458 } else { |
| 463 EXTENSION_FUNCTION_VALIDATE(false); | 459 EXTENSION_FUNCTION_VALIDATE(false); |
| 464 } | 460 } |
| 465 NotifyChange(); | 461 NotifyChange(); |
| 466 return true; | 462 return RespondNow(NoArguments()); |
| 467 } | 463 } |
| 468 | 464 |
| 469 bool ExtensionActionSetTitleFunction::RunExtensionAction() { | 465 ExtensionFunction::ResponseAction |
| 466 ExtensionActionSetTitleFunction::RunExtensionAction() { | |
| 470 EXTENSION_FUNCTION_VALIDATE(details_); | 467 EXTENSION_FUNCTION_VALIDATE(details_); |
| 471 std::string title; | 468 std::string title; |
| 472 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title)); | 469 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title)); |
| 473 extension_action_->SetTitle(tab_id_, title); | 470 extension_action_->SetTitle(tab_id_, title); |
| 474 NotifyChange(); | 471 NotifyChange(); |
| 475 return true; | 472 return RespondNow(NoArguments()); |
| 476 } | 473 } |
| 477 | 474 |
| 478 bool ExtensionActionSetPopupFunction::RunExtensionAction() { | 475 ExtensionFunction::ResponseAction |
| 476 ExtensionActionSetPopupFunction::RunExtensionAction() { | |
| 479 EXTENSION_FUNCTION_VALIDATE(details_); | 477 EXTENSION_FUNCTION_VALIDATE(details_); |
| 480 std::string popup_string; | 478 std::string popup_string; |
| 481 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string)); | 479 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string)); |
| 482 | 480 |
| 483 GURL popup_url; | 481 GURL popup_url; |
| 484 if (!popup_string.empty()) | 482 if (!popup_string.empty()) |
| 485 popup_url = extension()->GetResourceURL(popup_string); | 483 popup_url = extension()->GetResourceURL(popup_string); |
| 486 | 484 |
| 487 extension_action_->SetPopupUrl(tab_id_, popup_url); | 485 extension_action_->SetPopupUrl(tab_id_, popup_url); |
| 488 NotifyChange(); | 486 NotifyChange(); |
| 489 return true; | 487 return RespondNow(NoArguments()); |
| 490 } | 488 } |
| 491 | 489 |
| 492 bool ExtensionActionSetBadgeTextFunction::RunExtensionAction() { | 490 ExtensionFunction::ResponseAction |
| 491 ExtensionActionSetBadgeTextFunction::RunExtensionAction() { | |
| 493 EXTENSION_FUNCTION_VALIDATE(details_); | 492 EXTENSION_FUNCTION_VALIDATE(details_); |
| 494 std::string badge_text; | 493 std::string badge_text; |
| 495 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text)); | 494 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text)); |
| 496 extension_action_->SetBadgeText(tab_id_, badge_text); | 495 extension_action_->SetBadgeText(tab_id_, badge_text); |
| 497 NotifyChange(); | 496 NotifyChange(); |
| 498 return true; | 497 return RespondNow(NoArguments()); |
| 499 } | 498 } |
| 500 | 499 |
| 501 bool ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() { | 500 ExtensionFunction::ResponseAction |
| 501 ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() { | |
| 502 EXTENSION_FUNCTION_VALIDATE(details_); | 502 EXTENSION_FUNCTION_VALIDATE(details_); |
| 503 base::Value* color_value = NULL; | 503 base::Value* color_value = NULL; |
| 504 EXTENSION_FUNCTION_VALIDATE(details_->Get("color", &color_value)); | 504 EXTENSION_FUNCTION_VALIDATE(details_->Get("color", &color_value)); |
| 505 SkColor color = 0; | 505 SkColor color = 0; |
| 506 if (color_value->IsType(base::Value::TYPE_LIST)) { | 506 if (color_value->IsType(base::Value::TYPE_LIST)) { |
| 507 base::ListValue* list = NULL; | 507 base::ListValue* list = NULL; |
| 508 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list)); | 508 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list)); |
| 509 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); | 509 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); |
| 510 | 510 |
| 511 int color_array[4] = {0}; | 511 int color_array[4] = {0}; |
| 512 for (size_t i = 0; i < arraysize(color_array); ++i) { | 512 for (size_t i = 0; i < arraysize(color_array); ++i) { |
| 513 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); | 513 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); |
| 514 } | 514 } |
| 515 | 515 |
| 516 color = SkColorSetARGB(color_array[3], color_array[0], | 516 color = SkColorSetARGB(color_array[3], color_array[0], |
| 517 color_array[1], color_array[2]); | 517 color_array[1], color_array[2]); |
| 518 } else if (color_value->IsType(base::Value::TYPE_STRING)) { | 518 } else if (color_value->IsType(base::Value::TYPE_STRING)) { |
| 519 std::string color_string; | 519 std::string color_string; |
| 520 EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string)); | 520 EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string)); |
| 521 if (!image_util::ParseCssColorString(color_string, &color)) { | 521 if (!image_util::ParseCssColorString(color_string, &color)) |
| 522 error_ = kInvalidColorError; | 522 return RespondNow(Error(kInvalidColorError)); |
| 523 return false; | |
| 524 } | |
| 525 } | 523 } |
| 526 | 524 |
| 527 extension_action_->SetBadgeBackgroundColor(tab_id_, color); | 525 extension_action_->SetBadgeBackgroundColor(tab_id_, color); |
| 528 NotifyChange(); | 526 NotifyChange(); |
| 529 return true; | 527 return RespondNow(NoArguments()); |
| 530 } | 528 } |
| 531 | 529 |
| 532 bool ExtensionActionGetTitleFunction::RunExtensionAction() { | 530 ExtensionFunction::ResponseAction |
| 533 SetResult(base::MakeUnique<base::StringValue>( | 531 ExtensionActionGetTitleFunction::RunExtensionAction() { |
| 534 extension_action_->GetTitle(tab_id_))); | 532 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>( |
| 535 return true; | 533 extension_action_->GetTitle(tab_id_)))); |
| 536 } | 534 } |
| 537 | 535 |
| 538 bool ExtensionActionGetPopupFunction::RunExtensionAction() { | 536 ExtensionFunction::ResponseAction |
| 539 SetResult(base::MakeUnique<base::StringValue>( | 537 ExtensionActionGetPopupFunction::RunExtensionAction() { |
| 540 extension_action_->GetPopupUrl(tab_id_).spec())); | 538 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>( |
| 541 return true; | 539 extension_action_->GetPopupUrl(tab_id_).spec()))); |
| 542 } | 540 } |
| 543 | 541 |
| 544 bool ExtensionActionGetBadgeTextFunction::RunExtensionAction() { | 542 ExtensionFunction::ResponseAction |
| 545 SetResult(base::MakeUnique<base::StringValue>( | 543 ExtensionActionGetBadgeTextFunction::RunExtensionAction() { |
| 546 extension_action_->GetBadgeText(tab_id_))); | 544 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>( |
| 547 return true; | 545 extension_action_->GetBadgeText(tab_id_)))); |
| 548 } | 546 } |
| 549 | 547 |
| 550 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | 548 ExtensionFunction::ResponseAction |
| 549 ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | |
| 551 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 550 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
| 552 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); | 551 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); |
| 553 list->AppendInteger(static_cast<int>(SkColorGetR(color))); | 552 list->AppendInteger(static_cast<int>(SkColorGetR(color))); |
| 554 list->AppendInteger(static_cast<int>(SkColorGetG(color))); | 553 list->AppendInteger(static_cast<int>(SkColorGetG(color))); |
| 555 list->AppendInteger(static_cast<int>(SkColorGetB(color))); | 554 list->AppendInteger(static_cast<int>(SkColorGetB(color))); |
| 556 list->AppendInteger(static_cast<int>(SkColorGetA(color))); | 555 list->AppendInteger(static_cast<int>(SkColorGetA(color))); |
| 557 SetResult(std::move(list)); | 556 return RespondNow(OneArgument(std::move(list))); |
| 558 return true; | |
| 559 } | 557 } |
| 560 | 558 |
| 561 BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction() | 559 BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction() |
| 562 : response_sent_(false) { | 560 : response_sent_(false) { |
| 563 } | 561 } |
| 564 | 562 |
| 565 bool BrowserActionOpenPopupFunction::RunAsync() { | 563 bool BrowserActionOpenPopupFunction::RunAsync() { |
| 566 // We only allow the popup in the active window. | 564 // We only allow the popup in the active window. |
| 567 Profile* profile = GetProfile(); | 565 Profile* profile = GetProfile(); |
| 568 Browser* browser = chrome::FindLastActiveWithProfile(profile); | 566 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 || | 628 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP || |
| 631 host->extension()->id() != extension_->id()) | 629 host->extension()->id() != extension_->id()) |
| 632 return; | 630 return; |
| 633 | 631 |
| 634 SendResponse(true); | 632 SendResponse(true); |
| 635 response_sent_ = true; | 633 response_sent_ = true; |
| 636 registrar_.RemoveAll(); | 634 registrar_.RemoveAll(); |
| 637 } | 635 } |
| 638 | 636 |
| 639 } // namespace extensions | 637 } // namespace extensions |
| OLD | NEW |