1 // Copyright 2013 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/web_resource/resource_request_allowed_notifier.h" 6 7 #include "base/command_line.h" 8 #include "chrome/common/chrome_switches.h" 9 10 ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier() 11 : observer_requested_permission_(false), 12 waiting_for_network_(false), 13 waiting_for_user_to_accept_eula_(false), 14 observer_(NULL) { 15 } 16 17 ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() { 18 if (observer_) 19 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); 20 } 21 22 void ResourceRequestAllowedNotifier::Init(Observer* observer) { 23 DCHECK(!observer_ && observer); 24 observer_ = observer; 25 26 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); 27 28 // Check this state during initialization. It is not expected to change until 29 // the corresponding notification is received. 30 waiting_for_network_ = net::NetworkChangeNotifier::IsOffline(); 31 32 eula_notifier_.reset(CreateEulaNotifier()); 33 if (eula_notifier_) { 34 eula_notifier_->Init(this); 35 waiting_for_user_to_accept_eula_ = !eula_notifier_->IsEulaAccepted(); 36 } 37 } 38 39 bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() { 40 if (CommandLine::ForCurrentProcess()->HasSwitch( 41 switches::kDisableBackgroundNetworking)) { 42 return false; 43 } 44 45 // The observer requested permission. Return the current criteria state and 46 // set a flag to remind this class to notify the observer once the criteria 47 // is met. 48 observer_requested_permission_ = waiting_for_user_to_accept_eula_ || 49 waiting_for_network_; 50 return !observer_requested_permission_; 51 } 52 53 void ResourceRequestAllowedNotifier::SetWaitingForNetworkForTesting( 54 bool waiting) { 55 waiting_for_network_ = waiting; 56 } 57 58 void ResourceRequestAllowedNotifier::SetWaitingForEulaForTesting( 59 bool waiting) { 60 waiting_for_user_to_accept_eula_ = waiting; 61 } 62 63 void ResourceRequestAllowedNotifier::SetObserverRequestedForTesting( 64 bool requested) { 65 observer_requested_permission_ = requested; 66 } 67 68 void ResourceRequestAllowedNotifier::MaybeNotifyObserver() { 69 // Need to ensure that all criteria are met before notifying observers. 70 if (observer_requested_permission_ && ResourceRequestsAllowed()) { 71 DVLOG(1) << "Notifying observer of state change."; 72 observer_->OnResourceRequestsAllowed(); 73 // Reset this so the observer is not informed again unless they check 74 // ResourceRequestsAllowed again. 75 observer_requested_permission_ = false; 76 } 77 } 78 79 EulaAcceptedNotifier* ResourceRequestAllowedNotifier::CreateEulaNotifier() { 80 return EulaAcceptedNotifier::Create(); 81 } 82 83 void ResourceRequestAllowedNotifier::OnEulaAccepted() { 84 // This flag should have been set if this was waiting on the EULA 85 // notification. 86 DCHECK(waiting_for_user_to_accept_eula_); 87 DVLOG(1) << "EULA was accepted."; 88 waiting_for_user_to_accept_eula_ = false; 89 MaybeNotifyObserver(); 90 } 91 92 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged( 93 net::NetworkChangeNotifier::ConnectionType type) { 94 // Only attempt to notify observers if this was previously waiting for the 95 // network to reconnect, and new network state is actually available. This 96 // prevents the notifier from notifying the observer if the notifier was never 97 // waiting on the network, or if the network changes from one online state 98 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were 99 // flaky). 100 if (waiting_for_network_ && 101 type != net::NetworkChangeNotifier::CONNECTION_NONE) { 102 waiting_for_network_ = false; 103 DVLOG(1) << "Network came back online."; 104 MaybeNotifyObserver(); 105 } else if (!waiting_for_network_ && 106 type == net::NetworkChangeNotifier::CONNECTION_NONE) { 107 waiting_for_network_ = true; 108 DVLOG(1) << "Network went offline."; 109 } 110 } 111