Commit fd3cc2bb authored by Julian Pastarmov's avatar Julian Pastarmov Committed by Commit Bot

Add reset option to the setDeviceData API

If the data parameter is undefined the API will clear the current stored
data and delete the file used to store as well as its immediate folder
if it has become empty.

BUG=1044673
TEST=unit_tests

Change-Id: I2f4dd796c9b0e4eea2474def32a47924d0195ccc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2015168Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Julian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#735346}
parent 09a941a9
...@@ -419,7 +419,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report, ...@@ -419,7 +419,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report,
} }
void StoreDeviceData(const std::string& id, void StoreDeviceData(const std::string& id,
const std::vector<uint8_t>& data, const std::unique_ptr<std::vector<uint8_t>> data,
base::OnceCallback<void(bool)> callback) { base::OnceCallback<void(bool)> callback) {
base::FilePath data_file = GetEndpointVerificationDir(); base::FilePath data_file = GetEndpointVerificationDir();
if (data_file.empty()) { if (data_file.empty()) {
...@@ -431,28 +431,36 @@ void StoreDeviceData(const std::string& id, ...@@ -431,28 +431,36 @@ void StoreDeviceData(const std::string& id,
// subdir+file of the EV folder. // subdir+file of the EV folder.
data_file = data_file.AppendASCII(id); data_file = data_file.AppendASCII(id);
// Ensure the directory exists. bool success = false;
bool success = base::CreateDirectory(data_file.DirName()); if (data) {
if (!success) { // Ensure the directory exists.
LOG(ERROR) << "Could not create directory: " success = base::CreateDirectory(data_file.DirName());
<< data_file.DirName().LossyDisplayName(); if (!success) {
std::move(callback).Run(false); LOG(ERROR) << "Could not create directory: "
return; << data_file.DirName().LossyDisplayName();
} std::move(callback).Run(false);
return;
}
base::FilePath tmp_path; base::FilePath tmp_path;
success = base::CreateTemporaryFileInDir(data_file.DirName(), &tmp_path); success = base::CreateTemporaryFileInDir(data_file.DirName(), &tmp_path);
if (!success) { if (!success) {
LOG(ERROR) << "Could not open file for writing: " LOG(ERROR) << "Could not open file for writing: "
<< tmp_path.LossyDisplayName(); << tmp_path.LossyDisplayName();
std::move(callback).Run(false); std::move(callback).Run(false);
return; return;
} }
base::WriteFile(tmp_path, reinterpret_cast<const char*>(data.data()), base::WriteFile(tmp_path, reinterpret_cast<const char*>(data->data()),
data.size()); data->size());
success = base::Move(tmp_path, data_file);
} else {
// Not passing a second parameter means clear the data sored under |id|.
success = base::DeleteFile(data_file, false);
if (base::IsDirectoryEmpty(data_file.DirName()))
base::DeleteFile(data_file.DirName(), false);
}
success = base::Move(tmp_path, data_file);
std::move(callback).Run(success); std::move(callback).Run(success);
} }
......
...@@ -29,7 +29,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report, ...@@ -29,7 +29,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report,
// Store the |data| associated with the identifier |id|. Calls |callback| on // Store the |data| associated with the identifier |id|. Calls |callback| on
// completion with true on success. // completion with true on success.
void StoreDeviceData(const std::string& id, void StoreDeviceData(const std::string& id,
const std::vector<uint8_t>& data, const std::unique_ptr<std::vector<uint8_t>> data,
base::OnceCallback<void(bool)> callback); base::OnceCallback<void(bool)> callback);
// Retrieves the data associated with the identifier |id|. Calls |callback| on // Retrieves the data associated with the identifier |id|. Calls |callback| on
......
...@@ -266,7 +266,7 @@ EnterpriseReportingPrivateSetDeviceDataFunction::Run() { ...@@ -266,7 +266,7 @@ EnterpriseReportingPrivateSetDeviceDataFunction::Run() {
{base::ThreadPool(), base::MayBlock(), {base::ThreadPool(), base::MayBlock(),
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce( base::BindOnce(
&StoreDeviceData, params->id, params->data, &StoreDeviceData, params->id, std::move(params->data),
base::BindOnce( base::BindOnce(
&EnterpriseReportingPrivateSetDeviceDataFunction::OnDataStored, &EnterpriseReportingPrivateSetDeviceDataFunction::OnDataStored,
this))); this)));
......
...@@ -242,6 +242,29 @@ TEST_F(EnterpriseReportingPrivateDeviceDataFunctionsTest, RetrieveDeviceData) { ...@@ -242,6 +242,29 @@ TEST_F(EnterpriseReportingPrivateDeviceDataFunctionsTest, RetrieveDeviceData) {
ASSERT_TRUE(single_result); ASSERT_TRUE(single_result);
ASSERT_TRUE(single_result->is_blob()); ASSERT_TRUE(single_result->is_blob());
EXPECT_EQ(base::Value::BlobStorage({1, 2, 3}), single_result->GetBlob()); EXPECT_EQ(base::Value::BlobStorage({1, 2, 3}), single_result->GetBlob());
// Clear the data and check that it is gone.
auto set_function2 =
base::MakeRefCounted<EnterpriseReportingPrivateSetDeviceDataFunction>();
std::unique_ptr<base::ListValue> reset_values =
std::make_unique<base::ListValue>();
reset_values->AppendString("c");
extension_function_test_utils::RunFunction(set_function2.get(),
std::move(reset_values), browser(),
extensions::api_test_utils::NONE);
ASSERT_TRUE(set_function2->GetError().empty());
auto get_function2 =
base::MakeRefCounted<EnterpriseReportingPrivateGetDeviceDataFunction>();
std::unique_ptr<base::ListValue> values2 =
std::make_unique<base::ListValue>();
values2->AppendString("c");
extension_function_test_utils::RunFunction(get_function2.get(),
std::move(values2), browser(),
extensions::api_test_utils::NONE);
ASSERT_TRUE(get_function2->GetResultList());
EXPECT_EQ(0u, get_function2->GetResultList()->GetSize());
EXPECT_FALSE(get_function2->GetError().empty());
} }
// TODO(pastarmovj): Remove once implementation for the other platform exists. // TODO(pastarmovj): Remove once implementation for the other platform exists.
......
...@@ -55,9 +55,11 @@ namespace enterprise.reportingPrivate { ...@@ -55,9 +55,11 @@ namespace enterprise.reportingPrivate {
// Gets the device data for |id|. Sets runtime.lastError on failure. // Gets the device data for |id|. Sets runtime.lastError on failure.
static void getDeviceData(DOMString id, GetDeviceDataCallback callback); static void getDeviceData(DOMString id, GetDeviceDataCallback callback);
// Sets the device data for |id|. Sets runtime.lastError on failure. // Sets the device data for |id|. Sets runtime.lastError on failure. If the
// |data| parameter is undefined and there is already data associated with
// |id| it will be cleared.
static void setDeviceData(DOMString id, static void setDeviceData(DOMString id,
ArrayBuffer data, optional ArrayBuffer data,
optional DoneCallback callback); optional DoneCallback callback);
// Gets the device information (including disk encryption status, // Gets the device information (including disk encryption status,
......
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