| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/services/proxy_resolution_service_provider.h" | 5 #include "chromeos/dbus/services/proxy_resolution_service_provider.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "dbus/bus.h" | 14 #include "dbus/bus.h" |
| 15 #include "dbus/message.h" | 15 #include "dbus/message.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/log/net_log_with_source.h" | 17 #include "net/log/net_log_with_source.h" |
| 18 #include "net/proxy/proxy_info.h" | 18 #include "net/proxy/proxy_info.h" |
| 19 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
| 20 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 24 | 24 |
| 25 namespace chromeos { | 25 namespace chromeos { |
| 26 | 26 |
| 27 struct ProxyResolutionServiceProvider::Request { | 27 struct ProxyResolutionServiceProvider::Request { |
| 28 public: | 28 public: |
| 29 // Constructor for returning proxy info via an asynchronous D-Bus response. | |
| 30 Request(const std::string& source_url, | 29 Request(const std::string& source_url, |
| 31 std::unique_ptr<dbus::Response> response, | 30 std::unique_ptr<dbus::Response> response, |
| 32 const dbus::ExportedObject::ResponseSender& response_sender, | 31 const dbus::ExportedObject::ResponseSender& response_sender, |
| 33 scoped_refptr<net::URLRequestContextGetter> context_getter) | 32 scoped_refptr<net::URLRequestContextGetter> context_getter) |
| 34 : source_url(source_url), | 33 : source_url(source_url), |
| 35 response(std::move(response)), | 34 response(std::move(response)), |
| 36 response_sender(response_sender), | 35 response_sender(response_sender), |
| 37 context_getter(context_getter) { | 36 context_getter(context_getter) { |
| 38 DCHECK(this->response); | 37 DCHECK(this->response); |
| 39 DCHECK(!response_sender.is_null()); | 38 DCHECK(!response_sender.is_null()); |
| 40 } | 39 } |
| 41 | |
| 42 // Constructor for returning proxy info via a D-Bus signal. | |
| 43 Request(const std::string& source_url, | |
| 44 const std::string& signal_interface, | |
| 45 const std::string& signal_name, | |
| 46 scoped_refptr<net::URLRequestContextGetter> context_getter) | |
| 47 : source_url(source_url), | |
| 48 signal_interface(signal_interface), | |
| 49 signal_name(signal_name), | |
| 50 context_getter(context_getter) { | |
| 51 DCHECK(!signal_interface.empty()); | |
| 52 DCHECK(!signal_name.empty()); | |
| 53 } | |
| 54 | |
| 55 ~Request() = default; | 40 ~Request() = default; |
| 56 | 41 |
| 57 // URL being resolved. | 42 // URL being resolved. |
| 58 const std::string source_url; | 43 const std::string source_url; |
| 59 | 44 |
| 60 // D-Bus response and callback for returning data on resolution completion. | 45 // D-Bus response and callback for returning data on resolution completion. |
| 61 // Either these two members or |signal_interface|/|signal_name| must be | |
| 62 // supplied, but not both. | |
| 63 std::unique_ptr<dbus::Response> response; | 46 std::unique_ptr<dbus::Response> response; |
| 64 const dbus::ExportedObject::ResponseSender response_sender; | 47 const dbus::ExportedObject::ResponseSender response_sender; |
| 65 | 48 |
| 66 // D-Bus interface and name for emitting result signal after resolution is | |
| 67 // complete. | |
| 68 // TODO(derat): Remove these and associated code after all callers use async | |
| 69 // responses instead of signals: http://crbug.com/446115 | |
| 70 const std::string signal_interface; | |
| 71 const std::string signal_name; | |
| 72 | |
| 73 // Used to get the network context associated with the profile used to run | 49 // Used to get the network context associated with the profile used to run |
| 74 // this request. | 50 // this request. |
| 75 const scoped_refptr<net::URLRequestContextGetter> context_getter; | 51 const scoped_refptr<net::URLRequestContextGetter> context_getter; |
| 76 | 52 |
| 77 // ProxyInfo resolved for |source_url|. | 53 // ProxyInfo resolved for |source_url|. |
| 78 net::ProxyInfo proxy_info; | 54 net::ProxyInfo proxy_info; |
| 79 | 55 |
| 80 // Error from proxy resolution. | 56 // Error from proxy resolution. |
| 81 std::string error; | 57 std::string error; |
| 82 | 58 |
| 83 private: | 59 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(Request); | 60 DISALLOW_COPY_AND_ASSIGN(Request); |
| 85 }; | 61 }; |
| 86 | 62 |
| 87 ProxyResolutionServiceProvider::ProxyResolutionServiceProvider( | 63 ProxyResolutionServiceProvider::ProxyResolutionServiceProvider( |
| 88 const std::string& dbus_interface, | |
| 89 const std::string& dbus_method_name, | |
| 90 std::unique_ptr<Delegate> delegate) | 64 std::unique_ptr<Delegate> delegate) |
| 91 : dbus_interface_(dbus_interface), | 65 : delegate_(std::move(delegate)), |
| 92 dbus_method_name_(dbus_method_name), | |
| 93 delegate_(std::move(delegate)), | |
| 94 origin_thread_(base::ThreadTaskRunnerHandle::Get()), | 66 origin_thread_(base::ThreadTaskRunnerHandle::Get()), |
| 95 weak_ptr_factory_(this) {} | 67 weak_ptr_factory_(this) {} |
| 96 | 68 |
| 97 ProxyResolutionServiceProvider::~ProxyResolutionServiceProvider() { | 69 ProxyResolutionServiceProvider::~ProxyResolutionServiceProvider() { |
| 98 DCHECK(OnOriginThread()); | 70 DCHECK(OnOriginThread()); |
| 99 } | 71 } |
| 100 | 72 |
| 101 void ProxyResolutionServiceProvider::Start( | 73 void ProxyResolutionServiceProvider::Start( |
| 102 scoped_refptr<dbus::ExportedObject> exported_object) { | 74 scoped_refptr<dbus::ExportedObject> exported_object) { |
| 103 DCHECK(OnOriginThread()); | 75 DCHECK(OnOriginThread()); |
| 104 exported_object_ = exported_object; | 76 exported_object_ = exported_object; |
| 105 VLOG(1) << "ProxyResolutionServiceProvider started"; | 77 VLOG(1) << "ProxyResolutionServiceProvider started"; |
| 106 exported_object_->ExportMethod( | 78 exported_object_->ExportMethod( |
| 107 dbus_interface_, dbus_method_name_, | 79 kNetworkProxyServiceInterface, kNetworkProxyServiceResolveProxyMethod, |
| 108 base::Bind(&ProxyResolutionServiceProvider::ResolveProxy, | 80 base::Bind(&ProxyResolutionServiceProvider::ResolveProxy, |
| 109 weak_ptr_factory_.GetWeakPtr()), | 81 weak_ptr_factory_.GetWeakPtr()), |
| 110 base::Bind(&ProxyResolutionServiceProvider::OnExported, | 82 base::Bind(&ProxyResolutionServiceProvider::OnExported, |
| 111 weak_ptr_factory_.GetWeakPtr())); | 83 weak_ptr_factory_.GetWeakPtr())); |
| 112 } | 84 } |
| 113 | 85 |
| 114 bool ProxyResolutionServiceProvider::OnOriginThread() { | 86 bool ProxyResolutionServiceProvider::OnOriginThread() { |
| 115 return origin_thread_->BelongsToCurrentThread(); | 87 return origin_thread_->BelongsToCurrentThread(); |
| 116 } | 88 } |
| 117 | 89 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 133 VLOG(1) << "Handling method call: " << method_call->ToString(); | 105 VLOG(1) << "Handling method call: " << method_call->ToString(); |
| 134 dbus::MessageReader reader(method_call); | 106 dbus::MessageReader reader(method_call); |
| 135 std::string source_url; | 107 std::string source_url; |
| 136 if (!reader.PopString(&source_url)) { | 108 if (!reader.PopString(&source_url)) { |
| 137 LOG(ERROR) << "Method call lacks source URL: " << method_call->ToString(); | 109 LOG(ERROR) << "Method call lacks source URL: " << method_call->ToString(); |
| 138 response_sender.Run(dbus::ErrorResponse::FromMethodCall( | 110 response_sender.Run(dbus::ErrorResponse::FromMethodCall( |
| 139 method_call, DBUS_ERROR_INVALID_ARGS, "No source URL string arg")); | 111 method_call, DBUS_ERROR_INVALID_ARGS, "No source URL string arg")); |
| 140 return; | 112 return; |
| 141 } | 113 } |
| 142 | 114 |
| 143 // The signal interface and name arguments are optional but must be supplied | |
| 144 // together. | |
| 145 std::string signal_interface, signal_name; | |
| 146 if (reader.HasMoreData() && | |
| 147 (!reader.PopString(&signal_interface) || signal_interface.empty() || | |
| 148 !reader.PopString(&signal_name) || signal_name.empty())) { | |
| 149 LOG(ERROR) << "Method call has invalid interface/name args: " | |
| 150 << method_call->ToString(); | |
| 151 response_sender.Run(dbus::ErrorResponse::FromMethodCall( | |
| 152 method_call, DBUS_ERROR_INVALID_ARGS, "Invalid interface/name args")); | |
| 153 return; | |
| 154 } | |
| 155 | |
| 156 std::unique_ptr<dbus::Response> response = | 115 std::unique_ptr<dbus::Response> response = |
| 157 dbus::Response::FromMethodCall(method_call); | 116 dbus::Response::FromMethodCall(method_call); |
| 158 scoped_refptr<net::URLRequestContextGetter> context_getter = | 117 scoped_refptr<net::URLRequestContextGetter> context_getter = |
| 159 delegate_->GetRequestContext(); | 118 delegate_->GetRequestContext(); |
| 160 | 119 |
| 161 // If signal information was supplied, emit a signal instead of including | 120 std::unique_ptr<Request> request = base::MakeUnique<Request>( |
| 162 // proxy information in the response. | 121 source_url, std::move(response), response_sender, context_getter); |
| 163 std::unique_ptr<Request> request = | |
| 164 !signal_interface.empty() | |
| 165 ? base::MakeUnique<Request>(source_url, signal_interface, signal_name, | |
| 166 context_getter) | |
| 167 : base::MakeUnique<Request>(source_url, std::move(response), | |
| 168 response_sender, context_getter); | |
| 169 | |
| 170 NotifyCallback notify_callback = | 122 NotifyCallback notify_callback = |
| 171 base::Bind(&ProxyResolutionServiceProvider::NotifyProxyResolved, | 123 base::Bind(&ProxyResolutionServiceProvider::NotifyProxyResolved, |
| 172 weak_ptr_factory_.GetWeakPtr()); | 124 weak_ptr_factory_.GetWeakPtr()); |
| 173 | 125 |
| 174 // This would ideally call PostTaskAndReply() instead of PostTask(), but | 126 // This would ideally call PostTaskAndReply() instead of PostTask(), but |
| 175 // ResolveProxyOnNetworkThread()'s call to net::ProxyService::ResolveProxy() | 127 // ResolveProxyOnNetworkThread()'s call to net::ProxyService::ResolveProxy() |
| 176 // can result in an asynchronous lookup, in which case the result won't be | 128 // can result in an asynchronous lookup, in which case the result won't be |
| 177 // available immediately. | 129 // available immediately. |
| 178 context_getter->GetNetworkTaskRunner()->PostTask( | 130 context_getter->GetNetworkTaskRunner()->PostTask( |
| 179 FROM_HERE, | 131 FROM_HERE, |
| 180 base::Bind(&ProxyResolutionServiceProvider::ResolveProxyOnNetworkThread, | 132 base::Bind(&ProxyResolutionServiceProvider::ResolveProxyOnNetworkThread, |
| 181 base::Passed(std::move(request)), origin_thread_, | 133 base::Passed(std::move(request)), origin_thread_, |
| 182 notify_callback)); | 134 notify_callback)); |
| 183 | |
| 184 // If we didn't already pass the response to the Request object because we're | |
| 185 // returning data via a signal, send an empty response immediately. | |
| 186 if (response) | |
| 187 response_sender.Run(std::move(response)); | |
| 188 } | 135 } |
| 189 | 136 |
| 190 // static | 137 // static |
| 191 void ProxyResolutionServiceProvider::ResolveProxyOnNetworkThread( | 138 void ProxyResolutionServiceProvider::ResolveProxyOnNetworkThread( |
| 192 std::unique_ptr<Request> request, | 139 std::unique_ptr<Request> request, |
| 193 scoped_refptr<base::SingleThreadTaskRunner> notify_thread, | 140 scoped_refptr<base::SingleThreadTaskRunner> notify_thread, |
| 194 NotifyCallback notify_callback) { | 141 NotifyCallback notify_callback) { |
| 195 DCHECK(request->context_getter->GetNetworkTaskRunner() | 142 DCHECK(request->context_getter->GetNetworkTaskRunner() |
| 196 ->BelongsToCurrentThread()); | 143 ->BelongsToCurrentThread()); |
| 197 | 144 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 request->error = net::ErrorToString(result); | 180 request->error = net::ErrorToString(result); |
| 234 | 181 |
| 235 notify_thread->PostTask( | 182 notify_thread->PostTask( |
| 236 FROM_HERE, base::Bind(notify_callback, base::Passed(std::move(request)))); | 183 FROM_HERE, base::Bind(notify_callback, base::Passed(std::move(request)))); |
| 237 } | 184 } |
| 238 | 185 |
| 239 void ProxyResolutionServiceProvider::NotifyProxyResolved( | 186 void ProxyResolutionServiceProvider::NotifyProxyResolved( |
| 240 std::unique_ptr<Request> request) { | 187 std::unique_ptr<Request> request) { |
| 241 DCHECK(OnOriginThread()); | 188 DCHECK(OnOriginThread()); |
| 242 | 189 |
| 243 if (request->response) { | 190 // Reply to the original D-Bus method call. |
| 244 // Reply to the original D-Bus method call. | 191 dbus::MessageWriter writer(request->response.get()); |
| 245 dbus::MessageWriter writer(request->response.get()); | 192 writer.AppendString(request->proxy_info.ToPacString()); |
| 246 writer.AppendString(request->proxy_info.ToPacString()); | 193 writer.AppendString(request->error); |
| 247 writer.AppendString(request->error); | 194 request->response_sender.Run(std::move(request->response)); |
| 248 request->response_sender.Run(std::move(request->response)); | |
| 249 } else { | |
| 250 // Send a signal to the client. | |
| 251 dbus::Signal signal(request->signal_interface, request->signal_name); | |
| 252 dbus::MessageWriter writer(&signal); | |
| 253 writer.AppendString(request->source_url); | |
| 254 writer.AppendString(request->proxy_info.ToPacString()); | |
| 255 writer.AppendString(request->error); | |
| 256 exported_object_->SendSignal(&signal); | |
| 257 VLOG(1) << "Sending signal: " << signal.ToString(); | |
| 258 } | |
| 259 } | 195 } |
| 260 | 196 |
| 261 } // namespace chromeos | 197 } // namespace chromeos |
| OLD | NEW |