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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 } | 165 } |
166 // |data_plan_vector| will be owned by callback. | 166 // |data_plan_vector| will be owned by callback. |
167 watcher->callback_.Run(modem_service_path, data_plan_vector); | 167 watcher->callback_.Run(modem_service_path, data_plan_vector); |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 DataPlanUpdateWatcherCallback callback_; | 171 DataPlanUpdateWatcherCallback callback_; |
172 DataPlanUpdateMonitor monitor_; | 172 DataPlanUpdateMonitor monitor_; |
173 }; | 173 }; |
174 | 174 |
| 175 // Converts a string to a CellularDataPlanType. |
| 176 CellularDataPlanType ParseCellularDataPlanType(const std::string& type) { |
| 177 if (type == cashew::kCellularDataPlanUnlimited) |
| 178 return CELLULAR_DATA_PLAN_UNLIMITED; |
| 179 if (type == cashew::kCellularDataPlanMeteredPaid) |
| 180 return CELLULAR_DATA_PLAN_METERED_PAID; |
| 181 if (type == cashew::kCellularDataPlanMeteredBase) |
| 182 return CELLULAR_DATA_PLAN_METERED_BASE; |
| 183 return CELLULAR_DATA_PLAN_UNKNOWN; |
| 184 } |
| 185 |
| 186 // Gets a string property from dictionary. |
| 187 bool GetStringProperty(const base::DictionaryValue& dictionary, |
| 188 const std::string& key, |
| 189 std::string* out) { |
| 190 const bool result = dictionary.GetStringWithoutPathExpansion(key, out); |
| 191 LOG_IF(WARNING, !result) << "Cannnot get property " << key; |
| 192 return result; |
| 193 } |
| 194 |
| 195 // Gets an int64 property from dictionary. |
| 196 bool GetInt64Property(const base::DictionaryValue& dictionary, |
| 197 const std::string& key, |
| 198 int64* out) { |
| 199 // Int64 value is stored as a double because it cannot be fitted in int32. |
| 200 double value_double = 0; |
| 201 const bool result = dictionary.GetDoubleWithoutPathExpansion(key, |
| 202 &value_double); |
| 203 if (result) |
| 204 *out = value_double; |
| 205 else |
| 206 LOG(WARNING) << "Cannnot get property " << key; |
| 207 return result; |
| 208 } |
| 209 |
| 210 // Gets a base::Time property from dictionary. |
| 211 bool GetTimeProperty(const base::DictionaryValue& dictionary, |
| 212 const std::string& key, |
| 213 base::Time* out) { |
| 214 int64 value_int64 = 0; |
| 215 if (!GetInt64Property(dictionary, key, &value_int64)) |
| 216 return false; |
| 217 *out = base::Time::FromInternalValue(value_int64); |
| 218 return true; |
| 219 } |
| 220 |
| 221 // Class to watch data plan update without Libcros. |
| 222 class DataPlanUpdateWatcher : public CrosNetworkWatcher { |
| 223 public: |
| 224 explicit DataPlanUpdateWatcher(const DataPlanUpdateWatcherCallback& callback) |
| 225 : callback_(callback) { |
| 226 DBusThreadManager::Get()->GetCashewClient()->SetDataPlansUpdateHandler( |
| 227 base::Bind(&DataPlanUpdateWatcher::OnDataPlansUpdate, |
| 228 base::Unretained(this))); |
| 229 } |
| 230 virtual ~DataPlanUpdateWatcher() { |
| 231 DBusThreadManager::Get()->GetCashewClient()->ResetDataPlansUpdateHandler(); |
| 232 } |
| 233 |
| 234 private: |
| 235 void OnDataPlansUpdate(const std::string& service, |
| 236 const base::ListValue& data_plans) { |
| 237 CellularDataPlanVector* data_plan_vector = new CellularDataPlanVector; |
| 238 for (size_t i = 0; i != data_plans.GetSize(); ++i) { |
| 239 base::DictionaryValue* data_plan = NULL; |
| 240 if (!data_plans.GetDictionary(i, &data_plan)) { |
| 241 LOG(ERROR) << "data_plans[" << i << "] is not a dictionary."; |
| 242 continue; |
| 243 } |
| 244 CellularDataPlan* plan = new CellularDataPlan; |
| 245 // Plan name. |
| 246 GetStringProperty(*data_plan, cashew::kCellularPlanNameProperty, |
| 247 &plan->plan_name); |
| 248 // Plan type. |
| 249 std::string plan_type_string; |
| 250 GetStringProperty(*data_plan, cashew::kCellularPlanTypeProperty, |
| 251 &plan_type_string); |
| 252 plan->plan_type = ParseCellularDataPlanType(plan_type_string); |
| 253 // Update time. |
| 254 GetTimeProperty(*data_plan, cashew::kCellularPlanUpdateTimeProperty, |
| 255 &plan->update_time); |
| 256 // Start time. |
| 257 GetTimeProperty(*data_plan, cashew::kCellularPlanStartProperty, |
| 258 &plan->plan_start_time); |
| 259 // End time. |
| 260 GetTimeProperty(*data_plan, cashew::kCellularPlanEndProperty, |
| 261 &plan->plan_end_time); |
| 262 // Data bytes. |
| 263 GetInt64Property(*data_plan, cashew::kCellularPlanDataBytesProperty, |
| 264 &plan->plan_data_bytes); |
| 265 // Bytes used. |
| 266 GetInt64Property(*data_plan, cashew::kCellularDataBytesUsedProperty, |
| 267 &plan->data_bytes_used); |
| 268 data_plan_vector->push_back(plan); |
| 269 } |
| 270 callback_.Run(service, data_plan_vector); |
| 271 } |
| 272 |
| 273 DataPlanUpdateWatcherCallback callback_; |
| 274 }; |
| 275 |
175 // Class to watch sms with Libcros. | 276 // Class to watch sms with Libcros. |
176 class CrosSMSWatcher : public CrosNetworkWatcher { | 277 class CrosSMSWatcher : public CrosNetworkWatcher { |
177 public: | 278 public: |
178 CrosSMSWatcher(const std::string& modem_device_path, | 279 CrosSMSWatcher(const std::string& modem_device_path, |
179 MonitorSMSCallback callback, | 280 MonitorSMSCallback callback, |
180 void* object) | 281 void* object) |
181 : monitor_(chromeos::MonitorSMS(modem_device_path.c_str(), | 282 : monitor_(chromeos::MonitorSMS(modem_device_path.c_str(), |
182 callback, object)) {} | 283 callback, object)) {} |
183 virtual ~CrosSMSWatcher() { | 284 virtual ~CrosSMSWatcher() { |
184 chromeos::DisconnectSMSMonitor(monitor_); | 285 chromeos::DisconnectSMSMonitor(monitor_); |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 const NetworkPropertiesWatcherCallback& callback, | 515 const NetworkPropertiesWatcherCallback& callback, |
415 const std::string& device_path) { | 516 const std::string& device_path) { |
416 if (g_libcros_network_functions_enabled) | 517 if (g_libcros_network_functions_enabled) |
417 return new CrosNetworkDevicePropertiesWatcher(callback, device_path); | 518 return new CrosNetworkDevicePropertiesWatcher(callback, device_path); |
418 else | 519 else |
419 return new NetworkDevicePropertiesWatcher(callback, device_path); | 520 return new NetworkDevicePropertiesWatcher(callback, device_path); |
420 } | 521 } |
421 | 522 |
422 CrosNetworkWatcher* CrosMonitorCellularDataPlan( | 523 CrosNetworkWatcher* CrosMonitorCellularDataPlan( |
423 const DataPlanUpdateWatcherCallback& callback) { | 524 const DataPlanUpdateWatcherCallback& callback) { |
424 return new CrosDataPlanUpdateWatcher(callback); | 525 if (g_libcros_network_functions_enabled) |
| 526 return new CrosDataPlanUpdateWatcher(callback); |
| 527 else |
| 528 return new DataPlanUpdateWatcher(callback); |
425 } | 529 } |
426 | 530 |
427 CrosNetworkWatcher* CrosMonitorSMS(const std::string& modem_device_path, | 531 CrosNetworkWatcher* CrosMonitorSMS(const std::string& modem_device_path, |
428 MonitorSMSCallback callback, | 532 MonitorSMSCallback callback, |
429 void* object) { | 533 void* object) { |
430 return new CrosSMSWatcher(modem_device_path, callback, object); | 534 return new CrosSMSWatcher(modem_device_path, callback, object); |
431 } | 535 } |
432 | 536 |
433 void CrosRequestNetworkServiceConnect(const std::string& service_path, | 537 void CrosRequestNetworkServiceConnect(const std::string& service_path, |
434 NetworkActionCallback callback, | 538 NetworkActionCallback callback, |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 ScopedGHashTable ghash( | 1024 ScopedGHashTable ghash( |
921 ConvertDictionaryValueToStringValueGHashTable(properties)); | 1025 ConvertDictionaryValueToStringValueGHashTable(properties)); |
922 chromeos::ConfigureService("", ghash.get(), OnConfigureService, NULL); | 1026 chromeos::ConfigureService("", ghash.get(), OnConfigureService, NULL); |
923 } else { | 1027 } else { |
924 DBusThreadManager::Get()->GetFlimflamManagerClient()->ConfigureService( | 1028 DBusThreadManager::Get()->GetFlimflamManagerClient()->ConfigureService( |
925 properties, base::Bind(&DoNothing)); | 1029 properties, base::Bind(&DoNothing)); |
926 } | 1030 } |
927 } | 1031 } |
928 | 1032 |
929 } // namespace chromeos | 1033 } // namespace chromeos |
OLD | NEW |