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 specified Shill 86 // profile, and returns the new service_path to |callback| if successful. 87 // kProfileProperty must be set in |properties|. See note on |callback| and 88 // |error_callback|, in the class description above. This may also be used to 89 // update an existing matching configuration, see Shill documentation for 90 // Manager.ConfigureServiceForProfile. 91 void CreateConfiguration( 92 const base::DictionaryValue& properties, 93 const network_handler::StringResultCallback& callback, 94 const network_handler::ErrorCallback& error_callback); 95 96 // Removes the network |service_path| from any profiles that include it. 97 // See note on |callback| and |error_callback| in class description above. 98 void RemoveConfiguration( 99 const std::string& service_path, 100 const base::Closure& callback, 101 const network_handler::ErrorCallback& error_callback); 102 103 // Changes the profile for the network |service_path| to |profile_path|. 104 // See note on |callback| and |error_callback| in class description above. 105 void SetNetworkProfile(const std::string& service_path, 106 const std::string& profile_path, 107 const base::Closure& callback, 108 const network_handler::ErrorCallback& error_callback); 109 110 // Construct and initialize an instance for testing. 111 static NetworkConfigurationHandler* InitializeForTest( 112 NetworkStateHandler* network_state_handler); 113 114 protected: 115 friend class ClientCertResolverTest; 116 friend class NetworkHandler; 117 friend class NetworkConfigurationHandlerTest; 118 friend class NetworkConfigurationHandlerStubTest; 119 class ProfileEntryDeleter; 120 121 NetworkConfigurationHandler(); 122 void Init(NetworkStateHandler* network_state_handler); 123 124 void RunCreateNetworkCallback( 125 const network_handler::StringResultCallback& callback, 126 const dbus::ObjectPath& service_path); 127 128 // Called from ProfileEntryDeleter instances when they complete causing 129 // this class to delete the instance. 130 void ProfileEntryDeleterCompleted(const std::string& service_path); PendingProfileEntryDeleterForTest(const std::string & service_path)131 bool PendingProfileEntryDeleterForTest(const std::string& service_path) { 132 return profile_entry_deleters_.count(service_path); 133 } 134 135 // Invoke the callback and inform NetworkStateHandler to request an update 136 // for the service after setting properties. 137 void SetPropertiesSuccessCallback(const std::string& service_path, 138 const base::Closure& callback); 139 void SetPropertiesErrorCallback( 140 const std::string& service_path, 141 const network_handler::ErrorCallback& error_callback, 142 const std::string& dbus_error_name, 143 const std::string& dbus_error_message); 144 145 // Invoke the callback and inform NetworkStateHandler to request an update 146 // for the service after clearing properties. 147 void ClearPropertiesSuccessCallback( 148 const std::string& service_path, 149 const std::vector<std::string>& names, 150 const base::Closure& callback, 151 const base::ListValue& result); 152 void ClearPropertiesErrorCallback( 153 const std::string& service_path, 154 const network_handler::ErrorCallback& error_callback, 155 const std::string& dbus_error_name, 156 const std::string& dbus_error_message); 157 158 // Unowned associated NetworkStateHandler* (global or test instance). 159 NetworkStateHandler* network_state_handler_; 160 161 // Map of in-progress deleter instances. Owned by this class. 162 std::map<std::string, ProfileEntryDeleter*> profile_entry_deleters_; 163 164 DISALLOW_COPY_AND_ASSIGN(NetworkConfigurationHandler); 165 }; 166 167 } // namespace chromeos 168 169 #endif // CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_ 170