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 <map>
22 #include <string>
23 #include <utility>
24 
25 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
26 
27 #include <brillo/message_loops/message_loop.h>
28 
29 #include "update_engine/libcros_proxy.h"
30 #include "update_engine/proxy_resolver.h"
31 
32 namespace chromeos_update_engine {
33 
34 extern const char kLibCrosServiceName[];
35 extern const char kLibCrosProxyResolveName[];
36 extern const char kLibCrosProxyResolveSignalInterface[];
37 
38 class ChromeBrowserProxyResolver : public ProxyResolver {
39  public:
40   explicit ChromeBrowserProxyResolver(LibCrosProxy* libcros_proxy);
41   ~ChromeBrowserProxyResolver() override;
42 
43   // Initialize the ProxyResolver using the provided DBus proxies.
44   bool Init();
45 
46   bool GetProxiesForUrl(const std::string& url,
47                         ProxiesResolvedFn callback,
48                         void* data) override;
49 
50  private:
51   FRIEND_TEST(ChromeBrowserProxyResolverTest, ParseTest);
52   FRIEND_TEST(ChromeBrowserProxyResolverTest, SuccessTest);
53   typedef std::multimap<std::string, std::pair<ProxiesResolvedFn, void*>>
54       CallbacksMap;
55   typedef std::multimap<std::string, brillo::MessageLoop::TaskId> TimeoutsMap;
56 
57   // Called when the signal in UpdateEngineLibcrosProxyResolvedInterface is
58   // connected.
59   void OnSignalConnected(const std::string& interface_name,
60                          const std::string& signal_name,
61                          bool successful);
62 
63   // Handle a reply from Chrome:
64   void OnProxyResolvedSignal(const std::string& source_url,
65                              const std::string& proxy_info,
66                              const std::string& error_message);
67 
68   // Handle no reply:
69   void HandleTimeout(std::string source_url);
70 
71   // Parses a string-encoded list of proxies and returns a deque
72   // of individual proxies. The last one will always be kNoProxy.
73   static std::deque<std::string> ParseProxyString(const std::string& input);
74 
75   // Deletes internal state for the first instance of url in the state.
76   // If delete_timer is set, calls CancelTask on the timer id.
77   // Returns the callback in an out parameter. Returns true on success.
78   bool DeleteUrlState(const std::string& url,
79                       bool delete_timer,
80                       std::pair<ProxiesResolvedFn, void*>* callback);
81 
82   // Shutdown the dbus proxy object.
83   void Shutdown();
84 
85   // DBus proxies to request a HTTP proxy resolution. The request is done in the
86   // service_interface_proxy() interface and the response is received as a
87   // signal in the ue_proxy_resolved_interface().
88   LibCrosProxy* libcros_proxy_;
89 
90   int timeout_;
91   TimeoutsMap timers_;
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