Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / standard_management_policy_provider.cc
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.
4
5 #include "chrome/browser/extensions/standard_management_policy_provider.h"
6
7 #include <string>
8
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"
18
19 namespace extensions {
20
21 namespace {
22
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;
30   }
31
32   if (error) {
33     *error = l10n_util::GetStringFUTF16(
34         IDS_EXTENSION_CANT_MODIFY_POLICY_REQUIRED,
35         base::UTF8ToUTF16(extension->name()));
36   }
37
38   return false;
39 }
40
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()));
48   }
49   return false;
50 }
51
52 }  // namespace
53
54 StandardManagementPolicyProvider::StandardManagementPolicyProvider(
55     const ExtensionManagement* settings)
56     : settings_(settings) {
57 }
58
59 StandardManagementPolicyProvider::~StandardManagementPolicyProvider() {
60 }
61
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
70 }
71
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;
78
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;
84
85   ExtensionManagement::InstallationMode installation_mode =
86       settings_->GetInstallationMode(extension);
87
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);
92   }
93
94   // Check whether the extension type is allowed.
95   //
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;
114     }
115     case Manifest::NUM_LOAD_TYPES:
116       NOTREACHED();
117   }
118
119   if (installation_mode == ExtensionManagement::INSTALLATION_BLOCKED)
120     return ReturnLoadError(extension, error);
121
122   return true;
123 }
124
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));
131 }
132
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));
139 }
140
141 bool StandardManagementPolicyProvider::MustRemainInstalled(
142     const Extension* extension,
143     base::string16* error) const {
144   ExtensionManagement::InstallationMode mode =
145       settings_->GetInstallationMode(extension);
146   // Disallow removing of recommended extension, to avoid re-install it
147   // again while policy is reload. But disabling of recommended extension is
148   // allowed.
149   if (mode == ExtensionManagement::INSTALLATION_FORCED ||
150       mode == ExtensionManagement::INSTALLATION_RECOMMENDED) {
151     if (error) {
152       *error = l10n_util::GetStringFUTF16(
153           IDS_EXTENSION_CANT_UNINSTALL_POLICY_REQUIRED,
154           base::UTF8ToUTF16(extension->name()));
155     }
156     return true;
157   }
158   return false;
159 }
160
161 }  // namespace extensions