electron/atom/browser/browser.cc

253 lines
6.2 KiB
C++
Raw Normal View History

// 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
// found in the LICENSE file.
2014-03-16 00:30:26 +00:00
#include "atom/browser/browser.h"
#include <memory>
#include <string>
#include <utility>
2014-03-16 00:30:26 +00:00
#include "atom/browser/atom_browser_main_parts.h"
2018-10-24 10:49:10 +00:00
#include "atom/browser/atom_paths.h"
2017-01-24 03:34:39 +00:00
#include "atom/browser/browser_observer.h"
#include "atom/browser/login_handler.h"
#include "atom/browser/native_window.h"
2014-03-16 00:30:26 +00:00
#include "atom/browser/window_list.h"
2018-10-24 10:49:10 +00:00
#include "atom/common/application_info.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
2017-01-24 03:38:46 +00:00
#include "base/run_loop.h"
#include "base/threading/thread_restrictions.h"
2016-11-30 07:30:03 +00:00
#include "base/threading/thread_task_runner_handle.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 11:20:52 +00:00
if (is_quiting_)
return;
is_quiting_ = HandleBeforeQuit();
if (!is_quiting_)
return;
if (atom::WindowList::IsEmpty())
2014-04-10 07:22:17 +00:00
NotifyAndShutdown();
else
atom::WindowList::CloseAllWindows();
}
2016-08-30 03:30:56 +00: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 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;
2014-04-10 07:22:17 +00:00
is_quiting_ = true;
2015-10-04 11:20:52 +00:00
2017-01-24 03:34:39 +00:00
for (BrowserObserver& observer : observers_)
observer.OnQuit();
if (quit_main_message_loop_) {
std::move(quit_main_message_loop_).Run();
} else {
// 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.
}
2014-04-10 07:22:17 +00:00
}
2013-12-05 02:26:01 +00:00
std::string Browser::GetVersion() const {
2018-10-24 10:49:10 +00:00
std::string ret = GetOverriddenApplicationVersion();
if (ret.empty())
ret = GetExecutableFileVersion();
return ret;
2013-12-05 02:26:01 +00:00
}
void Browser::SetVersion(const std::string& version) {
2018-10-24 10:49:10 +00:00
OverrideApplicationVersion(version);
2013-12-05 02:26:01 +00:00
}
std::string Browser::GetName() const {
2018-10-24 10:49:10 +00:00
std::string ret = GetOverriddenApplicationName();
if (ret.empty())
ret = GetExecutableFileProductName();
return ret;
}
void Browser::SetName(const std::string& name) {
2018-10-24 10:49:10 +00:00
OverrideApplicationName(name);
}
2016-07-01 08:39:01 +00:00
int Browser::GetBadgeCount() {
2016-07-02 01:36:11 +00:00
return badge_count_;
2016-07-01 08:39:01 +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);
return prevent_default;
}
void Browser::OpenURL(const std::string& url) {
2017-01-24 03:34:39 +00:00
for (BrowserObserver& observer : observers_)
observer.OnOpenURL(url);
}
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);
}
void Browser::WillFinishLaunching() {
2017-01-24 03:34:39 +00: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;
2018-10-24 10:49:10 +00:00
if (base::PathService::Get(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 03:34:39 +00: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 03:34:39 +00:00
for (BrowserObserver& observer : observers_)
observer.OnLogin(login_handler, *(request_details.get()));
2015-10-28 11:34:01 +00:00
}
void Browser::PreMainMessageLoopRun() {
for (BrowserObserver& observer : observers_) {
observer.OnPreMainMessageLoopRun();
}
}
void Browser::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
if (is_shutdown_)
std::move(quit_closure).Run();
else
quit_main_message_loop_ = std::move(quit_closure);
}
2014-04-10 07:22:17 +00:00
void Browser::NotifyAndShutdown() {
2015-10-04 11:20:52 +00:00
if (is_shutdown_)
return;
bool prevent_default = false;
2017-01-24 03:34:39 +00:00
for (BrowserObserver& observer : observers_)
observer.OnWillQuit(&prevent_default);
if (prevent_default) {
is_quiting_ = false;
return;
}
2014-04-10 07:22:17 +00:00
Shutdown();
}
bool Browser::HandleBeforeQuit() {
bool prevent_default = false;
2017-01-24 03:34:39 +00: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 03:34:39 +00:00
if (is_exiting_) {
Shutdown();
2017-01-24 03:34:39 +00:00
} else if (is_quiting_) {
2014-04-10 07:22:17 +00:00
NotifyAndShutdown();
2017-01-24 03:34:39 +00:00
} else {
for (BrowserObserver& observer : observers_)
observer.OnWindowAllClosed();
}
}
#if defined(OS_MACOSX)
void Browser::NewWindowForTab() {
for (BrowserObserver& observer : observers_)
observer.OnNewWindowForTab();
}
#endif
} // namespace atom