| Index: chromecast/crash/linux/crash_testing_utils.cc
|
| diff --git a/chromecast/crash/linux/crash_testing_utils.cc b/chromecast/crash/linux/crash_testing_utils.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..61b9ea13f313f77cd0304e34936deb0027478e9a
|
| --- /dev/null
|
| +++ b/chromecast/crash/linux/crash_testing_utils.cc
|
| @@ -0,0 +1,93 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chromecast/crash/linux/crash_testing_utils.h"
|
| +
|
| +#include "base/files/file_util.h"
|
| +#include "base/values.h"
|
| +#include "chromecast/base/serializers.h"
|
| +#include "chromecast/crash/linux/dump_info.h"
|
| +
|
| +namespace chromecast {
|
| +namespace {
|
| +
|
| +const char kDumpsKey[] = "dumps";
|
| +
|
| +// Gets the list of deserialized DumpInfo given a deserialized |lockfile|.
|
| +base::ListValue* GetDumpList(base::Value* lockfile) {
|
| + base::DictionaryValue* dict;
|
| + base::ListValue* dump_list;
|
| + if (!lockfile || !lockfile->GetAsDictionary(&dict) ||
|
| + !dict->GetList(kDumpsKey, &dump_list)) {
|
| + return nullptr;
|
| + }
|
| +
|
| + return dump_list;
|
| +}
|
| +
|
| +} // namespcae
|
| +
|
| +scoped_ptr<DumpInfo> CreateDumpInfo(const std::string& json_string) {
|
| + scoped_ptr<base::Value> value(DeserializeFromJson(json_string));
|
| + return make_scoped_ptr(new DumpInfo(value.get()));
|
| +}
|
| +
|
| +bool FetchDumps(const std::string& lockfile_path,
|
| + ScopedVector<DumpInfo>* dumps) {
|
| + if (!dumps) {
|
| + return false;
|
| + }
|
| + dumps->clear();
|
| +
|
| + base::FilePath path(lockfile_path);
|
| + scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
|
| + base::ListValue* dump_list = GetDumpList(contents.get());
|
| + if (!dump_list) {
|
| + return false;
|
| + }
|
| +
|
| + for (base::Value* elem : *dump_list) {
|
| + scoped_ptr<DumpInfo> dump = make_scoped_ptr(new DumpInfo(elem));
|
| + if (!dump->valid()) {
|
| + return false;
|
| + }
|
| + dumps->push_back(dump.Pass());
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool CreateLockFile(const std::string& lockfile_path) {
|
| + scoped_ptr<base::DictionaryValue> output =
|
| + make_scoped_ptr(new base::DictionaryValue());
|
| + output->Set(kDumpsKey, make_scoped_ptr(new base::ListValue()));
|
| +
|
| + base::FilePath path(lockfile_path);
|
| +
|
| + const base::Value* val = output.get();
|
| + return SerializeJsonToFile(path, *val);
|
| +}
|
| +
|
| +bool AppendLockFile(const std::string& lockfile_path, const DumpInfo& dump) {
|
| + base::FilePath path(lockfile_path);
|
| +
|
| + scoped_ptr<base::Value> contents(DeserializeJsonFromFile(path));
|
| + if (!contents) {
|
| + CreateLockFile(lockfile_path);
|
| + if (!(contents = DeserializeJsonFromFile(path))) {
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + base::ListValue* dump_list = GetDumpList(contents.get());
|
| + if (!dump_list) {
|
| + return false;
|
| + }
|
| +
|
| + dump_list->Append(dump.GetAsValue());
|
| +
|
| + return SerializeJsonToFile(path, *contents);
|
| +}
|
| +
|
| +} // namespace chromecast
|
|
|