Index: chrome/browser/extensions/api/networking_private/networking_private_service_client.h |
diff --git a/chrome/browser/extensions/api/networking_private/networking_private_service_client.h b/chrome/browser/extensions/api/networking_private/networking_private_service_client.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ce4b5fff10d8263e677c09f0129a1e61be1acad1 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/networking_private/networking_private_service_client.h |
@@ -0,0 +1,238 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_EXTENSIONS_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_ |
+#define CHROME_BROWSER_EXTENSIONS_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_ |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/id_map.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/observer_list.h" |
+#include "base/strings/string16.h" |
+#include "base/supports_user_data.h" |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "base/values.h" |
+#include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
+#include "components/wifi/wifi_service.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/utility_process_host.h" |
+#include "content/public/browser/utility_process_host_client.h" |
+ |
+namespace base { |
+class SequencedTaskRunner; |
+} // namespace base |
+ |
+namespace wifi { |
+class WiFiService; |
+} // namespace wifi |
+ |
+namespace extensions { |
+ |
+namespace api { |
+namespace networking_private { |
+struct VerificationProperties; |
+} // namespace networking_private |
+} // namespace api |
+ |
+using api::networking_private::VerificationProperties; |
+using wifi::WiFiService; |
+ |
+// This class is the client wrapper for the WiFiService and CryptoVerify |
tbarzic
2013/11/19 00:05:44
nit: I'd drop "This class is" part of the comment.
mef
2013/11/19 17:37:03
Done.
|
+// interfaces to invoke them on worker thread. |
+class NetworkingPrivateServiceClient |
+ : public BrowserContextKeyedService { |
+ public: |
+ // Interface for Verify* methods implementation. |
+ class CryptoVerify { |
+ public: |
+ virtual void VerifyDestination(scoped_ptr<base::ListValue> args, |
+ bool* verified, |
+ std::string* error) = 0; |
+ |
+ virtual void VerifyAndEncryptData(scoped_ptr<base::ListValue> args, |
+ std::string* base64_encoded_ciphertext, |
+ std::string* error) = 0; |
+ |
+ virtual ~CryptoVerify() { } |
tbarzic
2013/11/19 00:05:44
nit: nuke the space inside {}
and move this before
mef
2013/11/19 17:37:03
Done.
|
+ |
+ static CryptoVerify* Create(); |
+ }; |
+ |
+ // Interface for observing Network Events. |
+ class Observer { |
+ public: |
+ virtual void OnNetworksChangedEvent( |
+ const std::vector<std::string>& network_guids) = 0; |
+ virtual void OnNetworkListChangedEvent( |
+ const std::vector<std::string>& network_guids) = 0; |
+ virtual ~Observer() { } |
+ }; |
+ |
+ // An error callback used by most of API functions to receive error results |
+ // from the NetworkingPrivateServiceClient. |
+ typedef base::Callback< |
+ void(const std::string& error_name, |
+ scoped_ptr<base::DictionaryValue> error_data)> ErrorCallback; |
+ |
+ // An error callback used by most of Crypto Verify* API functions to receive |
+ // error results from the NetworkingPrivateServiceClient. |
+ // TODO(mef): Cleanup networking_private_api.* to make consistent |
+ // NetworkingPrivateXXXFunction naming and error callbacks. |
+ typedef base::Callback< |
+ void(const std::string& error_name, |
+ const std::string& error)> CryptoErrorCallback; |
+ |
+ // Callback used to return bool result from VerifyDestination function. |
+ typedef base::Callback<void(bool result)> BoolResultCallback; |
+ |
+ // Callback used to return string result from VerifyAndEncryptData function. |
+ typedef base::Callback<void(const std::string& result)> StringResultCallback; |
+ |
+ // Callback used to return Dictionary of network properties. |
+ typedef base::Callback< |
+ void(const std::string& network_guid, |
+ const base::DictionaryValue& dictionary)> DictionaryResultCallback; |
+ |
+ // Callback used to return List of visibile networks. |
+ typedef base::Callback< |
+ void(const base::ListValue& network_list)> ListResultCallback; |
+ |
+ // Takes ownership of |wifi_service| and |crypto_verify|. They are accessed |
+ // on worker thread and deleted from worker thread after all tasks are done. |
tbarzic
2013/11/19 00:05:44
nit: "after all tasks are done" is not completely
mef
2013/11/19 17:37:03
Done.
|
+ NetworkingPrivateServiceClient(wifi::WiFiService* wifi_service, |
tbarzic
2013/11/19 00:05:44
theoretically, could wifi service be shared across
mef
2013/11/19 17:37:03
I think it could, but that would complicate its li
|
+ CryptoVerify* crypto_verify); |
+ |
+ // Gets the properties of the network with id |service_path|. See note on |
+ // |callback| and |error_callback|, in class description above. |
+ void GetProperties(const std::string& service_path, |
+ const DictionaryResultCallback& callback, |
+ const ErrorCallback& error_callback); |
+ |
+ // Start connect to the network with id |service_path|. See note on |
+ // |callback| and |error_callback|, in class description above. |
+ void StartConnect(const std::string& service_path, |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback); |
+ |
+ // Start disconnect from the network with id |service_path|. See note on |
+ // |callback| and |error_callback|, in class description above. |
+ void StartDisconnect(const std::string& service_path, |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback); |
+ |
+ // Sets the |properties| of the network with id |service_path|. See note on |
+ // |callback| and |error_callback|, in class description above. |
+ void SetProperties(const std::string& service_path, |
+ const base::DictionaryValue& properties, |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback); |
+ |
+ // Requests network scan. Broadcasts NetworkListChangedEvent upon completion. |
+ void RequestNetworkScan(); |
+ |
+ // Gets the list of visible networks and calls |callback|. |
+ void GetVisibleNetworks(const ListResultCallback& callback); |
+ |
+ // Verify that Chromecast provides valid cryptographically signed properties. |
+ void VerifyDestination(scoped_ptr<base::ListValue> args, |
+ const BoolResultCallback& callback, |
+ const CryptoErrorCallback& error_callback); |
+ |
+ // Verify that Chromecast provides valid cryptographically signed properties. |
+ // If valid, then encrypt data using Chromecast's public key. |
+ void VerifyAndEncryptData(scoped_ptr<base::ListValue> args, |
+ const StringResultCallback& callback, |
+ const CryptoErrorCallback& error_callback); |
+ |
+ // Adds observer to network events. |
+ void AddObserver(Observer* network_events_observer); |
+ |
+ // Removes observer to network events. If there is no observers, |
+ // then process can be shut down when there are no more calls pending return. |
+ void RemoveObserver(Observer* network_events_observer); |
+ |
+ // BrowserContextKeyedServices method override. |
tbarzic
2013/11/19 00:05:44
nit: I'd move this after ctor
mef
2013/11/19 17:37:03
Done.
|
+ virtual void Shutdown() OVERRIDE; |
+ |
+ private: |
+ // Callbacks to extension api function objects. Keep reference to API object |
+ // and are released in ShutdownOnUIThread. Run when WiFiService calls back |
+ // into NetworkingPrivateServiceClient's callback wrappers. |
+ typedef int32 ServiceCallbacksID; |
+ struct ServiceCallbacks { |
+ ServiceCallbacks(); |
+ ~ServiceCallbacks(); |
+ |
+ DictionaryResultCallback get_properties_callback; |
+ base::Closure start_connect_callback; |
+ base::Closure start_disconnect_callback; |
+ base::Closure set_properties_callback; |
+ ListResultCallback get_visible_networks_callback; |
+ ErrorCallback error_callback; |
+ |
+ BoolResultCallback verify_destination_callback; |
+ StringResultCallback verify_and_encrypt_data_callback; |
+ CryptoErrorCallback crypto_error_callback; |
+ |
+ ServiceCallbacksID id; |
+ }; |
+ typedef IDMap<ServiceCallbacks, IDMapOwnPointer> ServiceCallbacksMap; |
+ |
+ // Callback wrappers. |
+ void AfterGetProperties(ServiceCallbacksID callback_id, |
+ const std::string& network_guid, |
+ const DictionaryValue* properties, |
+ const std::string* error); |
+ void AfterSetProperties(ServiceCallbacksID callback_id, |
+ const std::string* error); |
+ void AfterGetVisibleNetworks(ServiceCallbacksID callback_id, |
+ const ListValue* network_list); |
+ void AfterStartConnect(ServiceCallbacksID callback_id, |
+ const std::string* error); |
+ void AfterStartDisconnect(ServiceCallbacksID callback_id, |
+ const std::string* error); |
+ void AfterVerifyDestination(ServiceCallbacksID callback_id, |
+ const bool* result, |
+ const std::string* error); |
+ void AfterVerifyAndEncryptData(ServiceCallbacksID callback_id, |
+ const std::string* result, |
+ const std::string* error); |
+ |
+ void OnNetworksChangedEventOnUIThread( |
+ const WiFiService::NetworkGuidList& network_guid_list); |
+ void OnNetworkListChangedEventOnUIThread( |
+ const WiFiService::NetworkGuidList& network_guid_list); |
+ |
+ virtual ~NetworkingPrivateServiceClient(); |
tbarzic
2013/11/19 00:05:44
move this after ServiceCallbacksMap typedef
mef
2013/11/19 17:37:03
Done.
|
+ |
+ // Add new |ServiceCallbacks| to |callbacks_map_|. |
+ ServiceCallbacks* AddServiceCallbacks(); |
+ // Removes ServiceCallbacks for |callback_id| from |callbacks_map_|. |
+ void RemoveServiceCallbacks(ServiceCallbacksID callback_id); |
+ |
+ // Callbacks to run when callback is called from WiFiService. |
+ ServiceCallbacksMap callbacks_map_; |
+ // Observers to Network Events. |
+ ObserverList<Observer> network_events_observers_; |
+ // Interface for Verify* methods. Lives on the worker thread. |
+ scoped_ptr<CryptoVerify> crypto_verify_; |
+ // Interface to WiFiService. Lives on the worker thread. |
tbarzic
2013/11/19 00:05:44
s/Lives on/Used on/
mef
2013/11/19 17:37:03
Done.
|
+ scoped_ptr<wifi::WiFiService> wifi_service_; |
+ // Sequence token associated with wifi tasks. |
+ base::SequencedWorkerPool::SequenceToken sequence_token_; |
+ // Task runner for worker tasks. |
+ scoped_refptr<base::SequencedTaskRunner> task_runner_; |
+ // Use WeakPtrs for callbacks from |wifi_service_| and |crypto_verify_|. |
+ base::WeakPtrFactory<NetworkingPrivateServiceClient> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateServiceClient); |
+}; |
+ |
+} // namespace extensions |
+ |
+#endif // CHROME_BROWSER_EXTENSIONS_API_NETWORKING_PRIVATE_NETWORKING_PRIVATE_SERVICE_CLIENT_H_ |