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