diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-26 17:43:28 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-26 17:43:28 +0000 |
commit | cffd78950f9e8e740085dbec9ecec7472e6eb98d (patch) | |
tree | 33820b7d9817b837cabe852f669f1faaf1bacfa9 /chrome/browser/extensions | |
parent | 3bb8499b2cd51027ee896ccda306e2c55fc2c5f0 (diff) | |
download | chromium_src-cffd78950f9e8e740085dbec9ecec7472e6eb98d.zip chromium_src-cffd78950f9e8e740085dbec9ecec7472e6eb98d.tar.gz chromium_src-cffd78950f9e8e740085dbec9ecec7472e6eb98d.tar.bz2 |
First cut at experimental API for managing installed/enabled extensions.
The documented methods should all be working, but events are not hooked up yet.
BUG=51178
TEST=none
Review URL: http://codereview.chromium.org/3200012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57544 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
5 files changed, 228 insertions, 2 deletions
diff --git a/chrome/browser/extensions/extension_apitest.cc b/chrome/browser/extensions/extension_apitest.cc index 6039c45..ec99224 100644 --- a/chrome/browser/extensions/extension_apitest.cc +++ b/chrome/browser/extensions/extension_apitest.cc @@ -84,9 +84,11 @@ bool ExtensionApiTest::RunExtensionTestImpl(const char* extension_name, // If there is a subtest to load, navigate to the subtest page. if (!subtest_page.empty()) { - Extension* extension = GetSingleLoadedExtension(); + ExtensionsService* service = browser()->profile()->GetExtensionsService(); + Extension* extension = + service->GetExtensionById(last_loaded_extension_id_, false); if (!extension) - return false; // message_ was set by GetSingleLoadedExtension(). + return false; GURL url = extension->GetResourceURL(subtest_page); LOG(ERROR) << "Loading subtest page url: " << url.spec(); diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 836c46c..51cc8fe 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -33,6 +33,7 @@ #if defined(TOOLKIT_VIEWS) #include "chrome/browser/extensions/extension_input_api.h" #endif +#include "chrome/browser/extensions/extension_management_api.h" #include "chrome/browser/extensions/extension_message_service.h" #include "chrome/browser/extensions/extension_metrics_module.h" #include "chrome/browser/extensions/extension_omnibox_api.h" @@ -272,6 +273,12 @@ void FactoryRegistry::ResetFunctions() { // Input. RegisterFunction<SendKeyboardEventInputFunction>(); #endif + + // Management. + RegisterFunction<GetAllExtensionsFunction>(); + RegisterFunction<SetEnabledFunction>(); + RegisterFunction<InstallFunction>(); + RegisterFunction<UninstallFunction>(); } void FactoryRegistry::GetAllNames(std::vector<std::string>* names) { diff --git a/chrome/browser/extensions/extension_management_api.cc b/chrome/browser/extensions/extension_management_api.cc new file mode 100644 index 0000000..6be0476 --- /dev/null +++ b/chrome/browser/extensions/extension_management_api.cc @@ -0,0 +1,130 @@ +// Copyright (c) 2010 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/extension_management_api.h" + +#include <map> +#include <string> + +#include "base/string_number_conversions.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/extensions/extensions_service.h" +#include "chrome/common/extensions/extension_error_utils.h" + +using base::IntToString; + +const char kAppLaunchUrlKey[] = "appLaunchUrl"; +const char kEnabledKey[] = "enabled"; +const char kIconsKey[] = "icons"; +const char kIdKey[] = "id"; +const char kIsAppKey[] = "isApp"; +const char kNameKey[] = "name"; +const char kOptionsUrlKey[] = "optionsUrl"; +const char kSizeKey[] = "size"; +const char kUrlKey[] = "url"; + +const char kNoExtensionError[] = "No extension with id *"; + + +ExtensionsService* ExtensionManagementFunction::service() { + return profile()->GetExtensionsService(); +} + +static DictionaryValue* CreateExtensionInfo(const Extension& extension, + bool enabled) { + DictionaryValue* info = new DictionaryValue(); + info->SetString(kIdKey, extension.id()); + info->SetBoolean(kIsAppKey, extension.is_app()); + info->SetString(kNameKey, extension.name()); + info->SetBoolean(kEnabledKey, enabled); + if (!extension.options_url().is_empty()) + info->SetString(kOptionsUrlKey, + extension.options_url().possibly_invalid_spec()); + if (extension.is_app()) + info->SetString(kAppLaunchUrlKey, + extension.GetFullLaunchURL().possibly_invalid_spec()); + + const std::map<int, std::string>& icons = extension.icons(); + if (!icons.empty()) { + ListValue* icon_list = new ListValue(); + std::map<int, std::string>::const_iterator icon_iter; + for (icon_iter = icons.begin(); icon_iter != icons.end(); ++icon_iter) { + DictionaryValue* icon_info = new DictionaryValue(); + GURL url = extension.GetResourceURL(icon_iter->second); + icon_info->SetInteger(kSizeKey, icon_iter->first); + icon_info->SetString(kUrlKey, url.possibly_invalid_spec()); + icon_list->Append(icon_info); + } + info->Set("icons", icon_list); + } + + return info; +} + +static void AddExtensionInfo(ListValue* list, + const ExtensionList& extensions, + bool enabled) { + for (ExtensionList::const_iterator i = extensions.begin(); + i != extensions.end(); ++i) { + const Extension& extension = **i; + + if (extension.location() == Extension::COMPONENT) + continue; // Skip built-in extensions. + + list->Append(CreateExtensionInfo(extension, enabled)); + } +} + +bool GetAllExtensionsFunction::RunImpl() { + ListValue* result = new ListValue(); + result_.reset(result); + + AddExtensionInfo(result, *service()->extensions(), true); + AddExtensionInfo(result, *service()->disabled_extensions(), false); + + return true; +} + +bool SetEnabledFunction::RunImpl() { + std::string extension_id; + bool enable; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id)); + EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &enable)); + + if (!service()->GetExtensionById(extension_id, true)) { + error_ = ExtensionErrorUtils::FormatErrorMessage( + kNoExtensionError, extension_id); + return false; + } + + ExtensionPrefs* prefs = service()->extension_prefs(); + Extension::State state = prefs->GetExtensionState(extension_id); + + if (state == Extension::DISABLED && enable) { + service()->EnableExtension(extension_id); + } else if (state == Extension::ENABLED && !enable) { + service()->DisableExtension(extension_id); + } + + return true; +} + +bool InstallFunction::RunImpl() { + NOTIMPLEMENTED(); + return false; +} + +bool UninstallFunction::RunImpl() { + std::string extension_id; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id)); + + if (!service()->GetExtensionById(extension_id, true)) { + error_ = ExtensionErrorUtils::FormatErrorMessage( + kNoExtensionError, extension_id); + return false; + } + + service()->UninstallExtension(extension_id, false /* external_uninstall */); + return true; +} diff --git a/chrome/browser/extensions/extension_management_api.h b/chrome/browser/extensions/extension_management_api.h new file mode 100644 index 0000000..c32c833 --- /dev/null +++ b/chrome/browser/extensions/extension_management_api.h @@ -0,0 +1,42 @@ +// Copyright (c) 2010 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_EXTENSION_MANAGEMENT_API_H__ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_API_H__ +#pragma once + +#include "chrome/browser/extensions/extension_function.h" + +class ExtensionsService; + +class ExtensionManagementFunction : public SyncExtensionFunction { + protected: + ExtensionsService* service(); +}; + +class GetAllExtensionsFunction : public ExtensionManagementFunction { + ~GetAllExtensionsFunction() {} + virtual bool RunImpl(); + DECLARE_EXTENSION_FUNCTION_NAME("experimental.management.getAll"); +}; + +class SetEnabledFunction : public ExtensionManagementFunction { + ~SetEnabledFunction() {} + virtual bool RunImpl(); + DECLARE_EXTENSION_FUNCTION_NAME("experimental.management.setEnabled"); +}; + +class InstallFunction : public ExtensionManagementFunction { + ~InstallFunction() {} + virtual bool RunImpl(); + DECLARE_EXTENSION_FUNCTION_NAME("experimental.management.install"); +}; + +class UninstallFunction : public ExtensionManagementFunction { + ~UninstallFunction() {} + virtual bool RunImpl(); + DECLARE_EXTENSION_FUNCTION_NAME("experimental.management.uninstall"); +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_API_H__ diff --git a/chrome/browser/extensions/extension_management_apitest.cc b/chrome/browser/extensions/extension_management_apitest.cc new file mode 100644 index 0000000..8d734cf --- /dev/null +++ b/chrome/browser/extensions/extension_management_apitest.cc @@ -0,0 +1,45 @@ +// Copyright (c) 2010 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 "base/command_line.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/browser/extensions/extensions_service.h" +#include "chrome/browser/profile.h" +#include "chrome/common/chrome_switches.h" + +class ExtensionManagementApiTest : public ExtensionApiTest { + public: + + virtual void SetUpCommandLine(CommandLine* command_line) { + ExtensionApiTest::SetUpCommandLine(command_line); + command_line->AppendSwitch( + switches::kEnableExperimentalExtensionApis); + } + + virtual void InstallExtensions() { + FilePath basedir = test_data_dir_.AppendASCII("management"); + + // Load 2 enabled items. + ASSERT_TRUE(LoadExtension(basedir.AppendASCII("enabled_extension"))); + ASSERT_TRUE(LoadExtension(basedir.AppendASCII("enabled_app"))); + + // Load 2 disabled items. + ExtensionsService* service = browser()->profile()->GetExtensionsService(); + ASSERT_TRUE(LoadExtension(basedir.AppendASCII("disabled_extension"))); + service->DisableExtension(last_loaded_extension_id_); + ASSERT_TRUE(LoadExtension(basedir.AppendASCII("disabled_app"))); + service->DisableExtension(last_loaded_extension_id_); + } +}; + +IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Basics) { + InstallExtensions(); + ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html")); +} + +IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Uninstall) { + InstallExtensions(); + ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html")); +} |