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 "extensions/browser/api/management/management_api.h" | 5 #include "extensions/browser/api/management/management_api.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 ExtensionRegistry::Get(browser_context()) | 311 ExtensionRegistry::Get(browser_context()) |
| 312 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); | 312 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); |
| 313 if (!extension) | 313 if (!extension) |
| 314 return RespondNow(Error(keys::kNoExtensionError, params->id)); | 314 return RespondNow(Error(keys::kNoExtensionError, params->id)); |
| 315 | 315 |
| 316 std::vector<std::string> warnings = CreateWarningsList(extension); | 316 std::vector<std::string> warnings = CreateWarningsList(extension); |
| 317 return RespondNow(ArgumentList( | 317 return RespondNow(ArgumentList( |
| 318 management::GetPermissionWarningsById::Results::Create(warnings))); | 318 management::GetPermissionWarningsById::Results::Create(warnings))); |
| 319 } | 319 } |
| 320 | 320 |
| 321 bool ManagementGetPermissionWarningsByManifestFunction::RunAsync() { | 321 ExtensionFunction::ResponseAction |
| 322 ManagementGetPermissionWarningsByManifestFunction::Run() { | |
| 322 std::unique_ptr<management::GetPermissionWarningsByManifest::Params> params( | 323 std::unique_ptr<management::GetPermissionWarningsByManifest::Params> params( |
| 323 management::GetPermissionWarningsByManifest::Params::Create(*args_)); | 324 management::GetPermissionWarningsByManifest::Params::Create(*args_)); |
| 324 EXTENSION_FUNCTION_VALIDATE(params.get()); | 325 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 325 | 326 |
| 326 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() | 327 const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() |
| 327 ->Get(browser_context()) | 328 ->Get(browser_context()) |
| 328 ->GetDelegate(); | 329 ->GetDelegate(); |
| 329 | 330 |
| 330 if (delegate) { | 331 if (delegate) { |
| 331 delegate->GetPermissionWarningsByManifestFunctionDelegate( | 332 delegate->GetPermissionWarningsByManifestFunctionDelegate( |
| 332 this, params->manifest_str); | 333 this, params->manifest_str); |
| 333 | 334 |
| 334 // Matched with a Release() in OnParseSuccess/Failure(). | 335 // Matched with a Release() in OnParseSuccess/Failure(). |
| 335 AddRef(); | 336 AddRef(); |
| 336 | 337 |
| 337 // Response is sent async in OnParseSuccess/Failure(). | 338 // Response is sent async in OnParseSuccess/Failure(). |
| 338 return true; | 339 return RespondLater(); |
| 339 } else { | 340 } else { |
| 340 // TODO(lfg) add error string | 341 // TODO(lfg) add error string |
| 341 OnParseFailure(""); | 342 return RespondNow(Error(kUnknownErrorDoNotUse)); |
| 342 return false; | |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | 345 |
| 346 void ManagementGetPermissionWarningsByManifestFunction::OnParseSuccess( | 346 void ManagementGetPermissionWarningsByManifestFunction::OnParseSuccess( |
| 347 std::unique_ptr<base::Value> value) { | 347 std::unique_ptr<base::Value> value) { |
| 348 if (!value->IsType(base::Value::Type::DICTIONARY)) { | 348 if (!value->IsType(base::Value::Type::DICTIONARY)) { |
| 349 OnParseFailure(keys::kManifestParseError); | 349 OnParseFailure(keys::kManifestParseError); |
| 350 return; | 350 return; |
| 351 } | 351 } |
| 352 const base::DictionaryValue* parsed_manifest = | 352 const base::DictionaryValue* parsed_manifest = |
| 353 static_cast<const base::DictionaryValue*>(value.get()); | 353 static_cast<const base::DictionaryValue*>(value.get()); |
| 354 | 354 |
| 355 std::string error; | |
| 355 scoped_refptr<Extension> extension = | 356 scoped_refptr<Extension> extension = |
| 356 Extension::Create(base::FilePath(), Manifest::INVALID_LOCATION, | 357 Extension::Create(base::FilePath(), Manifest::INVALID_LOCATION, |
| 357 *parsed_manifest, Extension::NO_FLAGS, &error_); | 358 *parsed_manifest, Extension::NO_FLAGS, &error); |
| 359 // TODO(lazyboy): Do we need to use |error|? | |
| 358 if (!extension) { | 360 if (!extension) { |
| 359 OnParseFailure(keys::kExtensionCreateError); | 361 OnParseFailure(keys::kExtensionCreateError); |
| 360 return; | 362 return; |
| 361 } | 363 } |
| 362 | 364 |
| 363 std::vector<std::string> warnings = CreateWarningsList(extension.get()); | 365 std::vector<std::string> warnings = CreateWarningsList(extension.get()); |
| 364 results_ = | 366 Respond(ArgumentList( |
| 365 management::GetPermissionWarningsByManifest::Results::Create(warnings); | 367 management::GetPermissionWarningsByManifest::Results::Create(warnings))); |
| 366 SendResponse(true); | |
| 367 | 368 |
| 368 // Matched with AddRef() in RunAsync(). | 369 // Matched with AddRef() in Run(). |
| 369 Release(); | 370 Release(); |
| 370 } | 371 } |
| 371 | 372 |
| 372 void ManagementGetPermissionWarningsByManifestFunction::OnParseFailure( | 373 void ManagementGetPermissionWarningsByManifestFunction::OnParseFailure( |
| 373 const std::string& error) { | 374 const std::string& error) { |
| 374 error_ = error; | 375 Respond(Error(error)); |
| 375 SendResponse(false); | |
| 376 | 376 |
| 377 // Matched with AddRef() in RunAsync(). | 377 // Matched with AddRef() in Run(). |
| 378 Release(); | 378 Release(); |
| 379 } | 379 } |
| 380 | 380 |
| 381 ExtensionFunction::ResponseAction ManagementLaunchAppFunction::Run() { | 381 ExtensionFunction::ResponseAction ManagementLaunchAppFunction::Run() { |
| 382 std::unique_ptr<management::LaunchApp::Params> params( | 382 std::unique_ptr<management::LaunchApp::Params> params( |
| 383 management::LaunchApp::Params::Create(*args_)); | 383 management::LaunchApp::Params::Create(*args_)); |
| 384 EXTENSION_FUNCTION_VALIDATE(params.get()); | 384 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 385 const Extension* extension = | 385 const Extension* extension = |
| 386 ExtensionRegistry::Get(browser_context()) | 386 ExtensionRegistry::Get(browser_context()) |
| 387 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); | 387 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 625 ManagementCreateAppShortcutFunction::~ManagementCreateAppShortcutFunction() { | 625 ManagementCreateAppShortcutFunction::~ManagementCreateAppShortcutFunction() { |
| 626 } | 626 } |
| 627 | 627 |
| 628 // static | 628 // static |
| 629 void ManagementCreateAppShortcutFunction::SetAutoConfirmForTest( | 629 void ManagementCreateAppShortcutFunction::SetAutoConfirmForTest( |
| 630 bool should_proceed) { | 630 bool should_proceed) { |
| 631 auto_confirm_for_test = should_proceed ? PROCEED : ABORT; | 631 auto_confirm_for_test = should_proceed ? PROCEED : ABORT; |
| 632 } | 632 } |
| 633 | 633 |
| 634 void ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt(bool created) { | 634 void ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt(bool created) { |
| 635 if (!created) | 635 Respond(created ? NoArguments() : Error(keys::kCreateShortcutCanceledError)); |
| 636 error_ = keys::kCreateShortcutCanceledError; | |
| 637 SendResponse(created); | |
| 638 Release(); | 636 Release(); |
|
Devlin
2017/01/20 00:16:01
Can we add a "balanced by" comment?
lazyboy
2017/01/20 00:54:47
Done.
| |
| 639 } | 637 } |
| 640 | 638 |
| 641 bool ManagementCreateAppShortcutFunction::RunAsync() { | 639 ExtensionFunction::ResponseAction ManagementCreateAppShortcutFunction::Run() { |
| 642 if (!user_gesture()) { | 640 if (!user_gesture()) |
| 643 error_ = keys::kGestureNeededForCreateAppShortcutError; | 641 return RespondNow(Error(keys::kGestureNeededForCreateAppShortcutError)); |
| 644 return false; | |
| 645 } | |
| 646 | 642 |
| 647 std::unique_ptr<management::CreateAppShortcut::Params> params( | 643 std::unique_ptr<management::CreateAppShortcut::Params> params( |
| 648 management::CreateAppShortcut::Params::Create(*args_)); | 644 management::CreateAppShortcut::Params::Create(*args_)); |
| 649 EXTENSION_FUNCTION_VALIDATE(params.get()); | 645 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 650 const Extension* extension = | 646 const Extension* extension = |
| 651 ExtensionRegistry::Get(browser_context()) | 647 ExtensionRegistry::Get(browser_context()) |
| 652 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); | 648 ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); |
| 653 if (!extension) { | 649 if (!extension) { |
| 654 error_ = | 650 return RespondNow(Error( |
| 655 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); | 651 ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id))); |
| 656 return false; | |
| 657 } | 652 } |
| 658 | 653 |
| 659 if (!extension->is_app()) { | 654 if (!extension->is_app()) { |
| 660 error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id); | 655 return RespondNow(Error( |
| 661 return false; | 656 ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id))); |
| 662 } | 657 } |
| 663 | 658 |
| 664 #if defined(OS_MACOSX) | 659 #if defined(OS_MACOSX) |
| 665 if (!extension->is_platform_app()) { | 660 if (!extension->is_platform_app()) |
| 666 error_ = keys::kCreateOnlyPackagedAppShortcutMac; | 661 return RespondNow(Error(keys::kCreateOnlyPackagedAppShortcutMac)); |
| 667 return false; | |
| 668 } | |
| 669 #endif | 662 #endif |
| 670 | 663 |
| 671 if (auto_confirm_for_test != DO_NOT_SKIP) { | 664 if (auto_confirm_for_test != DO_NOT_SKIP) { |
| 672 // Matched with a Release() in OnCloseShortcutPrompt(). | 665 // Matched with a Release() in OnCloseShortcutPrompt(). |
| 673 AddRef(); | 666 AddRef(); |
| 674 | 667 |
| 675 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); | 668 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); |
| 676 | 669 // OnCloseShortcutPrompt() might have called Respond() already. |
| 677 return true; | 670 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 678 } | 671 } |
| 679 | 672 |
| 680 std::string error; | 673 std::string error; |
| 681 if (ManagementAPI::GetFactoryInstance() | 674 if (ManagementAPI::GetFactoryInstance() |
| 682 ->Get(browser_context()) | 675 ->Get(browser_context()) |
| 683 ->GetDelegate() | 676 ->GetDelegate() |
| 684 ->CreateAppShortcutFunctionDelegate(this, extension, &error)) { | 677 ->CreateAppShortcutFunctionDelegate(this, extension, &error)) { |
| 685 // Matched with a Release() in OnCloseShortcutPrompt(). | 678 // Matched with a Release() in OnCloseShortcutPrompt(). |
| 686 AddRef(); | 679 AddRef(); |
| 680 // Response is sent async in OnCloseShortcutPrompt(). | |
| 681 return RespondLater(); | |
| 687 } else { | 682 } else { |
| 688 SetError(error); | 683 return RespondNow(Error(error)); |
| 689 } | 684 } |
| 690 | |
| 691 // Response is sent async in OnCloseShortcutPrompt(). | |
| 692 return true; | |
| 693 } | 685 } |
| 694 | 686 |
| 695 ExtensionFunction::ResponseAction ManagementSetLaunchTypeFunction::Run() { | 687 ExtensionFunction::ResponseAction ManagementSetLaunchTypeFunction::Run() { |
| 696 if (!user_gesture()) | 688 if (!user_gesture()) |
| 697 return RespondNow(Error(keys::kGestureNeededForSetLaunchTypeError)); | 689 return RespondNow(Error(keys::kGestureNeededForSetLaunchTypeError)); |
| 698 | 690 |
| 699 std::unique_ptr<management::SetLaunchType::Params> params( | 691 std::unique_ptr<management::SetLaunchType::Params> params( |
| 700 management::SetLaunchType::Params::Create(*args_)); | 692 management::SetLaunchType::Params::Create(*args_)); |
| 701 EXTENSION_FUNCTION_VALIDATE(params.get()); | 693 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 702 const Extension* extension = | 694 const Extension* extension = |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 745 | 737 |
| 746 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { | 738 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { |
| 747 } | 739 } |
| 748 | 740 |
| 749 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { | 741 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { |
| 750 } | 742 } |
| 751 | 743 |
| 752 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( | 744 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( |
| 753 const Extension* extension, | 745 const Extension* extension, |
| 754 const WebApplicationInfo& web_app_info) { | 746 const WebApplicationInfo& web_app_info) { |
| 755 if (extension) { | 747 ResponseValue response = |
| 756 results_ = management::GenerateAppForLink::Results::Create( | 748 extension ? ArgumentList(management::GenerateAppForLink::Results::Create( |
| 757 CreateExtensionInfo(*extension, browser_context())); | 749 CreateExtensionInfo(*extension, browser_context()))) |
| 758 | 750 : Error(keys::kGenerateAppForLinkInstallError); |
| 759 SendResponse(true); | 751 Respond(std::move(response)); |
| 760 Release(); | 752 Release(); // Balanced in Run(). |
| 761 } else { | |
| 762 error_ = keys::kGenerateAppForLinkInstallError; | |
| 763 SendResponse(false); | |
| 764 Release(); | |
| 765 } | |
| 766 } | 753 } |
| 767 | 754 |
| 768 bool ManagementGenerateAppForLinkFunction::RunAsync() { | 755 ExtensionFunction::ResponseAction ManagementGenerateAppForLinkFunction::Run() { |
| 769 if (!user_gesture()) { | 756 if (!user_gesture()) |
| 770 error_ = keys::kGestureNeededForGenerateAppForLinkError; | 757 return RespondNow(Error(keys::kGestureNeededForGenerateAppForLinkError)); |
| 771 return false; | |
| 772 } | |
| 773 | 758 |
| 774 std::unique_ptr<management::GenerateAppForLink::Params> params( | 759 std::unique_ptr<management::GenerateAppForLink::Params> params( |
| 775 management::GenerateAppForLink::Params::Create(*args_)); | 760 management::GenerateAppForLink::Params::Create(*args_)); |
| 776 EXTENSION_FUNCTION_VALIDATE(params.get()); | 761 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 777 | 762 |
| 778 GURL launch_url(params->url); | 763 GURL launch_url(params->url); |
| 779 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { | 764 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { |
| 780 error_ = | 765 return RespondNow(Error( |
| 781 ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, params->url); | 766 ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, params->url))); |
| 782 return false; | |
| 783 } | 767 } |
| 784 | 768 |
| 785 if (params->title.empty()) { | 769 if (params->title.empty()) |
| 786 error_ = keys::kEmptyTitleError; | 770 return RespondNow(Error(keys::kEmptyTitleError)); |
| 787 return false; | |
| 788 } | |
| 789 | 771 |
| 790 app_for_link_delegate_ = | 772 app_for_link_delegate_ = |
| 791 ManagementAPI::GetFactoryInstance() | 773 ManagementAPI::GetFactoryInstance() |
| 792 ->Get(browser_context()) | 774 ->Get(browser_context()) |
| 793 ->GetDelegate() | 775 ->GetDelegate() |
| 794 ->GenerateAppForLinkFunctionDelegate(this, browser_context(), | 776 ->GenerateAppForLinkFunctionDelegate(this, browser_context(), |
| 795 params->title, launch_url); | 777 params->title, launch_url); |
| 796 | 778 |
| 797 // Matched with a Release() in FinishCreateBookmarkApp(). | 779 // Matched with a Release() in FinishCreateBookmarkApp(). |
| 798 AddRef(); | 780 AddRef(); |
| 799 | 781 |
| 800 // Response is sent async in FinishCreateBookmarkApp(). | 782 // Response is sent async in FinishCreateBookmarkApp(). |
| 801 return true; | 783 return RespondLater(); |
| 802 } | 784 } |
| 803 | 785 |
| 804 ManagementEventRouter::ManagementEventRouter(content::BrowserContext* context) | 786 ManagementEventRouter::ManagementEventRouter(content::BrowserContext* context) |
| 805 : browser_context_(context), extension_registry_observer_(this) { | 787 : browser_context_(context), extension_registry_observer_(this) { |
| 806 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 788 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 807 } | 789 } |
| 808 | 790 |
| 809 ManagementEventRouter::~ManagementEventRouter() { | 791 ManagementEventRouter::~ManagementEventRouter() { |
| 810 } | 792 } |
| 811 | 793 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 883 ManagementAPI::GetFactoryInstance() { | 865 ManagementAPI::GetFactoryInstance() { |
| 884 return g_factory.Pointer(); | 866 return g_factory.Pointer(); |
| 885 } | 867 } |
| 886 | 868 |
| 887 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 869 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 888 management_event_router_.reset(new ManagementEventRouter(browser_context_)); | 870 management_event_router_.reset(new ManagementEventRouter(browser_context_)); |
| 889 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 871 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 890 } | 872 } |
| 891 | 873 |
| 892 } // namespace extensions | 874 } // namespace extensions |
| OLD | NEW |