tfarina@chromium.org | 66fb53c8 | 2012-05-15 20:29:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
stevenjb@google.com | c748094 | 2011-11-08 19:18:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tfarina@chromium.org | c27541f | 2012-05-24 19:25:54 | [diff] [blame] | 5 | #include "chrome/browser/ui/views/chrome_browser_main_extra_parts_views.h" |
stevenjb@google.com | c748094 | 2011-11-08 19:18:27 | [diff] [blame] | 6 | |
cernekee | 7bc9606 | 2017-04-11 04:18:23 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
James Cook | 2eed987 | 2017-11-14 21:08:54 | [diff] [blame] | 9 | #include "base/command_line.h" |
Greg Thompson | 875403e0 | 2018-02-27 16:22:52 | [diff] [blame] | 10 | #include "build/build_config.h" |
Leonard Grey | 24fe86c | 2019-04-14 21:02:56 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
| 12 | #include "chrome/browser/net/system_network_context_manager.h" |
oshima | 136691a | 2014-10-24 21:54:11 | [diff] [blame] | 13 | #include "chrome/browser/ui/views/chrome_constrained_window_views_client.h" |
Peter Boström | f01dd3f | 2018-08-15 21:28:18 | [diff] [blame] | 14 | #include "chrome/browser/ui/views/chrome_layout_provider.h" |
stevenjb@google.com | c748094 | 2011-11-08 19:18:27 | [diff] [blame] | 15 | #include "chrome/browser/ui/views/chrome_views_delegate.h" |
Mitsuru Oshima | 4de59ac | 2019-09-03 20:49:09 | [diff] [blame] | 16 | #include "chrome/browser/ui/views/devtools_process_observer.h" |
Stefan Teodorescu | 54339ac | 2018-07-24 13:24:18 | [diff] [blame] | 17 | #include "chrome/browser/ui/views/relaunch_notification/relaunch_notification_controller.h" |
oshima | 136691a | 2014-10-24 21:54:11 | [diff] [blame] | 18 | #include "components/constrained_window/constrained_window_views.h" |
Andrew Lee | 197daae6 | 2019-08-22 21:51:43 | [diff] [blame] | 19 | #include "components/ui_devtools/connector_delegate.h" |
Leonard Grey | 24fe86c | 2019-04-14 21:02:56 | [diff] [blame] | 20 | #include "components/ui_devtools/switches.h" |
| 21 | #include "components/ui_devtools/views/devtools_server_util.h" |
Ken Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 22 | #include "content/public/browser/tracing_service.h" |
Jay Civelli | 3ae893e | 2018-04-17 20:07:05 | [diff] [blame] | 23 | #include "services/service_manager/sandbox/switches.h" |
tapted@chromium.org | 20ff1bc | 2014-07-19 06:47:52 | [diff] [blame] | 24 | |
| 25 | #if defined(USE_AURA) |
sadrul | a15cfccb | 2016-08-08 18:04:07 | [diff] [blame] | 26 | #include "base/run_loop.h" |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 27 | #include "content/public/browser/browser_context.h" |
sadrul | 75f7dde | 2016-08-22 23:37:24 | [diff] [blame] | 28 | #include "content/public/browser/browser_thread.h" |
Scott Violet | 703b824 | 2019-06-11 19:34:36 | [diff] [blame] | 29 | #include "services/viz/public/cpp/gpu/gpu.h" // nogncheck |
sadrul | 943e3b3 | 2016-08-04 18:22:59 | [diff] [blame] | 30 | #include "ui/display/screen.h" |
sadrul | 943e3b3 | 2016-08-04 18:22:59 | [diff] [blame] | 31 | #include "ui/views/widget/desktop_aura/desktop_screen.h" |
| 32 | #include "ui/wm/core/wm_state.h" |
| 33 | #endif // defined(USE_AURA) |
sadrul | d2861a3e | 2016-06-07 08:05:14 | [diff] [blame] | 34 | |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 35 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
cernekee | 7bc9606 | 2017-04-11 04:18:23 | [diff] [blame] | 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 40 | #include "chrome/browser/ui/simple_message_box.h" |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 41 | #include "chrome/grit/chromium_strings.h" |
| 42 | #include "chrome/grit/generated_resources.h" |
thomasanderson | 357c1755 | 2017-01-11 23:55:46 | [diff] [blame] | 43 | #include "content/public/common/content_switches.h" |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 44 | #include "ui/base/l10n/l10n_util.h" |
| 45 | #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 46 | |
Andrew Lee | 197daae6 | 2019-08-22 21:51:43 | [diff] [blame] | 47 | // This connector is used in ui_devtools's TracingAgent to hook up with the |
| 48 | // tracing service. |
| 49 | class 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 Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 56 | content::GetTracingService().BindConsumerHost(std::move(receiver)); |
Andrew Lee | 197daae6 | 2019-08-22 21:51:43 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
Mike Wasserman | 8f8b085 | 2018-12-03 22:08:10 | [diff] [blame] | 60 | ChromeBrowserMainExtraPartsViews::ChromeBrowserMainExtraPartsViews() {} |
stevenjb@google.com | c748094 | 2011-11-08 19:18:27 | [diff] [blame] | 61 | |
sky@chromium.org | ef663fa | 2013-12-20 17:55:00 | [diff] [blame] | 62 | ChromeBrowserMainExtraPartsViews::~ChromeBrowserMainExtraPartsViews() { |
oshima | dd3db6a | 2014-11-10 22:21:23 | [diff] [blame] | 63 | constrained_window::SetConstrainedWindowViewsClient(nullptr); |
sky@chromium.org | ef663fa | 2013-12-20 17:55:00 | [diff] [blame] | 64 | } |
| 65 | |
stevenjb@google.com | 50462bf0 | 2011-11-21 19:13:31 | [diff] [blame] | 66 | void ChromeBrowserMainExtraPartsViews::ToolkitInitialized() { |
stevenjb@google.com | c748094 | 2011-11-08 19:18:27 | [diff] [blame] | 67 | // The delegate needs to be set before any UI is created so that windows |
| 68 | // display the correct icon. |
mohsen | 0ff8c0e | 2015-06-10 06:49:07 | [diff] [blame] | 69 | if (!views::ViewsDelegate::GetInstance()) |
Jinho Bang | cc28079 | 2018-01-17 23:33:55 | [diff] [blame] | 70 | views_delegate_ = std::make_unique<ChromeViewsDelegate>(); |
kylixrd | 4e8cac4 | 2017-04-13 17:15:56 | [diff] [blame] | 71 | |
oshima | 136691a | 2014-10-24 21:54:11 | [diff] [blame] | 72 | SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient()); |
| 73 | |
tapted@chromium.org | 20ff1bc | 2014-07-19 06:47:52 | [diff] [blame] | 74 | #if defined(USE_AURA) |
Peter Boström | 7d960415 | 2019-08-23 01:18:45 | [diff] [blame] | 75 | wm_state_ = std::make_unique<wm::WMState>(); |
tapted@chromium.org | 20ff1bc | 2014-07-19 06:47:52 | [diff] [blame] | 76 | #endif |
Tom Anderson | 226435a7 | 2018-10-08 23:48:19 | [diff] [blame] | 77 | |
| 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.com | c748094 | 2011-11-08 19:18:27 | [diff] [blame] | 82 | } |
estade | d3aa0e8 | 2016-02-17 22:55:21 | [diff] [blame] | 83 | |
| 84 | void ChromeBrowserMainExtraPartsViews::PreCreateThreads() { |
Scott Violet | 61b65cd | 2019-05-13 18:21:27 | [diff] [blame] | 85 | #if defined(USE_AURA) && !defined(OS_CHROMEOS) |
Maksim Sisov | d1a1ebb | 2018-05-09 11:29:38 | [diff] [blame] | 86 | views::InstallDesktopScreenIfNecessary(); |
estade | d3aa0e8 | 2016-02-17 22:55:21 | [diff] [blame] | 87 | #endif |
| 88 | } |
sadrul | d2861a3e | 2016-06-07 08:05:14 | [diff] [blame] | 89 | |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 90 | void ChromeBrowserMainExtraPartsViews::PreProfileInit() { |
Mike Wasserman | 8f8b085 | 2018-12-03 22:08:10 | [diff] [blame] | 91 | if (ui_devtools::UiDevToolsServer::IsUiDevToolsEnabled( |
| 92 | ui_devtools::switches::kEnableUiDevTools)) { |
Mike Wasserman | 209557db | 2019-05-29 18:15:07 | [diff] [blame] | 93 | // Starts the UI Devtools server for browser UI (and Ash UI on Chrome OS). |
Andrew Lee | 197daae6 | 2019-08-22 21:51:43 | [diff] [blame] | 94 | auto connector = std::make_unique<UiDevtoolsConnector>(); |
Mike Wasserman | 8f8b085 | 2018-12-03 22:08:10 | [diff] [blame] | 95 | devtools_server_ = ui_devtools::CreateUiDevToolsServerForViews( |
Andrew Lee | 197daae6 | 2019-08-22 21:51:43 | [diff] [blame] | 96 | g_browser_process->system_network_context_manager()->GetContext(), |
| 97 | std::move(connector)); |
Mitsuru Oshima | 4de59ac | 2019-09-03 20:49:09 | [diff] [blame] | 98 | devtools_process_observer_ = std::make_unique<DevtoolsProcessObserver>( |
| 99 | devtools_server_->tracing_agent()); |
thanhph | a37a887 | 2017-06-22 20:32:13 | [diff] [blame] | 100 | } |
tonikitoo | 6d4c4c1 | 2016-11-30 08:21:05 | [diff] [blame] | 101 | |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 102 | #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. |
thomasanderson | 357c1755 | 2017-01-11 23:55:46 | [diff] [blame] | 106 | if (geteuid() != 0) |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 107 | return; |
| 108 | |
cernekee | 7bc9606 | 2017-04-11 04:18:23 | [diff] [blame] | 109 | // 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 | |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 116 | const base::CommandLine& command_line = |
| 117 | *base::CommandLine::ForCurrentProcess(); |
Jay Civelli | 3ae893e | 2018-04-17 20:07:05 | [diff] [blame] | 118 | if (command_line.HasSwitch(service_manager::switches::kNoSandbox)) |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 119 | 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); |
tonikitoo | 6d4c4c1 | 2016-11-30 08:21:05 | [diff] [blame] | 133 | #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
tonikitoo | da97a9f | 2016-11-02 20:43:39 | [diff] [blame] | 134 | } |
| 135 | |
Greg Thompson | 2e11e2b | 2018-02-08 12:58:17 | [diff] [blame] | 136 | void ChromeBrowserMainExtraPartsViews::PostBrowserStart() { |
Greg Thompson | 2e11e2b | 2018-02-08 12:58:17 | [diff] [blame] | 137 | relaunch_notification_controller_ = |
| 138 | std::make_unique<RelaunchNotificationController>( |
| 139 | UpgradeDetector::GetInstance()); |
Greg Thompson | 2e11e2b | 2018-02-08 12:58:17 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void ChromeBrowserMainExtraPartsViews::PostMainMessageLoopRun() { |
Greg Thompson | 2e11e2b | 2018-02-08 12:58:17 | [diff] [blame] | 143 | // 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 Thompson | 2e11e2b | 2018-02-08 12:58:17 | [diff] [blame] | 147 | } |