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_apple.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 NetworkConfigWatcherApple API out of 49 // ProxyConfigServiceMac's public API. 50 class Forwarder : public NetworkConfigWatcherApple::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 // NetworkConfigWatcherApple::Delegate implementation: StartReachabilityNotifications()59 void StartReachabilityNotifications() override {} 60 void SetDynamicStoreNotificationKeys( 61 base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store) override; 62 void OnNetworkConfigChange(CFArrayRef changed_keys) override; CleanUpOnNotifierThread()63 void CleanUpOnNotifierThread() override {} 64 65 private: 66 const raw_ptr<ProxyConfigServiceMac> proxy_config_service_; 67 }; 68 69 // Methods directly called by the NetworkConfigWatcherApple::Delegate: 70 void SetDynamicStoreNotificationKeys( 71 base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store); 72 void OnNetworkConfigChange(CFArrayRef changed_keys); 73 74 // Called when the proxy configuration has changed, to notify the observers. 75 void OnProxyConfigChanged(const ProxyConfigWithAnnotation& new_config); 76 77 Forwarder forwarder_; 78 std::unique_ptr<const NetworkConfigWatcherApple> config_watcher_; 79 80 base::ObserverList<Observer>::Unchecked observers_; 81 82 // Holds the last system proxy settings that we fetched. 83 bool has_fetched_config_ = false; 84 ProxyConfigWithAnnotation last_config_fetched_; 85 86 scoped_refptr<Helper> helper_; 87 88 // The task runner that |this| will be operated on. 89 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; 90 91 const NetworkTrafficAnnotationTag traffic_annotation_; 92 }; 93 94 } // namespace net 95 96 #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_MAC_H_ 97