• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/id_map.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/strings/string16.h"
17 #include "base/supports_user_data.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/values.h"
20 #include "components/keyed_service/core/keyed_service.h"
21 #include "components/wifi/wifi_service.h"
22 #include "content/public/browser/utility_process_host.h"
23 #include "content/public/browser/utility_process_host_client.h"
24 #include "net/base/network_change_notifier.h"
25 
26 namespace base {
27 class SequencedTaskRunner;
28 }
29 
30 namespace wifi {
31 class WiFiService;
32 }
33 
34 namespace extensions {
35 
36 using wifi::WiFiService;
37 
38 // The client wrapper for the WiFiService and CryptoVerify interfaces to invoke
39 // them on worker thread. Observes |OnNetworkChanged| notifications and posts
40 // them to WiFiService on worker thread to |UpdateConnectedNetwork|. Always used
41 // from UI thread only.
42 class NetworkingPrivateServiceClient
43     : public KeyedService,
44       net::NetworkChangeNotifier::NetworkChangeObserver {
45  public:
46   // Interface for Verify* methods implementation.
47   class CryptoVerify {
48    public:
49     typedef base::Callback<
50         void(const std::string& key_data, const std::string& error)>
51         VerifyAndEncryptCredentialsCallback;
52 
CryptoVerify()53     CryptoVerify() {}
~CryptoVerify()54     virtual ~CryptoVerify() {}
55 
56     static CryptoVerify* Create();
57 
58     virtual void VerifyDestination(scoped_ptr<base::ListValue> args,
59                                    bool* verified,
60                                    std::string* error) = 0;
61 
62     virtual void VerifyAndEncryptCredentials(
63         scoped_ptr<base::ListValue> args,
64         const VerifyAndEncryptCredentialsCallback& callback) = 0;
65 
66     virtual void VerifyAndEncryptData(scoped_ptr<base::ListValue> args,
67                                       std::string* base64_encoded_ciphertext,
68                                       std::string* error) = 0;
69    private:
70     DISALLOW_COPY_AND_ASSIGN(CryptoVerify);
71   };
72 
73   // Interface for observing Network Events.
74   class Observer {
75    public:
Observer()76     Observer() {}
~Observer()77     virtual ~Observer() {}
78 
79     virtual void OnNetworksChangedEvent(
80         const std::vector<std::string>& network_guids) = 0;
81     virtual void OnNetworkListChangedEvent(
82         const std::vector<std::string>& network_guids) = 0;
83 
84    private:
85     DISALLOW_COPY_AND_ASSIGN(Observer);
86   };
87 
88   // An error callback used by most of API functions to receive error results
89   // from the NetworkingPrivateServiceClient.
90   typedef base::Callback<
91       void(const std::string& error_name,
92            scoped_ptr<base::DictionaryValue> error_data)> ErrorCallback;
93 
94   // An error callback used by most of Crypto Verify* API functions to receive
95   // error results from the NetworkingPrivateServiceClient.
96   // TODO(mef): Cleanup networking_private_api.* to make consistent
97   // NetworkingPrivateXXXFunction naming and error callbacks.
98   typedef base::Callback<
99       void(const std::string& error_name,
100            const std::string& error)> CryptoErrorCallback;
101 
102   // Callback used to return bool result from VerifyDestination function.
103   typedef base::Callback<void(bool result)> BoolResultCallback;
104 
105   // Callback used to return string result from VerifyAndEncryptData function.
106   typedef base::Callback<void(const std::string& result)> StringResultCallback;
107 
108   // Callback used to return Dictionary of network properties.
109   typedef base::Callback<
110       void(const std::string& network_guid,
111            const base::DictionaryValue& dictionary)> DictionaryResultCallback;
112 
113   // Callback used to return List of visibile networks.
114   typedef base::Callback<
115       void(const base::ListValue& network_list)> ListResultCallback;
116 
117   // Takes ownership of |wifi_service| and |crypto_verify|. They are accessed
118   // and deleted on the worker thread. The deletion task is posted during the
119   // NetworkingPrivateServiceClient shutdown.
120   NetworkingPrivateServiceClient(wifi::WiFiService* wifi_service,
121                                  CryptoVerify* crypto_verify);
122 
123   // KeyedService method override.
124   virtual void Shutdown() OVERRIDE;
125 
126   // Gets the properties of the network with id |network_guid|. See note on
127   // |callback| and |error_callback|, in class description above.
128   void GetProperties(const std::string& network_guid,
129                      const DictionaryResultCallback& callback,
130                      const ErrorCallback& error_callback);
131 
132   // Gets the merged properties of the network with id |network_guid| from these
133   // sources: User settings, shared settings, user policy, device policy and
134   // the currently active settings. See note on
135   // |callback| and |error_callback|, in class description above.
136   void GetManagedProperties(const std::string& network_guid,
137                             const DictionaryResultCallback& callback,
138                             const ErrorCallback& error_callback);
139 
140   // Gets the cached read-only properties of the network with id |network_guid|.
141   // This is meant to be a higher performance function than |GetProperties|,
142   // which requires a round trip to query the networking subsystem. It only
143   // returns a subset of the properties returned by |GetProperties|. See note on
144   // |callback| and |error_callback|, in class description above.
145   void GetState(const std::string& network_guid,
146                 const DictionaryResultCallback& callback,
147                 const ErrorCallback& error_callback);
148 
149   // Start connect to the network with id |network_guid|. See note on
150   // |callback| and |error_callback|, in class description above.
151   void StartConnect(const std::string& network_guid,
152                     const base::Closure& callback,
153                     const ErrorCallback& error_callback);
154 
155   // Start disconnect from the network with id |network_guid|. See note on
156   // |callback| and |error_callback|, in class description above.
157   void StartDisconnect(const std::string& network_guid,
158                        const base::Closure& callback,
159                        const ErrorCallback& error_callback);
160 
161   // Sets the |properties| of the network with id |network_guid|. See note on
162   // |callback| and |error_callback|, in class description above.
163   void SetProperties(const std::string& network_guid,
164                      const base::DictionaryValue& properties,
165                      const base::Closure& callback,
166                      const ErrorCallback& error_callback);
167 
168   // Creates a new network configuration from |properties|. If |shared| is true,
169   // share this network configuration with other users. If a matching configured
170   // network already exists, this will fail. On success invokes |callback| with
171   // the |network_guid| of the new network. See note on |callback| and
172   // error_callback|, in class description above.
173   void CreateNetwork(bool shared,
174                      const base::DictionaryValue& properties,
175                      const StringResultCallback& callback,
176                      const ErrorCallback& error_callback);
177 
178   // Requests network scan. Broadcasts NetworkListChangedEvent upon completion.
179   void RequestNetworkScan();
180 
181   // Gets the list of visible networks of |network_type| and calls |callback|.
182   void GetVisibleNetworks(const std::string& network_type,
183                           const ListResultCallback& callback);
184 
185   // Verify that Chromecast provides valid cryptographically signed properties.
186   void VerifyDestination(scoped_ptr<base::ListValue> args,
187                          const BoolResultCallback& callback,
188                          const CryptoErrorCallback& error_callback);
189 
190   // Verify that Chromecast provides valid cryptographically signed properties.
191   // If valid, then get WiFi credentials from the system and encrypt them using
192   // Chromecast's public key.
193   void VerifyAndEncryptCredentials(scoped_ptr<base::ListValue> args,
194                                    const StringResultCallback& callback,
195                                    const CryptoErrorCallback& error_callback);
196 
197   // Verify that Chromecast provides valid cryptographically signed properties.
198   // If valid, then encrypt data using Chromecast's public key.
199   void VerifyAndEncryptData(scoped_ptr<base::ListValue> args,
200                             const StringResultCallback& callback,
201                             const CryptoErrorCallback& error_callback);
202 
203   // Adds observer to network events.
204   void AddObserver(Observer* network_events_observer);
205 
206   // Removes observer to network events. If there is no observers,
207   // then process can be shut down when there are no more calls pending return.
208   void RemoveObserver(Observer* network_events_observer);
209 
210   // NetworkChangeNotifier::NetworkChangeObserver implementation.
211   virtual void OnNetworkChanged(
212       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
213 
214  private:
215   // Callbacks to extension api function objects. Keep reference to API object
216   // and are released in ShutdownOnUIThread. Run when WiFiService calls back
217   // into NetworkingPrivateServiceClient's callback wrappers.
218   typedef int32 ServiceCallbacksID;
219   struct ServiceCallbacks {
220     ServiceCallbacks();
221     ~ServiceCallbacks();
222 
223     DictionaryResultCallback get_properties_callback;
224     base::Closure start_connect_callback;
225     base::Closure start_disconnect_callback;
226     base::Closure set_properties_callback;
227     StringResultCallback create_network_callback;
228     ListResultCallback get_visible_networks_callback;
229     ErrorCallback error_callback;
230 
231     BoolResultCallback verify_destination_callback;
232     StringResultCallback verify_and_encrypt_data_callback;
233     StringResultCallback verify_and_encrypt_credentials_callback;
234     CryptoErrorCallback crypto_error_callback;
235 
236     ServiceCallbacksID id;
237   };
238   typedef IDMap<ServiceCallbacks, IDMapOwnPointer> ServiceCallbacksMap;
239 
240   virtual ~NetworkingPrivateServiceClient();
241 
242   // Callback wrappers.
243   void AfterGetProperties(ServiceCallbacksID callback_id,
244                           const std::string& network_guid,
245                           const base::DictionaryValue* properties,
246                           const std::string* error);
247   void AfterSetProperties(ServiceCallbacksID callback_id,
248                           const std::string* error);
249   void AfterCreateNetwork(ServiceCallbacksID callback_id,
250                           const std::string* network_guid,
251                           const std::string* error);
252   void AfterGetVisibleNetworks(ServiceCallbacksID callback_id,
253                                const base::ListValue* network_list);
254   void AfterStartConnect(ServiceCallbacksID callback_id,
255                          const std::string* error);
256   void AfterStartDisconnect(ServiceCallbacksID callback_id,
257                             const std::string* error);
258   void AfterVerifyDestination(ServiceCallbacksID callback_id,
259                               const bool* result,
260                               const std::string* error);
261   void AfterVerifyAndEncryptData(ServiceCallbacksID callback_id,
262                                  const std::string* result,
263                                  const std::string* error);
264   void AfterVerifyAndEncryptCredentials(ServiceCallbacksID callback_id,
265                                         const std::string& encrypted_data,
266                                         const std::string& error);
267 
268   void OnNetworksChangedEventOnUIThread(
269       const WiFiService::NetworkGuidList& network_guid_list);
270   void OnNetworkListChangedEventOnUIThread(
271       const WiFiService::NetworkGuidList& network_guid_list);
272 
273   // Add new |ServiceCallbacks| to |callbacks_map_|.
274   ServiceCallbacks* AddServiceCallbacks();
275   // Removes ServiceCallbacks for |callback_id| from |callbacks_map_|.
276   void RemoveServiceCallbacks(ServiceCallbacksID callback_id);
277 
278   // Callbacks to run when callback is called from WiFiService.
279   ServiceCallbacksMap callbacks_map_;
280   // Observers to Network Events.
281   ObserverList<Observer> network_events_observers_;
282   // Interface for Verify* methods. Used and deleted on the worker thread.
283   scoped_ptr<CryptoVerify> crypto_verify_;
284   // Interface to WiFiService. Used and deleted on the worker thread.
285   scoped_ptr<wifi::WiFiService> wifi_service_;
286   // Sequence token associated with wifi tasks.
287   base::SequencedWorkerPool::SequenceToken sequence_token_;
288   // Task runner for worker tasks.
289   scoped_refptr<base::SequencedTaskRunner> task_runner_;
290   // Use WeakPtrs for callbacks from |wifi_service_| and |crypto_verify_|.
291   base::WeakPtrFactory<NetworkingPrivateServiceClient> weak_factory_;
292 
293   DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateServiceClient);
294 };
295 
296 }  // namespace extensions
297 
298 #endif  // CHROME_BROWSER_EXTENSIONS_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_
299