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 #ifndef COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
6 #define COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
8 #include "components/web_resource/eula_accepted_notifier.h"
9 #include "net/base/network_change_notifier.h"
13 namespace web_resource
{
15 // This class informs an interested observer when resource requests over the
16 // network are permitted.
18 // Currently, the criteria for allowing resource requests are:
19 // 1. The network is currently available,
20 // 2. The EULA was accepted by the user (ChromeOS only), and
21 // 3. The --disable-background-networking command line switch is not set.
23 // Interested services should add themselves as an observer of
24 // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
25 // requests are permitted. If it returns true, they can go ahead and make their
26 // request. If it returns false, ResourceRequestAllowedNotifier will notify the
27 // service when the criteria is met.
29 // If ResourceRequestsAllowed returns true the first time,
30 // ResourceRequestAllowedNotifier will not notify the service in the future.
32 // Note that this class handles the criteria state for a single service, so
33 // services should keep their own instance of this class rather than sharing a
35 class ResourceRequestAllowedNotifier
36 : public EulaAcceptedNotifier::Observer
,
37 public net::NetworkChangeNotifier::ConnectionTypeObserver
{
39 // Observes resource request allowed state changes.
42 virtual void OnResourceRequestsAllowed() = 0;
45 // Specifies the resource request allowed state.
48 DISALLOWED_EULA_NOT_ACCEPTED
,
49 DISALLOWED_NETWORK_DOWN
,
50 DISALLOWED_COMMAND_LINE_DISABLED
,
53 // Creates a new ResourceRequestAllowedNotifier.
54 // |local_state| is the PrefService to observe.
55 // |disable_network_switch| is the command line switch to disable network
56 // activity. It is expected to outlive the ResourceRequestAllowedNotifier and
58 ResourceRequestAllowedNotifier(PrefService
* local_state
,
59 const char* disable_network_switch
);
60 ~ResourceRequestAllowedNotifier() override
;
62 // Sets |observer| as the service to be notified by this instance, and
63 // performs initial checks on the criteria. |observer| may not be null.
64 // This is to be called immediately after construction of an instance of
65 // ResourceRequestAllowedNotifier to pass it the interested service.
66 void Init(Observer
* observer
);
68 // Returns whether resource requests are allowed, per the various criteria.
69 // If not, this call will set some flags so it knows to notify the observer
70 // if the criteria change. Note that the observer will not be notified unless
71 // it calls this method first.
72 // This is virtual so it can be overridden for tests.
73 virtual State
GetResourceRequestsAllowedState();
75 // Convenience function, equivalent to:
76 // GetResourceRequestsAllowedState() == ALLOWED.
77 bool ResourceRequestsAllowed();
79 void SetWaitingForNetworkForTesting(bool waiting
);
80 void SetWaitingForEulaForTesting(bool waiting
);
81 void SetObserverRequestedForTesting(bool requested
);
84 // Notifies the observer if all criteria needed for resource requests are met.
85 // This is protected so it can be called from subclasses for testing.
86 void MaybeNotifyObserver();
89 // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so
90 // that it can be overridden by test subclasses.
91 virtual EulaAcceptedNotifier
* CreateEulaNotifier();
93 // EulaAcceptedNotifier::Observer overrides:
94 void OnEulaAccepted() override
;
96 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
97 void OnConnectionTypeChanged(
98 net::NetworkChangeNotifier::ConnectionType type
) override
;
100 // Name of the command line switch to disable the network activity.
101 const char* disable_network_switch_
;
103 // The local state this class is observing.
104 PrefService
* local_state_
;
106 // Tracks whether or not the observer/service depending on this class actually
107 // requested permission to make a request or not. If it did not, then this
108 // class should not notify it even if the criteria is met.
109 bool observer_requested_permission_
;
111 // Tracks network connectivity criteria.
112 bool waiting_for_network_
;
114 // Tracks EULA acceptance criteria.
115 bool waiting_for_user_to_accept_eula_
;
117 // Platform-specific notifier of EULA acceptance, or null if not needed.
118 scoped_ptr
<EulaAcceptedNotifier
> eula_notifier_
;
120 // Observing service interested in request permissions.
123 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier
);
126 } // namespace web_resource
128 #endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_