Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/cros/cros_network_functions.h" | 5 #include "chrome/browser/chromeos/cros/cros_network_functions.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/cros/gvalue_util.h" | 10 #include "chrome/browser/chromeos/cros/gvalue_util.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 } | 164 } |
| 165 // |data_plan_vector| will be owned by callback. | 165 // |data_plan_vector| will be owned by callback. |
| 166 watcher->callback_.Run(modem_service_path, data_plan_vector); | 166 watcher->callback_.Run(modem_service_path, data_plan_vector); |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 DataPlanUpdateWatcherCallback callback_; | 170 DataPlanUpdateWatcherCallback callback_; |
| 171 DataPlanUpdateMonitor monitor_; | 171 DataPlanUpdateMonitor monitor_; |
| 172 }; | 172 }; |
| 173 | 173 |
| 174 // Converts a string to a CellularDataPlanType. | |
| 175 CellularDataPlanType ParseCellularDataPlanType(const std::string& type) { | |
| 176 if (type == cashew::kCellularDataPlanUnlimited) | |
| 177 return CELLULAR_DATA_PLAN_UNLIMITED; | |
| 178 if (type == cashew::kCellularDataPlanMeteredPaid) | |
| 179 return CELLULAR_DATA_PLAN_METERED_PAID; | |
| 180 if (type == cashew::kCellularDataPlanMeteredBase) | |
| 181 return CELLULAR_DATA_PLAN_METERED_BASE; | |
| 182 return CELLULAR_DATA_PLAN_UNKNOWN; | |
| 183 } | |
| 184 | |
| 185 // Gets a string property from dictionary. | |
| 186 bool GetStringProperty(const base::DictionaryValue& dictionary, | |
| 187 const std::string& key, | |
| 188 std::string* out) { | |
| 189 const bool result = dictionary.GetStringWithoutPathExpansion(key, out); | |
| 190 LOG_IF(ERROR, !result) << "Cannnot get property " << key; | |
|
stevenjb
2012/04/25 01:37:50
should just be a WARNING
hashimoto
2012/04/25 15:40:17
Done.
| |
| 191 return result; | |
| 192 } | |
| 193 | |
| 194 // Gets an int64 property from dictionary. | |
| 195 bool GetInt64Property(const base::DictionaryValue& dictionary, | |
| 196 const std::string& key, | |
| 197 int64* out) { | |
| 198 // Int64 value is stored as a double because it cannot be fitted in int32. | |
| 199 double value_double = 0; | |
| 200 const bool result = dictionary.GetDoubleWithoutPathExpansion(key, | |
| 201 &value_double); | |
| 202 if (result) | |
| 203 *out = value_double; | |
| 204 else | |
| 205 LOG(ERROR) << "Cannnot get property " << key; | |
|
stevenjb
2012/04/25 01:37:50
WARNING
hashimoto
2012/04/25 15:40:17
Done.
| |
| 206 return result; | |
| 207 } | |
| 208 | |
| 209 // Gets a base::Time property from dictionary. | |
| 210 bool GetTimeProperty(const base::DictionaryValue& dictionary, | |
| 211 const std::string& key, | |
| 212 base::Time* out) { | |
| 213 int64 value_int64 = 0; | |
| 214 if (!GetInt64Property(dictionary, key, &value_int64)) | |
| 215 return false; | |
| 216 *out = base::Time::FromInternalValue(value_int64); | |
| 217 return true; | |
| 218 } | |
| 219 | |
| 220 // Class to watch data plan update without Libcros. | |
| 221 class DataPlanUpdateWatcher : public CrosNetworkWatcher { | |
| 222 public: | |
| 223 explicit DataPlanUpdateWatcher(const DataPlanUpdateWatcherCallback& callback) | |
| 224 : callback_(callback) { | |
| 225 DBusThreadManager::Get()->GetCashewClient()->SetDataPlansUpdateHandler( | |
| 226 base::Bind(&DataPlanUpdateWatcher::OnDataPlansUpdate, | |
| 227 base::Unretained(this))); | |
| 228 } | |
| 229 virtual ~DataPlanUpdateWatcher() { | |
| 230 DBusThreadManager::Get()->GetCashewClient()->ResetDataPlansUpdateHandler(); | |
| 231 } | |
| 232 | |
| 233 private: | |
| 234 void OnDataPlansUpdate(const std::string& service, | |
| 235 const base::ListValue& data_plans) { | |
| 236 CellularDataPlanVector* data_plan_vector = new CellularDataPlanVector; | |
| 237 for (size_t i = 0; i != data_plans.GetSize(); ++i) { | |
| 238 base::DictionaryValue* data_plan = NULL; | |
| 239 if (!data_plans.GetDictionary(i, &data_plan)) { | |
| 240 LOG(ERROR) << "data_plans[" << i << "] is not a dictionary."; | |
| 241 continue; | |
| 242 } | |
| 243 CellularDataPlan* plan = new CellularDataPlan; | |
| 244 // Plan name. | |
| 245 GetStringProperty(*data_plan, cashew::kCellularPlanNameProperty, | |
| 246 &plan->plan_name); | |
| 247 // Plan type. | |
| 248 std::string plan_type_string; | |
| 249 GetStringProperty(*data_plan, cashew::kCellularPlanTypeProperty, | |
| 250 &plan_type_string); | |
| 251 plan->plan_type = ParseCellularDataPlanType(plan_type_string); | |
| 252 // Update time. | |
| 253 GetTimeProperty(*data_plan, cashew::kCellularPlanUpdateTimeProperty, | |
| 254 &plan->update_time); | |
| 255 // Start time. | |
| 256 GetTimeProperty(*data_plan, cashew::kCellularPlanStartProperty, | |
| 257 &plan->plan_start_time); | |
| 258 // End time. | |
| 259 GetTimeProperty(*data_plan, cashew::kCellularPlanEndProperty, | |
| 260 &plan->plan_end_time); | |
| 261 // Data bytes. | |
| 262 GetInt64Property(*data_plan, cashew::kCellularPlanDataBytesProperty, | |
| 263 &plan->plan_data_bytes); | |
| 264 // Bytes used. | |
| 265 GetInt64Property(*data_plan, cashew::kCellularDataBytesUsedProperty, | |
| 266 &plan->data_bytes_used); | |
| 267 data_plan_vector->push_back(plan); | |
| 268 } | |
| 269 callback_.Run(service, data_plan_vector); | |
| 270 } | |
| 271 | |
| 272 DataPlanUpdateWatcherCallback callback_; | |
| 273 }; | |
| 274 | |
| 174 // Class to watch sms with Libcros. | 275 // Class to watch sms with Libcros. |
| 175 class CrosSMSWatcher : public CrosNetworkWatcher { | 276 class CrosSMSWatcher : public CrosNetworkWatcher { |
| 176 public: | 277 public: |
| 177 CrosSMSWatcher(const std::string& modem_device_path, | 278 CrosSMSWatcher(const std::string& modem_device_path, |
| 178 MonitorSMSCallback callback, | 279 MonitorSMSCallback callback, |
| 179 void* object) | 280 void* object) |
| 180 : monitor_(chromeos::MonitorSMS(modem_device_path.c_str(), | 281 : monitor_(chromeos::MonitorSMS(modem_device_path.c_str(), |
| 181 callback, object)) {} | 282 callback, object)) {} |
| 182 virtual ~CrosSMSWatcher() { | 283 virtual ~CrosSMSWatcher() { |
| 183 chromeos::DisconnectSMSMonitor(monitor_); | 284 chromeos::DisconnectSMSMonitor(monitor_); |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 const NetworkPropertiesWatcherCallback& callback, | 493 const NetworkPropertiesWatcherCallback& callback, |
| 393 const std::string& device_path) { | 494 const std::string& device_path) { |
| 394 if (g_libcros_network_functions_enabled) | 495 if (g_libcros_network_functions_enabled) |
| 395 return new CrosNetworkDevicePropertiesWatcher(callback, device_path); | 496 return new CrosNetworkDevicePropertiesWatcher(callback, device_path); |
| 396 else | 497 else |
| 397 return new NetworkDevicePropertiesWatcher(callback, device_path); | 498 return new NetworkDevicePropertiesWatcher(callback, device_path); |
| 398 } | 499 } |
| 399 | 500 |
| 400 CrosNetworkWatcher* CrosMonitorCellularDataPlan( | 501 CrosNetworkWatcher* CrosMonitorCellularDataPlan( |
| 401 const DataPlanUpdateWatcherCallback& callback) { | 502 const DataPlanUpdateWatcherCallback& callback) { |
| 402 return new CrosDataPlanUpdateWatcher(callback); | 503 if (g_libcros_network_functions_enabled) |
| 504 return new CrosDataPlanUpdateWatcher(callback); | |
| 505 else | |
| 506 return new DataPlanUpdateWatcher(callback); | |
| 403 } | 507 } |
| 404 | 508 |
| 405 CrosNetworkWatcher* CrosMonitorSMS(const std::string& modem_device_path, | 509 CrosNetworkWatcher* CrosMonitorSMS(const std::string& modem_device_path, |
| 406 MonitorSMSCallback callback, | 510 MonitorSMSCallback callback, |
| 407 void* object) { | 511 void* object) { |
| 408 return new CrosSMSWatcher(modem_device_path, callback, object); | 512 return new CrosSMSWatcher(modem_device_path, callback, object); |
| 409 } | 513 } |
| 410 | 514 |
| 411 void CrosRequestNetworkServiceConnect(const std::string& service_path, | 515 void CrosRequestNetworkServiceConnect(const std::string& service_path, |
| 412 NetworkActionCallback callback, | 516 NetworkActionCallback callback, |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 762 ScopedGHashTable ghash( | 866 ScopedGHashTable ghash( |
| 763 ConvertDictionaryValueToStringValueGHashTable(properties)); | 867 ConvertDictionaryValueToStringValueGHashTable(properties)); |
| 764 chromeos::ConfigureService("", ghash.get(), OnConfigureService, NULL); | 868 chromeos::ConfigureService("", ghash.get(), OnConfigureService, NULL); |
| 765 } else { | 869 } else { |
| 766 DBusThreadManager::Get()->GetFlimflamManagerClient()->ConfigureService( | 870 DBusThreadManager::Get()->GetFlimflamManagerClient()->ConfigureService( |
| 767 properties, base::Bind(&DoNothing)); | 871 properties, base::Bind(&DoNothing)); |
| 768 } | 872 } |
| 769 } | 873 } |
| 770 | 874 |
| 771 } // namespace chromeos | 875 } // namespace chromeos |
| OLD | NEW |