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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/cros/libcros_service_library.h"
#include "base/synchronization/lock.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "content/browser/browser_thread.h"
#include "cros/chromeos_libcros_service.h"
#include "net/base/net_errors.h"
#include "net/proxy/proxy_service.h"
namespace chromeos {
class LibCrosServiceLibraryImpl : public LibCrosServiceLibrary {
public:
// Base class for all services of LibCrosService.
// Each subclass should declare DISABLE_RUNNABLE_METHOD_REFCOUNT to disable
// refcounting, which is okay since the subclass's object will exist as a
// scoped_ptr in Singleton LibCrosServiceLibraryImpl, guaranteeing that its
// lifetime is longer than that of any message loop.
class ServicingLibrary {
public:
explicit ServicingLibrary(LibCrosServiceConnection service_connection);
virtual ~ServicingLibrary();
// Clears service_connection_ (which is stored as weak pointer) so that it
// can't be used anymore.
virtual void ClearServiceConnection();
protected:
LibCrosServiceConnection service_connection_; // Weak pointer.
// Lock for data members to synchronize access on multiple threads.
base::Lock data_lock_;
private:
DISALLOW_COPY_AND_ASSIGN(ServicingLibrary);
};
// Library that provides network proxy service for LibCrosService.
// For now, it only processes proxy resolution requests for ChromeOS clients.
class NetworkProxyLibrary : public ServicingLibrary {
public:
explicit NetworkProxyLibrary(LibCrosServiceConnection connection);
virtual ~NetworkProxyLibrary();
// Sets the ProxyService that will handle network proxy requests, e.g.
// proxy resolution for a url.
void SetHandler(net::ProxyService* handler);
private:
// Data being used in one proxy resolution.
class Request {
public:
explicit Request(const std::string& source_url);
virtual ~Request() {}
// Callback on IO thread for when net::ProxyService::ResolveProxy
// completes, synchronously or asynchronously.
void OnCompletion(int result);
net::CompletionCallbackImpl<Request> completion_callback_;
std::string source_url_; // URL being resolved.
int result_; // Result of proxy resolution.
net::ProxyInfo proxy_info_; // ProxyInfo resolved for source_url_.
std::string error_; // Error from proxy resolution.
Task* notify_task_; // Task to notify of resolution result.
private:
DISALLOW_COPY_AND_ASSIGN(Request);
};
// Static callback passed to LibCrosService to be invoked when ChromeOS
// clients send network proxy resolution requests to the service running in
// chrome executable. Called on UI thread from dbus request.
static void ResolveProxyHandler(void* object,
const char* source_url);
void ResolveProxy(const std::string& source_url);
// Wrapper on UI thread to call LibCrosService::NotifyNetworkProxyResolved.
void NotifyProxyResolved(Request* request);
// ProxyService to call ResolveProxy on.
scoped_refptr<net::ProxyService> proxy_service_;
std::vector<Request*> all_requests_;
DISALLOW_COPY_AND_ASSIGN(NetworkProxyLibrary);
};
LibCrosServiceLibraryImpl();
virtual ~LibCrosServiceLibraryImpl();
// LibCrosServiceLibrary implementation.
virtual void RegisterNetworkProxyHandler(net::ProxyService* handler);
private:
// Starts LibCrosService running on dbus if not already started.
bool StartService();
// Connection to LibCrosService.
LibCrosServiceConnection service_connection_;
// Libraries that form LibCrosService.
scoped_ptr<NetworkProxyLibrary> network_proxy_lib_;
DISALLOW_COPY_AND_ASSIGN(LibCrosServiceLibraryImpl);
};
//---------------- LibCrosServiceLibraryImpl: public ---------------------------
LibCrosServiceLibraryImpl::LibCrosServiceLibraryImpl()
: service_connection_(NULL) {
if (!CrosLibrary::Get()->EnsureLoaded()) {
LOG(ERROR) << "Cros library has not been loaded.";
}
}
LibCrosServiceLibraryImpl::~LibCrosServiceLibraryImpl() {
if (service_connection_) {
// Clear service connections in servicing libraries which held the former
// as weak pointers.
if (network_proxy_lib_.get())
network_proxy_lib_->ClearServiceConnection();
StopLibCrosService(service_connection_);
VLOG(1) << "LibCrosService stopped.";
service_connection_ = NULL;
}
}
// Called on UI thread to register network proxy handler for LibCrosService.
void LibCrosServiceLibraryImpl::RegisterNetworkProxyHandler(
net::ProxyService* handler) {
// Make sure we're running on UI thread.
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
scoped_refptr<net::ProxyService> ref_handler(handler);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this,
&LibCrosServiceLibraryImpl::RegisterNetworkProxyHandler,
ref_handler));
return;
}
if (StartService()) {
if (!network_proxy_lib_.get())
network_proxy_lib_.reset(new NetworkProxyLibrary(service_connection_));
network_proxy_lib_->SetHandler(handler);
}
}
//---------------- LibCrosServiceLibraryImpl: private --------------------------
bool LibCrosServiceLibraryImpl::StartService() {
if (service_connection_) // Service has already been started.
return true;
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Starts LibCrosService; the returned connection is used for future
// interactions with the service.
service_connection_ = StartLibCrosService();
if (service_connection_) {
VLOG(1) << "LibCrosService started.";
return true;
}
LOG(WARNING) << "Error starting LibCrosService";
return false;
}
//------------- LibCrosServiceLibraryImpl::ServicingLibrary: public ------------
LibCrosServiceLibraryImpl::ServicingLibrary::ServicingLibrary(
LibCrosServiceConnection connection)
: service_connection_(connection) {
}
LibCrosServiceLibraryImpl::ServicingLibrary::~ServicingLibrary() {
ClearServiceConnection();
}
void LibCrosServiceLibraryImpl::ServicingLibrary::ClearServiceConnection() {
base::AutoLock lock(data_lock_);
service_connection_ = NULL;
}
//----------- LibCrosServiceLibraryImpl::NetworkProxyLibrary: public -----------
LibCrosServiceLibraryImpl::NetworkProxyLibrary::NetworkProxyLibrary(
LibCrosServiceConnection connection)
: ServicingLibrary(connection) {
// Register callback for LibCrosService::ResolveNetworkProxy.
SetNetworkProxyResolver(&ResolveProxyHandler, this, service_connection_);
}
LibCrosServiceLibraryImpl::NetworkProxyLibrary::~NetworkProxyLibrary() {
base::AutoLock lock(data_lock_);
if (!all_requests_.empty()) {
for (size_t i = all_requests_.size() - 1; i >= 0; --i) {
LOG(WARNING) << "Pending request for " << all_requests_[i]->source_url_;
delete all_requests_[i];
}
all_requests_.clear();
}
}
// Called on UI thread to register handler for LibCrosService's network proxy
// requests.
void LibCrosServiceLibraryImpl::NetworkProxyLibrary::SetHandler(
net::ProxyService* handler) {
base::AutoLock lock(data_lock_);
DCHECK(service_connection_);
proxy_service_ = handler;
}
//----------- LibCrosServiceLibraryImpl::NetworkProxyLibrary: private ----------
// Static, called on UI thread from LibCrosService::ResolveProxy via dbus.
void LibCrosServiceLibraryImpl::NetworkProxyLibrary::ResolveProxyHandler(
void* object, const char* source_url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
NetworkProxyLibrary* lib = static_cast<NetworkProxyLibrary*>(object);
// source_url will be freed when this function returns, so make a copy of it.
std::string url(source_url);
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
NewRunnableMethod(lib, &NetworkProxyLibrary::ResolveProxy, url));
}
// Called on IO thread as task posted from ResolveProxyHandler on UI thread.
void LibCrosServiceLibraryImpl::NetworkProxyLibrary::ResolveProxy(
const std::string& source_url) {
// Make sure we're running on IO thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Request* request = new Request(source_url);
request->notify_task_ = NewRunnableMethod(this,
&NetworkProxyLibrary::NotifyProxyResolved, request);
{
base::AutoLock lock(data_lock_);
all_requests_.push_back(request);
if (!service_connection_)
request->error_ = "LibCrosService not started";
else if (!proxy_service_)
request->error_ = "Network proxy resolver not registered";
}
if (request->error_ != "") {
LOG(ERROR) << request->error_;
request->result_ = net::OK; // Set to OK since error string is set.
} else {
VLOG(1) << "Starting networy proxy resolution for " << request->source_url_;
request->result_ = proxy_service_->ResolveProxy(
GURL(request->source_url_), &request->proxy_info_,
&request->completion_callback_, NULL, net::BoundNetLog());
}
if (request->result_ != net::ERR_IO_PENDING) {
VLOG(1) << "Network proxy resolution completed synchronously.";
request->OnCompletion(request->result_);
}
}
// Called on UI thread as task posted from Request::OnCompletion on IO thread.
void LibCrosServiceLibraryImpl::NetworkProxyLibrary::NotifyProxyResolved(
Request* request) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
base::AutoLock lock(data_lock_);
if (service_connection_) {
if (!NotifyNetworkProxyResolved(request->source_url_.c_str(),
request->proxy_info_.ToPacString().c_str(),
request->error_.c_str(),
service_connection_)) {
LOG(ERROR) << "LibCrosService has error with NotifyNetworkProxyResolved";
} else {
VLOG(1) << "LibCrosService has notified proxy resoloution for "
<< request->source_url_;
}
}
std::vector<Request*>::iterator iter =
std::find(all_requests_.begin(), all_requests_.end(), request);
DCHECK(iter != all_requests_.end());
all_requests_.erase(iter);
delete request;
}
//---------- LibCrosServiceLibraryImpl::NetworkProxyLibrary::Request -----------
LibCrosServiceLibraryImpl::NetworkProxyLibrary::Request::Request(
const std::string& source_url)
: ALLOW_THIS_IN_INITIALIZER_LIST(
completion_callback_(this, &Request::OnCompletion)),
source_url_(source_url),
result_(net::ERR_FAILED),
notify_task_(NULL) {
}
// Called on IO thread when net::ProxyService::ResolveProxy has completed.
void LibCrosServiceLibraryImpl::NetworkProxyLibrary::Request::OnCompletion(
int result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
result_ = result;
if (result_ != net::OK)
error_ = net::ErrorToString(result_);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, notify_task_);
notify_task_ = NULL;
}
//--------------------- LibCrosServiceLibraryStubImpl --------------------------
class LibCrosServiceLibraryStubImpl : public LibCrosServiceLibrary {
public:
LibCrosServiceLibraryStubImpl() {}
virtual ~LibCrosServiceLibraryStubImpl() {}
// LibCrosServiceLibrary overrides.
virtual void RegisterNetworkProxyHandler(net::ProxyService* handler) {}
DISALLOW_COPY_AND_ASSIGN(LibCrosServiceLibraryStubImpl);
};
//--------------------------- LibCrosServiceLibrary ----------------------------
// Static.
LibCrosServiceLibrary* LibCrosServiceLibrary::GetImpl(bool stub) {
if (stub)
return new LibCrosServiceLibraryStubImpl();
return new LibCrosServiceLibraryImpl();
}
} // namespace chromeos
// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run, so are all its
// scoped_ptred class members.
DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::LibCrosServiceLibraryImpl);
DISABLE_RUNNABLE_METHOD_REFCOUNT(
chromeos::LibCrosServiceLibraryImpl::NetworkProxyLibrary);
|