1 //
2 // Copyright (C) 2011 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_CHROME_BROWSER_PROXY_RESOLVER_H_
18 #define UPDATE_ENGINE_CHROME_BROWSER_PROXY_RESOLVER_H_
19 
20 #include <deque>
21 #include <string>
22 
23 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
24 
25 #include <brillo/message_loops/message_loop.h>
26 
27 #include "update_engine/libcros_proxy.h"
28 #include "update_engine/proxy_resolver.h"
29 
30 namespace chromeos_update_engine {
31 
32 extern const char kLibCrosServiceName[];
33 extern const char kLibCrosProxyResolveName[];
34 extern const char kLibCrosProxyResolveSignalInterface[];
35 
36 class ChromeBrowserProxyResolver : public ProxyResolver {
37  public:
38   explicit ChromeBrowserProxyResolver(LibCrosProxy* libcros_proxy);
39   ~ChromeBrowserProxyResolver() override;
40 
41   // Initialize the ProxyResolver using the provided DBus proxies.
42   bool Init();
43 
44   ProxyRequestId GetProxiesForUrl(const std::string& url,
45                                   const ProxiesResolvedFn& callback) override;
46   bool CancelProxyRequest(ProxyRequestId request) override;
47 
48  private:
49   FRIEND_TEST(ChromeBrowserProxyResolverTest, ParseTest);
50   FRIEND_TEST(ChromeBrowserProxyResolverTest, SuccessTest);
51   struct ProxyRequestData {
52     brillo::MessageLoop::TaskId timeout_id;
53     ProxiesResolvedFn callback;
54   };
55   typedef std::multimap<std::string, std::unique_ptr<ProxyRequestData>>
56       CallbacksMap;
57 
58   // Called when the signal in UpdateEngineLibcrosProxyResolvedInterface is
59   // connected.
60   void OnSignalConnected(const std::string& interface_name,
61                          const std::string& signal_name,
62                          bool successful);
63 
64   // Handle a reply from Chrome:
65   void OnProxyResolvedSignal(const std::string& source_url,
66                              const std::string& proxy_info,
67                              const std::string& error_message);
68 
69   // Handle no reply. The |request| pointer points to the ProxyRequestData in
70   // the |callbacks_| map that triggered this timeout.
71   void HandleTimeout(std::string source_url, ProxyRequestData* request);
72 
73   // Parses a string-encoded list of proxies and returns a deque
74   // of individual proxies. The last one will always be kNoProxy.
75   static std::deque<std::string> ParseProxyString(const std::string& input);
76 
77   // Process a proxy response by calling all the callbacks associated with the
78   // passed |source_url|. All the timeouts associated with these callbacks will
79   // be removed.
80   void ProcessUrlResponse(const std::string& source_url,
81                           const std::deque<std::string>& proxies);
82 
83   // Shutdown the dbus proxy object.
84   void Shutdown();
85 
86   // DBus proxies to request a HTTP proxy resolution. The request is done in the
87   // service_interface_proxy() interface and the response is received as a
88   // signal in the ue_proxy_resolved_interface().
89   LibCrosProxy* libcros_proxy_;
90 
91   int timeout_;
92   CallbacksMap callbacks_;
93   DISALLOW_COPY_AND_ASSIGN(ChromeBrowserProxyResolver);
94 };
95 
96 }  // namespace chromeos_update_engine
97 
98 #endif  // UPDATE_ENGINE_CHROME_BROWSER_PROXY_RESOLVER_H_
99