fix: fall back to default logs path in getPath('logs') (#19653)

This commit is contained in:
Shelley Vohr 2019-08-19 15:16:00 -07:00 committed by GitHub
parent 0851697cb7
commit 1dc02e6dbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 19 deletions

View file

@ -11,6 +11,7 @@
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/optional.h"
#include "base/path_service.h"
#include "base/system/sys_info.h"
#include "chrome/browser/browser_process.h"
@ -845,14 +846,14 @@ void App::SetAppPath(const base::FilePath& app_path) {
}
#if !defined(OS_MACOSX)
void App::SetAppLogsPath(mate::Arguments* args) {
base::FilePath custom_path;
if (args->GetNext(&custom_path)) {
if (!custom_path.IsAbsolute()) {
void App::SetAppLogsPath(base::Optional<base::FilePath> custom_path,
mate::Arguments* args) {
if (custom_path.has_value()) {
if (!custom_path->IsAbsolute()) {
args->ThrowError("Path must be absolute");
return;
}
base::PathService::Override(DIR_APP_LOGS, custom_path);
base::PathService::Override(DIR_APP_LOGS, custom_path.value());
} else {
base::FilePath path;
if (base::PathService::Get(DIR_USER_DATA, &path)) {
@ -867,18 +868,22 @@ void App::SetAppLogsPath(mate::Arguments* args) {
base::FilePath App::GetPath(mate::Arguments* args, const std::string& name) {
bool succeed = false;
base::FilePath path;
int key = GetPathConstant(name);
if (key >= 0)
if (key >= 0) {
succeed = base::PathService::Get(key, &path);
if (!succeed) {
if (name == "logs") {
args->ThrowError("Failed to get '" + name +
"' path: setAppLogsPath() must be called first.");
} else {
args->ThrowError("Failed to get '" + name + "' path");
// If users try to get the logs path before setting a logs path,
// set the path to a sensible default and then try to get it again
if (!succeed && name == "logs") {
base::ThreadRestrictions::ScopedAllowIO allow_io;
SetAppLogsPath(base::Optional<base::FilePath>(), args);
succeed = base::PathService::Get(key, &path);
}
}
if (!succeed)
args->ThrowError("Failed to get '" + name + "' path");
return path;
}

View file

@ -164,7 +164,8 @@ class App : public AtomBrowserClient::Delegate,
void ChildProcessLaunched(int process_type, base::ProcessHandle handle);
void ChildProcessDisconnected(base::ProcessId pid);
void SetAppLogsPath(mate::Arguments* args);
void SetAppLogsPath(base::Optional<base::FilePath> custom_path,
mate::Arguments* args);
// Get/Set the pre-defined path in PathService.
base::FilePath GetPath(mate::Arguments* args, const std::string& name);

View file

@ -13,14 +13,14 @@ namespace electron {
namespace api {
void App::SetAppLogsPath(mate::Arguments* args) {
base::FilePath custom_path;
if (args->GetNext(&custom_path)) {
if (!custom_path.IsAbsolute()) {
void App::SetAppLogsPath(base::Optional<base::FilePath> custom_path,
mate::Arguments* args) {
if (custom_path.has_value()) {
if (!custom_path->IsAbsolute()) {
args->ThrowError("Path must be absolute");
return;
}
base::PathService::Override(DIR_APP_LOGS, custom_path);
base::PathService::Override(DIR_APP_LOGS, custom_path.value());
} else {
NSString* bundle_name =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];

View file

@ -7,6 +7,8 @@
#include "native_mate/converter.h"
#include "base/optional.h"
namespace base {
class DictionaryValue;
class ListValue;
@ -33,6 +35,29 @@ struct Converter<base::Value> {
const base::Value& val);
};
template <typename T>
struct Converter<base::Optional<T>> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Optional<T>* out) {
if (val->IsNull() || val->IsUndefined()) {
return true;
}
T converted;
if (Converter<T>::FromV8(isolate, val, &converted)) {
return true;
}
out->emplace(converted);
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Optional<T>& val) {
if (val.has_value())
return Converter<T>::ToV8(val.value());
return v8::Undefined(isolate);
}
};
template <>
struct Converter<base::ListValue> {
static bool FromV8(v8::Isolate* isolate,