electron/atom/browser/browser.cc

229 lines
5.6 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 <string>
2014-03-16 00:30:26 +00:00
#include "atom/browser/atom_browser_main_parts.h"
2017-01-24 03:34:39 +00:00
#include "atom/browser/browser_observer.h"
#include "atom/browser/native_window.h"
2014-03-16 00:30:26 +00: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 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"
#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 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();
2017-01-24 03:38:46 +00:00
if (base::ThreadTaskRunnerHandle::IsSet()) {
2016-11-30 07:30:03 +00:00
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
} else {
// There is no message loop available so we are in early stage.
exit(0);
}
2014-04-10 07:22:17 +00:00
}
2013-12-05 02:26:01 +00:00
std::string Browser::GetVersion() const {
std::string ret = brightray::GetOverriddenApplicationVersion();
if (ret.empty())
ret = GetExecutableFileVersion();
return ret;
2013-12-05 02:26:01 +00:00
}
void Browser::SetVersion(const std::string& version) {
brightray::OverrideApplicationVersion(version);
2013-12-05 02:26:01 +00: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 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;
if (PathService::Get(brightray::DIR_USER_DATA, &user_data))
base::CreateDirectoryAndGetError(user_data, nullptr);
is_ready_ = true;
for (BrowserObserver& observer : observers_)
observer.OnFinishLaunching(launch_info);
}
void Browser::OnAccessibilitySupportChanged() {
2017-01-24 03:34:39 +00:00
for (BrowserObserver& observer : observers_)
observer.OnAccessibilitySupportChanged();
}
void Browser::RequestLogin(
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();
}
}
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