xref: /aosp_15_r20/external/cronet/net/proxy_resolution/proxy_config_service.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_H_
6 #define NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_H_
7 
8 #include <memory>
9 
10 #include "base/memory/scoped_refptr.h"
11 #include "net/base/net_export.h"
12 
13 namespace base {
14 class SequencedTaskRunner;
15 }  // namespace base
16 
17 namespace net {
18 
19 class ProxyConfigWithAnnotation;
20 
21 // Service for watching when the proxy settings have changed.
22 class NET_EXPORT ProxyConfigService {
23  public:
24   // Indicates whether proxy configuration is valid, and if not, why.
25   enum ConfigAvailability {
26     // Configuration is pending, observers will be notified later.
27     CONFIG_PENDING,
28     // Configuration is present and valid.
29     CONFIG_VALID,
30     // No configuration is set.
31     CONFIG_UNSET
32   };
33 
34   // Observer for being notified when the proxy settings have changed.
35   class NET_EXPORT Observer {
36    public:
37     virtual ~Observer() = default;
38     // Notification callback that should be invoked by ProxyConfigService
39     // implementors whenever the configuration changes. |availability| indicates
40     // the new availability status and can be CONFIG_UNSET or CONFIG_VALID (in
41     // which case |config| contains the configuration). Implementors must not
42     // pass CONFIG_PENDING.
43     virtual void OnProxyConfigChanged(const ProxyConfigWithAnnotation& config,
44                                       ConfigAvailability availability) = 0;
45   };
46 
47   virtual ~ProxyConfigService() = default;
48 
49   // Adds/Removes an observer that will be called whenever the proxy
50   // configuration has changed.
51   virtual void AddObserver(Observer* observer) = 0;
52   virtual void RemoveObserver(Observer* observer) = 0;
53 
54   // Gets the most recent availability status. If a configuration is present,
55   // the proxy configuration is written to |config| and CONFIG_VALID is
56   // returned. Returns CONFIG_PENDING if it is not available yet. In this case,
57   // it is guaranteed that subscribed observers will be notified of a change at
58   // some point in the future once the configuration is available.
59   // Note that to avoid re-entrancy problems, implementations should not
60   // dispatch any change notifications from within this function.
61   virtual ConfigAvailability GetLatestProxyConfig(
62       ProxyConfigWithAnnotation* config) = 0;
63 
64   // ConfiguredProxyResolutionService will call this periodically during periods
65   // of activity. It can be used as a signal for polling-based implementations.
66   //
67   // Note that this is purely used as an optimization -- polling
68   // implementations could simply set a global timer that goes off every
69   // X seconds at which point they check for changes. However that has
70   // the disadvantage of doing continuous work even during idle periods.
OnLazyPoll()71   virtual void OnLazyPoll() {}
72 
73   // True if this implementation uses polling, i.e. needs `OnLazyPoll` to be
74   // called.
75   virtual bool UsesPolling();
76 
77   // Creates a config service appropriate for this platform that fetches the
78   // system proxy settings. |main_task_runner| is the sequence where the
79   // consumer of the ProxyConfigService will live.
80   static std::unique_ptr<ProxyConfigService> CreateSystemProxyConfigService(
81       scoped_refptr<base::SequencedTaskRunner> main_task_runner);
82 };
83 
84 }  // namespace net
85 
86 #endif  // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_H_
87