Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / screens / network_screen.cc
blobaf6d66e588267ea1907963a0a2a1ab538bffb090
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"
24 namespace {
26 // Time in seconds for connection timeout.
27 const int kConnectionTimeoutSec = 15;
29 } // namespace
31 namespace chromeos {
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),
41 actor_(actor),
42 network_state_helper_(new login::NetworkStateHelper) {
43 DCHECK(actor_);
44 if (actor_)
45 actor_->SetDelegate(this);
48 NetworkScreen::~NetworkScreen() {
49 if (actor_)
50 actor_->SetDelegate(NULL);
51 connection_timer_.Stop();
52 UnsubscribeNetworkNotification();
55 ////////////////////////////////////////////////////////////////////////////////
56 // NetworkScreen, WizardScreen implementation:
58 void NetworkScreen::PrepareToShow() {
59 if (actor_)
60 actor_->PrepareToShow();
63 void NetworkScreen::Show() {
64 Refresh();
65 if (actor_)
66 actor_->Show();
69 void NetworkScreen::Hide() {
70 if (actor_)
71 actor_->Hide();
74 std::string NetworkScreen::GetName() const {
75 return WizardController::kNetworkScreenName;
78 ////////////////////////////////////////////////////////////////////////////////
79 // NetworkScreen, NetworkStateHandlerObserver implementation:
81 void NetworkScreen::NetworkConnectionStateChanged(const NetworkState* network) {
82 UpdateStatus();
85 void NetworkScreen::DefaultNetworkChanged(const NetworkState* network) {
86 UpdateStatus();
89 ////////////////////////////////////////////////////////////////////////////////
90 // NetworkScreen, public:
92 void NetworkScreen::Refresh() {
93 SubscribeNetworkNotification();
94 UpdateStatus();
97 ///////////////////////////////////////////////////////////////////////////////
98 // NetworkScreen, NetworkScreenActor::Delegate implementation:
100 void NetworkScreen::OnActorDestroyed(NetworkScreenActor* actor) {
101 if (actor_ == actor)
102 actor_ = NULL;
105 void NetworkScreen::OnContinuePressed() {
106 if (network_state_helper_->IsConnected()) {
107 NotifyOnConnection();
108 } else {
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(
126 this, FROM_HERE);
130 void NetworkScreen::UnsubscribeNetworkNotification() {
131 if (is_network_subscribed_) {
132 is_network_subscribed_ = false;
133 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
134 this, FROM_HERE);
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.
149 actor_->ShowError(
150 l10n_util::GetStringFUTF16(
151 IDS_NETWORK_SELECTION_ERROR,
152 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME),
153 network_id_));
157 void NetworkScreen::UpdateStatus() {
158 if (!actor_)
159 return;
161 bool is_connected = network_state_helper_->IsConnected();
162 if (is_connected)
163 actor_->ClearErrors();
165 base::string16 network_name = network_state_helper_->GetCurrentNetworkName();
166 if (is_connected) {
167 StopWaitingForConnection(network_name);
168 } else if (network_state_helper_->IsConnecting()) {
169 WaitForConnection(network_name);
170 } else {
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();
179 return;
182 continue_pressed_ = false;
183 connection_timer_.Stop();
185 network_id_ = network_id;
186 if (actor_) {
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),
197 this,
198 &NetworkScreen::OnConnectionTimeout);
201 network_id_ = network_id;
202 if (actor_) {
203 actor_->ShowConnectingStatus(continue_pressed_, network_id_);
204 actor_->EnableContinue(false);
208 } // namespace chromeos