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 CHROME_BROWSER_CHROMEOS_DBUS_PROXY_RESOLUTION_SERVICE_PROVIDER_H_
6 #define CHROME_BROWSER_CHROMEOS_DBUS_PROXY_RESOLUTION_SERVICE_PROVIDER_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/platform_thread.h"
16 #include "chrome/browser/chromeos/dbus/cros_dbus_service.h"
17 #include "dbus/exported_object.h"
26 class ProxyResolverInterface;
28 // This class provides proxy resolution service for CrosDBusService.
29 // It processes proxy resolution requests for ChromeOS clients.
31 // The following methods are exported.
33 // Interface: org.chromium.LibCrosServiceInterface (kLibCrosServiceInterface)
34 // Method: ResolveNetworkProxy (kResolveNetworkProxy)
35 // Parameters: string:source_url
36 // string:signal_interface
39 // Resolves the proxy for |source_url|. Returns the result
40 // as a D-Bus signal sent to |signal_interface| and |signal_name|.
42 // The returned signal will contain the three values:
43 // - string:source_url - requested source URL.
44 // - string:proxy_info - proxy info for the source URL in PAC format
45 // like "PROXY cache.example.com:12345"
46 // - string:error_message - error message. Empty if successful.
48 // This service can be manually tested using dbus-monitor and
49 // dbus-send. For instance, you can resolve proxy configuration for
50 // http://www.gmail.com/ as follows:
52 // 1. Open a terminal and run the following:
54 // % dbus-monitor --system interface=org.chromium.TestInterface
56 // 2. Open another terminal and run the following:
58 // % dbus-send --system --type=method_call
59 // --dest=org.chromium.LibCrosService
60 // /org/chromium/LibCrosService
61 // org.chromium.LibCrosServiceInterface.ResolveNetworkProxy
62 // string:http://www.gmail.com/
63 // string:org.chromium.TestInterface
66 // 3. Go back to the original terminal and check the output which should
69 // signal sender=:1.23 -> dest=(null destination) serial=12345
70 // path=/org/chromium/LibCrosService; interface=org.chromium.TestInterface;
72 // string "http://www.gmail.com/"
73 // string "PROXY proxy.example.com:8080"
77 class ProxyResolutionServiceProvider
78 : public CrosDBusService::ServiceProviderInterface {
80 virtual ~ProxyResolutionServiceProvider();
82 // CrosDBusService::ServiceProviderInterface override.
84 scoped_refptr<dbus::ExportedObject> exported_object) override;
86 // Creates the instance.
87 static ProxyResolutionServiceProvider* Create();
90 explicit ProxyResolutionServiceProvider(ProxyResolverInterface *resovler);
92 // Creates the instance for testing. Takes the ownership of |resovler|
93 friend class ProxyResolutionServiceProviderTest;
94 static ProxyResolutionServiceProvider* CreateForTesting(
95 ProxyResolverInterface* resolver);
97 // Called from ExportedObject, when ResolveProxyHandler() is exported as
98 // a D-Bus method, or failed to be exported.
99 void OnExported(const std::string& interface_name,
100 const std::string& method_name,
103 // Callback to be invoked when ChromeOS clients send network proxy
104 // resolution requests to the service running in chrome executable.
105 // Called on UI thread from dbus request.
106 void ResolveProxyHandler(dbus::MethodCall* method_call,
107 dbus::ExportedObject::ResponseSender response_sender);
109 // Calls ResolveProxyHandler() if weak_ptr is not NULL. Used to ensure a
111 static void CallResolveProxyHandler(
112 base::WeakPtr<ProxyResolutionServiceProvider> weak_ptr,
113 dbus::MethodCall* method_call,
114 dbus::ExportedObject::ResponseSender response_sender);
116 // Returns true if the current thread is on the origin thread.
117 bool OnOriginThread();
119 scoped_refptr<dbus::ExportedObject> exported_object_;
120 scoped_ptr<ProxyResolverInterface> resolver_;
121 base::PlatformThreadId origin_thread_id_;
122 base::WeakPtrFactory<ProxyResolutionServiceProvider> weak_ptr_factory_;
124 DISALLOW_COPY_AND_ASSIGN(ProxyResolutionServiceProvider);
127 // The interface is defined so we can mock out the proxy resolver
129 class ProxyResolverInterface {
131 // Resolves the proxy for the given URL. Returns the result as a
132 // signal sent to |signal_interface| and
133 // |signal_name|. |exported_object| will be used to send the
134 // signal. The signal contains the three string members:
136 // - source url: the requested source URL.
137 // - proxy info: proxy info for the source URL in PAC format.
138 // - error message: empty if the proxy resolution was successful.
139 virtual void ResolveProxy(
140 const std::string& source_url,
141 const std::string& signal_interface,
142 const std::string& signal_name,
143 scoped_refptr<dbus::ExportedObject> exported_object) = 0;
145 virtual ~ProxyResolverInterface();
148 } // namespace chromeos
150 #endif // CHROME_BROWSER_CHROMEOS_DBUS_PROXY_RESOLUTION_SERVICE_PROVIDER_H_