Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(814)

Unified Diff: chrome/browser/chromeos/cros/cros_network_functions.cc

Issue 10140016: Reimplement MonitorCellularDataPlan without Libcros (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: _ Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/chromeos/cros/cros_network_functions_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/cros/cros_network_functions.cc
diff --git a/chrome/browser/chromeos/cros/cros_network_functions.cc b/chrome/browser/chromeos/cros/cros_network_functions.cc
index c0b1b7682046ed4fffb18b3703a52ad6836f9b08..4ab9192cda5151bb8e85736ce010fc46e8e6b703 100644
--- a/chrome/browser/chromeos/cros/cros_network_functions.cc
+++ b/chrome/browser/chromeos/cros/cros_network_functions.cc
@@ -171,6 +171,107 @@ class CrosDataPlanUpdateWatcher : public CrosNetworkWatcher {
DataPlanUpdateMonitor monitor_;
};
+// Converts a string to a CellularDataPlanType.
+CellularDataPlanType ParseCellularDataPlanType(const std::string& type) {
+ if (type == cashew::kCellularDataPlanUnlimited)
+ return CELLULAR_DATA_PLAN_UNLIMITED;
+ if (type == cashew::kCellularDataPlanMeteredPaid)
+ return CELLULAR_DATA_PLAN_METERED_PAID;
+ if (type == cashew::kCellularDataPlanMeteredBase)
+ return CELLULAR_DATA_PLAN_METERED_BASE;
+ return CELLULAR_DATA_PLAN_UNKNOWN;
+}
+
+// Gets a string property from dictionary.
+bool GetStringProperty(const base::DictionaryValue& dictionary,
+ const std::string& key,
+ std::string* out) {
+ const bool result = dictionary.GetStringWithoutPathExpansion(key, out);
+ 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.
+ return result;
+}
+
+// Gets an int64 property from dictionary.
+bool GetInt64Property(const base::DictionaryValue& dictionary,
+ const std::string& key,
+ int64* out) {
+ // Int64 value is stored as a double because it cannot be fitted in int32.
+ double value_double = 0;
+ const bool result = dictionary.GetDoubleWithoutPathExpansion(key,
+ &value_double);
+ if (result)
+ *out = value_double;
+ else
+ LOG(ERROR) << "Cannnot get property " << key;
stevenjb 2012/04/25 01:37:50 WARNING
hashimoto 2012/04/25 15:40:17 Done.
+ return result;
+}
+
+// Gets a base::Time property from dictionary.
+bool GetTimeProperty(const base::DictionaryValue& dictionary,
+ const std::string& key,
+ base::Time* out) {
+ int64 value_int64 = 0;
+ if (!GetInt64Property(dictionary, key, &value_int64))
+ return false;
+ *out = base::Time::FromInternalValue(value_int64);
+ return true;
+}
+
+// Class to watch data plan update without Libcros.
+class DataPlanUpdateWatcher : public CrosNetworkWatcher {
+ public:
+ explicit DataPlanUpdateWatcher(const DataPlanUpdateWatcherCallback& callback)
+ : callback_(callback) {
+ DBusThreadManager::Get()->GetCashewClient()->SetDataPlansUpdateHandler(
+ base::Bind(&DataPlanUpdateWatcher::OnDataPlansUpdate,
+ base::Unretained(this)));
+ }
+ virtual ~DataPlanUpdateWatcher() {
+ DBusThreadManager::Get()->GetCashewClient()->ResetDataPlansUpdateHandler();
+ }
+
+ private:
+ void OnDataPlansUpdate(const std::string& service,
+ const base::ListValue& data_plans) {
+ CellularDataPlanVector* data_plan_vector = new CellularDataPlanVector;
+ for (size_t i = 0; i != data_plans.GetSize(); ++i) {
+ base::DictionaryValue* data_plan = NULL;
+ if (!data_plans.GetDictionary(i, &data_plan)) {
+ LOG(ERROR) << "data_plans[" << i << "] is not a dictionary.";
+ continue;
+ }
+ CellularDataPlan* plan = new CellularDataPlan;
+ // Plan name.
+ GetStringProperty(*data_plan, cashew::kCellularPlanNameProperty,
+ &plan->plan_name);
+ // Plan type.
+ std::string plan_type_string;
+ GetStringProperty(*data_plan, cashew::kCellularPlanTypeProperty,
+ &plan_type_string);
+ plan->plan_type = ParseCellularDataPlanType(plan_type_string);
+ // Update time.
+ GetTimeProperty(*data_plan, cashew::kCellularPlanUpdateTimeProperty,
+ &plan->update_time);
+ // Start time.
+ GetTimeProperty(*data_plan, cashew::kCellularPlanStartProperty,
+ &plan->plan_start_time);
+ // End time.
+ GetTimeProperty(*data_plan, cashew::kCellularPlanEndProperty,
+ &plan->plan_end_time);
+ // Data bytes.
+ GetInt64Property(*data_plan, cashew::kCellularPlanDataBytesProperty,
+ &plan->plan_data_bytes);
+ // Bytes used.
+ GetInt64Property(*data_plan, cashew::kCellularDataBytesUsedProperty,
+ &plan->data_bytes_used);
+ data_plan_vector->push_back(plan);
+ }
+ callback_.Run(service, data_plan_vector);
+ }
+
+ DataPlanUpdateWatcherCallback callback_;
+};
+
// Class to watch sms with Libcros.
class CrosSMSWatcher : public CrosNetworkWatcher {
public:
@@ -399,7 +500,10 @@ CrosNetworkWatcher* CrosMonitorNetworkDeviceProperties(
CrosNetworkWatcher* CrosMonitorCellularDataPlan(
const DataPlanUpdateWatcherCallback& callback) {
- return new CrosDataPlanUpdateWatcher(callback);
+ if (g_libcros_network_functions_enabled)
+ return new CrosDataPlanUpdateWatcher(callback);
+ else
+ return new DataPlanUpdateWatcher(callback);
}
CrosNetworkWatcher* CrosMonitorSMS(const std::string& modem_device_path,
« no previous file with comments | « no previous file | chrome/browser/chromeos/cros/cros_network_functions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698