1 // Copyright (c) 2012 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_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/macros.h"
12 #include "base/memory/ref_counted.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   ~ProxyConfigServiceMac() override;
33 
34  public:
35   // ProxyConfigService implementation:
36   void AddObserver(Observer* observer) override;
37   void RemoveObserver(Observer* observer) override;
38   ConfigAvailability GetLatestProxyConfig(
39       ProxyConfigWithAnnotation* config) override;
40 
41  private:
42   class Helper;
43 
44   // Forwarder just exists to keep the NetworkConfigWatcherMac API out of
45   // ProxyConfigServiceMac's public API.
46   class Forwarder : public NetworkConfigWatcherMac::Delegate {
47    public:
Forwarder(ProxyConfigServiceMac * proxy_config_service)48     explicit Forwarder(ProxyConfigServiceMac* proxy_config_service)
49         : proxy_config_service_(proxy_config_service) {}
50 
51     // NetworkConfigWatcherMac::Delegate implementation:
StartReachabilityNotifications()52     void StartReachabilityNotifications() override {}
53     void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store) override;
54     void OnNetworkConfigChange(CFArrayRef changed_keys) override;
55 
56    private:
57     ProxyConfigServiceMac* const proxy_config_service_;
58     DISALLOW_COPY_AND_ASSIGN(Forwarder);
59   };
60 
61   // Methods directly called by the NetworkConfigWatcherMac::Delegate:
62   void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store);
63   void OnNetworkConfigChange(CFArrayRef changed_keys);
64 
65   // Called when the proxy configuration has changed, to notify the observers.
66   void OnProxyConfigChanged(const ProxyConfigWithAnnotation& new_config);
67 
68   Forwarder forwarder_;
69   std::unique_ptr<const NetworkConfigWatcherMac> config_watcher_;
70 
71   base::ObserverList<Observer>::Unchecked observers_;
72 
73   // Holds the last system proxy settings that we fetched.
74   bool has_fetched_config_;
75   ProxyConfigWithAnnotation last_config_fetched_;
76 
77   scoped_refptr<Helper> helper_;
78 
79   // The task runner that |this| will be operated on.
80   const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
81 
82   const NetworkTrafficAnnotationTag traffic_annotation_;
83 
84   DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceMac);
85 };
86 
87 }  // namespace net
88 
89 #endif  // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_MAC_H_
90