resource_request_allowed_notifier_test_util.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2013 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. #include "components/web_resource/resource_request_allowed_notifier_test_util.h"
  5. #include "base/bind.h"
  6. namespace web_resource {
  7. TestRequestAllowedNotifier::TestRequestAllowedNotifier(
  8. PrefService* local_state,
  9. network::NetworkConnectionTracker* network_connection_tracker)
  10. : ResourceRequestAllowedNotifier(
  11. local_state,
  12. nullptr,
  13. base::BindOnce(
  14. [](network::NetworkConnectionTracker* tracker) {
  15. return tracker;
  16. },
  17. network_connection_tracker)),
  18. override_requests_allowed_(false),
  19. requests_allowed_(true) {}
  20. TestRequestAllowedNotifier::~TestRequestAllowedNotifier() {
  21. }
  22. void TestRequestAllowedNotifier::InitWithEulaAcceptNotifier(
  23. Observer* observer,
  24. std::unique_ptr<EulaAcceptedNotifier> eula_notifier) {
  25. test_eula_notifier_.swap(eula_notifier);
  26. Init(observer, false /* leaky */);
  27. }
  28. void TestRequestAllowedNotifier::SetRequestsAllowedOverride(bool allowed) {
  29. override_requests_allowed_ = true;
  30. requests_allowed_ = allowed;
  31. }
  32. void TestRequestAllowedNotifier::NotifyObserver() {
  33. // Force the allowed state and requested state to true. This forces
  34. // MaybeNotifyObserver to always notify observers, as MaybeNotifyObserver
  35. // checks ResourceRequestsAllowed and requested state.
  36. override_requests_allowed_ = true;
  37. requests_allowed_ = true;
  38. SetObserverRequestedForTesting(true);
  39. MaybeNotifyObserver();
  40. }
  41. ResourceRequestAllowedNotifier::State
  42. TestRequestAllowedNotifier::GetResourceRequestsAllowedState() {
  43. if (override_requests_allowed_)
  44. return requests_allowed_ ? ALLOWED : DISALLOWED_NETWORK_DOWN;
  45. return ResourceRequestAllowedNotifier::GetResourceRequestsAllowedState();
  46. }
  47. EulaAcceptedNotifier* TestRequestAllowedNotifier::CreateEulaNotifier() {
  48. return test_eula_notifier_.release();
  49. }
  50. } // namespace web_resource