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
{
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
)) {
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
) {
46 base::FilePath
path(lockfile_path
);
47 scoped_ptr
<base::Value
> contents(DeserializeJsonFromFile(path
));
48 base::ListValue
* dump_list
= GetDumpList(contents
.get());
53 for (base::Value
* elem
: *dump_list
) {
54 scoped_ptr
<DumpInfo
> dump
= make_scoped_ptr(new DumpInfo(elem
));
58 dumps
->push_back(dump
.Pass());
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());
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
));
97 CreateLockFile(lockfile_path
);
98 if (!(contents
= DeserializeJsonFromFile(path
))) {
103 base::ListValue
* dump_list
= GetDumpList(contents
.get());
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
)) {
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