blob: 8cbdfdb82cb9e1f71cf87b7500d9108e665177fc [file] [log] [blame]
Kaido Kert25902c62024-06-17 17:10:28 -07001// Copyright 2011 The Chromium Authors
Andrew Top0d1858f2019-05-15 22:01:47 -07002// 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"
Kaido Kert25902c62024-06-17 17:10:28 -07009#include "base/memory/scoped_refptr.h"
Andrew Top0d1858f2019-05-15 22:01:47 -070010#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
15namespace 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.
22class NET_EXPORT_PRIVATE PollingProxyConfigService : public ProxyConfigService {
23 public:
Kaido Kert25902c62024-06-17 17:10:28 -070024 PollingProxyConfigService(const PollingProxyConfigService&) = delete;
25 PollingProxyConfigService& operator=(const PollingProxyConfigService&) =
26 delete;
27
Andrew Top0d1858f2019-05-15 22:01:47 -070028 // 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;
Kaido Kert25902c62024-06-17 17:10:28 -070034 bool UsesPolling() override;
Andrew Top0d1858f2019-05-15 22:01:47 -070035
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_;
Andrew Top0d1858f2019-05-15 22:01:47 -070059};
60
61} // namespace net
62
63#endif // NET_PROXY_RESOLUTION_POLLING_PROXY_CONFIG_SERVICE_H_