Chromium Code Reviews| Index: chrome/browser/resource_request_allowed_notifier.cc |
| diff --git a/chrome/browser/resource_request_allowed_notifier.cc b/chrome/browser/resource_request_allowed_notifier.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c37e26aadd01fb55471fd3ff9d09490353753ac3 |
| --- /dev/null |
| +++ b/chrome/browser/resource_request_allowed_notifier.cc |
| @@ -0,0 +1,101 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/metrics/histogram.h" |
| +#include "chrome/browser/resource_request_allowed_notifier.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "content/public/browser/notification_service.h" |
| + |
| +#if defined(OS_CHROMEOS) |
| +#include "chrome/browser/chromeos/login/wizard_controller.h" |
| +#endif |
| + |
| +ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier() : |
| +#if defined(OS_CHROMEOS) |
| + was_offline_during_last_request_attempt_(false), |
| +#endif |
| + was_waiting_for_user_to_accept_eula_(false) { |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
I think you want the OS_CHROMEOS ifdef over this l
SteveT
2012/09/14 20:25:09
D'oh. You can tell I haven't ran my Non-CrOS tests
|
| + net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| + |
| +#if defined(OS_CHROMEOS) |
| + registrar_.Add(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, |
| + content::NotificationService::AllSources()); |
|
Ilya Sherman
2012/09/11 22:51:27
nit: Listening to notifications from AllSources()
SteveT
2012/09/14 20:25:09
Did the work to resolve all this.
|
| +#endif |
| +} |
| + |
| +ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() { |
| + net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| + |
| +#if defined(OS_CHROMEOS) |
| + registrar_.Remove(this, chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, |
| + content::NotificationService::AllSources()); |
|
Ilya Sherman
2012/09/11 22:51:27
nit: Listening to notifications from AllSources()
SteveT
2012/09/14 20:25:09
Done.
|
| +#endif |
| +} |
| + |
| +void ResourceRequestAllowedNotifier::AddObserver(Observer* observer) { |
| + DCHECK(observer_list_.size() == 0) << "One service per notifier."; |
|
Ilya Sherman
2012/09/11 22:51:27
nit: Please drop the """<< "One service per notifi
Alexei Svitkine (slow)
2012/09/12 15:15:46
If you're enforcing this, why not just make it enf
SteveT
2012/09/14 20:25:09
Didn't realize that was the case. I'll drop the lo
SteveT
2012/09/14 20:25:09
No, you're totally right. I've changed this class
|
| + observer_list_.AddObserver(observer); |
| +} |
| + |
| +void ResourceRequestAllowedNotifier::RemoveObserver(Observer* observer) { |
| + observer_list_.RemoveObserver(observer); |
| + DCHECK(observer_list_.size() == 0) << "One service per notifier."; |
|
Ilya Sherman
2012/09/11 22:51:27
nit: Please drop the logged line.
SteveT
2012/09/14 20:25:09
Done.
|
| +} |
| + |
| +bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() { |
| +#if defined(OS_CHROMEOS) |
| + was_waiting_for_user_to_accept_eula_ = |
| + !chromeos::WizardController::IsEulaAccepted(); |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Do we need to query this each time or can we just
SteveT
2012/09/14 20:25:09
I think your suggestion is reasonable, because onc
|
| +#endif |
| + was_offline_during_last_request_attempt_ = |
| + net::NetworkChangeNotifier::IsOffline(); |
| + |
| + // TODO(stevet): Remove this or rename. |
| + UMA_HISTOGRAM_BOOLEAN("Variations.NetworkAvailability", |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Maybe keep this histogram in variations service an
SteveT
2012/09/14 20:25:09
I am considering just sunsetting, since we already
|
| + !was_offline_during_last_request_attempt_); |
| + |
| + return |
| +#if defined(OS_CHROMEOS) |
| + !was_waiting_for_user_to_accept_eula_ && |
| +#endif |
| + !was_offline_during_last_request_attempt_; |
| +} |
| + |
| +#if defined(OS_CHROMEOS) |
| +void ResourceRequestAllowedNotifier::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK(type == chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED); |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Can you use DCHECK_EQ() here?
SteveT
2012/09/14 20:25:09
Done.
|
| + // If the observing service did not fail earlier due to an unaccepted EULA, |
| + // do not notify it. |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Wouldn't the condition you describe in this commen
Nikita (slow)
2012/09/12 15:20:23
No, it is not possible.
|
| + if (was_waiting_for_user_to_accept_eula_) { |
| + DVLOG(1) << "EULA was accepted."; |
|
Ilya Sherman
2012/09/11 22:51:27
Is this DVLOG just to help you while developing, o
SteveT
2012/09/14 20:25:09
Well I expect it to be useful in the future as dev
|
| + MaybeNotifyObservers(); |
| + } |
| +} |
| +#endif |
| + |
| +// net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| +void ResourceRequestAllowedNotifier::OnConnectionTypeChanged( |
| + net::NetworkChangeNotifier::ConnectionType type) { |
| + if (was_offline_during_last_request_attempt_ && |
| + type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Can you add a comment explaining the if statement
SteveT
2012/09/14 20:25:09
Done.
|
| + DVLOG(1) << "Network came back online."; |
| + MaybeNotifyObservers(); |
| + } |
| +} |
| + |
| +void ResourceRequestAllowedNotifier:: |
| + SetWasOfflineDuringLastRequestAttemptForTesting(bool offline) { |
| + was_offline_during_last_request_attempt_ = offline; |
| +} |
| + |
| +void ResourceRequestAllowedNotifier::MaybeNotifyObservers() { |
| + // Need to ensure that all criteria is met before notifying observers. |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Nit: "is" -> "are".
SteveT
2012/09/14 20:25:09
Done.
|
| + if (ResourceRequestsAllowed()) { |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
I think this is problematic per what mmenke said i
SteveT
2012/09/14 20:25:09
X being one of Eula-accepted or network-online? So
|
| + DVLOG(1) << "Notifying observer of state change."; |
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnResourceRequestsAllowed()); |
| + } |
| +} |