Chromium Code Reviews| Index: components/update_client/persisted_data.cc |
| diff --git a/components/update_client/persisted_data.cc b/components/update_client/persisted_data.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2ee57badd4063fc7bd9f602fb7677d75b35b4950 |
| --- /dev/null |
| +++ b/components/update_client/persisted_data.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2016 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/update_client/persisted_data.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/important_file_writer.h" |
| +#include "base/json/json_file_value_serializer.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "base/values.h" |
| + |
| +namespace update_client { |
| + |
| +PersistedData::PersistedData( |
| + const base::FilePath& path, |
| + const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| + : path_(path), |
| + file_writer_(path, task_runner), |
| + ready_(false), |
| + task_runner_(task_runner) { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PersistedData::Initialize, |
| + scoped_refptr<PersistedData>(this), |
| + base::ThreadTaskRunnerHandle::Get())); |
| +} |
| + |
| +PersistedData::~PersistedData() { |
| +} |
| + |
| +void PersistedData::Initialize( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) { |
| + DCHECK(!ready_); |
| + LoadFromFile(); |
| + main_task_runner->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PersistedData::Ready, |
| + scoped_refptr<PersistedData>(this))); |
| +} |
| + |
| +void PersistedData::LoadFromFile() { |
| + if (path_.empty() || !base::PathExists(path_)) { |
| + metadata_ = scoped_ptr<base::DictionaryValue>(new base::DictionaryValue()); |
| + return; |
| + } |
| + JSONFileValueDeserializer deserializer(path_); |
| + std::string error; |
| + scoped_ptr<base::Value> root = deserializer.Deserialize(NULL, &error); |
| + metadata_ = root.get() && root->IsType(base::Value::TYPE_DICTIONARY) |
| + ? scoped_ptr<base::DictionaryValue>( |
|
Sorin Jianu
2016/04/07 18:53:50
could do the ?: as an argument to scoped_ptr<base:
waffles
2016/04/07 22:45:18
Good point, but moot.
|
| + static_cast<base::DictionaryValue*>(root.release())) |
| + : scoped_ptr<base::DictionaryValue>(new base::DictionaryValue()); |
| +} |
| + |
| +void PersistedData::Ready() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + ready_ = true; |
| +} |
| + |
| +int PersistedData::DateLastRollCall(const std::string& id) const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!ready_) |
| + return -2; |
| + DCHECK(metadata_); |
| + int dlrc = -2; |
| + // We assume ids do not contain '.' characters. |
| + if (!metadata_->GetInteger("apps." + id + ".dlrc", &dlrc)) |
| + return -2; |
| + return dlrc; |
| +} |
| + |
| +void PersistedData::SetDateLastRollCall( |
| + const std::vector<std::string>& ids, int datenum) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!ready_) // Trying to write before we've ever read it: drop the update. |
| + return; |
| + if (datenum < 0) |
| + return; |
| + DCHECK(metadata_); |
| + for (auto id : ids) { |
| + // We assume ids do not contain '.' characters. |
| + metadata_->SetInteger("apps." + id + ".dlrc", datenum); |
| + } |
| + if (path_.empty()) |
| + return; |
| + scoped_ptr<std::string> serialized(new std::string()); |
| + base::JSONWriter::Write(*metadata_, serialized.get()); |
| + file_writer_.WriteNow(std::move(serialized)); |
| +} |
| + |
| +} // namespace update_client |