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