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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2013-05-02 15:43:23 +00:00
|
|
|
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2015-11-06 10:27:13 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/window_list.h"
|
2014-04-10 07:22:17 +00:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2013-05-02 15:43:23 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
Browser::Browser()
|
2014-10-30 13:32:35 +00:00
|
|
|
: is_quiting_(false),
|
2016-03-31 14:12:03 +00:00
|
|
|
is_exiting_(false),
|
2015-10-04 11:20:52 +00:00
|
|
|
is_ready_(false),
|
|
|
|
is_shutdown_(false) {
|
2013-05-02 15:43:23 +00:00
|
|
|
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;
|
|
|
|
|
2015-02-26 03:33:42 +00:00
|
|
|
is_quiting_ = HandleBeforeQuit();
|
|
|
|
if (!is_quiting_)
|
|
|
|
return;
|
2013-06-26 09:22:24 +00:00
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
atom::WindowList* window_list = atom::WindowList::GetInstance();
|
|
|
|
if (window_list->size() == 0)
|
2014-04-10 07:22:17 +00:00
|
|
|
NotifyAndShutdown();
|
2013-05-02 15:43:23 +00:00
|
|
|
|
|
|
|
window_list->CloseAllWindows();
|
|
|
|
}
|
|
|
|
|
2015-11-06 10:27:13 +00:00
|
|
|
void Browser::Exit(int code) {
|
|
|
|
if (!AtomBrowserMainParts::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.
|
2015-11-06 10:27:13 +00:00
|
|
|
is_quiting_ = true;
|
|
|
|
|
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.
|
|
|
|
atom::WindowList* window_list = atom::WindowList::GetInstance();
|
|
|
|
if (window_list->size() == 0) {
|
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.
|
|
|
|
for (NativeWindow* window : *window_list)
|
|
|
|
window->CloseContents(nullptr); // e.g. Destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnQuit());
|
2015-10-22 08:06:37 +00:00
|
|
|
|
|
|
|
if (base::MessageLoop::current()) {
|
|
|
|
base::MessageLoop::current()->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 {
|
|
|
|
if (version_override_.empty()) {
|
|
|
|
std::string version = GetExecutableFileVersion();
|
|
|
|
if (!version.empty())
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return version_override_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::SetVersion(const std::string& version) {
|
|
|
|
version_override_ = version;
|
|
|
|
}
|
|
|
|
|
2013-12-05 02:32:58 +00:00
|
|
|
std::string Browser::GetName() const {
|
2013-12-05 02:42:04 +00:00
|
|
|
if (name_override_.empty()) {
|
|
|
|
std::string name = GetExecutableFileProductName();
|
|
|
|
if (!name.empty())
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name_override_;
|
2013-12-05 02:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::SetName(const std::string& name) {
|
|
|
|
name_override_ = name;
|
|
|
|
}
|
|
|
|
|
2013-05-30 08:03:10 +00:00
|
|
|
bool Browser::OpenFile(const std::string& file_path) {
|
|
|
|
bool prevent_default = false;
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver,
|
|
|
|
observers_,
|
|
|
|
OnOpenFile(&prevent_default, file_path));
|
|
|
|
|
|
|
|
return prevent_default;
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:10:38 +00:00
|
|
|
void Browser::OpenURL(const std::string& url) {
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnOpenURL(url));
|
|
|
|
}
|
|
|
|
|
2015-09-15 02:05:53 +00:00
|
|
|
void Browser::Activate(bool has_visible_windows) {
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver,
|
|
|
|
observers_,
|
|
|
|
OnActivate(has_visible_windows));
|
2015-09-14 11:28:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 07:31:46 +00:00
|
|
|
void Browser::WillFinishLaunching() {
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillFinishLaunching());
|
|
|
|
}
|
|
|
|
|
2013-05-30 11:12:14 +00:00
|
|
|
void Browser::DidFinishLaunching() {
|
2014-10-30 13:32:35 +00:00
|
|
|
is_ready_ = true;
|
2013-05-30 11:12:14 +00:00
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnFinishLaunching());
|
2015-06-11 15:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 11:34:01 +00:00
|
|
|
void Browser::RequestLogin(LoginHandler* login_handler) {
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnLogin(login_handler));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default));
|
|
|
|
|
2013-06-26 09:22:24 +00:00
|
|
|
if (prevent_default) {
|
|
|
|
is_quiting_ = 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;
|
2015-02-26 05:57:25 +00:00
|
|
|
FOR_EACH_OBSERVER(BrowserObserver,
|
|
|
|
observers_,
|
|
|
|
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) {
|
2014-04-14 16:02:33 +00:00
|
|
|
if (is_quiting_)
|
2013-06-26 09:22:24 +00:00
|
|
|
// Once a beforeunload handler has prevented the closing, we think the quit
|
|
|
|
// is cancelled too.
|
|
|
|
is_quiting_ = false;
|
2013-05-02 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::OnWindowAllClosed() {
|
2016-03-31 14:12:03 +00:00
|
|
|
if (is_exiting_)
|
|
|
|
Shutdown();
|
|
|
|
else if (is_quiting_)
|
2014-04-10 07:22:17 +00:00
|
|
|
NotifyAndShutdown();
|
2013-05-03 02:53:54 +00:00
|
|
|
else
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWindowAllClosed());
|
2013-05-02 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 04:58:58 +00:00
|
|
|
void Browser::PlatformThemeChanged() {
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnPlatformThemeChanged());
|
2016-03-01 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
} // namespace atom
|