Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1188)

Side by Side Diff: athena/main/debug/network_selector.cc

Issue 425783003: athena: Add a network-selector widget. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « athena/main/debug/network_selector.h ('k') | athena/resources/athena_resources.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
51 // The View for the user to enter the password for connceting to a network. This
52 // view also shows an error message if the network connection fails.
53 class PasswordView : public views::View, public views::ButtonListener {
54 public:
55 PasswordView(const ui::NetworkInfo& network,
56 const base::Callback<void(bool)>& callback,
57 views::View* parent_container)
58 : network_(network),
59 callback_(callback),
60 parent_container_(parent_container),
61 connect_(NULL),
62 cancel_(NULL),
63 textfield_(NULL),
64 error_msg_(NULL),
65 weak_ptr_(this) {
66 const int kHorizontal = 5;
67 const int kVertical = 0;
68 const int kPadding = 0;
69
70 views::BoxLayout* layout = new views::BoxLayout(
71 views::BoxLayout::kVertical, kHorizontal, kVertical, kPadding);
72 layout->set_main_axis_alignment(
73 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
74 layout->set_cross_axis_alignment(
75 views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
76 SetLayoutManager(layout);
77
78 views::View* container = new views::View;
79 layout = new views::BoxLayout(
80 views::BoxLayout::kHorizontal, kHorizontal, kVertical, kPadding);
81 layout->set_main_axis_alignment(
82 views::BoxLayout::MAIN_AXIS_ALIGNMENT_START);
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 InvalidateLayout();
124 parent_container_->Layout();
125 }
126 connect_->SetEnabled(true);
127 }
128
129 void OnSetProfileSucceed(const base::string16& password) {
130 base::DictionaryValue properties;
131 properties.SetStringWithoutPathExpansion(shill::kPassphraseProperty,
132 textfield_->text());
133 NetworkHandler::Get()->network_configuration_handler()->SetProperties(
134 network_.service_path,
135 properties,
136 base::Bind(&PasswordView::OnSetPropertiesSucceed,
137 weak_ptr_.GetWeakPtr()),
138 base::Bind(&PasswordView::OnKnownError, weak_ptr_.GetWeakPtr()));
139 }
140
141 void OnSetPropertiesSucceed() {
142 const bool check_error_state = false;
143 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
144 network_.service_path,
145 base::Bind(&PasswordView::OnConnectionSucceed, weak_ptr_.GetWeakPtr()),
146 base::Bind(&PasswordView::OnKnownError, weak_ptr_.GetWeakPtr()),
147 check_error_state);
148 }
149
150 void OnConnectionSucceed() { Close(true); }
151
152 // views::ButtonListener:
153 virtual void ButtonPressed(views::Button* sender,
154 const ui::Event& event) OVERRIDE {
155 if (sender == connect_) {
156 if (error_msg_) {
157 RemoveChildView(error_msg_);
158 delete error_msg_;
159 error_msg_ = NULL;
160 }
161 connect_->SetEnabled(false);
162 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile(
163 network_.service_path,
164 NetworkProfileHandler::GetSharedProfilePath(),
165 base::Bind(&PasswordView::OnSetProfileSucceed,
166 weak_ptr_.GetWeakPtr(),
167 textfield_->text()),
168 base::Bind(&PasswordView::OnKnownError, weak_ptr_.GetWeakPtr()));
169 } else if (sender == cancel_) {
170 views::View* parent_view = parent();
171 Close(false);
172 parent_view->Layout();
173 } else {
174 NOTREACHED();
175 }
176 }
177
178 ui::NetworkInfo network_;
179 base::Callback<void(bool)> callback_;
180 views::View* parent_container_;
181
182 views::BlueButton* connect_;
183 views::LabelButton* cancel_;
184 views::Textfield* textfield_;
185 views::Label* error_msg_;
186 base::WeakPtrFactory<PasswordView> weak_ptr_;
187
188 DISALLOW_COPY_AND_ASSIGN(PasswordView);
189 };
190
191 // A View that represents a single row in the network list. This row also
192 // contains the View for taking password for password-protected networks.
193 class NetworkRow : public views::View {
194 public:
195 NetworkRow(const ui::NetworkInfo& network, views::View* container)
196 : network_(network), container_(container), weak_ptr_(this) {
197 SetBorder(views::Border::CreateEmptyBorder(10, 5, 10, 5));
198 SetLayoutManager(
199 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 10));
200 Update(network);
201 }
202
203 virtual ~NetworkRow() {}
204
205 void Update(const ui::NetworkInfo& network) {
206 network_ = network;
207 views::ImageView* icon = new views::ImageView();
208 icon->SetImage(network.image);
209 icon->SetBounds(0, 0, network.image.width(), network.image.height());
210
211 views::Label* label = new views::Label(network.label);
212 if (network.highlight)
213 label->SetFontList(label->font_list().Derive(0, gfx::Font::BOLD));
214 AddChildView(icon);
215 AddChildView(label);
216 if (password_view_)
217 AddChildView(password_view_.get());
218 }
219
220 private:
221 void OnPasswordComplete(bool successful) {
222 password_view_.reset();
223 InvalidateLayout();
224 container_->Layout();
225 }
226
227 void ShowPasswordView(const std::string& service_path) {
228 const NetworkState* network =
229 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
230 service_path);
231 if (!network)
232 return;
233
234 // If this is not a wifi network that needs a password, then ignore.
235 if (network->type() != shill::kTypeWifi ||
236 network->security() == shill::kSecurityNone) {
237 return;
238 }
239
240 password_view_.reset(new PasswordView(
241 network_,
242 base::Bind(&NetworkRow::OnPasswordComplete, weak_ptr_.GetWeakPtr()),
243 container_));
244 password_view_->set_owned_by_client();
245 AddChildView(password_view_.get());
246 PreferredSizeChanged();
247 container_->Layout();
248 }
249
250 void OnNetworkConnectionError(const std::string& service_path,
251 const std::string& error_name,
252 scoped_ptr<base::DictionaryValue> error_data) {
253 if (error_name == NetworkConnectionHandler::kErrorConnectCanceled)
254 return;
255 if (error_name == shill::kErrorBadPassphrase ||
256 error_name == NetworkConnectionHandler::kErrorPassphraseRequired ||
257 error_name == NetworkConnectionHandler::kErrorConfigurationRequired ||
258 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) {
259 ShowPasswordView(service_path);
260 }
261 }
262
263 void ActivateNetwork() {
264 const chromeos::NetworkState* network =
265 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
266 network_.service_path);
267 if (!network)
268 return;
269 if (network->IsConnectedState()) {
270 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
271 network_.service_path,
272 base::Closure(),
273 chromeos::network_handler::ErrorCallback());
274 } else if (!network->IsConnectingState()) {
275 // |network| is not connected, and not already trying to connect.
276 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
277 network_.service_path,
278 base::Closure(),
279 base::Bind(&NetworkRow::OnNetworkConnectionError,
280 weak_ptr_.GetWeakPtr(),
281 network_.service_path),
282 false);
283 }
284 }
285
286 // views::View:
287 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
288 if (event->type() != ui::ET_MOUSE_PRESSED)
289 return;
290 ActivateNetwork();
291 event->SetHandled();
292 }
293
294 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
295 if (gesture->type() != ui::ET_GESTURE_TAP)
296 return;
297 ActivateNetwork();
298 gesture->SetHandled();
299 }
300
301 ui::NetworkInfo network_;
302 views::View* container_;
303 base::WeakPtrFactory<NetworkRow> weak_ptr_;
304 scoped_ptr<views::View> password_view_;
305
306 DISALLOW_COPY_AND_ASSIGN(NetworkRow);
307 };
308
309 class NetworkSelector : public ui::NetworkListDelegate,
310 public chromeos::NetworkStateHandlerObserver,
311 public ui::EventHandler {
312 public:
313 explicit NetworkSelector(aura::Window* container)
314 : background_view_(NULL),
315 scroll_content_(NULL),
316 scroller_(NULL),
317 network_list_(this) {
318 CreateWidget(container);
319 CreateNetworkList();
320
321 NetworkHandler::Get()->network_state_handler()->RequestScan();
322 NetworkHandler::Get()->network_state_handler()->AddObserver(this,
323 FROM_HERE);
324 }
325
326 virtual ~NetworkSelector() {
327 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
328 FROM_HERE);
329 }
330
331 private:
332 void CreateWidget(aura::Window* container) {
333 views::Widget::InitParams params;
334 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
335 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
336 params.activatable = views::Widget::InitParams::ACTIVATABLE_DEFAULT;
337 params.accept_events = true;
338 params.bounds = gfx::Rect(container->bounds().size());
339 params.parent = container;
340 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
341 widget_.reset(new views::Widget());
342 widget_->Init(params);
343 widget_->Show();
344
345 background_view_ = new views::View;
346 background_view_->set_background(
347 views::Background::CreateSolidBackground(kBackgroundColor));
348 background_view_->SetBorder(
349 views::Border::CreateEmptyBorder(100, 300, 300, 300));
350 background_view_->SetLayoutManager(new views::FillLayout());
351 background_view_->set_target_handler(this);
352
353 widget_->SetContentsView(background_view_);
354 }
355
356 void CreateNetworkList() {
357 const int kListHeight = 500;
358 scroller_ = new views::ScrollView();
359 scroller_->set_background(
360 views::Background::CreateSolidBackground(SK_ColorWHITE));
361 scroller_->SetBounds(0, 0, 400, kListHeight);
362
363 scroll_content_ = new views::View;
364 scroll_content_->SetLayoutManager(
365 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
366 scroller_->SetContents(scroll_content_);
367
368 scroller_->ClipHeightTo(kListHeight, kListHeight);
369 background_view_->AddChildView(scroller_);
370
371 background_view_->Layout();
372
373 network_list_.set_content_view(scroll_content_);
374 }
375
376 void UpdateNetworkList() { network_list_.UpdateNetworkList(); }
377
378 void Close() { delete this; }
379
380 // ui::NetworkListDelegate:
381 virtual views::View* CreateViewForNetwork(
382 const ui::NetworkInfo& info) OVERRIDE {
383 return new NetworkRow(info, background_view_);
384 }
385
386 virtual bool IsViewHovered(views::View* view) OVERRIDE { return false; }
387
388 virtual chromeos::NetworkTypePattern GetNetworkTypePattern() const OVERRIDE {
389 return chromeos::NetworkTypePattern::NonVirtual();
390 }
391
392 virtual void UpdateViewForNetwork(views::View* view,
393 const ui::NetworkInfo& info) OVERRIDE {
394 static_cast<NetworkRow*>(view)->Update(info);
395 }
396
397 virtual views::Label* CreateInfoLabel() OVERRIDE {
398 views::Label* label = new views::Label();
399 return label;
400 }
401
402 virtual void RelayoutScrollList() OVERRIDE { scroller_->Layout(); }
403
404 // chromeos::NetworkStateHandlerObserver:
405 virtual void NetworkListChanged() OVERRIDE { UpdateNetworkList(); }
406
407 virtual void DeviceListChanged() OVERRIDE {}
408
409 virtual void DefaultNetworkChanged(
410 const chromeos::NetworkState* network) OVERRIDE {}
411
412 virtual void NetworkConnectionStateChanged(
413 const chromeos::NetworkState* network) OVERRIDE {}
414
415 virtual void NetworkPropertiesUpdated(
416 const chromeos::NetworkState* network) OVERRIDE {}
417
418 // ui::EventHandler:
419 virtual void OnMouseEvent(ui::MouseEvent* mouse) OVERRIDE {
420 CHECK_EQ(background_view_, mouse->target());
421 if (mouse->type() == ui::ET_MOUSE_PRESSED && !mouse->handled()) {
422 Close();
423 mouse->SetHandled();
424 }
425 }
426
427 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
428 CHECK_EQ(background_view_, gesture->target());
429 if (gesture->type() == ui::ET_GESTURE_TAP && !gesture->handled()) {
430 Close();
431 gesture->SetHandled();
432 }
433 }
434
435 scoped_ptr<views::Widget> widget_;
436 views::View* background_view_;
437 views::View* scroll_content_;
438 views::ScrollView* scroller_;
439
440 views::View* connect_;
441
442 ui::NetworkListView network_list_;
443
444 DISALLOW_COPY_AND_ASSIGN(NetworkSelector);
445 };
446
447 } // namespace
448
449 namespace debug {
450
451 void CreateNetworkSelector(aura::Window* container) {
452 new NetworkSelector(container);
453 }
454
455 } // namespace debug
OLDNEW
« no previous file with comments | « athena/main/debug/network_selector.h ('k') | athena/resources/athena_resources.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698