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.
5 #include "chrome/browser/chromeos/login/screens/network_screen.h"
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.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/screen_manager.h"
15 #include "chrome/browser/chromeos/login/screens/screen_observer.h"
16 #include "chrome/browser/chromeos/login/wizard_controller.h"
17 #include "chrome/grit/chromium_strings.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "chromeos/network/network_handler.h"
20 #include "chromeos/network/network_state_handler.h"
21 #include "ui/base/l10n/l10n_util.h"
25 // Time in seconds for connection timeout.
26 const int kConnectionTimeoutSec
= 40;
32 ///////////////////////////////////////////////////////////////////////////////
33 // NetworkScreen, public:
36 NetworkScreen
* NetworkScreen::Get(ScreenManager
* manager
) {
37 return static_cast<NetworkScreen
*>(
38 manager
->GetScreen(WizardController::kNetworkScreenName
));
41 NetworkScreen::NetworkScreen(ScreenObserver
* screen_observer
,
42 NetworkScreenActor
* actor
)
43 : WizardScreen(screen_observer
),
44 is_network_subscribed_(false),
45 continue_pressed_(false),
47 network_state_helper_(new login::NetworkStateHelper
) {
50 actor_
->SetDelegate(this);
53 NetworkScreen::~NetworkScreen() {
55 actor_
->SetDelegate(NULL
);
56 connection_timer_
.Stop();
57 UnsubscribeNetworkNotification();
60 ////////////////////////////////////////////////////////////////////////////////
61 // NetworkScreen, WizardScreen implementation:
63 void NetworkScreen::PrepareToShow() {
65 actor_
->PrepareToShow();
68 void NetworkScreen::Show() {
74 void NetworkScreen::Hide() {
79 std::string
NetworkScreen::GetName() const {
80 return WizardController::kNetworkScreenName
;
83 ////////////////////////////////////////////////////////////////////////////////
84 // NetworkScreen, NetworkStateHandlerObserver implementation:
86 void NetworkScreen::NetworkConnectionStateChanged(const NetworkState
* network
) {
90 void NetworkScreen::DefaultNetworkChanged(const NetworkState
* network
) {
94 ////////////////////////////////////////////////////////////////////////////////
95 // NetworkScreen, public:
97 void NetworkScreen::Refresh() {
98 SubscribeNetworkNotification();
102 ///////////////////////////////////////////////////////////////////////////////
103 // NetworkScreen, NetworkScreenActor::Delegate implementation:
105 void NetworkScreen::OnActorDestroyed(NetworkScreenActor
* actor
) {
110 void NetworkScreen::OnContinuePressed() {
111 if (network_state_helper_
->IsConnected()) {
112 NotifyOnConnection();
114 continue_pressed_
= true;
115 WaitForConnection(network_id_
);
119 ////////////////////////////////////////////////////////////////////////////////
120 // NetworkScreen, private:
122 void NetworkScreen::SetNetworkStateHelperForTest(
123 login::NetworkStateHelper
* helper
) {
124 network_state_helper_
.reset(helper
);
127 void NetworkScreen::SubscribeNetworkNotification() {
128 if (!is_network_subscribed_
) {
129 is_network_subscribed_
= true;
130 NetworkHandler::Get()->network_state_handler()->AddObserver(
135 void NetworkScreen::UnsubscribeNetworkNotification() {
136 if (is_network_subscribed_
) {
137 is_network_subscribed_
= false;
138 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
143 void NetworkScreen::NotifyOnConnection() {
144 // TODO(nkostylev): Check network connectivity.
145 UnsubscribeNetworkNotification();
146 connection_timer_
.Stop();
147 get_screen_observer()->OnExit(ScreenObserver::NETWORK_CONNECTED
);
150 void NetworkScreen::OnConnectionTimeout() {
151 StopWaitingForConnection(network_id_
);
152 if (!network_state_helper_
->IsConnected() && actor_
) {
153 // Show error bubble.
155 l10n_util::GetStringFUTF16(
156 IDS_NETWORK_SELECTION_ERROR
,
157 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME
),
162 void NetworkScreen::UpdateStatus() {
166 bool is_connected
= network_state_helper_
->IsConnected();
168 actor_
->ClearErrors();
170 base::string16 network_name
= network_state_helper_
->GetCurrentNetworkName();
172 StopWaitingForConnection(network_name
);
173 } else if (network_state_helper_
->IsConnecting()) {
174 WaitForConnection(network_name
);
176 StopWaitingForConnection(network_id_
);
180 void NetworkScreen::StopWaitingForConnection(const base::string16
& network_id
) {
181 bool is_connected
= network_state_helper_
->IsConnected();
182 if (is_connected
&& continue_pressed_
) {
183 NotifyOnConnection();
187 continue_pressed_
= false;
188 connection_timer_
.Stop();
190 network_id_
= network_id
;
192 actor_
->ShowConnectingStatus(false, network_id_
);
193 actor_
->EnableContinue(is_connected
);
197 void NetworkScreen::WaitForConnection(const base::string16
& network_id
) {
198 if (network_id_
!= network_id
|| !connection_timer_
.IsRunning()) {
199 connection_timer_
.Stop();
200 connection_timer_
.Start(FROM_HERE
,
201 base::TimeDelta::FromSeconds(kConnectionTimeoutSec
),
203 &NetworkScreen::OnConnectionTimeout
);
206 network_id_
= network_id
;
208 actor_
->ShowConnectingStatus(continue_pressed_
, network_id_
);
209 actor_
->EnableContinue(false);
213 } // namespace chromeos