| 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 #include "chrome/browser/metrics/variations/resource_request_allowed_notifier.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "chrome/common/chrome_notification_types.h" |
| 9 #include "content/public/browser/notification_service.h" |
| 10 |
| 11 #if defined(OS_CHROMEOS) |
| 12 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 13 #endif |
| 14 |
| 15 ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier() : |
| 16 #if defined(OS_CHROMEOS) |
| 17 // Check this once immediately at creation time. It is not expected to |
| 18 // change until the corresponding notification is received. |
| 19 was_waiting_for_user_to_accept_eula_( |
| 20 !chromeos::WizardController::IsEulaAccepted()), |
| 21 #endif |
| 22 was_waiting_for_network_(false), |
| 23 observer_(NULL) { |
| 24 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 25 } |
| 26 |
| 27 ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() { |
| 28 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 29 } |
| 30 |
| 31 void ResourceRequestAllowedNotifier::SetObserver(Observer* observer) { |
| 32 DCHECK(!observer_ && observer); |
| 33 observer_ = observer; |
| 34 } |
| 35 |
| 36 void ResourceRequestAllowedNotifier::ClearObserver() { |
| 37 DCHECK(observer_); |
| 38 observer_ = NULL; |
| 39 } |
| 40 |
| 41 bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() { |
| 42 was_waiting_for_network_ = net::NetworkChangeNotifier::IsOffline(); |
| 43 |
| 44 return |
| 45 #if defined(OS_CHROMEOS) |
| 46 !was_waiting_for_user_to_accept_eula_ && |
| 47 #endif |
| 48 !was_waiting_for_network_; |
| 49 } |
| 50 |
| 51 void ResourceRequestAllowedNotifier::RegisterForEulaAcceptedNotification( |
| 52 chromeos::WizardController* source) { |
| 53 #if defined(OS_CHROMEOS) |
| 54 registrar_.Add(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, |
| 55 content::NotificationSource( |
| 56 content::Source<chromeos::WizardController>(source))); |
| 57 #endif |
| 58 } |
| 59 |
| 60 void ResourceRequestAllowedNotifier:: |
| 61 SetWasWaitingForNetworkForTesting(bool waiting) { |
| 62 was_waiting_for_network_ = waiting; |
| 63 } |
| 64 |
| 65 #if defined(OS_CHROMEOS) |
| 66 void ResourceRequestAllowedNotifier:: |
| 67 SetWasWaitingForEulaForTesting(bool waiting) { |
| 68 was_waiting_for_user_to_accept_eula_ = waiting; |
| 69 } |
| 70 #endif |
| 71 |
| 72 void ResourceRequestAllowedNotifier::MaybeNotifyObservers() { |
| 73 // Need to ensure that all criteria are met before notifying observers. |
| 74 if (observer_ && ResourceRequestsAllowed()) { |
| 75 DVLOG(1) << "Notifying observer of state change."; |
| 76 observer_->OnResourceRequestsAllowed(); |
| 77 } |
| 78 } |
| 79 |
| 80 #if defined(OS_CHROMEOS) |
| 81 void ResourceRequestAllowedNotifier::Observe( |
| 82 int type, |
| 83 const content::NotificationSource& source, |
| 84 const content::NotificationDetails& details) { |
| 85 DCHECK_EQ(chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, type); |
| 86 registrar_.Remove(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, source); |
| 87 |
| 88 // If the observing service did not fail earlier due to an unaccepted EULA, |
| 89 // do not notify it. |
| 90 if (was_waiting_for_user_to_accept_eula_) { |
| 91 DVLOG(1) << "EULA was accepted."; |
| 92 was_waiting_for_user_to_accept_eula_ = false; |
| 93 MaybeNotifyObservers(); |
| 94 } |
| 95 } |
| 96 #endif |
| 97 |
| 98 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 99 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged( |
| 100 net::NetworkChangeNotifier::ConnectionType type) { |
| 101 // Only attempt to notify observers if this was previously waiting for the |
| 102 // network to reconnect, and new network state is actually available. This |
| 103 // prevents the notifier from notifying the observer if the notifier was never |
| 104 // waiting on the network, or if the network changes from one online state |
| 105 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were |
| 106 // flaky). |
| 107 if (was_waiting_for_network_ && |
| 108 type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| 109 DVLOG(1) << "Network came back online."; |
| 110 MaybeNotifyObservers(); |
| 111 } |
| 112 } |
| OLD | NEW |