OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "athena/main/debug/network_selector.h" |
| 6 |
| 7 #include "base/memory/weak_ptr.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chromeos/network/network_configuration_handler.h" |
| 10 #include "chromeos/network/network_connection_handler.h" |
| 11 #include "chromeos/network/network_handler.h" |
| 12 #include "chromeos/network/network_profile_handler.h" |
| 13 #include "chromeos/network/network_state.h" |
| 14 #include "chromeos/network/network_state_handler.h" |
| 15 #include "chromeos/network/network_state_handler_observer.h" |
| 16 #include "chromeos/network/network_type_pattern.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 #include "ui/aura/window.h" |
| 19 #include "ui/chromeos/network/network_icon.h" |
| 20 #include "ui/chromeos/network/network_info.h" |
| 21 #include "ui/chromeos/network/network_list.h" |
| 22 #include "ui/chromeos/network/network_list_delegate.h" |
| 23 #include "ui/compositor/layer.h" |
| 24 #include "ui/gfx/canvas.h" |
| 25 #include "ui/gfx/font.h" |
| 26 #include "ui/gfx/font_list.h" |
| 27 #include "ui/gfx/geometry/rect.h" |
| 28 #include "ui/gfx/text_constants.h" |
| 29 #include "ui/views/background.h" |
| 30 #include "ui/views/border.h" |
| 31 #include "ui/views/controls/button/blue_button.h" |
| 32 #include "ui/views/controls/button/button.h" |
| 33 #include "ui/views/controls/image_view.h" |
| 34 #include "ui/views/controls/label.h" |
| 35 #include "ui/views/controls/scroll_view.h" |
| 36 #include "ui/views/controls/textfield/textfield.h" |
| 37 #include "ui/views/layout/box_layout.h" |
| 38 #include "ui/views/layout/fill_layout.h" |
| 39 #include "ui/views/widget/widget.h" |
| 40 |
| 41 using chromeos::NetworkConfigurationHandler; |
| 42 using chromeos::NetworkConnectionHandler; |
| 43 using chromeos::NetworkHandler; |
| 44 using chromeos::NetworkProfileHandler; |
| 45 using chromeos::NetworkState; |
| 46 |
| 47 namespace { |
| 48 |
| 49 const int kBackgroundColor = SkColorSetARGB(0x7f, 0, 0, 0); |
| 50 const int kBackgroundColorLighter = SkColorSetARGB(0x2f, 0, 0, 0); |
| 51 |
| 52 // The View for the user to enter the password for connceting to a network. This |
| 53 // view also shows an error message if the network connection fails. |
| 54 class PasswordView : public views::View, public views::ButtonListener { |
| 55 public: |
| 56 PasswordView(const ui::NetworkInfo& network, |
| 57 const base::Callback<void(bool)>& callback, |
| 58 views::View* parent_container) |
| 59 : network_(network), |
| 60 callback_(callback), |
| 61 parent_container_(parent_container), |
| 62 connect_(NULL), |
| 63 cancel_(NULL), |
| 64 textfield_(NULL), |
| 65 error_msg_(NULL), |
| 66 weak_ptr_(this) { |
| 67 const int kHorizontal = 5; |
| 68 const int kVertical = 0; |
| 69 const int kPadding = 0; |
| 70 |
| 71 views::BoxLayout* layout = new views::BoxLayout( |
| 72 views::BoxLayout::kVertical, kHorizontal, kVertical, kPadding); |
| 73 layout->set_main_axis_alignment( |
| 74 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 75 layout->set_cross_axis_alignment( |
| 76 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 77 SetLayoutManager(layout); |
| 78 |
| 79 views::View* container = new views::View; |
| 80 layout = new views::BoxLayout( |
| 81 views::BoxLayout::kHorizontal, kHorizontal, kVertical, kPadding); |
| 82 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_FILL); |
| 83 container->SetLayoutManager(layout); |
| 84 |
| 85 textfield_ = new views::Textfield(); |
| 86 textfield_->set_placeholder_text(base::ASCIIToUTF16("Password")); |
| 87 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 88 textfield_->set_default_width_in_chars(35); |
| 89 container->AddChildView(textfield_); |
| 90 |
| 91 connect_ = new views::BlueButton(this, base::ASCIIToUTF16("Connect")); |
| 92 container->AddChildView(connect_); |
| 93 |
| 94 cancel_ = new views::LabelButton(this, base::ASCIIToUTF16("Cancel")); |
| 95 cancel_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 96 container->AddChildView(cancel_); |
| 97 |
| 98 AddChildView(container); |
| 99 } |
| 100 |
| 101 virtual ~PasswordView() {} |
| 102 |
| 103 private: |
| 104 void Close(bool successful) { callback_.Run(successful); } |
| 105 |
| 106 void OnKnownError(const std::string& error_name, |
| 107 scoped_ptr<base::DictionaryValue> error_data) { |
| 108 std::string message; |
| 109 if (!error_data->GetString(chromeos::network_handler::kDbusErrorMessage, |
| 110 &message)) |
| 111 message = error_name; |
| 112 if (message.empty()) |
| 113 message = std::string("Unknown error."); |
| 114 if (!error_msg_) { |
| 115 error_msg_ = new views::Label(); |
| 116 error_msg_->SetFontList( |
| 117 error_msg_->font_list().Derive(0, gfx::Font::BOLD)); |
| 118 error_msg_->SetEnabledColor(SK_ColorRED); |
| 119 } |
| 120 error_msg_->SetText(base::UTF8ToUTF16(message)); |
| 121 if (!error_msg_->parent()) { |
| 122 AddChildView(error_msg_); |
| 123 // It is unfortunately necessary to have all the views upto the container |
| 124 // do a layout because of the change in height of the view because of the |
| 125 // error message. |
| 126 for (views::View* v = this; v != parent_container_; v = v->parent()) |
| 127 v->Layout(); |
| 128 parent_container_->Layout(); |
| 129 } |
| 130 connect_->SetEnabled(true); |
| 131 } |
| 132 |
| 133 void OnSetProfileSucceed(const base::string16& password) { |
| 134 base::DictionaryValue properties; |
| 135 properties.SetStringWithoutPathExpansion(shill::kPassphraseProperty, |
| 136 textfield_->text()); |
| 137 NetworkHandler::Get()->network_configuration_handler()->SetProperties( |
| 138 network_.service_path, |
| 139 properties, |
| 140 base::Bind(&PasswordView::OnSetPropertiesSucceed, |
| 141 weak_ptr_.GetWeakPtr()), |
| 142 base::Bind(&PasswordView::OnKnownError, weak_ptr_.GetWeakPtr())); |
| 143 } |
| 144 |
| 145 void OnSetPropertiesSucceed() { |
| 146 const bool check_error_state = false; |
| 147 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( |
| 148 network_.service_path, |
| 149 base::Bind(&PasswordView::OnConnectionSucceed, weak_ptr_.GetWeakPtr()), |
| 150 base::Bind(&PasswordView::OnKnownError, weak_ptr_.GetWeakPtr()), |
| 151 check_error_state); |
| 152 } |
| 153 |
| 154 void OnConnectionSucceed() { Close(true); } |
| 155 |
| 156 // views::ButtonListener: |
| 157 virtual void ButtonPressed(views::Button* sender, |
| 158 const ui::Event& event) OVERRIDE { |
| 159 if (sender == connect_) { |
| 160 if (error_msg_) { |
| 161 RemoveChildView(error_msg_); |
| 162 delete error_msg_; |
| 163 error_msg_ = NULL; |
| 164 } |
| 165 connect_->SetEnabled(false); |
| 166 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile( |
| 167 network_.service_path, |
| 168 NetworkProfileHandler::GetSharedProfilePath(), |
| 169 base::Bind(&PasswordView::OnSetProfileSucceed, |
| 170 weak_ptr_.GetWeakPtr(), |
| 171 textfield_->text()), |
| 172 base::Bind(&PasswordView::OnKnownError, weak_ptr_.GetWeakPtr())); |
| 173 } else if (sender == cancel_) { |
| 174 views::View* parent_view = parent(); |
| 175 Close(false); |
| 176 parent_view->Layout(); |
| 177 } else { |
| 178 NOTREACHED(); |
| 179 } |
| 180 } |
| 181 |
| 182 ui::NetworkInfo network_; |
| 183 base::Callback<void(bool)> callback_; |
| 184 views::View* parent_container_; |
| 185 |
| 186 views::BlueButton* connect_; |
| 187 views::LabelButton* cancel_; |
| 188 views::Textfield* textfield_; |
| 189 views::Label* error_msg_; |
| 190 base::WeakPtrFactory<PasswordView> weak_ptr_; |
| 191 |
| 192 DISALLOW_COPY_AND_ASSIGN(PasswordView); |
| 193 }; |
| 194 |
| 195 // A View that represents a single row in the network list. This row also |
| 196 // contains the View for taking password for password-protected networks. |
| 197 class NetworkRow : public views::View { |
| 198 public: |
| 199 NetworkRow(const ui::NetworkInfo& network, views::View* container) |
| 200 : network_(network), container_(container), weak_ptr_(this) { |
| 201 SetBorder(views::Border::CreateEmptyBorder(10, 5, 10, 5)); |
| 202 SetLayoutManager( |
| 203 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 10)); |
| 204 Update(network); |
| 205 } |
| 206 |
| 207 virtual ~NetworkRow() {} |
| 208 |
| 209 void Update(const ui::NetworkInfo& network) { |
| 210 network_ = network; |
| 211 views::ImageView* icon = new views::ImageView(); |
| 212 icon->SetImage(network.image); |
| 213 icon->SetBounds(0, 0, network.image.width(), network.image.height()); |
| 214 |
| 215 views::Label* label = new views::Label(network.label); |
| 216 if (network.highlight) |
| 217 label->SetFontList(label->font_list().Derive(0, gfx::Font::BOLD)); |
| 218 AddChildView(icon); |
| 219 AddChildView(label); |
| 220 if (password_view_) |
| 221 AddChildView(password_view_.get()); |
| 222 } |
| 223 |
| 224 private: |
| 225 void OnPasswordComplete(bool successful) { |
| 226 password_view_.reset(); |
| 227 Layout(); |
| 228 parent()->Layout(); |
| 229 container_->Layout(); |
| 230 } |
| 231 |
| 232 void ShowPasswordView(const std::string& service_path) { |
| 233 const NetworkState* network = |
| 234 NetworkHandler::Get()->network_state_handler()->GetNetworkState( |
| 235 service_path); |
| 236 if (!network) |
| 237 return; |
| 238 |
| 239 // If this is not a wifi network that needs a password, then ignore. |
| 240 if (network->type() != shill::kTypeWifi || |
| 241 network->security() == shill::kSecurityNone) { |
| 242 return; |
| 243 } |
| 244 |
| 245 password_view_.reset(new PasswordView( |
| 246 network_, |
| 247 base::Bind(&NetworkRow::OnPasswordComplete, weak_ptr_.GetWeakPtr()), |
| 248 container_)); |
| 249 password_view_->set_owned_by_client(); |
| 250 AddChildView(password_view_.get()); |
| 251 PreferredSizeChanged(); |
| 252 Layout(); |
| 253 parent()->Layout(); |
| 254 container_->Layout(); |
| 255 } |
| 256 |
| 257 void OnNetworkConnectionError(const std::string& service_path, |
| 258 const std::string& error_name, |
| 259 scoped_ptr<base::DictionaryValue> error_data) { |
| 260 if (error_name == NetworkConnectionHandler::kErrorConnectCanceled) |
| 261 return; |
| 262 if (error_name == shill::kErrorBadPassphrase || |
| 263 error_name == NetworkConnectionHandler::kErrorPassphraseRequired || |
| 264 error_name == NetworkConnectionHandler::kErrorConfigurationRequired || |
| 265 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) { |
| 266 ShowPasswordView(service_path); |
| 267 } |
| 268 } |
| 269 |
| 270 void Activate() { |
| 271 const chromeos::NetworkState* network = |
| 272 NetworkHandler::Get()->network_state_handler()->GetNetworkState( |
| 273 network_.service_path); |
| 274 if (!network) |
| 275 return; |
| 276 if (network->IsConnectedState()) { |
| 277 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork( |
| 278 network_.service_path, |
| 279 base::Closure(), |
| 280 chromeos::network_handler::ErrorCallback()); |
| 281 } else if (!network->IsConnectingState()) { |
| 282 // |network| is not connected, and not already trying to connect. |
| 283 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( |
| 284 network_.service_path, |
| 285 base::Closure(), |
| 286 base::Bind(&NetworkRow::OnNetworkConnectionError, |
| 287 weak_ptr_.GetWeakPtr(), |
| 288 network_.service_path), |
| 289 false); |
| 290 } |
| 291 } |
| 292 |
| 293 // views::View: |
| 294 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE { |
| 295 if (event->type() != ui::ET_MOUSE_PRESSED) |
| 296 return; |
| 297 Activate(); |
| 298 event->SetHandled(); |
| 299 } |
| 300 |
| 301 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE { |
| 302 if (gesture->type() != ui::ET_GESTURE_TAP) |
| 303 return; |
| 304 Activate(); |
| 305 gesture->SetHandled(); |
| 306 } |
| 307 |
| 308 ui::NetworkInfo network_; |
| 309 views::View* container_; |
| 310 base::WeakPtrFactory<NetworkRow> weak_ptr_; |
| 311 scoped_ptr<views::View> password_view_; |
| 312 |
| 313 DISALLOW_COPY_AND_ASSIGN(NetworkRow); |
| 314 }; |
| 315 |
| 316 class NetworkSelector : public ui::NetworkListDelegate, |
| 317 public chromeos::NetworkStateHandlerObserver, |
| 318 public ui::EventHandler { |
| 319 public: |
| 320 explicit NetworkSelector(aura::Window* container) |
| 321 : background_view_(NULL), |
| 322 scroll_content_(NULL), |
| 323 scroller_(NULL), |
| 324 network_list_(this) { |
| 325 CreateWidget(container); |
| 326 CreateNetworkList(); |
| 327 |
| 328 NetworkHandler::Get()->network_state_handler()->RequestScan(); |
| 329 NetworkHandler::Get()->network_state_handler()->AddObserver(this, |
| 330 FROM_HERE); |
| 331 } |
| 332 |
| 333 virtual ~NetworkSelector() { |
| 334 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this, |
| 335 FROM_HERE); |
| 336 } |
| 337 |
| 338 private: |
| 339 void CreateWidget(aura::Window* container) { |
| 340 views::Widget::InitParams params; |
| 341 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 342 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 343 params.activatable = views::Widget::InitParams::ACTIVATABLE_DEFAULT; |
| 344 params.accept_events = true; |
| 345 params.bounds = gfx::Rect(container->bounds().size()); |
| 346 params.parent = container; |
| 347 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 348 widget_.reset(new views::Widget()); |
| 349 widget_->Init(params); |
| 350 widget_->Show(); |
| 351 |
| 352 background_view_ = new views::View; |
| 353 background_view_->set_background( |
| 354 views::Background::CreateSolidBackground(kBackgroundColor)); |
| 355 background_view_->SetBorder( |
| 356 views::Border::CreateEmptyBorder(100, 300, 100, 300)); |
| 357 background_view_->SetLayoutManager(new views::FillLayout()); |
| 358 background_view_->set_target_handler(this); |
| 359 |
| 360 widget_->SetContentsView(background_view_); |
| 361 } |
| 362 |
| 363 void CreateNetworkList() { |
| 364 const int kListHeight = 500; |
| 365 scroller_ = new views::ScrollView(); |
| 366 scroller_->set_background( |
| 367 views::Background::CreateSolidBackground(SK_ColorWHITE)); |
| 368 scroller_->SetBounds(0, 0, 400, kListHeight); |
| 369 |
| 370 scroll_content_ = new views::View; |
| 371 scroll_content_->SetLayoutManager( |
| 372 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
| 373 scroller_->SetContents(scroll_content_); |
| 374 |
| 375 scroller_->ClipHeightTo(kListHeight, kListHeight); |
| 376 background_view_->AddChildView(scroller_); |
| 377 |
| 378 background_view_->Layout(); |
| 379 scroller_->Layout(); |
| 380 scroll_content_->Layout(); |
| 381 |
| 382 network_list_.set_content_view(scroll_content_); |
| 383 } |
| 384 |
| 385 void UpdateNetworkList() { network_list_.UpdateNetworkList(); } |
| 386 |
| 387 void Close() { delete this; } |
| 388 |
| 389 // ui::NetworkListDelegate: |
| 390 virtual views::View* CreateViewForNetwork( |
| 391 const ui::NetworkInfo& info) OVERRIDE { |
| 392 return new NetworkRow(info, background_view_); |
| 393 } |
| 394 |
| 395 virtual bool IsViewHovered(views::View* view) OVERRIDE { return false; } |
| 396 |
| 397 virtual chromeos::NetworkTypePattern GetNetworkTypePattern() const OVERRIDE { |
| 398 return chromeos::NetworkTypePattern::NonVirtual(); |
| 399 } |
| 400 |
| 401 virtual void UpdateViewForNetwork(views::View* view, |
| 402 const ui::NetworkInfo& info) OVERRIDE { |
| 403 static_cast<NetworkRow*>(view)->Update(info); |
| 404 } |
| 405 |
| 406 virtual views::Label* CreateInfoLabel() OVERRIDE { |
| 407 views::Label* label = new views::Label(); |
| 408 return label; |
| 409 } |
| 410 |
| 411 virtual void RelayoutScrollList() OVERRIDE { scroller_->Layout(); } |
| 412 |
| 413 // chromeos::NetworkStateHandlerObserver: |
| 414 virtual void NetworkListChanged() OVERRIDE { UpdateNetworkList(); } |
| 415 |
| 416 virtual void DeviceListChanged() OVERRIDE {} |
| 417 |
| 418 virtual void DefaultNetworkChanged( |
| 419 const chromeos::NetworkState* network) OVERRIDE {} |
| 420 |
| 421 virtual void NetworkConnectionStateChanged( |
| 422 const chromeos::NetworkState* network) OVERRIDE {} |
| 423 |
| 424 virtual void NetworkPropertiesUpdated( |
| 425 const chromeos::NetworkState* network) OVERRIDE {} |
| 426 |
| 427 // ui::EventHandler: |
| 428 virtual void OnMouseEvent(ui::MouseEvent* mouse) OVERRIDE { |
| 429 CHECK_EQ(background_view_, mouse->target()); |
| 430 if (mouse->type() == ui::ET_MOUSE_PRESSED && !mouse->handled()) { |
| 431 Close(); |
| 432 mouse->SetHandled(); |
| 433 } |
| 434 } |
| 435 |
| 436 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE { |
| 437 CHECK_EQ(background_view_, gesture->target()); |
| 438 if (gesture->type() == ui::ET_GESTURE_TAP && !gesture->handled()) { |
| 439 Close(); |
| 440 gesture->SetHandled(); |
| 441 } |
| 442 } |
| 443 |
| 444 scoped_ptr<views::Widget> widget_; |
| 445 views::View* background_view_; |
| 446 views::View* scroll_content_; |
| 447 views::ScrollView* scroller_; |
| 448 |
| 449 views::View* connect_; |
| 450 |
| 451 ui::NetworkListView network_list_; |
| 452 |
| 453 DISALLOW_COPY_AND_ASSIGN(NetworkSelector); |
| 454 }; |
| 455 |
| 456 } // namespace |
| 457 |
| 458 namespace debug { |
| 459 |
| 460 void CreateNetworkSelector(aura::Window* container) { |
| 461 new NetworkSelector(container); |
| 462 } |
| 463 |
| 464 } // namespace debug |
OLD | NEW |