blob: a6e9ac40e6bfb28f00c3ab0723ac887c97198a4a [file] [log] [blame]
[email protected]1aabf592013-06-07 08:53:011// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]d06ba1242012-09-25 20:51:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
drogerf8479942014-11-21 17:47:535#ifndef COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
6#define COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
[email protected]d06ba1242012-09-25 20:51:397
dcheng3f767dc32016-04-25 22:54:228#include <memory>
9
avi5dd91f82015-12-25 22:30:4610#include "base/macros.h"
drogerf8479942014-11-21 17:47:5311#include "components/web_resource/eula_accepted_notifier.h"
[email protected]d06ba1242012-09-25 20:51:3912#include "net/base/network_change_notifier.h"
13
drogerf4455482014-11-21 09:50:1814class PrefService;
15
drogerc4ccb832014-11-27 16:10:4916namespace web_resource {
17
[email protected]d06ba1242012-09-25 20:51:3918// This class informs an interested observer when resource requests over the
19// network are permitted.
20//
[email protected]f593f5bb2013-03-07 08:49:3421// Currently, the criteria for allowing resource requests are:
22// 1. The network is currently available,
23// 2. The EULA was accepted by the user (ChromeOS only), and
24// 3. The --disable-background-networking command line switch is not set.
[email protected]d06ba1242012-09-25 20:51:3925//
26// Interested services should add themselves as an observer of
27// ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
28// requests are permitted. If it returns true, they can go ahead and make their
29// request. If it returns false, ResourceRequestAllowedNotifier will notify the
30// service when the criteria is met.
31//
32// If ResourceRequestsAllowed returns true the first time,
33// ResourceRequestAllowedNotifier will not notify the service in the future.
34//
35// Note that this class handles the criteria state for a single service, so
36// services should keep their own instance of this class rather than sharing a
37// global instance.
[email protected]05d242d2013-04-08 21:49:4938class ResourceRequestAllowedNotifier
39 : public EulaAcceptedNotifier::Observer,
40 public net::NetworkChangeNotifier::ConnectionTypeObserver {
[email protected]d06ba1242012-09-25 20:51:3941 public:
42 // Observes resource request allowed state changes.
43 class Observer {
44 public:
[email protected]d06ba1242012-09-25 20:51:3945 virtual void OnResourceRequestsAllowed() = 0;
46 };
47
[email protected]06b9cde2013-11-21 07:38:0648 // Specifies the resource request allowed state.
49 enum State {
50 ALLOWED,
51 DISALLOWED_EULA_NOT_ACCEPTED,
52 DISALLOWED_NETWORK_DOWN,
53 DISALLOWED_COMMAND_LINE_DISABLED,
54 };
55
drogerf8479942014-11-21 17:47:5356 // Creates a new ResourceRequestAllowedNotifier.
57 // |local_state| is the PrefService to observe.
58 // |disable_network_switch| is the command line switch to disable network
drogerc4ccb832014-11-27 16:10:4959 // activity. It is expected to outlive the ResourceRequestAllowedNotifier and
60 // may be null.
drogerf8479942014-11-21 17:47:5361 ResourceRequestAllowedNotifier(PrefService* local_state,
62 const char* disable_network_switch);
Daniel Chenga542fca2014-10-21 09:51:2963 ~ResourceRequestAllowedNotifier() override;
[email protected]d06ba1242012-09-25 20:51:3964
65 // Sets |observer| as the service to be notified by this instance, and
drogerf8479942014-11-21 17:47:5366 // performs initial checks on the criteria. |observer| may not be null.
[email protected]d06ba1242012-09-25 20:51:3967 // This is to be called immediately after construction of an instance of
68 // ResourceRequestAllowedNotifier to pass it the interested service.
69 void Init(Observer* observer);
70
[email protected]06b9cde2013-11-21 07:38:0671 // Returns whether resource requests are allowed, per the various criteria.
72 // If not, this call will set some flags so it knows to notify the observer
73 // if the criteria change. Note that the observer will not be notified unless
74 // it calls this method first.
75 // This is virtual so it can be overridden for tests.
76 virtual State GetResourceRequestsAllowedState();
77
78 // Convenience function, equivalent to:
79 // GetResourceRequestsAllowedState() == ALLOWED.
80 bool ResourceRequestsAllowed();
[email protected]d06ba1242012-09-25 20:51:3981
[email protected]05d242d2013-04-08 21:49:4982 void SetWaitingForNetworkForTesting(bool waiting);
83 void SetWaitingForEulaForTesting(bool waiting);
[email protected]95af7892013-06-19 07:16:3384 void SetObserverRequestedForTesting(bool requested);
[email protected]d06ba1242012-09-25 20:51:3985
86 protected:
87 // Notifies the observer if all criteria needed for resource requests are met.
88 // This is protected so it can be called from subclasses for testing.
89 void MaybeNotifyObserver();
90
[email protected]d06ba1242012-09-25 20:51:3991 private:
drogerf8479942014-11-21 17:47:5392 // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so
[email protected]05d242d2013-04-08 21:49:4993 // that it can be overridden by test subclasses.
94 virtual EulaAcceptedNotifier* CreateEulaNotifier();
95
96 // EulaAcceptedNotifier::Observer overrides:
Daniel Chenga542fca2014-10-21 09:51:2997 void OnEulaAccepted() override;
[email protected]05d242d2013-04-08 21:49:4998
[email protected]d06ba1242012-09-25 20:51:3999 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
Daniel Chenga542fca2014-10-21 09:51:29100 void OnConnectionTypeChanged(
mostynbfb66cb4f2014-10-07 09:15:42101 net::NetworkChangeNotifier::ConnectionType type) override;
[email protected]d06ba1242012-09-25 20:51:39102
drogerf8479942014-11-21 17:47:53103 // Name of the command line switch to disable the network activity.
104 const char* disable_network_switch_;
105
drogerf4455482014-11-21 09:50:18106 // The local state this class is observing.
107 PrefService* local_state_;
108
[email protected]d06ba1242012-09-25 20:51:39109 // Tracks whether or not the observer/service depending on this class actually
110 // requested permission to make a request or not. If it did not, then this
111 // class should not notify it even if the criteria is met.
112 bool observer_requested_permission_;
113
114 // Tracks network connectivity criteria.
[email protected]05d242d2013-04-08 21:49:49115 bool waiting_for_network_;
[email protected]d06ba1242012-09-25 20:51:39116
[email protected]d06ba1242012-09-25 20:51:39117 // Tracks EULA acceptance criteria.
[email protected]05d242d2013-04-08 21:49:49118 bool waiting_for_user_to_accept_eula_;
[email protected]d06ba1242012-09-25 20:51:39119
drogerf8479942014-11-21 17:47:53120 // Platform-specific notifier of EULA acceptance, or null if not needed.
dcheng3f767dc32016-04-25 22:54:22121 std::unique_ptr<EulaAcceptedNotifier> eula_notifier_;
[email protected]d06ba1242012-09-25 20:51:39122
123 // Observing service interested in request permissions.
124 Observer* observer_;
125
126 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier);
127};
128
drogerc4ccb832014-11-27 16:10:49129} // namespace web_resource
130
drogerf8479942014-11-21 17:47:53131#endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_