This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
cnet_proxy_service.cc
125 lines (107 loc) · 3.74 KB
/
cnet_proxy_service.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014, Yahoo! Inc.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "yahoo/cnet/cnet_proxy_service.h"
namespace cnet {
ProxyConfigService::ProxyConfigService()
: is_manual_(false) {
status_ = ProxyConfigService::CONFIG_PENDING;
config_.reset(new net::ProxyConfig);
*config_ = net::ProxyConfig::CreateDirect();
}
ProxyConfigService::~ProxyConfigService() {
if (system_proxy_service_ != NULL) {
system_proxy_service_->RemoveObserver(this);
}
}
void ProxyConfigService::ActivateSystemProxyService(
net::ProxyConfigService *system_proxy_service) {
DCHECK(thread_checker_.CalledOnValidThread());
if (system_proxy_service_ != NULL) {
system_proxy_service_->RemoveObserver(this);
}
system_proxy_service_.reset(system_proxy_service);
if (system_proxy_service_ != NULL) {
// Start observing.
system_proxy_service_->AddObserver(this);
} else {
LOG(WARNING) << "Running without system proxy support.";
}
if (!is_manual_) {
net::ProxyConfig config;
net::ProxyConfigService::ConfigAvailability status;
if (system_proxy_service_ != NULL) {
// Get a config that happened before we started observing.
status = system_proxy_service_->GetLatestProxyConfig(&config);
} else {
config = net::ProxyConfig::CreateDirect();
status = ProxyConfigService::CONFIG_VALID;
}
OnProxyConfigChanged(config, status);
}
}
void ProxyConfigService::OnProxyConfigChanged(
const net::ProxyConfig& config,
net::ProxyConfigService::ConfigAvailability availability) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!is_manual_) {
status_ = availability;
config_.reset(new net::ProxyConfig());
*config_ = config;
if (status_ != ProxyConfigService::CONFIG_PENDING) {
// Let the observers know that something changed.
FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
OnProxyConfigChanged(*config_, status_));
}
}
}
void ProxyConfigService::AddObserver(
net::ProxyConfigService::Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
observers_.AddObserver(observer);
}
void ProxyConfigService::RemoveObserver(
net::ProxyConfigService::Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
observers_.RemoveObserver(observer);
}
void ProxyConfigService::SetProxyConfig(const std::string& rules) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!is_manual_ && rules.empty()) {
// Nothing changes.
return;
}
config_.reset(new net::ProxyConfig());
if (!rules.empty()) {
// Use a manually-specified config.
is_manual_ = true;
config_->proxy_rules().ParseFromString(rules);
status_ = ProxyConfigService::CONFIG_VALID;
} else if (system_proxy_service_ != NULL) {
is_manual_ = false;
// Use the system's configuration.
status_ = system_proxy_service_->GetLatestProxyConfig(config_.get());
} else {
// Disable the proxy. Note: we might get the system configuration
// in the future.
is_manual_ = false;
*config_ = net::ProxyConfig::CreateDirect();
status_ = ProxyConfigService::CONFIG_VALID;
}
// Let the observers know that something changed.
if (status_ != ProxyConfigService::CONFIG_PENDING) {
FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
OnProxyConfigChanged(*config_, status_));
}
}
net::ProxyConfigService::ConfigAvailability
ProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) {
DCHECK(thread_checker_.CalledOnValidThread());
if (is_manual_ || (system_proxy_service_ == NULL)) {
*config = *config_;
return status_;
} else {
return system_proxy_service_->GetLatestProxyConfig(config);
}
}
} // namespace cnet