electron/atom/browser/browser.cc

244 lines
6 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 17:49:37 +08:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
2014-03-16 08:30:26 +08:00
#include "atom/browser/browser.h"
#include <memory>
#include <string>
2014-03-16 08:30:26 +08:00
#include "atom/browser/atom_browser_main_parts.h"
2017-01-24 12:34:39 +09:00
#include "atom/browser/browser_observer.h"
#include "atom/browser/login_handler.h"
#include "atom/browser/native_window.h"
2014-03-16 08:30:26 +08:00
#include "atom/browser/window_list.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
2017-01-24 12:38:46 +09:00
#include "base/run_loop.h"
#include "base/threading/thread_restrictions.h"
2016-11-30 16:30:03 +09:00
#include "base/threading/thread_task_runner_handle.h"
#include "brightray/browser/brightray_paths.h"
#include "brightray/common/application_info.h"
namespace atom {
Browser::LoginItemSettings::LoginItemSettings() = default;
Browser::LoginItemSettings::~LoginItemSettings() = default;
Browser::LoginItemSettings::LoginItemSettings(const LoginItemSettings& other) =
default;
Browser::Browser() {
WindowList::AddObserver(this);
}
Browser::~Browser() {
WindowList::RemoveObserver(this);
}
// static
Browser* Browser::Get() {
return AtomBrowserMainParts::Get()->browser();
}
void Browser::Quit() {
2015-10-04 19:20:52 +08:00
if (is_quiting_)
return;
is_quiting_ = HandleBeforeQuit();
if (!is_quiting_)
return;
if (atom::WindowList::IsEmpty())
2014-04-10 15:22:17 +08:00
NotifyAndShutdown();
else
atom::WindowList::CloseAllWindows();
}
2016-08-30 13:30:56 +10:00
void Browser::Exit(mate::Arguments* args) {
int code = 0;
args->GetNext(&code);
if (!AtomBrowserMainParts::Get()->SetExitCode(code)) {
// Message loop is not ready, quit directly.
exit(code);
} else {
// Prepare to quit when all windows have been closed.
is_quiting_ = true;
// Remember this caller so that we don't emit unrelated events.
is_exiting_ = true;
// Must destroy windows before quitting, otherwise bad things can happen.
if (atom::WindowList::IsEmpty()) {
Shutdown();
} else {
// Unlike Quit(), we do not ask to close window, but destroy the window
// without asking.
atom::WindowList::DestroyAllWindows();
}
}
}
2014-04-10 15:22:17 +08:00
void Browser::Shutdown() {
2015-10-04 19:20:52 +08:00
if (is_shutdown_)
return;
2014-09-25 21:47:54 +08:00
2015-10-04 19:20:52 +08:00
is_shutdown_ = true;
2014-04-10 15:22:17 +08:00
is_quiting_ = true;
2015-10-04 19:20:52 +08:00
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnQuit();
2017-01-24 12:38:46 +09:00
if (base::ThreadTaskRunnerHandle::IsSet()) {
2016-11-30 16:30:03 +09:00
base::ThreadTaskRunnerHandle::Get()->PostTask(
feat: upgrade to Chromium 68.0.3440.128 and Node 10.11.0 (#14677) * Update to Chromium 68.0.3440.128 and Node 10.10.0 * update v8, ffmpeg, chromium, crashpad, boringssl, and webrtc patches * fix SSL_get_tlsext_status_type patch * pass encryption_modes_supported to CdmInfo * kNoSandbox moved into service_manager * bump CHROME_VERSION_STRING TODO: automatically pull in the real chrome version * PathService -> base::PathService * net::X509Certificate::Equals -> net::X509Certificate::EqualsExcludingChain * use content::ChildProcessTerminationInfo * GetHandle() -> GetProcess().Handle() * ScopedNestableTaskAllower doesn't take an argument * net::HttpAuthCache::ClearEntriesAddedWithin -> ClearAllEntries * std::unique_ptr<WebContents> * blink::WebFullscreenOptions * OnAudioStateChanged doesn't take a WebContents * content::RESULT_CODE_NORMAL_EXIT -> service_manager::RESULT_CODE_NORMAL_EXIT * MessageLoopCurrent * WasResized -> SynchronizeVisualProperties * SetTimeStamp takes a base::TimeTicks * ExecuteScriptInIsolatedWorld is single-script only * DispatchNonPersistentCloseEvent takes a callback now * expose URLRequestContextGetter::{Add,Remove}Observer * test: remove no longer existing Chromium test deps cc_blink_unittests has been removed in https://chromium-review.googlesource.com/1053765 mojo_common_unittests has been removed in https://chromium-review.googlesource.com/1028000 * SetFdLimit -> IncreaseFdLimitTo NOTE: the behaviour of this API has changed slightly, and we should mention that in the notes. * MessageLoop::QuitWhenIdleClosure -> RunLoop::QuitCurrentWhenIdleClosureDeprecated * certificate_transparency moved out of net/ pending a clearer decision about what to do with CT in the mean time, copy CreateLogVerifiersForKnownLogs from deleted chromium source * add secure_origin_whitelist to chrome source list NOTE: is this something we actually want? cc @deepak1556 * DrainBackgroundTasks -> DrainTasks * use new node options parser * fix disable_scroll_begin_dcheck.patch * ViewsDelegate::CreateWebContents went away see https://chromium-review.googlesource.com/c/chromium/src/+/1031314 * kZygoteProcess moved into service_manager * test: minor improvements to the Node spec - reformat some parts - better failures reporting with `expect` - skip some tests instead of marking them as passed * chromium removed *_posix.cc from the source filters * test: fix :electron_tests compilation * better crash diagnostics in ffmpeg test * fix: enable back a DCHECK in viz::ServerSharedBitmapManager Fixes #14327. Backports https://chromium-review.googlesource.com/802574. * chore: update linux sysroots * chore: remove obsolete "install-sysroot.py" script * test: fix frame-subscriber test on Mac * disable OSR for now * test: make before-input-event test more robust * test: make run-as-node --inspect test more robust on windows * roll node to v10.11.0 * avoid duplicate files when building a zip * disable failing assert in beginFrameSubscription dirty-rectangle test * experiment with is_cfi = false * fix: build torque with x64 toolchain Co-Authored-By: Alexey Kuzmin <github@alexeykuzmin.com> * test: disable the "app.relaunch" test on Linux * chore: bump node to get header tar file * chore: bump node to fix tar.py line endings
2018-10-03 19:02:14 -07:00
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
} else {
// There is no message loop available so we are in early stage.
exit(0);
}
2014-04-10 15:22:17 +08:00
}
2013-12-05 10:26:01 +08:00
std::string Browser::GetVersion() const {
std::string ret = brightray::GetOverriddenApplicationVersion();
if (ret.empty())
ret = GetExecutableFileVersion();
return ret;
2013-12-05 10:26:01 +08:00
}
void Browser::SetVersion(const std::string& version) {
brightray::OverrideApplicationVersion(version);
2013-12-05 10:26:01 +08:00
}
std::string Browser::GetName() const {
std::string ret = brightray::GetOverriddenApplicationName();
if (ret.empty())
ret = GetExecutableFileProductName();
return ret;
}
void Browser::SetName(const std::string& name) {
brightray::OverrideApplicationName(name);
}
2016-07-01 17:39:01 +09:00
int Browser::GetBadgeCount() {
2016-07-02 10:36:11 +09:00
return badge_count_;
2016-07-01 17:39:01 +09:00
}
bool Browser::OpenFile(const std::string& file_path) {
bool prevent_default = false;
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnOpenFile(&prevent_default, file_path);
return prevent_default;
}
void Browser::OpenURL(const std::string& url) {
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnOpenURL(url);
}
2015-09-14 19:05:53 -07:00
void Browser::Activate(bool has_visible_windows) {
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnActivate(has_visible_windows);
}
void Browser::WillFinishLaunching() {
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnWillFinishLaunching();
}
void Browser::DidFinishLaunching(const base::DictionaryValue& launch_info) {
// Make sure the userData directory is created.
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::FilePath user_data;
feat: upgrade to Chromium 68.0.3440.128 and Node 10.11.0 (#14677) * Update to Chromium 68.0.3440.128 and Node 10.10.0 * update v8, ffmpeg, chromium, crashpad, boringssl, and webrtc patches * fix SSL_get_tlsext_status_type patch * pass encryption_modes_supported to CdmInfo * kNoSandbox moved into service_manager * bump CHROME_VERSION_STRING TODO: automatically pull in the real chrome version * PathService -> base::PathService * net::X509Certificate::Equals -> net::X509Certificate::EqualsExcludingChain * use content::ChildProcessTerminationInfo * GetHandle() -> GetProcess().Handle() * ScopedNestableTaskAllower doesn't take an argument * net::HttpAuthCache::ClearEntriesAddedWithin -> ClearAllEntries * std::unique_ptr<WebContents> * blink::WebFullscreenOptions * OnAudioStateChanged doesn't take a WebContents * content::RESULT_CODE_NORMAL_EXIT -> service_manager::RESULT_CODE_NORMAL_EXIT * MessageLoopCurrent * WasResized -> SynchronizeVisualProperties * SetTimeStamp takes a base::TimeTicks * ExecuteScriptInIsolatedWorld is single-script only * DispatchNonPersistentCloseEvent takes a callback now * expose URLRequestContextGetter::{Add,Remove}Observer * test: remove no longer existing Chromium test deps cc_blink_unittests has been removed in https://chromium-review.googlesource.com/1053765 mojo_common_unittests has been removed in https://chromium-review.googlesource.com/1028000 * SetFdLimit -> IncreaseFdLimitTo NOTE: the behaviour of this API has changed slightly, and we should mention that in the notes. * MessageLoop::QuitWhenIdleClosure -> RunLoop::QuitCurrentWhenIdleClosureDeprecated * certificate_transparency moved out of net/ pending a clearer decision about what to do with CT in the mean time, copy CreateLogVerifiersForKnownLogs from deleted chromium source * add secure_origin_whitelist to chrome source list NOTE: is this something we actually want? cc @deepak1556 * DrainBackgroundTasks -> DrainTasks * use new node options parser * fix disable_scroll_begin_dcheck.patch * ViewsDelegate::CreateWebContents went away see https://chromium-review.googlesource.com/c/chromium/src/+/1031314 * kZygoteProcess moved into service_manager * test: minor improvements to the Node spec - reformat some parts - better failures reporting with `expect` - skip some tests instead of marking them as passed * chromium removed *_posix.cc from the source filters * test: fix :electron_tests compilation * better crash diagnostics in ffmpeg test * fix: enable back a DCHECK in viz::ServerSharedBitmapManager Fixes #14327. Backports https://chromium-review.googlesource.com/802574. * chore: update linux sysroots * chore: remove obsolete "install-sysroot.py" script * test: fix frame-subscriber test on Mac * disable OSR for now * test: make before-input-event test more robust * test: make run-as-node --inspect test more robust on windows * roll node to v10.11.0 * avoid duplicate files when building a zip * disable failing assert in beginFrameSubscription dirty-rectangle test * experiment with is_cfi = false * fix: build torque with x64 toolchain Co-Authored-By: Alexey Kuzmin <github@alexeykuzmin.com> * test: disable the "app.relaunch" test on Linux * chore: bump node to get header tar file * chore: bump node to fix tar.py line endings
2018-10-03 19:02:14 -07:00
if (base::PathService::Get(brightray::DIR_USER_DATA, &user_data))
base::CreateDirectoryAndGetError(user_data, nullptr);
is_ready_ = true;
if (ready_promise_) {
ready_promise_->Resolve();
}
for (BrowserObserver& observer : observers_)
observer.OnFinishLaunching(launch_info);
}
util::Promise* Browser::WhenReady(v8::Isolate* isolate) {
if (!ready_promise_) {
ready_promise_ = new util::Promise(isolate);
if (is_ready()) {
ready_promise_->Resolve();
}
}
return ready_promise_;
}
void Browser::OnAccessibilitySupportChanged() {
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnAccessibilitySupportChanged();
}
void Browser::RequestLogin(
scoped_refptr<LoginHandler> login_handler,
std::unique_ptr<base::DictionaryValue> request_details) {
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnLogin(login_handler, *(request_details.get()));
2015-10-28 19:34:01 +08:00
}
void Browser::PreMainMessageLoopRun() {
for (BrowserObserver& observer : observers_) {
observer.OnPreMainMessageLoopRun();
}
}
2014-04-10 15:22:17 +08:00
void Browser::NotifyAndShutdown() {
2015-10-04 19:20:52 +08:00
if (is_shutdown_)
return;
bool prevent_default = false;
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnWillQuit(&prevent_default);
if (prevent_default) {
is_quiting_ = false;
return;
}
2014-04-10 15:22:17 +08:00
Shutdown();
}
bool Browser::HandleBeforeQuit() {
bool prevent_default = false;
2017-01-24 12:34:39 +09:00
for (BrowserObserver& observer : observers_)
observer.OnBeforeQuit(&prevent_default);
return !prevent_default;
}
void Browser::OnWindowCloseCancelled(NativeWindow* window) {
if (is_quiting_)
// Once a beforeunload handler has prevented the closing, we think the quit
// is cancelled too.
is_quiting_ = false;
}
void Browser::OnWindowAllClosed() {
2017-01-24 12:34:39 +09:00
if (is_exiting_) {
Shutdown();
2017-01-24 12:34:39 +09:00
} else if (is_quiting_) {
2014-04-10 15:22:17 +08:00
NotifyAndShutdown();
2017-01-24 12:34:39 +09:00
} else {
for (BrowserObserver& observer : observers_)
observer.OnWindowAllClosed();
}
}
#if defined(OS_MACOSX)
void Browser::NewWindowForTab() {
for (BrowserObserver& observer : observers_)
observer.OnNewWindowForTab();
}
#endif
} // namespace atom