| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/network_message_observer.h" | 5 #include "chrome/browser/chromeos/network_message_observer.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); | 58 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); |
| 59 netlib->RemoveNetworkManagerObserver(this); | 59 netlib->RemoveNetworkManagerObserver(this); |
| 60 netlib->RemoveCellularDataPlanObserver(this); | 60 netlib->RemoveCellularDataPlanObserver(this); |
| 61 notification_connection_error_.Hide(); | 61 notification_connection_error_.Hide(); |
| 62 notification_low_data_.Hide(); | 62 notification_low_data_.Hide(); |
| 63 notification_no_data_.Hide(); | 63 notification_no_data_.Hide(); |
| 64 STLDeleteValues(&cellular_networks_); | 64 STLDeleteValues(&cellular_networks_); |
| 65 STLDeleteValues(&wifi_networks_); | 65 STLDeleteValues(&wifi_networks_); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // static |
| 69 bool NetworkMessageObserver::IsApplicableBackupPlan( |
| 70 const CellularDataPlan* plan, const CellularDataPlan* other_plan) { |
| 71 // By applicable plan, we mean that the other plan has data AND the timeframe |
| 72 // will apply: (unlimited OR used bytes < max bytes) AND |
| 73 // ((start time - 1 sec) <= end time of currently active plan). |
| 74 // In other words, there is data available and there is no gap of more than a |
| 75 // second in time between the old plan and the new plan. |
| 76 bool has_data = other_plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED || |
| 77 other_plan->remaining_data() > 0; |
| 78 bool will_apply = |
| 79 (other_plan->plan_start_time - plan->plan_end_time).InSeconds() <= 1; |
| 80 return has_data && will_apply; |
| 81 } |
| 82 |
| 68 void NetworkMessageObserver::OpenMobileSetupPage(const ListValue* args) { | 83 void NetworkMessageObserver::OpenMobileSetupPage(const ListValue* args) { |
| 69 Browser* browser = BrowserList::GetLastActive(); | 84 Browser* browser = BrowserList::GetLastActive(); |
| 70 if (browser) | 85 if (browser) |
| 71 browser->OpenMobilePlanTabAndActivate(); | 86 browser->OpenMobilePlanTabAndActivate(); |
| 72 } | 87 } |
| 73 | 88 |
| 74 void NetworkMessageObserver::OpenMoreInfoPage(const ListValue* args) { | 89 void NetworkMessageObserver::OpenMoreInfoPage(const ListValue* args) { |
| 75 Browser* browser = BrowserList::GetLastActive(); | 90 Browser* browser = BrowserList::GetLastActive(); |
| 76 if (!browser) | 91 if (!browser) |
| 77 return; | 92 return; |
| 78 chromeos::NetworkLibrary* lib = | 93 chromeos::NetworkLibrary* lib = |
| 79 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); | 94 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); |
| 80 const chromeos::CellularNetwork* cellular = lib->cellular_network(); | 95 const chromeos::CellularNetwork* cellular = lib->cellular_network(); |
| 81 if (!cellular) | 96 if (!cellular) |
| 82 return; | 97 return; |
| 83 browser->ShowSingletonTab(GURL(cellular->payment_url()), false); | 98 browser->ShowSingletonTab(GURL(cellular->payment_url()), false); |
| 84 } | 99 } |
| 85 | 100 |
| 101 void NetworkMessageObserver::HideDataNotifications() { |
| 102 notification_low_data_.Hide(); |
| 103 notification_no_data_.Hide(); |
| 104 } |
| 105 |
| 106 void NetworkMessageObserver::InitNewPlan(const CellularDataPlan* plan) { |
| 107 HideDataNotifications(); |
| 108 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { |
| 109 notification_no_data_.set_title( |
| 110 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE, |
| 111 ASCIIToUTF16(plan->plan_name))); |
| 112 notification_low_data_.set_title( |
| 113 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE, |
| 114 ASCIIToUTF16(plan->plan_name))); |
| 115 } else { |
| 116 notification_no_data_.set_title( |
| 117 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE, |
| 118 ASCIIToUTF16(plan->plan_name))); |
| 119 notification_low_data_.set_title( |
| 120 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE, |
| 121 ASCIIToUTF16(plan->plan_name))); |
| 122 } |
| 123 } |
| 124 |
| 125 void NetworkMessageObserver::ShowNoDataNotification( |
| 126 const CellularDataPlan* plan) { |
| 127 notification_low_data_.Hide(); |
| 128 string16 message = |
| 129 plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? |
| 130 TimeFormat::TimeRemaining(base::TimeDelta()) : |
| 131 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, |
| 132 ASCIIToUTF16("0")); |
| 133 notification_no_data_.Show(message, |
| 134 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), |
| 135 NewCallback(this, &NetworkMessageObserver::OpenMobileSetupPage), |
| 136 false, false); |
| 137 } |
| 138 |
| 139 void NetworkMessageObserver::ShowLowDataNotification( |
| 140 const CellularDataPlan* plan) { |
| 141 notification_no_data_.Hide(); |
| 142 string16 message = |
| 143 plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? |
| 144 plan->GetPlanExpiration() : |
| 145 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, |
| 146 UTF8ToUTF16(base::Int64ToString(plan->remaining_mbytes()))); |
| 147 notification_low_data_.Show(message, |
| 148 l10n_util::GetStringUTF16(IDS_NETWORK_MORE_INFO_MESSAGE), |
| 149 NewCallback(this, &NetworkMessageObserver::OpenMoreInfoPage), |
| 150 false, false); |
| 151 } |
| 152 |
| 86 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { | 153 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { |
| 87 const WifiNetworkVector& wifi_networks = obj->wifi_networks(); | 154 const WifiNetworkVector& wifi_networks = obj->wifi_networks(); |
| 88 const CellularNetworkVector& cellular_networks = obj->cellular_networks(); | 155 const CellularNetworkVector& cellular_networks = obj->cellular_networks(); |
| 89 | 156 |
| 90 std::string new_failed_network; | 157 std::string new_failed_network; |
| 91 // Check to see if we have any newly failed wifi network. | 158 // Check to see if we have any newly failed wifi network. |
| 92 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); | 159 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); |
| 93 it < wifi_networks.end(); it++) { | 160 it < wifi_networks.end(); it++) { |
| 94 const WifiNetwork* wifi = *it; | 161 const WifiNetwork* wifi = *it; |
| 95 if (wifi->failed()) { | 162 if (wifi->failed()) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 // Hide if already shown to force show it in case user has closed it. | 209 // Hide if already shown to force show it in case user has closed it. |
| 143 if (notification_connection_error_.visible()) | 210 if (notification_connection_error_.visible()) |
| 144 notification_connection_error_.Hide(); | 211 notification_connection_error_.Hide(); |
| 145 notification_connection_error_.Show(l10n_util::GetStringFUTF16( | 212 notification_connection_error_.Show(l10n_util::GetStringFUTF16( |
| 146 IDS_NETWORK_CONNECTION_ERROR_MESSAGE, | 213 IDS_NETWORK_CONNECTION_ERROR_MESSAGE, |
| 147 ASCIIToUTF16(new_failed_network)), false, false); | 214 ASCIIToUTF16(new_failed_network)), false, false); |
| 148 } | 215 } |
| 149 } | 216 } |
| 150 | 217 |
| 151 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* obj) { | 218 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* obj) { |
| 219 if (!ShouldShowMobilePlanNotifications()) { |
| 220 HideDataNotifications(); |
| 221 return; |
| 222 } |
| 223 |
| 152 const CellularNetwork* cellular = obj->cellular_network(); | 224 const CellularNetwork* cellular = obj->cellular_network(); |
| 153 if (!cellular) | 225 if (!cellular) |
| 154 return; | 226 return; |
| 155 const CellularDataPlan* plan = cellular->GetSignificantDataPlan(); | |
| 156 std::string new_plan_name = plan ? plan->plan_name : ""; | |
| 157 CellularDataPlanType new_plan_type = plan ? plan->plan_type : | |
| 158 CELLULAR_DATA_PLAN_UNKNOWN; | |
| 159 | 227 |
| 160 // If connected cellular network changed, or data plan is different, then | 228 // If no plans available, check to see if we need a new plan. |
| 161 // it's a new network. Then hide all previous notifications. | 229 if (cellular->GetDataPlans().size() == 0) { |
| 162 bool new_plan = false; | 230 HideDataNotifications(); |
| 163 if (cellular->service_path() != cellular_service_path_) { | 231 if (cellular->needs_new_plan()) { |
| 164 cellular_service_path_ = cellular->service_path(); | |
| 165 new_plan = true; | |
| 166 } else if (new_plan_name != cellular_data_plan_name_ || | |
| 167 new_plan_type != cellular_data_plan_type_) { | |
| 168 new_plan = true; | |
| 169 } | |
| 170 | |
| 171 cellular_data_plan_name_ = new_plan_name; | |
| 172 cellular_data_plan_type_ = new_plan_type; | |
| 173 | |
| 174 bool should_show_notifications = ShouldShowMobilePlanNotifications(); | |
| 175 if (!should_show_notifications) { | |
| 176 notification_low_data_.Hide(); | |
| 177 notification_no_data_.Hide(); | |
| 178 return; | |
| 179 } | |
| 180 | |
| 181 if (new_plan) { | |
| 182 notification_low_data_.Hide(); | |
| 183 notification_no_data_.Hide(); | |
| 184 if (!plan && cellular->needs_new_plan()) { | |
| 185 notification_no_data_.set_title( | 232 notification_no_data_.set_title( |
| 186 l10n_util::GetStringFUTF16(IDS_NETWORK_NO_DATA_PLAN_TITLE, | 233 l10n_util::GetStringFUTF16(IDS_NETWORK_NO_DATA_PLAN_TITLE, |
| 187 UTF8ToUTF16(cellular->service_name()))); | 234 UTF8ToUTF16(cellular->service_name()))); |
| 188 notification_no_data_.Show( | 235 notification_no_data_.Show( |
| 189 l10n_util::GetStringFUTF16( | 236 l10n_util::GetStringFUTF16( |
| 190 IDS_NETWORK_NO_DATA_PLAN_MESSAGE, | 237 IDS_NETWORK_NO_DATA_PLAN_MESSAGE, |
| 191 UTF8ToUTF16(cellular->service_name())), | 238 UTF8ToUTF16(cellular->service_name())), |
| 192 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), | 239 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), |
| 193 NewCallback(this, &NetworkMessageObserver::OpenMobileSetupPage), | 240 NewCallback(this, &NetworkMessageObserver::OpenMobileSetupPage), |
| 194 false, false); | 241 false, false); |
| 242 } |
| 243 return; |
| 244 } |
| 245 |
| 246 const CellularDataPlanVector& plans = cellular->GetDataPlans(); |
| 247 CellularDataPlanVector::const_iterator iter = plans.begin(); |
| 248 const CellularDataPlan* current_plan = *iter; |
| 249 |
| 250 // If current plan is not the last plan (there is another backup plan), |
| 251 // then we do not show notifications for this plan. |
| 252 // For example, if there is another data plan available when this runs out. |
| 253 for (++iter; iter != plans.end(); ++iter) { |
| 254 if (IsApplicableBackupPlan(current_plan, *iter)) { |
| 255 HideDataNotifications(); |
| 195 return; | 256 return; |
| 196 } else if (cellular_data_plan_type_ == CELLULAR_DATA_PLAN_UNLIMITED) { | |
| 197 notification_no_data_.set_title( | |
| 198 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE, | |
| 199 ASCIIToUTF16(cellular_data_plan_name_))); | |
| 200 notification_low_data_.set_title( | |
| 201 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE, | |
| 202 ASCIIToUTF16(cellular_data_plan_name_))); | |
| 203 } else { | |
| 204 notification_no_data_.set_title( | |
| 205 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE, | |
| 206 ASCIIToUTF16(cellular_data_plan_name_))); | |
| 207 notification_low_data_.set_title( | |
| 208 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE, | |
| 209 ASCIIToUTF16(cellular_data_plan_name_))); | |
| 210 } | 257 } |
| 211 } | 258 } |
| 212 | 259 |
| 213 if (cellular_data_plan_type_ == CELLULAR_DATA_PLAN_UNKNOWN) | 260 // If connected cellular network changed, or data plan is different, then |
| 214 return; | 261 // it's a new network. Then hide all previous notifications. |
| 262 bool new_plan = cellular->service_path() != cellular_service_path_ || |
| 263 current_plan->GetUniqueIdentifier() != cellular_data_plan_unique_id_; |
| 264 |
| 265 if (new_plan) { |
| 266 InitNewPlan(current_plan); |
| 267 } |
| 215 | 268 |
| 216 if (cellular->GetDataLeft() == CellularNetwork::DATA_NONE) { | 269 if (cellular->GetDataLeft() == CellularNetwork::DATA_NONE) { |
| 217 notification_low_data_.Hide(); | 270 ShowNoDataNotification(current_plan); |
| 218 string16 message = | |
| 219 cellular_data_plan_type_ == CELLULAR_DATA_PLAN_UNLIMITED ? | |
| 220 TimeFormat::TimeRemaining(base::TimeDelta()) : | |
| 221 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, | |
| 222 ASCIIToUTF16("0")); | |
| 223 notification_no_data_.Show(message, | |
| 224 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), | |
| 225 NewCallback(this, &NetworkMessageObserver::OpenMobileSetupPage), | |
| 226 false, false); | |
| 227 } else if (cellular->GetDataLeft() == CellularNetwork::DATA_VERY_LOW) { | 271 } else if (cellular->GetDataLeft() == CellularNetwork::DATA_VERY_LOW) { |
| 228 notification_no_data_.Hide(); | 272 ShowLowDataNotification(current_plan); |
| 229 string16 message = | |
| 230 cellular_data_plan_type_ == CELLULAR_DATA_PLAN_UNLIMITED ? | |
| 231 plan->GetPlanExpiration() : | |
| 232 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, | |
| 233 UTF8ToUTF16(base::Int64ToString(plan->remaining_mbytes()))); | |
| 234 notification_low_data_.Show(message, | |
| 235 l10n_util::GetStringUTF16(IDS_NETWORK_MORE_INFO_MESSAGE), | |
| 236 NewCallback(this, &NetworkMessageObserver::OpenMoreInfoPage), | |
| 237 false, false); | |
| 238 } else { | 273 } else { |
| 239 // Got data, so hide notifications. | 274 // Got data, so hide notifications. |
| 240 notification_low_data_.Hide(); | 275 HideDataNotifications(); |
| 241 notification_no_data_.Hide(); | |
| 242 } | 276 } |
| 277 |
| 278 cellular_service_path_ = cellular->service_path(); |
| 279 cellular_data_plan_unique_id_ = current_plan->GetUniqueIdentifier(); |
| 243 } | 280 } |
| 244 | 281 |
| 245 } // namespace chromeos | 282 } // namespace chromeos |
| OLD | NEW |