refactor: convert crash reporter to gin (#17952)
This commit is contained in:
parent
493af7f84c
commit
3949f0bd50
10 changed files with 174 additions and 72 deletions
|
@ -40,6 +40,7 @@
|
||||||
#include "atom/common/native_mate_converters/gfx_converter.h"
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
||||||
#include "atom/common/native_mate_converters/gurl_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/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/net_converter.h"
|
||||||
#include "atom/common/native_mate_converters/network_converter.h"
|
#include "atom/common/native_mate_converters/network_converter.h"
|
||||||
#include "atom/common/native_mate_converters/once_callback.h"
|
#include "atom/common/native_mate_converters/once_callback.h"
|
||||||
|
|
|
@ -10,11 +10,11 @@
|
||||||
#import <Squirrel/Squirrel.h>
|
#import <Squirrel/Squirrel.h>
|
||||||
|
|
||||||
#include "atom/browser/browser.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 "atom/common/native_mate_converters/value_converter.h"
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "base/strings/sys_string_conversions.h"
|
#include "base/strings/sys_string_conversions.h"
|
||||||
#include "base/time/time.h"
|
#include "base/time/time.h"
|
||||||
#include "native_mate/converter.h"
|
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
namespace auto_updater {
|
namespace auto_updater {
|
||||||
|
|
|
@ -6,64 +6,57 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/common/crash_reporter/crash_reporter.h"
|
#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/file_path_converter.h"
|
||||||
|
#include "atom/common/native_mate_converters/map_converter.h"
|
||||||
#include "base/bind.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"
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
using crash_reporter::CrashReporter;
|
using crash_reporter::CrashReporter;
|
||||||
|
|
||||||
namespace mate {
|
namespace gin {
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<CrashReporter::UploadReportResult> {
|
struct Converter<CrashReporter::UploadReportResult> {
|
||||||
static v8::Local<v8::Value> ToV8(
|
static v8::Local<v8::Value> ToV8(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
const CrashReporter::UploadReportResult& reports) {
|
const CrashReporter::UploadReportResult& reports) {
|
||||||
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
return gin::DataObjectBuilder(isolate)
|
||||||
dict.Set("date",
|
.Set("date",
|
||||||
v8::Date::New(isolate->GetCurrentContext(), reports.first * 1000.0)
|
v8::Date::New(isolate->GetCurrentContext(), reports.first * 1000.0)
|
||||||
.ToLocalChecked());
|
.ToLocalChecked())
|
||||||
dict.Set("id", reports.second);
|
.Set("id", reports.second)
|
||||||
return dict.GetHandle();
|
.Build();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace gin
|
||||||
|
|
||||||
namespace {
|
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,
|
void Initialize(v8::Local<v8::Object> exports,
|
||||||
v8::Local<v8::Value> unused,
|
v8::Local<v8::Value> unused,
|
||||||
v8::Local<v8::Context> context,
|
v8::Local<v8::Context> context,
|
||||||
void* priv) {
|
void* priv) {
|
||||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
using gin_util::SetMethod;
|
||||||
auto reporter = base::Unretained(CrashReporter::GetInstance());
|
auto reporter = base::Unretained(CrashReporter::GetInstance());
|
||||||
dict.SetMethod("start", base::BindRepeating(&CrashReporter::Start, reporter));
|
SetMethod(exports, "start",
|
||||||
dict.SetMethod("addExtraParameter", &AddExtraParameter);
|
base::BindRepeating(&CrashReporter::Start, reporter));
|
||||||
dict.SetMethod("removeExtraParameter", &RemoveExtraParameter);
|
SetMethod(exports, "addExtraParameter",
|
||||||
dict.SetMethod("getParameters", &GetParameters);
|
base::BindRepeating(&CrashReporter::AddExtraParameter, reporter));
|
||||||
dict.SetMethod(
|
SetMethod(
|
||||||
"getUploadedReports",
|
exports, "removeExtraParameter",
|
||||||
|
base::BindRepeating(&CrashReporter::RemoveExtraParameter, reporter));
|
||||||
|
SetMethod(exports, "getParameters",
|
||||||
|
base::BindRepeating(&CrashReporter::GetParameters, reporter));
|
||||||
|
SetMethod(exports, "getUploadedReports",
|
||||||
base::BindRepeating(&CrashReporter::GetUploadedReports, reporter));
|
base::BindRepeating(&CrashReporter::GetUploadedReports, reporter));
|
||||||
dict.SetMethod(
|
SetMethod(exports, "setUploadToServer",
|
||||||
"setUploadToServer",
|
|
||||||
base::BindRepeating(&CrashReporter::SetUploadToServer, reporter));
|
base::BindRepeating(&CrashReporter::SetUploadToServer, reporter));
|
||||||
dict.SetMethod(
|
SetMethod(exports, "getUploadToServer",
|
||||||
"getUploadToServer",
|
|
||||||
base::BindRepeating(&CrashReporter::GetUploadToServer, reporter));
|
base::BindRepeating(&CrashReporter::GetUploadToServer, reporter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "atom/browser/browser.h"
|
#include "atom/browser/browser.h"
|
||||||
#include "atom/common/atom_version.h"
|
#include "atom/common/atom_version.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_converter.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/command_line.h"
|
||||||
#include "base/files/file_util.h"
|
#include "base/files/file_util.h"
|
||||||
#include "base/strings/string_number_conversions.h"
|
#include "base/strings/string_number_conversions.h"
|
||||||
|
|
29
atom/common/gin_util.h
Normal file
29
atom/common/gin_util.h
Normal 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_
|
|
@ -10,7 +10,7 @@
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace gin {
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<base::FilePath> {
|
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
|
} // namespace mate
|
||||||
|
|
||||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_
|
||||||
|
|
70
atom/common/native_mate_converters/map_converter.h
Normal file
70
atom/common/native_mate_converters/map_converter.h
Normal 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_
|
|
@ -6,9 +6,10 @@
|
||||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
|
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
|
||||||
|
|
||||||
#include "base/strings/string16.h"
|
#include "base/strings/string16.h"
|
||||||
|
#include "gin/converter.h"
|
||||||
#include "native_mate/converter.h"
|
#include "native_mate/converter.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace gin {
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<base::string16> {
|
struct Converter<base::string16> {
|
||||||
|
@ -36,6 +37,28 @@ inline v8::Local<v8::String> StringToV8(v8::Isolate* isolate,
|
||||||
return ConvertToV8(isolate, input).As<v8::String>();
|
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
|
} // namespace mate
|
||||||
|
|
||||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
|
||||||
|
|
|
@ -605,6 +605,7 @@ filenames = {
|
||||||
"atom/common/crash_reporter/win/crash_service_main.h",
|
"atom/common/crash_reporter/win/crash_service_main.h",
|
||||||
"atom/common/draggable_region.cc",
|
"atom/common/draggable_region.cc",
|
||||||
"atom/common/draggable_region.h",
|
"atom/common/draggable_region.h",
|
||||||
|
"atom/common/gin_util.h",
|
||||||
"atom/common/heap_snapshot.cc",
|
"atom/common/heap_snapshot.cc",
|
||||||
"atom/common/heap_snapshot.h",
|
"atom/common/heap_snapshot.h",
|
||||||
"atom/common/key_weak_map.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/gurl_converter.h",
|
||||||
"atom/common/native_mate_converters/image_converter.cc",
|
"atom/common/native_mate_converters/image_converter.cc",
|
||||||
"atom/common/native_mate_converters/image_converter.h",
|
"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.cc",
|
||||||
"atom/common/native_mate_converters/net_converter.h",
|
"atom/common/native_mate_converters/net_converter.h",
|
||||||
"atom/common/native_mate_converters/network_converter.cc",
|
"atom/common/native_mate_converters/network_converter.cc",
|
||||||
|
|
|
@ -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.
|
// Convenience functions that deduce T.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, const T& input) {
|
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, const T& input) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue