blob: a12b69b03bcfe9aefba563833d68dc6e8394eb73 [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
drogerf8479942014-11-21 17:47:538#include "components/web_resource/eula_accepted_notifier.h"
[email protected]d06ba1242012-09-25 20:51:399#include "net/base/network_change_notifier.h"
10
drogerf4455482014-11-21 09:50:1811class PrefService;
12
[email protected]d06ba1242012-09-25 20:51:3913// This class informs an interested observer when resource requests over the
14// network are permitted.
15//
[email protected]f593f5bb2013-03-07 08:49:3416// Currently, the criteria for allowing resource requests are:
17// 1. The network is currently available,
18// 2. The EULA was accepted by the user (ChromeOS only), and
19// 3. The --disable-background-networking command line switch is not set.
[email protected]d06ba1242012-09-25 20:51:3920//
21// Interested services should add themselves as an observer of
22// ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
23// requests are permitted. If it returns true, they can go ahead and make their
24// request. If it returns false, ResourceRequestAllowedNotifier will notify the
25// service when the criteria is met.
26//
27// If ResourceRequestsAllowed returns true the first time,
28// ResourceRequestAllowedNotifier will not notify the service in the future.
29//
30// Note that this class handles the criteria state for a single service, so
31// services should keep their own instance of this class rather than sharing a
32// global instance.
[email protected]05d242d2013-04-08 21:49:4933class ResourceRequestAllowedNotifier
34 : public EulaAcceptedNotifier::Observer,
35 public net::NetworkChangeNotifier::ConnectionTypeObserver {
[email protected]d06ba1242012-09-25 20:51:3936 public:
37 // Observes resource request allowed state changes.
38 class Observer {
39 public:
[email protected]d06ba1242012-09-25 20:51:3940 virtual void OnResourceRequestsAllowed() = 0;
41 };
42
[email protected]06b9cde2013-11-21 07:38:0643 // Specifies the resource request allowed state.
44 enum State {
45 ALLOWED,
46 DISALLOWED_EULA_NOT_ACCEPTED,
47 DISALLOWED_NETWORK_DOWN,
48 DISALLOWED_COMMAND_LINE_DISABLED,
49 };
50
drogerf8479942014-11-21 17:47:5351 // Creates a new ResourceRequestAllowedNotifier.
52 // |local_state| is the PrefService to observe.
53 // |disable_network_switch| is the command line switch to disable network
54 // activity, and is expected to outlive the ResourceRequestAllowedNotifier.
55 ResourceRequestAllowedNotifier(PrefService* local_state,
56 const char* disable_network_switch);
Daniel Chenga542fca2014-10-21 09:51:2957 ~ResourceRequestAllowedNotifier() override;
[email protected]d06ba1242012-09-25 20:51:3958
59 // Sets |observer| as the service to be notified by this instance, and
drogerf8479942014-11-21 17:47:5360 // performs initial checks on the criteria. |observer| may not be null.
[email protected]d06ba1242012-09-25 20:51:3961 // This is to be called immediately after construction of an instance of
62 // ResourceRequestAllowedNotifier to pass it the interested service.
63 void Init(Observer* observer);
64
[email protected]06b9cde2013-11-21 07:38:0665 // Returns whether resource requests are allowed, per the various criteria.
66 // If not, this call will set some flags so it knows to notify the observer
67 // if the criteria change. Note that the observer will not be notified unless
68 // it calls this method first.
69 // This is virtual so it can be overridden for tests.
70 virtual State GetResourceRequestsAllowedState();
71
72 // Convenience function, equivalent to:
73 // GetResourceRequestsAllowedState() == ALLOWED.
74 bool ResourceRequestsAllowed();
[email protected]d06ba1242012-09-25 20:51:3975
[email protected]05d242d2013-04-08 21:49:4976 void SetWaitingForNetworkForTesting(bool waiting);
77 void SetWaitingForEulaForTesting(bool waiting);
[email protected]95af7892013-06-19 07:16:3378 void SetObserverRequestedForTesting(bool requested);
[email protected]d06ba1242012-09-25 20:51:3979
80 protected:
81 // Notifies the observer if all criteria needed for resource requests are met.
82 // This is protected so it can be called from subclasses for testing.
83 void MaybeNotifyObserver();
84
[email protected]d06ba1242012-09-25 20:51:3985 private:
drogerf8479942014-11-21 17:47:5386 // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so
[email protected]05d242d2013-04-08 21:49:4987 // that it can be overridden by test subclasses.
88 virtual EulaAcceptedNotifier* CreateEulaNotifier();
89
90 // EulaAcceptedNotifier::Observer overrides:
Daniel Chenga542fca2014-10-21 09:51:2991 void OnEulaAccepted() override;
[email protected]05d242d2013-04-08 21:49:4992
[email protected]d06ba1242012-09-25 20:51:3993 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
Daniel Chenga542fca2014-10-21 09:51:2994 void OnConnectionTypeChanged(
mostynbfb66cb4f2014-10-07 09:15:4295 net::NetworkChangeNotifier::ConnectionType type) override;
[email protected]d06ba1242012-09-25 20:51:3996
drogerf8479942014-11-21 17:47:5397 // Name of the command line switch to disable the network activity.
98 const char* disable_network_switch_;
99
drogerf4455482014-11-21 09:50:18100 // The local state this class is observing.
101 PrefService* local_state_;
102
[email protected]d06ba1242012-09-25 20:51:39103 // Tracks whether or not the observer/service depending on this class actually
104 // requested permission to make a request or not. If it did not, then this
105 // class should not notify it even if the criteria is met.
106 bool observer_requested_permission_;
107
108 // Tracks network connectivity criteria.
[email protected]05d242d2013-04-08 21:49:49109 bool waiting_for_network_;
[email protected]d06ba1242012-09-25 20:51:39110
[email protected]d06ba1242012-09-25 20:51:39111 // Tracks EULA acceptance criteria.
[email protected]05d242d2013-04-08 21:49:49112 bool waiting_for_user_to_accept_eula_;
[email protected]d06ba1242012-09-25 20:51:39113
drogerf8479942014-11-21 17:47:53114 // Platform-specific notifier of EULA acceptance, or null if not needed.
[email protected]05d242d2013-04-08 21:49:49115 scoped_ptr<EulaAcceptedNotifier> eula_notifier_;
[email protected]d06ba1242012-09-25 20:51:39116
117 // Observing service interested in request permissions.
118 Observer* observer_;
119
120 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier);
121};
122
drogerf8479942014-11-21 17:47:53123#endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_