OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_management.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/extensions/external_provider_impl.h" |
| 10 #include "components/crx_file/id_util.h" |
| 11 #include "extensions/browser/pref_names.h" |
| 12 #include "extensions/common/url_pattern.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 void ExtensionManagement::IndividualSettings::Reset() { |
| 17 installation_mode = ExtensionManagement::INSTALLATION_ALLOWED; |
| 18 update_url.clear(); |
| 19 } |
| 20 |
| 21 ExtensionManagement::GlobalSettings::GlobalSettings() { |
| 22 Reset(); |
| 23 } |
| 24 |
| 25 ExtensionManagement::GlobalSettings::~GlobalSettings() { |
| 26 } |
| 27 |
| 28 void ExtensionManagement::GlobalSettings::Reset() { |
| 29 has_restricted_install_sources = false; |
| 30 install_sources.ClearPatterns(); |
| 31 has_restricted_allowed_types = false; |
| 32 allowed_types.clear(); |
| 33 } |
| 34 |
| 35 ExtensionManagement::ExtensionManagement(PrefService* pref_service) |
| 36 : pref_service_(pref_service) { |
| 37 } |
| 38 |
| 39 ExtensionManagement::~ExtensionManagement() { |
| 40 } |
| 41 |
| 42 void ExtensionManagement::Refresh() { |
| 43 // Load all extension management settings preferences. |
| 44 const base::ListValue* allowed_list_pref = |
| 45 static_cast<const base::ListValue*>(LoadPreference( |
| 46 pref_names::kInstallAllowList, true, base::Value::TYPE_LIST)); |
| 47 // Allow user to use preference to block certain extensions. Note that policy |
| 48 // managed forcelist or whitelist will always override this. |
| 49 const base::ListValue* denied_list_pref = |
| 50 static_cast<const base::ListValue*>(LoadPreference( |
| 51 pref_names::kInstallDenyList, false, base::Value::TYPE_LIST)); |
| 52 const base::DictionaryValue* forced_list_pref = |
| 53 static_cast<const base::DictionaryValue*>(LoadPreference( |
| 54 pref_names::kInstallForceList, true, base::Value::TYPE_DICTIONARY)); |
| 55 const base::ListValue* install_sources_pref = |
| 56 static_cast<const base::ListValue*>(LoadPreference( |
| 57 pref_names::kAllowedInstallSites, true, base::Value::TYPE_LIST)); |
| 58 const base::ListValue* allowed_types_pref = |
| 59 static_cast<const base::ListValue*>(LoadPreference( |
| 60 pref_names::kAllowedTypes, true, base::Value::TYPE_LIST)); |
| 61 |
| 62 // Reset all settings. |
| 63 global_settings_.Reset(); |
| 64 settings_by_id_.clear(); |
| 65 default_settings_.Reset(); |
| 66 |
| 67 // Parse default settings. |
| 68 const base::StringValue wildcard("*"); |
| 69 if (denied_list_pref && |
| 70 denied_list_pref->Find(wildcard) != denied_list_pref->end()) { |
| 71 default_settings_.installation_mode = INSTALLATION_BLOCKED; |
| 72 } |
| 73 |
| 74 // Parse legacy preferences. |
| 75 ExtensionId id; |
| 76 |
| 77 if (allowed_list_pref) { |
| 78 for (base::ListValue::const_iterator it = allowed_list_pref->begin(); |
| 79 it != allowed_list_pref->end(); ++it) { |
| 80 if ((*it)->GetAsString(&id) && crx_file::id_util::IdIsValid(id)) |
| 81 AccessById(id)->installation_mode = INSTALLATION_ALLOWED; |
| 82 } |
| 83 } |
| 84 |
| 85 if (denied_list_pref) { |
| 86 for (base::ListValue::const_iterator it = denied_list_pref->begin(); |
| 87 it != denied_list_pref->end(); ++it) { |
| 88 if ((*it)->GetAsString(&id) && crx_file::id_util::IdIsValid(id)) |
| 89 AccessById(id)->installation_mode = INSTALLATION_BLOCKED; |
| 90 } |
| 91 } |
| 92 |
| 93 if (forced_list_pref) { |
| 94 std::string update_url; |
| 95 for (base::DictionaryValue::Iterator it(*forced_list_pref); !it.IsAtEnd(); |
| 96 it.Advance()) { |
| 97 if (!crx_file::id_util::IdIsValid(it.key())) |
| 98 continue; |
| 99 const base::DictionaryValue* dict_value = NULL; |
| 100 if (it.value().GetAsDictionary(&dict_value) && |
| 101 dict_value->GetStringWithoutPathExpansion( |
| 102 ExternalProviderImpl::kExternalUpdateUrl, &update_url)) { |
| 103 IndividualSettings* by_id = AccessById(it.key()); |
| 104 by_id->installation_mode = INSTALLATION_FORCED; |
| 105 by_id->update_url = update_url; |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 if (install_sources_pref) { |
| 111 global_settings_.has_restricted_install_sources = true; |
| 112 std::string url_pattern; |
| 113 for (base::ListValue::const_iterator it = install_sources_pref->begin(); |
| 114 it != install_sources_pref->end(); ++it) { |
| 115 URLPattern entry(URLPattern::SCHEME_ALL); |
| 116 if ((*it)->GetAsString(&url_pattern)) { |
| 117 if (entry.Parse(url_pattern) == URLPattern::PARSE_SUCCESS) { |
| 118 global_settings_.install_sources.AddPattern(entry); |
| 119 } else { |
| 120 LOG(WARNING) << "Invalid URL pattern in for preference " |
| 121 << pref_names::kAllowedInstallSites << ": " |
| 122 << url_pattern << "."; |
| 123 } |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 if (allowed_types_pref) { |
| 129 global_settings_.has_restricted_allowed_types = true; |
| 130 for (base::ListValue::const_iterator it = allowed_types_pref->begin(); |
| 131 it != allowed_types_pref->end(); ++it) { |
| 132 int int_value; |
| 133 if ((*it)->GetAsInteger(&int_value) && int_value >= 0 && |
| 134 int_value < Manifest::Type::NUM_LOAD_TYPES) { |
| 135 global_settings_.allowed_types.push_back( |
| 136 static_cast<Manifest::Type>(int_value)); |
| 137 } |
| 138 } |
| 139 } |
| 140 |
| 141 // TODO(binjin): Add parsing of new ExtensionManagement preference after the |
| 142 // new ExtensionManagement policy is added. |
| 143 } |
| 144 |
| 145 const ExtensionManagement::IndividualSettings& ExtensionManagement::ReadById( |
| 146 const ExtensionId& id) const { |
| 147 DCHECK(crx_file::id_util::IdIsValid(id)) << "Invalid ID: " << id; |
| 148 SettingsIdMap::const_iterator it = settings_by_id_.find(id); |
| 149 if (it != settings_by_id_.end()) |
| 150 return it->second; |
| 151 return default_settings_; |
| 152 } |
| 153 |
| 154 const ExtensionManagement::GlobalSettings& |
| 155 ExtensionManagement::ReadGlobalSettings() const { |
| 156 return global_settings_; |
| 157 } |
| 158 |
| 159 const base::Value* ExtensionManagement::LoadPreference( |
| 160 const char* pref_name, |
| 161 bool force_managed, |
| 162 base::Value::Type expected_type) { |
| 163 const PrefService::Preference* pref = |
| 164 pref_service_->FindPreference(pref_name); |
| 165 if (pref && !pref->IsDefaultValue() && |
| 166 (!force_managed || pref->IsManaged())) { |
| 167 const base::Value* value = pref->GetValue(); |
| 168 if (value && value->IsType(expected_type)) |
| 169 return value; |
| 170 } |
| 171 return NULL; |
| 172 } |
| 173 |
| 174 ExtensionManagement::IndividualSettings* ExtensionManagement::AccessById( |
| 175 const ExtensionId& id) { |
| 176 DCHECK(crx_file::id_util::IdIsValid(id)) << "Invalid ID: " << id; |
| 177 SettingsIdMap::iterator it = settings_by_id_.find(id); |
| 178 if (it == settings_by_id_.end()) |
| 179 it = settings_by_id_.insert(std::make_pair(id, default_settings_)).first; |
| 180 return &it->second; |
| 181 } |
| 182 |
| 183 } // namespace extensions |
OLD | NEW |