Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / chromecast / crash / linux / crash_testing_utils.cc
blob752fd9e7807d92c7030f0fc2627db4114127d2ec
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
3 // found in the LICENSE file.
5 #include "chromecast/crash/linux/crash_testing_utils.h"
7 #include "base/files/file_util.h"
8 #include "base/values.h"
9 #include "chromecast/base/serializers.h"
10 #include "chromecast/crash/linux/dump_info.h"
12 namespace chromecast {
13 namespace {
15 const char kDumpsKey[] = "dumps";
16 const char kRatelimitKey[] = "ratelimit";
17 const char kRatelimitPeriodStartKey[] = "period_start";
18 const char kRatelimitPeriodDumpsKey[] = "period_dumps";
20 // Gets the list of deserialized DumpInfo given a deserialized |lockfile|.
21 base::ListValue* GetDumpList(base::Value* lockfile) {
22 base::DictionaryValue* dict;
23 base::ListValue* dump_list;
24 if (!lockfile || !lockfile->GetAsDictionary(&dict) ||
25 !dict->GetList(kDumpsKey, &dump_list)) {
26 return nullptr;
29 return dump_list;
32 } // namespcae
34 scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) {
35 scoped_ptr<base::Value> value(DeserializeFromJson(json_string));
36 return make_scoped_ptr(new DumpInfo(value.get()));
39 bool FetchDumps(const std::string& lockfile_path,
40 ScopedVector<DumpInfo>* dumps) {
41 if (!dumps) {
42 return false;
44 dumps->clear();
46 base::FilePath path(lockfile_path);
47 scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
48 base::ListValue* dump_list = GetDumpList(contents.get());
49 if (!dump_list) {
50 return false;
53 for (base::Value* elem : *dump_list) {
54 scoped_ptr<DumpInfo> dump = make_scoped_ptr(new DumpInfo(elem));
55 if (!dump->valid()) {
56 return false;
58 dumps->push_back(dump.Pass());
61 return true;
64 bool ClearDumps(const std::string& lockfile_path) {
65 base::FilePath path(lockfile_path);
66 scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
67 base::ListValue* dump_list = GetDumpList(contents.get());
68 if (!dump_list) {
69 return false;
72 dump_list->Clear();
74 return SerializeJsonToFile(path, *contents);
77 bool CreateLockFile(const std::string& lockfile_path) {
78 scoped_ptr<base::DictionaryValue> output(
79 make_scoped_ptr(new base::DictionaryValue()));
80 output->Set(kDumpsKey, make_scoped_ptr(new base::ListValue()));
82 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue();
83 output->Set(kRatelimitKey, make_scoped_ptr(ratelimit_fields));
84 ratelimit_fields->SetString(kRatelimitPeriodStartKey, "0");
85 ratelimit_fields->SetInteger(kRatelimitPeriodDumpsKey, 0);
87 base::FilePath path(lockfile_path);
88 const base::Value* val = output.get();
89 return SerializeJsonToFile(path, *val);
92 bool AppendLockFile(const std::string& lockfile_path, const DumpInfo& dump) {
93 base::FilePath path(lockfile_path);
95 scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
96 if (!contents) {
97 CreateLockFile(lockfile_path);
98 if (!(contents = DeserializeJsonFromFile(path))) {
99 return false;
103 base::ListValue* dump_list = GetDumpList(contents.get());
104 if (!dump_list) {
105 return false;
108 dump_list->Append(dump.GetAsValue());
110 return SerializeJsonToFile(path, *contents);
113 bool SetRatelimitPeriodStart(const std::string& lockfile_path, time_t start) {
114 base::FilePath path(lockfile_path);
116 scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
118 base::DictionaryValue* dict;
119 base::DictionaryValue* ratelimit_params;
120 if (!contents || !contents->GetAsDictionary(&dict) ||
121 !dict->GetDictionary(kRatelimitKey, &ratelimit_params)) {
122 return false;
125 char buf[128];
126 snprintf(buf, sizeof(buf), "%lld", static_cast<long long>(start));
127 std::string period_start_str(buf);
128 ratelimit_params->SetString(kRatelimitPeriodStartKey, period_start_str);
130 return SerializeJsonToFile(path, *contents);
133 } // namespace chromecast