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.
5 #include "components/web_resource/resource_request_allowed_notifier.h"
7 #include "base/command_line.h"
9 namespace web_resource
{
11 ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier(
12 PrefService
* local_state
,
13 const char* disable_network_switch
)
14 : disable_network_switch_(disable_network_switch
),
15 local_state_(local_state
),
16 observer_requested_permission_(false),
17 waiting_for_network_(false),
18 waiting_for_user_to_accept_eula_(false),
22 ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() {
24 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
27 void ResourceRequestAllowedNotifier::Init(Observer
* observer
) {
28 DCHECK(!observer_
&& observer
);
31 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
33 // Check this state during initialization. It is not expected to change until
34 // the corresponding notification is received.
35 waiting_for_network_
= net::NetworkChangeNotifier::IsOffline();
37 eula_notifier_
.reset(CreateEulaNotifier());
39 eula_notifier_
->Init(this);
40 waiting_for_user_to_accept_eula_
= !eula_notifier_
->IsEulaAccepted();
44 ResourceRequestAllowedNotifier::State
45 ResourceRequestAllowedNotifier::GetResourceRequestsAllowedState() {
46 if (disable_network_switch_
&&
47 base::CommandLine::ForCurrentProcess()->HasSwitch(
48 disable_network_switch_
)) {
49 return DISALLOWED_COMMAND_LINE_DISABLED
;
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
55 observer_requested_permission_
= waiting_for_user_to_accept_eula_
||
57 if (!observer_requested_permission_
)
59 return waiting_for_user_to_accept_eula_
? DISALLOWED_EULA_NOT_ACCEPTED
:
60 DISALLOWED_NETWORK_DOWN
;
63 bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() {
64 return GetResourceRequestsAllowedState() == ALLOWED
;
67 void ResourceRequestAllowedNotifier::SetWaitingForNetworkForTesting(
69 waiting_for_network_
= waiting
;
72 void ResourceRequestAllowedNotifier::SetWaitingForEulaForTesting(bool waiting
) {
73 waiting_for_user_to_accept_eula_
= waiting
;
76 void ResourceRequestAllowedNotifier::SetObserverRequestedForTesting(
78 observer_requested_permission_
= requested
;
81 void ResourceRequestAllowedNotifier::MaybeNotifyObserver() {
82 // Need to ensure that all criteria are met before notifying observers.
83 if (observer_requested_permission_
&& ResourceRequestsAllowed()) {
84 DVLOG(1) << "Notifying observer of state change.";
85 observer_
->OnResourceRequestsAllowed();
86 // Reset this so the observer is not informed again unless they check
87 // ResourceRequestsAllowed again.
88 observer_requested_permission_
= false;
92 EulaAcceptedNotifier
* ResourceRequestAllowedNotifier::CreateEulaNotifier() {
93 return EulaAcceptedNotifier::Create(local_state_
);
96 void ResourceRequestAllowedNotifier::OnEulaAccepted() {
97 // This flag should have been set if this was waiting on the EULA
99 DCHECK(waiting_for_user_to_accept_eula_
);
100 DVLOG(1) << "EULA was accepted.";
101 waiting_for_user_to_accept_eula_
= false;
102 MaybeNotifyObserver();
105 void ResourceRequestAllowedNotifier::OnConnectionTypeChanged(
106 net::NetworkChangeNotifier::ConnectionType type
) {
107 // Only attempt to notify observers if this was previously waiting for the
108 // network to reconnect, and new network state is actually available. This
109 // prevents the notifier from notifying the observer if the notifier was never
110 // waiting on the network, or if the network changes from one online state
111 // to another (for example, Wifi to 3G, or Wifi to Wifi, if the network were
113 if (waiting_for_network_
&&
114 type
!= net::NetworkChangeNotifier::CONNECTION_NONE
) {
115 waiting_for_network_
= false;
116 DVLOG(1) << "Network came back online.";
117 MaybeNotifyObserver();
118 } else if (!waiting_for_network_
&&
119 type
== net::NetworkChangeNotifier::CONNECTION_NONE
) {
120 waiting_for_network_
= true;
121 DVLOG(1) << "Network went offline.";
125 } // namespace web_resource