Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / extensions / standard_management_policy_provider.cc
blob02ac0854827706237ce6abf66959c86fdfd87da9
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 ExtensionManagement::InstallationMode installation_mode =
80 settings_->GetInstallationMode(extension->id());
82 // Force-installed extensions cannot be overwritten manually.
83 if (!Manifest::IsPolicyLocation(extension->location()) &&
84 installation_mode == ExtensionManagement::INSTALLATION_FORCED) {
85 return ReturnLoadError(extension, error);
88 // Check whether the extension type is allowed.
90 // If you get a compile error here saying that the type you added is not
91 // handled by the switch statement below, please consider whether enterprise
92 // policy should be able to disallow extensions of the new type. If so, add
93 // a branch to the second block and add a line to the definition of
94 // kExtensionAllowedTypesMap in configuration_policy_handler_list.cc.
95 switch (extension->GetType()) {
96 case Manifest::TYPE_UNKNOWN:
97 break;
98 case Manifest::TYPE_EXTENSION:
99 case Manifest::TYPE_THEME:
100 case Manifest::TYPE_USER_SCRIPT:
101 case Manifest::TYPE_HOSTED_APP:
102 case Manifest::TYPE_LEGACY_PACKAGED_APP:
103 case Manifest::TYPE_PLATFORM_APP:
104 case Manifest::TYPE_SHARED_MODULE: {
105 if (!settings_->IsAllowedManifestType(extension->GetType()))
106 return ReturnLoadError(extension, error);
107 break;
109 case Manifest::NUM_LOAD_TYPES:
110 NOTREACHED();
113 if (installation_mode == ExtensionManagement::INSTALLATION_BLOCKED)
114 return ReturnLoadError(extension, error);
116 return true;
119 bool StandardManagementPolicyProvider::UserMayModifySettings(
120 const Extension* extension,
121 base::string16* error) const {
122 return AdminPolicyIsModifiable(extension, error) ||
123 (extension->location() == extensions::Manifest::EXTERNAL_COMPONENT &&
124 ExternalComponentLoader::IsModifiable(extension));
127 bool StandardManagementPolicyProvider::MustRemainEnabled(
128 const Extension* extension,
129 base::string16* error) const {
130 return !AdminPolicyIsModifiable(extension, error) ||
131 (extension->location() == extensions::Manifest::EXTERNAL_COMPONENT &&
132 ExternalComponentLoader::IsModifiable(extension));
135 bool StandardManagementPolicyProvider::MustRemainInstalled(
136 const Extension* extension,
137 base::string16* error) const {
138 ExtensionManagement::InstallationMode mode =
139 settings_->GetInstallationMode(extension->id());
140 // Disallow removing of recommended extension, to avoid re-install it
141 // again while policy is reload. But disabling of recommended extension is
142 // allowed.
143 if (mode == ExtensionManagement::INSTALLATION_FORCED ||
144 mode == ExtensionManagement::INSTALLATION_RECOMMENDED) {
145 if (error) {
146 *error = l10n_util::GetStringFUTF16(
147 IDS_EXTENSION_CANT_UNINSTALL_POLICY_REQUIRED,
148 base::UTF8ToUTF16(extension->name()));
150 return true;
152 return false;
155 } // namespace extensions