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