blob: 6ab947d92f887b6348147cfc7a824d865186b83b [file] [log] [blame]
[email protected]1ff3fac2013-11-08 09:39:471// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]013724b2012-06-05 19:40:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1ff3fac2013-11-08 09:39:475#include "extensions/browser/admin_policy.h"
[email protected]013724b2012-06-05 19:40:396
[email protected]135cb802013-06-09 16:44:207#include "base/strings/utf_string_conversions.h"
[email protected]e4452d32013-11-15 23:07:418#include "extensions/common/extension.h"
[email protected]d42c11152013-08-22 19:36:329#include "extensions/common/manifest.h"
[email protected]99c0125b2014-04-17 05:21:5710#include "grit/extensions_strings.h"
[email protected]013724b2012-06-05 19:40:3911#include "ui/base/l10n/l10n_util.h"
12
13namespace {
14
[email protected]013724b2012-06-05 19:40:3915bool ManagementPolicyImpl(const extensions::Extension* extension,
[email protected]0d163dc2013-12-20 23:48:3616 base::string16* error,
[email protected]013724b2012-06-05 19:40:3917 bool modifiable_value) {
[email protected]75181f62014-03-19 21:41:1218 // Note that COMPONENT and EXTERNAL_COMPONENT are treated differently
19 // below. EXTERNAL_COMPONENT extensions can be modified including
20 // enabled, disabled, uninstalled while COMPONENT extensions cannot.
21 // However, those options are only available for EXTERNAL_COMPONENT
22 // extensions when the proper command line flag is passed.
[email protected]366a3b92012-12-14 14:25:1023 bool modifiable =
[email protected]75181f62014-03-19 21:41:1224 extension->location() != extensions::Manifest::COMPONENT &&
[email protected]9bb691632013-09-26 18:50:1225 !extensions::Manifest::IsPolicyLocation(extension->location());
[email protected]013724b2012-06-05 19:40:3926 // Some callers equate "no restriction" to true, others to false.
27 if (modifiable)
28 return modifiable_value;
29
30 if (error) {
31 *error = l10n_util::GetStringFUTF16(
32 IDS_EXTENSION_CANT_MODIFY_POLICY_REQUIRED,
[email protected]ad65a3e2013-12-25 18:18:0133 base::UTF8ToUTF16(extension->name()));
[email protected]013724b2012-06-05 19:40:3934 }
35 return !modifiable_value;
36}
37
[email protected]0d163dc2013-12-20 23:48:3638bool ReturnLoadError(const extensions::Extension* extension,
39 base::string16* error) {
[email protected]ce4e5d7a2012-12-11 20:59:2640 if (error) {
41 *error = l10n_util::GetStringFUTF16(
42 IDS_EXTENSION_CANT_INSTALL_POLICY_BLOCKED,
[email protected]ad65a3e2013-12-25 18:18:0143 base::UTF8ToUTF16(extension->name()),
44 base::UTF8ToUTF16(extension->id()));
[email protected]ce4e5d7a2012-12-11 20:59:2645 }
46 return false;
47}
48
[email protected]013724b2012-06-05 19:40:3949} // namespace
50
51namespace extensions {
52namespace admin_policy {
53
54bool BlacklistedByDefault(const base::ListValue* blacklist) {
55 base::StringValue wildcard("*");
56 return blacklist && blacklist->Find(wildcard) != blacklist->end();
57}
58
[email protected]695b5712012-12-06 23:55:2859bool UserMayLoad(const base::ListValue* blacklist,
[email protected]013724b2012-06-05 19:40:3960 const base::ListValue* whitelist,
[email protected]e410b5f2012-12-14 14:02:2461 const base::DictionaryValue* forcelist,
[email protected]ce4e5d7a2012-12-11 20:59:2662 const base::ListValue* allowed_types,
[email protected]013724b2012-06-05 19:40:3963 const Extension* extension,
[email protected]0d163dc2013-12-20 23:48:3664 base::string16* error) {
[email protected]366a3b92012-12-14 14:25:1065 // Component extensions are always allowed.
[email protected]1d5e58b2013-01-31 08:41:4066 if (extension->location() == Manifest::COMPONENT)
[email protected]013724b2012-06-05 19:40:3967 return true;
68
[email protected]ec1b3e712013-05-25 14:29:1669 // Forced installed extensions cannot be overwritten manually.
[email protected]9bb691632013-09-26 18:50:1270 if (extension->location() != Manifest::EXTERNAL_POLICY &&
71 extension->location() != Manifest::EXTERNAL_POLICY_DOWNLOAD &&
[email protected]ec1b3e712013-05-25 14:29:1672 forcelist && forcelist->HasKey(extension->id())) {
73 return ReturnLoadError(extension, error);
74 }
75
[email protected]ce4e5d7a2012-12-11 20:59:2676 // Early exit for the common case of no policy restrictions.
77 if ((!blacklist || blacklist->empty()) && (!allowed_types))
[email protected]013724b2012-06-05 19:40:3978 return true;
79
[email protected]ce4e5d7a2012-12-11 20:59:2680 // Check whether the extension type is allowed.
81 //
82 // If you get a compile error here saying that the type you added is not
83 // handled by the switch statement below, please consider whether enterprise
84 // policy should be able to disallow extensions of the new type. If so, add a
85 // branch to the second block and add a line to the definition of
86 // kExtensionAllowedTypesMap in configuration_policy_handler_list.cc.
87 switch (extension->GetType()) {
[email protected]1d5e58b2013-01-31 08:41:4088 case Manifest::TYPE_UNKNOWN:
[email protected]ce4e5d7a2012-12-11 20:59:2689 break;
[email protected]1d5e58b2013-01-31 08:41:4090 case Manifest::TYPE_EXTENSION:
91 case Manifest::TYPE_THEME:
92 case Manifest::TYPE_USER_SCRIPT:
93 case Manifest::TYPE_HOSTED_APP:
94 case Manifest::TYPE_LEGACY_PACKAGED_APP:
95 case Manifest::TYPE_PLATFORM_APP:
[email protected]180d4e92014-05-22 15:35:1696 case Manifest::TYPE_SHARED_MODULE: {
[email protected]ce4e5d7a2012-12-11 20:59:2697 base::FundamentalValue type_value(extension->GetType());
98 if (allowed_types &&
99 allowed_types->Find(type_value) == allowed_types->end())
100 return ReturnLoadError(extension, error);
101 break;
[email protected]180d4e92014-05-22 15:35:16102 }
103 case Manifest::NUM_LOAD_TYPES:
104 NOTREACHED();
[email protected]ce4e5d7a2012-12-11 20:59:26105 }
106
[email protected]695b5712012-12-06 23:55:28107 // Check the whitelist/forcelist first.
[email protected]013724b2012-06-05 19:40:39108 base::StringValue id_value(extension->id());
[email protected]4ee07c62012-08-21 12:40:42109 if ((whitelist && whitelist->Find(id_value) != whitelist->end()) ||
[email protected]e410b5f2012-12-14 14:02:24110 (forcelist && forcelist->HasKey(extension->id())))
[email protected]013724b2012-06-05 19:40:39111 return true;
112
[email protected]695b5712012-12-06 23:55:28113 // Then check the admin blacklist.
[email protected]ce4e5d7a2012-12-11 20:59:26114 if ((blacklist && blacklist->Find(id_value) != blacklist->end()) ||
115 BlacklistedByDefault(blacklist))
116 return ReturnLoadError(extension, error);
117
118 return true;
[email protected]013724b2012-06-05 19:40:39119}
120
[email protected]0d163dc2013-12-20 23:48:36121bool UserMayModifySettings(const Extension* extension, base::string16* error) {
[email protected]013724b2012-06-05 19:40:39122 return ManagementPolicyImpl(extension, error, true);
123}
124
[email protected]0d163dc2013-12-20 23:48:36125bool MustRemainEnabled(const Extension* extension, base::string16* error) {
[email protected]013724b2012-06-05 19:40:39126 return ManagementPolicyImpl(extension, error, false);
127}
128
[email protected]d42c11152013-08-22 19:36:32129} // namespace admin_policy
130} // namespace extensions