blob: cb17a459161268141df6569e1d0a9d553af65f7d [file] [log] [blame]
// Copyright (c) 2012 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 <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/dbus/services/cros_dbus_service.h"
#include "dbus/exported_object.h"
#include "net/base/network_isolation_key.h"
namespace base {
class SingleThreadTaskRunner;
namespace dbus {
class MethodCall;
namespace network {
namespace mojom {
class NetworkContext;
} // namespace network
namespace chromeos {
// This class processes proxy resolution requests for Chrome OS clients.
// The following method is exported:
// Interface: org.chromium.NetworkProxyServiceInterface
// (kNetworkProxyServiceInterface)
// Method: ResolveProxy (kNetworkProxyServiceResolveProxyMethod)
// Parameters: string:source_url
// Resolves the proxy for |source_url| and returns proxy information via an
// asynchronous response containing two values:
// - string:proxy_info - proxy info for the source URL in PAC format
// like "PROXY"
// - string:error_message - error message. Empty if successful.
// This service can be manually tested using dbus-send:
// % dbus-send --system --type=method_call --print-reply
// --dest=org.chromium.NetworkProxyService
// /org/chromium/NetworkProxyService
// org.chromium.NetworkProxyServiceInterface.ResolveProxy
// string:
class ProxyResolutionServiceProvider
: public CrosDBusService::ServiceProviderInterface {
// Callback that is invoked with the result of proxy resolution. On success
// |error| is empty, and |pac_string| contains the result. Otherwise |error|
// is non-empty.
using NotifyCallback =
base::OnceCallback<void(const std::string& error,
const std::string& pac_string)>;
~ProxyResolutionServiceProvider() override;
void set_network_context_for_test(
network::mojom::NetworkContext* network_context) {
network_context_for_test_ = network_context;
use_network_context_for_test_ = true;
// CrosDBusService::ServiceProviderInterface:
void Start(scoped_refptr<dbus::ExportedObject> exported_object) override;
friend class ProxyResolutionServiceProviderTestWrapper;
// Returns true if called on |origin_thread_|.
bool OnOriginThread();
// Called when ResolveProxy() is exported as a D-Bus method.
void OnExported(const std::string& interface_name,
const std::string& method_name,
bool success);
// Callback invoked when Chrome OS clients send network proxy resolution
// requests to the service. Called on UI thread.
void DbusResolveProxy(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender);
void ResolveProxyInternal(const std::string& source_url,
NotifyCallback callback);
// Called on UI thread from OnResolutionComplete() to pass the resolved proxy
// information to the client over D-Bus.
void NotifyProxyResolved(std::unique_ptr<dbus::Response> response,
dbus::ExportedObject::ResponseSender response_sender,
const std::string& error,
const std::string& pac_string);
// Returns the NetworkContext to use for resolving the proxy. This may be
// mocked by testing using set_network_context_for_test(), otherwise it picks
// the primary user profile's default.
network::mojom::NetworkContext* GetNetworkContext();
scoped_refptr<dbus::ExportedObject> exported_object_;
scoped_refptr<base::SingleThreadTaskRunner> origin_thread_;
network::mojom::NetworkContext* network_context_for_test_ = nullptr;
bool use_network_context_for_test_ = false;
// A transient NetworkIsolationKey used for all requests. This prevents what
// hostnames have been resolved using any PAC scripts to websites, while
// allowing cached host resolutions between DBus calls. Since only Chrome OS
// system daemons have access to DBus, don't have to worry about information
// leaks between them. In the case no PAC script is in use, the
// NetworkIsolationKey has no effect.
const net::NetworkIsolationKey network_isolation_key_;
base::WeakPtrFactory<ProxyResolutionServiceProvider> weak_ptr_factory_{this};
} // namespace chromeos