[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 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 10 | #include "base/macros.h" |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 11 | #include "components/web_resource/eula_accepted_notifier.h" |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 12 | #include "net/base/network_change_notifier.h" |
| 13 | |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 14 | class PrefService; |
| 15 | |
droger | c4ccb83 | 2014-11-27 16:10:49 | [diff] [blame] | 16 | namespace web_resource { |
| 17 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 18 | // This class informs an interested observer when resource requests over the |
| 19 | // network are permitted. |
| 20 | // |
[email protected] | f593f5bb | 2013-03-07 08:49:34 | [diff] [blame] | 21 | // Currently, the criteria for allowing resource requests are: |
| 22 | // 1. The network is currently available, |
| 23 | // 2. The EULA was accepted by the user (ChromeOS only), and |
| 24 | // 3. The --disable-background-networking command line switch is not set. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 25 | // |
| 26 | // Interested services should add themselves as an observer of |
| 27 | // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if |
| 28 | // requests are permitted. If it returns true, they can go ahead and make their |
| 29 | // request. If it returns false, ResourceRequestAllowedNotifier will notify the |
| 30 | // service when the criteria is met. |
| 31 | // |
| 32 | // If ResourceRequestsAllowed returns true the first time, |
| 33 | // ResourceRequestAllowedNotifier will not notify the service in the future. |
| 34 | // |
| 35 | // Note that this class handles the criteria state for a single service, so |
| 36 | // services should keep their own instance of this class rather than sharing a |
| 37 | // global instance. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 38 | class ResourceRequestAllowedNotifier |
| 39 | : public EulaAcceptedNotifier::Observer, |
| 40 | public net::NetworkChangeNotifier::ConnectionTypeObserver { |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 41 | public: |
| 42 | // Observes resource request allowed state changes. |
| 43 | class Observer { |
| 44 | public: |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 45 | virtual void OnResourceRequestsAllowed() = 0; |
| 46 | }; |
| 47 | |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 48 | // Specifies the resource request allowed state. |
| 49 | enum State { |
| 50 | ALLOWED, |
| 51 | DISALLOWED_EULA_NOT_ACCEPTED, |
| 52 | DISALLOWED_NETWORK_DOWN, |
| 53 | DISALLOWED_COMMAND_LINE_DISABLED, |
| 54 | }; |
| 55 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 56 | // Creates a new ResourceRequestAllowedNotifier. |
| 57 | // |local_state| is the PrefService to observe. |
| 58 | // |disable_network_switch| is the command line switch to disable network |
droger | c4ccb83 | 2014-11-27 16:10:49 | [diff] [blame] | 59 | // activity. It is expected to outlive the ResourceRequestAllowedNotifier and |
| 60 | // may be null. |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 61 | ResourceRequestAllowedNotifier(PrefService* local_state, |
| 62 | const char* disable_network_switch); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 63 | ~ResourceRequestAllowedNotifier() override; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 64 | |
| 65 | // Sets |observer| as the service to be notified by this instance, and |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 66 | // performs initial checks on the criteria. |observer| may not be null. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 67 | // This is to be called immediately after construction of an instance of |
| 68 | // ResourceRequestAllowedNotifier to pass it the interested service. |
| 69 | void Init(Observer* observer); |
| 70 | |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 71 | // Returns whether resource requests are allowed, per the various criteria. |
| 72 | // If not, this call will set some flags so it knows to notify the observer |
| 73 | // if the criteria change. Note that the observer will not be notified unless |
| 74 | // it calls this method first. |
| 75 | // This is virtual so it can be overridden for tests. |
| 76 | virtual State GetResourceRequestsAllowedState(); |
| 77 | |
| 78 | // Convenience function, equivalent to: |
| 79 | // GetResourceRequestsAllowedState() == ALLOWED. |
| 80 | bool ResourceRequestsAllowed(); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 81 | |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 82 | void SetWaitingForNetworkForTesting(bool waiting); |
| 83 | void SetWaitingForEulaForTesting(bool waiting); |
[email protected] | 95af789 | 2013-06-19 07:16:33 | [diff] [blame] | 84 | void SetObserverRequestedForTesting(bool requested); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 85 | |
| 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 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 91 | private: |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 92 | // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 93 | // that it can be overridden by test subclasses. |
| 94 | virtual EulaAcceptedNotifier* CreateEulaNotifier(); |
| 95 | |
| 96 | // EulaAcceptedNotifier::Observer overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 97 | void OnEulaAccepted() override; |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 98 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 99 | // net::NetworkChangeNotifier::ConnectionTypeObserver overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 100 | void OnConnectionTypeChanged( |
mostynb | fb66cb4f | 2014-10-07 09:15:42 | [diff] [blame] | 101 | net::NetworkChangeNotifier::ConnectionType type) override; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 102 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 103 | // Name of the command line switch to disable the network activity. |
| 104 | const char* disable_network_switch_; |
| 105 | |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 106 | // The local state this class is observing. |
| 107 | PrefService* local_state_; |
| 108 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 109 | // Tracks whether or not the observer/service depending on this class actually |
| 110 | // requested permission to make a request or not. If it did not, then this |
| 111 | // class should not notify it even if the criteria is met. |
| 112 | bool observer_requested_permission_; |
| 113 | |
| 114 | // Tracks network connectivity criteria. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 115 | bool waiting_for_network_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 116 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 117 | // Tracks EULA acceptance criteria. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 118 | bool waiting_for_user_to_accept_eula_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 119 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 120 | // Platform-specific notifier of EULA acceptance, or null if not needed. |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 121 | std::unique_ptr<EulaAcceptedNotifier> eula_notifier_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 122 | |
| 123 | // Observing service interested in request permissions. |
| 124 | Observer* observer_; |
| 125 | |
| 126 | DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier); |
| 127 | }; |
| 128 | |
droger | c4ccb83 | 2014-11-27 16:10:49 | [diff] [blame] | 129 | } // namespace web_resource |
| 130 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame] | 131 | #endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |