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/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chromecast/base/path_utils.h" | 14 #include "chromecast/base/path_utils.h" |
14 #include "chromecast/base/serializers.h" | 15 #include "chromecast/base/serializers.h" |
15 #include "chromecast/crash/linux/dump_info.h" | 16 #include "chromecast/crash/linux/dump_info.h" |
16 | 17 |
17 #define RCHECK(cond, retval, err) \ | 18 #define RCHECK(cond, retval, err) \ |
18 do { \ | 19 do { \ |
19 LOG(ERROR) << (err); \ | 20 LOG(ERROR) << (err); \ |
20 if (!(cond)) { \ | 21 if (!(cond)) { \ |
21 return (retval); \ | 22 return (retval); \ |
22 } \ | 23 } \ |
23 } while (0) | 24 } while (0) |
24 | 25 |
25 namespace chromecast { | 26 namespace chromecast { |
26 namespace { | 27 namespace { |
27 | 28 |
28 const char kRatelimitKey[] = "ratelimit"; | 29 const char kRatelimitKey[] = "ratelimit"; |
29 const char kRatelimitPeriodStartKey[] = "period_start"; | 30 const char kRatelimitPeriodStartKey[] = "period_start"; |
30 const char kRatelimitPeriodDumpsKey[] = "period_dumps"; | 31 const char kRatelimitPeriodDumpsKey[] = "period_dumps"; |
31 | 32 |
32 scoped_ptr<base::ListValue> ParseLockFile(const std::string& path) { | 33 std::unique_ptr<base::ListValue> ParseLockFile(const std::string& path) { |
33 std::string lockfile_string; | 34 std::string lockfile_string; |
34 RCHECK(base::ReadFileToString(base::FilePath(path), &lockfile_string), | 35 RCHECK(base::ReadFileToString(base::FilePath(path), &lockfile_string), |
35 nullptr, | 36 nullptr, |
36 "Failed to read file"); | 37 "Failed to read file"); |
37 | 38 |
38 std::vector<std::string> lines = base::SplitString( | 39 std::vector<std::string> lines = base::SplitString( |
39 lockfile_string, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 40 lockfile_string, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
40 | 41 |
41 scoped_ptr<base::ListValue> dumps = make_scoped_ptr(new base::ListValue()); | 42 std::unique_ptr<base::ListValue> dumps = |
| 43 base::WrapUnique(new base::ListValue()); |
42 | 44 |
43 // Validate dumps | 45 // Validate dumps |
44 for (const std::string& line : lines) { | 46 for (const std::string& line : lines) { |
45 if (line.size() == 0) | 47 if (line.size() == 0) |
46 continue; | 48 continue; |
47 scoped_ptr<base::Value> dump_info = DeserializeFromJson(line); | 49 std::unique_ptr<base::Value> dump_info = DeserializeFromJson(line); |
48 DumpInfo info(dump_info.get()); | 50 DumpInfo info(dump_info.get()); |
49 RCHECK(info.valid(), nullptr, "Invalid DumpInfo"); | 51 RCHECK(info.valid(), nullptr, "Invalid DumpInfo"); |
50 dumps->Append(std::move(dump_info)); | 52 dumps->Append(std::move(dump_info)); |
51 } | 53 } |
52 | 54 |
53 return dumps; | 55 return dumps; |
54 } | 56 } |
55 | 57 |
56 scoped_ptr<base::Value> ParseMetadataFile(const std::string& path) { | 58 std::unique_ptr<base::Value> ParseMetadataFile(const std::string& path) { |
57 return DeserializeJsonFromFile(base::FilePath(path)); | 59 return DeserializeJsonFromFile(base::FilePath(path)); |
58 } | 60 } |
59 | 61 |
60 int WriteLockFile(const std::string& path, base::ListValue* contents) { | 62 int WriteLockFile(const std::string& path, base::ListValue* contents) { |
61 DCHECK(contents); | 63 DCHECK(contents); |
62 std::string lockfile; | 64 std::string lockfile; |
63 | 65 |
64 for (const base::Value* elem : *contents) { | 66 for (const base::Value* elem : *contents) { |
65 scoped_ptr<std::string> dump_info = SerializeToJson(*elem); | 67 std::unique_ptr<std::string> dump_info = SerializeToJson(*elem); |
66 RCHECK(dump_info, -1, "Failed to serialize DumpInfo"); | 68 RCHECK(dump_info, -1, "Failed to serialize DumpInfo"); |
67 lockfile += *dump_info; | 69 lockfile += *dump_info; |
68 lockfile += "\n"; // Add line seperatators | 70 lockfile += "\n"; // Add line seperatators |
69 } | 71 } |
70 | 72 |
71 return WriteFile(base::FilePath(path), lockfile.c_str(), lockfile.size()) >= 0 | 73 return WriteFile(base::FilePath(path), lockfile.c_str(), lockfile.size()) >= 0 |
72 ? 0 | 74 ? 0 |
73 : -1; | 75 : -1; |
74 } | 76 } |
75 | 77 |
76 bool WriteMetadataFile(const std::string& path, const base::Value* metadata) { | 78 bool WriteMetadataFile(const std::string& path, const base::Value* metadata) { |
77 DCHECK(metadata); | 79 DCHECK(metadata); |
78 return SerializeJsonToFile(base::FilePath(path), *metadata); | 80 return SerializeJsonToFile(base::FilePath(path), *metadata); |
79 } | 81 } |
80 | 82 |
81 } // namespace | 83 } // namespace |
82 | 84 |
83 scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) { | 85 std::unique_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) { |
84 scoped_ptr<base::Value> value(DeserializeFromJson(json_string)); | 86 std::unique_ptr<base::Value> value(DeserializeFromJson(json_string)); |
85 return make_scoped_ptr(new DumpInfo(value.get())); | 87 return base::WrapUnique(new DumpInfo(value.get())); |
86 } | 88 } |
87 | 89 |
88 bool FetchDumps(const std::string& lockfile_path, | 90 bool FetchDumps(const std::string& lockfile_path, |
89 ScopedVector<DumpInfo>* dumps) { | 91 ScopedVector<DumpInfo>* dumps) { |
90 DCHECK(dumps); | 92 DCHECK(dumps); |
91 scoped_ptr<base::ListValue> dump_list = ParseLockFile(lockfile_path); | 93 std::unique_ptr<base::ListValue> dump_list = ParseLockFile(lockfile_path); |
92 RCHECK(dump_list, false, "Failed to parse lockfile"); | 94 RCHECK(dump_list, false, "Failed to parse lockfile"); |
93 | 95 |
94 dumps->clear(); | 96 dumps->clear(); |
95 | 97 |
96 for (base::Value* elem : *dump_list) { | 98 for (base::Value* elem : *dump_list) { |
97 scoped_ptr<DumpInfo> dump = make_scoped_ptr(new DumpInfo(elem)); | 99 std::unique_ptr<DumpInfo> dump = base::WrapUnique(new DumpInfo(elem)); |
98 RCHECK(dump->valid(), false, "Invalid DumpInfo"); | 100 RCHECK(dump->valid(), false, "Invalid DumpInfo"); |
99 dumps->push_back(std::move(dump)); | 101 dumps->push_back(std::move(dump)); |
100 } | 102 } |
101 | 103 |
102 return true; | 104 return true; |
103 } | 105 } |
104 | 106 |
105 bool ClearDumps(const std::string& lockfile_path) { | 107 bool ClearDumps(const std::string& lockfile_path) { |
106 scoped_ptr<base::ListValue> dump_list = | 108 std::unique_ptr<base::ListValue> dump_list = |
107 make_scoped_ptr(new base::ListValue()); | 109 base::WrapUnique(new base::ListValue()); |
108 return WriteLockFile(lockfile_path, dump_list.get()) == 0; | 110 return WriteLockFile(lockfile_path, dump_list.get()) == 0; |
109 } | 111 } |
110 | 112 |
111 bool CreateFiles(const std::string& lockfile_path, | 113 bool CreateFiles(const std::string& lockfile_path, |
112 const std::string& metadata_path) { | 114 const std::string& metadata_path) { |
113 scoped_ptr<base::DictionaryValue> metadata = | 115 std::unique_ptr<base::DictionaryValue> metadata = |
114 make_scoped_ptr(new base::DictionaryValue()); | 116 base::WrapUnique(new base::DictionaryValue()); |
115 | 117 |
116 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue(); | 118 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue(); |
117 metadata->Set(kRatelimitKey, make_scoped_ptr(ratelimit_fields)); | 119 metadata->Set(kRatelimitKey, base::WrapUnique(ratelimit_fields)); |
118 ratelimit_fields->SetString(kRatelimitPeriodStartKey, "0"); | 120 ratelimit_fields->SetString(kRatelimitPeriodStartKey, "0"); |
119 ratelimit_fields->SetInteger(kRatelimitPeriodDumpsKey, 0); | 121 ratelimit_fields->SetInteger(kRatelimitPeriodDumpsKey, 0); |
120 | 122 |
121 scoped_ptr<base::ListValue> dumps = make_scoped_ptr(new base::ListValue()); | 123 std::unique_ptr<base::ListValue> dumps = |
| 124 base::WrapUnique(new base::ListValue()); |
122 | 125 |
123 return WriteLockFile(lockfile_path, dumps.get()) == 0 && | 126 return WriteLockFile(lockfile_path, dumps.get()) == 0 && |
124 WriteMetadataFile(metadata_path, metadata.get()); | 127 WriteMetadataFile(metadata_path, metadata.get()); |
125 } | 128 } |
126 | 129 |
127 bool AppendLockFile(const std::string& lockfile_path, | 130 bool AppendLockFile(const std::string& lockfile_path, |
128 const std::string& metadata_path, | 131 const std::string& metadata_path, |
129 const DumpInfo& dump) { | 132 const DumpInfo& dump) { |
130 scoped_ptr<base::ListValue> contents = ParseLockFile(lockfile_path); | 133 std::unique_ptr<base::ListValue> contents = ParseLockFile(lockfile_path); |
131 if (!contents) { | 134 if (!contents) { |
132 CreateFiles(lockfile_path, metadata_path); | 135 CreateFiles(lockfile_path, metadata_path); |
133 if (!(contents = ParseLockFile(lockfile_path))) { | 136 if (!(contents = ParseLockFile(lockfile_path))) { |
134 return false; | 137 return false; |
135 } | 138 } |
136 } | 139 } |
137 | 140 |
138 contents->Append(dump.GetAsValue()); | 141 contents->Append(dump.GetAsValue()); |
139 | 142 |
140 return WriteLockFile(lockfile_path, contents.get()) == 0; | 143 return WriteLockFile(lockfile_path, contents.get()) == 0; |
141 } | 144 } |
142 | 145 |
143 bool SetRatelimitPeriodStart(const std::string& metadata_path, time_t start) { | 146 bool SetRatelimitPeriodStart(const std::string& metadata_path, time_t start) { |
144 scoped_ptr<base::Value> contents = ParseMetadataFile(metadata_path); | 147 std::unique_ptr<base::Value> contents = ParseMetadataFile(metadata_path); |
145 | 148 |
146 base::DictionaryValue* dict; | 149 base::DictionaryValue* dict; |
147 base::DictionaryValue* ratelimit_params; | 150 base::DictionaryValue* ratelimit_params; |
148 if (!contents || !contents->GetAsDictionary(&dict) || | 151 if (!contents || !contents->GetAsDictionary(&dict) || |
149 !dict->GetDictionary(kRatelimitKey, &ratelimit_params)) { | 152 !dict->GetDictionary(kRatelimitKey, &ratelimit_params)) { |
150 return false; | 153 return false; |
151 } | 154 } |
152 | 155 |
153 std::string period_start_str = | 156 std::string period_start_str = |
154 base::StringPrintf("%lld", static_cast<long long>(start)); | 157 base::StringPrintf("%lld", static_cast<long long>(start)); |
155 ratelimit_params->SetString(kRatelimitPeriodStartKey, period_start_str); | 158 ratelimit_params->SetString(kRatelimitPeriodStartKey, period_start_str); |
156 | 159 |
157 return WriteMetadataFile(metadata_path, contents.get()) == 0; | 160 return WriteMetadataFile(metadata_path, contents.get()) == 0; |
158 } | 161 } |
159 | 162 |
160 } // namespace chromecast | 163 } // namespace chromecast |
OLD | NEW |