Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(820)

Side by Side Diff: chrome/browser/component_updater/supervised_user_whitelist_installer.cc

Issue 1443033004: Supervised User whitelists: update to json format v2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: title Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h " 5 #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h "
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/file_enumerator.h" 9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 15 matching lines...) Expand all
26 #include "chrome/browser/profiles/profile_info_cache_observer.h" 26 #include "chrome/browser/profiles/profile_info_cache_observer.h"
27 #include "chrome/browser/supervised_user/supervised_user_whitelist_service.h" 27 #include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
28 #include "chrome/common/chrome_paths.h" 28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
30 #include "components/component_updater/component_updater_paths.h" 30 #include "components/component_updater/component_updater_paths.h"
31 #include "components/component_updater/component_updater_service.h" 31 #include "components/component_updater/component_updater_service.h"
32 #include "components/component_updater/default_component_installer.h" 32 #include "components/component_updater/default_component_installer.h"
33 #include "components/crx_file/id_util.h" 33 #include "components/crx_file/id_util.h"
34 #include "components/safe_json/json_sanitizer.h" 34 #include "components/safe_json/json_sanitizer.h"
35 #include "content/public/browser/browser_thread.h" 35 #include "content/public/browser/browser_thread.h"
36 #include "extensions/common/extension_l10n_util.h"
37 #include "extensions/common/manifest_constants.h"
36 38
37 namespace component_updater { 39 namespace component_updater {
38 40
39 namespace { 41 namespace {
40 42
41 const char kSanitizedWhitelistExtension[] = ".json"; 43 const char kSanitizedWhitelistExtension[] = ".json";
42 44
43 const char kWhitelist[] = "whitelist"; 45 const char kWhitelistedContent[] = "whitelisted_content";
44 const char kFile[] = "file"; 46 const char kSites[] = "sites";
45 47
46 const char kClients[] = "clients"; 48 const char kClients[] = "clients";
47 const char kName[] = "name"; 49 const char kName[] = "name";
48 50
51 using RawWhitelistReadyCallback =
52 base::Callback<void(const base::string16&, const base::FilePath&)>;
53
54 base::string16 GetWhitelistTitle(const base::DictionaryValue& manifest) {
55 base::string16 title;
56 if (!manifest.GetString(extensions::manifest_keys::kShortName, &title))
57 manifest.GetString(extensions::manifest_keys::kName, &title);
58 return title;
59 }
60
49 base::FilePath GetRawWhitelistPath(const base::DictionaryValue& manifest, 61 base::FilePath GetRawWhitelistPath(const base::DictionaryValue& manifest,
50 const base::FilePath& install_dir) { 62 const base::FilePath& install_dir) {
51 const base::DictionaryValue* whitelist_dict = nullptr; 63 const base::DictionaryValue* whitelist_dict = nullptr;
52 if (!manifest.GetDictionary(kWhitelist, &whitelist_dict)) 64 if (!manifest.GetDictionary(kWhitelistedContent, &whitelist_dict))
53 return base::FilePath(); 65 return base::FilePath();
54 66
55 base::FilePath::StringType whitelist_file; 67 base::FilePath::StringType whitelist_file;
56 if (!whitelist_dict->GetString(kFile, &whitelist_file)) 68 if (!whitelist_dict->GetString(kSites, &whitelist_file))
57 return base::FilePath(); 69 return base::FilePath();
58 70
59 return install_dir.Append(whitelist_file); 71 return install_dir.Append(whitelist_file);
60 } 72 }
61 73
74 void LocalizeManifestOnBlockingPool(base::DictionaryValue* manifest,
75 const base::FilePath& install_dir) {
76 std::string error;
77 if (!extension_l10n_util::LocalizeExtension(install_dir, manifest, &error))
78 LOG(WARNING) << "Localizing whitelist failed: " << error;
79 }
80
81 void OnManifestLocalized(
82 const RawWhitelistReadyCallback& callback,
83 const base::FilePath& install_dir,
84 const base::DictionaryValue* manifest) {
85 callback.Run(GetWhitelistTitle(*manifest),
86 GetRawWhitelistPath(*manifest, install_dir));
87 }
88
62 base::FilePath GetSanitizedWhitelistPath(const std::string& crx_id) { 89 base::FilePath GetSanitizedWhitelistPath(const std::string& crx_id) {
63 base::FilePath base_dir; 90 base::FilePath base_dir;
64 PathService::Get(chrome::DIR_SUPERVISED_USER_INSTALLED_WHITELISTS, &base_dir); 91 PathService::Get(chrome::DIR_SUPERVISED_USER_INSTALLED_WHITELISTS, &base_dir);
65 return base_dir.empty() 92 return base_dir.empty()
66 ? base::FilePath() 93 ? base::FilePath()
67 : base_dir.AppendASCII(crx_id + kSanitizedWhitelistExtension); 94 : base_dir.AppendASCII(crx_id + kSanitizedWhitelistExtension);
68 } 95 }
69 96
70 void RecordUncleanUninstall() { 97 void RecordUncleanUninstall() {
71 content::BrowserThread::PostTask( 98 content::BrowserThread::PostTask(
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 } 210 }
184 } 211 }
185 } 212 }
186 213
187 class SupervisedUserWhitelistComponentInstallerTraits 214 class SupervisedUserWhitelistComponentInstallerTraits
188 : public ComponentInstallerTraits { 215 : public ComponentInstallerTraits {
189 public: 216 public:
190 SupervisedUserWhitelistComponentInstallerTraits( 217 SupervisedUserWhitelistComponentInstallerTraits(
191 const std::string& crx_id, 218 const std::string& crx_id,
192 const std::string& name, 219 const std::string& name,
193 const base::Callback<void(const base::FilePath&)>& callback) 220 const RawWhitelistReadyCallback& callback)
194 : crx_id_(crx_id), name_(name), callback_(callback) {} 221 : crx_id_(crx_id), name_(name), callback_(callback) {}
195 ~SupervisedUserWhitelistComponentInstallerTraits() override {} 222 ~SupervisedUserWhitelistComponentInstallerTraits() override {}
196 223
197 private: 224 private:
198 // ComponentInstallerTraits overrides: 225 // ComponentInstallerTraits overrides:
199 bool VerifyInstallation(const base::DictionaryValue& manifest, 226 bool VerifyInstallation(const base::DictionaryValue& manifest,
200 const base::FilePath& install_dir) const override; 227 const base::FilePath& install_dir) const override;
201 bool CanAutoUpdate() const override; 228 bool CanAutoUpdate() const override;
202 bool OnCustomInstall(const base::DictionaryValue& manifest, 229 bool OnCustomInstall(const base::DictionaryValue& manifest,
203 const base::FilePath& install_dir) override; 230 const base::FilePath& install_dir) override;
204 void ComponentReady(const base::Version& version, 231 void ComponentReady(const base::Version& version,
205 const base::FilePath& install_dir, 232 const base::FilePath& install_dir,
206 scoped_ptr<base::DictionaryValue> manifest) override; 233 scoped_ptr<base::DictionaryValue> manifest) override;
207 base::FilePath GetBaseDirectory() const override; 234 base::FilePath GetBaseDirectory() const override;
208 void GetHash(std::vector<uint8_t>* hash) const override; 235 void GetHash(std::vector<uint8_t>* hash) const override;
209 std::string GetName() const override; 236 std::string GetName() const override;
210 237
211 std::string crx_id_; 238 std::string crx_id_;
212 std::string name_; 239 std::string name_;
213 base::Callback<void(const base::FilePath&)> callback_; 240 RawWhitelistReadyCallback callback_;
214 241
215 DISALLOW_COPY_AND_ASSIGN(SupervisedUserWhitelistComponentInstallerTraits); 242 DISALLOW_COPY_AND_ASSIGN(SupervisedUserWhitelistComponentInstallerTraits);
216 }; 243 };
217 244
218 bool SupervisedUserWhitelistComponentInstallerTraits::VerifyInstallation( 245 bool SupervisedUserWhitelistComponentInstallerTraits::VerifyInstallation(
219 const base::DictionaryValue& manifest, 246 const base::DictionaryValue& manifest,
220 const base::FilePath& install_dir) const { 247 const base::FilePath& install_dir) const {
221 // Check whether the whitelist exists at the path specified by the manifest. 248 // Check whether the whitelist exists at the path specified by the manifest.
222 // This does not check whether the whitelist is wellformed. 249 // This does not check whether the whitelist is wellformed.
223 return base::PathExists(GetRawWhitelistPath(manifest, install_dir)); 250 return base::PathExists(GetRawWhitelistPath(manifest, install_dir));
224 } 251 }
225 252
226 bool SupervisedUserWhitelistComponentInstallerTraits::CanAutoUpdate() const { 253 bool SupervisedUserWhitelistComponentInstallerTraits::CanAutoUpdate() const {
227 return true; 254 return true;
228 } 255 }
229 256
230 bool SupervisedUserWhitelistComponentInstallerTraits::OnCustomInstall( 257 bool SupervisedUserWhitelistComponentInstallerTraits::OnCustomInstall(
231 const base::DictionaryValue& manifest, 258 const base::DictionaryValue& manifest,
232 const base::FilePath& install_dir) { 259 const base::FilePath& install_dir) {
233 // Delete the existing sanitized whitelist. 260 // Delete the existing sanitized whitelist.
234 return base::DeleteFile(GetSanitizedWhitelistPath(crx_id_), false); 261 return base::DeleteFile(GetSanitizedWhitelistPath(crx_id_), false);
235 } 262 }
236 263
237 void SupervisedUserWhitelistComponentInstallerTraits::ComponentReady( 264 void SupervisedUserWhitelistComponentInstallerTraits::ComponentReady(
238 const base::Version& version, 265 const base::Version& version,
239 const base::FilePath& install_dir, 266 const base::FilePath& install_dir,
240 scoped_ptr<base::DictionaryValue> manifest) { 267 scoped_ptr<base::DictionaryValue> manifest) {
241 callback_.Run(GetRawWhitelistPath(*manifest, install_dir)); 268 base::DictionaryValue* manifest_raw = manifest.get();
269 content::BrowserThread::PostBlockingPoolTaskAndReply(
270 FROM_HERE,
271 base::Bind(&LocalizeManifestOnBlockingPool, manifest_raw, install_dir),
272 base::Bind(&OnManifestLocalized, callback_, install_dir,
273 base::Owned(manifest.release())));
Bernhard Bauer 2015/11/18 12:10:40 base::Passed(&manifest)?
Marc Treib 2015/11/18 12:44:18 Done.
242 } 274 }
243 275
244 base::FilePath 276 base::FilePath
245 SupervisedUserWhitelistComponentInstallerTraits::GetBaseDirectory() const { 277 SupervisedUserWhitelistComponentInstallerTraits::GetBaseDirectory() const {
246 base::FilePath whitelist_directory; 278 base::FilePath whitelist_directory;
247 PathService::Get(DIR_SUPERVISED_USER_WHITELISTS, &whitelist_directory); 279 PathService::Get(DIR_SUPERVISED_USER_WHITELISTS, &whitelist_directory);
248 return whitelist_directory.AppendASCII(crx_id_); 280 return whitelist_directory.AppendASCII(crx_id_);
249 } 281 }
250 282
251 void SupervisedUserWhitelistComponentInstallerTraits::GetHash( 283 void SupervisedUserWhitelistComponentInstallerTraits::GetHash(
(...skipping 17 matching lines...) Expand all
269 private: 301 private:
270 void RegisterComponent(const std::string& crx_id, 302 void RegisterComponent(const std::string& crx_id,
271 const std::string& name, 303 const std::string& name,
272 const base::Closure& callback); 304 const base::Closure& callback);
273 void RegisterNewComponent(const std::string& crx_id, const std::string& name); 305 void RegisterNewComponent(const std::string& crx_id, const std::string& name);
274 bool UnregisterWhitelistInternal(base::DictionaryValue* pref_dict, 306 bool UnregisterWhitelistInternal(base::DictionaryValue* pref_dict,
275 const std::string& client_id, 307 const std::string& client_id,
276 const std::string& crx_id); 308 const std::string& crx_id);
277 309
278 void OnRawWhitelistReady(const std::string& crx_id, 310 void OnRawWhitelistReady(const std::string& crx_id,
311 const base::string16& title,
279 const base::FilePath& whitelist_path); 312 const base::FilePath& whitelist_path);
280 void OnSanitizedWhitelistReady(const std::string& crx_id); 313 void OnSanitizedWhitelistReady(const std::string& crx_id,
314 const base::string16& title);
281 315
282 // SupervisedUserWhitelistInstaller overrides: 316 // SupervisedUserWhitelistInstaller overrides:
283 void RegisterComponents() override; 317 void RegisterComponents() override;
284 void Subscribe(const WhitelistReadyCallback& callback) override; 318 void Subscribe(const WhitelistReadyCallback& callback) override;
285 void RegisterWhitelist(const std::string& client_id, 319 void RegisterWhitelist(const std::string& client_id,
286 const std::string& crx_id, 320 const std::string& crx_id,
287 const std::string& name) override; 321 const std::string& name) override;
288 void UnregisterWhitelist(const std::string& client_id, 322 void UnregisterWhitelist(const std::string& client_id,
289 const std::string& crx_id) override; 323 const std::string& crx_id) override;
290 324
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 DCHECK(result); 396 DCHECK(result);
363 397
364 result = base::DeleteFile(GetSanitizedWhitelistPath(crx_id), false); 398 result = base::DeleteFile(GetSanitizedWhitelistPath(crx_id), false);
365 DCHECK(result); 399 DCHECK(result);
366 400
367 return removed; 401 return removed;
368 } 402 }
369 403
370 void SupervisedUserWhitelistInstallerImpl::OnRawWhitelistReady( 404 void SupervisedUserWhitelistInstallerImpl::OnRawWhitelistReady(
371 const std::string& crx_id, 405 const std::string& crx_id,
406 const base::string16& title,
372 const base::FilePath& whitelist_path) { 407 const base::FilePath& whitelist_path) {
373 cus_->GetSequencedTaskRunner()->PostTask( 408 cus_->GetSequencedTaskRunner()->PostTask(
374 FROM_HERE, 409 FROM_HERE,
375 base::Bind( 410 base::Bind(
376 &CheckForSanitizedWhitelistOnTaskRunner, crx_id, whitelist_path, 411 &CheckForSanitizedWhitelistOnTaskRunner, crx_id, whitelist_path,
377 base::ThreadTaskRunnerHandle::Get(), 412 base::ThreadTaskRunnerHandle::Get(),
378 base::Bind( 413 base::Bind(
379 &SupervisedUserWhitelistInstallerImpl::OnSanitizedWhitelistReady, 414 &SupervisedUserWhitelistInstallerImpl::OnSanitizedWhitelistReady,
380 weak_ptr_factory_.GetWeakPtr(), crx_id))); 415 weak_ptr_factory_.GetWeakPtr(), crx_id, title)));
381 } 416 }
382 417
383 void SupervisedUserWhitelistInstallerImpl::OnSanitizedWhitelistReady( 418 void SupervisedUserWhitelistInstallerImpl::OnSanitizedWhitelistReady(
384 const std::string& crx_id) { 419 const std::string& crx_id,
420 const base::string16& title) {
385 for (const WhitelistReadyCallback& callback : callbacks_) 421 for (const WhitelistReadyCallback& callback : callbacks_)
386 callback.Run(crx_id, GetSanitizedWhitelistPath(crx_id)); 422 callback.Run(crx_id, title, GetSanitizedWhitelistPath(crx_id));
387 } 423 }
388 424
389 void SupervisedUserWhitelistInstallerImpl::RegisterComponents() { 425 void SupervisedUserWhitelistInstallerImpl::RegisterComponents() {
390 const std::map<std::string, std::string> command_line_whitelists = 426 const std::map<std::string, std::string> command_line_whitelists =
391 SupervisedUserWhitelistService::GetWhitelistsFromCommandLine(); 427 SupervisedUserWhitelistService::GetWhitelistsFromCommandLine();
392 428
393 std::set<std::string> registered_whitelists; 429 std::set<std::string> registered_whitelists;
394 std::set<std::string> stale_whitelists; 430 std::set<std::string> stale_whitelists;
395 DictionaryPrefUpdate update(local_state_, 431 DictionaryPrefUpdate update(local_state_,
396 prefs::kRegisteredSupervisedUserWhitelists); 432 prefs::kRegisteredSupervisedUserWhitelists);
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 581
546 // static 582 // static
547 void SupervisedUserWhitelistInstaller::TriggerComponentUpdate( 583 void SupervisedUserWhitelistInstaller::TriggerComponentUpdate(
548 OnDemandUpdater* updater, 584 OnDemandUpdater* updater,
549 const std::string& crx_id) { 585 const std::string& crx_id) {
550 const bool result = updater->OnDemandUpdate(crx_id); 586 const bool result = updater->OnDemandUpdate(crx_id);
551 DCHECK(result); 587 DCHECK(result);
552 } 588 }
553 589
554 } // namespace component_updater 590 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698