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

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: Flip EULA and added test. Created 8 years, 2 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()
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 SetObserverRequestedPermissionForTesting(bool requested) {
65 observer_requested_permission_ = requested;
66 }
67
68 void ResourceRequestAllowedNotifier::
69 SetWasWaitingForNetworkForTesting(bool waiting) {
70 was_waiting_for_network_ = waiting;
71 }
72
73 #if defined(OS_CHROMEOS)
74 void ResourceRequestAllowedNotifier::
75 SetWasWaitingForEulaForTesting(bool waiting) {
76 was_waiting_for_user_to_accept_eula_ = waiting;
77 }
78 #endif
79
80 void ResourceRequestAllowedNotifier::MaybeNotifyObserver() {
81 // Need to ensure that all criteria are met before notifying observers.
82 if (observer_requested_permission_ && ResourceRequestsAllowed()) {
83 DVLOG(1) << "Notifying observer of state change.";
84 observer_->OnResourceRequestsAllowed();
85 // Reset this so the observer is not informed again unless they check
86 // ResourceRequestsAllowed again.
87 observer_requested_permission_ = false;
88 }
89 }
90
91 #if defined(OS_CHROMEOS)
92 bool ResourceRequestAllowedNotifier::NeedsEulaAcceptance() {
93 #if defined(GOOGLE_CHROME_BUILD)
94 return !chromeos::WizardController::IsEulaAccepted();
95 #else
96 // On unofficial builds, there is no notion of a EULA.
97 return false;
98 #endif
99 }
100 #endif
101
102 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged(
103 net::NetworkChangeNotifier::ConnectionType type) {
104 // Only attempt to notify observers if this was previously waiting for the
105 // network to reconnect, and new network state is actually available. This
106 // prevents the notifier from notifying the observer if the notifier was never
107 // waiting on the network, or if the network changes from one online state
108 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were
109 // flaky).
110 if (was_waiting_for_network_ &&
111 type != net::NetworkChangeNotifier::CONNECTION_NONE) {
112 was_waiting_for_network_ = false;
113 DVLOG(1) << "Network came back online.";
114 MaybeNotifyObserver();
115 }
116 }
117
118 #if defined(OS_CHROMEOS)
119 void ResourceRequestAllowedNotifier::Observe(
120 int type,
121 const content::NotificationSource& source,
122 const content::NotificationDetails& details) {
123 DCHECK_EQ(chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, type);
124 // This should only ever be received once. Remove it after this call.
125 DCHECK(!registrar_.IsEmpty());
126 registrar_.Remove(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED,
127 content::NotificationService::AllSources());
128
129 // This flag should have been set if this was waiting on the EULA
130 // notification.
131 DCHECK(was_waiting_for_user_to_accept_eula_);
132 DVLOG(1) << "EULA was accepted.";
133 was_waiting_for_user_to_accept_eula_ = false;
134 MaybeNotifyObserver();
135 }
136 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698