Provide a flexible API for app.relaunch

This commit is contained in:
Cheng Zhao 2016-06-02 21:49:29 +09:00
parent 3de41fb22d
commit 91a9e67dca
4 changed files with 43 additions and 38 deletions

View file

@ -14,6 +14,8 @@
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/login_handler.h" #include "atom/browser/login_handler.h"
#include "atom/browser/relauncher.h"
#include "atom/common/atom_command_line.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.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/gurl_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h"
@ -408,6 +410,43 @@ void App::ReleaseSingleInstance() {
} }
} }
bool App::Relaunch(mate::Arguments* js_args) {
// Parse parameters.
bool override_argv = false;
base::FilePath exec_path;
relauncher::StringVector args;
mate::Dictionary options;
if (js_args->GetNext(&options)) {
if (options.Get("execPath", &exec_path) || options.Get("args", &args))
override_argv = true;
}
if (!override_argv) {
#if defined(OS_WIN)
const relauncher::StringVector& argv = atom::AtomCommandLine::wargv();
#else
const relauncher::StringVector& argv = atom::AtomCommandLine::argv();
#endif
return relauncher::RelaunchApp(argv);
}
relauncher::StringVector argv;
argv.reserve(1 + args.size());
if (exec_path.empty()) {
base::FilePath current_exe_path;
PathService::Get(base::FILE_EXE, &current_exe_path);
argv.push_back(current_exe_path.value());
} else {
argv.push_back(exec_path.value());
}
argv.insert(argv.end(), args.begin(), args.end());
return relauncher::RelaunchApp(argv);
}
#if defined(USE_NSS_CERTS) #if defined(USE_NSS_CERTS)
void App::ImportCertificate( void App::ImportCertificate(
const base::DictionaryValue& options, const base::DictionaryValue& options,
@ -450,7 +489,6 @@ void App::BuildPrototype(
mate::ObjectTemplateBuilder(isolate, prototype) mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("quit", base::Bind(&Browser::Quit, browser)) .SetMethod("quit", base::Bind(&Browser::Quit, browser))
.SetMethod("exit", base::Bind(&Browser::Exit, browser)) .SetMethod("exit", base::Bind(&Browser::Exit, browser))
.SetMethod("relaunch", base::Bind(&Browser::Relaunch, browser))
.SetMethod("focus", base::Bind(&Browser::Focus, browser)) .SetMethod("focus", base::Bind(&Browser::Focus, browser))
.SetMethod("getVersion", base::Bind(&Browser::GetVersion, browser)) .SetMethod("getVersion", base::Bind(&Browser::GetVersion, browser))
.SetMethod("setVersion", base::Bind(&Browser::SetVersion, browser)) .SetMethod("setVersion", base::Bind(&Browser::SetVersion, browser))
@ -489,7 +527,8 @@ void App::BuildPrototype(
.SetMethod("importCertificate", &App::ImportCertificate) .SetMethod("importCertificate", &App::ImportCertificate)
#endif #endif
.SetMethod("makeSingleInstance", &App::MakeSingleInstance) .SetMethod("makeSingleInstance", &App::MakeSingleInstance)
.SetMethod("releaseSingleInstance", &App::ReleaseSingleInstance); .SetMethod("releaseSingleInstance", &App::ReleaseSingleInstance)
.SetMethod("relaunch", &App::Relaunch);
} }
} // namespace api } // namespace api

View file

@ -106,10 +106,11 @@ class App : public AtomBrowserClient::Delegate,
const base::FilePath& path); const base::FilePath& path);
void SetDesktopName(const std::string& desktop_name); void SetDesktopName(const std::string& desktop_name);
std::string GetLocale();
bool MakeSingleInstance( bool MakeSingleInstance(
const ProcessSingleton::NotificationCallback& callback); const ProcessSingleton::NotificationCallback& callback);
void ReleaseSingleInstance(); void ReleaseSingleInstance();
std::string GetLocale(); bool Relaunch(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,

View file

@ -9,7 +9,6 @@
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/atom_command_line.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
@ -34,34 +33,6 @@ Browser* Browser::Get() {
return AtomBrowserMainParts::Get()->browser(); return AtomBrowserMainParts::Get()->browser();
} }
bool Browser::Relaunch(bool override_argv,
const base::FilePath& app,
const relauncher::StringVector& args) {
if (!override_argv) {
#if defined(OS_WIN)
const relauncher::StringVector& argv = atom::AtomCommandLine::wargv();
#else
const relauncher::StringVector& argv = atom::AtomCommandLine::argv();
#endif
return relauncher::RelaunchApp(argv);
}
relauncher::StringVector argv;
argv.reserve(1 + args.size());
if (app.empty()) {
base::FilePath exe_path;
PathService::Get(base::FILE_EXE, &exe_path);
argv.push_back(exe_path.value());
} else {
argv.push_back(app.value());
}
argv.insert(argv.end(), args.begin(), args.end());
return relauncher::RelaunchApp(argv);
}
void Browser::Quit() { void Browser::Quit() {
if (is_quiting_) if (is_quiting_)
return; return;

View file

@ -13,7 +13,6 @@
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "atom/browser/browser_observer.h" #include "atom/browser/browser_observer.h"
#include "atom/browser/relauncher.h"
#include "atom/browser/window_list_observer.h" #include "atom/browser/window_list_observer.h"
#include "native_mate/arguments.h" #include "native_mate/arguments.h"
@ -54,11 +53,6 @@ class Browser : public WindowListObserver {
// Cleanup everything and shutdown the application gracefully. // Cleanup everything and shutdown the application gracefully.
void Shutdown(); void Shutdown();
// Restart the app.
bool Relaunch(bool override_argv,
const base::FilePath& app,
const relauncher::StringVector& args);
// Focus the application. // Focus the application.
void Focus(); void Focus();