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

Side by Side Diff: ash/system/network/vpn_list_view.cc

Issue 2853143002: Simplify code for VPN entries in system menu (Closed)
Patch Set: Removed unnecessary return Created 3 years, 7 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
« no previous file with comments | « ash/system/network/vpn_list_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/system/network/vpn_list_view.h" 5 #include "ash/system/network/vpn_list_view.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "ash/ash_view_ids.h" 11 #include "ash/ash_view_ids.h"
12 #include "ash/resources/vector_icons/vector_icons.h" 12 #include "ash/resources/vector_icons/vector_icons.h"
13 #include "ash/shell.h" 13 #include "ash/shell.h"
14 #include "ash/shell_port.h" 14 #include "ash/shell_port.h"
15 #include "ash/strings/grit/ash_strings.h" 15 #include "ash/strings/grit/ash_strings.h"
16 #include "ash/system/network/network_icon.h" 16 #include "ash/system/network/network_icon.h"
17 #include "ash/system/network/network_icon_animation.h" 17 #include "ash/system/network/network_icon_animation.h"
18 #include "ash/system/network/network_icon_animation_observer.h" 18 #include "ash/system/network/network_icon_animation_observer.h"
19 #include "ash/system/network/network_state_list_detailed_view.h" 19 #include "ash/system/network/network_state_list_detailed_view.h"
20 #include "ash/system/network/vpn_list.h" 20 #include "ash/system/network/vpn_list.h"
21 #include "ash/system/tray/hover_highlight_view.h" 21 #include "ash/system/tray/hover_highlight_view.h"
22 #include "ash/system/tray/system_menu_button.h" 22 #include "ash/system/tray/system_menu_button.h"
23 #include "ash/system/tray/system_tray_controller.h" 23 #include "ash/system/tray/system_tray_controller.h"
24 #include "ash/system/tray/throbber_view.h" 24 #include "ash/system/tray/throbber_view.h"
25 #include "ash/system/tray/tray_constants.h" 25 #include "ash/system/tray/tray_constants.h"
26 #include "ash/system/tray/tray_popup_utils.h" 26 #include "ash/system/tray/tray_popup_utils.h"
27 #include "ash/system/tray/tri_view.h" 27 #include "ash/system/tray/tri_view.h"
28 #include "ash/system/tray/view_click_listener.h"
28 #include "base/bind.h" 29 #include "base/bind.h"
29 #include "base/bind_helpers.h" 30 #include "base/bind_helpers.h"
30 #include "base/logging.h" 31 #include "base/logging.h"
31 #include "base/strings/utf_string_conversions.h" 32 #include "base/strings/utf_string_conversions.h"
32 #include "base/values.h" 33 #include "base/values.h"
33 #include "chromeos/network/network_connect.h" 34 #include "chromeos/network/network_connect.h"
34 #include "chromeos/network/network_handler.h" 35 #include "chromeos/network/network_handler.h"
35 #include "chromeos/network/network_state.h" 36 #include "chromeos/network/network_state.h"
36 #include "chromeos/network/network_type_pattern.h" 37 #include "chromeos/network/network_type_pattern.h"
37 #include "third_party/cros_system_api/dbus/service_constants.h" 38 #include "third_party/cros_system_api/dbus/service_constants.h"
(...skipping 22 matching lines...) Expand all
60 return false; 61 return false;
61 const bool network_uses_third_party_provider = 62 const bool network_uses_third_party_provider =
62 network.vpn_provider_type() == shill::kProviderThirdPartyVpn; 63 network.vpn_provider_type() == shill::kProviderThirdPartyVpn;
63 if (!provider.third_party) 64 if (!provider.third_party)
64 return !network_uses_third_party_provider; 65 return !network_uses_third_party_provider;
65 return network_uses_third_party_provider && 66 return network_uses_third_party_provider &&
66 network.third_party_vpn_provider_extension_id() == 67 network.third_party_vpn_provider_extension_id() ==
67 provider.extension_id; 68 provider.extension_id;
68 } 69 }
69 70
70 // The base class of all list entries, a |HoverHighlightView| with no border.
71 class VPNListEntryBase : public HoverHighlightView {
72 public:
73 // When the user clicks the entry, the |parent|'s OnViewClicked() will be
74 // invoked.
75 explicit VPNListEntryBase(VPNListView* parent);
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(VPNListEntryBase);
79 };
80
81 // A list entry that represents a VPN provider. 71 // A list entry that represents a VPN provider.
82 class VPNListProviderEntry : public views::ButtonListener, public views::View { 72 class VPNListProviderEntry : public views::ButtonListener, public views::View {
83 public: 73 public:
84 VPNListProviderEntry(ViewClickListener* parent, 74 VPNListProviderEntry(const VPNProvider& vpn_provider,
85 bool top_item, 75 bool top_item,
86 const std::string& name, 76 const std::string& name,
87 int button_accessible_name_id) 77 int button_accessible_name_id)
88 : parent_(parent) { 78 : vpn_provider_(vpn_provider) {
89 TrayPopupUtils::ConfigureAsStickyHeader(this); 79 TrayPopupUtils::ConfigureAsStickyHeader(this);
90 SetLayoutManager(new views::FillLayout); 80 SetLayoutManager(new views::FillLayout);
91 TriView* tri_view = TrayPopupUtils::CreateSubHeaderRowView(false); 81 TriView* tri_view = TrayPopupUtils::CreateSubHeaderRowView(false);
92 AddChildView(tri_view); 82 AddChildView(tri_view);
93 83
94 views::Label* label = TrayPopupUtils::CreateDefaultLabel(); 84 views::Label* label = TrayPopupUtils::CreateDefaultLabel();
95 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SUB_HEADER); 85 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SUB_HEADER);
96 style.SetupLabel(label); 86 style.SetupLabel(label);
97 label->SetText(base::ASCIIToUTF16(name)); 87 label->SetText(base::ASCIIToUTF16(name));
98 tri_view->AddView(TriView::Container::CENTER, label); 88 tri_view->AddView(TriView::Container::CENTER, label);
99 89
100 const SkColor image_color = GetNativeTheme()->GetSystemColor( 90 const SkColor image_color = GetNativeTheme()->GetSystemColor(
101 ui::NativeTheme::kColorId_ProminentButtonColor); 91 ui::NativeTheme::kColorId_ProminentButtonColor);
102 gfx::ImageSkia icon = 92 gfx::ImageSkia icon =
103 gfx::CreateVectorIcon(kSystemMenuAddConnectionIcon, image_color); 93 gfx::CreateVectorIcon(kSystemMenuAddConnectionIcon, image_color);
104 SystemMenuButton* add_vpn_button = 94 SystemMenuButton* add_vpn_button =
105 new SystemMenuButton(this, TrayPopupInkDropStyle::HOST_CENTERED, icon, 95 new SystemMenuButton(this, TrayPopupInkDropStyle::HOST_CENTERED, icon,
106 icon, button_accessible_name_id); 96 icon, button_accessible_name_id);
107 add_vpn_button->SetInkDropColor(image_color); 97 add_vpn_button->SetInkDropColor(image_color);
108 add_vpn_button->SetEnabled(true); 98 add_vpn_button->SetEnabled(true);
109 tri_view->AddView(TriView::Container::END, add_vpn_button); 99 tri_view->AddView(TriView::Container::END, add_vpn_button);
110 } 100 }
111 101
112 protected: 102 protected:
113 // views::ButtonListener: 103 // views::ButtonListener:
114 void ButtonPressed(views::Button* sender, const ui::Event& event) override { 104 void ButtonPressed(views::Button* sender, const ui::Event& event) override {
115 parent_->OnViewClicked(this); 105 // If the user clicks on a provider entry, request that the "add network"
106 // dialog for this provider be shown.
107 if (vpn_provider_.third_party) {
108 ShellPort::Get()->RecordUserMetricsAction(
109 UMA_STATUS_AREA_VPN_ADD_THIRD_PARTY_CLICKED);
110 Shell::Get()->system_tray_controller()->ShowThirdPartyVpnCreate(
111 vpn_provider_.extension_id);
112 } else {
113 ShellPort::Get()->RecordUserMetricsAction(
114 UMA_STATUS_AREA_VPN_ADD_BUILT_IN_CLICKED);
115 Shell::Get()->system_tray_controller()->ShowNetworkCreate(
116 shill::kTypeVPN);
117 }
116 } 118 }
117 119
118 private: 120 private:
119 // Our parent to handle events. 121 const VPNProvider vpn_provider_;
120 ViewClickListener* parent_;
121 122
122 DISALLOW_COPY_AND_ASSIGN(VPNListProviderEntry); 123 DISALLOW_COPY_AND_ASSIGN(VPNListProviderEntry);
123 }; 124 };
124 125
125 // A list entry that represents a network. If the network is currently 126 // A list entry that represents a network. If the network is currently
126 // connecting, the icon shown by this list entry will be animated. If the 127 // connecting, the icon shown by this list entry will be animated. If the
127 // network is currently connected, a disconnect button will be shown next to its 128 // network is currently connected, a disconnect button will be shown next to its
128 // name. 129 // name.
129 class VPNListNetworkEntry : public VPNListEntryBase, 130 class VPNListNetworkEntry : public HoverHighlightView,
130 public network_icon::AnimationObserver { 131 public network_icon::AnimationObserver {
131 public: 132 public:
132 VPNListNetworkEntry(VPNListView* parent, 133 VPNListNetworkEntry(ViewClickListener* listener,
133 const chromeos::NetworkState* network); 134 const chromeos::NetworkState* network);
134 ~VPNListNetworkEntry() override; 135 ~VPNListNetworkEntry() override;
135 136
136 // network_icon::AnimationObserver: 137 // network_icon::AnimationObserver:
137 void NetworkIconChanged() override; 138 void NetworkIconChanged() override;
138 139
139 // views::ButtonListener: 140 // views::ButtonListener:
140 void ButtonPressed(Button* sender, const ui::Event& event) override; 141 void ButtonPressed(Button* sender, const ui::Event& event) override;
141 142
142 private: 143 private:
143 void UpdateFromNetworkState(const chromeos::NetworkState* network); 144 void UpdateFromNetworkState(const chromeos::NetworkState* network);
144 void SetupConnectedItem(const base::string16& text, 145 void SetupConnectedItem(const base::string16& text,
145 const gfx::ImageSkia& image); 146 const gfx::ImageSkia& image);
146 void SetupConnectingItem(const base::string16& text, 147 void SetupConnectingItem(const base::string16& text,
147 const gfx::ImageSkia& image); 148 const gfx::ImageSkia& image);
148 149
149 const std::string guid_; 150 const std::string guid_;
150 151
151 views::LabelButton* disconnect_button_ = nullptr; 152 views::LabelButton* disconnect_button_ = nullptr;
152 153
153 DISALLOW_COPY_AND_ASSIGN(VPNListNetworkEntry); 154 DISALLOW_COPY_AND_ASSIGN(VPNListNetworkEntry);
154 }; 155 };
155 156
156 VPNListEntryBase::VPNListEntryBase(VPNListView* parent) 157 VPNListNetworkEntry::VPNListNetworkEntry(ViewClickListener* listener,
157 : HoverHighlightView(parent) {}
158
159 VPNListNetworkEntry::VPNListNetworkEntry(VPNListView* parent,
160 const chromeos::NetworkState* network) 158 const chromeos::NetworkState* network)
161 : VPNListEntryBase(parent), guid_(network->guid()) { 159 : HoverHighlightView(listener), guid_(network->guid()) {
162 UpdateFromNetworkState(network); 160 UpdateFromNetworkState(network);
163 } 161 }
164 162
165 VPNListNetworkEntry::~VPNListNetworkEntry() { 163 VPNListNetworkEntry::~VPNListNetworkEntry() {
166 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); 164 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
167 } 165 }
168 166
169 void VPNListNetworkEntry::NetworkIconChanged() { 167 void VPNListNetworkEntry::NetworkIconChanged() {
170 UpdateFromNetworkState(chromeos::NetworkHandler::Get() 168 UpdateFromNetworkState(chromeos::NetworkHandler::Get()
171 ->network_state_handler() 169 ->network_state_handler()
172 ->GetNetworkStateFromGuid(guid_)); 170 ->GetNetworkStateFromGuid(guid_));
173 } 171 }
174 172
175 void VPNListNetworkEntry::ButtonPressed(Button* sender, 173 void VPNListNetworkEntry::ButtonPressed(Button* sender,
176 const ui::Event& event) { 174 const ui::Event& event) {
177 if (sender != disconnect_button_) { 175 if (sender != disconnect_button_) {
178 VPNListEntryBase::ButtonPressed(sender, event); 176 HoverHighlightView::ButtonPressed(sender, event);
179 return; 177 return;
180 } 178 }
181 179
182 chromeos::NetworkConnect::Get()->DisconnectFromNetworkId(guid_); 180 chromeos::NetworkConnect::Get()->DisconnectFromNetworkId(guid_);
183 } 181 }
184 182
185 void VPNListNetworkEntry::UpdateFromNetworkState( 183 void VPNListNetworkEntry::UpdateFromNetworkState(
186 const chromeos::NetworkState* network) { 184 const chromeos::NetworkState* network) {
187 if (network && network->IsConnectingState()) 185 if (network && network->IsConnectingState())
188 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this); 186 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 if (entry == network_view_guid_map_.end()) 328 if (entry == network_view_guid_map_.end())
331 return false; 329 return false;
332 *guid = entry->second; 330 *guid = entry->second;
333 return true; 331 return true;
334 } 332 }
335 333
336 void VPNListView::OnVPNProvidersChanged() { 334 void VPNListView::OnVPNProvidersChanged() {
337 Update(); 335 Update();
338 } 336 }
339 337
340 void VPNListView::OnViewClicked(views::View* sender) {
341 const auto& provider_iter = provider_view_map_.find(sender);
342 if (provider_iter != provider_view_map_.end()) {
343 // If the user clicks on a provider entry, request that the "add network"
344 // dialog for this provider be shown.
345 const VPNProvider& provider = provider_iter->second;
346 if (provider.third_party) {
347 ShellPort::Get()->RecordUserMetricsAction(
348 UMA_STATUS_AREA_VPN_ADD_THIRD_PARTY_CLICKED);
349 Shell::Get()->system_tray_controller()->ShowThirdPartyVpnCreate(
350 provider.extension_id);
351 } else {
352 ShellPort::Get()->RecordUserMetricsAction(
353 UMA_STATUS_AREA_VPN_ADD_BUILT_IN_CLICKED);
354 Shell::Get()->system_tray_controller()->ShowNetworkCreate(
355 shill::kTypeVPN);
356 }
357 return;
358 }
359
360 // If the user clicked on a network entry, let the |delegate_| trigger a
361 // connection attempt (if the network is currently disconnected) or show a
362 // configuration dialog (if the network is currently connected or connecting).
363 detailed_view()->OnNetworkEntryClicked(sender);
364 }
365
366 void VPNListView::AddNetwork(const chromeos::NetworkState* network) { 338 void VPNListView::AddNetwork(const chromeos::NetworkState* network) {
367 views::View* entry(new VPNListNetworkEntry(this, network)); 339 views::View* entry(new VPNListNetworkEntry(detailed_view(), network));
368 container()->AddChildView(entry); 340 container()->AddChildView(entry);
369 network_view_guid_map_[entry] = network->guid(); 341 network_view_guid_map_[entry] = network->guid();
370 list_empty_ = false; 342 list_empty_ = false;
371 } 343 }
372 344
373 void VPNListView::AddProviderAndNetworks( 345 void VPNListView::AddProviderAndNetworks(
374 const VPNProvider& vpn_provider, 346 const VPNProvider& vpn_provider,
375 const chromeos::NetworkStateHandler::NetworkStateList& networks) { 347 const chromeos::NetworkStateHandler::NetworkStateList& networks) {
376 // Add a visual separator, unless this is the topmost entry in the list. 348 // Add a visual separator, unless this is the topmost entry in the list.
377 if (!list_empty_) 349 if (!list_empty_)
378 container()->AddChildView(TrayPopupUtils::CreateListSubHeaderSeparator()); 350 container()->AddChildView(TrayPopupUtils::CreateListSubHeaderSeparator());
379 std::string vpn_name = 351 std::string vpn_name =
380 vpn_provider.third_party 352 vpn_provider.third_party
381 ? vpn_provider.third_party_provider_name 353 ? vpn_provider.third_party_provider_name
382 : l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_VPN_BUILT_IN_PROVIDER); 354 : l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_VPN_BUILT_IN_PROVIDER);
383 355
384 // Add a list entry for the VPN provider. 356 // Add a list entry for the VPN provider.
385 views::View* provider_view = nullptr; 357 views::View* provider_view = nullptr;
386 provider_view = new VPNListProviderEntry(this, list_empty_, vpn_name, 358 provider_view = new VPNListProviderEntry(vpn_provider, list_empty_, vpn_name,
387 IDS_ASH_STATUS_TRAY_ADD_CONNECTION); 359 IDS_ASH_STATUS_TRAY_ADD_CONNECTION);
388 container()->AddChildView(provider_view); 360 container()->AddChildView(provider_view);
389 provider_view_map_[provider_view] = vpn_provider; 361 provider_view_map_[provider_view] = vpn_provider;
390 list_empty_ = false; 362 list_empty_ = false;
391 // Add the networks belonging to this provider, in the priority order returned 363 // Add the networks belonging to this provider, in the priority order returned
392 // by shill. 364 // by shill.
393 for (const chromeos::NetworkState* const& network : networks) { 365 for (const chromeos::NetworkState* const& network : networks) {
394 if (VpnProviderMatchesNetwork(vpn_provider, *network)) 366 if (VpnProviderMatchesNetwork(vpn_provider, *network))
395 AddNetwork(network); 367 AddNetwork(network);
396 } 368 }
(...skipping 19 matching lines...) Expand all
416 } 388 }
417 } 389 }
418 390
419 // Add providers without any configured networks, in the order that the 391 // Add providers without any configured networks, in the order that the
420 // providers were returned by the extensions system. 392 // providers were returned by the extensions system.
421 for (const VPNProvider& provider : providers) 393 for (const VPNProvider& provider : providers)
422 AddProviderAndNetworks(provider, networks); 394 AddProviderAndNetworks(provider, networks);
423 } 395 }
424 396
425 } // namespace ash 397 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/network/vpn_list_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698