[email protected] | 1aabf59 | 2013-06-07 08:53:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 5 | #ifndef COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |
| 6 | #define COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 7 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 10 | #include "base/feature_list.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 12 | #include "components/web_resource/eula_accepted_notifier.h" |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 13 | #include "services/network/public/cpp/network_connection_tracker.h" |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 14 | |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 15 | class PrefService; |
| 16 | |
droger | c4ccb83 | 2014-11-27 16:10:49 | [diff] [blame] | 17 | namespace web_resource { |
| 18 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 19 | // This class informs an interested observer when resource requests over the |
| 20 | // network are permitted. |
| 21 | // |
[email protected] | f593f5bb | 2013-03-07 08:49:34 | [diff] [blame] | 22 | // Currently, the criteria for allowing resource requests are: |
| 23 | // 1. The network is currently available, |
| 24 | // 2. The EULA was accepted by the user (ChromeOS only), and |
| 25 | // 3. The --disable-background-networking command line switch is not set. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 26 | // |
| 27 | // Interested services should add themselves as an observer of |
| 28 | // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if |
| 29 | // requests are permitted. If it returns true, they can go ahead and make their |
| 30 | // request. If it returns false, ResourceRequestAllowedNotifier will notify the |
| 31 | // service when the criteria is met. |
| 32 | // |
| 33 | // If ResourceRequestsAllowed returns true the first time, |
| 34 | // ResourceRequestAllowedNotifier will not notify the service in the future. |
| 35 | // |
| 36 | // Note that this class handles the criteria state for a single service, so |
| 37 | // services should keep their own instance of this class rather than sharing a |
| 38 | // global instance. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 39 | class ResourceRequestAllowedNotifier |
| 40 | : public EulaAcceptedNotifier::Observer, |
Clark DuVall | 9a92e39 | 2018-09-20 18:02:47 | [diff] [blame] | 41 | public network::NetworkConnectionTracker::NetworkConnectionObserver { |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 42 | public: |
| 43 | // Observes resource request allowed state changes. |
| 44 | class Observer { |
| 45 | public: |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 46 | virtual void OnResourceRequestsAllowed() = 0; |
Eugene But | 9b0497e | 2017-09-26 16:36:59 | [diff] [blame] | 47 | virtual ~Observer() = default; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 48 | }; |
| 49 | |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 50 | // Specifies the resource request allowed state. |
| 51 | enum State { |
| 52 | ALLOWED, |
| 53 | DISALLOWED_EULA_NOT_ACCEPTED, |
| 54 | DISALLOWED_NETWORK_DOWN, |
| 55 | DISALLOWED_COMMAND_LINE_DISABLED, |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 56 | DISALLOWED_NETWORK_STATE_NOT_INITIALIZED, |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 57 | }; |
| 58 | |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 59 | using NetworkConnectionTrackerGetter = |
| 60 | base::OnceCallback<network::NetworkConnectionTracker*()>; |
| 61 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 62 | // Creates a new ResourceRequestAllowedNotifier. |
| 63 | // |local_state| is the PrefService to observe. |
| 64 | // |disable_network_switch| is the command line switch to disable network |
droger | c4ccb83 | 2014-11-27 16:10:49 | [diff] [blame] | 65 | // activity. It is expected to outlive the ResourceRequestAllowedNotifier and |
| 66 | // may be null. |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 67 | ResourceRequestAllowedNotifier( |
| 68 | PrefService* local_state, |
| 69 | const char* disable_network_switch, |
| 70 | NetworkConnectionTrackerGetter network_connection_tracker_getter); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 71 | |
| 72 | ResourceRequestAllowedNotifier(const ResourceRequestAllowedNotifier&) = |
| 73 | delete; |
| 74 | ResourceRequestAllowedNotifier& operator=( |
| 75 | const ResourceRequestAllowedNotifier&) = delete; |
| 76 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 77 | ~ResourceRequestAllowedNotifier() override; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 78 | |
| 79 | // Sets |observer| as the service to be notified by this instance, and |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 80 | // performs initial checks on the criteria. |observer| may not be null. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 81 | // This is to be called immediately after construction of an instance of |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 82 | // ResourceRequestAllowedNotifier to pass it the interested service. Set |
| 83 | // |leaky| to true if this class will not be destructed before shutdown. |
| 84 | void Init(Observer* observer, bool leaky); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 85 | |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 86 | // Returns whether resource requests are allowed, per the various criteria. |
| 87 | // If not, this call will set some flags so it knows to notify the observer |
| 88 | // if the criteria change. Note that the observer will not be notified unless |
| 89 | // it calls this method first. |
| 90 | // This is virtual so it can be overridden for tests. |
| 91 | virtual State GetResourceRequestsAllowedState(); |
| 92 | |
| 93 | // Convenience function, equivalent to: |
| 94 | // GetResourceRequestsAllowedState() == ALLOWED. |
| 95 | bool ResourceRequestsAllowed(); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 96 | |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 97 | void SetWaitingForEulaForTesting(bool waiting); |
[email protected] | 95af789 | 2013-06-19 07:16:33 | [diff] [blame] | 98 | void SetObserverRequestedForTesting(bool requested); |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 99 | void SetConnectionTypeForTesting( |
| 100 | network::mojom::ConnectionType connection_type); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 101 | |
| 102 | protected: |
| 103 | // Notifies the observer if all criteria needed for resource requests are met. |
| 104 | // This is protected so it can be called from subclasses for testing. |
| 105 | void MaybeNotifyObserver(); |
| 106 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 107 | private: |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 108 | // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 109 | // that it can be overridden by test subclasses. |
| 110 | virtual EulaAcceptedNotifier* CreateEulaNotifier(); |
| 111 | |
| 112 | // EulaAcceptedNotifier::Observer overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 113 | void OnEulaAccepted() override; |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 114 | |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 115 | // network::NetworkConnectionTracker::NetworkConnectionObserver overrides: |
| 116 | void OnConnectionChanged(network::mojom::ConnectionType type) override; |
| 117 | |
| 118 | void SetConnectionType(network::mojom::ConnectionType connection_type); |
| 119 | bool IsOffline(); |
| 120 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 121 | // Name of the command line switch to disable the network activity. |
| 122 | const char* disable_network_switch_; |
| 123 | |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 124 | // The local state this class is observing. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 125 | raw_ptr<PrefService> local_state_; |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 126 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 127 | // Tracks whether or not the observer/service depending on this class actually |
| 128 | // requested permission to make a request or not. If it did not, then this |
| 129 | // class should not notify it even if the criteria is met. |
| 130 | bool observer_requested_permission_; |
| 131 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 132 | // Tracks EULA acceptance criteria. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 133 | bool waiting_for_user_to_accept_eula_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 134 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 135 | // Platform-specific notifier of EULA acceptance, or null if not needed. |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 136 | std::unique_ptr<EulaAcceptedNotifier> eula_notifier_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 137 | |
| 138 | // Observing service interested in request permissions. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 139 | raw_ptr<Observer> observer_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 140 | |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 141 | NetworkConnectionTrackerGetter network_connection_tracker_getter_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 142 | raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_ = |
| 143 | nullptr; |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 144 | network::mojom::ConnectionType connection_type_ = |
| 145 | network::mojom::ConnectionType::CONNECTION_UNKNOWN; |
| 146 | bool connection_initialized_ = false; |
| 147 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 148 | base::WeakPtrFactory<ResourceRequestAllowedNotifier> weak_factory_{this}; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 149 | }; |
| 150 | |
Clark DuVall | 5bf72b1 | 2018-08-31 19:53:30 | [diff] [blame] | 151 | extern const base::Feature kResourceRequestAllowedMigration; |
| 152 | |
droger | c4ccb83 | 2014-11-27 16:10:49 | [diff] [blame] | 153 | } // namespace web_resource |
| 154 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 155 | #endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |