This source file includes following definitions.
- observer_
- Init
- GetResourceRequestsAllowedState
- ResourceRequestsAllowed
- SetWaitingForNetworkForTesting
- SetWaitingForEulaForTesting
- SetObserverRequestedForTesting
- MaybeNotifyObserver
- CreateEulaNotifier
- OnEulaAccepted
- OnConnectionTypeChanged
#include "chrome/browser/web_resource/resource_request_allowed_notifier.h"
#include "base/command_line.h"
#include "chrome/common/chrome_switches.h"
ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier()
: observer_requested_permission_(false),
waiting_for_network_(false),
waiting_for_user_to_accept_eula_(false),
observer_(NULL) {
}
ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() {
if (observer_)
net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
}
void ResourceRequestAllowedNotifier::Init(Observer* observer) {
DCHECK(!observer_ && observer);
observer_ = observer;
net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
waiting_for_network_ = net::NetworkChangeNotifier::IsOffline();
eula_notifier_.reset(CreateEulaNotifier());
if (eula_notifier_) {
eula_notifier_->Init(this);
waiting_for_user_to_accept_eula_ = !eula_notifier_->IsEulaAccepted();
}
}
ResourceRequestAllowedNotifier::State
ResourceRequestAllowedNotifier::GetResourceRequestsAllowedState() {
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableBackgroundNetworking)) {
return DISALLOWED_COMMAND_LINE_DISABLED;
}
observer_requested_permission_ = waiting_for_user_to_accept_eula_ ||
waiting_for_network_;
if (!observer_requested_permission_)
return ALLOWED;
return waiting_for_user_to_accept_eula_ ? DISALLOWED_EULA_NOT_ACCEPTED :
DISALLOWED_NETWORK_DOWN;
}
bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() {
return GetResourceRequestsAllowedState() == ALLOWED;
}
void ResourceRequestAllowedNotifier::SetWaitingForNetworkForTesting(
bool waiting) {
waiting_for_network_ = waiting;
}
void ResourceRequestAllowedNotifier::SetWaitingForEulaForTesting(
bool waiting) {
waiting_for_user_to_accept_eula_ = waiting;
}
void ResourceRequestAllowedNotifier::SetObserverRequestedForTesting(
bool requested) {
observer_requested_permission_ = requested;
}
void ResourceRequestAllowedNotifier::MaybeNotifyObserver() {
if (observer_requested_permission_ && ResourceRequestsAllowed()) {
DVLOG(1) << "Notifying observer of state change.";
observer_->OnResourceRequestsAllowed();
observer_requested_permission_ = false;
}
}
EulaAcceptedNotifier* ResourceRequestAllowedNotifier::CreateEulaNotifier() {
return EulaAcceptedNotifier::Create();
}
void ResourceRequestAllowedNotifier::OnEulaAccepted() {
DCHECK(waiting_for_user_to_accept_eula_);
DVLOG(1) << "EULA was accepted.";
waiting_for_user_to_accept_eula_ = false;
MaybeNotifyObserver();
}
void ResourceRequestAllowedNotifier::OnConnectionTypeChanged(
net::NetworkChangeNotifier::ConnectionType type) {
if (waiting_for_network_ &&
type != net::NetworkChangeNotifier::CONNECTION_NONE) {
waiting_for_network_ = false;
DVLOG(1) << "Network came back online.";
MaybeNotifyObserver();
} else if (!waiting_for_network_ &&
type == net::NetworkChangeNotifier::CONNECTION_NONE) {
waiting_for_network_ = true;
DVLOG(1) << "Network went offline.";
}
}