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"
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
{
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())) {
33 *error
= l10n_util::GetStringFUTF16(
34 IDS_EXTENSION_CANT_MODIFY_POLICY_REQUIRED
,
35 base::UTF8ToUTF16(extension
->name()));
41 bool ReturnLoadError(const extensions::Extension
* extension
,
42 base::string16
* error
) {
44 *error
= l10n_util::GetStringFUTF16(
45 IDS_EXTENSION_CANT_INSTALL_POLICY_BLOCKED
,
46 base::UTF8ToUTF16(extension
->name()),
47 base::UTF8ToUTF16(extension
->id()));
54 StandardManagementPolicyProvider::StandardManagementPolicyProvider(
55 const ExtensionManagement
* settings
)
56 : settings_(settings
) {
59 StandardManagementPolicyProvider::~StandardManagementPolicyProvider() {
63 StandardManagementPolicyProvider::GetDebugPolicyProviderName() const {
68 return "extension management policy controlled settings";
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()))
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())
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
:
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
);
115 case Manifest::NUM_LOAD_TYPES
:
119 if (installation_mode
== ExtensionManagement::INSTALLATION_BLOCKED
)
120 return ReturnLoadError(extension
, error
);
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
)) {
148 *reason
= Extension::DISABLE_UPDATE_REQUIRED_BY_POLICY
;
150 *error
= l10n_util::GetStringFUTF16(
151 IDS_EXTENSION_DISABLED_UPDATE_REQUIRED_BY_POLICY
,
152 base::UTF8ToUTF16(extension
->name()),
153 base::ASCIIToUTF16(required_version
));
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
168 if (mode
== ExtensionManagement::INSTALLATION_FORCED
||
169 mode
== ExtensionManagement::INSTALLATION_RECOMMENDED
) {
171 *error
= l10n_util::GetStringFUTF16(
172 IDS_EXTENSION_CANT_UNINSTALL_POLICY_REQUIRED
,
173 base::UTF8ToUTF16(extension
->name()));
180 } // namespace extensions