Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / web_resource / resource_request_allowed_notifier.h
blob4495aa2ecf7976cc6d29149a1c1b297c4b3be9da
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 CHROME_BROWSER_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
6 #define CHROME_BROWSER_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
8 #include "chrome/browser/web_resource/eula_accepted_notifier.h"
9 #include "net/base/network_change_notifier.h"
11 // This class informs an interested observer when resource requests over the
12 // network are permitted.
14 // Currently, the criteria for allowing resource requests are:
15 // 1. The network is currently available,
16 // 2. The EULA was accepted by the user (ChromeOS only), and
17 // 3. The --disable-background-networking command line switch is not set.
19 // Interested services should add themselves as an observer of
20 // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
21 // requests are permitted. If it returns true, they can go ahead and make their
22 // request. If it returns false, ResourceRequestAllowedNotifier will notify the
23 // service when the criteria is met.
25 // If ResourceRequestsAllowed returns true the first time,
26 // ResourceRequestAllowedNotifier will not notify the service in the future.
28 // Note that this class handles the criteria state for a single service, so
29 // services should keep their own instance of this class rather than sharing a
30 // global instance.
31 class ResourceRequestAllowedNotifier
32 : public EulaAcceptedNotifier::Observer,
33 public net::NetworkChangeNotifier::ConnectionTypeObserver {
34 public:
35 // Observes resource request allowed state changes.
36 class Observer {
37 public:
38 virtual void OnResourceRequestsAllowed() = 0;
41 // Specifies the resource request allowed state.
42 enum State {
43 ALLOWED,
44 DISALLOWED_EULA_NOT_ACCEPTED,
45 DISALLOWED_NETWORK_DOWN,
46 DISALLOWED_COMMAND_LINE_DISABLED,
49 ResourceRequestAllowedNotifier();
50 virtual ~ResourceRequestAllowedNotifier();
52 // Sets |observer| as the service to be notified by this instance, and
53 // performs initial checks on the criteria. |observer| may not be NULL.
54 // This is to be called immediately after construction of an instance of
55 // ResourceRequestAllowedNotifier to pass it the interested service.
56 void Init(Observer* observer);
58 // Returns whether resource requests are allowed, per the various criteria.
59 // If not, this call will set some flags so it knows to notify the observer
60 // if the criteria change. Note that the observer will not be notified unless
61 // it calls this method first.
62 // This is virtual so it can be overridden for tests.
63 virtual State GetResourceRequestsAllowedState();
65 // Convenience function, equivalent to:
66 // GetResourceRequestsAllowedState() == ALLOWED.
67 bool ResourceRequestsAllowed();
69 void SetWaitingForNetworkForTesting(bool waiting);
70 void SetWaitingForEulaForTesting(bool waiting);
71 void SetObserverRequestedForTesting(bool requested);
73 protected:
74 // Notifies the observer if all criteria needed for resource requests are met.
75 // This is protected so it can be called from subclasses for testing.
76 void MaybeNotifyObserver();
78 private:
79 // Creates the EulaAcceptNotifier or NULL if one is not needed. Virtual so
80 // that it can be overridden by test subclasses.
81 virtual EulaAcceptedNotifier* CreateEulaNotifier();
83 // EulaAcceptedNotifier::Observer overrides:
84 virtual void OnEulaAccepted() OVERRIDE;
86 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
87 virtual void OnConnectionTypeChanged(
88 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
90 // Tracks whether or not the observer/service depending on this class actually
91 // requested permission to make a request or not. If it did not, then this
92 // class should not notify it even if the criteria is met.
93 bool observer_requested_permission_;
95 // Tracks network connectivity criteria.
96 bool waiting_for_network_;
98 // Tracks EULA acceptance criteria.
99 bool waiting_for_user_to_accept_eula_;
101 // Platform-specific notifier of EULA acceptance, or NULL if not needed.
102 scoped_ptr<EulaAcceptedNotifier> eula_notifier_;
104 // Observing service interested in request permissions.
105 Observer* observer_;
107 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier);
110 #endif // CHROME_BROWSER_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_