12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // Copyright 2019 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 "components/sync/nigori/nigori_storage_impl.h"
- #include <string>
- #include "base/files/file_util.h"
- #include "base/files/important_file_writer.h"
- #include "base/logging.h"
- #include "components/os_crypt/os_crypt.h"
- namespace syncer {
- NigoriStorageImpl::NigoriStorageImpl(const base::FilePath& path)
- : path_(path) {}
- NigoriStorageImpl::~NigoriStorageImpl() {
- DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
- }
- void NigoriStorageImpl::StoreData(const sync_pb::NigoriLocalData& data) {
- DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
- std::string serialized_data = data.SerializeAsString();
- if (serialized_data.empty()) {
- DLOG(ERROR) << "Failed to serialize NigoriLocalData.";
- return;
- }
- std::string encrypted_data;
- if (!OSCrypt::EncryptString(serialized_data, &encrypted_data)) {
- DLOG(ERROR) << "Failed to encrypt NigoriLocalData.";
- return;
- }
- if (!base::ImportantFileWriter::WriteFileAtomically(path_, encrypted_data)) {
- DLOG(ERROR) << "Failed to write NigoriLocalData into file.";
- }
- }
- absl::optional<sync_pb::NigoriLocalData> NigoriStorageImpl::RestoreData() {
- DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
- if (!base::PathExists(path_)) {
- return absl::nullopt;
- }
- std::string encrypted_data;
- if (!base::ReadFileToString(path_, &encrypted_data)) {
- DLOG(ERROR) << "Failed to read NigoriLocalData from file.";
- return absl::nullopt;
- }
- std::string serialized_data;
- if (!OSCrypt::DecryptString(encrypted_data, &serialized_data)) {
- DLOG(ERROR) << "Failed to decrypt NigoriLocalData.";
- return absl::nullopt;
- }
- sync_pb::NigoriLocalData data;
- if (!data.ParseFromString(serialized_data)) {
- DLOG(ERROR) << "Failed to parse NigoriLocalData.";
- return absl::nullopt;
- }
- return data;
- }
- void NigoriStorageImpl::ClearData() {
- base::DeleteFile(path_);
- }
- } // namespace syncer
|