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
3 // found in the LICENSE file.
5 #ifndef CHROMEOS_DBUS_SERVICES_PROXY_RESOLUTION_SERVICE_PROVIDER_H_
6 #define CHROMEOS_DBUS_SERVICES_PROXY_RESOLUTION_SERVICE_PROVIDER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/services/cros_dbus_service.h"
16 #include "dbus/exported_object.h"
19 class SingleThreadTaskRunner
;
27 class URLRequestContextGetter
;
32 class ProxyResolverDelegate
;
33 class ProxyResolverInterface
;
35 // This class provides proxy resolution service for CrosDBusService.
36 // It processes proxy resolution requests for ChromeOS clients.
38 // The following methods are exported.
40 // Interface: org.chromium.LibCrosServiceInterface (kLibCrosServiceInterface)
41 // Method: ResolveNetworkProxy (kResolveNetworkProxy)
42 // Parameters: string:source_url
43 // string:signal_interface
46 // Resolves the proxy for |source_url|. Returns the result
47 // as a D-Bus signal sent to |signal_interface| and |signal_name|.
49 // The returned signal will contain the three values:
50 // - string:source_url - requested source URL.
51 // - string:proxy_info - proxy info for the source URL in PAC format
52 // like "PROXY cache.example.com:12345"
53 // - string:error_message - error message. Empty if successful.
55 // This service can be manually tested using dbus-monitor and
56 // dbus-send. For instance, you can resolve proxy configuration for
57 // http://www.gmail.com/ as follows:
59 // 1. Open a terminal and run the following:
61 // % dbus-monitor --system interface=org.chromium.TestInterface
63 // 2. Open another terminal and run the following:
65 // % dbus-send --system --type=method_call
66 // --dest=org.chromium.LibCrosService
67 // /org/chromium/LibCrosService
68 // org.chromium.LibCrosServiceInterface.ResolveNetworkProxy
69 // string:http://www.gmail.com/
70 // string:org.chromium.TestInterface
73 // 3. Go back to the original terminal and check the output which should
76 // signal sender=:1.23 -> dest=(null destination) serial=12345
77 // path=/org/chromium/LibCrosService; interface=org.chromium.TestInterface;
79 // string "http://www.gmail.com/"
80 // string "PROXY proxy.example.com:8080"
84 class CHROMEOS_EXPORT ProxyResolutionServiceProvider
85 : public CrosDBusService::ServiceProviderInterface
{
87 virtual ~ProxyResolutionServiceProvider();
89 // CrosDBusService::ServiceProviderInterface override.
91 scoped_refptr
<dbus::ExportedObject
> exported_object
) override
;
93 // Creates the instance.
94 static ProxyResolutionServiceProvider
* Create(
95 scoped_ptr
<ProxyResolverDelegate
> delgate
);
98 explicit ProxyResolutionServiceProvider(ProxyResolverInterface
*resovler
);
100 // Called from ExportedObject, when ResolveProxyHandler() is exported as
101 // a D-Bus method, or failed to be exported.
102 void OnExported(const std::string
& interface_name
,
103 const std::string
& method_name
,
106 // Callback to be invoked when ChromeOS clients send network proxy
107 // resolution requests to the service running in chrome executable.
108 // Called on UI thread from dbus request.
109 void ResolveProxyHandler(dbus::MethodCall
* method_call
,
110 dbus::ExportedObject::ResponseSender response_sender
);
112 // Calls ResolveProxyHandler() if weak_ptr is not NULL. Used to ensure a
114 static void CallResolveProxyHandler(
115 base::WeakPtr
<ProxyResolutionServiceProvider
> weak_ptr
,
116 dbus::MethodCall
* method_call
,
117 dbus::ExportedObject::ResponseSender response_sender
);
119 // Returns true if the current thread is on the origin thread.
120 bool OnOriginThread();
122 scoped_refptr
<dbus::ExportedObject
> exported_object_
;
123 scoped_ptr
<ProxyResolverInterface
> resolver_
;
124 scoped_refptr
<base::SingleThreadTaskRunner
> origin_thread_
;
125 base::WeakPtrFactory
<ProxyResolutionServiceProvider
> weak_ptr_factory_
;
127 DISALLOW_COPY_AND_ASSIGN(ProxyResolutionServiceProvider
);
130 // The delegate which provides necessary objects to the proxy resolver.
131 class CHROMEOS_EXPORT ProxyResolverDelegate
{
133 virtual ~ProxyResolverDelegate() {}
135 // Returns the request context used to perform proxy resolution.
136 // Always called on UI thread.
137 virtual scoped_refptr
<net::URLRequestContextGetter
> GetRequestContext() = 0;
140 // The interface is defined so we can mock out the proxy resolver
142 class CHROMEOS_EXPORT ProxyResolverInterface
{
144 // Resolves the proxy for the given URL. Returns the result as a
145 // signal sent to |signal_interface| and
146 // |signal_name|. |exported_object| will be used to send the
147 // signal. The signal contains the three string members:
149 // - source url: the requested source URL.
150 // - proxy info: proxy info for the source URL in PAC format.
151 // - error message: empty if the proxy resolution was successful.
152 virtual void ResolveProxy(
153 const std::string
& source_url
,
154 const std::string
& signal_interface
,
155 const std::string
& signal_name
,
156 scoped_refptr
<dbus::ExportedObject
> exported_object
) = 0;
158 virtual ~ProxyResolverInterface();
161 } // namespace chromeos
163 #endif // CHROMEOS_DBUS_SERVICES_PROXY_RESOLUTION_SERVICE_PROVIDER_H_