| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/stl_util-inl.h" |
| 9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser.h" | 12 #include "chrome/browser/browser.h" |
| 12 #include "chrome/browser/browser_list.h" | 13 #include "chrome/browser/browser_list.h" |
| 13 #include "chrome/browser/browser_window.h" | 14 #include "chrome/browser/browser_window.h" |
| 14 #include "chrome/browser/chromeos/cros/cros_library.h" | 15 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 15 #include "chrome/browser/chromeos/cros/network_library.h" | 16 #include "chrome/browser/chromeos/cros/network_library.h" |
| 16 #include "chrome/browser/chromeos/notifications/balloon_view_host.h" | 17 #include "chrome/browser/chromeos/notifications/balloon_view_host.h" |
| 17 #include "chrome/browser/chromeos/options/network_config_view.h" | 18 #include "chrome/browser/chromeos/options/network_config_view.h" |
| 18 #include "chrome/browser/views/window.h" | 19 #include "chrome/browser/views/window.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 35 IDR_NOTIFICATION_BARS_EMPTY, | 36 IDR_NOTIFICATION_BARS_EMPTY, |
| 36 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { | 37 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { |
| 37 NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); | 38 NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); |
| 38 initialized_ = true; | 39 initialized_ = true; |
| 39 } | 40 } |
| 40 | 41 |
| 41 NetworkMessageObserver::~NetworkMessageObserver() { | 42 NetworkMessageObserver::~NetworkMessageObserver() { |
| 42 notification_connection_error_.Hide(); | 43 notification_connection_error_.Hide(); |
| 43 notification_low_data_.Hide(); | 44 notification_low_data_.Hide(); |
| 44 notification_no_data_.Hide(); | 45 notification_no_data_.Hide(); |
| 46 STLDeleteValues(&cellular_networks_); |
| 47 STLDeleteValues(&wifi_networks_); |
| 45 } | 48 } |
| 46 | 49 |
| 47 void NetworkMessageObserver::CreateModalPopup(views::WindowDelegate* view) { | 50 void NetworkMessageObserver::CreateModalPopup(views::WindowDelegate* view) { |
| 48 Browser* browser = BrowserList::GetLastActive(); | 51 Browser* browser = BrowserList::GetLastActive(); |
| 49 if (browser && browser->type() != Browser::TYPE_NORMAL) { | 52 if (browser && browser->type() != Browser::TYPE_NORMAL) { |
| 50 browser = BrowserList::FindBrowserWithType(browser->profile(), | 53 browser = BrowserList::FindBrowserWithType(browser->profile(), |
| 51 Browser::TYPE_NORMAL, | 54 Browser::TYPE_NORMAL, |
| 52 true); | 55 true); |
| 53 } | 56 } |
| 54 DCHECK(browser); | 57 DCHECK(browser); |
| 55 views::Window* window = browser::CreateViewsWindow( | 58 views::Window* window = browser::CreateViewsWindow( |
| 56 browser->window()->GetNativeHandle(), gfx::Rect(), view); | 59 browser->window()->GetNativeHandle(), gfx::Rect(), view); |
| 57 window->SetIsAlwaysOnTop(true); | 60 window->SetIsAlwaysOnTop(true); |
| 58 window->Show(); | 61 window->Show(); |
| 59 } | 62 } |
| 60 | 63 |
| 61 void NetworkMessageObserver::MobileSetup(const ListValue* args) { | 64 void NetworkMessageObserver::MobileSetup(const ListValue* args) { |
| 62 BrowserList::GetLastActive()->OpenMobilePlanTabAndActivate(); | 65 BrowserList::GetLastActive()->OpenMobilePlanTabAndActivate(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void NetworkMessageObserver::NetworkChanged(NetworkLibrary* obj) { | 68 void NetworkMessageObserver::NetworkChanged(NetworkLibrary* obj) { |
| 66 const WifiNetworkVector& wifi_networks = obj->wifi_networks(); | 69 const WifiNetworkVector& wifi_networks = obj->wifi_networks(); |
| 67 const CellularNetworkVector& cellular_networks = obj->cellular_networks(); | 70 const CellularNetworkVector& cellular_networks = obj->cellular_networks(); |
| 68 | 71 |
| 69 NetworkConfigView* view = NULL; | 72 NetworkConfigView* view = NULL; |
| 70 std::string new_failed_network; | 73 std::string new_failed_network; |
| 71 // Check to see if we have any newly failed wifi network. | 74 // Check to see if we have any newly failed wifi network. |
| 72 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); | 75 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); |
| 73 it < wifi_networks.end(); it++) { | 76 it < wifi_networks.end(); it++) { |
| 74 const WifiNetwork& wifi = *it; | 77 const WifiNetwork* wifi = *it; |
| 75 if (wifi.failed()) { | 78 if (wifi->failed()) { |
| 76 ServicePathWifiMap::iterator iter = | 79 ServicePathWifiMap::iterator iter = |
| 77 wifi_networks_.find(wifi.service_path()); | 80 wifi_networks_.find(wifi->service_path()); |
| 78 // If the network did not previously exist, then don't do anything. | 81 // If the network did not previously exist, then don't do anything. |
| 79 // For example, if the user travels to a location and finds a service | 82 // For example, if the user travels to a location and finds a service |
| 80 // that has previously failed, we don't want to show a notification. | 83 // that has previously failed, we don't want to show a notification. |
| 81 if (iter == wifi_networks_.end()) | 84 if (iter == wifi_networks_.end()) |
| 82 continue; | 85 continue; |
| 83 | 86 |
| 84 const WifiNetwork& wifi_old = iter->second; | 87 const WifiNetwork* wifi_old = iter->second; |
| 85 // If this network was in a failed state previously, then it's not new. | 88 // If this network was in a failed state previously, then it's not new. |
| 86 if (wifi_old.failed()) | 89 if (wifi_old->failed()) |
| 87 continue; | 90 continue; |
| 88 | 91 |
| 89 // Display login box again for bad_passphrase and bad_wepkey errors. | 92 // Display login box again for bad_passphrase and bad_wepkey errors. |
| 90 if (wifi.error() == ERROR_BAD_PASSPHRASE || | 93 if (wifi->error() == ERROR_BAD_PASSPHRASE || |
| 91 wifi.error() == ERROR_BAD_WEPKEY) { | 94 wifi->error() == ERROR_BAD_WEPKEY) { |
| 92 // The NetworkConfigView will show the appropriate error message. | 95 // The NetworkConfigView will show the appropriate error message. |
| 93 view = new NetworkConfigView(wifi, true); | 96 view = new NetworkConfigView(wifi, true); |
| 94 // There should only be one wifi network that failed to connect. | 97 // There should only be one wifi network that failed to connect. |
| 95 // If for some reason, we have more than one failure, | 98 // If for some reason, we have more than one failure, |
| 96 // we only display the first one. So we break here. | 99 // we only display the first one. So we break here. |
| 97 break; | 100 break; |
| 98 } | 101 } |
| 99 | 102 |
| 100 // If network connection failed, display a notification. | 103 // If network connection failed, display a notification. |
| 101 // We only do this if we were trying to make a new connection. | 104 // We only do this if we were trying to make a new connection. |
| 102 // So if a previously connected network got disconnected for any reason, | 105 // So if a previously connected network got disconnected for any reason, |
| 103 // we don't display notification. | 106 // we don't display notification. |
| 104 if (wifi_old.connecting()) { | 107 if (wifi_old->connecting()) { |
| 105 new_failed_network = wifi.name(); | 108 new_failed_network = wifi->name(); |
| 106 // Like above, there should only be one newly failed network. | 109 // Like above, there should only be one newly failed network. |
| 107 break; | 110 break; |
| 108 } | 111 } |
| 109 } | 112 } |
| 110 | 113 |
| 111 // If we find any network connecting, we hide the error notification. | 114 // If we find any network connecting, we hide the error notification. |
| 112 if (wifi.connecting()) { | 115 if (wifi->connecting()) { |
| 113 notification_connection_error_.Hide(); | 116 notification_connection_error_.Hide(); |
| 114 } | 117 } |
| 115 } | 118 } |
| 116 | 119 |
| 117 // Refresh stored networks. | 120 // Refresh stored networks. |
| 121 STLDeleteValues(&wifi_networks_); |
| 118 wifi_networks_.clear(); | 122 wifi_networks_.clear(); |
| 119 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); | 123 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); |
| 120 it < wifi_networks.end(); it++) { | 124 it < wifi_networks.end(); it++) { |
| 121 const WifiNetwork& wifi = *it; | 125 const WifiNetwork* wifi = *it; |
| 122 wifi_networks_[wifi.service_path()] = wifi; | 126 wifi_networks_[wifi->service_path()] = new WifiNetwork(*wifi); |
| 123 } | 127 } |
| 128 |
| 129 STLDeleteValues(&cellular_networks_); |
| 124 cellular_networks_.clear(); | 130 cellular_networks_.clear(); |
| 125 for (CellularNetworkVector::const_iterator it = cellular_networks.begin(); | 131 for (CellularNetworkVector::const_iterator it = cellular_networks.begin(); |
| 126 it < cellular_networks.end(); it++) { | 132 it < cellular_networks.end(); it++) { |
| 127 const CellularNetwork& cellular = *it; | 133 const CellularNetwork* cellular = *it; |
| 128 cellular_networks_[cellular.service_path()] = cellular; | 134 cellular_networks_[cellular->service_path()] = |
| 135 new CellularNetwork(*cellular); |
| 129 } | 136 } |
| 130 | 137 |
| 131 // Show connection error notification if necessary. | 138 // Show connection error notification if necessary. |
| 132 if (!new_failed_network.empty()) { | 139 if (!new_failed_network.empty()) { |
| 133 // Hide if already shown to force show it in case user has closed it. | 140 // Hide if already shown to force show it in case user has closed it. |
| 134 if (notification_connection_error_.visible()) | 141 if (notification_connection_error_.visible()) |
| 135 notification_connection_error_.Hide(); | 142 notification_connection_error_.Hide(); |
| 136 notification_connection_error_.Show(l10n_util::GetStringFUTF16( | 143 notification_connection_error_.Show(l10n_util::GetStringFUTF16( |
| 137 IDS_NETWORK_CONNECTION_ERROR_MESSAGE, | 144 IDS_NETWORK_CONNECTION_ERROR_MESSAGE, |
| 138 ASCIIToUTF16(new_failed_network)), false, false); | 145 ASCIIToUTF16(new_failed_network)), false, false); |
| 139 } | 146 } |
| 140 | 147 |
| 141 // Show login box if necessary. | 148 // Show login box if necessary. |
| 142 if (view && initialized_) | 149 if (view && initialized_) |
| 143 CreateModalPopup(view); | 150 CreateModalPopup(view); |
| 144 } | 151 } |
| 145 | 152 |
| 146 void NetworkMessageObserver::CellularDataPlanChanged(NetworkLibrary* obj) { | 153 void NetworkMessageObserver::CellularDataPlanChanged(NetworkLibrary* obj) { |
| 147 const CellularNetwork& cellular = obj->cellular_network(); | 154 const CellularNetwork* cellular = obj->cellular_network(); |
| 155 if (!cellular) |
| 156 return; |
| 148 // Active plan is the first one in the list. Use empty one if none found. | 157 // Active plan is the first one in the list. Use empty one if none found. |
| 149 const CellularDataPlanList& plans = cellular.GetDataPlans(); | 158 const CellularDataPlanList& plans = cellular->GetDataPlans(); |
| 150 CellularDataPlan plan = plans.empty() ? CellularDataPlan() : plans[0]; | 159 CellularDataPlan plan = plans.empty() ? CellularDataPlan() : plans[0]; |
| 151 // If connected cellular network changed, or data plan is different, then | 160 // If connected cellular network changed, or data plan is different, then |
| 152 // it's a new network. Then hide all previous notifications. | 161 // it's a new network. Then hide all previous notifications. |
| 153 bool new_plan = false; | 162 bool new_plan = false; |
| 154 if (cellular.service_path() != cellular_service_path_) { | 163 if (cellular->service_path() != cellular_service_path_) { |
| 155 cellular_service_path_ = cellular.service_path(); | 164 cellular_service_path_ = cellular->service_path(); |
| 156 new_plan = true; | 165 new_plan = true; |
| 157 } else if (plan.plan_name != cellular_data_plan_.plan_name || | 166 } else if (plan.plan_name != cellular_data_plan_.plan_name || |
| 158 plan.plan_type != cellular_data_plan_.plan_type) { | 167 plan.plan_type != cellular_data_plan_.plan_type) { |
| 159 new_plan = true; | 168 new_plan = true; |
| 160 } | 169 } |
| 161 if (new_plan) { | 170 if (new_plan) { |
| 162 // New network, so hide the notifications and set the notifications title. | 171 // New network, so hide the notifications and set the notifications title. |
| 163 notification_low_data_.Hide(); | 172 notification_low_data_.Hide(); |
| 164 notification_no_data_.Hide(); | 173 notification_no_data_.Hide(); |
| 165 if (plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { | 174 if (plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { |
| 166 notification_no_data_.set_title( | 175 notification_no_data_.set_title( |
| 167 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE, | 176 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE, |
| 168 ASCIIToUTF16(plan.plan_name))); | 177 ASCIIToUTF16(plan.plan_name))); |
| 169 notification_low_data_.set_title( | 178 notification_low_data_.set_title( |
| 170 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE, | 179 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE, |
| 171 ASCIIToUTF16(plan.plan_name))); | 180 ASCIIToUTF16(plan.plan_name))); |
| 172 } else { | 181 } else { |
| 173 notification_no_data_.set_title( | 182 notification_no_data_.set_title( |
| 174 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE, | 183 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE, |
| 175 ASCIIToUTF16(plan.plan_name))); | 184 ASCIIToUTF16(plan.plan_name))); |
| 176 notification_low_data_.set_title( | 185 notification_low_data_.set_title( |
| 177 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE, | 186 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE, |
| 178 ASCIIToUTF16(plan.plan_name))); | 187 ASCIIToUTF16(plan.plan_name))); |
| 179 } | 188 } |
| 180 } | 189 } |
| 181 | 190 |
| 182 if (plan.plan_type != CELLULAR_DATA_PLAN_UNKNOWN) { | 191 if (plan.plan_type != CELLULAR_DATA_PLAN_UNKNOWN) { |
| 183 if (cellular.data_left() == CellularNetwork::DATA_NONE) { | 192 if (cellular->data_left() == CellularNetwork::DATA_NONE) { |
| 184 notification_low_data_.Hide(); | 193 notification_low_data_.Hide(); |
| 185 int message = plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? | 194 int message = plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? |
| 186 IDS_NETWORK_MINUTES_REMAINING_MESSAGE : | 195 IDS_NETWORK_MINUTES_REMAINING_MESSAGE : |
| 187 IDS_NETWORK_DATA_REMAINING_MESSAGE; | 196 IDS_NETWORK_DATA_REMAINING_MESSAGE; |
| 188 notification_no_data_.Show(l10n_util::GetStringFUTF16( | 197 notification_no_data_.Show(l10n_util::GetStringFUTF16( |
| 189 message, ASCIIToUTF16("0")), | 198 message, ASCIIToUTF16("0")), |
| 190 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), | 199 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), |
| 191 NewCallback(this, &NetworkMessageObserver::MobileSetup), | 200 NewCallback(this, &NetworkMessageObserver::MobileSetup), |
| 192 false, false); | 201 false, false); |
| 193 } else if (cellular.data_left() == CellularNetwork::DATA_VERY_LOW) { | 202 } else if (cellular->data_left() == CellularNetwork::DATA_VERY_LOW) { |
| 194 notification_no_data_.Hide(); | 203 notification_no_data_.Hide(); |
| 195 int message = plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? | 204 int message = plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? |
| 196 IDS_NETWORK_MINUTES_REMAINING_MESSAGE : | 205 IDS_NETWORK_MINUTES_REMAINING_MESSAGE : |
| 197 IDS_NETWORK_DATA_REMAINING_MESSAGE; | 206 IDS_NETWORK_DATA_REMAINING_MESSAGE; |
| 198 int64 remaining = plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? | 207 int64 remaining = plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? |
| 199 (plan.plan_end_time - plan.update_time) / 60 : | 208 (plan.plan_end_time - plan.update_time) / 60 : |
| 200 (plan.plan_data_bytes - plan.data_bytes_used) / (1024 * 1024); | 209 (plan.plan_data_bytes - plan.data_bytes_used) / (1024 * 1024); |
| 201 notification_low_data_.Show(l10n_util::GetStringFUTF16( | 210 notification_low_data_.Show(l10n_util::GetStringFUTF16( |
| 202 message, UTF8ToUTF16(base::Int64ToString(remaining))), | 211 message, UTF8ToUTF16(base::Int64ToString(remaining))), |
| 203 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), | 212 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), |
| 204 NewCallback(this, &NetworkMessageObserver::MobileSetup), | 213 NewCallback(this, &NetworkMessageObserver::MobileSetup), |
| 205 false, false); | 214 false, false); |
| 206 } else { | 215 } else { |
| 207 // Got data, so hide notifications. | 216 // Got data, so hide notifications. |
| 208 notification_low_data_.Hide(); | 217 notification_low_data_.Hide(); |
| 209 notification_no_data_.Hide(); | 218 notification_no_data_.Hide(); |
| 210 } | 219 } |
| 211 } | 220 } |
| 212 | 221 |
| 213 cellular_data_plan_ = plan; | 222 cellular_data_plan_ = plan; |
| 214 } | 223 } |
| 215 | 224 |
| 216 } // namespace chromeos | 225 } // namespace chromeos |
| OLD | NEW |