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-04-12 01:46:58 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_main_parts.h"
|
2013-04-12 01:46:58 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-10-23 20:15:55 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include <glib.h> // for g_setenv()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "base/base_switches.h"
|
2014-07-03 17:30:36 +00:00
|
|
|
#include "base/command_line.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "base/feature_list.h"
|
|
|
|
#include "base/path_service.h"
|
2020-06-22 17:35:10 +00:00
|
|
|
#include "base/run_loop.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2018-10-05 19:12:53 +00:00
|
|
|
#include "chrome/browser/icon_manager.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2016-08-03 23:00:25 +00:00
|
|
|
#include "content/public/browser/child_process_security_policy.h"
|
2020-01-17 18:41:52 +00:00
|
|
|
#include "content/public/browser/device_service.h"
|
2018-10-19 13:50:30 +00:00
|
|
|
#include "content/public/browser/web_ui_controller_factory.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "content/public/common/content_features.h"
|
2018-10-19 13:50:30 +00:00
|
|
|
#include "content/public/common/content_switches.h"
|
2018-04-05 21:09:37 +00:00
|
|
|
#include "content/public/common/result_codes.h"
|
2018-10-01 20:00:53 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "media/base/localized_strings.h"
|
2019-02-27 12:06:11 +00:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2019-11-22 02:05:41 +00:00
|
|
|
#include "services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/app/electron_main_delegate.h"
|
|
|
|
#include "shell/browser/api/electron_api_app.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/browser/browser_process_impl.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_client.h"
|
|
|
|
#include "shell/browser/electron_browser_context.h"
|
|
|
|
#include "shell/browser/electron_web_ui_controller_factory.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/feature_list.h"
|
|
|
|
#include "shell/browser/javascript_environment.h"
|
|
|
|
#include "shell/browser/media/media_capture_devices_dispatcher.h"
|
|
|
|
#include "shell/browser/ui/devtools_manager_delegate.h"
|
|
|
|
#include "shell/common/api/electron_bindings.h"
|
|
|
|
#include "shell/common/application_info.h"
|
|
|
|
#include "shell/common/asar/asar_util.h"
|
2020-05-07 20:31:26 +00:00
|
|
|
#include "shell/common/electron_paths.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_helper/trackable_object.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_bindings.h"
|
|
|
|
#include "shell/common/node_includes.h"
|
2018-03-14 05:42:08 +00:00
|
|
|
#include "ui/base/idle/idle.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "ui/base/ui_base_switches.h"
|
|
|
|
|
|
|
|
#if defined(USE_AURA)
|
|
|
|
#include "ui/display/display.h"
|
|
|
|
#include "ui/display/screen.h"
|
|
|
|
#include "ui/views/widget/desktop_aura/desktop_screen.h"
|
|
|
|
#include "ui/wm/core/wm_state.h"
|
|
|
|
#endif
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2020-10-20 18:24:52 +00:00
|
|
|
#if defined(OS_LINUX)
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "base/environment.h"
|
|
|
|
#include "base/nix/xdg_util.h"
|
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2020-10-20 18:24:52 +00:00
|
|
|
#include "ui/gtk/gtk_ui.h"
|
|
|
|
#include "ui/gtk/gtk_ui_delegate.h"
|
|
|
|
#include "ui/gtk/gtk_util.h"
|
|
|
|
#include "ui/views/linux_ui/linux_ui.h"
|
|
|
|
|
|
|
|
#if defined(USE_OZONE)
|
|
|
|
#include "ui/ozone/public/ozone_platform.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_X11)
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "ui/base/x/x11_util.h"
|
2016-01-11 11:23:53 +00:00
|
|
|
#include "ui/events/devices/x11/touch_factory_x11.h"
|
2020-05-21 22:41:25 +00:00
|
|
|
#include "ui/gfx/color_utils.h"
|
2020-04-13 23:39:26 +00:00
|
|
|
#include "ui/gfx/x/x11_types.h"
|
2020-05-26 20:06:26 +00:00
|
|
|
#include "ui/gfx/x/xproto_util.h"
|
2020-04-21 00:25:38 +00:00
|
|
|
#include "ui/gtk/x/gtk_ui_delegate_x11.h"
|
2020-10-20 18:24:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_OZONE) || defined(USE_X11)
|
|
|
|
#include "ui/base/ui_base_features.h"
|
|
|
|
#endif
|
|
|
|
|
2018-10-23 20:15:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "ui/base/cursor/cursor_loader_win.h"
|
2019-01-12 00:25:13 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
2018-10-23 20:15:55 +00:00
|
|
|
#include "ui/base/l10n/l10n_util_win.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "ui/display/win/dpi.h"
|
2019-04-20 17:20:37 +00:00
|
|
|
#include "ui/gfx/system_fonts_win.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "ui/strings/grit/app_locale_settings.h"
|
2014-07-03 17:30:36 +00:00
|
|
|
#endif
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/cocoa/views_delegate_mac.h"
|
2018-04-19 06:38:33 +00:00
|
|
|
#else
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/ui/views/electron_views_delegate.h"
|
2018-04-19 06:26:34 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-23 20:15:55 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include "device/bluetooth/bluetooth_adapter_factory.h"
|
|
|
|
#include "device/bluetooth/dbus/dbus_bluez_manager_wrapper_linux.h"
|
|
|
|
#endif
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
#include "components/keyed_service/content/browser_context_dependency_manager.h"
|
|
|
|
#include "extensions/browser/browser_context_keyed_service_factories.h"
|
|
|
|
#include "extensions/common/extension_api.h"
|
2020-02-03 22:01:10 +00:00
|
|
|
#include "shell/browser/extensions/electron_browser_context_keyed_service_factories.h"
|
|
|
|
#include "shell/browser/extensions/electron_extensions_browser_client.h"
|
|
|
|
#include "shell/common/extensions/electron_extensions_client.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
#include "chrome/browser/spellchecker/spellcheck_factory.h" // nogncheck
|
|
|
|
#endif
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-04-12 01:46:58 +00:00
|
|
|
|
2016-11-30 07:30:03 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <typename T>
|
2015-12-03 09:04:40 +00:00
|
|
|
void Erase(T* container, typename T::iterator iter) {
|
|
|
|
container->erase(iter);
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:15:55 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
// gfx::Font callbacks
|
2019-04-20 17:20:37 +00:00
|
|
|
void AdjustUIFont(gfx::win::FontAdjustment* font_adjustment) {
|
2019-01-12 01:00:43 +00:00
|
|
|
l10n_util::NeedOverrideDefaultUIFont(&font_adjustment->font_family_override,
|
|
|
|
&font_adjustment->font_scale);
|
|
|
|
font_adjustment->font_scale *= display::win::GetAccessibilityFontScale();
|
2018-10-23 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GetMinimumFontSize() {
|
2019-01-12 01:00:43 +00:00
|
|
|
int min_font_size;
|
|
|
|
base::StringToInt(l10n_util::GetStringUTF16(IDS_MINIMUM_UI_FONT_SIZE),
|
|
|
|
&min_font_size);
|
|
|
|
return min_font_size;
|
2018-10-23 20:15:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
base::string16 MediaStringProvider(media::MessageId id) {
|
|
|
|
switch (id) {
|
|
|
|
case media::DEFAULT_AUDIO_DEVICE_NAME:
|
|
|
|
return base::ASCIIToUTF16("Default");
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
case media::COMMUNICATIONS_AUDIO_DEVICE_NAME:
|
|
|
|
return base::ASCIIToUTF16("Communications");
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return base::string16();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:05:34 +00:00
|
|
|
#if defined(OS_LINUX)
|
2018-10-23 20:15:55 +00:00
|
|
|
void OverrideLinuxAppDataPath() {
|
|
|
|
base::FilePath path;
|
2018-10-24 10:49:10 +00:00
|
|
|
if (base::PathService::Get(DIR_APP_DATA, &path))
|
2018-10-23 20:15:55 +00:00
|
|
|
return;
|
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
path = base::nix::GetXDGDirectory(env.get(), base::nix::kXdgConfigHomeEnvVar,
|
|
|
|
base::nix::kDotConfigDir);
|
2018-10-24 10:49:10 +00:00
|
|
|
base::PathService::Override(DIR_APP_DATA, path);
|
2018-10-23 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 22:41:25 +00:00
|
|
|
// GTK does not provide a way to check if current theme is dark, so we compare
|
|
|
|
// the text and background luminosity to get a result.
|
|
|
|
// This trick comes from FireFox.
|
|
|
|
void UpdateDarkThemeSetting() {
|
|
|
|
float bg = color_utils::GetRelativeLuminance(gtk::GetBgColor("GtkLabel"));
|
|
|
|
float fg = color_utils::GetRelativeLuminance(gtk::GetFgColor("GtkLabel"));
|
|
|
|
bool is_dark = fg > bg;
|
|
|
|
// Pass it to NativeUi theme, which is used by the nativeTheme module and most
|
|
|
|
// places in Electron.
|
|
|
|
ui::NativeTheme::GetInstanceForNativeUi()->set_use_dark_colors(is_dark);
|
|
|
|
// Pass it to Web Theme, to make "prefers-color-scheme" media query work.
|
|
|
|
ui::NativeTheme::GetInstanceForWeb()->set_use_dark_colors(is_dark);
|
|
|
|
}
|
2018-10-23 20:15:55 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-30 07:30:03 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-09-21 00:11:59 +00:00
|
|
|
#if defined(OS_LINUX)
|
2020-05-21 22:41:25 +00:00
|
|
|
class DarkThemeObserver : public ui::NativeThemeObserver {
|
|
|
|
public:
|
|
|
|
DarkThemeObserver() = default;
|
|
|
|
|
|
|
|
// ui::NativeThemeObserver:
|
|
|
|
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override {
|
|
|
|
UpdateDarkThemeSetting();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-04-22 13:32:48 +00:00
|
|
|
// static
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserMainParts* ElectronBrowserMainParts::self_ = nullptr;
|
2013-04-22 13:32:48 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserMainParts::ElectronBrowserMainParts(
|
2018-10-04 18:08:56 +00:00
|
|
|
const content::MainFunctionParams& params)
|
2018-10-11 13:52:12 +00:00
|
|
|
: fake_browser_process_(new BrowserProcessImpl),
|
2015-06-18 08:33:12 +00:00
|
|
|
browser_(new Browser),
|
2019-05-03 18:11:41 +00:00
|
|
|
node_bindings_(
|
2020-10-27 17:51:45 +00:00
|
|
|
NodeBindings::Create(NodeBindings::BrowserEnvironment::kBrowser)),
|
2020-07-03 16:19:27 +00:00
|
|
|
electron_bindings_(new ElectronBindings(node_bindings_->uv_loop())) {
|
2020-02-04 20:19:40 +00:00
|
|
|
DCHECK(!self_) << "Cannot have two ElectronBrowserMainParts";
|
2013-04-22 13:32:48 +00:00
|
|
|
self_ = this;
|
2013-04-12 01:46:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserMainParts::~ElectronBrowserMainParts() {
|
2017-03-10 08:33:27 +00:00
|
|
|
asar::ClearArchives();
|
2013-04-12 01:46:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 13:32:48 +00:00
|
|
|
// static
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserMainParts* ElectronBrowserMainParts::Get() {
|
2013-04-22 13:32:48 +00:00
|
|
|
DCHECK(self_);
|
|
|
|
return self_;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronBrowserMainParts::SetExitCode(int code) {
|
2015-11-06 10:27:13 +00:00
|
|
|
if (!exit_code_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*exit_code_ = code;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
int ElectronBrowserMainParts::GetExitCode() {
|
2015-12-10 02:09:59 +00:00
|
|
|
return exit_code_ != nullptr ? *exit_code_ : 0;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::RegisterDestructionCallback(
|
2018-03-30 13:24:55 +00:00
|
|
|
base::OnceClosure callback) {
|
2018-05-17 00:09:45 +00:00
|
|
|
// The destructors should be called in reversed order, so dependencies between
|
|
|
|
// JavaScript objects can be correctly resolved.
|
|
|
|
// For example WebContentsView => WebContents => Session.
|
|
|
|
destructors_.insert(destructors_.begin(), std::move(callback));
|
2015-06-24 09:58:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
int ElectronBrowserMainParts::PreEarlyInitialization() {
|
2019-02-23 00:27:18 +00:00
|
|
|
field_trial_list_ = std::make_unique<base::FieldTrialList>(nullptr);
|
2020-09-29 16:05:34 +00:00
|
|
|
#if defined(OS_LINUX)
|
2018-10-23 20:15:55 +00:00
|
|
|
OverrideLinuxAppDataPath();
|
2020-09-29 16:05:34 +00:00
|
|
|
#endif
|
2018-10-23 20:15:55 +00:00
|
|
|
|
2015-10-04 11:21:36 +00:00
|
|
|
#if defined(OS_POSIX)
|
|
|
|
HandleSIGCHLD();
|
|
|
|
#endif
|
2018-04-05 21:09:37 +00:00
|
|
|
|
2020-10-16 01:30:41 +00:00
|
|
|
return content::RESULT_CODE_NORMAL_EXIT;
|
2015-10-04 11:21:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PostEarlyInitialization() {
|
2018-10-03 07:06:40 +00:00
|
|
|
// A workaround was previously needed because there was no ThreadTaskRunner
|
|
|
|
// set. If this check is failing we may need to re-add that workaround
|
|
|
|
DCHECK(base::ThreadTaskRunnerHandle::IsSet());
|
2015-10-22 06:56:57 +00:00
|
|
|
|
|
|
|
// The ProxyResolverV8 has setup a complete V8 environment, in order to
|
|
|
|
// avoid conflicts we only initialize our V8 environment after that.
|
2019-09-16 22:12:00 +00:00
|
|
|
js_env_ = std::make_unique<JavascriptEnvironment>(node_bindings_->uv_loop());
|
2017-12-22 12:17:41 +00:00
|
|
|
|
2020-03-11 01:16:58 +00:00
|
|
|
v8::HandleScope scope(js_env_->isolate());
|
|
|
|
|
2017-12-22 12:17:41 +00:00
|
|
|
node_bindings_->Initialize();
|
|
|
|
// Create the global environment.
|
2018-01-06 15:58:24 +00:00
|
|
|
node::Environment* env = node_bindings_->CreateEnvironment(
|
2020-02-25 16:46:08 +00:00
|
|
|
js_env_->context(), js_env_->platform());
|
2019-09-16 22:12:00 +00:00
|
|
|
node_env_ = std::make_unique<NodeEnvironment>(env);
|
2017-12-22 12:17:41 +00:00
|
|
|
|
2020-07-14 17:47:20 +00:00
|
|
|
env->set_trace_sync_io(env->options()->trace_sync_io);
|
|
|
|
|
2017-12-22 12:17:41 +00:00
|
|
|
// Add Electron extended APIs.
|
2019-03-18 19:37:06 +00:00
|
|
|
electron_bindings_->BindTo(js_env_->isolate(), env->process_object());
|
2017-12-22 12:17:41 +00:00
|
|
|
|
|
|
|
// Load everything.
|
|
|
|
node_bindings_->LoadEnvironment(env);
|
|
|
|
|
|
|
|
// Wrap the uv loop with global env.
|
|
|
|
node_bindings_->set_uv_env(env);
|
2018-08-03 00:53:54 +00:00
|
|
|
|
2018-10-23 20:15:55 +00:00
|
|
|
// We already initialized the feature list in PreEarlyInitialization(), but
|
2018-08-03 00:53:54 +00:00
|
|
|
// the user JS script would not have had a chance to alter the command-line
|
|
|
|
// switches at that point. Lets reinitialize it here to pick up the
|
|
|
|
// command-line changes.
|
|
|
|
base::FeatureList::ClearInstanceForTesting();
|
2018-10-23 20:15:55 +00:00
|
|
|
InitializeFeatureList();
|
2018-11-06 06:10:04 +00:00
|
|
|
|
|
|
|
// Initialize after user script environment creation.
|
|
|
|
fake_browser_process_->PostEarlyInitialization();
|
2013-04-13 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
int ElectronBrowserMainParts::PreCreateThreads() {
|
2018-10-23 20:15:55 +00:00
|
|
|
#if defined(USE_AURA)
|
|
|
|
display::Screen* screen = views::CreateDesktopScreen();
|
|
|
|
display::Screen::SetScreenInstance(screen);
|
2020-09-21 00:11:59 +00:00
|
|
|
#if defined(OS_LINUX)
|
2018-10-23 20:15:55 +00:00
|
|
|
views::LinuxUI::instance()->UpdateDeviceScaleFactor();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!views::LayoutProvider::Get())
|
2019-09-16 22:12:00 +00:00
|
|
|
layout_provider_ = std::make_unique<views::LayoutProvider>();
|
2018-10-23 08:45:41 +00:00
|
|
|
|
|
|
|
// Initialize the app locale.
|
2018-10-23 20:15:55 +00:00
|
|
|
fake_browser_process_->SetApplicationLocale(
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserClient::Get()->GetApplicationLocale());
|
2018-10-23 20:15:55 +00:00
|
|
|
|
|
|
|
// Force MediaCaptureDevicesDispatcher to be created on UI thread.
|
|
|
|
MediaCaptureDevicesDispatcher::GetInstance();
|
2018-03-14 05:42:08 +00:00
|
|
|
|
2018-10-19 18:51:43 +00:00
|
|
|
// Force MediaCaptureDevicesDispatcher to be created on UI thread.
|
|
|
|
MediaCaptureDevicesDispatcher::GetInstance();
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2018-04-18 01:55:30 +00:00
|
|
|
ui::InitIdleMonitor();
|
|
|
|
#endif
|
2018-03-14 05:42:08 +00:00
|
|
|
|
2019-07-03 01:22:09 +00:00
|
|
|
fake_browser_process_->PreCreateThreads();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
// Notify observers.
|
|
|
|
Browser::Get()->PreCreateThreads();
|
|
|
|
|
2018-10-23 20:15:55 +00:00
|
|
|
return 0;
|
2017-12-16 09:21:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PostCreateThreads() {
|
2019-11-22 02:05:41 +00:00
|
|
|
base::PostTask(
|
|
|
|
FROM_HERE, {content::BrowserThread::IO},
|
|
|
|
base::BindOnce(&tracing::TracingSamplerProfiler::CreateOnChildThread));
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PostDestroyThreads() {
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
extensions_browser_client_.reset();
|
|
|
|
extensions::ExtensionsBrowserClient::Set(nullptr);
|
|
|
|
#endif
|
2018-10-23 20:15:55 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
device::BluetoothAdapterFactory::Shutdown();
|
|
|
|
bluez::DBusBluezManagerWrapperLinux::Shutdown();
|
|
|
|
#endif
|
2018-11-07 13:23:09 +00:00
|
|
|
fake_browser_process_->PostDestroyThreads();
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::ToolkitInitialized() {
|
2020-04-13 23:39:26 +00:00
|
|
|
#if defined(USE_X11)
|
2020-10-20 18:24:52 +00:00
|
|
|
if (!features::IsUsingOzonePlatform()) {
|
|
|
|
// In Aura/X11, Gtk-based LinuxUI implementation is used.
|
|
|
|
gtk_ui_delegate_ =
|
|
|
|
std::make_unique<ui::GtkUiDelegateX11>(x11::Connection::Get());
|
|
|
|
ui::GtkUiDelegate::SetInstance(gtk_ui_delegate_.get());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
views::LinuxUI* linux_ui = BuildGtkUi(ui::GtkUiDelegate::instance());
|
2020-05-21 22:41:25 +00:00
|
|
|
views::LinuxUI::SetInstance(linux_ui);
|
|
|
|
linux_ui->Initialize();
|
|
|
|
|
|
|
|
// Chromium does not respect GTK dark theme setting, but they may change
|
|
|
|
// in future and this code might be no longer needed. Check the Chromium
|
|
|
|
// issue to keep updated:
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=998903
|
|
|
|
UpdateDarkThemeSetting();
|
2020-10-13 17:25:21 +00:00
|
|
|
// Update the native theme when GTK theme changes. The GetNativeTheme
|
2020-05-21 22:41:25 +00:00
|
|
|
// here returns a NativeThemeGtk, which monitors GTK settings.
|
|
|
|
dark_theme_observer_.reset(new DarkThemeObserver);
|
|
|
|
linux_ui->GetNativeTheme(nullptr)->AddObserver(dark_theme_observer_.get());
|
2018-10-23 20:15:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_AURA)
|
2019-09-16 22:12:00 +00:00
|
|
|
wm_state_ = std::make_unique<wm::WMState>();
|
2018-10-23 20:15:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
2019-04-20 17:20:37 +00:00
|
|
|
gfx::win::SetAdjustFontCallback(&AdjustUIFont);
|
|
|
|
gfx::win::SetGetMinimumFontSizeCallback(&GetMinimumFontSize);
|
2018-10-23 20:15:55 +00:00
|
|
|
|
|
|
|
wchar_t module_name[MAX_PATH] = {0};
|
2019-05-07 13:19:14 +00:00
|
|
|
if (GetModuleFileName(NULL, module_name, base::size(module_name)))
|
2018-10-23 20:15:55 +00:00
|
|
|
ui::CursorLoaderWin::SetCursorResourceModule(module_name);
|
|
|
|
#endif
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2018-04-19 06:26:34 +00:00
|
|
|
views_delegate_.reset(new ViewsDelegateMac);
|
2018-04-19 06:38:33 +00:00
|
|
|
#else
|
2019-09-16 22:12:00 +00:00
|
|
|
views_delegate_ = std::make_unique<ViewsDelegate>();
|
2018-04-19 06:26:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PreMainMessageLoopRun() {
|
2014-08-04 12:50:05 +00:00
|
|
|
// Run user's main script before most things get initialized, so we can have
|
|
|
|
// a chance to setup everything.
|
|
|
|
node_bindings_->PrepareMessageLoop();
|
|
|
|
node_bindings_->RunMessageLoop();
|
|
|
|
|
2020-03-03 21:35:05 +00:00
|
|
|
// url::Add*Scheme are not threadsafe, this helps prevent data races.
|
|
|
|
url::LockSchemeRegistries();
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
2020-02-03 22:01:10 +00:00
|
|
|
extensions_client_ = std::make_unique<ElectronExtensionsClient>();
|
2019-07-24 23:01:08 +00:00
|
|
|
extensions::ExtensionsClient::Set(extensions_client_.get());
|
|
|
|
|
|
|
|
// BrowserContextKeyedAPIServiceFactories require an ExtensionsBrowserClient.
|
2020-02-03 22:01:10 +00:00
|
|
|
extensions_browser_client_ =
|
|
|
|
std::make_unique<ElectronExtensionsBrowserClient>();
|
2019-07-24 23:01:08 +00:00
|
|
|
extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
|
|
|
|
|
|
|
|
extensions::EnsureBrowserContextKeyedServiceFactoriesBuilt();
|
|
|
|
extensions::electron::EnsureBrowserContextKeyedServiceFactoriesBuilt();
|
|
|
|
#endif
|
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
SpellcheckServiceFactory::GetInstance();
|
|
|
|
#endif
|
|
|
|
|
2016-01-11 11:23:53 +00:00
|
|
|
#if defined(USE_X11)
|
2020-10-20 18:24:52 +00:00
|
|
|
if (!features::IsUsingOzonePlatform())
|
|
|
|
ui::TouchFactory::SetTouchDeviceListFromCommandLine();
|
2016-01-11 11:23:53 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-17 14:23:13 +00:00
|
|
|
content::WebUIControllerFactory::RegisterFactory(
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronWebUIControllerFactory::GetInstance());
|
2017-01-17 14:23:13 +00:00
|
|
|
|
2018-10-19 13:50:30 +00:00
|
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
2020-05-08 04:27:55 +00:00
|
|
|
if (command_line->HasSwitch(switches::kRemoteDebuggingPipe)) {
|
|
|
|
// --remote-debugging-pipe
|
|
|
|
content::DevToolsAgentHost::StartRemoteDebuggingPipeHandler();
|
|
|
|
} else if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) {
|
|
|
|
// --remote-debugging-port
|
2018-10-19 13:50:30 +00:00
|
|
|
DevToolsManagerDelegate::StartHttpHandler();
|
2020-05-08 04:27:55 +00:00
|
|
|
}
|
2013-04-12 07:57:17 +00:00
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if !defined(OS_MAC)
|
2020-02-04 20:19:40 +00:00
|
|
|
// The corresponding call in macOS is in ElectronApplicationDelegate.
|
2013-06-03 07:31:46 +00:00
|
|
|
Browser::Get()->WillFinishLaunching();
|
2018-06-18 07:32:55 +00:00
|
|
|
Browser::Get()->DidFinishLaunching(base::DictionaryValue());
|
2013-05-30 11:12:14 +00:00
|
|
|
#endif
|
2017-09-14 13:08:18 +00:00
|
|
|
|
2018-01-03 09:59:12 +00:00
|
|
|
// Notify observers that main thread message loop was initialized.
|
|
|
|
Browser::Get()->PreMainMessageLoopRun();
|
2013-04-12 07:57:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
bool ElectronBrowserMainParts::MainMessageLoopRun(int* result_code) {
|
2018-10-15 15:26:47 +00:00
|
|
|
js_env_->OnMessageLoopCreated();
|
2015-11-06 10:27:13 +00:00
|
|
|
exit_code_ = result_code;
|
2018-10-23 20:15:55 +00:00
|
|
|
return content::BrowserMainParts::MainMessageLoopRun(result_code);
|
2015-11-06 10:27:13 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PreDefaultMainMessageLoopRun(
|
2018-10-03 21:47:00 +00:00
|
|
|
base::OnceClosure quit_closure) {
|
2019-02-01 15:21:49 +00:00
|
|
|
Browser::Get()->SetMainMessageLoopQuitClosure(std::move(quit_closure));
|
2018-10-03 21:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PostMainMessageLoopStart() {
|
2020-10-20 18:24:52 +00:00
|
|
|
#if defined(USE_OZONE)
|
|
|
|
if (features::IsUsingOzonePlatform()) {
|
|
|
|
auto shutdown_cb =
|
|
|
|
base::BindOnce(base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
|
|
|
|
ui::OzonePlatform::GetInstance()->PostMainMessageLoopStart(
|
|
|
|
std::move(shutdown_cb));
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-23 20:15:55 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
bluez::DBusBluezManagerWrapperLinux::Initialize();
|
|
|
|
#endif
|
2015-10-04 11:21:36 +00:00
|
|
|
#if defined(OS_POSIX)
|
|
|
|
HandleShutdownSignals();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PostMainMessageLoopRun() {
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2015-11-04 09:23:27 +00:00
|
|
|
FreeAppDelegate();
|
|
|
|
#endif
|
|
|
|
|
2015-09-09 11:27:08 +00:00
|
|
|
// Make sure destruction callbacks are called before message loop is
|
|
|
|
// destroyed, otherwise some objects that need to be deleted on IO thread
|
|
|
|
// won't be freed.
|
2015-12-03 09:04:40 +00:00
|
|
|
// We don't use ranged for loop because iterators are getting invalided when
|
|
|
|
// the callback runs.
|
|
|
|
for (auto iter = destructors_.begin(); iter != destructors_.end();) {
|
2018-03-30 13:24:55 +00:00
|
|
|
base::OnceClosure callback = std::move(*iter);
|
|
|
|
if (!callback.is_null())
|
|
|
|
std::move(callback).Run();
|
2015-12-03 09:04:40 +00:00
|
|
|
++iter;
|
|
|
|
}
|
2018-11-06 06:10:04 +00:00
|
|
|
|
2020-03-05 00:59:59 +00:00
|
|
|
// Destroy node platform after all destructors_ are executed, as they may
|
|
|
|
// invoke Node/V8 APIs inside them.
|
2020-07-14 17:47:20 +00:00
|
|
|
node_env_->env()->set_trace_sync_io(false);
|
2020-03-05 00:59:59 +00:00
|
|
|
js_env_->OnMessageLoopDestroying();
|
2020-09-14 21:08:46 +00:00
|
|
|
node::Stop(node_env_->env());
|
2020-04-15 22:30:13 +00:00
|
|
|
node_env_.reset();
|
2020-03-05 00:59:59 +00:00
|
|
|
|
2020-08-17 20:21:53 +00:00
|
|
|
ElectronBrowserContext::browser_context_map().clear();
|
|
|
|
|
2018-11-06 06:10:04 +00:00
|
|
|
fake_browser_process_->PostMainMessageLoopRun();
|
2020-05-08 04:27:55 +00:00
|
|
|
content::DevToolsAgentHost::StopRemoteDebuggingPipeHandler();
|
2015-09-09 11:27:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if !defined(OS_MAC)
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PreMainMessageLoopStart() {
|
2018-10-23 20:15:55 +00:00
|
|
|
PreMainMessageLoopStartCommon();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void ElectronBrowserMainParts::PreMainMessageLoopStartCommon() {
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2019-10-21 21:11:09 +00:00
|
|
|
InitializeMainNib();
|
2019-10-10 15:54:03 +00:00
|
|
|
RegisterURLHandler();
|
2018-10-23 20:15:55 +00:00
|
|
|
#endif
|
|
|
|
media::SetLocalizedStringProvider(MediaStringProvider);
|
|
|
|
}
|
|
|
|
|
2018-04-12 09:53:49 +00:00
|
|
|
device::mojom::GeolocationControl*
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserMainParts::GetGeolocationControl() {
|
2020-01-17 18:41:52 +00:00
|
|
|
if (!geolocation_control_) {
|
|
|
|
content::GetDeviceService().BindGeolocationControl(
|
|
|
|
geolocation_control_.BindNewPipeAndPassReceiver());
|
|
|
|
}
|
2018-04-12 09:53:49 +00:00
|
|
|
return geolocation_control_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
IconManager* ElectronBrowserMainParts::GetIconManager() {
|
2018-10-05 19:12:53 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
if (!icon_manager_.get())
|
2019-09-16 22:12:00 +00:00
|
|
|
icon_manager_ = std::make_unique<IconManager>();
|
2018-10-05 19:12:53 +00:00
|
|
|
return icon_manager_.get();
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|