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