1 // Copyright (c) 2012 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 CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 6 #define CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/basictypes.h" 14 #include "base/callback.h" 15 #include "base/gtest_prod_util.h" 16 #include "base/memory/weak_ptr.h" 17 #include "chromeos/chromeos_export.h" 18 #include "chromeos/dbus/dbus_method_call_status.h" 19 #include "chromeos/network/network_handler.h" 20 #include "chromeos/network/network_handler_callbacks.h" 21 22 namespace base { 23 class DictionaryValue; 24 class ListValue; 25 } 26 27 namespace dbus { 28 class ObjectPath; 29 } 30 31 namespace chromeos { 32 33 // The NetworkConfigurationHandler class is used to create and configure 34 // networks in ChromeOS. It mostly calls through to the Shill service API, and 35 // most calls are asynchronous for that reason. No calls will block on DBus 36 // calls. 37 // 38 // This is owned and it's lifetime managed by the Chrome startup code. It's 39 // basically a singleton, but with explicit lifetime management. 40 // 41 // For accessing lists of remembered networks, and other state information, see 42 // the class NetworkStateHandler. 43 // 44 // Note on callbacks: Because all the functions here are meant to be 45 // asynchronous, they all take a |callback| of some type, and an 46 // |error_callback|. When the operation succeeds, |callback| will be called, and 47 // when it doesn't, |error_callback| will be called with information about the 48 // error, including a symbolic name for the error and often some error message 49 // that is suitable for logging. None of the error message text is meant for 50 // user consumption. 51 52 class CHROMEOS_EXPORT NetworkConfigurationHandler 53 : public base::SupportsWeakPtr<NetworkConfigurationHandler> { 54 public: 55 ~NetworkConfigurationHandler(); 56 57 // Gets the properties of the network with id |service_path|. See note on 58 // |callback| and |error_callback|, in class description above. 59 void GetProperties( 60 const std::string& service_path, 61 const network_handler::DictionaryResultCallback& callback, 62 const network_handler::ErrorCallback& error_callback) const; 63 64 // Sets the properties of the network with id |service_path|. This means the 65 // given properties will be merged with the existing settings, and it won't 66 // clear any existing properties. See note on |callback| and |error_callback|, 67 // in class description above. 68 void SetProperties( 69 const std::string& service_path, 70 const base::DictionaryValue& properties, 71 const base::Closure& callback, 72 const network_handler::ErrorCallback& error_callback); 73 74 // Removes the properties with the given property paths. If any of them are 75 // unable to be cleared, the |error_callback| will only be run once with 76 // accumulated information about all of the errors as a list attached to the 77 // "errors" key of the error data, and the |callback| will not be run, even 78 // though some of the properties may have been cleared. If there are no 79 // errors, |callback| will be run. 80 void ClearProperties(const std::string& service_path, 81 const std::vector<std::string>& property_paths, 82 const base::Closure& callback, 83 const network_handler::ErrorCallback& error_callback); 84 85 // Creates a network with the given properties in the active Shill profile, 86 // and returns the new service_path to |callback| if successful. See note on 87 // |callback| and |error_callback|, in class description above. 88 // This may also be used to update an existing matching configuration, see 89 // Shill documentation for Manager.ConfigureService and Manger.GetService. 90 void CreateConfiguration( 91 const base::DictionaryValue& properties, 92 const network_handler::StringResultCallback& callback, 93 const network_handler::ErrorCallback& error_callback); 94 95 // Removes the network |service_path| from any profiles that include it. 96 // See note on |callback| and |error_callback| in class description above. 97 void RemoveConfiguration( 98 const std::string& service_path, 99 const base::Closure& callback, 100 const network_handler::ErrorCallback& error_callback); 101 102 // Changes the profile for the network |service_path| to |profile_path|. 103 // See note on |callback| and |error_callback| in class description above. 104 void SetNetworkProfile(const std::string& service_path, 105 const std::string& profile_path, 106 const base::Closure& callback, 107 const network_handler::ErrorCallback& error_callback); 108 109 // Construct and initialize an instance for testing. 110 static NetworkConfigurationHandler* InitializeForTest( 111 NetworkStateHandler* network_state_handler); 112 113 protected: 114 friend class NetworkHandler; 115 friend class NetworkConfigurationHandlerTest; 116 friend class NetworkConfigurationHandlerStubTest; 117 class ProfileEntryDeleter; 118 119 NetworkConfigurationHandler(); 120 void Init(NetworkStateHandler* network_state_handler); 121 122 void RunCreateNetworkCallback( 123 const network_handler::StringResultCallback& callback, 124 const dbus::ObjectPath& service_path); 125 126 // Called from ProfileEntryDeleter instances when they complete causing 127 // this class to delete the instance. 128 void ProfileEntryDeleterCompleted(const std::string& service_path); 129 bool PendingProfileEntryDeleterForTest(const std::string& service_path) { 130 return profile_entry_deleters_.count(service_path); 131 } 132 133 // Invoke the callback and inform NetworkStateHandler to request an update 134 // for the service after setting properties. 135 void SetPropertiesSuccessCallback(const std::string& service_path, 136 const base::Closure& callback); 137 void SetPropertiesErrorCallback( 138 const std::string& service_path, 139 const network_handler::ErrorCallback& error_callback, 140 const std::string& dbus_error_name, 141 const std::string& dbus_error_message); 142 143 // Invoke the callback and inform NetworkStateHandler to request an update 144 // for the service after clearing properties. 145 void ClearPropertiesSuccessCallback( 146 const std::string& service_path, 147 const std::vector<std::string>& names, 148 const base::Closure& callback, 149 const network_handler::ErrorCallback& error_callback, 150 const base::ListValue& result); 151 void ClearPropertiesErrorCallback( 152 const std::string& service_path, 153 const network_handler::ErrorCallback& error_callback, 154 const std::string& dbus_error_name, 155 const std::string& dbus_error_message); 156 157 // Unowned associated NetworkStateHandler* (global or test instance). 158 NetworkStateHandler* network_state_handler_; 159 160 // Map of in-progress deleter instances. Owned by this class. 161 std::map<std::string, ProfileEntryDeleter*> profile_entry_deleters_; 162 163 DISALLOW_COPY_AND_ASSIGN(NetworkConfigurationHandler); 164 }; 165 166 } // namespace chromeos 167 168 #endif // CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 169