Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / extensions / standard_management_policy_provider.cc
blob379778ba3edc11e7978cbc5ec341d986da093660
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/standard_management_policy_provider.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/extensions/extension_management.h"
13 #include "chrome/browser/extensions/external_component_loader.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/manifest.h"
16 #include "grit/extensions_strings.h"
17 #include "ui/base/l10n/l10n_util.h"
19 namespace extensions {
21 namespace {
23 // Returns whether the extension can be modified under admin policy or not, and
24 // fills |error| with corresponding error message if necessary.
25 bool AdminPolicyIsModifiable(const extensions::Extension* extension,
26 base::string16* error) {
27 if (!extensions::Manifest::IsComponentLocation(extension->location()) &&
28 !extensions::Manifest::IsPolicyLocation(extension->location())) {
29 return true;
32 if (error) {
33 *error = l10n_util::GetStringFUTF16(
34 IDS_EXTENSION_CANT_MODIFY_POLICY_REQUIRED,
35 base::UTF8ToUTF16(extension->name()));
38 return false;
41 bool ReturnLoadError(const extensions::Extension* extension,
42 base::string16* error) {
43 if (error) {
44 *error = l10n_util::GetStringFUTF16(
45 IDS_EXTENSION_CANT_INSTALL_POLICY_BLOCKED,
46 base::UTF8ToUTF16(extension->name()),
47 base::UTF8ToUTF16(extension->id()));
49 return false;
52 } // namespace
54 StandardManagementPolicyProvider::StandardManagementPolicyProvider(
55 const ExtensionManagement* settings)
56 : settings_(settings) {
59 StandardManagementPolicyProvider::~StandardManagementPolicyProvider() {
62 std::string
63 StandardManagementPolicyProvider::GetDebugPolicyProviderName() const {
64 #ifdef NDEBUG
65 NOTREACHED();
66 return std::string();
67 #else
68 return "extension management policy controlled settings";
69 #endif
72 bool StandardManagementPolicyProvider::UserMayLoad(
73 const Extension* extension,
74 base::string16* error) const {
75 // Component extensions are always allowed.
76 if (Manifest::IsComponentLocation(extension->location()))
77 return true;
79 // Shared modules are always allowed too: they only contain resources that
80 // are used by other extensions. The extension that depends on the shared
81 // module may be filtered by policy.
82 if (extension->is_shared_module())
83 return true;
85 ExtensionManagement::InstallationMode installation_mode =
86 settings_->GetInstallationMode(extension);
88 // Force-installed extensions cannot be overwritten manually.
89 if (!Manifest::IsPolicyLocation(extension->location()) &&
90 installation_mode == ExtensionManagement::INSTALLATION_FORCED) {
91 return ReturnLoadError(extension, error);
94 // Check whether the extension type is allowed.
96 // If you get a compile error here saying that the type you added is not
97 // handled by the switch statement below, please consider whether enterprise
98 // policy should be able to disallow extensions of the new type. If so, add
99 // a branch to the second block and add a line to the definition of
100 // kAllowedTypesMap in extension_management_constants.h.
101 switch (extension->GetType()) {
102 case Manifest::TYPE_UNKNOWN:
103 break;
104 case Manifest::TYPE_EXTENSION:
105 case Manifest::TYPE_THEME:
106 case Manifest::TYPE_USER_SCRIPT:
107 case Manifest::TYPE_HOSTED_APP:
108 case Manifest::TYPE_LEGACY_PACKAGED_APP:
109 case Manifest::TYPE_PLATFORM_APP:
110 case Manifest::TYPE_SHARED_MODULE: {
111 if (!settings_->IsAllowedManifestType(extension->GetType()))
112 return ReturnLoadError(extension, error);
113 break;
115 case Manifest::NUM_LOAD_TYPES:
116 NOTREACHED();
119 if (installation_mode == ExtensionManagement::INSTALLATION_BLOCKED)
120 return ReturnLoadError(extension, error);
122 return true;
125 bool StandardManagementPolicyProvider::UserMayModifySettings(
126 const Extension* extension,
127 base::string16* error) const {
128 return AdminPolicyIsModifiable(extension, error) ||
129 (extension->location() == extensions::Manifest::EXTERNAL_COMPONENT &&
130 ExternalComponentLoader::IsModifiable(extension));
133 bool StandardManagementPolicyProvider::MustRemainEnabled(
134 const Extension* extension,
135 base::string16* error) const {
136 return !AdminPolicyIsModifiable(extension, error) ||
137 (extension->location() == extensions::Manifest::EXTERNAL_COMPONENT &&
138 ExternalComponentLoader::IsModifiable(extension));
141 bool StandardManagementPolicyProvider::MustRemainDisabled(
142 const Extension* extension,
143 Extension::DisableReason* reason,
144 base::string16* error) const {
145 std::string required_version;
146 if (!settings_->CheckMinimumVersion(extension, &required_version)) {
147 if (reason)
148 *reason = Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY;
149 if (error) {
150 *error = l10n_util::GetStringFUTF16(
151 IDS_EXTENSION_DISABLED_UPDATE_REQUIRED_BY_POLICY,
152 base::UTF8ToUTF16(extension->name()),
153 base::ASCIIToUTF16(required_version));
155 return true;
157 return false;
160 bool StandardManagementPolicyProvider::MustRemainInstalled(
161 const Extension* extension,
162 base::string16* error) const {
163 ExtensionManagement::InstallationMode mode =
164 settings_->GetInstallationMode(extension);
165 // Disallow removing of recommended extension, to avoid re-install it
166 // again while policy is reload. But disabling of recommended extension is
167 // allowed.
168 if (mode == ExtensionManagement::INSTALLATION_FORCED ||
169 mode == ExtensionManagement::INSTALLATION_RECOMMENDED) {
170 if (error) {
171 *error = l10n_util::GetStringFUTF16(
172 IDS_EXTENSION_CANT_UNINSTALL_POLICY_REQUIRED,
173 base::UTF8ToUTF16(extension->name()));
175 return true;
177 return false;
180 } // namespace extensions