[go: nahoru, domu]

blob: 793226e317471d7849a6c5d24f92b784fbd2bfbb [file] [log] [blame]
tfarina@chromium.org66fb53c82012-05-15 20:29:491// Copyright (c) 2012 The Chromium Authors. All rights reserved.
stevenjb@google.comc7480942011-11-08 19:18:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tfarina@chromium.orgc27541f2012-05-24 19:25:545#include "chrome/browser/ui/views/chrome_browser_main_extra_parts_views.h"
stevenjb@google.comc7480942011-11-08 19:18:276
cernekee7bc96062017-04-11 04:18:237#include <utility>
8
James Cook2eed9872017-11-14 21:08:549#include "base/command_line.h"
Greg Thompson875403e02018-02-27 16:22:5210#include "build/build_config.h"
Leonard Grey24fe86c2019-04-14 21:02:5611#include "chrome/browser/browser_process.h"
12#include "chrome/browser/net/system_network_context_manager.h"
oshima136691a2014-10-24 21:54:1113#include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
Peter Boströmf01dd3f2018-08-15 21:28:1814#include "chrome/browser/ui/views/chrome_layout_provider.h"
stevenjb@google.comc7480942011-11-08 19:18:2715#include "chrome/browser/ui/views/chrome_views_delegate.h"
Mitsuru Oshima4de59ac2019-09-03 20:49:0916#include "chrome/browser/ui/views/devtools_process_observer.h"
Stefan Teodorescu54339ac2018-07-24 13:24:1817#include "chrome/browser/ui/views/relaunch_notification/relaunch_notification_controller.h"
oshima136691a2014-10-24 21:54:1118#include "components/constrained_window/constrained_window_views.h"
Andrew Lee197daae62019-08-22 21:51:4319#include "components/ui_devtools/connector_delegate.h"
Leonard Grey24fe86c2019-04-14 21:02:5620#include "components/ui_devtools/switches.h"
21#include "components/ui_devtools/views/devtools_server_util.h"
Ken Rockotcbead332019-11-21 04:35:4722#include "content/public/browser/tracing_service.h"
Jay Civelli3ae893e2018-04-17 20:07:0523#include "services/service_manager/sandbox/switches.h"
tapted@chromium.org20ff1bc2014-07-19 06:47:5224
25#if defined(USE_AURA)
sadrula15cfccb2016-08-08 18:04:0726#include "base/run_loop.h"
Lily Houghton7f7b2fb2018-05-01 21:40:4627#include "content/public/browser/browser_context.h"
sadrul75f7dde2016-08-22 23:37:2428#include "content/public/browser/browser_thread.h"
Scott Violet703b8242019-06-11 19:34:3629#include "services/viz/public/cpp/gpu/gpu.h" // nogncheck
sadrul943e3b32016-08-04 18:22:5930#include "ui/display/screen.h"
sadrul943e3b32016-08-04 18:22:5931#include "ui/views/widget/desktop_aura/desktop_screen.h"
32#include "ui/wm/core/wm_state.h"
33#endif // defined(USE_AURA)
sadruld2861a3e2016-06-07 08:05:1434
tonikitooda97a9f2016-11-02 20:43:3935#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
cernekee7bc96062017-04-11 04:18:2336#include <sys/stat.h>
37#include <sys/types.h>
38#include <unistd.h>
39
tonikitooda97a9f2016-11-02 20:43:3940#include "chrome/browser/ui/simple_message_box.h"
tonikitooda97a9f2016-11-02 20:43:3941#include "chrome/grit/chromium_strings.h"
42#include "chrome/grit/generated_resources.h"
thomasanderson357c17552017-01-11 23:55:4643#include "content/public/common/content_switches.h"
tonikitooda97a9f2016-11-02 20:43:3944#include "ui/base/l10n/l10n_util.h"
45#endif // defined(OS_LINUX) && !defined(OS_CHROMEOS)
46
Andrew Lee197daae62019-08-22 21:51:4347// This connector is used in ui_devtools's TracingAgent to hook up with the
48// tracing service.
49class UiDevtoolsConnector : public ui_devtools::ConnectorDelegate {
50 public:
51 UiDevtoolsConnector() {}
52 ~UiDevtoolsConnector() override = default;
53
54 void BindTracingConsumerHost(
55 mojo::PendingReceiver<tracing::mojom::ConsumerHost> receiver) override {
Ken Rockotcbead332019-11-21 04:35:4756 content::GetTracingService().BindConsumerHost(std::move(receiver));
Andrew Lee197daae62019-08-22 21:51:4357 }
58};
59
Mike Wasserman8f8b0852018-12-03 22:08:1060ChromeBrowserMainExtraPartsViews::ChromeBrowserMainExtraPartsViews() {}
stevenjb@google.comc7480942011-11-08 19:18:2761
sky@chromium.orgef663fa2013-12-20 17:55:0062ChromeBrowserMainExtraPartsViews::~ChromeBrowserMainExtraPartsViews() {
oshimadd3db6a2014-11-10 22:21:2363 constrained_window::SetConstrainedWindowViewsClient(nullptr);
sky@chromium.orgef663fa2013-12-20 17:55:0064}
65
stevenjb@google.com50462bf02011-11-21 19:13:3166void ChromeBrowserMainExtraPartsViews::ToolkitInitialized() {
stevenjb@google.comc7480942011-11-08 19:18:2767 // The delegate needs to be set before any UI is created so that windows
68 // display the correct icon.
mohsen0ff8c0e2015-06-10 06:49:0769 if (!views::ViewsDelegate::GetInstance())
Jinho Bangcc280792018-01-17 23:33:5570 views_delegate_ = std::make_unique<ChromeViewsDelegate>();
kylixrd4e8cac42017-04-13 17:15:5671
oshima136691a2014-10-24 21:54:1172 SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
73
tapted@chromium.org20ff1bc2014-07-19 06:47:5274#if defined(USE_AURA)
Peter Boström7d9604152019-08-23 01:18:4575 wm_state_ = std::make_unique<wm::WMState>();
tapted@chromium.org20ff1bc2014-07-19 06:47:5276#endif
Tom Anderson226435a72018-10-08 23:48:1977
78 // TODO(pkasting): Try to move ViewsDelegate creation here as well;
79 // see https://crbug.com/691894#c1
80 if (!views::LayoutProvider::Get())
81 layout_provider_ = ChromeLayoutProvider::CreateLayoutProvider();
stevenjb@google.comc7480942011-11-08 19:18:2782}
estaded3aa0e82016-02-17 22:55:2183
84void ChromeBrowserMainExtraPartsViews::PreCreateThreads() {
Scott Violet61b65cd2019-05-13 18:21:2785#if defined(USE_AURA) && !defined(OS_CHROMEOS)
Maksim Sisovd1a1ebb2018-05-09 11:29:3886 views::InstallDesktopScreenIfNecessary();
estaded3aa0e82016-02-17 22:55:2187#endif
88}
sadruld2861a3e2016-06-07 08:05:1489
tonikitooda97a9f2016-11-02 20:43:3990void ChromeBrowserMainExtraPartsViews::PreProfileInit() {
Mike Wasserman8f8b0852018-12-03 22:08:1091 if (ui_devtools::UiDevToolsServer::IsUiDevToolsEnabled(
92 ui_devtools::switches::kEnableUiDevTools)) {
Mike Wasserman209557db2019-05-29 18:15:0793 // Starts the UI Devtools server for browser UI (and Ash UI on Chrome OS).
Andrew Lee197daae62019-08-22 21:51:4394 auto connector = std::make_unique<UiDevtoolsConnector>();
Mike Wasserman8f8b0852018-12-03 22:08:1095 devtools_server_ = ui_devtools::CreateUiDevToolsServerForViews(
Andrew Lee197daae62019-08-22 21:51:4396 g_browser_process->system_network_context_manager()->GetContext(),
97 std::move(connector));
Mitsuru Oshima4de59ac2019-09-03 20:49:0998 devtools_process_observer_ = std::make_unique<DevtoolsProcessObserver>(
99 devtools_server_->tracing_agent());
thanhpha37a8872017-06-22 20:32:13100 }
tonikitoo6d4c4c12016-11-30 08:21:05101
tonikitooda97a9f2016-11-02 20:43:39102#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
103 // On the Linux desktop, we want to prevent the user from logging in as root,
104 // so that we don't destroy the profile. Now that we have some minimal ui
105 // initialized, check to see if we're running as root and bail if we are.
thomasanderson357c17552017-01-11 23:55:46106 if (geteuid() != 0)
tonikitooda97a9f2016-11-02 20:43:39107 return;
108
cernekee7bc96062017-04-11 04:18:23109 // Allow running inside an unprivileged user namespace. In that case, the
110 // root directory will be owned by an unmapped UID and GID (although this
111 // may not be the case if a chroot is also being used).
112 struct stat st;
113 if (stat("/", &st) == 0 && st.st_uid != 0)
114 return;
115
tonikitooda97a9f2016-11-02 20:43:39116 const base::CommandLine& command_line =
117 *base::CommandLine::ForCurrentProcess();
Jay Civelli3ae893e2018-04-17 20:07:05118 if (command_line.HasSwitch(service_manager::switches::kNoSandbox))
tonikitooda97a9f2016-11-02 20:43:39119 return;
120
121 base::string16 title = l10n_util::GetStringFUTF16(
122 IDS_REFUSE_TO_RUN_AS_ROOT, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
123 base::string16 message = l10n_util::GetStringFUTF16(
124 IDS_REFUSE_TO_RUN_AS_ROOT_2, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
125
126 chrome::ShowWarningMessageBox(NULL, title, message);
127
128 // Avoids gpu_process_transport_factory.cc(153)] Check failed:
129 // per_compositor_data_.empty() when quit is chosen.
130 base::RunLoop().RunUntilIdle();
131
132 exit(EXIT_FAILURE);
tonikitoo6d4c4c12016-11-30 08:21:05133#endif // defined(OS_LINUX) && !defined(OS_CHROMEOS)
tonikitooda97a9f2016-11-02 20:43:39134}
135
Greg Thompson2e11e2b2018-02-08 12:58:17136void ChromeBrowserMainExtraPartsViews::PostBrowserStart() {
Greg Thompson2e11e2b2018-02-08 12:58:17137 relaunch_notification_controller_ =
138 std::make_unique<RelaunchNotificationController>(
139 UpgradeDetector::GetInstance());
Greg Thompson2e11e2b2018-02-08 12:58:17140}
141
142void ChromeBrowserMainExtraPartsViews::PostMainMessageLoopRun() {
Greg Thompson2e11e2b2018-02-08 12:58:17143 // The relaunch notification controller acts on timer-based events. Tear it
144 // down explicitly here to avoid a case where such an event arrives during
145 // shutdown.
146 relaunch_notification_controller_.reset();
Greg Thompson2e11e2b2018-02-08 12:58:17147}