Commit 6c1f3ec4 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Introduce CrosNetworkConfig.ForgetNetwork

This adds CrosNetworkConfig.ForgetNetwork and uses it in the Internet
Settings WebUI.

Bug: 1001598
Change-Id: Ic9952b9024b0f8bb7ce42955f8f7d00afdc7ac55
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1808497
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698697}
parent a0a331e3
......@@ -103,12 +103,6 @@ Polymer({
value: false,
},
/**
* Interface for networkingPrivate calls, passed from internet_page.
* @type {NetworkingPrivate}
*/
networkingPrivate: Object,
/**
* The network AutoConnect state as a fake preference object.
* @private {!chrome.settingsPrivate.PrefObject|undefined}
......@@ -205,12 +199,7 @@ Polymer({
/** @private {settings.InternetPageBrowserProxy} */
browserProxy_: null,
/**
* This UI will use both the networkingPrivate extension API and the
* networkConfig mojo API until we provide all of the required functionality
* in networkConfig. TODO(stevenjb): Remove use of networkingPrivate api.
* @private {?chromeos.networkConfig.mojom.CrosNetworkConfigRemote}
*/
/** @private {?chromeos.networkConfig.mojom.CrosNetworkConfigRemote} */
networkConfig_: null,
/** @override */
......@@ -1076,7 +1065,11 @@ Polymer({
/** @private */
onForgetTap_: function() {
this.networkingPrivate.forgetNetwork(this.guid);
this.networkConfig_.forgetNetwork(this.guid).then(response => {
if (!response.success) {
console.error('Froget network failed for: ' + this.guid);
}
});
// A forgotten network no longer has a valid GUID, close the subpage.
this.close();
},
......
......@@ -25,12 +25,6 @@ Polymer({
observer: 'networkTypeChanged_',
},
/**
* Interface for networkingPrivate calls, passed from internet_page.
* @type {NetworkingPrivate}
*/
networkingPrivate: Object,
/**
* List of all network state data for the network type.
* @private {!Array<!OncMojo.NetworkStateProperties>}
......@@ -60,12 +54,7 @@ Polymer({
/** @private {string} */
selectedGuid_: '',
/**
* This UI will use both the networkingPrivate extension API and the
* networkConfig mojo API until we provide all of the required functionality
* in networkConfig. TODO(stevenjb): Remove use of networkingPrivate api.
* @private {?chromeos.networkConfig.mojom.CrosNetworkConfigRemote}
*/
/** @private {?chromeos.networkConfig.mojom.CrosNetworkConfigRemote} */
networkConfig_: null,
/** @override */
......@@ -215,7 +204,11 @@ Polymer({
/** @private */
onForgetTap_: function() {
this.networkingPrivate.forgetNetwork(this.selectedGuid_);
this.networkConfig_.forgetNetwork(this.selectedGuid_).then(response => {
if (!response.success) {
console.error('Froget network failed for: ' + this.selectedGuid_);
}
});
/** @type {!CrActionMenuElement} */ (this.$.dotsMenu).close();
},
......
......@@ -94,7 +94,6 @@
<settings-internet-detail-page prefs="{{prefs}}"
default-network="[[defaultNetwork]]"
global-policy="[[globalPolicy_]]"
networking-private="[[networkingPrivate]]"
managed-network-available="[[managedNetworkAvailable]]">
</settings-internet-detail-page>
</settings-subpage>
......@@ -103,8 +102,7 @@
<template is="dom-if" route-path="/knownNetworks" no-search restamp>
<settings-subpage page-title="$i18n{internetKnownNetworksPageTitle}">
<settings-internet-known-networks-page
network-type="[[knownNetworksType_]]"
networking-private="[[networkingPrivate]]">
network-type="[[knownNetworksType_]]">
</settings-internet-known-networks-page>
</settings-subpage>
</template>
......
......@@ -2008,6 +2008,76 @@ void CrosNetworkConfig::ConfigureNetworkFailure(
configure_network_callbacks_.erase(iter);
}
void CrosNetworkConfig::ForgetNetwork(const std::string& guid,
ForgetNetworkCallback callback) {
if (!network_configuration_handler_) {
NET_LOG(ERROR) << "ForgetNetwork called with no handler";
std::move(callback).Run(false);
return;
}
const NetworkState* network =
network_state_handler_->GetNetworkStateFromGuid(guid);
if (!network || network->profile_path().empty()) {
NET_LOG(ERROR) << "ForgetNetwork called with unconfigured network: "
<< guid;
std::move(callback).Run(false);
return;
}
bool allow_forget_shared_config = true;
::onc::ONCSource onc_source = ::onc::ONC_SOURCE_UNKNOWN;
std::string user_id_hash = LoginState::Get()->primary_user_hash();
if (network_configuration_handler_->FindPolicyByGUID(user_id_hash, guid,
&onc_source)) {
if (onc_source == ::onc::ONC_SOURCE_USER_POLICY) {
// Prevent a policy controlled configuration removal.
std::move(callback).Run(false);
return;
}
if (onc_source == ::onc::ONC_SOURCE_DEVICE_POLICY)
allow_forget_shared_config = false;
}
int callback_id = callback_id_++;
forget_network_callbacks_[callback_id] = std::move(callback);
if (allow_forget_shared_config) {
network_configuration_handler_->RemoveConfiguration(
network->path(),
base::Bind(&CrosNetworkConfig::ForgetNetworkSuccess,
weak_factory_.GetWeakPtr(), callback_id),
base::Bind(&CrosNetworkConfig::ForgetNetworkFailure,
weak_factory_.GetWeakPtr(), guid, callback_id));
} else {
network_configuration_handler_->RemoveConfigurationFromCurrentProfile(
network->path(),
base::Bind(&CrosNetworkConfig::ForgetNetworkSuccess,
weak_factory_.GetWeakPtr(), callback_id),
base::Bind(&CrosNetworkConfig::ForgetNetworkFailure,
weak_factory_.GetWeakPtr(), guid, callback_id));
}
}
void CrosNetworkConfig::ForgetNetworkSuccess(int callback_id) {
auto iter = forget_network_callbacks_.find(callback_id);
DCHECK(iter != forget_network_callbacks_.end());
std::move(iter->second).Run(/*success=*/true);
forget_network_callbacks_.erase(iter);
}
void CrosNetworkConfig::ForgetNetworkFailure(
const std::string& guid,
int callback_id,
const std::string& error_name,
std::unique_ptr<base::DictionaryValue> error_data) {
auto iter = forget_network_callbacks_.find(callback_id);
DCHECK(iter != forget_network_callbacks_.end());
NET_LOG(ERROR) << "Failed to forget network: " << guid
<< " Error: " << error_name;
std::move(iter->second).Run(/*success=*/false);
forget_network_callbacks_.erase(iter);
}
void CrosNetworkConfig::SetNetworkTypeEnabledState(
mojom::NetworkType type,
bool enabled,
......
......@@ -58,6 +58,8 @@ class CrosNetworkConfig : public mojom::CrosNetworkConfig,
void ConfigureNetwork(mojom::ConfigPropertiesPtr properties,
bool shared,
ConfigureNetworkCallback callback) override;
void ForgetNetwork(const std::string& guid,
ForgetNetworkCallback callback) override;
void SetNetworkTypeEnabledState(
mojom::NetworkType type,
bool enabled,
......@@ -109,6 +111,11 @@ class CrosNetworkConfig : public mojom::CrosNetworkConfig,
int callback_id,
const std::string& error_name,
std::unique_ptr<base::DictionaryValue> error_data);
void ForgetNetworkSuccess(int callback_id);
void ForgetNetworkFailure(const std::string& guid,
int callback_id,
const std::string& error_name,
std::unique_ptr<base::DictionaryValue> error_data);
void SetCellularSimStateSuccess(int callback_id);
void SetCellularSimStateFailure(
int callback_id,
......@@ -155,6 +162,7 @@ class CrosNetworkConfig : public mojom::CrosNetworkConfig,
get_managed_properties_callbacks_;
base::flat_map<int, SetPropertiesCallback> set_properties_callbacks_;
base::flat_map<int, ConfigureNetworkCallback> configure_network_callbacks_;
base::flat_map<int, ForgetNetworkCallback> forget_network_callbacks_;
base::flat_map<int, SetCellularSimStateCallback>
set_cellular_sim_state_callbacks_;
base::flat_map<int, SelectCellularMobileNetworkCallback>
......
......@@ -277,6 +277,21 @@ class CrosNetworkConfigTest : public testing::Test {
return guid;
}
bool ForgetNetwork(const std::string& guid) {
bool success = false;
base::RunLoop run_loop;
cros_network_config()->ForgetNetwork(
guid,
base::BindOnce(
[](bool* successp, base::OnceClosure quit_closure, bool success) {
*successp = success;
std::move(quit_closure).Run();
},
&success, run_loop.QuitClosure()));
run_loop.Run();
return success;
}
bool SetCellularSimState(const std::string& current_pin_or_puk,
base::Optional<std::string> new_pin,
bool require_pin) {
......@@ -709,6 +724,22 @@ TEST_F(CrosNetworkConfigTest, ConfigureNetwork) {
EXPECT_EQ(ssid, network->wifi->ssid);
}
TEST_F(CrosNetworkConfigTest, ForgetNetwork) {
// Use a non visible configured network.
const std::string kGUID = "wifi3_guid";
// Verify the configuration exists.
mojom::ManagedPropertiesPtr properties = GetManagedProperties(kGUID);
ASSERT_TRUE(properties);
ASSERT_EQ(kGUID, properties->guid);
// Forget the network and verify the configuration no longer exists.
bool result = ForgetNetwork(kGUID);
EXPECT_TRUE(result);
properties = GetManagedProperties(kGUID);
ASSERT_FALSE(properties);
}
TEST_F(CrosNetworkConfigTest, SetNetworkTypeEnabledState) {
std::vector<mojom::DeviceStatePropertiesPtr> devices = GetDeviceStateList();
ASSERT_EQ(3u, devices.size());
......
......@@ -846,6 +846,11 @@ interface CrosNetworkConfig {
ConfigureNetwork(ConfigProperties properties, bool shared) =>
(string? guid, string error_message);
// Forgets an existing network configuration matching |guid| by clearing all
// configured properties. Returns true if the network exists and is
// successfully removed.
ForgetNetwork(string guid) => (bool success);
// Sets a single network type (e.g. WiFi) to enabled or disabled. Types
// describing multiple technologies (e.g. kWireless) are not supported.
// Returns false if the type is invalid, unavailable, or can not be disabled.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment