blob: 5a131072d0f2e938c5b3c44eec293d91c6d59123 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/network/vpn_list_view.h"
#include <memory>
#include <vector>
#include "ash/metrics/user_metrics_recorder.h"
#include "ash/public/cpp/ash_pref_names.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/network/network_icon.h"
#include "ash/system/network/network_icon_animation.h"
#include "ash/system/network/network_icon_animation_observer.h"
#include "ash/system/network/vpn_list.h"
#include "ash/system/tray/hover_highlight_view.h"
#include "ash/system/tray/system_menu_button.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tri_view.h"
#include "ash/system/tray/view_click_listener.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/network/network_connect.h"
#include "chromeos/services/network_config/public/cpp/cros_network_config_util.h"
#include "chromeos/services/network_config/public/mojom/constants.mojom.h"
#include "components/onc/onc_constants.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "services/service_manager/public/cpp/connector.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view.h"
using chromeos::network_config::mojom::ConnectionStateType;
using chromeos::network_config::mojom::FilterType;
using chromeos::network_config::mojom::NetworkFilter;
using chromeos::network_config::mojom::NetworkStateProperties;
using chromeos::network_config::mojom::NetworkStatePropertiesPtr;
using chromeos::network_config::mojom::NetworkType;
using chromeos::network_config::mojom::VPNType;
namespace ash {
namespace tray {
namespace {
struct CompareArcVPNProviderByLastLaunchTime {
bool operator()(const VPNProvider& provider1, const VPNProvider& provider2) {
return provider1.last_launch_time > provider2.last_launch_time;
}
};
// Indicates whether |network| belongs to this VPN provider.
bool VpnProviderMatchesNetwork(const VPNProvider& provider,
const NetworkStateProperties* network) {
DCHECK(network);
// Never display non-VPN networks or VPNs with no provider info.
if (network->type != NetworkType::kVPN)
return false;
// Package name is the vpn provider id for ArcVPNProvider in network state.
if (network->vpn->type == VPNType::kArcVPN) {
return provider.provider_type == VPNProvider::ARC_VPN &&
network->vpn->provider_id == provider.package_name;
}
if (network->vpn->type == VPNType::kThirdPartyVPN) {
return provider.provider_type == VPNProvider::THIRD_PARTY_VPN &&
network->vpn->provider_id == provider.app_id;
}
return provider.provider_type == VPNProvider::BUILT_IN_VPN;
}
// Returns the PrefService that should be used for kVpnConfigAllowed, which is
// controlled by policy. If multiple users are logged in, the more restrictive
// policy is most likely in the primary user.
PrefService* GetPrefService() {
SessionControllerImpl* controller = Shell::Get()->session_controller();
PrefService* prefs = controller->GetPrimaryUserPrefService();
return prefs ? prefs : controller->GetActivePrefService();
}
bool IsVpnConfigAllowed() {
PrefService* prefs = GetPrefService();
DCHECK(prefs);
return prefs->GetBoolean(prefs::kVpnConfigAllowed);
}
// A list entry that represents a VPN provider.
class VPNListProviderEntry : public views::ButtonListener, public views::View {
public:
VPNListProviderEntry(const VPNProvider& vpn_provider,
bool top_item,
const std::string& name,
int button_accessible_name_id)
: vpn_provider_(vpn_provider) {
TrayPopupUtils::ConfigureAsStickyHeader(this);
SetLayoutManager(std::make_unique<views::FillLayout>());
TriView* tri_view = TrayPopupUtils::CreateSubHeaderRowView(true);
tri_view->AddView(TriView::Container::START,
TrayPopupUtils::CreateMainImageView());
AddChildView(tri_view);
views::Label* label = TrayPopupUtils::CreateDefaultLabel();
TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SUB_HEADER);
style.SetupLabel(label);
label->SetText(base::ASCIIToUTF16(name));
tri_view->AddView(TriView::Container::CENTER, label);
const SkColor image_color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_ProminentButtonColor);
gfx::ImageSkia icon =
gfx::CreateVectorIcon(kSystemMenuAddConnectionIcon, image_color);
SystemMenuButton* add_vpn_button =
new SystemMenuButton(this, icon, icon, button_accessible_name_id);
add_vpn_button->SetInkDropColor(image_color);
add_vpn_button->SetEnabled(true);
tri_view->AddView(TriView::Container::END, add_vpn_button);
}
protected:
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override {
// If the user clicks on a provider entry, request that the "add network"
// dialog for this provider be shown.
if (vpn_provider_.provider_type == VPNProvider::THIRD_PARTY_VPN) {
Shell::Get()->metrics()->RecordUserMetricsAction(
UMA_STATUS_AREA_VPN_ADD_THIRD_PARTY_CLICKED);
Shell::Get()->system_tray_model()->client_ptr()->ShowThirdPartyVpnCreate(
vpn_provider_.app_id);
} else if (vpn_provider_.provider_type == VPNProvider::ARC_VPN) {
// TODO(lgcheng@) Add UMA status if needed.
Shell::Get()->system_tray_model()->client_ptr()->ShowArcVpnCreate(
vpn_provider_.app_id);
} else {
Shell::Get()->metrics()->RecordUserMetricsAction(
UMA_STATUS_AREA_VPN_ADD_BUILT_IN_CLICKED);
Shell::Get()->system_tray_model()->client_ptr()->ShowNetworkCreate(
::onc::network_type::kVPN);
}
}
private:
const VPNProvider vpn_provider_;
DISALLOW_COPY_AND_ASSIGN(VPNListProviderEntry);
};
// A list entry that represents a network. If the network is currently
// connecting, the icon shown by this list entry will be animated. If the
// network is currently connected, a disconnect button will be shown next to its
// name.
class VPNListNetworkEntry : public HoverHighlightView,
public network_icon::AnimationObserver {
public:
VPNListNetworkEntry(VPNListView* vpn_list_view,
const NetworkStateProperties* network);
~VPNListNetworkEntry() override;
// network_icon::AnimationObserver:
void NetworkIconChanged() override;
// views::ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
private:
void OnGetNetworkState(NetworkStatePropertiesPtr result);
void UpdateFromNetworkState(const NetworkStateProperties* network);
VPNListView* const owner_;
const std::string guid_;
views::LabelButton* disconnect_button_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(VPNListNetworkEntry);
};
VPNListNetworkEntry::VPNListNetworkEntry(VPNListView* owner,
const NetworkStateProperties* network)
: HoverHighlightView(owner), owner_(owner), guid_(network->guid) {
UpdateFromNetworkState(network);
}
VPNListNetworkEntry::~VPNListNetworkEntry() {
network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
}
void VPNListNetworkEntry::NetworkIconChanged() {
chromeos::network_config::mojom::CrosNetworkConfig* cros_network_config =
owner_->cros_network_config();
if (!cros_network_config)
return;
cros_network_config->GetNetworkState(
guid_, base::BindOnce(&VPNListNetworkEntry::OnGetNetworkState,
base::Unretained(this)));
}
void VPNListNetworkEntry::ButtonPressed(Button* sender,
const ui::Event& event) {
if (sender != disconnect_button_) {
HoverHighlightView::ButtonPressed(sender, event);
return;
}
// TODO(stevenjb): Replace with mojo API. https://crbug.com/862420.
chromeos::NetworkConnect::Get()->DisconnectFromNetworkId(guid_);
}
void VPNListNetworkEntry::OnGetNetworkState(NetworkStatePropertiesPtr result) {
UpdateFromNetworkState(result.get());
}
void VPNListNetworkEntry::UpdateFromNetworkState(
const NetworkStateProperties* vpn) {
if (vpn && vpn->connection_state == ConnectionStateType::kConnecting)
network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
else
network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
if (!vpn) {
// This is a transient state where the vpn has been removed already but
// the network list in the UI has not been updated yet.
return;
}
Reset();
disconnect_button_ = nullptr;
network_icon::NetworkIconState vpn_icon_state(vpn);
gfx::ImageSkia image = network_icon::GetImageForVPN(
vpn_icon_state, network_icon::ICON_TYPE_LIST);
base::string16 label = network_icon::GetLabelForNetworkList(vpn_icon_state);
AddIconAndLabel(image, label);
if (chromeos::network_config::StateIsConnected(vpn->connection_state)) {
owner_->SetupConnectedScrollListItem(this);
if (IsVpnConfigAllowed()) {
disconnect_button_ = TrayPopupUtils::CreateTrayPopupButton(
this, l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_DISCONNECT));
AddRightView(disconnect_button_);
}
tri_view()->SetContainerBorder(
TriView::Container::END,
views::CreateEmptyBorder(
0, kTrayPopupButtonEndMargin - kTrayPopupLabelHorizontalPadding, 0,
kTrayPopupButtonEndMargin));
} else if (vpn->connection_state == ConnectionStateType::kConnecting) {
owner_->SetupConnectingScrollListItem(this);
}
Layout();
}
} // namespace
VPNListView::VPNListView(DetailedViewDelegate* delegate, LoginStatus login)
: NetworkStateListDetailedView(delegate, LIST_TYPE_VPN, login) {
Shell::Get()->vpn_list()->AddObserver(this);
BindCrosNetworkConfig();
}
VPNListView::~VPNListView() {
Shell::Get()->vpn_list()->RemoveObserver(this);
}
void VPNListView::UpdateNetworkList() {
DCHECK(cros_network_config_ptr_);
cros_network_config_ptr_->GetNetworkStateList(
NetworkFilter::New(FilterType::kVisible, NetworkType::kVPN,
chromeos::network_config::mojom::kNoLimit),
base::BindOnce(&VPNListView::OnGetNetworkStateList,
base::Unretained(this)));
}
void VPNListView::OnGetNetworkStateList(NetworkStateList networks) {
// Before updating the list, determine whether the user was hovering over one
// of the VPN provider or network entries.
std::unique_ptr<VPNProvider> hovered_provider;
std::string hovered_network_guid;
for (const std::pair<const views::View* const, VPNProvider>& provider :
provider_view_map_) {
if (provider.first->IsMouseHovered()) {
hovered_provider.reset(new VPNProvider(provider.second));
break;
}
}
if (!hovered_provider) {
for (const std::pair<const views::View*, std::string>& entry :
network_view_guid_map_) {
if (entry.first->IsMouseHovered()) {
hovered_network_guid = entry.second;
break;
}
}
}
// Clear the list.
scroll_content()->RemoveAllChildViews(true);
provider_view_map_.clear();
network_view_guid_map_.clear();
list_empty_ = true;
// Show all VPN providers and all networks that are currently disconnected.
AddProvidersAndNetworks(networks);
// Determine whether one of the new list entries corresponds to the entry that
// the user was previously hovering over. If such an entry is found, the list
// will be scrolled to ensure the entry is visible.
const views::View* scroll_to_show_view = nullptr;
if (hovered_provider) {
for (const std::pair<const views::View* const, VPNProvider>& provider :
provider_view_map_) {
if (provider.second == *hovered_provider) {
scroll_to_show_view = provider.first;
break;
}
}
} else if (!hovered_network_guid.empty()) {
for (const std::pair<const views::View*, std::string>& entry :
network_view_guid_map_) {
if (entry.second == hovered_network_guid) {
scroll_to_show_view = entry.first;
break;
}
}
}
// Layout the updated list.
scroll_content()->SizeToPreferredSize();
scroller()->Layout();
if (scroll_to_show_view) {
// Scroll the list so that |scroll_to_show_view| is in view.
scroll_content()->ScrollRectToVisible(scroll_to_show_view->bounds());
}
}
bool VPNListView::IsNetworkEntry(views::View* view, std::string* guid) const {
const auto& entry = network_view_guid_map_.find(view);
if (entry == network_view_guid_map_.end())
return false;
*guid = entry->second;
return true;
}
void VPNListView::OnVPNProvidersChanged() {
UpdateNetworkList();
}
void VPNListView::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kVpnConfigAllowed, true,
PrefRegistry::PUBLIC);
}
void VPNListView::BindCrosNetworkConfig() {
// Ensure binding is reset in case this is called after a failure.
cros_network_config_ptr_.reset();
service_manager::Connector* connector = Shell::Get()->connector();
if (!connector)
return;
connector->BindInterface(chromeos::network_config::mojom::kServiceName,
&cros_network_config_ptr_);
// If the connection is lost (e.g. due to a crash), attempt to rebind it.
cros_network_config_ptr_.set_connection_error_handler(base::BindOnce(
&VPNListView::BindCrosNetworkConfig, base::Unretained(this)));
}
void VPNListView::AddNetwork(const NetworkStateProperties* network) {
views::View* entry(new VPNListNetworkEntry(this, network));
scroll_content()->AddChildView(entry);
network_view_guid_map_[entry] = network->guid;
list_empty_ = false;
}
void VPNListView::AddProviderAndNetworks(const VPNProvider& vpn_provider) {
AddProviderAndNetworks(vpn_provider, {});
}
void VPNListView::AddProviderAndNetworks(const VPNProvider& vpn_provider,
const NetworkStateList& networks) {
// Add a visual separator, unless this is the topmost entry in the list.
if (!list_empty_) {
scroll_content()->AddChildView(CreateListSubHeaderSeparator());
}
std::string vpn_name =
vpn_provider.provider_type == VPNProvider::BUILT_IN_VPN
? l10n_util::GetStringUTF8(IDS_ASH_STATUS_TRAY_VPN_BUILT_IN_PROVIDER)
: vpn_provider.provider_name;
// Add a list entry for the VPN provider.
views::View* provider_view = nullptr;
provider_view = new VPNListProviderEntry(vpn_provider, list_empty_, vpn_name,
IDS_ASH_STATUS_TRAY_ADD_CONNECTION);
scroll_content()->AddChildView(provider_view);
provider_view_map_[provider_view] = vpn_provider;
list_empty_ = false;
// Add the networks belonging to this provider, in the priority order returned
// by shill.
for (const auto& network : networks) {
if (VpnProviderMatchesNetwork(vpn_provider, network.get()))
AddNetwork(network.get());
}
}
bool VPNListView::ProcessProviderForNetwork(
const NetworkStateProperties* network,
const NetworkStateList& networks,
std::vector<VPNProvider>* providers) {
for (auto provider_iter = providers->begin();
provider_iter != providers->end(); ++provider_iter) {
if (!VpnProviderMatchesNetwork(*provider_iter, network))
continue;
AddProviderAndNetworks(*provider_iter, networks);
providers->erase(provider_iter);
return true;
}
return false;
}
void VPNListView::AddProvidersAndNetworks(const NetworkStateList& networks) {
// Get the list of VPN providers enabled in the primary user's profile.
std::vector<VPNProvider> extension_providers =
Shell::Get()->vpn_list()->extension_vpn_providers();
// Get the list of Arc VPN providers installed in the primary user's profile.
std::vector<VPNProvider> arc_providers =
Shell::Get()->vpn_list()->arc_vpn_providers();
std::sort(arc_providers.begin(), arc_providers.end(),
CompareArcVPNProviderByLastLaunchTime());
// Add connected ARCVPN network. If we can find the correct provider, nest
// the network under the provider. Otherwise list it unnested.
for (const auto& network : networks) {
if (network->connection_state == ConnectionStateType::kNotConnected)
break;
if (network->vpn->type != VPNType::kArcVPN)
continue;
// If no matched provider found for this network. Show it unnested.
// TODO(lgcheng@) add UMA status to track this.
if (!ProcessProviderForNetwork(network.get(), networks, &arc_providers))
AddNetwork(network.get());
}
// Add providers with at least one configured network along with their
// networks. Providers are added in the order of their highest priority
// network.
for (const auto& network : networks)
ProcessProviderForNetwork(network.get(), networks, &extension_providers);
// Add providers without any configured networks, in the order that the
// providers were returned by the extensions system.
for (const VPNProvider& extension_provider : extension_providers)
AddProviderAndNetworks(extension_provider);
// Add Arc VPN providers without any connected or connecting networks. These
// providers are sorted by last launch time.
for (const VPNProvider& arc_provider : arc_providers) {
AddProviderAndNetworks(arc_provider);
}
}
} // namespace tray
} // namespace ash