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

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 Layout();
248 parent()->Layout();
249 container_->Layout();
oshima 2014/07/29 07:33:43 Isn't this enough? (PreferredSizeChanged calls Inv
sadrul 2014/07/29 09:25:15 Done.
250 }
251
252 void OnNetworkConnectionError(const std::string& service_path,
253 const std::string& error_name,
254 scoped_ptr<base::DictionaryValue> error_data) {
255 if (error_name == NetworkConnectionHandler::kErrorConnectCanceled)
256 return;
257 if (error_name == shill::kErrorBadPassphrase ||
258 error_name == NetworkConnectionHandler::kErrorPassphraseRequired ||
259 error_name == NetworkConnectionHandler::kErrorConfigurationRequired ||
260 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) {
261 ShowPasswordView(service_path);
262 }
263 }
264
265 void ActivateNetwork() {
266 const chromeos::NetworkState* network =
267 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
268 network_.service_path);
269 if (!network)
270 return;
271 if (network->IsConnectedState()) {
272 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
273 network_.service_path,
274 base::Closure(),
275 chromeos::network_handler::ErrorCallback());
276 } else if (!network->IsConnectingState()) {
277 // |network| is not connected, and not already trying to connect.
278 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
279 network_.service_path,
280 base::Closure(),
281 base::Bind(&NetworkRow::OnNetworkConnectionError,
282 weak_ptr_.GetWeakPtr(),
283 network_.service_path),
284 false);
285 }
286 }
287
288 // views::View:
289 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
290 if (event->type() != ui::ET_MOUSE_PRESSED)
291 return;
292 ActivateNetwork();
293 event->SetHandled();
294 }
295
296 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
297 if (gesture->type() != ui::ET_GESTURE_TAP)
298 return;
299 ActivateNetwork();
300 gesture->SetHandled();
301 }
302
303 ui::NetworkInfo network_;
304 views::View* container_;
305 base::WeakPtrFactory<NetworkRow> weak_ptr_;
306 scoped_ptr<views::View> password_view_;
307
308 DISALLOW_COPY_AND_ASSIGN(NetworkRow);
309 };
310
311 class NetworkSelector : public ui::NetworkListDelegate,
312 public chromeos::NetworkStateHandlerObserver,
313 public ui::EventHandler {
314 public:
315 explicit NetworkSelector(aura::Window* container)
316 : background_view_(NULL),
317 scroll_content_(NULL),
318 scroller_(NULL),
319 network_list_(this) {
320 CreateWidget(container);
321 CreateNetworkList();
322
323 NetworkHandler::Get()->network_state_handler()->RequestScan();
324 NetworkHandler::Get()->network_state_handler()->AddObserver(this,
325 FROM_HERE);
326 }
327
328 virtual ~NetworkSelector() {
329 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
330 FROM_HERE);
331 }
332
333 private:
334 void CreateWidget(aura::Window* container) {
335 views::Widget::InitParams params;
336 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
337 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
338 params.activatable = views::Widget::InitParams::ACTIVATABLE_DEFAULT;
339 params.accept_events = true;
340 params.bounds = gfx::Rect(container->bounds().size());
341 params.parent = container;
342 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
343 widget_.reset(new views::Widget());
344 widget_->Init(params);
345 widget_->Show();
346
347 background_view_ = new views::View;
348 background_view_->set_background(
349 views::Background::CreateSolidBackground(kBackgroundColor));
350 background_view_->SetBorder(
351 views::Border::CreateEmptyBorder(100, 300, 100, 300));
352 background_view_->SetLayoutManager(new views::FillLayout());
353 background_view_->set_target_handler(this);
354
355 widget_->SetContentsView(background_view_);
356 }
357
358 void CreateNetworkList() {
359 const int kListHeight = 500;
360 scroller_ = new views::ScrollView();
361 scroller_->set_background(
362 views::Background::CreateSolidBackground(SK_ColorWHITE));
363 scroller_->SetBounds(0, 0, 400, kListHeight);
364
365 scroll_content_ = new views::View;
366 scroll_content_->SetLayoutManager(
367 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
368 scroller_->SetContents(scroll_content_);
369
370 scroller_->ClipHeightTo(kListHeight, kListHeight);
371 background_view_->AddChildView(scroller_);
372
373 background_view_->Layout();
374 scroller_->Layout();
375 scroll_content_->Layout();
376
377 network_list_.set_content_view(scroll_content_);
378 }
379
380 void UpdateNetworkList() { network_list_.UpdateNetworkList(); }
381
382 void Close() { delete this; }
383
384 // ui::NetworkListDelegate:
385 virtual views::View* CreateViewForNetwork(
386 const ui::NetworkInfo& info) OVERRIDE {
387 return new NetworkRow(info, background_view_);
388 }
389
390 virtual bool IsViewHovered(views::View* view) OVERRIDE { return false; }
391
392 virtual chromeos::NetworkTypePattern GetNetworkTypePattern() const OVERRIDE {
393 return chromeos::NetworkTypePattern::NonVirtual();
394 }
395
396 virtual void UpdateViewForNetwork(views::View* view,
397 const ui::NetworkInfo& info) OVERRIDE {
398 static_cast<NetworkRow*>(view)->Update(info);
399 }
400
401 virtual views::Label* CreateInfoLabel() OVERRIDE {
402 views::Label* label = new views::Label();
403 return label;
404 }
405
406 virtual void RelayoutScrollList() OVERRIDE { scroller_->Layout(); }
407
408 // chromeos::NetworkStateHandlerObserver:
409 virtual void NetworkListChanged() OVERRIDE { UpdateNetworkList(); }
410
411 virtual void DeviceListChanged() OVERRIDE {}
412
413 virtual void DefaultNetworkChanged(
414 const chromeos::NetworkState* network) OVERRIDE {}
415
416 virtual void NetworkConnectionStateChanged(
417 const chromeos::NetworkState* network) OVERRIDE {}
418
419 virtual void NetworkPropertiesUpdated(
420 const chromeos::NetworkState* network) OVERRIDE {}
421
422 // ui::EventHandler:
423 virtual void OnMouseEvent(ui::MouseEvent* mouse) OVERRIDE {
424 CHECK_EQ(background_view_, mouse->target());
425 if (mouse->type() == ui::ET_MOUSE_PRESSED && !mouse->handled()) {
426 Close();
427 mouse->SetHandled();
428 }
429 }
430
431 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE {
432 CHECK_EQ(background_view_, gesture->target());
433 if (gesture->type() == ui::ET_GESTURE_TAP && !gesture->handled()) {
434 Close();
435 gesture->SetHandled();
436 }
437 }
438
439 scoped_ptr<views::Widget> widget_;
440 views::View* background_view_;
441 views::View* scroll_content_;
442 views::ScrollView* scroller_;
443
444 views::View* connect_;
445
446 ui::NetworkListView network_list_;
447
448 DISALLOW_COPY_AND_ASSIGN(NetworkSelector);
449 };
450
451 } // namespace
452
453 namespace debug {
454
455 void CreateNetworkSelector(aura::Window* container) {
456 new NetworkSelector(container);
457 }
458
459 } // 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