• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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_MAC_H_
6 #define NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_MAC_H_
7 
8 #include <memory>
9 
10 #include "base/compiler_specific.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/observer_list.h"
14 #include "net/base/network_config_watcher_mac.h"
15 #include "net/proxy_resolution/proxy_config_service.h"
16 #include "net/proxy_resolution/proxy_config_with_annotation.h"
17 
18 namespace base {
19 class SequencedTaskRunner;
20 }  // namespace base
21 
22 namespace net {
23 
24 class ProxyConfigServiceMac : public ProxyConfigService {
25  public:
26   // Constructs a ProxyConfigService that watches the Mac OS system settings.
27   // This instance is expected to be operated and deleted on
28   // |sequenced_task_runner| (however it may be constructed elsewhere).
29   explicit ProxyConfigServiceMac(
30       const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
31       const NetworkTrafficAnnotationTag& traffic_annotation);
32 
33   ProxyConfigServiceMac(const ProxyConfigServiceMac&) = delete;
34   ProxyConfigServiceMac& operator=(const ProxyConfigServiceMac&) = delete;
35 
36   ~ProxyConfigServiceMac() override;
37 
38  public:
39   // ProxyConfigService implementation:
40   void AddObserver(Observer* observer) override;
41   void RemoveObserver(Observer* observer) override;
42   ConfigAvailability GetLatestProxyConfig(
43       ProxyConfigWithAnnotation* config) override;
44 
45  private:
46   class Helper;
47 
48   // Forwarder just exists to keep the NetworkConfigWatcherMac API out of
49   // ProxyConfigServiceMac's public API.
50   class Forwarder : public NetworkConfigWatcherMac::Delegate {
51    public:
Forwarder(ProxyConfigServiceMac * proxy_config_service)52     explicit Forwarder(ProxyConfigServiceMac* proxy_config_service)
53         : proxy_config_service_(proxy_config_service) {}
54 
55     Forwarder(const Forwarder&) = delete;
56     Forwarder& operator=(const Forwarder&) = delete;
57 
58     // NetworkConfigWatcherMac::Delegate implementation:
StartReachabilityNotifications()59     void StartReachabilityNotifications() override {}
60     void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store) override;
61     void OnNetworkConfigChange(CFArrayRef changed_keys) override;
62 
63    private:
64     const raw_ptr<ProxyConfigServiceMac> proxy_config_service_;
65   };
66 
67   // Methods directly called by the NetworkConfigWatcherMac::Delegate:
68   void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store);
69   void OnNetworkConfigChange(CFArrayRef changed_keys);
70 
71   // Called when the proxy configuration has changed, to notify the observers.
72   void OnProxyConfigChanged(const ProxyConfigWithAnnotation& new_config);
73 
74   Forwarder forwarder_;
75   std::unique_ptr<const NetworkConfigWatcherMac> config_watcher_;
76 
77   base::ObserverList<Observer>::Unchecked observers_;
78 
79   // Holds the last system proxy settings that we fetched.
80   bool has_fetched_config_ = false;
81   ProxyConfigWithAnnotation last_config_fetched_;
82 
83   scoped_refptr<Helper> helper_;
84 
85   // The task runner that |this| will be operated on.
86   const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
87 
88   const NetworkTrafficAnnotationTag traffic_annotation_;
89 };
90 
91 }  // namespace net
92 
93 #endif  // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_MAC_H_
94