resource_request_allowed_notifier.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.h"
  5. #include "base/bind.h"
  6. #include "base/command_line.h"
  7. namespace web_resource {
  8. ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier(
  9. PrefService* local_state,
  10. const char* disable_network_switch,
  11. NetworkConnectionTrackerGetter network_connection_tracker_getter)
  12. : disable_network_switch_(disable_network_switch),
  13. local_state_(local_state),
  14. observer_requested_permission_(false),
  15. waiting_for_user_to_accept_eula_(false),
  16. observer_(nullptr),
  17. network_connection_tracker_getter_(
  18. std::move(network_connection_tracker_getter)) {}
  19. ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() {
  20. if (observer_)
  21. network_connection_tracker_->RemoveNetworkConnectionObserver(this);
  22. }
  23. void ResourceRequestAllowedNotifier::Init(Observer* observer, bool leaky) {
  24. DCHECK(!observer_);
  25. DCHECK(observer);
  26. observer_ = observer;
  27. DCHECK(network_connection_tracker_getter_);
  28. network_connection_tracker_ =
  29. std::move(network_connection_tracker_getter_).Run();
  30. if (leaky)
  31. network_connection_tracker_->AddLeakyNetworkConnectionObserver(this);
  32. else
  33. network_connection_tracker_->AddNetworkConnectionObserver(this);
  34. if (network_connection_tracker_->GetConnectionType(
  35. &connection_type_,
  36. base::BindOnce(&ResourceRequestAllowedNotifier::SetConnectionType,
  37. weak_factory_.GetWeakPtr()))) {
  38. connection_initialized_ = true;
  39. }
  40. eula_notifier_.reset(CreateEulaNotifier());
  41. if (eula_notifier_) {
  42. eula_notifier_->Init(this);
  43. waiting_for_user_to_accept_eula_ = !eula_notifier_->IsEulaAccepted();
  44. }
  45. }
  46. ResourceRequestAllowedNotifier::State
  47. ResourceRequestAllowedNotifier::GetResourceRequestsAllowedState() {
  48. if (disable_network_switch_ &&
  49. base::CommandLine::ForCurrentProcess()->HasSwitch(
  50. disable_network_switch_)) {
  51. return DISALLOWED_COMMAND_LINE_DISABLED;
  52. }
  53. // The observer requested permission. Return the current criteria state and
  54. // set a flag to remind this class to notify the observer once the criteria
  55. // is met.
  56. observer_requested_permission_ =
  57. waiting_for_user_to_accept_eula_ || IsOffline();
  58. if (!observer_requested_permission_)
  59. return ALLOWED;
  60. if (waiting_for_user_to_accept_eula_)
  61. return DISALLOWED_EULA_NOT_ACCEPTED;
  62. if (!connection_initialized_)
  63. return DISALLOWED_NETWORK_STATE_NOT_INITIALIZED;
  64. return DISALLOWED_NETWORK_DOWN;
  65. }
  66. bool ResourceRequestAllowedNotifier::IsOffline() {
  67. return !connection_initialized_ ||
  68. connection_type_ == network::mojom::ConnectionType::CONNECTION_NONE;
  69. }
  70. bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() {
  71. return GetResourceRequestsAllowedState() == ALLOWED;
  72. }
  73. void ResourceRequestAllowedNotifier::SetWaitingForEulaForTesting(bool waiting) {
  74. waiting_for_user_to_accept_eula_ = waiting;
  75. }
  76. void ResourceRequestAllowedNotifier::SetObserverRequestedForTesting(
  77. bool requested) {
  78. observer_requested_permission_ = requested;
  79. }
  80. void ResourceRequestAllowedNotifier::SetConnectionTypeForTesting(
  81. network::mojom::ConnectionType type) {
  82. SetConnectionType(type);
  83. }
  84. void ResourceRequestAllowedNotifier::MaybeNotifyObserver() {
  85. // Need to ensure that all criteria are met before notifying observers.
  86. if (observer_requested_permission_ && ResourceRequestsAllowed()) {
  87. DVLOG(1) << "Notifying observer of state change.";
  88. observer_->OnResourceRequestsAllowed();
  89. // Reset this so the observer is not informed again unless they check
  90. // ResourceRequestsAllowed again.
  91. observer_requested_permission_ = false;
  92. }
  93. }
  94. EulaAcceptedNotifier* ResourceRequestAllowedNotifier::CreateEulaNotifier() {
  95. return EulaAcceptedNotifier::Create(local_state_);
  96. }
  97. void ResourceRequestAllowedNotifier::OnEulaAccepted() {
  98. // This flag should have been set if this was waiting on the EULA
  99. // notification.
  100. DCHECK(waiting_for_user_to_accept_eula_);
  101. DVLOG(1) << "EULA was accepted.";
  102. waiting_for_user_to_accept_eula_ = false;
  103. MaybeNotifyObserver();
  104. }
  105. void ResourceRequestAllowedNotifier::OnConnectionChanged(
  106. network::mojom::ConnectionType type) {
  107. SetConnectionType(type);
  108. if (type != network::mojom::ConnectionType::CONNECTION_NONE) {
  109. DVLOG(1) << "Network came online.";
  110. // MaybeNotifyObserver() internally guarantees that it will only notify the
  111. // observer if it's currently waiting for the network to come online.
  112. MaybeNotifyObserver();
  113. }
  114. }
  115. void ResourceRequestAllowedNotifier::SetConnectionType(
  116. network::mojom::ConnectionType connection_type) {
  117. connection_type_ = connection_type;
  118. if (!connection_initialized_) {
  119. connection_initialized_ = true;
  120. MaybeNotifyObserver();
  121. }
  122. }
  123. } // namespace web_resource