refactor: convert crash reporter to gin (#17952)

This commit is contained in:
Jeremy Apthorp 2019-05-01 15:19:11 -07:00 committed by GitHub
parent 493af7f84c
commit 3949f0bd50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 174 additions and 72 deletions

View file

@ -40,6 +40,7 @@
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/map_converter.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/native_mate_converters/network_converter.h"
#include "atom/common/native_mate_converters/once_callback.h"

View file

@ -10,11 +10,11 @@
#import <Squirrel/Squirrel.h>
#include "atom/browser/browser.h"
#include "atom/common/native_mate_converters/map_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "base/bind.h"
#include "base/strings/sys_string_conversions.h"
#include "base/time/time.h"
#include "native_mate/converter.h"
#include "native_mate/dictionary.h"
namespace auto_updater {

View file

@ -6,65 +6,58 @@
#include <string>
#include "atom/common/crash_reporter/crash_reporter.h"
#include "atom/common/gin_util.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/map_converter.h"
#include "base/bind.h"
#include "native_mate/dictionary.h"
#include "gin/data_object_builder.h"
#include "gin/dictionary.h"
#include "atom/common/node_includes.h"
using crash_reporter::CrashReporter;
namespace mate {
namespace gin {
template <>
struct Converter<CrashReporter::UploadReportResult> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const CrashReporter::UploadReportResult& reports) {
mate::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("date",
return gin::DataObjectBuilder(isolate)
.Set("date",
v8::Date::New(isolate->GetCurrentContext(), reports.first * 1000.0)
.ToLocalChecked());
dict.Set("id", reports.second);
return dict.GetHandle();
.ToLocalChecked())
.Set("id", reports.second)
.Build();
}
};
} // namespace mate
} // namespace gin
namespace {
void AddExtraParameter(const std::string& key, const std::string& value) {
CrashReporter::GetInstance()->AddExtraParameter(key, value);
}
void RemoveExtraParameter(const std::string& key) {
CrashReporter::GetInstance()->RemoveExtraParameter(key);
}
std::map<std::string, std::string> GetParameters() {
return CrashReporter::GetInstance()->GetParameters();
}
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
using gin_util::SetMethod;
auto reporter = base::Unretained(CrashReporter::GetInstance());
dict.SetMethod("start", base::BindRepeating(&CrashReporter::Start, reporter));
dict.SetMethod("addExtraParameter", &AddExtraParameter);
dict.SetMethod("removeExtraParameter", &RemoveExtraParameter);
dict.SetMethod("getParameters", &GetParameters);
dict.SetMethod(
"getUploadedReports",
base::BindRepeating(&CrashReporter::GetUploadedReports, reporter));
dict.SetMethod(
"setUploadToServer",
base::BindRepeating(&CrashReporter::SetUploadToServer, reporter));
dict.SetMethod(
"getUploadToServer",
base::BindRepeating(&CrashReporter::GetUploadToServer, reporter));
SetMethod(exports, "start",
base::BindRepeating(&CrashReporter::Start, reporter));
SetMethod(exports, "addExtraParameter",
base::BindRepeating(&CrashReporter::AddExtraParameter, reporter));
SetMethod(
exports, "removeExtraParameter",
base::BindRepeating(&CrashReporter::RemoveExtraParameter, reporter));
SetMethod(exports, "getParameters",
base::BindRepeating(&CrashReporter::GetParameters, reporter));
SetMethod(exports, "getUploadedReports",
base::BindRepeating(&CrashReporter::GetUploadedReports, reporter));
SetMethod(exports, "setUploadToServer",
base::BindRepeating(&CrashReporter::SetUploadToServer, reporter));
SetMethod(exports, "getUploadToServer",
base::BindRepeating(&CrashReporter::GetUploadToServer, reporter));
}
} // namespace

View file

@ -7,6 +7,7 @@
#include "atom/browser/browser.h"
#include "atom/common/atom_version.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/map_converter.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/strings/string_number_conversions.h"

29
atom/common/gin_util.h Normal file
View file

@ -0,0 +1,29 @@
// Copyright (c) 2018 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_GIN_UTIL_H_
#define ATOM_COMMON_GIN_UTIL_H_
#include "gin/converter.h"
#include "gin/function_template.h"
namespace gin_util {
template <typename T>
bool SetMethod(v8::Local<v8::Object> recv,
const base::StringPiece& key,
const T& callback) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
auto context = isolate->GetCurrentContext();
return recv
->Set(context, gin::StringToV8(isolate, key),
gin::CreateFunctionTemplate(isolate, callback)
->GetFunction(context)
.ToLocalChecked())
.ToChecked();
}
} // namespace gin_util
#endif // ATOM_COMMON_GIN_UTIL_H_

View file

