diff options
author | lfg <lfg@chromium.org> | 2014-11-04 14:26:25 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-04 22:27:09 +0000 |
commit | 3fba1a6a4f245a326d7221b031a9bf599c0c5874 (patch) | |
tree | 8ab2d1f131bfc010cf7859e9377d7553ed7c4f01 | |
parent | cf09bfbca9ce0280aa31f3c93564bc29031580d7 (diff) | |
download | chromium_src-3fba1a6a4f245a326d7221b031a9bf599c0c5874.zip chromium_src-3fba1a6a4f245a326d7221b031a9bf599c0c5874.tar.gz chromium_src-3fba1a6a4f245a326d7221b031a9bf599c0c5874.tar.bz2 |
Move the chrome.management API to extensions.
BUG=422618
Review URL: https://codereview.chromium.org/696543002
Cr-Commit-Position: refs/heads/master@{#302681}
35 files changed, 907 insertions, 438 deletions
diff --git a/chrome/browser/chromeos/first_run/first_run.cc b/chrome/browser/chromeos/first_run/first_run.cc index 8fafdc7..b9c7947 100644 --- a/chrome/browser/chromeos/first_run/first_run.cc +++ b/chrome/browser/chromeos/first_run/first_run.cc @@ -13,6 +13,7 @@ #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/ui/extensions/application_launch.h" #include "chrome/common/chrome_switches.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/common/pref_names.h" #include "chromeos/chromeos_switches.h" #include "components/pref_registry/pref_registry_syncable.h" diff --git a/chrome/browser/extensions/api/chrome_extensions_api_client.cc b/chrome/browser/extensions/api/chrome_extensions_api_client.cc index 07315ac..0590980 100644 --- a/chrome/browser/extensions/api/chrome_extensions_api_client.cc +++ b/chrome/browser/extensions/api/chrome_extensions_api_client.cc @@ -7,6 +7,7 @@ #include "base/files/file_path.h" #include "chrome/browser/extensions/api/chrome_device_permissions_prompt.h" #include "chrome/browser/extensions/api/declarative_content/chrome_content_rules_registry.h" +#include "chrome/browser/extensions/api/management/chrome_management_api_delegate.h" #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h" #include "chrome/browser/extensions/api/web_request/chrome_extension_web_request_event_router_delegate.h" #include "chrome/browser/guest_view/app_view/chrome_app_view_guest_delegate.h" @@ -106,4 +107,9 @@ ChromeExtensionsAPIClient::CreateVirtualKeyboardDelegate() const { #endif } +ManagementAPIDelegate* ChromeExtensionsAPIClient::CreateManagementAPIDelegate() + const { + return new ChromeManagementAPIDelegate; +} + } // namespace extensions diff --git a/chrome/browser/extensions/api/chrome_extensions_api_client.h b/chrome/browser/extensions/api/chrome_extensions_api_client.h index e9363a2..0f0083b 100644 --- a/chrome/browser/extensions/api/chrome_extensions_api_client.h +++ b/chrome/browser/extensions/api/chrome_extensions_api_client.h @@ -41,6 +41,7 @@ class ChromeExtensionsAPIClient : public ExtensionsAPIClient { content::WebContents* web_contents) const override; scoped_ptr<VirtualKeyboardDelegate> CreateVirtualKeyboardDelegate() const override; + ManagementAPIDelegate* CreateManagementAPIDelegate() const override; private: DISALLOW_COPY_AND_ASSIGN(ChromeExtensionsAPIClient); diff --git a/chrome/browser/extensions/api/management/chrome_management_api_delegate.cc b/chrome/browser/extensions/api/management/chrome_management_api_delegate.cc new file mode 100644 index 0000000..c5e5740 --- /dev/null +++ b/chrome/browser/extensions/api/management/chrome_management_api_delegate.cc @@ -0,0 +1,389 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/api/management/chrome_management_api_delegate.h" + +#include "base/strings/utf_string_conversions.h" +#include "chrome/browser/extensions/bookmark_app_helper.h" +#include "chrome/browser/extensions/chrome_extension_function_details.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/extensions/extension_util.h" +#include "chrome/browser/extensions/launch_util.h" +#include "chrome/browser/favicon/favicon_service.h" +#include "chrome/browser/favicon/favicon_service_factory.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser_dialogs.h" +#include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/browser_window.h" +#include "chrome/browser/ui/extensions/app_launch_params.h" +#include "chrome/browser/ui/extensions/application_launch.h" +#include "chrome/browser/ui/webui/extensions/extension_icon_source.h" +#include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" +#include "chrome/common/extensions/chrome_utility_extensions_messages.h" +#include "chrome/common/extensions/manifest_handlers/app_launch_info.h" +#include "chrome/common/web_application_info.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/utility_process_host.h" +#include "content/public/browser/utility_process_host_client.h" +#include "content/public/browser/web_contents.h" +#include "extensions/browser/api/management/management_api.h" +#include "extensions/browser/api/management/management_api_constants.h" +#include "extensions/browser/extension_prefs.h" +#include "extensions/browser/extension_registry.h" +#include "extensions/browser/extension_system.h" +#include "extensions/common/constants.h" +#include "extensions/common/extension.h" + +namespace { + +// This class helps ManagementGetPermissionWarningsByManifestFunction manage +// sending manifest JSON strings to the utility process for parsing. +class SafeManifestJSONParser : public content::UtilityProcessHostClient { + public: + SafeManifestJSONParser( + extensions::ManagementGetPermissionWarningsByManifestFunction* client, + const std::string& manifest) + : client_(client), manifest_(manifest) {} + + void Start() { + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, + base::Bind(&SafeManifestJSONParser::StartWorkOnIOThread, this)); + } + + void StartWorkOnIOThread() { + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); + content::UtilityProcessHost* host = content::UtilityProcessHost::Create( + this, base::MessageLoopProxy::current().get()); + host->Send(new ChromeUtilityMsg_ParseJSON(manifest_)); + } + + bool OnMessageReceived(const IPC::Message& message) override { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(SafeManifestJSONParser, message) + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded, + OnJSONParseSucceeded) + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed, + OnJSONParseFailed) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; + } + + void OnJSONParseSucceeded(const base::ListValue& wrapper) { + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); + const base::Value* value = NULL; + CHECK(wrapper.Get(0, &value)); + if (value->IsType(base::Value::TYPE_DICTIONARY)) + parsed_manifest_.reset( + static_cast<const base::DictionaryValue*>(value)->DeepCopy()); + else + error_ = extension_management_api_constants::kManifestParseError; + + content::BrowserThread::PostTask( + content::BrowserThread::UI, FROM_HERE, + base::Bind(&SafeManifestJSONParser::ReportResultFromUIThread, this)); + } + + void OnJSONParseFailed(const std::string& error) { + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); + error_ = error; + content::BrowserThread::PostTask( + content::BrowserThread::UI, FROM_HERE, + base::Bind(&SafeManifestJSONParser::ReportResultFromUIThread, this)); + } + + void ReportResultFromUIThread() { + CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + if (error_.empty() && parsed_manifest_.get()) + client_->OnParseSuccess(parsed_manifest_.Pass()); + else + client_->OnParseFailure(error_); + } + + private: + ~SafeManifestJSONParser() override {} + + // The client who we'll report results back to. + extensions::ManagementGetPermissionWarningsByManifestFunction* client_; + + // Data to parse. + std::string manifest_; + + // Results of parsing. + scoped_ptr<base::DictionaryValue> parsed_manifest_; + + std::string error_; +}; + +class ManagementSetEnabledFunctionInstallPromptDelegate + : public ExtensionInstallPrompt::Delegate, + public extensions::InstallPromptDelegate { + public: + ManagementSetEnabledFunctionInstallPromptDelegate( + extensions::ManagementSetEnabledFunction* function, + const extensions::Extension* extension) + : function_(function), details_(function) { + install_prompt_.reset( + new ExtensionInstallPrompt(details_.GetAssociatedWebContents())); + install_prompt_->ConfirmReEnable(this, extension); + } + virtual ~ManagementSetEnabledFunctionInstallPromptDelegate() {} + + protected: + // ExtensionInstallPrompt::Delegate. + void InstallUIProceed() override { function_->InstallUIProceed(); } + void InstallUIAbort(bool user_initiated) override { + function_->InstallUIAbort(user_initiated); + } + + private: + extensions::ManagementSetEnabledFunction* function_; + ChromeExtensionFunctionDetails details_; + + // Used for prompting to re-enable items with permissions escalation updates. + scoped_ptr<ExtensionInstallPrompt> install_prompt_; +}; + +class ManagementUninstallFunctionUninstallDialogDelegate + : public extensions::ExtensionUninstallDialog::Delegate, + public extensions::UninstallDialogDelegate { + public: + ManagementUninstallFunctionUninstallDialogDelegate( + extensions::ManagementUninstallFunctionBase* function, + const std::string& target_extension_id) + : function_(function) { + const extensions::Extension* target_extension = + extensions::ExtensionRegistry::Get(function->browser_context()) + ->GetExtensionById(target_extension_id, + extensions::ExtensionRegistry::EVERYTHING); + content::WebContents* web_contents = function->GetAssociatedWebContents(); + extension_uninstall_dialog_.reset( + extensions::ExtensionUninstallDialog::Create( + Profile::FromBrowserContext(function->browser_context()), + web_contents ? web_contents->GetTopLevelNativeWindow() : NULL, + this)); + if (function->extension_id() != target_extension_id) { + extension_uninstall_dialog_->ConfirmProgrammaticUninstall( + target_extension, function->extension()); + } else { + // If this is a self uninstall, show the generic uninstall dialog. + extension_uninstall_dialog_->ConfirmUninstall(target_extension); + } + } + ~ManagementUninstallFunctionUninstallDialogDelegate() override {} + + // ExtensionUninstallDialog::Delegate implementation. + void ExtensionUninstallAccepted() override { + function_->ExtensionUninstallAccepted(); + } + void ExtensionUninstallCanceled() override { + function_->ExtensionUninstallCanceled(); + } + + protected: + extensions::ManagementUninstallFunctionBase* function_; + scoped_ptr<extensions::ExtensionUninstallDialog> extension_uninstall_dialog_; +}; + +class ChromeAppForLinkDelegate : public extensions::AppForLinkDelegate { + public: + ChromeAppForLinkDelegate() {} + ~ChromeAppForLinkDelegate() override {} + + void OnFaviconForApp( + extensions::ManagementGenerateAppForLinkFunction* function, + content::BrowserContext* context, + const std::string& title, + const GURL& launch_url, + const favicon_base::FaviconImageResult& image_result) { + WebApplicationInfo web_app; + web_app.title = base::UTF8ToUTF16(std::string(title)); + web_app.app_url = launch_url; + + if (!image_result.image.IsEmpty()) { + WebApplicationInfo::IconInfo icon; + icon.data = image_result.image.AsBitmap(); + icon.width = icon.data.width(); + icon.height = icon.data.height(); + web_app.icons.push_back(icon); + } + + bookmark_app_helper_.reset(new extensions::BookmarkAppHelper( + extensions::ExtensionSystem::Get(context)->extension_service(), web_app, + NULL)); + bookmark_app_helper_->Create( + base::Bind(&extensions::ManagementGenerateAppForLinkFunction:: + FinishCreateBookmarkApp, + function)); + } + + scoped_ptr<extensions::BookmarkAppHelper> bookmark_app_helper_; + + // Used for favicon loading tasks. + base::CancelableTaskTracker cancelable_task_tracker_; +}; + +} // namespace + +ChromeManagementAPIDelegate::ChromeManagementAPIDelegate() { +} + +ChromeManagementAPIDelegate::~ChromeManagementAPIDelegate() { +} + +bool ChromeManagementAPIDelegate::LaunchAppFunctionDelegate( + const extensions::Extension* extension, + content::BrowserContext* context) const { + // Look at prefs to find the right launch container. + // If the user has not set a preference, the default launch value will be + // returned. + extensions::LaunchContainer launch_container = + GetLaunchContainer(extensions::ExtensionPrefs::Get(context), extension); + OpenApplication(AppLaunchParams(Profile::FromBrowserContext(context), + extension, launch_container, + NEW_FOREGROUND_TAB)); + CoreAppLauncherHandler::RecordAppLaunchType( + extension_misc::APP_LAUNCH_EXTENSION_API, extension->GetType()); + + return true; +} + +GURL ChromeManagementAPIDelegate::GetFullLaunchURL( + const extensions::Extension* extension) const { + return extensions::AppLaunchInfo::GetFullLaunchURL(extension); +} + +extensions::LaunchType ChromeManagementAPIDelegate::GetLaunchType( + const extensions::ExtensionPrefs* prefs, + const extensions::Extension* extension) const { + return extensions::GetLaunchType(prefs, extension); +} + +void ChromeManagementAPIDelegate:: + GetPermissionWarningsByManifestFunctionDelegate( + extensions::ManagementGetPermissionWarningsByManifestFunction* function, + const std::string& manifest_str) const { + scoped_refptr<SafeManifestJSONParser> parser = + new SafeManifestJSONParser(function, manifest_str); + parser->Start(); +} + +scoped_ptr<extensions::InstallPromptDelegate> +ChromeManagementAPIDelegate::SetEnabledFunctionDelegate( + extensions::ManagementSetEnabledFunction* function, + const extensions::Extension* extension) const { + return scoped_ptr<ManagementSetEnabledFunctionInstallPromptDelegate>( + new ManagementSetEnabledFunctionInstallPromptDelegate(function, + extension)); +} + +scoped_ptr<extensions::UninstallDialogDelegate> +ChromeManagementAPIDelegate::UninstallFunctionDelegate( + extensions::ManagementUninstallFunctionBase* function, + const std::string& target_extension_id) const { + return scoped_ptr<extensions::UninstallDialogDelegate>( + new ManagementUninstallFunctionUninstallDialogDelegate( + function, target_extension_id)); +} + +bool ChromeManagementAPIDelegate::CreateAppShortcutFunctionDelegate( + extensions::ManagementCreateAppShortcutFunction* function, + const extensions::Extension* extension) const { + Browser* browser = chrome::FindBrowserWithProfile( + Profile::FromBrowserContext(function->browser_context()), + chrome::HOST_DESKTOP_TYPE_NATIVE); + if (!browser) { + // Shouldn't happen if we have user gesture. + function->SetError( + extension_management_api_constants::kNoBrowserToCreateShortcut); + return false; + } + + chrome::ShowCreateChromeAppShortcutsDialog( + browser->window()->GetNativeWindow(), browser->profile(), extension, + base::Bind(&extensions::ManagementCreateAppShortcutFunction:: + OnCloseShortcutPrompt, + function)); + + return true; +} + +scoped_ptr<extensions::AppForLinkDelegate> +ChromeManagementAPIDelegate::GenerateAppForLinkFunctionDelegate( + extensions::ManagementGenerateAppForLinkFunction* function, + content::BrowserContext* context, + const std::string& title, + const GURL& launch_url) const { + FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( + Profile::FromBrowserContext(context), Profile::EXPLICIT_ACCESS); + DCHECK(favicon_service); + + ChromeAppForLinkDelegate* delegate = new ChromeAppForLinkDelegate; + + favicon_service->GetFaviconImageForPageURL( + launch_url, + base::Bind( + &ChromeAppForLinkDelegate::OnFaviconForApp, + base::Unretained(delegate), + scoped_refptr<extensions::ManagementGenerateAppForLinkFunction>( + function), + context, title, launch_url), + &delegate->cancelable_task_tracker_); + + return scoped_ptr<extensions::AppForLinkDelegate>(delegate); +} + +bool ChromeManagementAPIDelegate::IsStreamlinedHostedAppsEnabled() const { + return extensions::util::IsStreamlinedHostedAppsEnabled(); +} + +void ChromeManagementAPIDelegate::EnableExtension( + content::BrowserContext* context, + const std::string& extension_id) const { + extensions::ExtensionSystem::Get(context) + ->extension_service() + ->EnableExtension(extension_id); +} + +void ChromeManagementAPIDelegate::DisableExtension( + content::BrowserContext* context, + const std::string& extension_id, + extensions::Extension::DisableReason disable_reason) const { + extensions::ExtensionSystem::Get(context) + ->extension_service() + ->DisableExtension(extension_id, disable_reason); +} + +bool ChromeManagementAPIDelegate::UninstallExtension( + content::BrowserContext* context, + const std::string& transient_extension_id, + extensions::UninstallReason reason, + const base::Closure& deletion_done_callback, + base::string16* error) const { + return extensions::ExtensionSystem::Get(context) + ->extension_service() + ->UninstallExtension(transient_extension_id, reason, + deletion_done_callback, error); +} + +void ChromeManagementAPIDelegate::SetLaunchType( + content::BrowserContext* context, + const std::string& extension_id, + extensions::LaunchType launch_type) const { + extensions::SetLaunchType( + extensions::ExtensionSystem::Get(context)->extension_service(), + extension_id, launch_type); +} + +GURL ChromeManagementAPIDelegate::GetIconURL( + const extensions::Extension* extension, + int icon_size, + ExtensionIconSet::MatchType match, + bool grayscale, + bool* exists) const { + return extensions::ExtensionIconSource::GetIconURL(extension, icon_size, + match, grayscale, exists); +} diff --git a/chrome/browser/extensions/api/management/chrome_management_api_delegate.h b/chrome/browser/extensions/api/management/chrome_management_api_delegate.h new file mode 100644 index 0000000..9a6b17f --- /dev/null +++ b/chrome/browser/extensions/api/management/chrome_management_api_delegate.h @@ -0,0 +1,69 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_CHROME_MANAGEMENT_API_DELEGATE_H_ +#define CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_CHROME_MANAGEMENT_API_DELEGATE_H_ + +#include "base/task/cancelable_task_tracker.h" +#include "chrome/browser/extensions/extension_install_prompt.h" +#include "chrome/browser/extensions/extension_uninstall_dialog.h" +#include "extensions/browser/api/management/management_api_delegate.h" + +namespace favicon_base { +struct FaviconImageResult; +} // namespace favicon_base + +class ChromeManagementAPIDelegate : public extensions::ManagementAPIDelegate { + public: + ChromeManagementAPIDelegate(); + ~ChromeManagementAPIDelegate() override; + + // ManagementAPIDelegate. + bool LaunchAppFunctionDelegate( + const extensions::Extension* extension, + content::BrowserContext* context) const override; + GURL GetFullLaunchURL(const extensions::Extension* extension) const override; + extensions::LaunchType GetLaunchType( + const extensions::ExtensionPrefs* prefs, + const extensions::Extension* extension) const override; + void GetPermissionWarningsByManifestFunctionDelegate( + extensions::ManagementGetPermissionWarningsByManifestFunction* function, + const std::string& manifest_str) const override; + scoped_ptr<extensions::InstallPromptDelegate> SetEnabledFunctionDelegate( + extensions::ManagementSetEnabledFunction* function, + const extensions::Extension* extension) const override; + scoped_ptr<extensions::UninstallDialogDelegate> UninstallFunctionDelegate( + extensions::ManagementUninstallFunctionBase* function, + const std::string& target_extension_id) const override; + bool CreateAppShortcutFunctionDelegate( + extensions::ManagementCreateAppShortcutFunction* function, + const extensions::Extension* extension) const override; + scoped_ptr<extensions::AppForLinkDelegate> GenerateAppForLinkFunctionDelegate( + extensions::ManagementGenerateAppForLinkFunction* function, + content::BrowserContext* context, + const std::string& title, + const GURL& launch_url) const override; + bool IsStreamlinedHostedAppsEnabled() const override; + void EnableExtension(content::BrowserContext* context, + const std::string& extension_id) const override; + void DisableExtension( + content::BrowserContext* context, + const std::string& extension_id, + extensions::Extension::DisableReason disable_reason) const override; + bool UninstallExtension(content::BrowserContext* context, + const std::string& transient_extension_id, + extensions::UninstallReason reason, + const base::Closure& deletion_done_callback, + base::string16* error) const override; + void SetLaunchType(content::BrowserContext* context, + const std::string& extension_id, + extensions::LaunchType launch_type) const override; + GURL GetIconURL(const extensions::Extension* extension, + int icon_size, + ExtensionIconSet::MatchType match, + bool grayscale, + bool* exists) const override; +}; + +#endif // CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_CHROME_MANAGEMENT_API_DELEGATE_H_ diff --git a/chrome/browser/extensions/api/management/management_api_browsertest.cc b/chrome/browser/extensions/api/management/management_api_browsertest.cc index 86a7bc7..bb08fe6 100644 --- a/chrome/browser/extensions/api/management/management_api_browsertest.cc +++ b/chrome/browser/extensions/api/management/management_api_browsertest.cc @@ -6,10 +6,9 @@ #include "base/files/scoped_temp_dir.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" -#include "chrome/browser/extensions/api/management/management_api.h" -#include "chrome/browser/extensions/api/management/management_api_constants.h" #include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/extensions/extension_function_test_utils.h" +#include "chrome/browser/extensions/extension_install_prompt.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" @@ -17,6 +16,8 @@ #include "content/public/common/url_constants.h" #include "content/public/test/browser_test_utils.h" #include "content/public/test/test_utils.h" +#include "extensions/browser/api/management/management_api.h" +#include "extensions/browser/api/management/management_api_constants.h" #include "extensions/browser/extension_host.h" #include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_system.h" diff --git a/chrome/browser/extensions/api/management/management_apitest.cc b/chrome/browser/extensions/api/management/management_apitest.cc index 8bcab7c..6d7ef81 100644 --- a/chrome/browser/extensions/api/management/management_apitest.cc +++ b/chrome/browser/extensions/api/management/management_apitest.cc @@ -5,7 +5,6 @@ #include <map> #include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/extensions/api/management/management_api.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/launch_util.h" @@ -18,6 +17,7 @@ #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension_constants.h" #include "content/public/test/test_utils.h" +#include "extensions/browser/api/management/management_api.h" #include "extensions/browser/extension_system.h" #include "extensions/browser/test_management_policy.h" #include "extensions/common/manifest.h" diff --git a/chrome/browser/extensions/api/webstore_private/webstore_private_apitest.cc b/chrome/browser/extensions/api/webstore_private/webstore_private_apitest.cc index 3c4728a..d5e2f51 100644 --- a/chrome/browser/extensions/api/webstore_private/webstore_private_apitest.cc +++ b/chrome/browser/extensions/api/webstore_private/webstore_private_apitest.cc @@ -9,7 +9,6 @@ #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/extensions/api/management/management_api.h" #include "chrome/browser/extensions/api/webstore_private/webstore_private_api.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_function_test_utils.h" @@ -25,6 +24,7 @@ #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" #include "content/public/test/browser_test_utils.h" +#include "extensions/browser/api/management/management_api.h" #include "extensions/browser/extension_system.h" #include "extensions/browser/install/extension_install_ui.h" #include "gpu/config/gpu_feature_type.h" diff --git a/chrome/browser/extensions/app_sync_data.h b/chrome/browser/extensions/app_sync_data.h index b8499e7..5bf97b4 100644 --- a/chrome/browser/extensions/app_sync_data.h +++ b/chrome/browser/extensions/app_sync_data.h @@ -6,7 +6,7 @@ #define CHROME_BROWSER_EXTENSIONS_APP_SYNC_DATA_H_ #include "chrome/browser/extensions/extension_sync_data.h" -#include "chrome/common/extensions/extension_constants.h" +#include "extensions/common/constants.h" #include "sync/api/string_ordinal.h" #include "sync/api/sync_change.h" diff --git a/chrome/browser/extensions/browser_context_keyed_service_factories.cc b/chrome/browser/extensions/browser_context_keyed_service_factories.cc index 11e8120..4cda1c7 100644 --- a/chrome/browser/extensions/browser_context_keyed_service_factories.cc +++ b/chrome/browser/extensions/browser_context_keyed_service_factories.cc @@ -25,7 +25,6 @@ #include "chrome/browser/extensions/api/identity/identity_api.h" #include "chrome/browser/extensions/api/idle/idle_manager_factory.h" #include "chrome/browser/extensions/api/location/location_manager.h" -#include "chrome/browser/extensions/api/management/management_api.h" #include "chrome/browser/extensions/api/mdns/mdns_api.h" #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.h" #include "chrome/browser/extensions/api/networking_private/networking_private_event_router_factory.h" @@ -122,7 +121,6 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() { #if defined(OS_CHROMEOS) extensions::LogPrivateAPI::GetFactoryInstance(); #endif - extensions::ManagementAPI::GetFactoryInstance(); extensions::MDnsAPI::GetFactoryInstance(); extensions::MediaGalleriesPrivateAPI::GetFactoryInstance(); #if defined(OS_CHROMEOS) diff --git a/chrome/browser/extensions/launch_util.h b/chrome/browser/extensions/launch_util.h index cb937cbc..3cf2641 100644 --- a/chrome/browser/extensions/launch_util.h +++ b/chrome/browser/extensions/launch_util.h @@ -7,7 +7,7 @@ #include <string> -#include "chrome/common/extensions/extension_constants.h" +#include "extensions/common/constants.h" class ExtensionService; diff --git a/chrome/browser/ui/app_list/app_list_controller_delegate.h b/chrome/browser/ui/app_list/app_list_controller_delegate.h index c419c8f..0580731 100644 --- a/chrome/browser/ui/app_list/app_list_controller_delegate.h +++ b/chrome/browser/ui/app_list/app_list_controller_delegate.h @@ -8,6 +8,7 @@ #include <string> #include "chrome/common/extensions/extension_constants.h" +#include "extensions/common/constants.h" #include "ui/base/page_transition_types.h" #include "ui/base/window_open_disposition.h" #include "ui/gfx/native_widget_types.h" diff --git a/chrome/browser/ui/ash/launcher/chrome_launcher_controller.h b/chrome/browser/ui/ash/launcher/chrome_launcher_controller.h index 7929320..e3b234e 100644 --- a/chrome/browser/ui/ash/launcher/chrome_launcher_controller.h +++ b/chrome/browser/ui/ash/launcher/chrome_launcher_controller.h @@ -30,8 +30,8 @@ #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" #include "chrome/browser/ui/ash/launcher/chrome_launcher_types.h" #include "chrome/browser/ui/extensions/extension_enable_flow_delegate.h" -#include "chrome/common/extensions/extension_constants.h" #include "extensions/browser/extension_registry_observer.h" +#include "extensions/common/constants.h" #include "ui/aura/window_observer.h" class AppSyncUIState; diff --git a/chrome/browser/ui/views/apps/app_info_dialog/app_info_summary_panel.h b/chrome/browser/ui/views/apps/app_info_dialog/app_info_summary_panel.h index f6715fa..bd5e1b5 100644 --- a/chrome/browser/ui/views/apps/app_info_dialog/app_info_summary_panel.h +++ b/chrome/browser/ui/views/apps/app_info_dialog/app_info_summary_panel.h @@ -8,7 +8,7 @@ #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "chrome/browser/ui/views/apps/app_info_dialog/app_info_panel.h" -#include "chrome/common/extensions/extension_constants.h" +#include "extensions/common/constants.h" #include "ui/views/controls/combobox/combobox_listener.h" #include "ui/views/controls/link_listener.h" diff --git a/chrome/browser/ui/webui/chromeos/login/error_screen_handler.cc b/chrome/browser/ui/webui/chromeos/login/error_screen_handler.cc index 78d276b..3e11de4 100644 --- a/chrome/browser/ui/webui/chromeos/login/error_screen_handler.cc +++ b/chrome/browser/ui/webui/chromeos/login/error_screen_handler.cc @@ -19,6 +19,7 @@ #include "chrome/browser/ui/extensions/application_launch.h" #include "chrome/browser/ui/webui/chromeos/login/native_window_delegate.h" #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h" +#include "chrome/common/extensions/extension_constants.h" #include "chrome/grit/chromium_strings.h" #include "chrome/grit/generated_resources.h" #include "chromeos/dbus/dbus_thread_manager.h" diff --git a/chrome/chrome_browser_extensions.gypi b/chrome/chrome_browser_extensions.gypi index 4b5f49e..5123b80 100644 --- a/chrome/chrome_browser_extensions.gypi +++ b/chrome/chrome_browser_extensions.gypi @@ -297,10 +297,8 @@ 'browser/extensions/api/location/location_api.h', 'browser/extensions/api/location/location_manager.cc', 'browser/extensions/api/location/location_manager.h', - 'browser/extensions/api/management/management_api.cc', - 'browser/extensions/api/management/management_api.h', - 'browser/extensions/api/management/management_api_constants.cc', - 'browser/extensions/api/management/management_api_constants.h', + 'browser/extensions/api/management/chrome_management_api_delegate.cc', + 'browser/extensions/api/management/chrome_management_api_delegate.h', 'browser/extensions/api/mdns/dns_sd_delegate.cc', 'browser/extensions/api/mdns/dns_sd_delegate.h', 'browser/extensions/api/mdns/dns_sd_device_lister.cc', diff --git a/chrome/common/extensions/api/_api_features.json b/chrome/common/extensions/api/_api_features.json index 1f61fef..0e783d9 100644 --- a/chrome/common/extensions/api/_api_features.json +++ b/chrome/common/extensions/api/_api_features.json @@ -475,25 +475,6 @@ "dependencies": ["permission:webcamPrivate"], "contexts": ["blessed_extension"] }, - "management": { - "dependencies": ["permission:management"], - "contexts": ["blessed_extension"] - }, - "management.getPermissionWarningsByManifest": { - "dependencies": [], - "channel": "stable", - "extension_types": ["extension", "legacy_packaged_app", "platform_app"] - }, - "management.getSelf": { - "dependencies": [], - "channel": "stable", - "extension_types": ["extension", "legacy_packaged_app", "platform_app"] - }, - "management.uninstallSelf": { - "dependencies": [], - "channel": "stable", - "extension_types": ["extension", "legacy_packaged_app", "platform_app"] - }, // This is not a real API, only here for documentation purposes. // See http://crbug.com/275944 for background. "manifestTypes": { diff --git a/chrome/common/extensions/api/schemas.gypi b/chrome/common/extensions/api/schemas.gypi index c44f333..d2e9712 100644 --- a/chrome/common/extensions/api/schemas.gypi +++ b/chrome/common/extensions/api/schemas.gypi @@ -57,7 +57,6 @@ 'image_writer_private.idl', 'input_ime.json', 'location.idl', - 'management.json', 'manifest_types.json', 'mdns.idl', 'media_galleries.idl', diff --git a/chrome/common/extensions/extension_constants.h b/chrome/common/extensions/extension_constants.h index 86192d1..eba1170 100644 --- a/chrome/common/extensions/extension_constants.h +++ b/chrome/common/extensions/extension_constants.h @@ -263,38 +263,4 @@ extern const int kWebstoreSignaturesPublicKeySize; } // namespace extension_misc -namespace extensions { - -// This enum is used for the launch type the user wants to use for an -// application. -// Do not remove items or re-order this enum as it is used in preferences -// and histograms. -enum LaunchType { - LAUNCH_TYPE_INVALID = -1, - LAUNCH_TYPE_FIRST = 0, - LAUNCH_TYPE_PINNED = LAUNCH_TYPE_FIRST, - LAUNCH_TYPE_REGULAR = 1, - LAUNCH_TYPE_FULLSCREEN = 2, - LAUNCH_TYPE_WINDOW = 3, - NUM_LAUNCH_TYPES, - - // Launch an app in the in the way a click on the NTP would, - // if no user pref were set. Update this constant to change - // the default for the NTP and chrome.management.launchApp(). - LAUNCH_TYPE_DEFAULT = LAUNCH_TYPE_REGULAR -}; - -// Don't remove items or change the order of this enum. It's used in -// histograms and preferences. -enum LaunchContainer { - LAUNCH_CONTAINER_WINDOW, - LAUNCH_CONTAINER_PANEL, - LAUNCH_CONTAINER_TAB, - // For platform apps, which don't actually have a container (they just get a - // "onLaunched" event). - LAUNCH_CONTAINER_NONE -}; - -} // namespace extensions - #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_CONSTANTS_H_ diff --git a/chrome/common/extensions/manifest_handlers/app_launch_info.h b/chrome/common/extensions/manifest_handlers/app_launch_info.h index cf39263..5c833cb 100644 --- a/chrome/common/extensions/manifest_handlers/app_launch_info.h +++ b/chrome/common/extensions/manifest_handlers/app_launch_info.h @@ -9,6 +9,7 @@ #include <vector> #include "chrome/common/extensions/extension_constants.h" +#include "extensions/common/constants.h" #include "extensions/common/extension.h" #include "extensions/common/manifest.h" #include "extensions/common/manifest_handler.h" diff --git a/extensions/browser/BUILD.gn b/extensions/browser/BUILD.gn index 0fb6496..e04baaf 100644 --- a/extensions/browser/BUILD.gn +++ b/extensions/browser/BUILD.gn @@ -50,8 +50,6 @@ source_set("browser") { "api/app_view/app_view_guest_internal_api.h", "api/app_window/app_window_api.cc", "api/app_window/app_window_api.h", - "api/guest_view/guest_view_internal_api.cc", - "api/guest_view/guest_view_internal_api.h", "api/async_api_function.cc", "api/async_api_function.h", "api/bluetooth/bluetooth_api.cc", @@ -143,12 +141,19 @@ source_set("browser") { "api/execute_code_function.h", "api/extensions_api_client.cc", "api/extensions_api_client.h", + "api/guest_view/guest_view_internal_api.cc", + "api/guest_view/guest_view_internal_api.h", "api/hid/hid_api.cc", "api/hid/hid_api.h", "api/hid/hid_connection_resource.cc", "api/hid/hid_connection_resource.h", "api/hid/hid_device_manager.cc", "api/hid/hid_device_manager.h", + "api/management/management_api.cc", + "api/management/management_api.h", + "api/management/management_api_constants.cc", + "api/management/management_api_constants.h", + "api/management/management_api_delegate.h", "api/messaging/native_message_host.cc", "api/power/power_api.cc", "api/power/power_api.h", diff --git a/extensions/browser/api/extensions_api_client.cc b/extensions/browser/api/extensions_api_client.cc index 222e686..d588a6b 100644 --- a/extensions/browser/api/extensions_api_client.cc +++ b/extensions/browser/api/extensions_api_client.cc @@ -81,4 +81,9 @@ ExtensionsAPIClient::CreateVirtualKeyboardDelegate() const { return nullptr; } +ManagementAPIDelegate* ExtensionsAPIClient::CreateManagementAPIDelegate() + const { + return nullptr; +} + } // namespace extensions diff --git a/extensions/browser/api/extensions_api_client.h b/extensions/browser/api/extensions_api_client.h index 96db1b0..f6c9e25 100644 --- a/extensions/browser/api/extensions_api_client.h +++ b/extensions/browser/api/extensions_api_client.h @@ -29,6 +29,7 @@ class ContentRulesRegistry; class DevicePermissionsPrompt; class ExtensionOptionsGuest; class ExtensionOptionsGuestDelegate; +class ManagementAPIDelegate; class MimeHandlerViewGuest; class MimeHandlerViewGuestDelegate; class WebViewGuest; @@ -107,6 +108,9 @@ class ExtensionsAPIClient { virtual scoped_ptr<VirtualKeyboardDelegate> CreateVirtualKeyboardDelegate() const; + // Creates a delegate for handling the management extension api. + virtual ManagementAPIDelegate* CreateManagementAPIDelegate() const; + // NOTE: If this interface gains too many methods (perhaps more than 20) it // should be split into one interface per API. }; diff --git a/chrome/browser/extensions/api/management/management_api.cc b/extensions/browser/api/management/management_api.cc index 1f6a8ce..bcf42b8 100644 --- a/chrome/browser/extensions/api/management/management_api.cc +++ b/extensions/browser/api/management/management_api.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/extensions/api/management/management_api.h" +#include "extensions/browser/api/management/management_api.h" #include <string> #include <vector> @@ -18,32 +18,16 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" -#include "chrome/browser/extensions/api/management/management_api_constants.h" -#include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/extensions/extension_ui_util.h" -#include "chrome/browser/extensions/extension_uninstall_dialog.h" -#include "chrome/browser/extensions/extension_util.h" -#include "chrome/browser/extensions/launch_util.h" -#include "chrome/browser/favicon/favicon_service_factory.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/ui/browser_dialogs.h" -#include "chrome/browser/ui/browser_finder.h" -#include "chrome/browser/ui/browser_window.h" -#include "chrome/browser/ui/extensions/application_launch.h" -#include "chrome/browser/ui/webui/extensions/extension_icon_source.h" -#include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" -#include "chrome/common/extensions/api/management.h" -#include "chrome/common/extensions/chrome_utility_extensions_messages.h" -#include "chrome/common/extensions/extension_constants.h" -#include "chrome/common/extensions/manifest_handlers/app_launch_info.h" -#include "content/public/browser/utility_process_host.h" -#include "content/public/browser/utility_process_host_client.h" +#include "content/public/browser/browser_context.h" +#include "extensions/browser/api/extensions_api_client.h" +#include "extensions/browser/api/management/management_api_constants.h" #include "extensions/browser/event_router.h" #include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_system.h" #include "extensions/browser/management_policy.h" #include "extensions/browser/uninstall_reason.h" +#include "extensions/common/api/management.h" #include "extensions/common/constants.h" #include "extensions/common/error_utils.h" #include "extensions/common/extension.h" @@ -58,25 +42,19 @@ using base::IntToString; using content::BrowserThread; -using content::UtilityProcessHost; -using content::UtilityProcessHostClient; namespace keys = extension_management_api_constants; namespace extensions { -namespace management = api::management; +namespace management = core_api::management; namespace { -typedef std::vector<linked_ptr<management::ExtensionInfo> > ExtensionInfoList; -typedef std::vector<linked_ptr<management::IconInfo> > IconInfoList; +typedef std::vector<linked_ptr<management::ExtensionInfo>> ExtensionInfoList; +typedef std::vector<linked_ptr<management::IconInfo>> IconInfoList; -enum AutoConfirmForTest { - DO_NOT_SKIP = 0, - PROCEED, - ABORT -}; +enum AutoConfirmForTest { DO_NOT_SKIP = 0, PROCEED, ABORT }; AutoConfirmForTest auto_confirm_for_test = DO_NOT_SKIP; @@ -93,7 +71,8 @@ std::vector<std::string> CreateWarningsList(const Extension* extension) { } std::vector<management::LaunchType> GetAvailableLaunchTypes( - const Extension& extension) { + const Extension& extension, + const ManagementAPIDelegate* delegate) { std::vector<management::LaunchType> launch_type_list; if (extension.is_platform_app()) { launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_WINDOW); @@ -106,7 +85,7 @@ std::vector<management::LaunchType> GetAvailableLaunchTypes( launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_WINDOW); #endif - if (!util::IsStreamlinedHostedAppsEnabled()) { + if (!delegate->IsStreamlinedHostedAppsEnabled()) { launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_AS_PINNED_TAB); launch_type_list.push_back(management::LAUNCH_TYPE_OPEN_FULL_SCREEN); } @@ -115,22 +94,25 @@ std::vector<management::LaunchType> GetAvailableLaunchTypes( scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( const Extension& extension, - ExtensionSystem* system) { + content::BrowserContext* context) { + ExtensionSystem* system = ExtensionSystem::Get(context); + ExtensionRegistry* registry = ExtensionRegistry::Get(context); + const ManagementAPIDelegate* delegate = + ManagementAPI::GetFactoryInstance()->Get(context)->GetDelegate(); scoped_ptr<management::ExtensionInfo> info(new management::ExtensionInfo()); - ExtensionService* service = system->extension_service(); info->id = extension.id(); info->name = extension.name(); info->short_name = extension.short_name(); - info->enabled = service->IsExtensionEnabled(info->id); + info->enabled = registry->enabled_extensions().Contains(info->id); info->offline_enabled = OfflineEnabledInfo::IsOfflineEnabled(&extension); info->version = extension.VersionString(); info->description = extension.description(); info->options_url = OptionsPageInfo::GetOptionsPage(&extension).spec(); - info->homepage_url.reset(new std::string( - ManifestURL::GetHomepageURL(&extension).spec())); - info->may_disable = system->management_policy()-> - UserMayModifySettings(&extension, NULL); + info->homepage_url.reset( + new std::string(ManifestURL::GetHomepageURL(&extension).spec())); + info->may_disable = + system->management_policy()->UserMayModifySettings(&extension, NULL); info->is_app = extension.is_app(); if (info->is_app) { if (extension.is_legacy_packaged_app()) @@ -148,7 +130,7 @@ scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( if (info->enabled) { info->disabled_reason = management::ExtensionInfo::DISABLED_REASON_NONE; } else { - ExtensionPrefs* prefs = ExtensionPrefs::Get(service->profile()); + ExtensionPrefs* prefs = ExtensionPrefs::Get(context); if (prefs->DidExtensionEscalatePermissions(extension.id())) { info->disabled_reason = management::ExtensionInfo::DISABLED_REASON_PERMISSIONS_INCREASE; @@ -159,13 +141,13 @@ scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( } if (!ManifestURL::GetUpdateURL(&extension).is_empty()) { - info->update_url.reset(new std::string( - ManifestURL::GetUpdateURL(&extension).spec())); + info->update_url.reset( + new std::string(ManifestURL::GetUpdateURL(&extension).spec())); } if (extension.is_app()) { - info->app_launch_url.reset(new std::string( - AppLaunchInfo::GetFullLaunchURL(&extension).spec())); + info->app_launch_url.reset( + new std::string(delegate->GetFullLaunchURL(&extension).spec())); } const ExtensionIconSet::IconMap& icons = @@ -176,9 +158,9 @@ scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( for (icon_iter = icons.begin(); icon_iter != icons.end(); ++icon_iter) { management::IconInfo* icon_info = new management::IconInfo(); icon_info->size = icon_iter->first; - GURL url = ExtensionIconSource::GetIconURL( - &extension, icon_info->size, ExtensionIconSet::MATCH_EXACTLY, false, - NULL); + GURL url = + delegate->GetIconURL(&extension, icon_info->size, + ExtensionIconSet::MATCH_EXACTLY, false, nullptr); icon_info->url = url.spec(); info->icons->push_back(make_linked_ptr<management::IconInfo>(icon_info)); } @@ -237,7 +219,7 @@ scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( launch_type = LAUNCH_TYPE_WINDOW; } else { launch_type = - GetLaunchType(ExtensionPrefs::Get(service->profile()), &extension); + delegate->GetLaunchType(ExtensionPrefs::Get(context), &extension); } switch (launch_type) { @@ -259,49 +241,45 @@ scoped_ptr<management::ExtensionInfo> CreateExtensionInfo( } info->available_launch_types.reset(new std::vector<management::LaunchType>( - GetAvailableLaunchTypes(extension))); + GetAvailableLaunchTypes(extension, delegate))); } return info.Pass(); } +bool ShouldNotBeVisible(const Extension* extension, + content::BrowserContext* context) { + return (extension->ShouldNotBeVisible() || + ExtensionPrefs::Get(context)->IsEphemeralApp(extension->id())); +} + void AddExtensionInfo(const ExtensionSet& extensions, - ExtensionSystem* system, - ExtensionInfoList* extension_list, - content::BrowserContext* context) { + ExtensionInfoList* extension_list, + content::BrowserContext* context) { for (ExtensionSet::const_iterator iter = extensions.begin(); iter != extensions.end(); ++iter) { const Extension& extension = *iter->get(); - if (ui_util::ShouldNotBeVisible(&extension, context)) + if (ShouldNotBeVisible(&extension, context)) continue; // Skip built-in extensions/apps. extension_list->push_back(make_linked_ptr<management::ExtensionInfo>( - CreateExtensionInfo(extension, system).release())); + CreateExtensionInfo(extension, context).release())); } } } // namespace -ExtensionService* ManagementFunction::service() { - return ExtensionSystem::Get(GetProfile())->extension_service(); -} - -ExtensionService* AsyncManagementFunction::service() { - return ExtensionSystem::Get(GetProfile())->extension_service(); -} - bool ManagementGetAllFunction::RunSync() { ExtensionInfoList extensions; - ExtensionRegistry* registry = ExtensionRegistry::Get(GetProfile()); - ExtensionSystem* system = ExtensionSystem::Get(GetProfile()); + ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context()); - AddExtensionInfo(registry->enabled_extensions(), - system, &extensions, browser_context()); - AddExtensionInfo(registry->disabled_extensions(), - system, &extensions, browser_context()); - AddExtensionInfo(registry->terminated_extensions(), - system, &extensions, browser_context()); + AddExtensionInfo(registry->enabled_extensions(), &extensions, + browser_context()); + AddExtensionInfo(registry->disabled_extensions(), &extensions, + browser_context()); + AddExtensionInfo(registry->terminated_extensions(), &extensions, + browser_context()); results_ = management::GetAll::Results::Create(extensions); return true; @@ -311,16 +289,18 @@ bool ManagementGetFunction::RunSync() { scoped_ptr<management::Get::Params> params( management::Get::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); + ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context()); - const Extension* extension = service()->GetExtensionById(params->id, true); + const Extension* extension = + registry->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); if (!extension) { - error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, - params->id); + error_ = + ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); return false; } scoped_ptr<management::ExtensionInfo> info = - CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); + CreateExtensionInfo(*extension, browser_context()); results_ = management::Get::Results::Create(*info); return true; @@ -328,7 +308,7 @@ bool ManagementGetFunction::RunSync() { bool ManagementGetSelfFunction::RunSync() { scoped_ptr<management::ExtensionInfo> info = - CreateExtensionInfo(*extension_, ExtensionSystem::Get(GetProfile())); + CreateExtensionInfo(*extension_, browser_context()); results_ = management::Get::Results::Create(*info); return true; @@ -339,10 +319,12 @@ bool ManagementGetPermissionWarningsByIdFunction::RunSync() { management::GetPermissionWarningsById::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); - const Extension* extension = service()->GetExtensionById(params->id, true); + const Extension* extension = + ExtensionRegistry::Get(browser_context()) + ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); if (!extension) { - error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, - params->id); + error_ = + ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); return false; } @@ -351,118 +333,38 @@ bool ManagementGetPermissionWarningsByIdFunction::RunSync() { return true; } -namespace { - -// This class helps ManagementGetPermissionWarningsByManifestFunction manage -// sending manifest JSON strings to the utility process for parsing. -class SafeManifestJSONParser : public UtilityProcessHostClient { - public: - SafeManifestJSONParser( - ManagementGetPermissionWarningsByManifestFunction* client, - const std::string& manifest) - : client_(client), - manifest_(manifest) {} - - void Start() { - CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - BrowserThread::PostTask( - BrowserThread::IO, - FROM_HERE, - base::Bind(&SafeManifestJSONParser::StartWorkOnIOThread, this)); - } - - void StartWorkOnIOThread() { - CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - UtilityProcessHost* host = UtilityProcessHost::Create( - this, base::MessageLoopProxy::current().get()); - host->Send(new ChromeUtilityMsg_ParseJSON(manifest_)); - } - - bool OnMessageReceived(const IPC::Message& message) override { - bool handled = true; - IPC_BEGIN_MESSAGE_MAP(SafeManifestJSONParser, message) - IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded, - OnJSONParseSucceeded) - IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed, - OnJSONParseFailed) - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled; - } - - void OnJSONParseSucceeded(const base::ListValue& wrapper) { - CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - const base::Value* value = NULL; - CHECK(wrapper.Get(0, &value)); - if (value->IsType(base::Value::TYPE_DICTIONARY)) - parsed_manifest_.reset( - static_cast<const base::DictionaryValue*>(value)->DeepCopy()); - else - error_ = keys::kManifestParseError; - - BrowserThread::PostTask( - BrowserThread::UI, - FROM_HERE, - base::Bind(&SafeManifestJSONParser::ReportResultFromUIThread, this)); - } - - void OnJSONParseFailed(const std::string& error) { - CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - error_ = error; - BrowserThread::PostTask( - BrowserThread::UI, - FROM_HERE, - base::Bind(&SafeManifestJSONParser::ReportResultFromUIThread, this)); - } - - void ReportResultFromUIThread() { - CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (error_.empty() && parsed_manifest_.get()) - client_->OnParseSuccess(parsed_manifest_.Pass()); - else - client_->OnParseFailure(error_); - } - - private: - ~SafeManifestJSONParser() override {} - - // The client who we'll report results back to. - ManagementGetPermissionWarningsByManifestFunction* client_; - - // Data to parse. - std::string manifest_; - - // Results of parsing. - scoped_ptr<base::DictionaryValue> parsed_manifest_; - - std::string error_; -}; - -} // namespace - bool ManagementGetPermissionWarningsByManifestFunction::RunAsync() { scoped_ptr<management::GetPermissionWarningsByManifest::Params> params( management::GetPermissionWarningsByManifest::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); - scoped_refptr<SafeManifestJSONParser> parser = - new SafeManifestJSONParser(this, params->manifest_str); - parser->Start(); + const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate(); - // Matched with a Release() in OnParseSuccess/Failure(). - AddRef(); + if (delegate) { + delegate->GetPermissionWarningsByManifestFunctionDelegate( + this, params->manifest_str); - // Response is sent async in OnParseSuccess/Failure(). - return true; + // Matched with a Release() in OnParseSuccess/Failure(). + AddRef(); + + // Response is sent async in OnParseSuccess/Failure(). + return true; + } else { + // TODO(lfg) add error string + OnParseFailure(""); + return false; + } } void ManagementGetPermissionWarningsByManifestFunction::OnParseSuccess( scoped_ptr<base::DictionaryValue> parsed_manifest) { CHECK(parsed_manifest.get()); - scoped_refptr<Extension> extension = Extension::Create( - base::FilePath(), Manifest::INVALID_LOCATION, *parsed_manifest, - Extension::NO_FLAGS, &error_); + scoped_refptr<Extension> extension = + Extension::Create(base::FilePath(), Manifest::INVALID_LOCATION, + *parsed_manifest, Extension::NO_FLAGS, &error_); if (!extension.get()) { OnParseFailure(keys::kExtensionCreateError); return; @@ -490,30 +392,23 @@ bool ManagementLaunchAppFunction::RunSync() { scoped_ptr<management::LaunchApp::Params> params( management::LaunchApp::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); - const Extension* extension = service()->GetExtensionById(params->id, true); + const Extension* extension = + ExtensionRegistry::Get(browser_context()) + ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); if (!extension) { - error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, - params->id); + error_ = + ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); return false; } if (!extension->is_app()) { - error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, - params->id); + error_ = ErrorUtils::FormatErrorMessage(keys::kNotAnAppError, params->id); return false; } - // Look at prefs to find the right launch container. - // If the user has not set a preference, the default launch value will be - // returned. - LaunchContainer launch_container = - GetLaunchContainer(ExtensionPrefs::Get(GetProfile()), extension); - OpenApplication(AppLaunchParams( - GetProfile(), extension, launch_container, NEW_FOREGROUND_TAB)); - CoreAppLauncherHandler::RecordAppLaunchType( - extension_misc::APP_LAUNCH_EXTENSION_API, - extension->GetType()); - - return true; + const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate(); + return delegate->LaunchAppFunctionDelegate(extension, browser_context()); } ManagementSetEnabledFunction::ManagementSetEnabledFunction() { @@ -526,58 +421,64 @@ bool ManagementSetEnabledFunction::RunAsync() { scoped_ptr<management::SetEnabled::Params> params( management::SetEnabled::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); + ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context()); + const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate(); extension_id_ = params->id; const Extension* extension = - ExtensionRegistry::Get(GetProfile()) - ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); - if (!extension || ui_util::ShouldNotBeVisible(extension, browser_context())) { - error_ = ErrorUtils::FormatErrorMessage( - keys::kNoExtensionError, extension_id_); + registry->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); + if (!extension || ShouldNotBeVisible(extension, browser_context())) { + error_ = + ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, extension_id_); return false; } const ManagementPolicy* policy = - ExtensionSystem::Get(GetProfile())->management_policy(); + ExtensionSystem::Get(browser_context())->management_policy(); if (!policy->UserMayModifySettings(extension, NULL) || (!params->enabled && policy->MustRemainEnabled(extension, NULL)) || (params->enabled && policy->MustRemainDisabled(extension, NULL, NULL))) { - error_ = ErrorUtils::FormatErrorMessage( - keys::kUserCantModifyError, extension_id_); + error_ = ErrorUtils::FormatErrorMessage(keys::kUserCantModifyError, + extension_id_); return false; } - bool currently_enabled = service()->IsExtensionEnabled(extension_id_); + bool currently_enabled = + registry->enabled_extensions().Contains(extension_id_) || + registry->terminated_extensions().Contains(extension_id_); if (!currently_enabled && params->enabled) { - ExtensionPrefs* prefs = ExtensionPrefs::Get(GetProfile()); + ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context()); if (prefs->DidExtensionEscalatePermissions(extension_id_)) { if (!user_gesture()) { - error_ = keys::kGestureNeededForEscalationError; + SetError(keys::kGestureNeededForEscalationError); return false; } AddRef(); // Matched in InstallUIProceed/InstallUIAbort - install_prompt_.reset( - new ExtensionInstallPrompt(GetAssociatedWebContents())); - install_prompt_->ConfirmReEnable(this, extension); + install_prompt_ = delegate->SetEnabledFunctionDelegate(this, extension); return true; } - service()->EnableExtension(extension_id_); + delegate->EnableExtension(browser_context(), extension_id_); } else if (currently_enabled && !params->enabled) { - service()->DisableExtension(extension_id_, Extension::DISABLE_USER_ACTION); + delegate->DisableExtension(browser_context(), extension_id_, + Extension::DISABLE_USER_ACTION); } BrowserThread::PostTask( - BrowserThread::UI, - FROM_HERE, + BrowserThread::UI, FROM_HERE, base::Bind(&ManagementSetEnabledFunction::SendResponse, this, true)); return true; } void ManagementSetEnabledFunction::InstallUIProceed() { - service()->EnableExtension(extension_id_); + ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate() + ->EnableExtension(browser_context(), extension_id_); SendResponse(true); Release(); } @@ -597,40 +498,32 @@ ManagementUninstallFunctionBase::~ManagementUninstallFunctionBase() { bool ManagementUninstallFunctionBase::Uninstall( const std::string& target_extension_id, bool show_confirm_dialog) { + const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate(); extension_id_ = target_extension_id; const Extension* target_extension = - extensions::ExtensionRegistry::Get(browser_context())-> - GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); + extensions::ExtensionRegistry::Get(browser_context()) + ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); if (!target_extension || - ui_util::ShouldNotBeVisible(target_extension, browser_context())) { - error_ = ErrorUtils::FormatErrorMessage( - keys::kNoExtensionError, extension_id_); + ShouldNotBeVisible(target_extension, browser_context())) { + error_ = + ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, extension_id_); return false; } - if (!ExtensionSystem::Get(GetProfile()) + if (!ExtensionSystem::Get(browser_context()) ->management_policy() ->UserMayModifySettings(target_extension, NULL)) { - error_ = ErrorUtils::FormatErrorMessage( - keys::kUserCantModifyError, extension_id_); + error_ = ErrorUtils::FormatErrorMessage(keys::kUserCantModifyError, + extension_id_); return false; } if (auto_confirm_for_test == DO_NOT_SKIP) { if (show_confirm_dialog) { AddRef(); // Balanced in ExtensionUninstallAccepted/Canceled - content::WebContents* web_contents = GetAssociatedWebContents(); - extension_uninstall_dialog_.reset(ExtensionUninstallDialog::Create( - GetProfile(), - web_contents ? web_contents->GetTopLevelNativeWindow() : NULL, - this)); - if (extension_id() != target_extension_id) { - extension_uninstall_dialog_->ConfirmProgrammaticUninstall( - target_extension, extension()); - } else { - // If this is a self uninstall, show the generic uninstall dialog. - extension_uninstall_dialog_->ConfirmUninstall(target_extension); - } + delegate->UninstallFunctionDelegate(this, target_extension_id); } else { Finish(true); } @@ -651,7 +544,7 @@ void ManagementUninstallFunctionBase::Finish(bool should_uninstall) { if (should_uninstall) { // The extension can be uninstalled in another window while the UI was // showing. Do nothing in that case. - ExtensionRegistry* registry = ExtensionRegistry::Get(GetProfile()); + ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context()); const Extension* extension = registry->GetExtensionById( extension_id_, ExtensionRegistry::EVERYTHING); if (!extension) { @@ -659,18 +552,21 @@ void ManagementUninstallFunctionBase::Finish(bool should_uninstall) { extension_id_); SendResponse(false); } else { - bool success = service()->UninstallExtension( - extension_id_, + const ManagementAPIDelegate* delegate = + ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate(); + bool success = delegate->UninstallExtension( + browser_context(), extension_id_, extensions::UNINSTALL_REASON_MANAGEMENT_API, - base::Bind(&base::DoNothing), - NULL); + base::Bind(&base::DoNothing), NULL); // TODO set error_ if !success SendResponse(success); } } else { - error_ = ErrorUtils::FormatErrorMessage( - keys::kUninstallCanceledError, extension_id_); + error_ = ErrorUtils::FormatErrorMessage(keys::kUninstallCanceledError, + extension_id_); SendResponse(false); } } @@ -757,10 +653,12 @@ bool ManagementCreateAppShortcutFunction::RunAsync() { scoped_ptr<management::CreateAppShortcut::Params> params( management::CreateAppShortcut::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); - const Extension* extension = service()->GetExtensionById(params->id, true); + const Extension* extension = + ExtensionRegistry::Get(browser_context()) + ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); if (!extension) { - error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, - params->id); + error_ = + ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); return false; } @@ -776,24 +674,21 @@ bool ManagementCreateAppShortcutFunction::RunAsync() { } #endif - Browser* browser = chrome::FindBrowserWithProfile( - GetProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE); - if (!browser) { - // Shouldn't happen if we have user gesture. - error_ = keys::kNoBrowserToCreateShortcut; - return false; - } + if (auto_confirm_for_test != DO_NOT_SKIP) { + // Matched with a Release() in OnCloseShortcutPrompt(). + AddRef(); - // Matched with a Release() in OnCloseShortcutPrompt(). - AddRef(); - - if (auto_confirm_for_test == DO_NOT_SKIP) { - chrome::ShowCreateChromeAppShortcutsDialog( - browser->window()->GetNativeWindow(), browser->profile(), extension, - base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, - this)); - } else { OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); + + return true; + } + + if (ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate() + ->CreateAppShortcutFunctionDelegate(this, extension)) { + // Matched with a Release() in OnCloseShortcutPrompt(). + AddRef(); } // Response is sent async in OnCloseShortcutPrompt(). @@ -809,7 +704,12 @@ bool ManagementSetLaunchTypeFunction::RunSync() { scoped_ptr<management::SetLaunchType::Params> params( management::SetLaunchType::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); - const Extension* extension = service()->GetExtensionById(params->id, true); + const Extension* extension = + ExtensionRegistry::Get(browser_context()) + ->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING); + const ManagementAPIDelegate* delegate = ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate(); if (!extension) { error_ = ErrorUtils::FormatErrorMessage(keys::kNoExtensionError, params->id); @@ -822,11 +722,10 @@ bool ManagementSetLaunchTypeFunction::RunSync() { } std::vector<management::LaunchType> available_launch_types = - GetAvailableLaunchTypes(*extension); + GetAvailableLaunchTypes(*extension, delegate); management::LaunchType app_launch_type = params->launch_type; - if (std::find(available_launch_types.begin(), - available_launch_types.end(), + if (std::find(available_launch_types.begin(), available_launch_types.end(), app_launch_type) == available_launch_types.end()) { error_ = keys::kLaunchTypeNotAvailableError; return false; @@ -850,7 +749,7 @@ bool ManagementSetLaunchTypeFunction::RunSync() { NOTREACHED(); } - SetLaunchType(service(), params->id, launch_type); + delegate->SetLaunchType(browser_context(), params->id, launch_type); return true; } @@ -866,7 +765,7 @@ void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( const WebApplicationInfo& web_app_info) { if (extension) { scoped_ptr<management::ExtensionInfo> info = - CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); + CreateExtensionInfo(*extension, browser_context()); results_ = management::GenerateAppForLink::Results::Create(*info); SendResponse(true); @@ -878,25 +777,6 @@ void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( } } -void ManagementGenerateAppForLinkFunction::OnFaviconForApp( - const favicon_base::FaviconImageResult& image_result) { - WebApplicationInfo web_app; - web_app.title = base::UTF8ToUTF16(title_); - web_app.app_url = launch_url_; - - if (!image_result.image.IsEmpty()) { - WebApplicationInfo::IconInfo icon; - icon.data = image_result.image.AsBitmap(); - icon.width = icon.data.width(); - icon.height = icon.data.height(); - web_app.icons.push_back(icon); - } - - bookmark_app_helper_.reset(new BookmarkAppHelper(service(), web_app, NULL)); - bookmark_app_helper_->Create(base::Bind( - &ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp, this)); -} - bool ManagementGenerateAppForLinkFunction::RunAsync() { if (!user_gesture()) { error_ = keys::kGestureNeededForGenerateAppForLinkError; @@ -909,8 +789,8 @@ bool ManagementGenerateAppForLinkFunction::RunAsync() { GURL launch_url(params->url); if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { - error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, - params->url); + error_ = + ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, params->url); return false; } @@ -919,23 +799,17 @@ bool ManagementGenerateAppForLinkFunction::RunAsync() { return false; } - FaviconService* favicon_service = - FaviconServiceFactory::GetForProfile(GetProfile(), - Profile::EXPLICIT_ACCESS); - DCHECK(favicon_service); - - title_ = params->title; - launch_url_ = launch_url; + app_for_link_delegate_ = + ManagementAPI::GetFactoryInstance() + ->Get(browser_context()) + ->GetDelegate() + ->GenerateAppForLinkFunctionDelegate(this, browser_context(), + params->title, launch_url); - favicon_service->GetFaviconImageForPageURL( - launch_url, - base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), - &cancelable_task_tracker_); - - // Matched with a Release() in OnExtensionLoaded(). + // Matched with a Release() in FinishCreateBookmarkApp(). AddRef(); - // Response is sent async in OnExtensionLoaded(). + // Response is sent async in FinishCreateBookmarkApp(). return true; } @@ -944,7 +818,8 @@ ManagementEventRouter::ManagementEventRouter(content::BrowserContext* context) extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); } -ManagementEventRouter::~ManagementEventRouter() {} +ManagementEventRouter::~ManagementEventRouter() { +} void ManagementEventRouter::OnExtensionLoaded( content::BrowserContext* browser_context, @@ -975,14 +850,14 @@ void ManagementEventRouter::OnExtensionUninstalled( void ManagementEventRouter::BroadcastEvent(const Extension* extension, const char* event_name) { - if (ui_util::ShouldNotBeVisible(extension, browser_context_)) + if (ShouldNotBeVisible(extension, browser_context_)) return; // Don't dispatch events for built-in extenions. scoped_ptr<base::ListValue> args(new base::ListValue()); if (event_name == management::OnUninstalled::kEventName) { args->Append(new base::StringValue(extension->id())); } else { scoped_ptr<management::ExtensionInfo> info = - CreateExtensionInfo(*extension, ExtensionSystem::Get(browser_context_)); + CreateExtensionInfo(*extension, browser_context_); args->Append(info->ToValue().release()); } @@ -991,7 +866,8 @@ void ManagementEventRouter::BroadcastEvent(const Extension* extension, } ManagementAPI::ManagementAPI(content::BrowserContext* context) - : browser_context_(context) { + : browser_context_(context), + delegate_(ExtensionsAPIClient::Get()->CreateManagementAPIDelegate()) { EventRouter* event_router = EventRouter::Get(browser_context_); event_router->RegisterObserver(this, management::OnInstalled::kEventName); event_router->RegisterObserver(this, management::OnUninstalled::kEventName); @@ -1006,7 +882,7 @@ void ManagementAPI::Shutdown() { EventRouter::Get(browser_context_)->UnregisterObserver(this); } -static base::LazyInstance<BrowserContextKeyedAPIFactory<ManagementAPI> > +static base::LazyInstance<BrowserContextKeyedAPIFactory<ManagementAPI>> g_factory = LAZY_INSTANCE_INITIALIZER; // static diff --git a/chrome/browser/extensions/api/management/management_api.h b/extensions/browser/api/management/management_api.h index 93133f9..d12903b 100644 --- a/chrome/browser/extensions/api/management/management_api.h +++ b/extensions/browser/api/management/management_api.h @@ -2,41 +2,33 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ -#define CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ +#ifndef EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_ +#define EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_ #include "base/compiler_specific.h" #include "base/scoped_observer.h" -#include "base/task/cancelable_task_tracker.h" -#include "chrome/browser/extensions/bookmark_app_helper.h" -#include "chrome/browser/extensions/chrome_extension_function.h" -#include "chrome/browser/extensions/extension_install_prompt.h" -#include "chrome/browser/extensions/extension_uninstall_dialog.h" -#include "chrome/common/web_application_info.h" -#include "components/favicon_base/favicon_types.h" #include "components/keyed_service/core/keyed_service.h" +#include "extensions/browser/api/management/management_api_delegate.h" #include "extensions/browser/browser_context_keyed_api_factory.h" #include "extensions/browser/event_router.h" +#include "extensions/browser/extension_function.h" #include "extensions/browser/extension_registry_observer.h" -class ExtensionService; +class ExtensionRegistry; class ExtensionUninstallDialog; +struct WebApplicationInfo; namespace extensions { class ExtensionRegistry; -class ManagementFunction : public ChromeSyncExtensionFunction { +class ManagementFunction : public SyncExtensionFunction { protected: ~ManagementFunction() override {} - - ExtensionService* service(); }; -class AsyncManagementFunction : public ChromeAsyncExtensionFunction { +class AsyncManagementFunction : public AsyncExtensionFunction { protected: ~AsyncManagementFunction() override {} - - ExtensionService* service(); }; class ManagementGetAllFunction : public ManagementFunction { @@ -85,11 +77,10 @@ class ManagementGetPermissionWarningsByIdFunction : public ManagementFunction { }; class ManagementGetPermissionWarningsByManifestFunction - : public ChromeAsyncExtensionFunction { + : public AsyncExtensionFunction { public: - DECLARE_EXTENSION_FUNCTION( - "management.getPermissionWarningsByManifest", - MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST); + DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsByManifest", + MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST); // Called when utility process finishes. void OnParseSuccess(scoped_ptr<base::DictionaryValue> parsed_manifest); @@ -113,54 +104,50 @@ class ManagementLaunchAppFunction : public ManagementFunction { bool RunSync() override; }; -class ManagementSetEnabledFunction : public AsyncManagementFunction, - public ExtensionInstallPrompt::Delegate { +class ManagementSetEnabledFunction : public AsyncManagementFunction { public: DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED) ManagementSetEnabledFunction(); + void InstallUIProceed(); + void InstallUIAbort(bool user_initiated); + protected: ~ManagementSetEnabledFunction() override; // ExtensionFunction: bool RunAsync() override; - // ExtensionInstallPrompt::Delegate. - void InstallUIProceed() override; - void InstallUIAbort(bool user_initiated) override; - private: std::string extension_id_; - // Used for prompting to re-enable items with permissions escalation updates. - scoped_ptr<ExtensionInstallPrompt> install_prompt_; + scoped_ptr<InstallPromptDelegate> install_prompt_; }; -class ManagementUninstallFunctionBase : public AsyncManagementFunction, - public ExtensionUninstallDialog::Delegate { +class ManagementUninstallFunctionBase : public AsyncManagementFunction { public: ManagementUninstallFunctionBase(); static void SetAutoConfirmForTest(bool should_proceed); - // ExtensionUninstallDialog::Delegate implementation. - void ExtensionUninstallAccepted() override; - void ExtensionUninstallCanceled() override; + void ExtensionUninstallAccepted(); + void ExtensionUninstallCanceled(); protected: ~ManagementUninstallFunctionBase() override; bool Uninstall(const std::string& extension_id, bool show_confirm_dialog); - private: + private: // If should_uninstall is true, this method does the actual uninstall. // If |show_uninstall_dialog|, then this function will be called by one of the // Accepted/Canceled callbacks. Otherwise, it's called directly from RunAsync. void Finish(bool should_uninstall); std::string extension_id_; - scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_; + + scoped_ptr<UninstallDialogDelegate> uninstall_dialog_; }; class ManagementUninstallFunction : public ManagementUninstallFunctionBase { @@ -178,7 +165,7 @@ class ManagementUninstallFunction : public ManagementUninstallFunctionBase { class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase { public: DECLARE_EXTENSION_FUNCTION("management.uninstallSelf", - MANAGEMENT_UNINSTALLSELF); + MANAGEMENT_UNINSTALLSELF); ManagementUninstallSelfFunction(); @@ -191,7 +178,7 @@ class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase { class ManagementCreateAppShortcutFunction : public AsyncManagementFunction { public: DECLARE_EXTENSION_FUNCTION("management.createAppShortcut", - MANAGEMENT_CREATEAPPSHORTCUT); + MANAGEMENT_CREATEAPPSHORTCUT); ManagementCreateAppShortcutFunction(); @@ -208,7 +195,7 @@ class ManagementCreateAppShortcutFunction : public AsyncManagementFunction { class ManagementSetLaunchTypeFunction : public ManagementFunction { public: DECLARE_EXTENSION_FUNCTION("management.setLaunchType", - MANAGEMENT_SETLAUNCHTYPE); + MANAGEMENT_SETLAUNCHTYPE); protected: ~ManagementSetLaunchTypeFunction() override {} @@ -219,27 +206,20 @@ class ManagementSetLaunchTypeFunction : public ManagementFunction { class ManagementGenerateAppForLinkFunction : public AsyncManagementFunction { public: DECLARE_EXTENSION_FUNCTION("management.generateAppForLink", - MANAGEMENT_GENERATEAPPFORLINK); + MANAGEMENT_GENERATEAPPFORLINK); ManagementGenerateAppForLinkFunction(); + void FinishCreateBookmarkApp(const Extension* extension, + const WebApplicationInfo& web_app_info); + protected: ~ManagementGenerateAppForLinkFunction() override; bool RunAsync() override; private: - void OnFaviconForApp(const favicon_base::FaviconImageResult& image_result); - void FinishCreateBookmarkApp(const Extension* extension, - const WebApplicationInfo& web_app_info); - - std::string title_; - GURL launch_url_; - - scoped_ptr<BookmarkAppHelper> bookmark_app_helper_; - - // Used for favicon loading tasks. - base::CancelableTaskTracker cancelable_task_tracker_; + scoped_ptr<AppForLinkDelegate> app_for_link_delegate_; }; class ManagementEventRouter : public ExtensionRegistryObserver { @@ -287,23 +267,26 @@ class ManagementAPI : public BrowserContextKeyedAPI, // EventRouter::Observer implementation. void OnListenerAdded(const EventListenerInfo& details) override; + // Returns the ManagementAPI delegate. + const ManagementAPIDelegate* GetDelegate() const { return delegate_.get(); } + private: friend class BrowserContextKeyedAPIFactory<ManagementAPI>; content::BrowserContext* browser_context_; // BrowserContextKeyedAPI implementation. - static const char* service_name() { - return "ManagementAPI"; - } + static const char* service_name() { return "ManagementAPI"; } static const bool kServiceIsNULLWhileTesting = true; // Created lazily upon OnListenerAdded. scoped_ptr<ManagementEventRouter> management_event_router_; + scoped_ptr<ManagementAPIDelegate> delegate_; + DISALLOW_COPY_AND_ASSIGN(ManagementAPI); }; } // namespace extensions -#endif // CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ +#endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_ diff --git a/chrome/browser/extensions/api/management/management_api_constants.cc b/extensions/browser/api/management/management_api_constants.cc index bb2e0cb..f4fb8ff 100644 --- a/chrome/browser/extensions/api/management/management_api_constants.cc +++ b/extensions/browser/api/management/management_api_constants.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/extensions/api/management/management_api_constants.h" +#include "extensions/browser/api/management/management_api_constants.h" namespace extension_management_api_constants { diff --git a/chrome/browser/extensions/api/management/management_api_constants.h b/extensions/browser/api/management/management_api_constants.h index 4592231..faadae1 100644 --- a/chrome/browser/extensions/api/management/management_api_constants.h +++ b/extensions/browser/api/management/management_api_constants.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_CONSTANTS_H_ -#define CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_CONSTANTS_H_ +#ifndef EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_CONSTANTS_H_ +#define EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_CONSTANTS_H_ namespace extension_management_api_constants { @@ -34,7 +34,6 @@ extern const char kInvalidURLError[]; extern const char kEmptyTitleError[]; extern const char kGenerateAppForLinkInstallError[]; - } // namespace extension_management_api_constants -#endif // CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_CONSTANTS_H_ +#endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_CONSTANTS_H_ diff --git a/extensions/browser/api/management/management_api_delegate.h b/extensions/browser/api/management/management_api_delegate.h new file mode 100644 index 0000000..c81ef09 --- /dev/null +++ b/extensions/browser/api/management/management_api_delegate.h @@ -0,0 +1,128 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_DELEGATER_H_ +#define EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_DELEGATER_H_ + +#include "base/callback.h" +#include "extensions/browser/uninstall_reason.h" +#include "extensions/common/constants.h" +#include "extensions/common/extension.h" +#include "extensions/common/extension_icon_set.h" +#include "url/gurl.h" + +namespace content { +class BrowserContext; +} // namespace content + +namespace extensions { + +class Extension; +class ExtensionPrefs; +class ManagementCreateAppShortcutFunction; +class ManagementGenerateAppForLinkFunction; +class ManagementGetPermissionWarningsByManifestFunction; +class ManagementSetEnabledFunction; +class ManagementUninstallFunctionBase; + +// Manages the lifetime of the install prompt. +class InstallPromptDelegate { + public: + virtual ~InstallPromptDelegate() {} +}; + +// Manages the lifetime of the uninstall prompt. +class UninstallDialogDelegate { + public: + virtual ~UninstallDialogDelegate() {} +}; + +// Manages the lifetime of the bookmark app creation. +class AppForLinkDelegate { + public: + virtual ~AppForLinkDelegate() {} +}; + +class ManagementAPIDelegate { + public: + virtual ~ManagementAPIDelegate() {} + + // Launches the app |extension|. Returns true on success. + virtual bool LaunchAppFunctionDelegate( + const Extension* extension, + content::BrowserContext* context) const = 0; + + // Forwards the call to extensions::util::IsStreamlinedHostedAppsEnabled in + // chrome. + virtual bool IsStreamlinedHostedAppsEnabled() const = 0; + + // Forwards the call to AppLaunchInfo::GetFullLaunchURL in chrome. + virtual GURL GetFullLaunchURL(const Extension* extension) const = 0; + + // Forwards the call to launch_util::GetLaunchType in chrome. + virtual LaunchType GetLaunchType(const ExtensionPrefs* prefs, + const Extension* extension) const = 0; + + // Parses the manifest and calls back the + // ManagementGetPermissionWarningsByManifestFunction. + virtual void GetPermissionWarningsByManifestFunctionDelegate( + ManagementGetPermissionWarningsByManifestFunction* function, + const std::string& manifest_str) const = 0; + + // Used to show a dialog prompt in chrome when management.setEnabled extension + // function is called. + virtual scoped_ptr<InstallPromptDelegate> SetEnabledFunctionDelegate( + ManagementSetEnabledFunction* function, + const Extension* extension) const = 0; + + // Enables the extension identified by |extension_id|. + virtual void EnableExtension(content::BrowserContext* context, + const std::string& extension_id) const = 0; + + // Disables the extension identified by |extension_id|. + virtual void DisableExtension( + content::BrowserContext* context, + const std::string& extension_id, + Extension::DisableReason disable_reason) const = 0; + + // Used to show a confirmation dialog when uninstalling |target_extension_id|. + virtual scoped_ptr<UninstallDialogDelegate> UninstallFunctionDelegate( + ManagementUninstallFunctionBase* function, + const std::string& target_extension_id) const = 0; + + // Uninstalls the extension. + virtual bool UninstallExtension(content::BrowserContext* context, + const std::string& transient_extension_id, + UninstallReason reason, + const base::Closure& deletion_done_callback, + base::string16* error) const = 0; + + // Creates an app shortcut. + virtual bool CreateAppShortcutFunctionDelegate( + ManagementCreateAppShortcutFunction* function, + const Extension* extension) const = 0; + + // Forwards the call to launch_util::SetLaunchType in chrome. + virtual void SetLaunchType(content::BrowserContext* context, + const std::string& extension_id, + LaunchType launch_type) const = 0; + + // Creates a bookmark app for |launch_url|. + virtual scoped_ptr<AppForLinkDelegate> GenerateAppForLinkFunctionDelegate( + ManagementGenerateAppForLinkFunction* function, + content::BrowserContext* context, + const std::string& title, + const GURL& launch_url) const = 0; + + // Forwards the call to ExtensionIconSource::GetIconURL in chrome. + virtual GURL GetIconURL(const Extension* extension, + int icon_size, + ExtensionIconSet::MatchType match, + bool grayscale, + bool* exists) const = 0; +}; + +} // namespace extensions + +#endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_DELEGATER_H_ diff --git a/extensions/browser/api/runtime/runtime_apitest.cc b/extensions/browser/api/runtime/runtime_apitest.cc index 764e22e..7f01ef3 100644 --- a/extensions/browser/api/runtime/runtime_apitest.cc +++ b/extensions/browser/api/runtime/runtime_apitest.cc @@ -3,12 +3,12 @@ // found in the LICENSE file. #include "chrome/browser/apps/app_browsertest_util.h" -#include "chrome/browser/extensions/api/management/management_api.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_function_test_utils.h" #include "chrome/browser/extensions/test_extension_dir.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/notification_service.h" +#include "extensions/browser/api/management/management_api.h" #include "extensions/browser/api/runtime/runtime_api.h" #include "extensions/browser/extension_registry.h" #include "extensions/test/result_catcher.h" diff --git a/extensions/browser/browser_context_keyed_service_factories.cc b/extensions/browser/browser_context_keyed_service_factories.cc index 0f97641..e6cd44d 100644 --- a/extensions/browser/browser_context_keyed_service_factories.cc +++ b/extensions/browser/browser_context_keyed_service_factories.cc @@ -5,6 +5,7 @@ #include "extensions/browser/browser_context_keyed_service_factories.h" #include "extensions/browser/api/api_resource_manager.h" +#include "extensions/browser/api/management/management_api.h" #include "extensions/browser/api/runtime/runtime_api.h" #include "extensions/browser/api/serial/serial_connection.h" #include "extensions/browser/api/socket/socket.h" @@ -31,6 +32,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() { core_api::TCPServerSocketEventDispatcher::GetFactoryInstance(); core_api::TCPSocketEventDispatcher::GetFactoryInstance(); core_api::UDPSocketEventDispatcher::GetFactoryInstance(); + extensions::ManagementAPI::GetFactoryInstance(); ExtensionPrefsFactory::GetInstance(); ProcessManagerFactory::GetInstance(); RendererStartupHelperFactory::GetInstance(); diff --git a/extensions/common/api/_api_features.json b/extensions/common/api/_api_features.json index cf92ef8..5cb571e 100644 --- a/extensions/common/api/_api_features.json +++ b/extensions/common/api/_api_features.json @@ -145,6 +145,25 @@ "dependencies": ["permission:hid"], "contexts": ["blessed_extension"] }, + "management": { + "dependencies": ["permission:management"], + "contexts": ["blessed_extension"] + }, + "management.getPermissionWarningsByManifest": { + "dependencies": [], + "channel": "stable", + "extension_types": ["extension", "legacy_packaged_app", "platform_app"] + }, + "management.getSelf": { + "dependencies": [], + "channel": "stable", + "extension_types": ["extension", "legacy_packaged_app", "platform_app"] + }, + "management.uninstallSelf": { + "dependencies": [], + "channel": "stable", + "extension_types": ["extension", "legacy_packaged_app", "platform_app"] + }, "power": { "dependencies": ["permission:power"], "contexts": ["blessed_extension"] diff --git a/chrome/common/extensions/api/management.json b/extensions/common/api/management.json index 4297638..4297638 100644 --- a/chrome/common/extensions/api/management.json +++ b/extensions/common/api/management.json diff --git a/extensions/common/api/schemas.gypi b/extensions/common/api/schemas.gypi index b877c3a..3669cdd 100644 --- a/extensions/common/api/schemas.gypi +++ b/extensions/common/api/schemas.gypi @@ -25,6 +25,7 @@ 'extension_options_internal.idl', 'extension_types.json', 'guest_view_internal.json', + 'management.json', 'hid.idl', 'mime_handler_view_guest_internal.json', 'power.idl', diff --git a/extensions/common/constants.h b/extensions/common/constants.h index bd44686..25d0be2 100644 --- a/extensions/common/constants.h +++ b/extensions/common/constants.h @@ -129,6 +129,36 @@ enum AppLaunchSource { NUM_APP_LAUNCH_SOURCES }; +// This enum is used for the launch type the user wants to use for an +// application. +// Do not remove items or re-order this enum as it is used in preferences +// and histograms. +enum LaunchType { + LAUNCH_TYPE_INVALID = -1, + LAUNCH_TYPE_FIRST = 0, + LAUNCH_TYPE_PINNED = LAUNCH_TYPE_FIRST, + LAUNCH_TYPE_REGULAR = 1, + LAUNCH_TYPE_FULLSCREEN = 2, + LAUNCH_TYPE_WINDOW = 3, + NUM_LAUNCH_TYPES, + + // Launch an app in the in the way a click on the NTP would, + // if no user pref were set. Update this constant to change + // the default for the NTP and chrome.management.launchApp(). + LAUNCH_TYPE_DEFAULT = LAUNCH_TYPE_REGULAR +}; + +// Don't remove items or change the order of this enum. It's used in +// histograms and preferences. +enum LaunchContainer { + LAUNCH_CONTAINER_WINDOW, + LAUNCH_CONTAINER_PANEL, + LAUNCH_CONTAINER_TAB, + // For platform apps, which don't actually have a container (they just get a + // "onLaunched" event). + LAUNCH_CONTAINER_NONE +}; + } // namespace extensions namespace extension_misc { diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index a3e48ea..2014048 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -325,8 +325,6 @@ 'browser/api/app_view/app_view_guest_internal_api.h', 'browser/api/app_window/app_window_api.cc', 'browser/api/app_window/app_window_api.h', - 'browser/api/guest_view/guest_view_internal_api.cc', - 'browser/api/guest_view/guest_view_internal_api.h', 'browser/api/async_api_function.cc', 'browser/api/async_api_function.h', 'browser/api/bluetooth/bluetooth_api.cc', @@ -420,12 +418,19 @@ 'browser/api/execute_code_function.h', 'browser/api/extensions_api_client.cc', 'browser/api/extensions_api_client.h', + 'browser/api/guest_view/guest_view_internal_api.cc', + 'browser/api/guest_view/guest_view_internal_api.h', 'browser/api/hid/hid_api.cc', 'browser/api/hid/hid_api.h', 'browser/api/hid/hid_connection_resource.cc', 'browser/api/hid/hid_connection_resource.h', 'browser/api/hid/hid_device_manager.cc', 'browser/api/hid/hid_device_manager.h', + 'browser/api/management/management_api.cc', + 'browser/api/management/management_api.h', + 'browser/api/management/management_api_delegate.h', + 'browser/api/management/management_api_constants.cc', + 'browser/api/management/management_api_constants.h', 'browser/api/messaging/native_message_host.cc', 'browser/api/power/power_api.cc', 'browser/api/power/power_api.h', |