| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |
| 6 #define CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ | 6 #define CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |
| 7 | 7 |
| 8 #include "chrome/browser/metrics/variations/eula_accepted_notifier.h" |
| 8 #include "net/base/network_change_notifier.h" | 9 #include "net/base/network_change_notifier.h" |
| 9 | 10 |
| 10 #if defined(OS_CHROMEOS) | |
| 11 #include "content/public/browser/notification_observer.h" | |
| 12 #include "content/public/browser/notification_registrar.h" | |
| 13 #endif | |
| 14 | |
| 15 // This class informs an interested observer when resource requests over the | 11 // This class informs an interested observer when resource requests over the |
| 16 // network are permitted. | 12 // network are permitted. |
| 17 // | 13 // |
| 18 // Currently, the criteria for allowing resource requests are: | 14 // Currently, the criteria for allowing resource requests are: |
| 19 // 1. The network is currently available, | 15 // 1. The network is currently available, |
| 20 // 2. The EULA was accepted by the user (ChromeOS only), and | 16 // 2. The EULA was accepted by the user (ChromeOS only), and |
| 21 // 3. The --disable-background-networking command line switch is not set. | 17 // 3. The --disable-background-networking command line switch is not set. |
| 22 // | 18 // |
| 23 // Interested services should add themselves as an observer of | 19 // Interested services should add themselves as an observer of |
| 24 // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if | 20 // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if |
| 25 // requests are permitted. If it returns true, they can go ahead and make their | 21 // requests are permitted. If it returns true, they can go ahead and make their |
| 26 // request. If it returns false, ResourceRequestAllowedNotifier will notify the | 22 // request. If it returns false, ResourceRequestAllowedNotifier will notify the |
| 27 // service when the criteria is met. | 23 // service when the criteria is met. |
| 28 // | 24 // |
| 29 // If ResourceRequestsAllowed returns true the first time, | 25 // If ResourceRequestsAllowed returns true the first time, |
| 30 // ResourceRequestAllowedNotifier will not notify the service in the future. | 26 // ResourceRequestAllowedNotifier will not notify the service in the future. |
| 31 // | 27 // |
| 32 // Note that this class handles the criteria state for a single service, so | 28 // Note that this class handles the criteria state for a single service, so |
| 33 // services should keep their own instance of this class rather than sharing a | 29 // services should keep their own instance of this class rather than sharing a |
| 34 // global instance. | 30 // global instance. |
| 35 class ResourceRequestAllowedNotifier : | 31 class ResourceRequestAllowedNotifier |
| 36 #if defined(OS_CHROMEOS) | 32 : public EulaAcceptedNotifier::Observer, |
| 37 public content::NotificationObserver, | 33 public net::NetworkChangeNotifier::ConnectionTypeObserver { |
| 38 #endif | |
| 39 public net::NetworkChangeNotifier::ConnectionTypeObserver { | |
| 40 public: | 34 public: |
| 41 // Observes resource request allowed state changes. | 35 // Observes resource request allowed state changes. |
| 42 class Observer { | 36 class Observer { |
| 43 public: | 37 public: |
| 44 virtual void OnResourceRequestsAllowed() = 0; | 38 virtual void OnResourceRequestsAllowed() = 0; |
| 45 }; | 39 }; |
| 46 | 40 |
| 47 ResourceRequestAllowedNotifier(); | 41 ResourceRequestAllowedNotifier(); |
| 48 virtual ~ResourceRequestAllowedNotifier(); | 42 virtual ~ResourceRequestAllowedNotifier(); |
| 49 | 43 |
| 50 // Sets |observer| as the service to be notified by this instance, and | 44 // Sets |observer| as the service to be notified by this instance, and |
| 51 // performs initial checks on the criteria. |observer| may not be NULL. | 45 // performs initial checks on the criteria. |observer| may not be NULL. |
| 52 // This is to be called immediately after construction of an instance of | 46 // This is to be called immediately after construction of an instance of |
| 53 // ResourceRequestAllowedNotifier to pass it the interested service. | 47 // ResourceRequestAllowedNotifier to pass it the interested service. |
| 54 void Init(Observer* observer); | 48 void Init(Observer* observer); |
| 55 | 49 |
| 56 // Returns true iff all resource request criteria are met. If not, this call | 50 // Returns true iff all resource request criteria are met. If not, this call |
| 57 // will set some flags so it knows to notify the observer if the criteria | 51 // will set some flags so it knows to notify the observer if the criteria |
| 58 // changes. Note that the observer will never be notified unless it calls this | 52 // changes. Note that the observer will never be notified unless it calls this |
| 59 // method first. This is virtual so it can be overriden for tests. | 53 // method first. This is virtual so it can be overridden for tests. |
| 60 virtual bool ResourceRequestsAllowed(); | 54 virtual bool ResourceRequestsAllowed(); |
| 61 | 55 |
| 62 void SetWasWaitingForNetworkForTesting(bool waiting); | 56 void SetWaitingForNetworkForTesting(bool waiting); |
| 63 #if defined(OS_CHROMEOS) | 57 void SetWaitingForEulaForTesting(bool waiting); |
| 64 void SetWasWaitingForEulaForTesting(bool waiting); | |
| 65 #endif | |
| 66 | 58 |
| 67 protected: | 59 protected: |
| 68 // Notifies the observer if all criteria needed for resource requests are met. | 60 // Notifies the observer if all criteria needed for resource requests are met. |
| 69 // This is protected so it can be called from subclasses for testing. | 61 // This is protected so it can be called from subclasses for testing. |
| 70 void MaybeNotifyObserver(); | 62 void MaybeNotifyObserver(); |
| 71 | 63 |
| 72 #if defined(OS_CHROMEOS) | 64 private: |
| 73 // On official builds, returns true iff the EULA needs to be accepted. This | 65 // Creates the EulaAcceptNotifier or NULL if one is not needed. Virtual so |
| 74 // always returns false on unofficial builds since there is no notion of a | 66 // that it can be overridden by test subclasses. |
| 75 // EULA. | 67 virtual EulaAcceptedNotifier* CreateEulaNotifier(); |
| 76 // | |
| 77 // This is virtual so it can be overriden by test classes to avoid making them | |
| 78 // aware of the ChromeOS details. This is protected so it call be overriden in | |
| 79 // subclasses for testing. | |
| 80 virtual bool NeedsEulaAcceptance(); | |
| 81 #endif | |
| 82 | 68 |
| 83 private: | 69 // EulaAcceptedNotifier::Observer overrides: |
| 70 virtual void OnEulaAccepted() OVERRIDE; |
| 71 |
| 84 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides: | 72 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides: |
| 85 virtual void OnConnectionTypeChanged( | 73 virtual void OnConnectionTypeChanged( |
| 86 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | 74 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 87 | 75 |
| 88 #if defined(OS_CHROMEOS) | |
| 89 // content::NotificationObserver overrides: | |
| 90 virtual void Observe(int type, | |
| 91 const content::NotificationSource& source, | |
| 92 const content::NotificationDetails& details) OVERRIDE; | |
| 93 #endif | |
| 94 | |
| 95 // Tracks whether or not the observer/service depending on this class actually | 76 // Tracks whether or not the observer/service depending on this class actually |
| 96 // requested permission to make a request or not. If it did not, then this | 77 // requested permission to make a request or not. If it did not, then this |
| 97 // class should not notify it even if the criteria is met. | 78 // class should not notify it even if the criteria is met. |
| 98 bool observer_requested_permission_; | 79 bool observer_requested_permission_; |
| 99 | 80 |
| 100 // Tracks network connectivity criteria. | 81 // Tracks network connectivity criteria. |
| 101 bool was_waiting_for_network_; | 82 bool waiting_for_network_; |
| 102 | 83 |
| 103 #if defined(OS_CHROMEOS) | |
| 104 // Tracks EULA acceptance criteria. | 84 // Tracks EULA acceptance criteria. |
| 105 bool was_waiting_for_user_to_accept_eula_; | 85 bool waiting_for_user_to_accept_eula_; |
| 106 | 86 |
| 107 // Used to listen for the EULA accepted notification. | 87 // Platform-specific notifier of EULA acceptance, or NULL if not needed. |
| 108 content::NotificationRegistrar registrar_; | 88 scoped_ptr<EulaAcceptedNotifier> eula_notifier_; |
| 109 #endif | |
| 110 | 89 |
| 111 // Observing service interested in request permissions. | 90 // Observing service interested in request permissions. |
| 112 Observer* observer_; | 91 Observer* observer_; |
| 113 | 92 |
| 114 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier); | 93 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier); |
| 115 }; | 94 }; |
| 116 | 95 |
| 117 #endif // CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H
_ | 96 #endif // CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H
_ |
| OLD | NEW |