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