1 // Copyright 2014 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/extension_management_internal.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_management_constants.h"
10 #include "extensions/common/url_pattern_set.h"
13 namespace extensions {
18 const char kMalformedPreferenceWarning[] =
19 "Malformed extension management preference.";
22 IndividualSettings::IndividualSettings() {
26 IndividualSettings::~IndividualSettings() {
29 bool IndividualSettings::Parse(const base::DictionaryValue* dict,
31 std::string installation_mode_str;
32 if (dict->GetStringWithoutPathExpansion(schema_constants::kInstallationMode,
33 &installation_mode_str)) {
34 if (installation_mode_str == schema_constants::kAllowed) {
35 installation_mode = ExtensionManagement::INSTALLATION_ALLOWED;
36 } else if (installation_mode_str == schema_constants::kBlocked) {
37 installation_mode = ExtensionManagement::INSTALLATION_BLOCKED;
38 } else if (installation_mode_str == schema_constants::kForceInstalled) {
39 installation_mode = ExtensionManagement::INSTALLATION_FORCED;
40 } else if (installation_mode_str == schema_constants::kNormalInstalled) {
41 installation_mode = ExtensionManagement::INSTALLATION_RECOMMENDED;
43 // Invalid value for 'installation_mode'.
44 LOG(WARNING) << kMalformedPreferenceWarning;
48 // Only proceed to fetch update url if force or recommended install mode
50 if (installation_mode == ExtensionManagement::INSTALLATION_FORCED ||
51 installation_mode == ExtensionManagement::INSTALLATION_RECOMMENDED) {
52 if (scope != SCOPE_INDIVIDUAL) {
53 // Only individual extensions are allowed to be automatically installed.
54 LOG(WARNING) << kMalformedPreferenceWarning;
57 std::string update_url_str;
58 if (dict->GetStringWithoutPathExpansion(schema_constants::kUpdateUrl,
60 GURL(update_url_str).is_valid()) {
61 update_url = update_url_str;
63 // No valid update URL for extension.
64 LOG(WARNING) << kMalformedPreferenceWarning;
70 // Parses the blocked permission settings.
71 const base::ListValue* list_value = nullptr;
74 // If applicable, inherit from global block list and remove all explicitly
75 // allowed permissions.
76 if (scope != SCOPE_DEFAULT &&
77 dict->GetListWithoutPathExpansion(schema_constants::kAllowedPermissions,
79 // It is assumed that Parse() is already called for SCOPE_DEFAULT and
80 // settings specified for |this| is initialized by copying from default
81 // settings, including the |blocked_permissions| setting here.
82 // That is, |blocked_permissions| should be the default block permissions
83 // list settings here.
84 APIPermissionSet globally_blocked_permissions = blocked_permissions;
85 APIPermissionSet explicitly_allowed_permissions;
86 // Reuses code for parsing API permissions from manifest. But note that we
87 // only support list of strings type.
88 if (!APIPermissionSet::ParseFromJSON(
90 APIPermissionSet::kDisallowInternalPermissions,
91 &explicitly_allowed_permissions,
94 // There might be unknown permissions, warn and just ignore them;
95 LOG(WARNING) << error;
97 APIPermissionSet::Difference(globally_blocked_permissions,
98 explicitly_allowed_permissions,
99 &blocked_permissions);
102 // Then add all newly blocked permissions to the list.
103 if (dict->GetListWithoutPathExpansion(schema_constants::kBlockedPermissions,
105 // The |blocked_permissions| might be the result of the routines above,
106 // or remains the same as default block permissions settings.
107 APIPermissionSet permissions_to_merge_from = blocked_permissions;
108 APIPermissionSet permissions_parsed;
109 if (!APIPermissionSet::ParseFromJSON(
111 APIPermissionSet::kDisallowInternalPermissions,
115 LOG(WARNING) << error;
117 APIPermissionSet::Union(
118 permissions_to_merge_from, permissions_parsed, &blocked_permissions);
124 void IndividualSettings::Reset() {
125 installation_mode = ExtensionManagement::INSTALLATION_ALLOWED;
127 blocked_permissions.clear();
130 GlobalSettings::GlobalSettings() {
134 GlobalSettings::~GlobalSettings() {
137 void GlobalSettings::Reset() {
138 has_restricted_install_sources = false;
139 install_sources.ClearPatterns();
140 has_restricted_allowed_types = false;
141 allowed_types.clear();
144 } // namespace internal
146 } // namespace extensions