OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/update_client/persisted_data.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/files/important_file_writer.h" | |
14 #include "base/json/json_file_value_serializer.h" | |
15 #include "base/json/json_writer.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/sequenced_task_runner.h" | |
20 #include "base/single_thread_task_runner.h" | |
21 #include "base/thread_task_runner_handle.h" | |
22 #include "base/values.h" | |
23 | |
24 namespace update_client { | |
25 | |
26 PersistedData::PersistedData( | |
27 const base::FilePath& path, | |
28 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
29 : path_(path), | |
30 file_writer_(path, task_runner), | |
31 ready_(false), | |
32 task_runner_(task_runner) { | |
33 task_runner_->PostTask( | |
34 FROM_HERE, | |
35 base::Bind(&PersistedData::Initialize, | |
36 scoped_refptr<PersistedData>(this), | |
37 base::ThreadTaskRunnerHandle::Get())); | |
38 } | |
39 | |
40 PersistedData::~PersistedData() { | |
41 } | |
42 | |
43 void PersistedData::Initialize( | |
44 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) { | |
45 DCHECK(!ready_); | |
46 LoadFromFile(); | |
47 main_task_runner->PostTask( | |
48 FROM_HERE, | |
49 base::Bind(&PersistedData::Ready, | |
50 scoped_refptr<PersistedData>(this))); | |
51 } | |
52 | |
53 void PersistedData::LoadFromFile() { | |
54 if (path_.empty() || !base::PathExists(path_)) { | |
55 metadata_ = scoped_ptr<base::DictionaryValue>(new base::DictionaryValue()); | |
56 return; | |
57 } | |
58 JSONFileValueDeserializer deserializer(path_); | |
59 std::string error; | |
60 scoped_ptr<base::Value> root = deserializer.Deserialize(NULL, &error); | |
61 metadata_ = root.get() && root->IsType(base::Value::TYPE_DICTIONARY) | |
62 ? 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.
| |
63 static_cast<base::DictionaryValue*>(root.release())) | |
64 : scoped_ptr<base::DictionaryValue>(new base::DictionaryValue()); | |
65 } | |
66 | |
67 void PersistedData::Ready() { | |
68 DCHECK(thread_checker_.CalledOnValidThread()); | |
69 ready_ = true; | |
70 } | |
71 | |
72 int PersistedData::DateLastRollCall(const std::string& id) const { | |
73 DCHECK(thread_checker_.CalledOnValidThread()); | |
74 if (!ready_) | |
75 return -2; | |
76 DCHECK(metadata_); | |
77 int dlrc = -2; | |
78 // We assume ids do not contain '.' characters. | |
79 if (!metadata_->GetInteger("apps." + id + ".dlrc", &dlrc)) | |
80 return -2; | |
81 return dlrc; | |
82 } | |
83 | |
84 void PersistedData::SetDateLastRollCall( | |
85 const std::vector<std::string>& ids, int datenum) { | |
86 DCHECK(thread_checker_.CalledOnValidThread()); | |
87 if (!ready_) // Trying to write before we've ever read it: drop the update. | |
88 return; | |
89 if (datenum < 0) | |
90 return; | |
91 DCHECK(metadata_); | |
92 for (auto id : ids) { | |
93 // We assume ids do not contain '.' characters. | |
94 metadata_->SetInteger("apps." + id + ".dlrc", datenum); | |
95 } | |
96 if (path_.empty()) | |
97 return; | |
98 scoped_ptr<std::string> serialized(new std::string()); | |
99 base::JSONWriter::Write(*metadata_, serialized.get()); | |
100 file_writer_.WriteNow(std::move(serialized)); | |
101 } | |
102 | |
103 } // namespace update_client | |
OLD | NEW |