| 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/login/network_screen.h" | 5 #include "chrome/browser/chromeos/login/network_screen.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/string16.h" | 8 #include "base/string16.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // Time in seconds for connection timeout. | 21 // Time in seconds for connection timeout. |
| 22 const int kConnectionTimeoutSec = 15; | 22 const int kConnectionTimeoutSec = 15; |
| 23 | 23 |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 namespace chromeos { | 26 namespace chromeos { |
| 27 | 27 |
| 28 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
| 29 // NetworkScreen, public: | 29 // NetworkScreen, public: |
| 30 | 30 |
| 31 NetworkScreen::NetworkScreen(WizardScreenDelegate* delegate, bool is_out_of_box) | 31 NetworkScreen::NetworkScreen(WizardScreenDelegate* delegate) |
| 32 : ViewScreen<NetworkSelectionView>(delegate), | 32 : ViewScreen<NetworkSelectionView>(delegate), |
| 33 is_network_subscribed_(false), | 33 is_network_subscribed_(false), |
| 34 wifi_disabled_(false), | 34 continue_pressed_(false) { |
| 35 is_out_of_box_(is_out_of_box), | |
| 36 is_waiting_for_connect_(false), | |
| 37 continue_pressed_(false), | |
| 38 ethernet_preselected_(false), | |
| 39 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | |
| 40 } | 35 } |
| 41 | 36 |
| 42 NetworkScreen::~NetworkScreen() { | 37 NetworkScreen::~NetworkScreen() { |
| 43 connection_timer_.Stop(); | 38 connection_timer_.Stop(); |
| 44 UnsubscribeNetworkNotification(); | 39 UnsubscribeNetworkNotification(); |
| 45 } | 40 } |
| 46 | 41 |
| 47 //////////////////////////////////////////////////////////////////////////////// | |
| 48 // ComboboxModel implementation: | |
| 49 | |
| 50 int NetworkScreen::GetItemCount() { | |
| 51 // Item with index = 0 is either "no networks are available" or | |
| 52 // "no selection". | |
| 53 // If WiFi is disabled adding extra item to enable it. | |
| 54 return static_cast<int>(networks_.GetNetworkCount()) + 1 + | |
| 55 (wifi_disabled_ ? 1 : 0); | |
| 56 } | |
| 57 | |
| 58 string16 NetworkScreen::GetItemAt(int index) { | |
| 59 if (index == 0) { | |
| 60 return networks_.IsEmpty() ? | |
| 61 l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE) : | |
| 62 l10n_util::GetStringUTF16(IDS_NETWORK_SELECTION_NONE); | |
| 63 } | |
| 64 if (wifi_disabled_ && | |
| 65 index == static_cast<int>(networks_.GetNetworkCount()) + 1) { | |
| 66 return l10n_util::GetStringFUTF16( | |
| 67 IDS_STATUSBAR_NETWORK_DEVICE_ENABLE, | |
| 68 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_WIFI)); | |
| 69 } | |
| 70 NetworkList::NetworkItem* network = | |
| 71 networks_.GetNetworkAt(index - 1); | |
| 72 return network ? network->label : string16(); | |
| 73 } | |
| 74 | |
| 75 //////////////////////////////////////////////////////////////////////////////// | |
| 76 // views::Combobox::Listener implementation: | |
| 77 | |
| 78 void NetworkScreen::ItemChanged(views::Combobox* sender, | |
| 79 int prev_index, | |
| 80 int new_index) { | |
| 81 view()->EnableContinue(new_index > 0); | |
| 82 // Corner case: item with index 0 is "No selection". Just select it. | |
| 83 if (new_index == prev_index || new_index <= 0 || prev_index < 0) | |
| 84 return; | |
| 85 | |
| 86 if (wifi_disabled_ && | |
| 87 new_index == static_cast<int>(networks_.GetNetworkCount()) + 1) { | |
| 88 view()->EnableContinue(false); | |
| 89 MessageLoop::current()->PostTask(FROM_HERE, | |
| 90 task_factory_.NewRunnableMethod(&NetworkScreen::EnableWiFi)); | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 if (networks_.IsEmpty()) | |
| 95 return; | |
| 96 | |
| 97 // Connect to network as early as possible. | |
| 98 const NetworkList::NetworkItem* network = | |
| 99 networks_.GetNetworkAt(new_index - 1); | |
| 100 MessageLoop::current()->PostTask(FROM_HERE, task_factory_.NewRunnableMethod( | |
| 101 &NetworkScreen::ConnectToNetwork, network->network_type, network->label)); | |
| 102 } | |
| 103 | |
| 104 /////////////////////////////////////////////////////////////////////////////// | 42 /////////////////////////////////////////////////////////////////////////////// |
| 105 // views::ButtonListener implementation: | 43 // views::ButtonListener implementation: |
| 106 | 44 |
| 107 void NetworkScreen::ButtonPressed(views::Button* sender, | 45 void NetworkScreen::ButtonPressed(views::Button* sender, |
| 108 const views::Event& event) { | 46 const views::Event& event) { |
| 109 // Proceed only when selected network is connected. | 47 NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary(); |
| 110 const NetworkList::NetworkItem* network = GetSelectedNetwork(); | 48 if (network && network->Connected()) { |
| 111 if (!network) | 49 NotifyOnConnection(); |
| 112 return; | |
| 113 if (networks_.IsNetworkConnected(network->network_type, network->label)) { | |
| 114 MessageLoop::current()->PostTask(FROM_HERE, | |
| 115 task_factory_.NewRunnableMethod(&NetworkScreen::NotifyOnConnection)); | |
| 116 } else { | 50 } else { |
| 117 continue_pressed_ = true; | 51 continue_pressed_ = true; |
| 118 if (is_waiting_for_connect_) { | 52 WaitForConnection(network_id_); |
| 119 ShowConnectingStatus(); | |
| 120 } else { | |
| 121 MessageLoop::current()->PostTask( | |
| 122 FROM_HERE, | |
| 123 task_factory_.NewRunnableMethod(&NetworkScreen::ConnectToNetwork, | |
| 124 network->network_type, | |
| 125 network->label)); | |
| 126 } | |
| 127 } | 53 } |
| 128 } | 54 } |
| 129 | 55 |
| 130 //////////////////////////////////////////////////////////////////////////////// | 56 //////////////////////////////////////////////////////////////////////////////// |
| 131 // NetworkLibrary::Observer implementation: | 57 // NetworkLibrary::Observer implementation: |
| 132 | 58 |
| 133 void NetworkScreen::NetworkChanged(NetworkLibrary* network_lib) { | 59 void NetworkScreen::NetworkChanged(NetworkLibrary* network_lib) { |
| 134 if (!view()) | 60 UpdateStatus(network_lib); |
| 135 return; | |
| 136 | |
| 137 // TODO(nkostylev): Reuse network menu button - http://crosbug.com/4133 | |
| 138 wifi_disabled_ = !chromeos::CrosLibrary::Get()-> | |
| 139 GetNetworkLibrary()->wifi_enabled(); | |
| 140 | |
| 141 // Save network selection in case it would be available after refresh. | |
| 142 NetworkList::NetworkType network_type = NetworkList::NETWORK_EMPTY; | |
| 143 string16 network_id; | |
| 144 const NetworkList::NetworkItem* selected_network = GetSelectedNetwork(); | |
| 145 if (selected_network) { | |
| 146 network_type = selected_network->network_type; | |
| 147 network_id = selected_network->label; | |
| 148 } | |
| 149 networks_.NetworkChanged(network_lib); | |
| 150 if (is_waiting_for_connect_ && | |
| 151 networks_.IsNetworkConnected(connecting_network_.network_type, | |
| 152 connecting_network_.label)) { | |
| 153 // Stop waiting & don't update spinner status. | |
| 154 StopWaitingForConnection(false); | |
| 155 if (continue_pressed_) { | |
| 156 MessageLoop::current()->PostTask(FROM_HERE, | |
| 157 task_factory_.NewRunnableMethod(&NetworkScreen::NotifyOnConnection)); | |
| 158 return; | |
| 159 } | |
| 160 } | |
| 161 view()->NetworkModelChanged(); | |
| 162 // Prefer Ethernet when it's connected (only once). | |
| 163 if (!ethernet_preselected_ && | |
| 164 networks_.IsNetworkConnected(NetworkList::NETWORK_ETHERNET, string16())) { | |
| 165 ethernet_preselected_ = true; | |
| 166 SelectNetwork(NetworkList::NETWORK_ETHERNET, string16()); | |
| 167 } else { | |
| 168 SelectNetwork(network_type, network_id); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 //////////////////////////////////////////////////////////////////////////////// | |
| 173 // NetworkLibrary::Observer implementation: | |
| 174 | |
| 175 void NetworkScreen::OnDialogAccepted() { | |
| 176 const NetworkList::NetworkItem* network = GetSelectedNetwork(); | |
| 177 if (network) | |
| 178 WaitForConnection(network); | |
| 179 } | |
| 180 | |
| 181 void NetworkScreen::OnDialogCancelled() { | |
| 182 if (view()) { | |
| 183 view()->EnableContinue(false); | |
| 184 view()->SetSelectedNetworkItem(0); | |
| 185 } | |
| 186 } | 61 } |
| 187 | 62 |
| 188 /////////////////////////////////////////////////////////////////////////////// | 63 /////////////////////////////////////////////////////////////////////////////// |
| 189 // NetworkScreen, ViewScreen implementation: | 64 // NetworkScreen, ViewScreen implementation: |
| 190 | 65 |
| 191 void NetworkScreen::CreateView() { | 66 void NetworkScreen::CreateView() { |
| 192 language_switch_menu_.InitLanguageMenu(); | 67 language_switch_menu_.InitLanguageMenu(); |
| 193 ViewScreen<NetworkSelectionView>::CreateView(); | 68 ViewScreen<NetworkSelectionView>::CreateView(); |
| 194 } | 69 } |
| 195 | 70 |
| 196 NetworkSelectionView* NetworkScreen::AllocateView() { | 71 NetworkSelectionView* NetworkScreen::AllocateView() { |
| 197 return new NetworkSelectionView(this); | 72 return new NetworkSelectionView(this); |
| 198 } | 73 } |
| 199 | 74 |
| 200 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
| 201 // NetworkScreen, public: | 76 // NetworkScreen, public: |
| 202 | 77 |
| 203 void NetworkScreen::Refresh() { | 78 void NetworkScreen::Refresh() { |
| 204 if (CrosLibrary::Get()->EnsureLoaded()) { | 79 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 205 SubscribeNetworkNotification(); | 80 SubscribeNetworkNotification(); |
| 206 NetworkChanged(chromeos::CrosLibrary::Get()->GetNetworkLibrary()); | 81 NetworkChanged(chromeos::CrosLibrary::Get()->GetNetworkLibrary()); |
| 207 } | 82 } |
| 208 } | 83 } |
| 209 | 84 |
| 210 //////////////////////////////////////////////////////////////////////////////// | 85 //////////////////////////////////////////////////////////////////////////////// |
| 211 // NetworkScreen, private: | 86 // NetworkScreen, private: |
| 212 | 87 |
| 213 void NetworkScreen::ConnectToNetwork(NetworkList::NetworkType type, | |
| 214 const string16& id) { | |
| 215 const NetworkList::NetworkItem* network = | |
| 216 networks_.GetNetworkById(type, id); | |
| 217 if (network && | |
| 218 !networks_.IsNetworkConnected(type, id)) { | |
| 219 if (NetworkList::NETWORK_WIFI == network->network_type) { | |
| 220 if (network->wifi_network.encrypted()) { | |
| 221 OpenPasswordDialog(network->wifi_network); | |
| 222 return; | |
| 223 } else { | |
| 224 WaitForConnection(network); | |
| 225 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> | |
| 226 ConnectToWifiNetwork(network->wifi_network, | |
| 227 std::string(), std::string(), std::string()); | |
| 228 } | |
| 229 } else if (NetworkList::NETWORK_CELLULAR == network->network_type) { | |
| 230 WaitForConnection(network); | |
| 231 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> | |
| 232 ConnectToCellularNetwork(network->cellular_network); | |
| 233 } | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 void NetworkScreen::EnableWiFi() { | |
| 238 if (CrosLibrary::Get()->EnsureLoaded()) { | |
| 239 chromeos::CrosLibrary::Get()->GetNetworkLibrary()-> | |
| 240 EnableWifiNetworkDevice(true); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 void NetworkScreen::SubscribeNetworkNotification() { | 88 void NetworkScreen::SubscribeNetworkNotification() { |
| 245 if (!is_network_subscribed_) { | 89 if (!is_network_subscribed_) { |
| 246 is_network_subscribed_ = true; | 90 is_network_subscribed_ = true; |
| 247 chromeos::CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); | 91 chromeos::CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); |
| 248 } | 92 } |
| 249 } | 93 } |
| 250 | 94 |
| 251 void NetworkScreen::UnsubscribeNetworkNotification() { | 95 void NetworkScreen::UnsubscribeNetworkNotification() { |
| 252 if (is_network_subscribed_) { | 96 if (is_network_subscribed_) { |
| 253 is_network_subscribed_ = false; | 97 is_network_subscribed_ = false; |
| 254 chromeos::CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); | 98 chromeos::CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); |
| 255 } | 99 } |
| 256 } | 100 } |
| 257 | 101 |
| 258 NetworkList::NetworkItem* NetworkScreen::GetSelectedNetwork() { | |
| 259 if (!view()) | |
| 260 return NULL; | |
| 261 | |
| 262 return networks_.GetNetworkAt(view()->GetSelectedNetworkItem() - 1); | |
| 263 } | |
| 264 | |
| 265 void NetworkScreen::NotifyOnConnection() { | 102 void NetworkScreen::NotifyOnConnection() { |
| 266 // TODO(nkostylev): Check network connectivity. | 103 // TODO(nkostylev): Check network connectivity. |
| 267 UnsubscribeNetworkNotification(); | 104 UnsubscribeNetworkNotification(); |
| 268 connection_timer_.Stop(); | 105 connection_timer_.Stop(); |
| 269 delegate()->GetObserver(this)->OnExit(ScreenObserver::NETWORK_CONNECTED); | 106 delegate()->GetObserver(this)->OnExit(ScreenObserver::NETWORK_CONNECTED); |
| 270 } | 107 } |
| 271 | 108 |
| 272 void NetworkScreen::OnConnectionTimeout() { | 109 void NetworkScreen::OnConnectionTimeout() { |
| 273 continue_pressed_ = false; | |
| 274 // TODO(nkostylev): Notify on connection error. | 110 // TODO(nkostylev): Notify on connection error. |
| 275 if (is_waiting_for_connect_) { | 111 StopWaitingForConnection(network_id_); |
| 276 // Stop waiting & show selection combobox. | 112 } |
| 277 StopWaitingForConnection(true); | 113 |
| 114 void NetworkScreen::UpdateStatus(NetworkLibrary* network) { |
| 115 if (!view() || !network) |
| 116 return; |
| 117 |
| 118 if (network->ethernet_connected()) { |
| 119 StopWaitingForConnection( |
| 120 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET)); |
| 121 } else if (network->wifi_connected()) { |
| 122 StopWaitingForConnection(ASCIIToUTF16(network->wifi_name())); |
| 123 } else if (network->cellular_connected()) { |
| 124 StopWaitingForConnection(ASCIIToUTF16(network->cellular_name())); |
| 125 } else if (network->ethernet_connecting()) { |
| 126 WaitForConnection( |
| 127 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET)); |
| 128 } else if (network->wifi_connecting()) { |
| 129 WaitForConnection(ASCIIToUTF16(network->wifi_name())); |
| 130 } else if (network->cellular_connecting()) { |
| 131 WaitForConnection(ASCIIToUTF16(network->cellular_name())); |
| 132 } else { |
| 133 view()->EnableContinue(network->Connected()); |
| 278 } | 134 } |
| 279 } | 135 } |
| 280 | 136 |
| 281 void NetworkScreen::OpenPasswordDialog(WifiNetwork network) { | 137 void NetworkScreen::StopWaitingForConnection(const string16& network_id) { |
| 282 NetworkConfigView* dialog = new NetworkConfigView(network, true); | 138 NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary(); |
| 283 dialog->set_delegate(this); | 139 bool is_connected = network && network->Connected(); |
| 284 dialog->set_browser_mode(false); | 140 if (is_connected && continue_pressed_) { |
| 285 views::Window* window = views::Window::CreateChromeWindow( | 141 NotifyOnConnection(); |
| 286 view()->GetNativeWindow(), gfx::Rect(), dialog); | 142 return; |
| 287 window->SetIsAlwaysOnTop(true); | 143 } |
| 288 window->Show(); | 144 |
| 289 dialog->SetLoginTextfieldFocus(); | 145 continue_pressed_ = false; |
| 146 connection_timer_.Stop(); |
| 147 |
| 148 network_id_ = network_id; |
| 149 view()->ShowConnectingStatus(false, network_id_); |
| 150 view()->EnableContinue(is_connected); |
| 290 } | 151 } |
| 291 | 152 |
| 292 void NetworkScreen::SelectNetwork(NetworkList::NetworkType type, | 153 void NetworkScreen::WaitForConnection(const string16& network_id) { |
| 293 const string16& id) { | 154 connection_timer_.Stop(); |
| 294 int index = networks_.GetNetworkIndexById(type, id); | |
| 295 if (index >= 0) { | |
| 296 view()->SetSelectedNetworkItem(index + 1); | |
| 297 } else { | |
| 298 view()->SetSelectedNetworkItem(0); | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 void NetworkScreen::ShowConnectingStatus() { | |
| 303 view()->ShowConnectingStatus(is_waiting_for_connect_, | |
| 304 connecting_network_.label); | |
| 305 } | |
| 306 | |
| 307 void NetworkScreen::StopWaitingForConnection(bool show_combobox) { | |
| 308 if (connection_timer_.IsRunning()) | |
| 309 connection_timer_.Stop(); | |
| 310 is_waiting_for_connect_ = false; | |
| 311 connecting_network_.network_type = NetworkList::NETWORK_EMPTY; | |
| 312 connecting_network_.label.clear(); | |
| 313 if (show_combobox) | |
| 314 ShowConnectingStatus(); | |
| 315 } | |
| 316 | |
| 317 void NetworkScreen::WaitForConnection(const NetworkList::NetworkItem* network) { | |
| 318 is_waiting_for_connect_ = true; | |
| 319 DCHECK(network); | |
| 320 connecting_network_.network_type = network->network_type; | |
| 321 connecting_network_.label = network->label; | |
| 322 if (connection_timer_.IsRunning()) | |
| 323 connection_timer_.Stop(); | |
| 324 connection_timer_.Start(base::TimeDelta::FromSeconds(kConnectionTimeoutSec), | 155 connection_timer_.Start(base::TimeDelta::FromSeconds(kConnectionTimeoutSec), |
| 325 this, | 156 this, |
| 326 &NetworkScreen::OnConnectionTimeout); | 157 &NetworkScreen::OnConnectionTimeout); |
| 327 if (continue_pressed_) | 158 |
| 328 ShowConnectingStatus(); | 159 network_id_ = network_id; |
| 160 view()->ShowConnectingStatus(true, network_id_); |
| 161 |
| 162 view()->EnableContinue(false); |
| 329 } | 163 } |
| 330 | 164 |
| 331 } // namespace chromeos | 165 } // namespace chromeos |
| OLD | NEW |