OLD | NEW |
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 "components/policy/core/browser/url_blacklist_manager.h" | 5 #include "components/policy/core/browser/url_blacklist_manager.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 pref_change_registrar_.Add(policy_prefs::kUrlBlacklist, callback); | 445 pref_change_registrar_.Add(policy_prefs::kUrlBlacklist, callback); |
446 pref_change_registrar_.Add(policy_prefs::kUrlWhitelist, callback); | 446 pref_change_registrar_.Add(policy_prefs::kUrlWhitelist, callback); |
447 | 447 |
448 // Start enforcing the policies without a delay when they are present at | 448 // Start enforcing the policies without a delay when they are present at |
449 // startup. | 449 // startup. |
450 if (pref_service_->HasPrefPath(policy_prefs::kUrlBlacklist)) | 450 if (pref_service_->HasPrefPath(policy_prefs::kUrlBlacklist)) |
451 Update(); | 451 Update(); |
452 } | 452 } |
453 | 453 |
454 void URLBlacklistManager::ShutdownOnUIThread() { | 454 void URLBlacklistManager::ShutdownOnUIThread() { |
455 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 455 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
456 // Cancel any pending updates, and stop listening for pref change updates. | 456 // Cancel any pending updates, and stop listening for pref change updates. |
457 ui_weak_ptr_factory_.InvalidateWeakPtrs(); | 457 ui_weak_ptr_factory_.InvalidateWeakPtrs(); |
458 pref_change_registrar_.RemoveAll(); | 458 pref_change_registrar_.RemoveAll(); |
459 } | 459 } |
460 | 460 |
461 URLBlacklistManager::~URLBlacklistManager() { | 461 URLBlacklistManager::~URLBlacklistManager() { |
462 } | 462 } |
463 | 463 |
464 void URLBlacklistManager::ScheduleUpdate() { | 464 void URLBlacklistManager::ScheduleUpdate() { |
465 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 465 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
466 // Cancel pending updates, if any. This can happen if two preferences that | 466 // Cancel pending updates, if any. This can happen if two preferences that |
467 // change the blacklist are updated in one message loop cycle. In those cases, | 467 // change the blacklist are updated in one message loop cycle. In those cases, |
468 // only rebuild the blacklist after all the preference updates are processed. | 468 // only rebuild the blacklist after all the preference updates are processed. |
469 ui_weak_ptr_factory_.InvalidateWeakPtrs(); | 469 ui_weak_ptr_factory_.InvalidateWeakPtrs(); |
470 ui_task_runner_->PostTask( | 470 ui_task_runner_->PostTask( |
471 FROM_HERE, | 471 FROM_HERE, |
472 base::Bind(&URLBlacklistManager::Update, | 472 base::Bind(&URLBlacklistManager::Update, |
473 ui_weak_ptr_factory_.GetWeakPtr())); | 473 ui_weak_ptr_factory_.GetWeakPtr())); |
474 } | 474 } |
475 | 475 |
476 void URLBlacklistManager::Update() { | 476 void URLBlacklistManager::Update() { |
477 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 477 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); |
478 | 478 |
479 // The preferences can only be read on the UI thread. | 479 // The preferences can only be read on the UI thread. |
480 std::unique_ptr<base::ListValue> block( | 480 std::unique_ptr<base::ListValue> block( |
481 pref_service_->GetList(policy_prefs::kUrlBlacklist)->DeepCopy()); | 481 pref_service_->GetList(policy_prefs::kUrlBlacklist)->DeepCopy()); |
482 std::unique_ptr<base::ListValue> allow( | 482 std::unique_ptr<base::ListValue> allow( |
483 pref_service_->GetList(policy_prefs::kUrlWhitelist)->DeepCopy()); | 483 pref_service_->GetList(policy_prefs::kUrlWhitelist)->DeepCopy()); |
484 | 484 |
485 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from | 485 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from |
486 // here, since this task will always execute before a potential deletion of | 486 // here, since this task will always execute before a potential deletion of |
487 // ProfileIOData on IO. | 487 // ProfileIOData on IO. |
488 io_task_runner_->PostTask(FROM_HERE, | 488 io_task_runner_->PostTask(FROM_HERE, |
489 base::Bind(&URLBlacklistManager::UpdateOnIO, | 489 base::Bind(&URLBlacklistManager::UpdateOnIO, |
490 base::Unretained(this), | 490 base::Unretained(this), |
491 base::Passed(&block), | 491 base::Passed(&block), |
492 base::Passed(&allow))); | 492 base::Passed(&allow))); |
493 } | 493 } |
494 | 494 |
495 void URLBlacklistManager::UpdateOnIO(std::unique_ptr<base::ListValue> block, | 495 void URLBlacklistManager::UpdateOnIO(std::unique_ptr<base::ListValue> block, |
496 std::unique_ptr<base::ListValue> allow) { | 496 std::unique_ptr<base::ListValue> allow) { |
497 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 497 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
498 // The URLBlacklist is built on a worker thread. Once it's ready, it is passed | 498 // The URLBlacklist is built on a worker thread. Once it's ready, it is passed |
499 // to the URLBlacklistManager on IO. | 499 // to the URLBlacklistManager on IO. |
500 base::PostTaskAndReplyWithResult( | 500 base::PostTaskAndReplyWithResult( |
501 background_task_runner_.get(), | 501 background_task_runner_.get(), |
502 FROM_HERE, | 502 FROM_HERE, |
503 base::Bind(&BuildBlacklist, | 503 base::Bind(&BuildBlacklist, |
504 base::Passed(&block), | 504 base::Passed(&block), |
505 base::Passed(&allow)), | 505 base::Passed(&allow)), |
506 base::Bind(&URLBlacklistManager::SetBlacklist, | 506 base::Bind(&URLBlacklistManager::SetBlacklist, |
507 io_weak_ptr_factory_.GetWeakPtr())); | 507 io_weak_ptr_factory_.GetWeakPtr())); |
508 } | 508 } |
509 | 509 |
510 void URLBlacklistManager::SetBlacklist( | 510 void URLBlacklistManager::SetBlacklist( |
511 std::unique_ptr<URLBlacklist> blacklist) { | 511 std::unique_ptr<URLBlacklist> blacklist) { |
512 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 512 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
513 blacklist_ = std::move(blacklist); | 513 blacklist_ = std::move(blacklist); |
514 } | 514 } |
515 | 515 |
516 bool URLBlacklistManager::IsURLBlocked(const GURL& url) const { | 516 bool URLBlacklistManager::IsURLBlocked(const GURL& url) const { |
517 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 517 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
518 return blacklist_->IsURLBlocked(url); | 518 return blacklist_->IsURLBlocked(url); |
519 } | 519 } |
520 | 520 |
521 URLBlacklist::URLBlacklistState URLBlacklistManager::GetURLBlacklistState( | 521 URLBlacklist::URLBlacklistState URLBlacklistManager::GetURLBlacklistState( |
522 const GURL& url) const { | 522 const GURL& url) const { |
523 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 523 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
524 return blacklist_->GetURLBlacklistState(url); | 524 return blacklist_->GetURLBlacklistState(url); |
525 } | 525 } |
526 | 526 |
527 bool URLBlacklistManager::ShouldBlockRequestForFrame(const GURL& url, | 527 bool URLBlacklistManager::ShouldBlockRequestForFrame(const GURL& url, |
528 int* reason) const { | 528 int* reason) const { |
529 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 529 DCHECK(io_task_runner_->RunsTasksInCurrentSequence()); |
530 | 530 |
531 bool block = false; | 531 bool block = false; |
532 if (override_blacklist_.Run(url, &block, reason)) | 532 if (override_blacklist_.Run(url, &block, reason)) |
533 return block; | 533 return block; |
534 | 534 |
535 *reason = net::ERR_BLOCKED_BY_ADMINISTRATOR; | 535 *reason = net::ERR_BLOCKED_BY_ADMINISTRATOR; |
536 return IsURLBlocked(url); | 536 return IsURLBlocked(url); |
537 } | 537 } |
538 | 538 |
539 // static | 539 // static |
540 void URLBlacklistManager::RegisterProfilePrefs( | 540 void URLBlacklistManager::RegisterProfilePrefs( |
541 user_prefs::PrefRegistrySyncable* registry) { | 541 user_prefs::PrefRegistrySyncable* registry) { |
542 registry->RegisterListPref(policy_prefs::kUrlBlacklist); | 542 registry->RegisterListPref(policy_prefs::kUrlBlacklist); |
543 registry->RegisterListPref(policy_prefs::kUrlWhitelist); | 543 registry->RegisterListPref(policy_prefs::kUrlWhitelist); |
544 } | 544 } |
545 | 545 |
546 } // namespace policy | 546 } // namespace policy |
OLD | NEW |