Chromium Code Reviews| 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 was_waiting_for_user_to_accept_eula_(false), | |
| 18 #endif | |
| 19 was_waiting_for_network_(false), | |
| 20 observer_(NULL) { | |
| 21 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
Do this in Init() so that there's no timing hole b
SteveT
2012/09/21 15:16:17
Done.
| |
| 22 } | |
| 23 | |
| 24 ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() { | |
| 25 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
After the above change, surround this in if (obser
SteveT
2012/09/21 15:16:17
Done.
| |
| 26 } | |
| 27 | |
| 28 void ResourceRequestAllowedNotifier::Init(Observer* observer) { | |
| 29 DCHECK(!observer_ && observer); | |
| 30 observer_ = observer; | |
| 31 | |
| 32 // Check this state during initialization. It is not expected to change until | |
| 33 // the corresponding notification is received. | |
| 34 was_waiting_for_user_to_accept_eula_ = !IsEulaAccepted(); | |
| 35 #if defined(OS_CHROMEOS) | |
| 36 if (was_waiting_for_user_to_accept_eula_) { | |
| 37 // Note that this must listen on AllSources due to the difficulty in knowing | |
| 38 // when the WizardController instance is created, and to avoid over-coupling | |
| 39 // the Chrome OS code with the VariationsService by directly attaching as an | |
| 40 // observer. This is OK because WizardController is essentially a singleton. | |
| 41 registrar_.Add(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 42 content::NotificationService::AllSources()); | |
| 43 } | |
| 44 #endif | |
| 45 } | |
| 46 | |
| 47 bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() { | |
| 48 was_waiting_for_network_ = net::NetworkChangeNotifier::IsOffline(); | |
| 49 | |
| 50 return | |
| 51 #if defined(OS_CHROMEOS) | |
| 52 !was_waiting_for_user_to_accept_eula_ && | |
| 53 #endif | |
| 54 !was_waiting_for_network_; | |
| 55 } | |
| 56 | |
| 57 void ResourceRequestAllowedNotifier:: | |
| 58 SetWasWaitingForNetworkForTesting(bool waiting) { | |
| 59 was_waiting_for_network_ = waiting; | |
| 60 } | |
| 61 | |
| 62 #if defined(OS_CHROMEOS) | |
| 63 void ResourceRequestAllowedNotifier:: | |
| 64 SetWasWaitingForEulaForTesting(bool waiting) { | |
| 65 was_waiting_for_user_to_accept_eula_ = waiting; | |
| 66 } | |
| 67 #endif | |
| 68 | |
| 69 void ResourceRequestAllowedNotifier::MaybeNotifyObservers() { | |
| 70 // Need to ensure that all criteria are met before notifying observers. | |
| 71 if (observer_ && ResourceRequestsAllowed()) { | |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
No need to check for |observer_| here once the abo
SteveT
2012/09/21 15:16:17
Done.
| |
| 72 DVLOG(1) << "Notifying observer of state change."; | |
| 73 observer_->OnResourceRequestsAllowed(); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 bool ResourceRequestAllowedNotifier::IsEulaAccepted() { | |
| 78 return chromeos::WizardController::IsEulaAccepted(); | |
| 79 } | |
| 80 | |
| 81 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged( | |
| 82 net::NetworkChangeNotifier::ConnectionType type) { | |
| 83 // Only attempt to notify observers if this was previously waiting for the | |
| 84 // network to reconnect, and new network state is actually available. This | |
| 85 // prevents the notifier from notifying the observer if the notifier was never | |
| 86 // waiting on the network, or if the network changes from one online state | |
| 87 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were | |
| 88 // flaky). | |
| 89 if (was_waiting_for_network_ && | |
| 90 type != net::NetworkChangeNotifier::CONNECTION_NONE) { | |
| 91 DVLOG(1) << "Network came back online."; | |
| 92 MaybeNotifyObservers(); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 #if defined(OS_CHROMEOS) | |
| 97 void ResourceRequestAllowedNotifier::Observe( | |
| 98 int type, | |
| 99 const content::NotificationSource& source, | |
| 100 const content::NotificationDetails& details) { | |
| 101 DCHECK_EQ(chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, type); | |
| 102 // This should only ever be received once. Remove it after this call. | |
| 103 DCHECK(!registrar_.IsEmpty()); | |
| 104 registrar_.Remove(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 105 content::NotificationService::AllSources()); | |
| 106 | |
| 107 // This flag should have been set if this was waiting on the EULA | |
| 108 // notification. | |
| 109 DCHECK(was_waiting_for_user_to_accept_eula_); | |
| 110 DVLOG(1) << "EULA was accepted."; | |
| 111 was_waiting_for_user_to_accept_eula_ = false; | |
| 112 MaybeNotifyObservers(); | |
| 113 } | |
| 114 #endif | |
| OLD | NEW |