[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / chromeos / dbus / services / proxy_resolution_service_provider.h
blobacc25c27e76a6b2b6caeab0723fe7f300919ffb5
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_
8 #include <string>
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"
18 namespace base {
19 class SingleThreadTaskRunner;
22 namespace dbus {
23 class MethodCall;
26 namespace net {
27 class URLRequestContextGetter;
30 namespace chromeos {
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
44 // string:signal_name
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
71 // string:TestSignal
73 // 3. Go back to the original terminal and check the output which should
74 // look like:
76 // signal sender=:1.23 -> dest=(null destination) serial=12345
77 // path=/org/chromium/LibCrosService; interface=org.chromium.TestInterface;
78 // member=TestSignal
79 // string "http://www.gmail.com/"
80 // string "PROXY proxy.example.com:8080"
81 // string ""
84 class CHROMEOS_EXPORT ProxyResolutionServiceProvider
85 : public CrosDBusService::ServiceProviderInterface {
86 public:
87 ~ProxyResolutionServiceProvider() override;
89 // CrosDBusService::ServiceProviderInterface override.
90 void Start(scoped_refptr<dbus::ExportedObject> exported_object) override;
92 // Creates the instance.
93 static ProxyResolutionServiceProvider* Create(
94 scoped_ptr<ProxyResolverDelegate> delgate);
96 private:
97 explicit ProxyResolutionServiceProvider(ProxyResolverInterface *resovler);
99 // Called from ExportedObject, when ResolveProxyHandler() is exported as
100 // a D-Bus method, or failed to be exported.
101 void OnExported(const std::string& interface_name,
102 const std::string& method_name,
103 bool success);
105 // Callback to be invoked when ChromeOS clients send network proxy
106 // resolution requests to the service running in chrome executable.
107 // Called on UI thread from dbus request.
108 void ResolveProxyHandler(dbus::MethodCall* method_call,
109 dbus::ExportedObject::ResponseSender response_sender);
111 // Calls ResolveProxyHandler() if weak_ptr is not NULL. Used to ensure a
112 // safe shutdown.
113 static void CallResolveProxyHandler(
114 base::WeakPtr<ProxyResolutionServiceProvider> weak_ptr,
115 dbus::MethodCall* method_call,
116 dbus::ExportedObject::ResponseSender response_sender);
118 // Returns true if the current thread is on the origin thread.
119 bool OnOriginThread();
121 scoped_refptr<dbus::ExportedObject> exported_object_;
122 scoped_ptr<ProxyResolverInterface> resolver_;
123 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_;
124 base::WeakPtrFactory<ProxyResolutionServiceProvider> weak_ptr_factory_;
126 DISALLOW_COPY_AND_ASSIGN(ProxyResolutionServiceProvider);
129 // The delegate which provides necessary objects to the proxy resolver.
130 class CHROMEOS_EXPORT ProxyResolverDelegate {
131 public:
132 virtual ~ProxyResolverDelegate() {}
134 // Returns the request context used to perform proxy resolution.
135 // Always called on UI thread.
136 virtual scoped_refptr<net::URLRequestContextGetter> GetRequestContext() = 0;
139 // The interface is defined so we can mock out the proxy resolver
140 // implementation.
141 class CHROMEOS_EXPORT ProxyResolverInterface {
142 public:
143 // Resolves the proxy for the given URL. Returns the result as a
144 // signal sent to |signal_interface| and
145 // |signal_name|. |exported_object| will be used to send the
146 // signal. The signal contains the three string members:
148 // - source url: the requested source URL.
149 // - proxy info: proxy info for the source URL in PAC format.
150 // - error message: empty if the proxy resolution was successful.
151 virtual void ResolveProxy(
152 const std::string& source_url,
153 const std::string& signal_interface,
154 const std::string& signal_name,
155 scoped_refptr<dbus::ExportedObject> exported_object) = 0;
157 virtual ~ProxyResolverInterface();
160 } // namespace chromeos
162 #endif // CHROMEOS_DBUS_SERVICES_PROXY_RESOLUTION_SERVICE_PROVIDER_H_