resource_request_allowed_notifier.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
  5. #define COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
  6. #include <memory>
  7. #include "base/feature_list.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/web_resource/eula_accepted_notifier.h"
  10. #include "services/network/public/cpp/network_connection_tracker.h"
  11. class PrefService;
  12. namespace web_resource {
  13. // This class informs an interested observer when resource requests over the
  14. // network are permitted.
  15. //
  16. // Currently, the criteria for allowing resource requests are:
  17. // 1. The network is currently available,
  18. // 2. The EULA was accepted by the user (ChromeOS only), and
  19. // 3. The --disable-background-networking command line switch is not set.
  20. //
  21. // Interested services should add themselves as an observer of
  22. // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
  23. // requests are permitted. If it returns true, they can go ahead and make their
  24. // request. If it returns false, ResourceRequestAllowedNotifier will notify the
  25. // service when the criteria is met.
  26. //
  27. // If ResourceRequestsAllowed returns true the first time,
  28. // ResourceRequestAllowedNotifier will not notify the service in the future.
  29. //
  30. // Note that this class handles the criteria state for a single service, so
  31. // services should keep their own instance of this class rather than sharing a
  32. // global instance.
  33. class ResourceRequestAllowedNotifier
  34. : public EulaAcceptedNotifier::Observer,
  35. public network::NetworkConnectionTracker::NetworkConnectionObserver {
  36. public:
  37. // Observes resource request allowed state changes.
  38. class Observer {
  39. public:
  40. virtual void OnResourceRequestsAllowed() = 0;
  41. virtual ~Observer() = default;
  42. };
  43. // Specifies the resource request allowed state.
  44. enum State {
  45. ALLOWED,
  46. DISALLOWED_EULA_NOT_ACCEPTED,
  47. DISALLOWED_NETWORK_DOWN,
  48. DISALLOWED_COMMAND_LINE_DISABLED,
  49. DISALLOWED_NETWORK_STATE_NOT_INITIALIZED,
  50. };
  51. using NetworkConnectionTrackerGetter =
  52. base::OnceCallback<network::NetworkConnectionTracker*()>;
  53. // Creates a new ResourceRequestAllowedNotifier.
  54. // |local_state| is the PrefService to observe.
  55. // |disable_network_switch| is the command line switch to disable network
  56. // activity. It is expected to outlive the ResourceRequestAllowedNotifier and
  57. // may be null.
  58. ResourceRequestAllowedNotifier(
  59. PrefService* local_state,
  60. const char* disable_network_switch,
  61. NetworkConnectionTrackerGetter network_connection_tracker_getter);
  62. ResourceRequestAllowedNotifier(const ResourceRequestAllowedNotifier&) =
  63. delete;
  64. ResourceRequestAllowedNotifier& operator=(
  65. const ResourceRequestAllowedNotifier&) = delete;
  66. ~ResourceRequestAllowedNotifier() override;
  67. // Sets |observer| as the service to be notified by this instance, and
  68. // performs initial checks on the criteria. |observer| may not be null.
  69. // This is to be called immediately after construction of an instance of
  70. // ResourceRequestAllowedNotifier to pass it the interested service. Set
  71. // |leaky| to true if this class will not be destructed before shutdown.
  72. void Init(Observer* observer, bool leaky);
  73. // Returns whether resource requests are allowed, per the various criteria.
  74. // If not, this call will set some flags so it knows to notify the observer
  75. // if the criteria change. Note that the observer will not be notified unless
  76. // it calls this method first.
  77. // This is virtual so it can be overridden for tests.
  78. virtual State GetResourceRequestsAllowedState();
  79. // Convenience function, equivalent to:
  80. // GetResourceRequestsAllowedState() == ALLOWED.
  81. bool ResourceRequestsAllowed();
  82. void SetWaitingForEulaForTesting(bool waiting);
  83. void SetObserverRequestedForTesting(bool requested);
  84. void SetConnectionTypeForTesting(
  85. network::mojom::ConnectionType connection_type);
  86. protected:
  87. // Notifies the observer if all criteria needed for resource requests are met.
  88. // This is protected so it can be called from subclasses for testing.
  89. void MaybeNotifyObserver();
  90. private:
  91. // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so
  92. // that it can be overridden by test subclasses.
  93. virtual EulaAcceptedNotifier* CreateEulaNotifier();
  94. // EulaAcceptedNotifier::Observer overrides:
  95. void OnEulaAccepted() override;
  96. // network::NetworkConnectionTracker::NetworkConnectionObserver overrides:
  97. void OnConnectionChanged(network::mojom::ConnectionType type) override;
  98. void SetConnectionType(network::mojom::ConnectionType connection_type);
  99. bool IsOffline();
  100. // Name of the command line switch to disable the network activity.
  101. const char* disable_network_switch_;
  102. // The local state this class is observing.
  103. raw_ptr<PrefService> local_state_;
  104. // Tracks whether or not the observer/service depending on this class actually
  105. // requested permission to make a request or not. If it did not, then this
  106. // class should not notify it even if the criteria is met.
  107. bool observer_requested_permission_;
  108. // Tracks EULA acceptance criteria.
  109. bool waiting_for_user_to_accept_eula_;
  110. // Platform-specific notifier of EULA acceptance, or null if not needed.
  111. std::unique_ptr<EulaAcceptedNotifier> eula_notifier_;
  112. // Observing service interested in request permissions.
  113. raw_ptr<Observer> observer_;
  114. NetworkConnectionTrackerGetter network_connection_tracker_getter_;
  115. raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_ =
  116. nullptr;
  117. network::mojom::ConnectionType connection_type_ =
  118. network::mojom::ConnectionType::CONNECTION_UNKNOWN;
  119. bool connection_initialized_ = false;
  120. base::WeakPtrFactory<ResourceRequestAllowedNotifier> weak_factory_{this};
  121. };
  122. extern const base::Feature kResourceRequestAllowedMigration;
  123. } // namespace web_resource
  124. #endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_