network_configuration_handler.h revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <map> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/callback.h" 15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/gtest_prod_util.h" 16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/weak_ptr.h" 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromeos/chromeos_export.h" 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromeos/dbus/dbus_method_call_status.h" 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromeos/network/network_handler.h" 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chromeos/network/network_handler_callbacks.h" 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace base { 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass DictionaryValue; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListValue; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace dbus { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ObjectPath; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos { 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The NetworkConfigurationHandler class is used to create and configure 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// networks in ChromeOS. It mostly calls through to the Shill service API, and 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// most calls are asynchronous for that reason. No calls will block on DBus 36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// calls. 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is owned and it's lifetime managed by the Chrome startup code. It's 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// basically a singleton, but with explicit lifetime management. 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// For accessing lists of remembered networks, and other state information, see 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the class NetworkStateHandler. 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Note on callbacks: Because all the functions here are meant to be 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// asynchronous, they all take a |callback| of some type, and an 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// |error_callback|. When the operation succeeds, |callback| will be called, and 47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// when it doesn't, |error_callback| will be called with information about the 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// error, including a symbolic name for the error and often some error message 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that is suitable for logging. None of the error message text is meant for 50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// user consumption. 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass CHROMEOS_EXPORT NetworkConfigurationHandler 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : public base::SupportsWeakPtr<NetworkConfigurationHandler> { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ~NetworkConfigurationHandler(); 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Gets the properties of the network with id |service_path|. See note on 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |callback| and |error_callback|, in class description above. 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void GetProperties( 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& service_path, 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const network_handler::DictionaryResultCallback& callback, 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const network_handler::ErrorCallback& error_callback) const; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Sets the properties of the network with id |service_path|. This means the 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // given properties will be merged with the existing settings, and it won't 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // clear any existing properties. See note on |callback| and |error_callback|, 67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // in class description above. 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void SetProperties( 69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& service_path, 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::DictionaryValue& properties, 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::Closure& callback, 72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback); 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Removes the properties with the given property paths. If any of them are 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // unable to be cleared, the |error_callback| will only be run once with 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // accumulated information about all of the errors as a list attached to the 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // "errors" key of the error data, and the |callback| will not be run, even 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // though some of the properties may have been cleared. If there are no 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // errors, |callback| will be run. 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void ClearProperties(const std::string& service_path, 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::vector<std::string>& property_paths, 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::Closure& callback, 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback); 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates a network with the given properties in the active Shill profile, 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and returns the new service_path to |callback| if successful. See note on 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |callback| and |error_callback|, in class description above. 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This may also be used to update an existing matching configuration, see 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Shill documentation for Manager.ConfigureService and Manger.GetService. 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void CreateConfiguration( 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::DictionaryValue& properties, 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::StringResultCallback& callback, 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback); 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Removes the network |service_path| from any profiles that include it. 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // See note on |callback| and |error_callback| in class description above. 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void RemoveConfiguration( 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& service_path, 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::Closure& callback, 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback); 101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Changes the profile for the network |service_path| to |profile_path|. 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // See note on |callback| and |error_callback| in class description above. 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetNetworkProfile(const std::string& service_path, 105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& profile_path, 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::Closure& callback, 107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Construct and initialize an instance for testing. 110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static NetworkConfigurationHandler* InitializeForTest( 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NetworkStateHandler* network_state_handler); 112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class NetworkHandler; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class NetworkConfigurationHandlerTest; 116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch friend class NetworkConfigurationHandlerStubTest; 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class ProfileEntryDeleter; 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NetworkConfigurationHandler(); 120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void Init(NetworkStateHandler* network_state_handler); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RunCreateNetworkCallback( 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const network_handler::StringResultCallback& callback, 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const dbus::ObjectPath& service_path); 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Called from ProfileEntryDeleter instances when they complete causing 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // this class to delete the instance. 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void ProfileEntryDeleterCompleted(const std::string& service_path); 129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool PendingProfileEntryDeleterForTest(const std::string& service_path) { 130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return profile_entry_deleters_.count(service_path); 131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Invoke the callback and inform NetworkStateHandler to request an update 134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // for the service after setting properties. 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void SetPropertiesSuccessCallback(const std::string& service_path, 136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::Closure& callback); 137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void SetPropertiesErrorCallback( 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& service_path, 139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback, 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& dbus_error_name, 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& dbus_error_message); 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Invoke the callback and inform NetworkStateHandler to request an update 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // for the service after clearing properties. 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void ClearPropertiesSuccessCallback( 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& service_path, 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::vector<std::string>& names, 148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::Closure& callback, 149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback, 150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::ListValue& result); 151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void ClearPropertiesErrorCallback( 152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& service_path, 153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const network_handler::ErrorCallback& error_callback, 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& dbus_error_name, 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& dbus_error_message); 156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Unowned associated NetworkStateHandler* (global or test instance). 158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NetworkStateHandler* network_state_handler_; 159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Map of in-progress deleter instances. Owned by this class. 161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::map<std::string, ProfileEntryDeleter*> profile_entry_deleters_; 162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DISALLOW_COPY_AND_ASSIGN(NetworkConfigurationHandler); 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace chromeos 167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif // CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)