| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |
| 6 #define CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_ |
| 7 |
| 8 #include "base/observer_list.h" |
| 9 #include "net/base/network_change_notifier.h" |
| 10 |
| 11 #if defined(OS_CHROMEOS) |
| 12 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" |
| 14 #endif |
| 15 |
| 16 // This class informs an interested observer when resource requests over the |
| 17 // network are permitted. |
| 18 // |
| 19 // Currently, the criteria for allowing resource requests include: |
| 20 // 1. The network is currently available. |
| 21 // 2. The EULA was accepted by the user (ChromeOS only). |
| 22 // |
| 23 // Interested services should add themselves as an observer of |
| 24 // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if |
| 25 // requests are permitted. If it returns true, they can go ahead and make their |
| 26 // request. If it returns false, ResourceRequestAllowedNotifier will notify the |
| 27 // service when the criteria is met. |
| 28 // |
| 29 // If ResourceRequestsAllowed returns true the first time, |
| 30 // ResourceRequestAllowedNotifier will not notify the service in the future. |
| 31 // |
| 32 // 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 |
| 34 // global instance. |
| 35 class ResourceRequestAllowedNotifier : |
| 36 #if defined(OS_CHROMEOS) |
| 37 public content::NotificationObserver, |
| 38 #endif |
| 39 public net::NetworkChangeNotifier::ConnectionTypeObserver { |
| 40 public: |
| 41 // Observes resource request allowed state changes. |
| 42 class Observer { |
| 43 public: |
| 44 virtual void OnResourceRequestsAllowed() = 0; |
| 45 }; |
| 46 |
| 47 // Note that the constructor has some logic to register for notifications from |
| 48 // the chromeos::WizardController if the EULA was not accepted at startup |
| 49 // (ChromeOS only). |
| 50 ResourceRequestAllowedNotifier(); |
| 51 virtual ~ResourceRequestAllowedNotifier(); |
| 52 |
| 53 // Sets |observer| as the service to be notified by this instance. |observer| |
| 54 // may not be NULL. This is to be called immediately after construction of |
| 55 // an instance of ResourceRequestAllowedNotifier to pass it the interested |
| 56 // service. |
| 57 void Init(Observer* observer); |
| 58 |
| 59 // Returns true iff all resource request criteria are met. If not, this call |
| 60 // will set some flags so it knows to notify the observer if the criteria |
| 61 // changes. This is virtual so it can be overriden for tests. |
| 62 virtual bool ResourceRequestsAllowed(); |
| 63 |
| 64 void SetWasWaitingForNetworkForTesting(bool waiting); |
| 65 #if defined(OS_CHROMEOS) |
| 66 void SetWasWaitingForEulaForTesting(bool waiting); |
| 67 #endif |
| 68 |
| 69 protected: |
| 70 // Notifies observers if all criteria needed for resource requests are met. |
| 71 // This is protected so it can be called from subclasses for testing. |
| 72 void MaybeNotifyObservers(); |
| 73 |
| 74 #if defined(OS_CHROMEOS) |
| 75 // Returns true iff the EULA has been accepted by the user. |
| 76 // This is virtual so it can be overriden by test classes to avoid making them |
| 77 // aware of the ChromeOS details. This is protected so it call be overriden in |
| 78 // subclasses for testing. |
| 79 virtual bool IsEulaAccepted(); |
| 80 #endif |
| 81 |
| 82 private: |
| 83 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides: |
| 84 virtual void OnConnectionTypeChanged( |
| 85 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 86 |
| 87 #if defined(OS_CHROMEOS) |
| 88 // content::NotificationObserver overrides: |
| 89 virtual void Observe(int type, |
| 90 const content::NotificationSource& source, |
| 91 const content::NotificationDetails& details) OVERRIDE; |
| 92 |
| 93 content::NotificationRegistrar registrar_; |
| 94 |
| 95 // Tracks EULA acceptance criteria. |
| 96 bool was_waiting_for_user_to_accept_eula_; |
| 97 #endif |
| 98 |
| 99 // Tracks network connectivity criteria. |
| 100 bool was_waiting_for_network_; |
| 101 |
| 102 // Observing service interested in request permissions. |
| 103 Observer* observer_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier); |
| 106 }; |
| 107 |
| 108 #endif // CHROME_BROWSER_METRICS_VARIATIONS_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H
_ |
| OLD | NEW |