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