network_screen.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/login/screens/network_screen.h" 6 7#include "base/logging.h" 8#include "base/strings/string16.h" 9#include "base/strings/utf_string_conversions.h" 10#include "chrome/browser/chromeos/cros/cros_library.h" 11#include "chrome/browser/chromeos/login/help_app_launcher.h" 12#include "chrome/browser/chromeos/login/helper.h" 13#include "chrome/browser/chromeos/login/login_utils.h" 14#include "chrome/browser/chromeos/login/screens/screen_observer.h" 15#include "chrome/browser/chromeos/login/wizard_controller.h" 16#include "chrome/browser/chromeos/net/connectivity_state_helper.h" 17#include "grit/chromium_strings.h" 18#include "grit/generated_resources.h" 19#include "grit/theme_resources.h" 20#include "ui/base/l10n/l10n_util.h" 21#include "ui/base/resource/resource_bundle.h" 22 23namespace { 24 25// Time in seconds for connection timeout. 26const int kConnectionTimeoutSec = 15; 27 28} // namespace 29 30namespace chromeos { 31 32/////////////////////////////////////////////////////////////////////////////// 33// NetworkScreen, public: 34 35NetworkScreen::NetworkScreen(ScreenObserver* screen_observer, 36 NetworkScreenActor* actor) 37 : WizardScreen(screen_observer), 38 is_network_subscribed_(false), 39 continue_pressed_(false), 40 actor_(actor) { 41 DCHECK(actor_); 42 if (actor_) 43 actor_->SetDelegate(this); 44} 45 46NetworkScreen::~NetworkScreen() { 47 if (actor_) 48 actor_->SetDelegate(NULL); 49 connection_timer_.Stop(); 50 UnsubscribeNetworkNotification(); 51} 52 53//////////////////////////////////////////////////////////////////////////////// 54// NetworkScreen, WizardScreen implementation: 55 56void NetworkScreen::PrepareToShow() { 57 if (actor_) 58 actor_->PrepareToShow(); 59} 60 61void NetworkScreen::Show() { 62 Refresh(); 63 if (actor_) 64 actor_->Show(); 65} 66 67void NetworkScreen::Hide() { 68 if (actor_) 69 actor_->Hide(); 70} 71 72std::string NetworkScreen::GetName() const { 73 return WizardController::kNetworkScreenName; 74} 75 76//////////////////////////////////////////////////////////////////////////////// 77// NetworkScreen, ConnectivityStateHelperObserver implementation: 78 79void NetworkScreen::NetworkManagerChanged() { 80 UpdateStatus(); 81} 82 83void NetworkScreen::DefaultNetworkChanged() { 84 NetworkManagerChanged(); 85} 86 87//////////////////////////////////////////////////////////////////////////////// 88// NetworkScreen, public: 89 90void NetworkScreen::Refresh() { 91 SubscribeNetworkNotification(); 92 NetworkManagerChanged(); 93} 94 95/////////////////////////////////////////////////////////////////////////////// 96// NetworkScreen, NetworkScreenActor::Delegate implementation: 97 98void NetworkScreen::OnActorDestroyed(NetworkScreenActor* actor) { 99 if (actor_ == actor) 100 actor_ = NULL; 101} 102 103void NetworkScreen::OnContinuePressed() { 104 if (ConnectivityStateHelper::Get()->IsConnected()) { 105 NotifyOnConnection(); 106 } else { 107 continue_pressed_ = true; 108 WaitForConnection(network_id_); 109 } 110} 111 112//////////////////////////////////////////////////////////////////////////////// 113// NetworkScreen, private: 114 115void NetworkScreen::SubscribeNetworkNotification() { 116 if (!is_network_subscribed_) { 117 is_network_subscribed_ = true; 118 ConnectivityStateHelper::Get()->AddNetworkManagerObserver(this); 119 } 120} 121 122void NetworkScreen::UnsubscribeNetworkNotification() { 123 if (is_network_subscribed_) { 124 is_network_subscribed_ = false; 125 ConnectivityStateHelper::Get()->RemoveNetworkManagerObserver(this); 126 } 127} 128 129void NetworkScreen::NotifyOnConnection() { 130 // TODO(nkostylev): Check network connectivity. 131 UnsubscribeNetworkNotification(); 132 connection_timer_.Stop(); 133 get_screen_observer()->OnExit(ScreenObserver::NETWORK_CONNECTED); 134} 135 136void NetworkScreen::OnConnectionTimeout() { 137 StopWaitingForConnection(network_id_); 138 if (!ConnectivityStateHelper::Get()->IsConnected() && actor_) { 139 // Show error bubble. 140 actor_->ShowError( 141 l10n_util::GetStringFUTF16( 142 IDS_NETWORK_SELECTION_ERROR, 143 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME), 144 network_id_)); 145 } 146} 147 148void NetworkScreen::UpdateStatus() { 149 if (!actor_) 150 return; 151 152 bool is_connected = ConnectivityStateHelper::Get()->IsConnected(); 153 if (is_connected) 154 actor_->ClearErrors(); 155 156 string16 network_name = GetCurrentNetworkName(); 157 if (is_connected) { 158 StopWaitingForConnection(network_name); 159 } else if (ConnectivityStateHelper::Get()->IsConnecting()) { 160 WaitForConnection(network_name); 161 } else { 162 StopWaitingForConnection(network_id_); 163 } 164} 165 166void NetworkScreen::StopWaitingForConnection(const string16& network_id) { 167 bool is_connected = ConnectivityStateHelper::Get()->IsConnected(); 168 if (is_connected && continue_pressed_) { 169 NotifyOnConnection(); 170 return; 171 } 172 173 continue_pressed_ = false; 174 connection_timer_.Stop(); 175 176 network_id_ = network_id; 177 if (actor_) { 178 actor_->ShowConnectingStatus(false, network_id_); 179 actor_->EnableContinue(is_connected); 180 } 181} 182 183void NetworkScreen::WaitForConnection(const string16& network_id) { 184 if (network_id_ != network_id || !connection_timer_.IsRunning()) { 185 connection_timer_.Stop(); 186 connection_timer_.Start(FROM_HERE, 187 base::TimeDelta::FromSeconds(kConnectionTimeoutSec), 188 this, 189 &NetworkScreen::OnConnectionTimeout); 190 } 191 192 network_id_ = network_id; 193 if (actor_) { 194 actor_->ShowConnectingStatus(continue_pressed_, network_id_); 195 actor_->EnableContinue(false); 196 } 197} 198 199} // namespace chromeos 200