network_device_handler.h revision a3f7b4e666c476898878fa745f637129375cd889
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 CHROMEOS_NETWORK_NETWORK_DEVICE_HANDLER_H_ 6#define CHROMEOS_NETWORK_NETWORK_DEVICE_HANDLER_H_ 7 8#include <string> 9 10#include "base/callback.h" 11#include "base/gtest_prod_util.h" 12#include "base/memory/weak_ptr.h" 13#include "chromeos/chromeos_export.h" 14#include "chromeos/network/network_handler.h" 15#include "chromeos/network/network_handler_callbacks.h" 16 17namespace base { 18 19class Value; 20 21} // namespace base 22 23namespace chromeos { 24 25// The NetworkDeviceHandler class allows making device specific requests on a 26// ChromeOS network device. All calls are asynchronous and interact with the 27// Shill device API. No calls will block on DBus calls. 28// 29// This is owned and its lifetime managed by the Chrome startup code. It's 30// basically a singleton, but with explicit lifetime management. 31// 32// Note on callbacks: Because all the functions here are meant to be 33// asynchronous, they all take a |callback| of some type, and an 34// |error_callback|. When the operation succeeds, |callback| will be called, and 35// when it doesn't, |error_callback| will be called with information about the 36// error, including a symbolic name for the error and often some error message 37// that is suitable for logging. None of the error message text is meant for 38// user consumption. 39 40class CHROMEOS_EXPORT NetworkDeviceHandler { 41 public: 42 43 // Constants for |error_name| from |error_callback|. 44 static const char kErrorFailure[]; 45 static const char kErrorIncorrectPin[]; 46 static const char kErrorNotFound[]; 47 static const char kErrorNotSupported[]; 48 static const char kErrorPinBlocked[]; 49 static const char kErrorPinRequired[]; 50 static const char kErrorUnknown[]; 51 52 virtual ~NetworkDeviceHandler(); 53 54 // Gets the properties of the device with id |device_path|. See note on 55 // |callback| and |error_callback|, in class description above. 56 void GetDeviceProperties( 57 const std::string& device_path, 58 const network_handler::DictionaryResultCallback& callback, 59 const network_handler::ErrorCallback& error_callback) const; 60 61 // Sets the value of property |name| on device with id |device_path| to 62 // |value|. 63 void SetDeviceProperty( 64 const std::string& device_path, 65 const std::string& name, 66 const base::Value& value, 67 const base::Closure& callback, 68 const network_handler::ErrorCallback& error_callback); 69 70 // Requests a refresh of the IP configuration for the device specified by 71 // |device_path| if it exists. This will apply any newly configured 72 // properties and renew the DHCP lease. 73 void RequestRefreshIPConfigs( 74 const std::string& device_path, 75 const base::Closure& callback, 76 const network_handler::ErrorCallback& error_callback); 77 78 // Requests a network scan on the device specified by |device_path|. 79 // For cellular networks, the result of this call gets asynchronously stored 80 // in the corresponding DeviceState object through a property update. For all 81 // other technologies a service gets created for each found network, which 82 // can be accessed through the corresponding NetworkState object. 83 // 84 // TODO(armansito): Device.ProposeScan is deprecated and the preferred method 85 // of requesting a network scan is Manager.RequestScan, however shill 86 // currently doesn't support cellular network scans via Manager.RequestScan. 87 // Remove this method once shill supports it (crbug.com/262356). 88 void ProposeScan( 89 const std::string& device_path, 90 const base::Closure& callback, 91 const network_handler::ErrorCallback& error_callback); 92 93 // Tells the device to set the modem carrier firmware, as specified by 94 // |carrier|. 95 // 96 // See note on |callback| and |error_callback| in the class description 97 // above. The operation will fail if: 98 // - Device |device_path| could not be found. 99 // - |carrier| doesn't match one of the supported carriers, as reported by 100 // - Shill. 101 // - Operation is not supported by the device. 102 void SetCarrier( 103 const std::string& device_path, 104 const std::string& carrier, 105 const base::Closure& callback, 106 const network_handler::ErrorCallback& error_callback); 107 108 // SIM PIN/PUK methods 109 110 // Tells the device whether or not a SIM PIN lock should be enforced by 111 // the device referenced by |device_path|. If |require_pin| is true, a PIN 112 // code (specified in |pin|) will be required before the next time the device 113 // can be enabled. If |require_pin| is false, the existing requirement will 114 // be lifted. 115 // 116 // See note on |callback| and |error_callback| in the class description 117 // above. The operation will fail if: 118 // - Device |device_path| could not be found. 119 // - The PIN requirement status already matches |require_pin|. 120 // - |pin| doesn't match the PIN code currently stored by the SIM. 121 // - No SIM exists on the device. 122 // 123 // This method applies to Cellular devices only. The call will fail with a 124 // "not-supported" error if called on a non-cellular device. 125 void RequirePin( 126 const std::string& device_path, 127 bool require_pin, 128 const std::string& pin, 129 const base::Closure& callback, 130 const network_handler::ErrorCallback& error_callback); 131 132 // Sends the PIN code |pin| to the device |device_path|. 133 // 134 // See note on |callback| and |error_callback| in the class description 135 // above. The operation will fail if: 136 // - Device |device_path| could not be found. 137 // - |pin| is incorrect. 138 // - The SIM is blocked. 139 // 140 // This method applies to Cellular devices only. The call will fail with a 141 // "not-supported" error if called on a non-cellular device. 142 void EnterPin( 143 const std::string& device_path, 144 const std::string& pin, 145 const base::Closure& callback, 146 const network_handler::ErrorCallback& error_callback); 147 148 // Sends the PUK code |puk| to the SIM to unblock a blocked SIM. On success, 149 // the SIM will be unblocked and its PIN code will be set to |pin|. 150 // 151 // See note on |callback| and |error_callback| in the class description 152 // above. The operation will fail if: 153 // - Device |device_path| could not be found. 154 // - |puk| is incorrect. 155 // 156 // This method applies to Cellular devices only. The call will fail with a 157 // "not-supported" error if called on a non-cellular device. 158 void UnblockPin( 159 const std::string& device_path, 160 const std::string& puk, 161 const std::string& new_pin, 162 const base::Closure& callback, 163 const network_handler::ErrorCallback& error_callback); 164 165 // Tells the device to change the PIN code used to unlock a locked SIM card. 166 // 167 // See note on |callback| and |error_callback| in the class description 168 // above. The operation will fail if: 169 // - Device |device_path| could not be found. 170 // - |old_pin| does not match the current PIN on the device. 171 // - The SIM is locked. 172 // - The SIM is blocked. 173 // 174 // This method applies to Cellular devices only. The call will fail with a 175 // "not-supported" error if called on a non-cellular device. 176 void ChangePin( 177 const std::string& device_path, 178 const std::string& old_pin, 179 const std::string& new_pin, 180 const base::Closure& callback, 181 const network_handler::ErrorCallback& error_callback); 182 183 private: 184 friend class NetworkHandler; 185 friend class NetworkDeviceHandlerTest; 186 FRIEND_TEST_ALL_PREFIXES(NetworkDeviceHandlerTest, ErrorTranslation); 187 188 NetworkDeviceHandler(); 189 190 void HandleShillCallFailureForTest( 191 const std::string& device_path, 192 const network_handler::ErrorCallback& error_callback, 193 const std::string& error_name, 194 const std::string& error_message); 195 196 DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandler); 197}; 198 199} // namespace chromeos 200 201#endif // CHROMEOS_NETWORK_NETWORK_DEVICE_HANDLER_H_ 202