@ -10,7 +10,7 @@
#include "atom/common/native_mate_converters/string16_converter.h"
#include "base/files/file_path.h"
namespace mate {
namespace gin {
template <>
struct Converter<base::FilePath> {
@ -34,6 +34,24 @@ struct Converter<base::FilePath> {
}
};
} // namespace gin
namespace mate {
template <>
struct Converter<base::FilePath> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::FilePath& val) {
return gin::Converter<base::FilePath::StringType>::ToV8(isolate,
val.value());
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::FilePath* out) {
return gin::Converter<base::FilePath>::FromV8(isolate, val, out);
}
};
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_

View file

@ -0,0 +1,70 @@
// Copyright (c) 2019 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_
#include <map>
#include <string>
#include <utility>
#include "gin/converter.h"
namespace gin {
template <typename T>
struct Converter<std::map<std::string, T>> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::map<std::string, T>* out) {
if (!val->IsObject())
return false;
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Object> dict = val->ToObject(context).ToLocalChecked();
v8::Local<v8::Array> keys =
dict->GetOwnPropertyNames(context).ToLocalChecked();
for (uint32_t i = 0; i < keys->Length(); ++i) {
v8::Local<v8::Value> key = keys->Get(context, i).ToLocalChecked();
T value;
if (Converter<T>::FromV8(
isolate, dict->Get(context, key).ToLocalChecked(), &value))
(*out)[gin::V8ToString(isolate, key)] = std::move(value);
}
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::map<std::string, T>& val) {
v8::Local<v8::Object> result = v8::Object::New(isolate);
auto context = isolate->GetCurrentContext();
for (auto i = val.begin(); i != val.end(); i++) {
result
->Set(context, Converter<T>::ToV8(isolate, i->first),
Converter<T>::ToV8(isolate, i->second))
.Check();
}
return result;
}
};
} // namespace gin
namespace mate {
template <typename T>
struct Converter<std::map<std::string, T>> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::map<std::string, T>* out) {
return gin::Converter<std::map<std::string, T>>::FromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::map<std::string, T>& val) {
return gin::Converter<std::map<std::string, T>>::ToV8(isolate, val);
}
};
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_

View file

@ -6,9 +6,10 @@
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
#include "base/strings/string16.h"
#include "gin/converter.h"
#include "native_mate/converter.h"
namespace mate {
namespace gin {
template <>
struct Converter<base::string16> {
@ -36,6 +37,28 @@ inline v8::Local<v8::String> StringToV8(v8::Isolate* isolate,
return ConvertToV8(isolate, input).As<v8::String>();
}
} // namespace gin
namespace mate {
template <>
struct Converter<base::string16> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::string16& val) {
return gin::Converter<base::string16>::ToV8(isolate, val);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::string16* out) {
return gin::Converter<base::string16>::FromV8(isolate, val, out);
}
};
inline v8::Local<v8::String> StringToV8(v8::Isolate* isolate,
const base::string16& input) {
return ConvertToV8(isolate, input).As<v8::String>();
}
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_

View file

@ -605,6 +605,7 @@ filenames = {
"atom/common/crash_reporter/win/crash_service_main.h",
"atom/common/draggable_region.cc",
"atom/common/draggable_region.h",
"atom/common/gin_util.h",
"atom/common/heap_snapshot.cc",
"atom/common/heap_snapshot.h",
"atom/common/key_weak_map.h",
@ -630,6 +631,7 @@ filenames = {
"atom/common/native_mate_converters/gurl_converter.h",
"atom/common/native_mate_converters/image_converter.cc",
"atom/common/native_mate_converters/image_converter.h",
"atom/common/native_mate_converters/map_converter.h",
"atom/common/native_mate_converters/net_converter.cc",
"atom/common/native_mate_converters/net_converter.h",
"atom/common/native_mate_converters/network_converter.cc",

View file

@ -277,41 +277,6 @@ struct Converter<std::set<T>> {
}
};
template <typename T>
struct Converter<std::map<std::string, T>> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::map<std::string, T>* out) {
if (!val->IsObject())
return false;
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Object> dict = val->ToObject(context).ToLocalChecked();
v8::Local<v8::Array> keys =
dict->GetOwnPropertyNames(context).ToLocalChecked();
for (uint32_t i = 0; i < keys->Length(); ++i) {
v8::Local<v8::Value> key = keys->Get(context, i).ToLocalChecked();
T value;
if (Converter<T>::FromV8(
isolate, dict->Get(context, key).ToLocalChecked(), &value))
(*out)[gin::V8ToString(isolate, key)] = std::move(value);
}
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::map<std::string, T>& val) {
v8::Local<v8::Object> result = v8::Object::New(isolate);
auto context = isolate->GetCurrentContext();
for (auto i = val.begin(); i != val.end(); i++) {
result
->Set(context, Converter<T>::ToV8(isolate, i->first),
Converter<T>::ToV8(isolate, i->second))
.Check();
}
return result;
}
};
// Convenience functions that deduce T.
template <typename T>
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, const T& input) {