| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/common/system/chromeos/network/vpn_list_view.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "ash/common/ash_view_ids.h" | |
| 12 #include "ash/common/material_design/material_design_controller.h" | |
| 13 #include "ash/common/system/chromeos/network/network_icon.h" | |
| 14 #include "ash/common/system/chromeos/network/network_icon_animation.h" | |
| 15 #include "ash/common/system/chromeos/network/network_icon_animation_observer.h" | |
| 16 #include "ash/common/system/chromeos/network/network_list_delegate.h" | |
| 17 #include "ash/common/system/chromeos/network/vpn_list.h" | |
| 18 #include "ash/common/system/tray/hover_highlight_view.h" | |
| 19 #include "ash/common/system/tray/system_menu_button.h" | |
| 20 #include "ash/common/system/tray/system_tray_controller.h" | |
| 21 #include "ash/common/system/tray/throbber_view.h" | |
| 22 #include "ash/common/system/tray/tray_constants.h" | |
| 23 #include "ash/common/system/tray/tray_popup_utils.h" | |
| 24 #include "ash/common/system/tray/tri_view.h" | |
| 25 #include "ash/common/wm_shell.h" | |
| 26 #include "ash/resources/vector_icons/vector_icons.h" | |
| 27 #include "ash/strings/grit/ash_strings.h" | |
| 28 #include "base/bind.h" | |
| 29 #include "base/bind_helpers.h" | |
| 30 #include "base/logging.h" | |
| 31 #include "base/strings/utf_string_conversions.h" | |
| 32 #include "base/values.h" | |
| 33 #include "chromeos/network/network_connection_handler.h" | |
| 34 #include "chromeos/network/network_handler.h" | |
| 35 #include "chromeos/network/network_state.h" | |
| 36 #include "chromeos/network/network_type_pattern.h" | |
| 37 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 38 #include "ui/base/l10n/l10n_util.h" | |
| 39 #include "ui/base/resource/resource_bundle.h" | |
| 40 #include "ui/gfx/geometry/rect.h" | |
| 41 #include "ui/gfx/image/image_skia.h" | |
| 42 #include "ui/gfx/paint_vector_icon.h" | |
| 43 #include "ui/gfx/text_constants.h" | |
| 44 #include "ui/views/border.h" | |
| 45 #include "ui/views/controls/button/button.h" | |
| 46 #include "ui/views/controls/button/label_button.h" | |
| 47 #include "ui/views/controls/label.h" | |
| 48 #include "ui/views/controls/separator.h" | |
| 49 #include "ui/views/layout/box_layout.h" | |
| 50 #include "ui/views/layout/fill_layout.h" | |
| 51 #include "ui/views/view.h" | |
| 52 | |
| 53 namespace ash { | |
| 54 | |
| 55 namespace { | |
| 56 | |
| 57 bool UseMd() { | |
| 58 return MaterialDesignController::IsSystemTrayMenuMaterial(); | |
| 59 } | |
| 60 | |
| 61 // Indicates whether |network| belongs to this VPN provider. | |
| 62 bool VpnProviderMatchesNetwork(const VPNProvider& provider, | |
| 63 const chromeos::NetworkState& network) { | |
| 64 if (network.type() != shill::kTypeVPN) | |
| 65 return false; | |
| 66 const bool network_uses_third_party_provider = | |
| 67 network.vpn_provider_type() == shill::kProviderThirdPartyVpn; | |
| 68 if (!provider.third_party) | |
| 69 return !network_uses_third_party_provider; | |
| 70 return network_uses_third_party_provider && | |
| 71 network.third_party_vpn_provider_extension_id() == | |
| 72 provider.extension_id; | |
| 73 } | |
| 74 | |
| 75 // The base class of all list entries, a |HoverHighlightView| with no border. | |
| 76 class VPNListEntryBase : public HoverHighlightView { | |
| 77 public: | |
| 78 // When the user clicks the entry, the |parent|'s OnViewClicked() will be | |
| 79 // invoked. | |
| 80 explicit VPNListEntryBase(VPNListView* parent); | |
| 81 | |
| 82 private: | |
| 83 DISALLOW_COPY_AND_ASSIGN(VPNListEntryBase); | |
| 84 }; | |
| 85 | |
| 86 // A list entry that represents a VPN provider. | |
| 87 class VPNListProviderEntry : public VPNListEntryBase { | |
| 88 public: | |
| 89 VPNListProviderEntry(VPNListView* parent, const std::string& name) | |
| 90 : VPNListEntryBase(parent) { | |
| 91 views::Label* const label = AddLabel( | |
| 92 base::UTF8ToUTF16(name), gfx::ALIGN_LEFT, false /* highlight */); | |
| 93 label->SetBorder(views::CreateEmptyBorder(5, 0, 5, 0)); | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 DISALLOW_COPY_AND_ASSIGN(VPNListProviderEntry); | |
| 98 }; | |
| 99 | |
| 100 // A list entry that represents a VPN provider with Material Design. | |
| 101 class VPNListProviderEntryMd : public views::ButtonListener, | |
| 102 public views::View { | |
| 103 public: | |
| 104 VPNListProviderEntryMd(ViewClickListener* parent, | |
| 105 bool top_item, | |
| 106 const std::string& name, | |
| 107 int button_accessible_name_id) | |
| 108 : parent_(parent) { | |
| 109 TrayPopupUtils::ConfigureAsStickyHeader(this); | |
| 110 SetLayoutManager(new views::FillLayout); | |
| 111 TriView* tri_view = TrayPopupUtils::CreateSubHeaderRowView(); | |
| 112 AddChildView(tri_view); | |
| 113 | |
| 114 views::Label* label = TrayPopupUtils::CreateDefaultLabel(); | |
| 115 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SUB_HEADER); | |
| 116 style.SetupLabel(label); | |
| 117 label->SetText(base::ASCIIToUTF16(name)); | |
| 118 tri_view->AddView(TriView::Container::CENTER, label); | |
| 119 | |
| 120 const SkColor image_color = GetNativeTheme()->GetSystemColor( | |
| 121 ui::NativeTheme::kColorId_ProminentButtonColor); | |
| 122 gfx::ImageSkia icon = | |
| 123 gfx::CreateVectorIcon(kSystemMenuAddConnectionIcon, image_color); | |
| 124 SystemMenuButton* add_vpn_button = | |
| 125 new SystemMenuButton(this, TrayPopupInkDropStyle::HOST_CENTERED, icon, | |
| 126 icon, button_accessible_name_id); | |
| 127 add_vpn_button->SetInkDropColor(image_color); | |
| 128 add_vpn_button->SetEnabled(true); | |
| 129 tri_view->AddView(TriView::Container::END, add_vpn_button); | |
| 130 } | |
| 131 | |
| 132 protected: | |
| 133 // views::ButtonListener: | |
| 134 void ButtonPressed(views::Button* sender, const ui::Event& event) override { | |
| 135 parent_->OnViewClicked(this); | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 // Our parent to handle events. | |
| 140 ViewClickListener* parent_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(VPNListProviderEntryMd); | |
| 143 }; | |
| 144 | |
| 145 // A list entry that represents a network. If the network is currently | |
| 146 // connecting, the icon shown by this list entry will be animated. If the | |
| 147 // network is currently connected, a disconnect button will be shown next to its | |
| 148 // name. | |
| 149 class VPNListNetworkEntry : public VPNListEntryBase, | |
| 150 public network_icon::AnimationObserver { | |
| 151 public: | |
| 152 VPNListNetworkEntry(VPNListView* parent, | |
| 153 const chromeos::NetworkState* network); | |
| 154 ~VPNListNetworkEntry() override; | |
| 155 | |
| 156 // network_icon::AnimationObserver: | |
| 157 void NetworkIconChanged() override; | |
| 158 | |
| 159 private: | |
| 160 void UpdateFromNetworkState(const chromeos::NetworkState* network); | |
| 161 void SetupConnectedItemMd(const base::string16& text, | |
| 162 const gfx::ImageSkia& image); | |
| 163 void SetupConnectingItemMd(const base::string16& text, | |
| 164 const gfx::ImageSkia& image); | |
| 165 | |
| 166 const std::string guid_; | |
| 167 | |
| 168 views::LabelButton* disconnect_button_ = nullptr; | |
| 169 | |
| 170 DISALLOW_COPY_AND_ASSIGN(VPNListNetworkEntry); | |
| 171 }; | |
| 172 | |
| 173 VPNListEntryBase::VPNListEntryBase(VPNListView* parent) | |
| 174 : HoverHighlightView(parent) { | |
| 175 if (!UseMd()) | |
| 176 SetBorder(views::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, 0, 0)); | |
| 177 } | |
| 178 | |
| 179 VPNListNetworkEntry::VPNListNetworkEntry(VPNListView* parent, | |
| 180 const chromeos::NetworkState* network) | |
| 181 : VPNListEntryBase(parent), guid_(network->guid()) { | |
| 182 UpdateFromNetworkState(network); | |
| 183 } | |
| 184 | |
| 185 VPNListNetworkEntry::~VPNListNetworkEntry() { | |
| 186 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); | |
| 187 } | |
| 188 | |
| 189 void VPNListNetworkEntry::NetworkIconChanged() { | |
| 190 UpdateFromNetworkState(chromeos::NetworkHandler::Get() | |
| 191 ->network_state_handler() | |
| 192 ->GetNetworkStateFromGuid(guid_)); | |
| 193 } | |
| 194 | |
| 195 void VPNListNetworkEntry::UpdateFromNetworkState( | |
| 196 const chromeos::NetworkState* network) { | |
| 197 if (network && network->IsConnectingState()) | |
| 198 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this); | |
| 199 else | |
| 200 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this); | |
| 201 | |
| 202 if (!network) { | |
| 203 // This is a transient state where the network has been removed already but | |
| 204 // the network list in the UI has not been updated yet. | |
| 205 return; | |
| 206 } | |
| 207 RemoveAllChildViews(true); | |
| 208 disconnect_button_ = nullptr; | |
| 209 | |
| 210 gfx::ImageSkia image = | |
| 211 network_icon::GetImageForNetwork(network, network_icon::ICON_TYPE_LIST); | |
| 212 base::string16 label = network_icon::GetLabelForNetwork( | |
| 213 network, UseMd() ? network_icon::ICON_TYPE_MENU_LIST | |
| 214 : network_icon::ICON_TYPE_LIST); | |
| 215 if (network->IsConnectedState()) | |
| 216 SetupConnectedItemMd(label, image); | |
| 217 else if (network->IsConnectingState()) | |
| 218 SetupConnectingItemMd(label, image); | |
| 219 else | |
| 220 AddIconAndLabel(image, label, false); | |
| 221 | |
| 222 if (network->IsConnectedState()) { | |
| 223 disconnect_button_ = TrayPopupUtils::CreateTrayPopupButton( | |
| 224 this, l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_DISCONNECT)); | |
| 225 tri_view()->AddView(TriView::Container::END, disconnect_button_); | |
| 226 tri_view()->SetContainerVisible(TriView::Container::END, true); | |
| 227 tri_view()->SetContainerBorder( | |
| 228 TriView::Container::END, | |
| 229 views::CreateEmptyBorder(0, 0, 0, kTrayPopupButtonEndMargin)); | |
| 230 } | |
| 231 Layout(); | |
| 232 } | |
| 233 | |
| 234 // TODO(varkha): Consolidate with a similar method in tray_bluetooth.cc. | |
| 235 void VPNListNetworkEntry::SetupConnectedItemMd(const base::string16& text, | |
| 236 const gfx::ImageSkia& image) { | |
| 237 AddIconAndLabels( | |
| 238 image, text, | |
| 239 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTED)); | |
| 240 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::CAPTION); | |
| 241 style.set_color_style(TrayPopupItemStyle::ColorStyle::CONNECTED); | |
| 242 style.SetupLabel(sub_text_label()); | |
| 243 } | |
| 244 | |
| 245 // TODO(varkha): Consolidate with a similar method in tray_bluetooth.cc. | |
| 246 void VPNListNetworkEntry::SetupConnectingItemMd(const base::string16& text, | |
| 247 const gfx::ImageSkia& image) { | |
| 248 AddIconAndLabels( | |
| 249 image, text, | |
| 250 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTING)); | |
| 251 ThrobberView* throbber = new ThrobberView; | |
| 252 throbber->Start(); | |
| 253 AddRightView(throbber); | |
| 254 } | |
| 255 | |
| 256 } // namespace | |
| 257 | |
| 258 VPNListView::VPNListView(NetworkListDelegate* delegate) : delegate_(delegate) { | |
| 259 WmShell::Get()->vpn_list()->AddObserver(this); | |
| 260 } | |
| 261 | |
| 262 VPNListView::~VPNListView() { | |
| 263 WmShell::Get()->vpn_list()->RemoveObserver(this); | |
| 264 } | |
| 265 | |
| 266 void VPNListView::Update() { | |
| 267 // Before updating the list, determine whether the user was hovering over one | |
| 268 // of the VPN provider or network entries. | |
| 269 std::unique_ptr<VPNProvider> hovered_provider; | |
| 270 std::string hovered_network_guid; | |
| 271 for (const std::pair<const views::View* const, VPNProvider>& provider : | |
| 272 provider_view_map_) { | |
| 273 if (static_cast<const HoverHighlightView*>(provider.first)->hover()) { | |
| 274 hovered_provider.reset(new VPNProvider(provider.second)); | |
| 275 break; | |
| 276 } | |
| 277 } | |
| 278 if (!hovered_provider) { | |
| 279 for (const std::pair<const views::View*, std::string>& entry : | |
| 280 network_view_guid_map_) { | |
| 281 if (static_cast<const HoverHighlightView*>(entry.first)->hover()) { | |
| 282 hovered_network_guid = entry.second; | |
| 283 break; | |
| 284 } | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 // Clear the list. | |
| 289 container()->RemoveAllChildViews(true); | |
| 290 provider_view_map_.clear(); | |
| 291 network_view_guid_map_.clear(); | |
| 292 list_empty_ = true; | |
| 293 if (!UseMd()) { | |
| 294 container()->SetLayoutManager( | |
| 295 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | |
| 296 } | |
| 297 | |
| 298 // Get the list of available VPN networks, in shill's priority order. | |
| 299 chromeos::NetworkStateHandler::NetworkStateList networks; | |
| 300 chromeos::NetworkHandler::Get() | |
| 301 ->network_state_handler() | |
| 302 ->GetVisibleNetworkListByType(chromeos::NetworkTypePattern::VPN(), | |
| 303 &networks); | |
| 304 | |
| 305 // Show all VPN providers and all networks that are currently disconnected. | |
| 306 AddProvidersAndNetworks(networks); | |
| 307 | |
| 308 // Determine whether one of the new list entries corresponds to the entry that | |
| 309 // the user was previously hovering over. If such an entry is found, the list | |
| 310 // will be scrolled to ensure the entry is visible. | |
| 311 const views::View* scroll_to_show_view = nullptr; | |
| 312 if (hovered_provider) { | |
| 313 for (const std::pair<const views::View* const, VPNProvider>& provider : | |
| 314 provider_view_map_) { | |
| 315 if (provider.second == *hovered_provider) { | |
| 316 scroll_to_show_view = provider.first; | |
| 317 break; | |
| 318 } | |
| 319 } | |
| 320 } else if (!hovered_network_guid.empty()) { | |
| 321 for (const std::pair<const views::View*, std::string>& entry : | |
| 322 network_view_guid_map_) { | |
| 323 if (entry.second == hovered_network_guid) { | |
| 324 scroll_to_show_view = entry.first; | |
| 325 break; | |
| 326 } | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 // Layout the updated list. | |
| 331 container()->SizeToPreferredSize(); | |
| 332 delegate_->RelayoutScrollList(); | |
| 333 | |
| 334 if (scroll_to_show_view) { | |
| 335 // Scroll the list so that |scroll_to_show_view| is in view. | |
| 336 container()->ScrollRectToVisible(scroll_to_show_view->bounds()); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 bool VPNListView::IsNetworkEntry(views::View* view, std::string* guid) const { | |
| 341 const auto& entry = network_view_guid_map_.find(view); | |
| 342 if (entry == network_view_guid_map_.end()) | |
| 343 return false; | |
| 344 *guid = entry->second; | |
| 345 return true; | |
| 346 } | |
| 347 | |
| 348 void VPNListView::OnVPNProvidersChanged() { | |
| 349 Update(); | |
| 350 } | |
| 351 | |
| 352 void VPNListView::OnViewClicked(views::View* sender) { | |
| 353 const auto& provider_iter = provider_view_map_.find(sender); | |
| 354 if (provider_iter != provider_view_map_.end()) { | |
| 355 // If the user clicks on a provider entry, request that the "add network" | |
| 356 // dialog for this provider be shown. | |
| 357 const VPNProvider& provider = provider_iter->second; | |
| 358 WmShell* shell = WmShell::Get(); | |
| 359 if (provider.third_party) { | |
| 360 shell->RecordUserMetricsAction( | |
| 361 UMA_STATUS_AREA_VPN_ADD_THIRD_PARTY_CLICKED); | |
| 362 shell->system_tray_controller()->ShowThirdPartyVpnCreate( | |
| 363 provider.extension_id); | |
| 364 } else { | |
| 365 shell->RecordUserMetricsAction(UMA_STATUS_AREA_VPN_ADD_BUILT_IN_CLICKED); | |
| 366 shell->system_tray_controller()->ShowNetworkCreate(shill::kTypeVPN); | |
| 367 } | |
| 368 return; | |
| 369 } | |
| 370 | |
| 371 // If the user clicked on a network entry, let the |delegate_| trigger a | |
| 372 // connection attempt (if the network is currently disconnected) or show a | |
| 373 // configuration dialog (if the network is currently connected or connecting). | |
| 374 delegate_->OnNetworkEntryClicked(sender); | |
| 375 } | |
| 376 | |
| 377 void VPNListView::AddNetwork(const chromeos::NetworkState* network) { | |
| 378 views::View* entry(new VPNListNetworkEntry(this, network)); | |
| 379 container()->AddChildView(entry); | |
| 380 network_view_guid_map_[entry] = network->guid(); | |
| 381 list_empty_ = false; | |
| 382 } | |
| 383 | |
| 384 void VPNListView::AddProviderAndNetworks( | |
| 385 const VPNProvider& vpn_provider, | |
| 386 const chromeos::NetworkStateHandler::NetworkStateList& networks) { | |
| 387 // Add a visual separator, unless this is the topmost entry in the list. | |
| 388 if (!list_empty_) | |
| 389 container()->AddChildView(TrayPopupUtils::CreateListSubHeaderSeparator()); | |
| 390 std::string vpn_name = | |
| 391 vpn_provider.third_party | |
| 392 ? vpn_provider.third_party_provider_name | |
| 393 : l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_VPN_BUILT_IN_PROVIDER); | |
| 394 | |
| 395 // Add a list entry for the VPN provider. | |
| 396 views::View* provider_view = nullptr; | |
| 397 if (UseMd()) { | |
| 398 provider_view = new VPNListProviderEntryMd( | |
| 399 this, list_empty_, vpn_name, IDS_ASH_STATUS_TRAY_ADD_CONNECTION); | |
| 400 } else { | |
| 401 provider_view = new VPNListProviderEntry(this, vpn_name); | |
| 402 } | |
| 403 container()->AddChildView(provider_view); | |
| 404 provider_view_map_[provider_view] = vpn_provider; | |
| 405 list_empty_ = false; | |
| 406 // Add the networks belonging to this provider, in the priority order returned | |
| 407 // by shill. | |
| 408 for (const chromeos::NetworkState* const& network : networks) { | |
| 409 if (VpnProviderMatchesNetwork(vpn_provider, *network)) | |
| 410 AddNetwork(network); | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 void VPNListView::AddProvidersAndNetworks( | |
| 415 const chromeos::NetworkStateHandler::NetworkStateList& networks) { | |
| 416 // Get the list of VPN providers enabled in the primary user's profile. | |
| 417 std::vector<VPNProvider> providers = | |
| 418 WmShell::Get()->vpn_list()->vpn_providers(); | |
| 419 | |
| 420 // Add providers with at least one configured network along with their | |
| 421 // networks. Providers are added in the order of their highest priority | |
| 422 // network. | |
| 423 for (const chromeos::NetworkState* const& network : networks) { | |
| 424 for (auto provider = providers.begin(); provider != providers.end(); | |
| 425 ++provider) { | |
| 426 if (!VpnProviderMatchesNetwork(*provider, *network)) | |
| 427 continue; | |
| 428 AddProviderAndNetworks(*provider, networks); | |
| 429 providers.erase(provider); | |
| 430 break; | |
| 431 } | |
| 432 } | |
| 433 | |
| 434 // Add providers without any configured networks, in the order that the | |
| 435 // providers were returned by the extensions system. | |
| 436 for (const VPNProvider& provider : providers) | |
| 437 AddProviderAndNetworks(provider, networks); | |
| 438 } | |
| 439 | |
| 440 } // namespace ash | |
| OLD | NEW |