Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: chrome/browser/metrics/variations/resource_request_allowed_notifier.cc

Issue 10917120: Activate the VariationsService for ChromeOS and ensure that it does not ping the server until the E… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Test fixups from asvit Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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() :
Alexei Svitkine (slow) 2012/09/21 15:52:05 Nit: ":" should go on the next line. Perhaps reord
SteveT 2012/09/24 15:38:38 Done.
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 }
22
23 ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() {
24 if (observer_)
25 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
26 }
27
28 void ResourceRequestAllowedNotifier::Init(Observer* observer) {
29 DCHECK(!observer_ && observer);
30 observer_ = observer;
31
32 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
33
34 // Check this state during initialization. It is not expected to change until
35 // the corresponding notification is received.
36 was_waiting_for_user_to_accept_eula_ = !IsEulaAccepted();
37 #if defined(OS_CHROMEOS)
38 if (was_waiting_for_user_to_accept_eula_) {
39 // Note that this must listen on AllSources due to the difficulty in knowing
40 // when the WizardController instance is created, and to avoid over-coupling
41 // the Chrome OS code with the VariationsService by directly attaching as an
42 // observer. This is OK because WizardController is essentially a singleton.
43 registrar_.Add(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED,
44 content::NotificationService::AllSources());
45 }
46 #endif
47 }
48
49 bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() {
50 was_waiting_for_network_ = net::NetworkChangeNotifier::IsOffline();
51
52 return
53 #if defined(OS_CHROMEOS)
54 !was_waiting_for_user_to_accept_eula_ &&
55 #endif
56 !was_waiting_for_network_;
57 }
58
59 void ResourceRequestAllowedNotifier::
60 SetWasWaitingForNetworkForTesting(bool waiting) {
61 was_waiting_for_network_ = waiting;
62 }
63
64 #if defined(OS_CHROMEOS)
65 void ResourceRequestAllowedNotifier::
66 SetWasWaitingForEulaForTesting(bool waiting) {
67 was_waiting_for_user_to_accept_eula_ = waiting;
68 }
69 #endif
70
71 void ResourceRequestAllowedNotifier::MaybeNotifyObservers() {
Alexei Svitkine (slow) 2012/09/21 15:52:05 Nit: Should be MaybeNotifyObserver() (singular).
SteveT 2012/09/24 15:38:38 Done.
72 // Need to ensure that all criteria are met before notifying observers.
73 if (ResourceRequestsAllowed()) {
74 DVLOG(1) << "Notifying observer of state change.";
75 observer_->OnResourceRequestsAllowed();
76 }
77 }
78
79 bool ResourceRequestAllowedNotifier::IsEulaAccepted() {
80 return chromeos::WizardController::IsEulaAccepted();
81 }
82
83 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged(
84 net::NetworkChangeNotifier::ConnectionType type) {
85 // Only attempt to notify observers if this was previously waiting for the
86 // network to reconnect, and new network state is actually available. This
87 // prevents the notifier from notifying the observer if the notifier was never
88 // waiting on the network, or if the network changes from one online state
89 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were
90 // flaky).
91 if (was_waiting_for_network_ &&
92 type != net::NetworkChangeNotifier::CONNECTION_NONE) {
93 DVLOG(1) << "Network came back online.";
94 MaybeNotifyObservers();
Alexei Svitkine (slow) 2012/09/21 15:52:05 This still doesn't work when net::NetworkChangeNot
SteveT 2012/09/24 15:38:38 As discussed offline, we addressed this by shiftin
95 }
96 }
97
98 #if defined(OS_CHROMEOS)
99 void ResourceRequestAllowedNotifier::Observe(
100 int type,
101 const content::NotificationSource& source,
102 const content::NotificationDetails& details) {
103 DCHECK_EQ(chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, type);
104 // This should only ever be received once. Remove it after this call.
105 DCHECK(!registrar_.IsEmpty());
106 registrar_.Remove(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED,
107 content::NotificationService::AllSources());
108
109 // This flag should have been set if this was waiting on the EULA
110 // notification.
111 DCHECK(was_waiting_for_user_to_accept_eula_);
112 DVLOG(1) << "EULA was accepted.";
113 was_waiting_for_user_to_accept_eula_ = false;
114 MaybeNotifyObservers();
115 }
116 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698