xref: /aosp_15_r20/external/cronet/net/proxy_resolution/polling_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_POLLING_PROXY_CONFIG_SERVICE_H_
6 #define NET_PROXY_RESOLUTION_POLLING_PROXY_CONFIG_SERVICE_H_
7 
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_refptr.h"
10 #include "base/time/time.h"
11 #include "net/base/net_export.h"
12 #include "net/proxy_resolution/proxy_config_service.h"
13 #include "net/traffic_annotation/network_traffic_annotation.h"
14 
15 namespace net {
16 
17 // PollingProxyConfigService is a base class for creating ProxyConfigService
18 // implementations that use polling to notice when settings have change.
19 //
20 // It runs code to get the current proxy settings on a background worker
21 // thread, and notifies registered observers when the value changes.
22 class NET_EXPORT_PRIVATE PollingProxyConfigService : public ProxyConfigService {
23  public:
24   PollingProxyConfigService(const PollingProxyConfigService&) = delete;
25   PollingProxyConfigService& operator=(const PollingProxyConfigService&) =
26       delete;
27 
28   // ProxyConfigService implementation:
29   void AddObserver(Observer* observer) override;
30   void RemoveObserver(Observer* observer) override;
31   ConfigAvailability GetLatestProxyConfig(
32       ProxyConfigWithAnnotation* config) override;
33   void OnLazyPoll() override;
34   bool UsesPolling() override;
35 
36  protected:
37   // Function for retrieving the current proxy configuration.
38   // Implementors must be threadsafe as the function will be invoked from
39   // worker threads.
40   typedef void (*GetConfigFunction)(NetworkTrafficAnnotationTag,
41                                     ProxyConfigWithAnnotation*);
42 
43   // Creates a polling-based ProxyConfigService which will test for new
44   // settings at most every |poll_interval| time by calling |get_config_func|
45   // on a worker thread.
46   PollingProxyConfigService(
47       base::TimeDelta poll_interval,
48       GetConfigFunction get_config_func,
49       const NetworkTrafficAnnotationTag& traffic_annotation);
50 
51   ~PollingProxyConfigService() override;
52 
53   // Polls for changes by posting a task to the worker pool.
54   void CheckForChangesNow();
55 
56  private:
57   class Core;
58   scoped_refptr<Core> core_;
59 };
60 
61 }  // namespace net
62 
63 #endif  // NET_PROXY_RESOLUTION_POLLING_PROXY_CONFIG_SERVICE_H_
64