| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromecast/crash/linux/crash_testing_utils.h" | 5 #include "chromecast/crash/linux/crash_testing_utils.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 std::unique_ptr<base::ListValue> ParseLockFile(const std::string& path) { | 33 std::unique_ptr<base::ListValue> ParseLockFile(const std::string& path) { |
| 34 std::string lockfile_string; | 34 std::string lockfile_string; |
| 35 RCHECK(base::ReadFileToString(base::FilePath(path), &lockfile_string), | 35 RCHECK(base::ReadFileToString(base::FilePath(path), &lockfile_string), |
| 36 nullptr, | 36 nullptr, |
| 37 "Failed to read file"); | 37 "Failed to read file"); |
| 38 | 38 |
| 39 std::vector<std::string> lines = base::SplitString( | 39 std::vector<std::string> lines = base::SplitString( |
| 40 lockfile_string, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 40 lockfile_string, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 41 | 41 |
| 42 std::unique_ptr<base::ListValue> dumps = | 42 std::unique_ptr<base::ListValue> dumps = base::MakeUnique<base::ListValue>(); |
| 43 base::WrapUnique(new base::ListValue()); | |
| 44 | 43 |
| 45 // Validate dumps | 44 // Validate dumps |
| 46 for (const std::string& line : lines) { | 45 for (const std::string& line : lines) { |
| 47 if (line.size() == 0) | 46 if (line.size() == 0) |
| 48 continue; | 47 continue; |
| 49 std::unique_ptr<base::Value> dump_info = DeserializeFromJson(line); | 48 std::unique_ptr<base::Value> dump_info = DeserializeFromJson(line); |
| 50 DumpInfo info(dump_info.get()); | 49 DumpInfo info(dump_info.get()); |
| 51 RCHECK(info.valid(), nullptr, "Invalid DumpInfo"); | 50 RCHECK(info.valid(), nullptr, "Invalid DumpInfo"); |
| 52 dumps->Append(std::move(dump_info)); | 51 dumps->Append(std::move(dump_info)); |
| 53 } | 52 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 77 | 76 |
| 78 bool WriteMetadataFile(const std::string& path, const base::Value* metadata) { | 77 bool WriteMetadataFile(const std::string& path, const base::Value* metadata) { |
| 79 DCHECK(metadata); | 78 DCHECK(metadata); |
| 80 return SerializeJsonToFile(base::FilePath(path), *metadata); | 79 return SerializeJsonToFile(base::FilePath(path), *metadata); |
| 81 } | 80 } |
| 82 | 81 |
| 83 } // namespace | 82 } // namespace |
| 84 | 83 |
| 85 std::unique_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) { | 84 std::unique_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) { |
| 86 std::unique_ptr<base::Value> value(DeserializeFromJson(json_string)); | 85 std::unique_ptr<base::Value> value(DeserializeFromJson(json_string)); |
| 87 return base::WrapUnique(new DumpInfo(value.get())); | 86 return base::MakeUnique<DumpInfo>(value.get()); |
| 88 } | 87 } |
| 89 | 88 |
| 90 bool FetchDumps(const std::string& lockfile_path, | 89 bool FetchDumps(const std::string& lockfile_path, |
| 91 std::vector<std::unique_ptr<DumpInfo>>* dumps) { | 90 std::vector<std::unique_ptr<DumpInfo>>* dumps) { |
| 92 DCHECK(dumps); | 91 DCHECK(dumps); |
| 93 std::unique_ptr<base::ListValue> dump_list = ParseLockFile(lockfile_path); | 92 std::unique_ptr<base::ListValue> dump_list = ParseLockFile(lockfile_path); |
| 94 RCHECK(dump_list, false, "Failed to parse lockfile"); | 93 RCHECK(dump_list, false, "Failed to parse lockfile"); |
| 95 | 94 |
| 96 dumps->clear(); | 95 dumps->clear(); |
| 97 | 96 |
| 98 for (const auto& elem : *dump_list) { | 97 for (const auto& elem : *dump_list) { |
| 99 std::unique_ptr<DumpInfo> dump(new DumpInfo(elem.get())); | 98 std::unique_ptr<DumpInfo> dump(new DumpInfo(elem.get())); |
| 100 RCHECK(dump->valid(), false, "Invalid DumpInfo"); | 99 RCHECK(dump->valid(), false, "Invalid DumpInfo"); |
| 101 dumps->push_back(std::move(dump)); | 100 dumps->push_back(std::move(dump)); |
| 102 } | 101 } |
| 103 | 102 |
| 104 return true; | 103 return true; |
| 105 } | 104 } |
| 106 | 105 |
| 107 bool ClearDumps(const std::string& lockfile_path) { | 106 bool ClearDumps(const std::string& lockfile_path) { |
| 108 std::unique_ptr<base::ListValue> dump_list = | 107 std::unique_ptr<base::ListValue> dump_list = |
| 109 base::WrapUnique(new base::ListValue()); | 108 base::MakeUnique<base::ListValue>(); |
| 110 return WriteLockFile(lockfile_path, dump_list.get()) == 0; | 109 return WriteLockFile(lockfile_path, dump_list.get()) == 0; |
| 111 } | 110 } |
| 112 | 111 |
| 113 bool CreateFiles(const std::string& lockfile_path, | 112 bool CreateFiles(const std::string& lockfile_path, |
| 114 const std::string& metadata_path) { | 113 const std::string& metadata_path) { |
| 115 std::unique_ptr<base::DictionaryValue> metadata = | 114 std::unique_ptr<base::DictionaryValue> metadata = |
| 116 base::WrapUnique(new base::DictionaryValue()); | 115 base::MakeUnique<base::DictionaryValue>(); |
| 117 | 116 |
| 118 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue(); | 117 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue(); |
| 119 metadata->Set(kRatelimitKey, base::WrapUnique(ratelimit_fields)); | 118 metadata->Set(kRatelimitKey, base::WrapUnique(ratelimit_fields)); |
| 120 ratelimit_fields->SetDouble(kRatelimitPeriodStartKey, 0.0); | 119 ratelimit_fields->SetDouble(kRatelimitPeriodStartKey, 0.0); |
| 121 ratelimit_fields->SetInteger(kRatelimitPeriodDumpsKey, 0); | 120 ratelimit_fields->SetInteger(kRatelimitPeriodDumpsKey, 0); |
| 122 | 121 |
| 123 std::unique_ptr<base::ListValue> dumps = | 122 std::unique_ptr<base::ListValue> dumps = base::MakeUnique<base::ListValue>(); |
| 124 base::WrapUnique(new base::ListValue()); | |
| 125 | 123 |
| 126 return WriteLockFile(lockfile_path, dumps.get()) == 0 && | 124 return WriteLockFile(lockfile_path, dumps.get()) == 0 && |
| 127 WriteMetadataFile(metadata_path, metadata.get()); | 125 WriteMetadataFile(metadata_path, metadata.get()); |
| 128 } | 126 } |
| 129 | 127 |
| 130 bool AppendLockFile(const std::string& lockfile_path, | 128 bool AppendLockFile(const std::string& lockfile_path, |
| 131 const std::string& metadata_path, | 129 const std::string& metadata_path, |
| 132 const DumpInfo& dump) { | 130 const DumpInfo& dump) { |
| 133 std::unique_ptr<base::ListValue> contents = ParseLockFile(lockfile_path); | 131 std::unique_ptr<base::ListValue> contents = ParseLockFile(lockfile_path); |
| 134 if (!contents) { | 132 if (!contents) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 152 if (!contents || !contents->GetAsDictionary(&dict) || | 150 if (!contents || !contents->GetAsDictionary(&dict) || |
| 153 !dict->GetDictionary(kRatelimitKey, &ratelimit_params)) { | 151 !dict->GetDictionary(kRatelimitKey, &ratelimit_params)) { |
| 154 return false; | 152 return false; |
| 155 } | 153 } |
| 156 | 154 |
| 157 ratelimit_params->SetDouble(kRatelimitPeriodStartKey, start.ToDoubleT()); | 155 ratelimit_params->SetDouble(kRatelimitPeriodStartKey, start.ToDoubleT()); |
| 158 return WriteMetadataFile(metadata_path, contents.get()) == 0; | 156 return WriteMetadataFile(metadata_path, contents.get()) == 0; |
| 159 } | 157 } |
| 160 | 158 |
| 161 } // namespace chromecast | 159 } // namespace chromecast |
| OLD | NEW |