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/screens/screen_observer.h"
15 #include "chrome/browser/chromeos/login/wizard_controller.h"
16 #include "chromeos/network/network_handler.h"
17 #include "chromeos/network/network_state_handler.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
26 // Time in seconds for connection timeout.
27 const int kConnectionTimeoutSec
= 15;
33 ///////////////////////////////////////////////////////////////////////////////
34 // NetworkScreen, public:
36 NetworkScreen::NetworkScreen(ScreenObserver
* screen_observer
,
37 NetworkScreenActor
* actor
)
38 : WizardScreen(screen_observer
),
39 is_network_subscribed_(false),
40 continue_pressed_(false),
42 network_state_helper_(new login::NetworkStateHelper
) {
45 actor_
->SetDelegate(this);
48 NetworkScreen::~NetworkScreen() {
50 actor_
->SetDelegate(NULL
);
51 connection_timer_
.Stop();
52 UnsubscribeNetworkNotification();
55 ////////////////////////////////////////////////////////////////////////////////
56 // NetworkScreen, WizardScreen implementation:
58 void NetworkScreen::PrepareToShow() {
60 actor_
->PrepareToShow();
63 void NetworkScreen::Show() {
69 void NetworkScreen::Hide() {
74 std::string
NetworkScreen::GetName() const {
75 return WizardController::kNetworkScreenName
;
78 ////////////////////////////////////////////////////////////////////////////////
79 // NetworkScreen, NetworkStateHandlerObserver implementation:
81 void NetworkScreen::NetworkConnectionStateChanged(const NetworkState
* network
) {
85 void NetworkScreen::DefaultNetworkChanged(const NetworkState
* network
) {
89 ////////////////////////////////////////////////////////////////////////////////
90 // NetworkScreen, public:
92 void NetworkScreen::Refresh() {
93 SubscribeNetworkNotification();
97 ///////////////////////////////////////////////////////////////////////////////
98 // NetworkScreen, NetworkScreenActor::Delegate implementation:
100 void NetworkScreen::OnActorDestroyed(NetworkScreenActor
* actor
) {
105 void NetworkScreen::OnContinuePressed() {
106 if (network_state_helper_
->IsConnected()) {
107 NotifyOnConnection();
109 continue_pressed_
= true;
110 WaitForConnection(network_id_
);
114 ////////////////////////////////////////////////////////////////////////////////
115 // NetworkScreen, private:
117 void NetworkScreen::SetNetworkStateHelperForTest(
118 login::NetworkStateHelper
* helper
) {
119 network_state_helper_
.reset(helper
);
122 void NetworkScreen::SubscribeNetworkNotification() {
123 if (!is_network_subscribed_
) {
124 is_network_subscribed_
= true;
125 NetworkHandler::Get()->network_state_handler()->AddObserver(
130 void NetworkScreen::UnsubscribeNetworkNotification() {
131 if (is_network_subscribed_
) {
132 is_network_subscribed_
= false;
133 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
138 void NetworkScreen::NotifyOnConnection() {
139 // TODO(nkostylev): Check network connectivity.
140 UnsubscribeNetworkNotification();
141 connection_timer_
.Stop();
142 get_screen_observer()->OnExit(ScreenObserver::NETWORK_CONNECTED
);
145 void NetworkScreen::OnConnectionTimeout() {
146 StopWaitingForConnection(network_id_
);
147 if (!network_state_helper_
->IsConnected() && actor_
) {
148 // Show error bubble.
150 l10n_util::GetStringFUTF16(
151 IDS_NETWORK_SELECTION_ERROR
,
152 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME
),
157 void NetworkScreen::UpdateStatus() {
161 bool is_connected
= network_state_helper_
->IsConnected();
163 actor_
->ClearErrors();
165 base::string16 network_name
= network_state_helper_
->GetCurrentNetworkName();
167 StopWaitingForConnection(network_name
);
168 } else if (network_state_helper_
->IsConnecting()) {
169 WaitForConnection(network_name
);
171 StopWaitingForConnection(network_id_
);
175 void NetworkScreen::StopWaitingForConnection(const base::string16
& network_id
) {
176 bool is_connected
= network_state_helper_
->IsConnected();
177 if (is_connected
&& continue_pressed_
) {
178 NotifyOnConnection();
182 continue_pressed_
= false;
183 connection_timer_
.Stop();
185 network_id_
= network_id
;
187 actor_
->ShowConnectingStatus(false, network_id_
);
188 actor_
->EnableContinue(is_connected
);
192 void NetworkScreen::WaitForConnection(const base::string16
& network_id
) {
193 if (network_id_
!= network_id
|| !connection_timer_
.IsRunning()) {
194 connection_timer_
.Stop();
195 connection_timer_
.Start(FROM_HERE
,
196 base::TimeDelta::FromSeconds(kConnectionTimeoutSec
),
198 &NetworkScreen::OnConnectionTimeout
);
201 network_id_
= network_id
;
203 actor_
->ShowConnectingStatus(continue_pressed_
, network_id_
);
204 actor_
->EnableContinue(false);
208 } // namespace chromeos