[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 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 8 | #include "components/web_resource/eula_accepted_notifier.h" |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 9 | #include "net/base/network_change_notifier.h" |
| 10 | |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 11 | class PrefService; |
| 12 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 13 | // This class informs an interested observer when resource requests over the |
| 14 | // network are permitted. |
| 15 | // |
[email protected] | f593f5bb | 2013-03-07 08:49:34 | [diff] [blame] | 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. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 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. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 33 | class ResourceRequestAllowedNotifier |
| 34 | : public EulaAcceptedNotifier::Observer, |
| 35 | public net::NetworkChangeNotifier::ConnectionTypeObserver { |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 36 | public: |
| 37 | // Observes resource request allowed state changes. |
| 38 | class Observer { |
| 39 | public: |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 40 | virtual void OnResourceRequestsAllowed() = 0; |
| 41 | }; |
| 42 | |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 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 | }; |
| 50 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 51 | // Creates a new ResourceRequestAllowedNotifier. |
| 52 | // |local_state| is the PrefService to observe. |
| 53 | // |disable_network_switch| is the command line switch to disable network |
| 54 | // activity, and is expected to outlive the ResourceRequestAllowedNotifier. |
| 55 | ResourceRequestAllowedNotifier(PrefService* local_state, |
| 56 | const char* disable_network_switch); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 57 | ~ResourceRequestAllowedNotifier() override; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 58 | |
| 59 | // Sets |observer| as the service to be notified by this instance, and |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 60 | // performs initial checks on the criteria. |observer| may not be null. |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 61 | // This is to be called immediately after construction of an instance of |
| 62 | // ResourceRequestAllowedNotifier to pass it the interested service. |
| 63 | void Init(Observer* observer); |
| 64 | |
[email protected] | 06b9cde | 2013-11-21 07:38:06 | [diff] [blame] | 65 | // Returns whether resource requests are allowed, per the various criteria. |
| 66 | // If not, this call will set some flags so it knows to notify the observer |
| 67 | // if the criteria change. Note that the observer will not be notified unless |
| 68 | // it calls this method first. |
| 69 | // This is virtual so it can be overridden for tests. |
| 70 | virtual State GetResourceRequestsAllowedState(); |
| 71 | |
| 72 | // Convenience function, equivalent to: |
| 73 | // GetResourceRequestsAllowedState() == ALLOWED. |
| 74 | bool ResourceRequestsAllowed(); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 75 | |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 76 | void SetWaitingForNetworkForTesting(bool waiting); |
| 77 | void SetWaitingForEulaForTesting(bool waiting); |
[email protected] | 95af789 | 2013-06-19 07:16:33 | [diff] [blame] | 78 | void SetObserverRequestedForTesting(bool requested); |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 79 | |
| 80 | protected: |
| 81 | // Notifies the observer if all criteria needed for resource requests are met. |
| 82 | // This is protected so it can be called from subclasses for testing. |
| 83 | void MaybeNotifyObserver(); |
| 84 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 85 | private: |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 86 | // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 87 | // that it can be overridden by test subclasses. |
| 88 | virtual EulaAcceptedNotifier* CreateEulaNotifier(); |
| 89 | |
| 90 | // EulaAcceptedNotifier::Observer overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 91 | void OnEulaAccepted() override; |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 92 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 93 | // net::NetworkChangeNotifier::ConnectionTypeObserver overrides: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 94 | void OnConnectionTypeChanged( |
mostynb | fb66cb4f | 2014-10-07 09:15:42 | [diff] [blame] | 95 | net::NetworkChangeNotifier::ConnectionType type) override; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 96 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 97 | // Name of the command line switch to disable the network activity. |
| 98 | const char* disable_network_switch_; |
| 99 | |
droger | f445548 | 2014-11-21 09:50:18 | [diff] [blame] | 100 | // The local state this class is observing. |
| 101 | PrefService* local_state_; |
| 102 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 103 | // Tracks whether or not the observer/service depending on this class actually |
| 104 | // requested permission to make a request or not. If it did not, then this |
| 105 | // class should not notify it even if the criteria is met. |
| 106 | bool observer_requested_permission_; |
| 107 | |
| 108 | // Tracks network connectivity criteria. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 109 | bool waiting_for_network_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 110 | |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 111 | // Tracks EULA acceptance criteria. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 112 | bool waiting_for_user_to_accept_eula_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 113 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 114 | // Platform-specific notifier of EULA acceptance, or null if not needed. |
[email protected] | 05d242d | 2013-04-08 21:49:49 | [diff] [blame] | 115 | scoped_ptr<EulaAcceptedNotifier> eula_notifier_; |
[email protected] | d06ba124 | 2012-09-25 20:51:39 | [diff] [blame] | 116 | |
| 117 | // Observing service interested in request permissions. |
| 118 | Observer* observer_; |
| 119 | |
| 120 | DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier); |
| 121 | }; |
| 122 | |
droger | f847994 | 2014-11-21 17:47:53 | [diff] [blame^] | 123 | #endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |