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