refactor: use Error util when only throwing errors (#19837)

This commit is contained in:
Shelley Vohr 2019-08-23 15:49:54 -07:00 committed by Samuel Attard
parent ef6d4a46c2
commit c89debd19a
3 changed files with 31 additions and 28 deletions

View file

@ -846,11 +846,11 @@ void App::SetAppPath(const base::FilePath& app_path) {
} }
#if !defined(OS_MACOSX) #if !defined(OS_MACOSX)
void App::SetAppLogsPath(base::Optional<base::FilePath> custom_path, void App::SetAppLogsPath(util::ErrorThrower thrower,
mate::Arguments* args) { base::Optional<base::FilePath> custom_path) {
if (custom_path.has_value()) { if (custom_path.has_value()) {
if (!custom_path->IsAbsolute()) { if (!custom_path->IsAbsolute()) {
args->ThrowError("Path must be absolute"); thrower.ThrowError("Path must be absolute");
return; return;
} }
base::PathService::Override(DIR_APP_LOGS, custom_path.value()); base::PathService::Override(DIR_APP_LOGS, custom_path.value());
@ -865,7 +865,8 @@ void App::SetAppLogsPath(base::Optional<base::FilePath> custom_path,
} }
#endif #endif
base::FilePath App::GetPath(mate::Arguments* args, const std::string& name) { base::FilePath App::GetPath(util::ErrorThrower thrower,
const std::string& name) {
bool succeed = false; bool succeed = false;
base::FilePath path; base::FilePath path;
@ -876,22 +877,22 @@ base::FilePath App::GetPath(mate::Arguments* args, const std::string& name) {
// set the path to a sensible default and then try to get it again // set the path to a sensible default and then try to get it again
if (!succeed && name == "logs") { if (!succeed && name == "logs") {
base::ThreadRestrictions::ScopedAllowIO allow_io; base::ThreadRestrictions::ScopedAllowIO allow_io;
SetAppLogsPath(base::Optional<base::FilePath>(), args); SetAppLogsPath(thrower, base::Optional<base::FilePath>());
succeed = base::PathService::Get(key, &path); succeed = base::PathService::Get(key, &path);
} }
} }
if (!succeed) if (!succeed)
args->ThrowError("Failed to get '" + name + "' path"); thrower.ThrowError("Failed to get '" + name + "' path");
return path; return path;
} }
void App::SetPath(mate::Arguments* args, void App::SetPath(util::ErrorThrower thrower,
const std::string& name, const std::string& name,
const base::FilePath& path) { const base::FilePath& path) {
if (!path.IsAbsolute()) { if (!path.IsAbsolute()) {
args->ThrowError("Path must be absolute"); thrower.ThrowError("Path must be absolute");
return; return;
} }
@ -901,7 +902,7 @@ void App::SetPath(mate::Arguments* args,
succeed = succeed =
base::PathService::OverrideAndCreateIfNeeded(key, path, true, false); base::PathService::OverrideAndCreateIfNeeded(key, path, true, false);
if (!succeed) if (!succeed)
args->ThrowError("Failed to set path"); thrower.ThrowError("Failed to set path");
} }
void App::SetDesktopName(const std::string& desktop_name) { void App::SetDesktopName(const std::string& desktop_name) {
@ -1030,9 +1031,9 @@ bool App::Relaunch(mate::Arguments* js_args) {
return relauncher::RelaunchApp(argv); return relauncher::RelaunchApp(argv);
} }
void App::DisableHardwareAcceleration(mate::Arguments* args) { void App::DisableHardwareAcceleration(util::ErrorThrower thrower) {
if (Browser::Get()->is_ready()) { if (Browser::Get()->is_ready()) {
args->ThrowError( thrower.ThrowError(
"app.disableHardwareAcceleration() can only be called " "app.disableHardwareAcceleration() can only be called "
"before app is ready"); "before app is ready");
return; return;
@ -1040,9 +1041,9 @@ void App::DisableHardwareAcceleration(mate::Arguments* args) {
content::GpuDataManager::GetInstance()->DisableHardwareAcceleration(); content::GpuDataManager::GetInstance()->DisableHardwareAcceleration();
} }
void App::DisableDomainBlockingFor3DAPIs(mate::Arguments* args) { void App::DisableDomainBlockingFor3DAPIs(util::ErrorThrower thrower) {
if (Browser::Get()->is_ready()) { if (Browser::Get()->is_ready()) {
args->ThrowError( thrower.ThrowError(
"app.disableDomainBlockingFor3DAPIs() can only be called " "app.disableDomainBlockingFor3DAPIs() can only be called "
"before app is ready"); "before app is ready");
return; return;
@ -1056,9 +1057,10 @@ bool App::IsAccessibilitySupportEnabled() {
return ax_state->IsAccessibleBrowser(); return ax_state->IsAccessibleBrowser();
} }
void App::SetAccessibilitySupportEnabled(bool enabled, mate::Arguments* args) { void App::SetAccessibilitySupportEnabled(util::ErrorThrower thrower,
bool enabled) {
if (!Browser::Get()->is_ready()) { if (!Browser::Get()->is_ready()) {
args->ThrowError( thrower.ThrowError(
"app.setAccessibilitySupportEnabled() can only be called " "app.setAccessibilitySupportEnabled() can only be called "
"after app is ready"); "after app is ready");
return; return;
@ -1312,9 +1314,9 @@ static void RemoveNoSandboxSwitch(base::CommandLine* command_line) {
} }
} }
void App::EnableSandbox(mate::Arguments* args) { void App::EnableSandbox(util::ErrorThrower thrower) {
if (Browser::Get()->is_ready()) { if (Browser::Get()->is_ready()) {
args->ThrowError( thrower.ThrowError(
"app.enableSandbox() can only be called " "app.enableSandbox() can only be called "
"before app is ready"); "before app is ready");
return; return;

View file

@ -27,6 +27,7 @@
#include "shell/browser/atom_browser_client.h" #include "shell/browser/atom_browser_client.h"
#include "shell/browser/browser.h" #include "shell/browser/browser.h"
#include "shell/browser/browser_observer.h" #include "shell/browser/browser_observer.h"
#include "shell/common/error_util.h"
#include "shell/common/native_mate_converters/callback.h" #include "shell/common/native_mate_converters/callback.h"
#include "shell/common/promise_util.h" #include "shell/common/promise_util.h"
@ -164,12 +165,12 @@ class App : public AtomBrowserClient::Delegate,
void ChildProcessLaunched(int process_type, base::ProcessHandle handle); void ChildProcessLaunched(int process_type, base::ProcessHandle handle);
void ChildProcessDisconnected(base::ProcessId pid); void ChildProcessDisconnected(base::ProcessId pid);
void SetAppLogsPath(base::Optional<base::FilePath> custom_path, void SetAppLogsPath(util::ErrorThrower thrower,
mate::Arguments* args); base::Optional<base::FilePath> custom_path);
// Get/Set the pre-defined path in PathService. // Get/Set the pre-defined path in PathService.
base::FilePath GetPath(mate::Arguments* args, const std::string& name); base::FilePath GetPath(util::ErrorThrower thrower, const std::string& name);
void SetPath(mate::Arguments* args, void SetPath(util::ErrorThrower thrower,
const std::string& name, const std::string& name,
const base::FilePath& path); const base::FilePath& path);
@ -182,10 +183,10 @@ class App : public AtomBrowserClient::Delegate,
bool RequestSingleInstanceLock(); bool RequestSingleInstanceLock();
void ReleaseSingleInstanceLock(); void ReleaseSingleInstanceLock();
bool Relaunch(mate::Arguments* args); bool Relaunch(mate::Arguments* args);
void DisableHardwareAcceleration(mate::Arguments* args); void DisableHardwareAcceleration(util::ErrorThrower thrower);
void DisableDomainBlockingFor3DAPIs(mate::Arguments* args); void DisableDomainBlockingFor3DAPIs(util::ErrorThrower thrower);
bool IsAccessibilitySupportEnabled(); bool IsAccessibilitySupportEnabled();
void SetAccessibilitySupportEnabled(bool enabled, mate::Arguments* args); void SetAccessibilitySupportEnabled(util::ErrorThrower thrower, bool enabled);
Browser::LoginItemSettings GetLoginItemSettings(mate::Arguments* args); Browser::LoginItemSettings GetLoginItemSettings(mate::Arguments* args);
#if defined(USE_NSS_CERTS) #if defined(USE_NSS_CERTS)
void ImportCertificate(const base::DictionaryValue& options, void ImportCertificate(const base::DictionaryValue& options,
@ -198,7 +199,7 @@ class App : public AtomBrowserClient::Delegate,
v8::Local<v8::Value> GetGPUFeatureStatus(v8::Isolate* isolate); v8::Local<v8::Value> GetGPUFeatureStatus(v8::Isolate* isolate);
v8::Local<v8::Promise> GetGPUInfo(v8::Isolate* isolate, v8::Local<v8::Promise> GetGPUInfo(v8::Isolate* isolate,
const std::string& info_type); const std::string& info_type);
void EnableSandbox(mate::Arguments* args); void EnableSandbox(util::ErrorThrower thrower);
void SetUserAgentFallback(const std::string& user_agent); void SetUserAgentFallback(const std::string& user_agent);
std::string GetUserAgentFallback(); std::string GetUserAgentFallback();
void SetBrowserClientCanUseCustomSiteInstance(bool should_disable); void SetBrowserClientCanUseCustomSiteInstance(bool should_disable);

View file

@ -13,11 +13,11 @@ namespace electron {
namespace api { namespace api {
void App::SetAppLogsPath(base::Optional<base::FilePath> custom_path, void App::SetAppLogsPath(util::ErrorThrower thrower,
mate::Arguments* args) { base::Optional<base::FilePath> custom_path) {
if (custom_path.has_value()) { if (custom_path.has_value()) {
if (!custom_path->IsAbsolute()) { if (!custom_path->IsAbsolute()) {
args->ThrowError("Path must be absolute"); thrower.ThrowError("Path must be absolute");
return; return;
} }
base::PathService::Override(DIR_APP_LOGS, custom_path.value()); base::PathService::Override(DIR_APP_LOGS, custom_path.value());