browser.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/browser.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/files/file_util.h"
  9. #include "base/no_destructor.h"
  10. #include "base/path_service.h"
  11. #include "base/run_loop.h"
  12. #include "base/task/single_thread_task_runner.h"
  13. #include "base/threading/thread_restrictions.h"
  14. #include "chrome/common/chrome_paths.h"
  15. #include "shell/browser/browser_observer.h"
  16. #include "shell/browser/electron_browser_main_parts.h"
  17. #include "shell/browser/login_handler.h"
  18. #include "shell/browser/native_window.h"
  19. #include "shell/browser/window_list.h"
  20. #include "shell/common/application_info.h"
  21. #include "shell/common/electron_paths.h"
  22. #include "shell/common/gin_helper/arguments.h"
  23. #include "shell/common/thread_restrictions.h"
  24. namespace electron {
  25. LoginItemSettings::LoginItemSettings() = default;
  26. LoginItemSettings::~LoginItemSettings() = default;
  27. LoginItemSettings::LoginItemSettings(const LoginItemSettings& other) = default;
  28. #if BUILDFLAG(IS_WIN)
  29. LaunchItem::LaunchItem() = default;
  30. LaunchItem::~LaunchItem() = default;
  31. LaunchItem::LaunchItem(const LaunchItem& other) = default;
  32. #endif
  33. namespace {
  34. // Call |quit| after Chromium is fully started.
  35. //
  36. // This is important for quitting immediately in the "ready" event, when
  37. // certain initialization task may still be pending, and quitting at that time
  38. // could end up with crash on exit.
  39. void RunQuitClosure(base::OnceClosure quit) {
  40. // On Linux/Windows the "ready" event is emitted in "PreMainMessageLoopRun",
  41. // make sure we quit after message loop has run for once.
  42. base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
  43. std::move(quit));
  44. }
  45. } // namespace
  46. Browser::Browser() {
  47. WindowList::AddObserver(this);
  48. }
  49. Browser::~Browser() {
  50. WindowList::RemoveObserver(this);
  51. }
  52. // static
  53. Browser* Browser::Get() {
  54. return ElectronBrowserMainParts::Get()->browser();
  55. }
  56. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  57. void Browser::Focus(gin::Arguments* args) {
  58. // Focus on the first visible window.
  59. for (auto* const window : WindowList::GetWindows()) {
  60. if (window->IsVisible()) {
  61. window->Focus(true);
  62. break;
  63. }
  64. }
  65. }
  66. #endif
  67. void Browser::Quit() {
  68. if (is_quitting_)
  69. return;
  70. is_quitting_ = HandleBeforeQuit();
  71. if (!is_quitting_)
  72. return;
  73. if (electron::WindowList::IsEmpty())
  74. NotifyAndShutdown();
  75. else
  76. electron::WindowList::CloseAllWindows();
  77. }
  78. void Browser::Exit(gin::Arguments* args) {
  79. int code = 0;
  80. args->GetNext(&code);
  81. if (!ElectronBrowserMainParts::Get()->SetExitCode(code)) {
  82. // Message loop is not ready, quit directly.
  83. exit(code);
  84. } else {
  85. // Prepare to quit when all windows have been closed.
  86. is_quitting_ = true;
  87. // Remember this caller so that we don't emit unrelated events.
  88. is_exiting_ = true;
  89. // Must destroy windows before quitting, otherwise bad things can happen.
  90. if (electron::WindowList::IsEmpty()) {
  91. Shutdown();
  92. } else {
  93. // Unlike Quit(), we do not ask to close window, but destroy the window
  94. // without asking.
  95. electron::WindowList::DestroyAllWindows();
  96. }
  97. }
  98. }
  99. void Browser::Shutdown() {
  100. if (is_shutdown_)
  101. return;
  102. is_shutdown_ = true;
  103. is_quitting_ = true;
  104. for (BrowserObserver& observer : observers_)
  105. observer.OnQuit();
  106. if (quit_main_message_loop_) {
  107. RunQuitClosure(std::move(quit_main_message_loop_));
  108. } else {
  109. // There is no message loop available so we are in early stage, wait until
  110. // the quit_main_message_loop_ is available.
  111. // Exiting now would leave defunct processes behind.
  112. }
  113. }
  114. std::string Browser::GetVersion() const {
  115. std::string ret = OverriddenApplicationVersion();
  116. if (ret.empty())
  117. ret = GetExecutableFileVersion();
  118. return ret;
  119. }
  120. void Browser::SetVersion(const std::string& version) {
  121. OverriddenApplicationVersion() = version;
  122. }
  123. std::string Browser::GetName() const {
  124. std::string ret = OverriddenApplicationName();
  125. if (ret.empty())
  126. ret = GetExecutableFileProductName();
  127. return ret;
  128. }
  129. void Browser::SetName(const std::string& name) {
  130. OverriddenApplicationName() = name;
  131. }
  132. bool Browser::OpenFile(const std::string& file_path) {
  133. bool prevent_default = false;
  134. for (BrowserObserver& observer : observers_)
  135. observer.OnOpenFile(&prevent_default, file_path);
  136. return prevent_default;
  137. }
  138. void Browser::OpenURL(const std::string& url) {
  139. for (BrowserObserver& observer : observers_)
  140. observer.OnOpenURL(url);
  141. }
  142. void Browser::Activate(bool has_visible_windows) {
  143. for (BrowserObserver& observer : observers_)
  144. observer.OnActivate(has_visible_windows);
  145. }
  146. void Browser::WillFinishLaunching() {
  147. for (BrowserObserver& observer : observers_)
  148. observer.OnWillFinishLaunching();
  149. }
  150. void Browser::DidFinishLaunching(base::Value::Dict launch_info) {
  151. // Make sure the userData directory is created.
  152. ScopedAllowBlockingForElectron allow_blocking;
  153. base::FilePath user_data;
  154. if (base::PathService::Get(chrome::DIR_USER_DATA, &user_data)) {
  155. base::CreateDirectoryAndGetError(user_data, nullptr);
  156. #if BUILDFLAG(IS_WIN)
  157. base::SetExtraNoExecuteAllowedPath(chrome::DIR_USER_DATA);
  158. #endif
  159. }
  160. is_ready_ = true;
  161. if (ready_promise_) {
  162. ready_promise_->Resolve();
  163. }
  164. for (BrowserObserver& observer : observers_)
  165. observer.OnFinishLaunching(launch_info.Clone());
  166. }
  167. v8::Local<v8::Value> Browser::WhenReady(v8::Isolate* isolate) {
  168. if (!ready_promise_) {
  169. ready_promise_ = std::make_unique<gin_helper::Promise<void>>(isolate);
  170. if (is_ready()) {
  171. ready_promise_->Resolve();
  172. }
  173. }
  174. return ready_promise_->GetHandle();
  175. }
  176. void Browser::OnAccessibilitySupportChanged() {
  177. for (BrowserObserver& observer : observers_)
  178. observer.OnAccessibilitySupportChanged();
  179. }
  180. void Browser::PreMainMessageLoopRun() {
  181. for (BrowserObserver& observer : observers_) {
  182. observer.OnPreMainMessageLoopRun();
  183. }
  184. }
  185. void Browser::PreCreateThreads() {
  186. for (BrowserObserver& observer : observers_) {
  187. observer.OnPreCreateThreads();
  188. }
  189. }
  190. void Browser::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
  191. if (is_shutdown_)
  192. RunQuitClosure(std::move(quit_closure));
  193. else
  194. quit_main_message_loop_ = std::move(quit_closure);
  195. }
  196. void Browser::NotifyAndShutdown() {
  197. if (is_shutdown_)
  198. return;
  199. bool prevent_default = false;
  200. for (BrowserObserver& observer : observers_)
  201. observer.OnWillQuit(&prevent_default);
  202. if (prevent_default) {
  203. is_quitting_ = false;
  204. return;
  205. }
  206. Shutdown();
  207. }
  208. bool Browser::HandleBeforeQuit() {
  209. bool prevent_default = false;
  210. for (BrowserObserver& observer : observers_)
  211. observer.OnBeforeQuit(&prevent_default);
  212. return !prevent_default;
  213. }
  214. void Browser::OnWindowCloseCancelled(NativeWindow* window) {
  215. if (is_quitting_)
  216. // Once a beforeunload handler has prevented the closing, we think the quit
  217. // is cancelled too.
  218. is_quitting_ = false;
  219. }
  220. void Browser::OnWindowAllClosed() {
  221. if (is_exiting_) {
  222. Shutdown();
  223. } else if (is_quitting_) {
  224. NotifyAndShutdown();
  225. } else {
  226. for (BrowserObserver& observer : observers_)
  227. observer.OnWindowAllClosed();
  228. }
  229. }
  230. #if BUILDFLAG(IS_MAC)
  231. void Browser::NewWindowForTab() {
  232. for (BrowserObserver& observer : observers_)
  233. observer.OnNewWindowForTab();
  234. }
  235. void Browser::DidBecomeActive() {
  236. for (BrowserObserver& observer : observers_)
  237. observer.OnDidBecomeActive();
  238. }
  239. void Browser::DidResignActive() {
  240. for (BrowserObserver& observer : observers_)
  241. observer.OnDidResignActive();
  242. }
  243. #endif
  244. } // namespace electron