Implement app.relaunch on OS X

This commit is contained in:
Cheng Zhao 2016-06-01 18:22:14 +09:00
parent 7b3ba739bf
commit abdcb9d481
6 changed files with 54 additions and 17 deletions

View file

@ -21,10 +21,18 @@
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(OS_MACOSX)
#include "chrome/browser/mac/relauncher.h"
#endif
namespace atom {
namespace {
#if defined(OS_MACOSX)
const char* kRelauncherProcess = "relauncher";
#endif
bool IsBrowserProcess(base::CommandLine* cmd) {
std::string process_type = cmd->GetSwitchValueASCII(switches::kProcessType);
return process_type.empty();
@ -146,6 +154,26 @@ content::ContentUtilityClient* AtomMainDelegate::CreateContentUtilityClient() {
return utility_client_.get();
}
#if defined(OS_MACOSX)
int AtomMainDelegate::RunProcess(
const std::string& process_type,
const content::MainFunctionParams& main_function_params) {
if (process_type == kRelauncherProcess)
return mac_relauncher::internal::RelauncherMain(main_function_params);
else
return -1;
}
bool AtomMainDelegate::ShouldSendMachPort(const std::string& process_type) {
return process_type != kRelauncherProcess;
}
bool AtomMainDelegate::DelaySandboxInitialization(
const std::string& process_type) {
return process_type == kRelauncherProcess;
}
#endif
std::unique_ptr<brightray::ContentClient>
AtomMainDelegate::CreateContentClient() {
return std::unique_ptr<brightray::ContentClient>(new AtomContentClient);

View file

@ -5,6 +5,8 @@
#ifndef ATOM_APP_ATOM_MAIN_DELEGATE_H_
#define ATOM_APP_ATOM_MAIN_DELEGATE_H_
#include <string>
#include "brightray/common/main_delegate.h"
#include "brightray/common/content_client.h"
@ -22,6 +24,13 @@ class AtomMainDelegate : public brightray::MainDelegate {
content::ContentBrowserClient* CreateContentBrowserClient() override;
content::ContentRendererClient* CreateContentRendererClient() override;
content::ContentUtilityClient* CreateContentUtilityClient() override;
#if defined(OS_MACOSX)
int RunProcess(
const std::string& process_type,
const content::MainFunctionParams& main_function_params) override;
bool ShouldSendMachPort(const std::string& process_type) override;
bool DelaySandboxInitialization(const std::string& process_type) override;
#endif
// brightray::MainDelegate:
std::unique_ptr<brightray::ContentClient> CreateContentClient() override;

View file

@ -450,6 +450,7 @@ void App::BuildPrototype(
mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("quit", base::Bind(&Browser::Quit, browser))
.SetMethod("exit", base::Bind(&Browser::Exit, browser))
.SetMethod("relaunch", base::Bind(&Browser::Relaunch, browser))
.SetMethod("focus", base::Bind(&Browser::Focus, browser))
.SetMethod("getVersion", base::Bind(&Browser::GetVersion, browser))
.SetMethod("setVersion", base::Bind(&Browser::SetVersion, browser))

View file

@ -53,6 +53,10 @@ class Browser : public WindowListObserver {
// Cleanup everything and shutdown the application gracefully.
void Shutdown();
// Restart the app.
void Relaunch(const std::vector<std::string>& args,
const std::string& app);
// Focus the application.
void Focus();

View file

@ -13,11 +13,22 @@
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "brightray/common/application_info.h"
#include "brightray/common/mac/main_application_bundle.h"
#include "chrome/browser/mac/relauncher.h"
#include "net/base/mac/url_conversions.h"
#include "url/gurl.h"
namespace atom {
void Browser::Relaunch(const std::vector<std::string>& args,
const std::string& app) {
std::vector<std::string> args_with_app(args);
args_with_app.insert(
args_with_app.begin(),
app.empty() ? brightray::MainApplicationBundlePath().value() : app);
mac_relauncher::RelaunchApp(args_with_app);
}
void Browser::Focus() {
[[AtomApplication sharedApplication] activateIgnoringOtherApps:YES];
}

View file

@ -29,8 +29,6 @@
#include "base/process/launch.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/mac/install_from_dmg.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/main_function_params.h"
@ -66,9 +64,7 @@ const char kPSNArg[] = "-psn_";
// Returns the "type" argument identifying a relauncher process
// ("--type=relauncher").
std::string RelauncherTypeArg() {
return base::StringPrintf("--%s=%s",
switches::kProcessType,
switches::kRelauncherProcess);
return base::StringPrintf("--%s=%s", switches::kProcessType, "relauncher");
}
} // namespace
@ -292,12 +288,9 @@ int RelauncherMain(const content::MainFunctionParams& main_parameters) {
// start it in the background.
bool background = false;
bool in_relaunch_args = false;
std::string dmg_bsd_device_name;
bool seen_relaunch_executable = false;
std::string relaunch_executable;
const std::string relauncher_arg_separator(kRelauncherArgSeparator);
const std::string relauncher_dmg_device_arg =
base::StringPrintf("--%s=", switches::kRelauncherProcessDMGDevice);
for (int argv_index = 2; argv_index < argc; ++argv_index) {
const std::string arg(argv[argv_index]);
@ -311,11 +304,6 @@ int RelauncherMain(const content::MainFunctionParams& main_parameters) {
in_relaunch_args = true;
} else if (arg == kRelauncherBackgroundArg) {
background = true;
} else if (arg.compare(0,
relauncher_dmg_device_arg.size(),
relauncher_dmg_device_arg) == 0) {
dmg_bsd_device_name.assign(
arg.substr(relauncher_dmg_device_arg.size()));
}
} else {
if (!seen_relaunch_executable) {
@ -369,10 +357,6 @@ int RelauncherMain(const content::MainFunctionParams& main_parameters) {
// relaunching). From this point on, only clean-up tasks should occur, and
// failures are tolerable.
if (!dmg_bsd_device_name.empty()) {
EjectAndTrashDiskImage(dmg_bsd_device_name);
}
return 0;
}