2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-02 15:43:23 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
2013-05-02 15:43:23 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
2018-10-03 21:47:00 +00:00
|
|
|
#include <utility>
|
2014-03-16 01:13:06 +00:00
|
|
|
|
2016-04-29 11:20:52 +00:00
|
|
|
#include "base/files/file_util.h"
|
|
|
|
#include "base/path_service.h"
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/task/single_thread_task_runner.h"
|
2018-01-03 10:05:16 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2021-06-15 00:32:56 +00:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser_observer.h"
|
|
|
|
#include "shell/browser/electron_browser_main_parts.h"
|
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/window_list.h"
|
|
|
|
#include "shell/common/application_info.h"
|
2024-07-22 09:31:32 +00:00
|
|
|
#include "shell/common/gin_converters/login_item_settings_converter.h"
|
2019-10-24 05:47:58 +00:00
|
|
|
#include "shell/common/gin_helper/arguments.h"
|
2022-11-17 19:59:23 +00:00
|
|
|
#include "shell/common/thread_restrictions.h"
|
2013-05-02 15:43:23 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2024-03-22 13:00:21 +00:00
|
|
|
LoginItemSettings::LoginItemSettings() = default;
|
|
|
|
LoginItemSettings::~LoginItemSettings() = default;
|
|
|
|
LoginItemSettings::LoginItemSettings(const LoginItemSettings& other) = default;
|
|
|
|
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
LaunchItem::LaunchItem() = default;
|
|
|
|
LaunchItem::~LaunchItem() = default;
|
|
|
|
LaunchItem::LaunchItem(const LaunchItem& other) = default;
|
|
|
|
#endif
|
|
|
|
|
2019-12-13 09:57:02 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Call |quit| after Chromium is fully started.
|
|
|
|
//
|
|
|
|
// This is important for quitting immediately in the "ready" event, when
|
|
|
|
// certain initialization task may still be pending, and quitting at that time
|
|
|
|
// could end up with crash on exit.
|
|
|
|
void RunQuitClosure(base::OnceClosure quit) {
|
|
|
|
// On Linux/Windows the "ready" event is emitted in "PreMainMessageLoopRun",
|
|
|
|
// make sure we quit after message loop has run for once.
|
2023-02-03 11:43:42 +00:00
|
|
|
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
|
|
|
|
std::move(quit));
|
2019-12-13 09:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-05-21 22:18:38 +00:00
|
|
|
Browser::Browser() {
|
2013-05-02 15:43:23 +00:00
|
|
|
WindowList::AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Browser::~Browser() {
|
|
|
|
WindowList::RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
2024-07-29 17:42:57 +00:00
|
|
|
void Browser::AddObserver(BrowserObserver* obs) {
|
|
|
|
observers_.AddObserver(obs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::RemoveObserver(BrowserObserver* obs) {
|
|
|
|
observers_.RemoveObserver(obs);
|
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
// static
|
|
|
|
Browser* Browser::Get() {
|
|
|
|
return ElectronBrowserMainParts::Get()->browser();
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
|
2021-01-13 21:46:54 +00:00
|
|
|
void Browser::Focus(gin::Arguments* args) {
|
|
|
|
// Focus on the first visible window.
|
|
|
|
for (auto* const window : WindowList::GetWindows()) {
|
|
|
|
if (window->IsVisible()) {
|
|
|
|
window->Focus(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
void Browser::Quit() {
|
2021-07-05 19:45:50 +00:00
|
|
|
if (is_quitting_)
|
2015-10-04 11:20:52 +00:00
|
|
|
return;
|
|
|
|
|
2021-07-05 19:45:50 +00:00
|
|
|
is_quitting_ = HandleBeforeQuit();
|
|
|
|
if (!is_quitting_)
|
2015-02-26 03:33:42 +00:00
|
|
|
return;
|
2013-06-26 09:22:24 +00:00
|
|
|
|
2017-04-06 21:48:58 +00:00
|
|
|
if (electron::WindowList::IsEmpty())
|
2014-04-10 07:22:17 +00:00
|
|
|
NotifyAndShutdown();
|
2017-04-06 21:48:58 +00:00
|
|
|
else
|
|
|
|
electron::WindowList::CloseAllWindows();
|
2013-05-02 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:43:43 +00:00
|
|
|
void Browser::Exit(gin::Arguments* args) {
|
2016-08-30 03:30:56 +00:00
|
|
|
int code = 0;
|
|
|
|
args->GetNext(&code);
|
|
|
|
|
2015-11-06 10:27:13 +00:00
|
|
|
if (!ElectronBrowserMainParts::Get()->SetExitCode(code)) {
|
|
|
|
// Message loop is not ready, quit directly.
|
|
|
|
exit(code);
|
|
|
|
} else {
|
2016-03-31 14:12:03 +00:00
|
|
|
// Prepare to quit when all windows have been closed.
|
2021-07-05 19:45:50 +00:00
|
|
|
is_quitting_ = true;
|
2015-11-06 10:27:13 +00:00
|
|
|
|
2016-03-31 14:12:03 +00:00
|
|
|
// Remember this caller so that we don't emit unrelated events.
|
|
|
|
is_exiting_ = true;
|
|
|
|
|
2015-11-06 10:27:13 +00:00
|
|
|
// Must destroy windows before quitting, otherwise bad things can happen.
|
2017-04-06 21:48:58 +00:00
|
|
|
if (electron::WindowList::IsEmpty()) {
|
2015-12-10 03:22:55 +00:00
|
|
|
Shutdown();
|
2015-11-06 10:27:13 +00:00
|
|
|
} else {
|
|
|
|
// Unlike Quit(), we do not ask to close window, but destroy the window
|
|
|
|
// without asking.
|
2017-04-06 21:25:22 +00:00
|
|
|
electron::WindowList::DestroyAllWindows();
|
2015-11-06 10:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 07:22:17 +00:00
|
|
|
void Browser::Shutdown() {
|
2015-10-04 11:20:52 +00:00
|
|
|
if (is_shutdown_)
|
|
|
|
return;
|
2014-09-25 13:47:54 +00:00
|
|
|
|
2015-10-04 11:20:52 +00:00
|
|
|
is_shutdown_ = true;
|
2021-07-05 19:45:50 +00:00
|
|
|
is_quitting_ = true;
|
2015-10-04 11:20:52 +00:00
|
|
|
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnQuit();
|
2015-10-22 08:06:37 +00:00
|
|
|
|
2019-02-01 15:21:49 +00:00
|
|
|
if (quit_main_message_loop_) {
|
2019-12-13 09:57:02 +00:00
|
|
|
RunQuitClosure(std::move(quit_main_message_loop_));
|
2015-10-22 08:06:37 +00:00
|
|
|
} else {
|
2019-02-01 15:21:49 +00:00
|
|
|
// There is no message loop available so we are in early stage, wait until
|
|
|
|
// the quit_main_message_loop_ is available.
|
|
|
|
// Exiting now would leave defunct processes behind.
|
2015-10-22 08:06:37 +00:00
|
|
|
}
|
2014-04-10 07:22:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:26:01 +00:00
|
|
|
std::string Browser::GetVersion() const {
|
2021-07-14 20:10:37 +00:00
|
|
|
std::string ret = OverriddenApplicationVersion();
|
2018-02-20 16:16:51 +00:00
|
|
|
if (ret.empty())
|
|
|
|
ret = GetExecutableFileVersion();
|
|
|
|
return ret;
|
2013-12-05 02:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::SetVersion(const std::string& version) {
|
2021-07-14 20:10:37 +00:00
|
|
|
OverriddenApplicationVersion() = version;
|
2013-12-05 02:26:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:32:58 +00:00
|
|
|
std::string Browser::GetName() const {
|
2021-07-14 20:10:37 +00:00
|
|
|
std::string ret = OverriddenApplicationName();
|
2018-02-20 16:16:51 +00:00
|
|
|
if (ret.empty())
|
|
|
|
ret = GetExecutableFileProductName();
|
|
|
|
return ret;
|
2013-12-05 02:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::SetName(const std::string& name) {
|
2021-07-14 20:10:37 +00:00
|
|
|
OverriddenApplicationName() = name;
|
2013-12-05 02:32:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:03:10 +00:00
|
|
|
bool Browser::OpenFile(const std::string& file_path) {
|
|
|
|
bool prevent_default = false;
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnOpenFile(&prevent_default, file_path);
|
2013-05-30 08:03:10 +00:00
|
|
|
|
|
|
|
return prevent_default;
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:10:38 +00:00
|
|
|
void Browser::OpenURL(const std::string& url) {
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnOpenURL(url);
|
2013-07-10 08:10:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 02:05:53 +00:00
|
|
|
void Browser::Activate(bool has_visible_windows) {
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnActivate(has_visible_windows);
|
2015-09-14 11:28:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 07:31:46 +00:00
|
|
|
void Browser::WillFinishLaunching() {
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnWillFinishLaunching();
|
2013-06-03 07:31:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 06:28:41 +00:00
|
|
|
void Browser::DidFinishLaunching(base::Value::Dict launch_info) {
|
2018-01-03 10:05:16 +00:00
|
|
|
// Make sure the userData directory is created.
|
2022-11-17 19:59:23 +00:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2018-01-03 10:05:16 +00:00
|
|
|
base::FilePath user_data;
|
2023-02-03 11:43:42 +00:00
|
|
|
if (base::PathService::Get(chrome::DIR_USER_DATA, &user_data)) {
|
2018-01-03 10:05:16 +00:00
|
|
|
base::CreateDirectoryAndGetError(user_data, nullptr);
|
2023-02-03 11:43:42 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
base::SetExtraNoExecuteAllowedPath(chrome::DIR_USER_DATA);
|
|
|
|
#endif
|
|
|
|
}
|
2017-12-09 17:24:37 +00:00
|
|
|
|
2018-01-03 10:05:16 +00:00
|
|
|
is_ready_ = true;
|
2018-06-27 21:06:08 +00:00
|
|
|
if (ready_promise_) {
|
|
|
|
ready_promise_->Resolve();
|
|
|
|
}
|
2018-01-03 10:05:16 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
2022-06-23 06:28:41 +00:00
|
|
|
observer.OnFinishLaunching(launch_info.Clone());
|
2015-06-11 15:22:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 05:47:58 +00:00
|
|
|
v8::Local<v8::Value> Browser::WhenReady(v8::Isolate* isolate) {
|
2018-06-27 21:06:08 +00:00
|
|
|
if (!ready_promise_) {
|
2019-11-01 06:10:32 +00:00
|
|
|
ready_promise_ = std::make_unique<gin_helper::Promise<void>>(isolate);
|
2018-06-27 21:06:08 +00:00
|
|
|
if (is_ready()) {
|
|
|
|
ready_promise_->Resolve();
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 05:47:58 +00:00
|
|
|
return ready_promise_->GetHandle();
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 21:32:14 +00:00
|
|
|
void Browser::OnAccessibilitySupportChanged() {
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnAccessibilitySupportChanged();
|
2016-07-11 21:04:56 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:55:57 +00:00
|
|
|
void Browser::PreMainMessageLoopRun() {
|
|
|
|
for (BrowserObserver& observer : observers_) {
|
|
|
|
observer.OnPreMainMessageLoopRun();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
void Browser::PreCreateThreads() {
|
|
|
|
for (BrowserObserver& observer : observers_) {
|
|
|
|
observer.OnPreCreateThreads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:47:00 +00:00
|
|
|
void Browser::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
|
2019-02-01 15:21:49 +00:00
|
|
|
if (is_shutdown_)
|
2019-12-13 09:57:02 +00:00
|
|
|
RunQuitClosure(std::move(quit_closure));
|
2019-02-01 15:21:49 +00:00
|
|
|
else
|
|
|
|
quit_main_message_loop_ = std::move(quit_closure);
|
2018-10-03 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 07:22:17 +00:00
|
|
|
void Browser::NotifyAndShutdown() {
|
2015-10-04 11:20:52 +00:00
|
|
|
if (is_shutdown_)
|
|
|
|
return;
|
|
|
|
|
2013-05-03 02:53:54 +00:00
|
|
|
bool prevent_default = false;
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnWillQuit(&prevent_default);
|
2013-05-03 02:53:54 +00:00
|
|
|
|
2013-06-26 09:22:24 +00:00
|
|
|
if (prevent_default) {
|
2021-07-05 19:45:50 +00:00
|
|
|
is_quitting_ = false;
|
2013-05-03 02:53:54 +00:00
|
|
|
return;
|
2013-06-26 09:22:24 +00:00
|
|
|
}
|
2013-05-03 02:53:54 +00:00
|
|
|
|
2014-04-10 07:22:17 +00:00
|
|
|
Shutdown();
|
2013-05-03 02:53:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 03:33:42 +00:00
|
|
|
bool Browser::HandleBeforeQuit() {
|
|
|
|
bool prevent_default = false;
|
2017-01-24 03:34:39 +00:00
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnBeforeQuit(&prevent_default);
|
2015-02-26 03:33:42 +00:00
|
|
|
|
|
|
|
return !prevent_default;
|
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
void Browser::OnWindowCloseCancelled(NativeWindow* window) {
|
2021-07-05 19:45:50 +00:00
|
|
|
if (is_quitting_)
|
2013-06-26 09:22:24 +00:00
|
|
|
// Once a beforeunload handler has prevented the closing, we think the quit
|
|
|
|
// is cancelled too.
|
2021-07-05 19:45:50 +00:00
|
|
|
is_quitting_ = false;
|
2013-05-02 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::OnWindowAllClosed() {
|
2017-01-24 03:34:39 +00:00
|
|
|
if (is_exiting_) {
|
2016-03-31 14:12:03 +00:00
|
|
|
Shutdown();
|
2021-07-05 19:45:50 +00:00
|
|
|
} else if (is_quitting_) {
|
2014-04-10 07:22:17 +00:00
|
|
|
NotifyAndShutdown();
|
2017-01-24 03:34:39 +00:00
|
|
|
} else {
|
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnWindowAllClosed();
|
|
|
|
}
|
2013-05-02 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2017-06-11 08:19:01 +00:00
|
|
|
void Browser::NewWindowForTab() {
|
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnNewWindowForTab();
|
|
|
|
}
|
2020-06-16 17:03:41 +00:00
|
|
|
|
|
|
|
void Browser::DidBecomeActive() {
|
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnDidBecomeActive();
|
|
|
|
}
|
2023-04-18 14:53:39 +00:00
|
|
|
|
|
|
|
void Browser::DidResignActive() {
|
|
|
|
for (BrowserObserver& observer : observers_)
|
|
|
|
observer.OnDidResignActive();
|
|
|
|
}
|
2017-06-11 08:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
} // namespace electron
|