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