Commit 0126d276 authored by Christos Froussios's avatar Christos Froussios Committed by Commit Bot

🔐 Introduce DestinationFileSystem

DestinationFileSystem is the desktop implemenation of Destination, which
sends the data to the file system.

Bug: 785237
Change-Id: I034fab562eafde2240dc75743cb2ebbc6f7ec9ac
Reviewed-on: https://chromium-review.googlesource.com/776596
Commit-Queue: Christos Froussios <cfroussios@chromium.org>
Reviewed-by: default avatarVaclav Brozek <vabr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517442}
parent 2e00483a
......@@ -136,6 +136,8 @@ split_static_library("ui") {
"page_info/page_info_ui.h",
"passwords/account_avatar_fetcher.cc",
"passwords/account_avatar_fetcher.h",
"passwords/destination_file_system.cc",
"passwords/destination_file_system.h",
"passwords/manage_passwords_state.cc",
"passwords/manage_passwords_state.h",
"passwords/manage_passwords_view_utils.cc",
......
// Copyright 2017 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 "chrome/browser/ui/passwords/destination_file_system.h"
#include <utility>
#include "base/files/file_util.h"
DestinationFileSystem::DestinationFileSystem(base::FilePath destination_path)
: destination_path_(std::move(destination_path)) {}
bool DestinationFileSystem::Write(const std::string& data) {
return base::WriteFile(destination_path_, data.c_str(), data.size());
}
// Copyright 2017 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.
#ifndef CHROME_BROWSER_UI_PASSWORDS_DESTINATION_FILE_SYSTEM_H_
#define CHROME_BROWSER_UI_PASSWORDS_DESTINATION_FILE_SYSTEM_H_
#include <string>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "components/password_manager/core/browser/export/destination.h"
class DestinationFileSystem : public password_manager::Destination {
public:
explicit DestinationFileSystem(base::FilePath destination_path);
virtual ~DestinationFileSystem() = default;
// password_manager::Destination
bool Write(const std::string& data) override;
private:
// The file, to which the data will be written.
const base::FilePath destination_path_;
DISALLOW_COPY_AND_ASSIGN(DestinationFileSystem);
};
#endif // CHROME_BROWSER_UI_PASSWORDS_DESTINATION_FILE_SYSTEM_H_
// Copyright 2017 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 "chrome/browser/ui/passwords/destination_file_system.h"
#include <memory>
#include "base/files/file_util.h"
#include "build/build_config.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class DestinationFileSystemTest : public testing::Test {
public:
DestinationFileSystemTest() = default;
~DestinationFileSystemTest() override = default;
void SetUp() override {
ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_));
destination_ = std::make_unique<DestinationFileSystem>(temp_file_);
}
void TearDown() override { ASSERT_TRUE(base::DeleteFile(temp_file_, false)); }
protected:
base::FilePath temp_file_;
std::unique_ptr<DestinationFileSystem> destination_;
private:
DISALLOW_COPY_AND_ASSIGN(DestinationFileSystemTest);
};
TEST_F(DestinationFileSystemTest, WriteToFile) {
const std::string kData = "Anything";
destination_->Write(kData);
std::string actual_written;
base::ReadFileToString(temp_file_, &actual_written);
EXPECT_EQ(kData, actual_written);
}
TEST_F(DestinationFileSystemTest, WriteNothingToFile) {
const std::string kData;
destination_->Write(kData);
std::string actual_written;
base::ReadFileToString(temp_file_, &actual_written);
EXPECT_EQ(kData, actual_written);
}
} // namespace
......@@ -9,7 +9,6 @@
#include <utility>
#include <vector>
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
......@@ -18,6 +17,7 @@
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/browser/ui/passwords/destination_file_system.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/grit/generated_resources.h"
#include "components/password_manager/core/browser/export/password_csv_writer.h"
......@@ -35,11 +35,6 @@
namespace {
// Wrapper for writing an std::string to a file.
void WriteToFile(const base::FilePath& path, std::string data) {
base::WriteFile(path, data.c_str(), data.size());
}
// The following are not used on Android due to the |SelectFileDialog| being
// unused.
#if !defined(OS_ANDROID)
......@@ -206,10 +201,14 @@ void PasswordManagerPorter::ExportPasswordsToPath(const base::FilePath& path) {
credential_provider_interface_->GetAllPasswords();
UMA_HISTOGRAM_COUNTS("PasswordManager.ExportedPasswordsPerUserInCSV",
password_list.size());
std::unique_ptr<DestinationFileSystem> destination(
new DestinationFileSystem(path));
base::PostTaskWithTraits(
FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
base::Bind(
&WriteToFile, path,
base::BindOnce(
base::IgnoreResult(&DestinationFileSystem::Write),
base::Passed(std::move(destination)),
base::Passed(password_manager::PasswordCSVWriter::SerializePasswords(
password_list))));
}
......@@ -2423,6 +2423,7 @@ test("unit_tests") {
"../browser/ui/find_bar/find_backend_unittest.cc",
"../browser/ui/login/login_handler_unittest.cc",
"../browser/ui/page_info/page_info_unittest.cc",
"../browser/ui/passwords/destination_file_system_unittest.cc",
"../browser/ui/passwords/manage_passwords_state_unittest.cc",
"../browser/ui/passwords/manage_passwords_view_utils_unittest.cc",
"../browser/ui/passwords/password_access_authenticator_unittest.cc",
......
......@@ -5,6 +5,8 @@
#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_EXPORT_DESTINATION_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_EXPORT_DESTINATION_H_
#include <string>
namespace password_manager {
// Interface of a medium, to where a serialised list of passwords can be
......@@ -13,8 +15,8 @@ class Destination {
public:
// Send the data to the destination, synchronously.
virtual bool Write(const std::string& data) = 0;
}
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_EXPORT_DESTINATION_H_
\ No newline at end of file
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_EXPORT_DESTINATION_H_
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