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 // 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 #if defined(OS_CHROMEOS) | |
| 27 if (was_waiting_for_user_to_accept_eula_) { | |
| 28 // Note that this must listen on AllSources due to the difficulty in knowing | |
| 29 // when the WizardController instance is created, and to avoid over-coupling | |
| 30 // the Chrome OS code with the VariationsService by directly attaching as an | |
| 31 // observer. | |
|
Ilya Sherman
2012/09/19 19:49:01
nit: Please mention that the WizardController is a
SteveT
2012/09/20 19:40:18
Done.
| |
| 32 registrar_.Add(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 33 content::NotificationService::AllSources()); | |
| 34 } | |
| 35 #endif | |
| 36 } | |
| 37 | |
| 38 ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() { | |
| 39 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
| 40 } | |
| 41 | |
| 42 void ResourceRequestAllowedNotifier::SetObserver(Observer* observer) { | |
| 43 DCHECK(!observer_ && observer); | |
|
Alexei Svitkine (slow)
2012/09/19 20:05:35
Either remove this check or document it in the hea
SteveT
2012/09/20 19:40:18
Will document in header due to the next comment...
| |
| 44 observer_ = observer; | |
|
Alexei Svitkine (slow)
2012/09/19 20:05:35
Hmm, I think perhaps the checks that are currently
SteveT
2012/09/20 19:40:18
I like your latter suggestion of setting the obser
| |
| 45 } | |
| 46 | |
| 47 void ResourceRequestAllowedNotifier::ClearObserver() { | |
| 48 DCHECK(observer_); | |
|
Alexei Svitkine (slow)
2012/09/19 20:05:35
Either remove this check or document it in the hea
SteveT
2012/09/20 19:40:18
Will document in header due to the previous commen
| |
| 49 observer_ = NULL; | |
| 50 } | |
| 51 | |
| 52 bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() { | |
| 53 was_waiting_for_network_ = net::NetworkChangeNotifier::IsOffline(); | |
| 54 | |
| 55 return | |
| 56 #if defined(OS_CHROMEOS) | |
| 57 !was_waiting_for_user_to_accept_eula_ && | |
| 58 #endif | |
| 59 !was_waiting_for_network_; | |
| 60 } | |
| 61 | |
| 62 void ResourceRequestAllowedNotifier:: | |
| 63 SetWasWaitingForNetworkForTesting(bool waiting) { | |
| 64 was_waiting_for_network_ = waiting; | |
| 65 } | |
| 66 | |
| 67 #if defined(OS_CHROMEOS) | |
| 68 void ResourceRequestAllowedNotifier:: | |
| 69 SetWasWaitingForEulaForTesting(bool waiting) { | |
| 70 was_waiting_for_user_to_accept_eula_ = waiting; | |
| 71 } | |
| 72 #endif | |
| 73 | |
| 74 void ResourceRequestAllowedNotifier::MaybeNotifyObservers() { | |
| 75 // Need to ensure that all criteria are met before notifying observers. | |
| 76 if (observer_ && ResourceRequestsAllowed()) { | |
| 77 DVLOG(1) << "Notifying observer of state change."; | |
| 78 observer_->OnResourceRequestsAllowed(); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 #if defined(OS_CHROMEOS) | |
| 83 void ResourceRequestAllowedNotifier::Observe( | |
| 84 int type, | |
| 85 const content::NotificationSource& source, | |
| 86 const content::NotificationDetails& details) { | |
| 87 DCHECK_EQ(chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, type); | |
| 88 // This should only ever be received once. Remove it after this call. | |
| 89 DCHECK(!registrar_.IsEmpty()); | |
| 90 registrar_.Remove(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 91 content::NotificationService::AllSources()); | |
| 92 | |
| 93 // If the observing service did not fail earlier due to an unaccepted EULA, | |
| 94 // do not notify it. | |
| 95 if (was_waiting_for_user_to_accept_eula_) { | |
|
Alexei Svitkine (slow)
2012/09/19 20:05:35
DCHECK that this is true instead.
SteveT
2012/09/20 19:40:18
Thanks - done. That caught a few test issues that
| |
| 96 DVLOG(1) << "EULA was accepted."; | |
| 97 was_waiting_for_user_to_accept_eula_ = false; | |
| 98 MaybeNotifyObservers(); | |
| 99 } | |
| 100 } | |
| 101 #endif | |
| 102 | |
| 103 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 104 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged( | |
| 105 net::NetworkChangeNotifier::ConnectionType type) { | |
| 106 // Only attempt to notify observers if this was previously waiting for the | |
| 107 // network to reconnect, and new network state is actually available. This | |
| 108 // prevents the notifier from notifying the observer if the notifier was never | |
| 109 // waiting on the network, or if the network changes from one online state | |
| 110 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were | |
| 111 // flaky). | |
| 112 if (was_waiting_for_network_ && | |
| 113 type != net::NetworkChangeNotifier::CONNECTION_NONE) { | |
| 114 DVLOG(1) << "Network came back online."; | |
| 115 MaybeNotifyObservers(); | |
| 116 } | |
| 117 } | |
| OLD | NEW |