Chromium Code Reviews| 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/component_metadata.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 ComponentMetadata::ComponentMetadata( | |
| 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(&ComponentMetadata::Initialize, | |
| 36 scoped_refptr<ComponentMetadata>(this), | |
|
Sorin Jianu
2016/04/07 04:03:19
is the cast needed, here and below?
waffles
2016/04/07 17:04:43
Not sure about this one, let's discuss offline. Th
Sorin Jianu
2016/04/07 18:27:35
If the code compiles without the function-style ca
| |
| 37 base::ThreadTaskRunnerHandle::Get())); | |
| 38 } | |
| 39 | |
| 40 ComponentMetadata::~ComponentMetadata() { | |
| 41 } | |
| 42 | |
| 43 void ComponentMetadata::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(&ComponentMetadata::Ready, | |
| 50 scoped_refptr<ComponentMetadata>(this))); | |
| 51 } | |
| 52 | |
| 53 void ComponentMetadata::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 if (!root.get() || !root->IsType(base::Value::TYPE_DICTIONARY)) { | |
|
Sorin Jianu
2016/04/07 04:03:19
Can 61-66 be written using ?
matadata_ = scoped_p
waffles
2016/04/07 17:04:43
Done.
| |
| 62 metadata_ = scoped_ptr<base::DictionaryValue>(new base::DictionaryValue()); | |
| 63 return; | |
| 64 } | |
| 65 metadata_ = scoped_ptr<base::DictionaryValue>( | |
| 66 static_cast<base::DictionaryValue*>(root.release())); | |
| 67 } | |
| 68 | |
| 69 void ComponentMetadata::Ready() { | |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 71 ready_ = true; | |
| 72 } | |
| 73 | |
| 74 int ComponentMetadata::DateLastRollCall(const std::string& id) const { | |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 76 if (!ready_) | |
| 77 return -2; | |
| 78 DCHECK(metadata_); | |
| 79 int dlrc; | |
|
Sorin Jianu
2016/04/07 04:03:19
not initialized
waffles
2016/04/07 17:04:43
Done.
| |
| 80 // We assume ids do not contain '.' characters. | |
| 81 if (!metadata_->GetInteger("apps." + id + ".dlrc", &dlrc)) | |
| 82 return -2; | |
| 83 return dlrc; | |
| 84 } | |
| 85 | |
| 86 void ComponentMetadata::SetDateLastRollCall( | |
| 87 const std::vector<std::string>& ids, int datenum) { | |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 89 if (!ready_) // Trying to write before we've ever read it: drop the update. | |
|
Sorin Jianu
2016/04/07 04:03:19
Will this cause any issue? Is it ok to silently fa
waffles
2016/04/07 17:04:43
At this time, it's not clear to me what the caller
Sorin Jianu
2016/04/07 18:27:35
The caller could assert, if it wants to enforce so
| |
| 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 |