blob: fff25e8e6341473f479a282e9d379f58d7f89d3d [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
Clark DuVall5bf72b12018-08-31 19:53:3010#include "base/feature_list.h"
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
drogerf8479942014-11-21 17:47:5312#include "components/web_resource/eula_accepted_notifier.h"
Clark DuVall5bf72b12018-08-31 19:53:3013#include "services/network/public/cpp/network_connection_tracker.h"
[email protected]d06ba1242012-09-25 20:51:3914
drogerf4455482014-11-21 09:50:1815class PrefService;
16
drogerc4ccb832014-11-27 16:10:4917namespace web_resource {
18
[email protected]d06ba1242012-09-25 20:51:3919// This class informs an interested observer when resource requests over the
20// network are permitted.
21//
[email protected]f593f5bb2013-03-07 08:49:3422// Currently, the criteria for allowing resource requests are:
23// 1. The network is currently available,
24// 2. The EULA was accepted by the user (ChromeOS only), and
25// 3. The --disable-background-networking command line switch is not set.
[email protected]d06ba1242012-09-25 20:51:3926//
27// Interested services should add themselves as an observer of
28// ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
29// requests are permitted. If it returns true, they can go ahead and make their
30// request. If it returns false, ResourceRequestAllowedNotifier will notify the
31// service when the criteria is met.
32//
33// If ResourceRequestsAllowed returns true the first time,
34// ResourceRequestAllowedNotifier will not notify the service in the future.
35//
36// Note that this class handles the criteria state for a single service, so
37// services should keep their own instance of this class rather than sharing a
38// global instance.
[email protected]05d242d2013-04-08 21:49:4939class ResourceRequestAllowedNotifier
40 : public EulaAcceptedNotifier::Observer,
Clark DuVall9a92e392018-09-20 18:02:4741 public network::NetworkConnectionTracker::NetworkConnectionObserver {
[email protected]d06ba1242012-09-25 20:51:3942 public:
43 // Observes resource request allowed state changes.
44 class Observer {
45 public:
[email protected]d06ba1242012-09-25 20:51:3946 virtual void OnResourceRequestsAllowed() = 0;
Eugene But9b0497e2017-09-26 16:36:5947 virtual ~Observer() = default;
[email protected]d06ba1242012-09-25 20:51:3948 };
49
[email protected]06b9cde2013-11-21 07:38:0650 // Specifies the resource request allowed state.
51 enum State {
52 ALLOWED,
53 DISALLOWED_EULA_NOT_ACCEPTED,
54 DISALLOWED_NETWORK_DOWN,
55 DISALLOWED_COMMAND_LINE_DISABLED,
Clark DuVall5bf72b12018-08-31 19:53:3056 DISALLOWED_NETWORK_STATE_NOT_INITIALIZED,
[email protected]06b9cde2013-11-21 07:38:0657 };
58
Clark DuVall5bf72b12018-08-31 19:53:3059 using NetworkConnectionTrackerGetter =
60 base::OnceCallback<network::NetworkConnectionTracker*()>;
61
drogerf8479942014-11-21 17:47:5362 // Creates a new ResourceRequestAllowedNotifier.
63 // |local_state| is the PrefService to observe.
64 // |disable_network_switch| is the command line switch to disable network
drogerc4ccb832014-11-27 16:10:4965 // activity. It is expected to outlive the ResourceRequestAllowedNotifier and
66 // may be null.
Clark DuVall5bf72b12018-08-31 19:53:3067 ResourceRequestAllowedNotifier(
68 PrefService* local_state,
69 const char* disable_network_switch,
70 NetworkConnectionTrackerGetter network_connection_tracker_getter);
Peter Boström09c01822021-09-20 22:43:2771
72 ResourceRequestAllowedNotifier(const ResourceRequestAllowedNotifier&) =
73 delete;
74 ResourceRequestAllowedNotifier& operator=(
75 const ResourceRequestAllowedNotifier&) = delete;
76
Daniel Chenga542fca2014-10-21 09:51:2977 ~ResourceRequestAllowedNotifier() override;
[email protected]d06ba1242012-09-25 20:51:3978
79 // Sets |observer| as the service to be notified by this instance, and
drogerf8479942014-11-21 17:47:5380 // performs initial checks on the criteria. |observer| may not be null.
[email protected]d06ba1242012-09-25 20:51:3981 // This is to be called immediately after construction of an instance of
Clark DuVall5bf72b12018-08-31 19:53:3082 // ResourceRequestAllowedNotifier to pass it the interested service. Set
83 // |leaky| to true if this class will not be destructed before shutdown.
84 void Init(Observer* observer, bool leaky);
[email protected]d06ba1242012-09-25 20:51:3985
[email protected]06b9cde2013-11-21 07:38:0686 // Returns whether resource requests are allowed, per the various criteria.
87 // If not, this call will set some flags so it knows to notify the observer
88 // if the criteria change. Note that the observer will not be notified unless
89 // it calls this method first.
90 // This is virtual so it can be overridden for tests.
91 virtual State GetResourceRequestsAllowedState();
92
93 // Convenience function, equivalent to:
94 // GetResourceRequestsAllowedState() == ALLOWED.
95 bool ResourceRequestsAllowed();
[email protected]d06ba1242012-09-25 20:51:3996
[email protected]05d242d2013-04-08 21:49:4997 void SetWaitingForEulaForTesting(bool waiting);
[email protected]95af7892013-06-19 07:16:3398 void SetObserverRequestedForTesting(bool requested);
Clark DuVall5bf72b12018-08-31 19:53:3099 void SetConnectionTypeForTesting(
100 network::mojom::ConnectionType connection_type);
[email protected]d06ba1242012-09-25 20:51:39101
102 protected:
103 // Notifies the observer if all criteria needed for resource requests are met.
104 // This is protected so it can be called from subclasses for testing.
105 void MaybeNotifyObserver();
106
[email protected]d06ba1242012-09-25 20:51:39107 private:
drogerf8479942014-11-21 17:47:53108 // Creates the EulaAcceptNotifier or null if one is not needed. Virtual so
[email protected]05d242d2013-04-08 21:49:49109 // that it can be overridden by test subclasses.
110 virtual EulaAcceptedNotifier* CreateEulaNotifier();
111
112 // EulaAcceptedNotifier::Observer overrides:
Daniel Chenga542fca2014-10-21 09:51:29113 void OnEulaAccepted() override;
[email protected]05d242d2013-04-08 21:49:49114
Clark DuVall5bf72b12018-08-31 19:53:30115 // network::NetworkConnectionTracker::NetworkConnectionObserver overrides:
116 void OnConnectionChanged(network::mojom::ConnectionType type) override;
117
118 void SetConnectionType(network::mojom::ConnectionType connection_type);
119 bool IsOffline();
120
drogerf8479942014-11-21 17:47:53121 // Name of the command line switch to disable the network activity.
122 const char* disable_network_switch_;
123
drogerf4455482014-11-21 09:50:18124 // The local state this class is observing.
Keishi Hattori0e45c022021-11-27 09:25:52125 raw_ptr<PrefService> local_state_;
drogerf4455482014-11-21 09:50:18126
[email protected]d06ba1242012-09-25 20:51:39127 // Tracks whether or not the observer/service depending on this class actually
128 // requested permission to make a request or not. If it did not, then this
129 // class should not notify it even if the criteria is met.
130 bool observer_requested_permission_;
131
[email protected]d06ba1242012-09-25 20:51:39132 // Tracks EULA acceptance criteria.
[email protected]05d242d2013-04-08 21:49:49133 bool waiting_for_user_to_accept_eula_;
[email protected]d06ba1242012-09-25 20:51:39134
drogerf8479942014-11-21 17:47:53135 // Platform-specific notifier of EULA acceptance, or null if not needed.
dcheng3f767dc32016-04-25 22:54:22136 std::unique_ptr<EulaAcceptedNotifier> eula_notifier_;
[email protected]d06ba1242012-09-25 20:51:39137
138 // Observing service interested in request permissions.
Keishi Hattori0e45c022021-11-27 09:25:52139 raw_ptr<Observer> observer_;
[email protected]d06ba1242012-09-25 20:51:39140
Clark DuVall5bf72b12018-08-31 19:53:30141 NetworkConnectionTrackerGetter network_connection_tracker_getter_;
Keishi Hattori0e45c022021-11-27 09:25:52142 raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_ =
143 nullptr;
Clark DuVall5bf72b12018-08-31 19:53:30144 network::mojom::ConnectionType connection_type_ =
145 network::mojom::ConnectionType::CONNECTION_UNKNOWN;
146 bool connection_initialized_ = false;
147
Jeremy Roman5c341f6d2019-07-15 15:56:10148 base::WeakPtrFactory<ResourceRequestAllowedNotifier> weak_factory_{this};
[email protected]d06ba1242012-09-25 20:51:39149};
150
Clark DuVall5bf72b12018-08-31 19:53:30151extern const base::Feature kResourceRequestAllowedMigration;
152
drogerc4ccb832014-11-27 16:10:49153} // namespace web_resource
154
drogerf8479942014-11-21 17:47:53155#endif // COMPONENTS_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_