File: chrome_browser_main_extra_parts_views.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (219 lines) | stat: -rw-r--r-- 7,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/chrome_browser_main_extra_parts_views.h"

#include <utility>

#include "base/command_line.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_views_delegate.h"
#include "chrome/browser/ui/views/devtools_process_observer.h"
#include "chrome/browser/ui/views/media_router/media_router_dialog_controller_views.h"
#include "chrome/browser/ui/views/relaunch_notification/relaunch_notification_controller.h"
#include "chrome/common/chrome_paths.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/media_router/browser/media_router_dialog_controller.h"
#include "components/ui_devtools/connector_delegate.h"
#include "components/ui_devtools/switches.h"
#include "components/ui_devtools/views/devtools_server_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/tracing_service.h"
#include "sandbox/policy/switches.h"

#if defined(USE_AURA)
#include "base/run_loop.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "services/viz/public/cpp/gpu/gpu.h"  // nogncheck
#if !BUILDFLAG(IS_CHROMEOS)
#include "ui/display/screen.h"
#include "ui/views/widget/desktop_aura/desktop_screen.h"
#endif
#include "ui/wm/core/wm_state.h"
#endif  // defined(USE_AURA)

#if BUILDFLAG(IS_LINUX)
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "chrome/browser/ui/simple_message_box.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/common/content_switches.h"
#include "ui/base/l10n/l10n_util.h"
#endif  // BUILDFLAG(IS_LINUX)

namespace {

// Owned by ChromeBrowserMainParts.
ChromeBrowserMainExtraPartsViews* g_main_parts_views = nullptr;

}  // namespace

// This connector is used in ui_devtools's TracingAgent to hook up with the
// tracing service.
class UiDevtoolsConnector : public ui_devtools::ConnectorDelegate {
 public:
  UiDevtoolsConnector() = default;
  ~UiDevtoolsConnector() override = default;

  void BindTracingConsumerHost(
      mojo::PendingReceiver<tracing::mojom::ConsumerHost> receiver) override {
    content::GetTracingService().BindConsumerHost(std::move(receiver));
  }
};

ChromeBrowserMainExtraPartsViews::ChromeBrowserMainExtraPartsViews() {
  DCHECK(!g_main_parts_views);
  g_main_parts_views = this;
}

ChromeBrowserMainExtraPartsViews::~ChromeBrowserMainExtraPartsViews() {
  DCHECK_EQ(g_main_parts_views, this);
  g_main_parts_views = nullptr;
  constrained_window::SetConstrainedWindowViewsClient(nullptr);
}

// static
ChromeBrowserMainExtraPartsViews* ChromeBrowserMainExtraPartsViews::Get() {
  return g_main_parts_views;
}

void ChromeBrowserMainExtraPartsViews::ToolkitInitialized() {
  // The delegate needs to be set before any UI is created so that windows
  // display the correct icon.
  if (!views::ViewsDelegate::GetInstance()) {
    views_delegate_ = std::make_unique<ChromeViewsDelegate>();
  }

  // Set our raw pointer to the views delegate. This should never be nullptr.
  views_delegate_ptr_ = views::ViewsDelegate::GetInstance();
  CHECK(views_delegate_ptr_);

  SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());

#if defined(USE_AURA)
  wm_state_ = std::make_unique<wm::WMState>();
#endif

  // TODO(pkasting): Try to move ViewsDelegate creation here as well;
  // see https://crbug.com/691894#c1
  if (!views::LayoutProvider::Get()) {
    layout_provider_ = ChromeLayoutProvider::CreateLayoutProvider();
  }
}

void ChromeBrowserMainExtraPartsViews::PostCreateMainMessageLoop() {
  views_delegate_ptr_->InitializeViewsAXManager();
}

void ChromeBrowserMainExtraPartsViews::PreCreateThreads() {
#if defined(USE_AURA) && !BUILDFLAG(IS_CHROMEOS)
  // The Screen instance may already be set in tests.
  if (!display::Screen::GetScreen()) {
    screen_ = views::CreateDesktopScreen();
  }
#endif
}

void ChromeBrowserMainExtraPartsViews::PreProfileInit() {
  if (ui_devtools::UiDevToolsServer::IsUiDevToolsEnabled(
          ui_devtools::switches::kEnableUiDevTools)) {
    CreateUiDevTools();
  }

  media_router::MediaRouterDialogController::SetGetOrCreate(
      base::BindRepeating([](content::WebContents* web_contents) {
        DCHECK(web_contents);
        media_router::MediaRouterDialogController* controller = nullptr;
        // This call does nothing if the controller already exists.
        media_router::MediaRouterDialogControllerViews::CreateForWebContents(
            web_contents);
        controller =
            media_router::MediaRouterDialogControllerViews::FromWebContents(
                web_contents);
        return controller;
      }));

#if BUILDFLAG(IS_LINUX)
  // On the Linux desktop, we want to prevent the user from logging in as root,
  // so that we don't destroy the profile. Now that we have some minimal ui
  // initialized, check to see if we're running as root and bail if we are.
  if (geteuid() != 0) {
    return;
  }

  // Allow running inside an unprivileged user namespace. In that case, the
  // root directory will be owned by an unmapped UID and GID (although this
  // may not be the case if a chroot is also being used).
  struct stat st;
  if (stat("/", &st) == 0 && st.st_uid != 0) {
    return;
  }

  const base::CommandLine& command_line =
      *base::CommandLine::ForCurrentProcess();
  if (command_line.HasSwitch(sandbox::policy::switches::kNoSandbox)) {
    return;
  }

  std::u16string title = l10n_util::GetStringFUTF16(
      IDS_REFUSE_TO_RUN_AS_ROOT, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
  std::u16string message = l10n_util::GetStringFUTF16(
      IDS_REFUSE_TO_RUN_AS_ROOT_2, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));

  chrome::ShowWarningMessageBox(nullptr, title, message);

  // Avoids gpu_process_transport_factory.cc(153)] Check failed:
  // per_compositor_data_.empty() when quit is chosen.
  base::RunLoop().RunUntilIdle();

  exit(EXIT_FAILURE);
#endif  // BUILDFLAG(IS_LINUX)
}

void ChromeBrowserMainExtraPartsViews::PostBrowserStart() {
  relaunch_notification_controller_ =
      std::make_unique<RelaunchNotificationController>(
          UpgradeDetector::GetInstance());
}

void ChromeBrowserMainExtraPartsViews::PostMainMessageLoopRun() {
  // The relaunch notification controller acts on timer-based events. Tear it
  // down explicitly here to avoid a case where such an event arrives during
  // shutdown.
  relaunch_notification_controller_.reset();
}

void ChromeBrowserMainExtraPartsViews::CreateUiDevTools() {
  DCHECK(!devtools_server_);
  DCHECK(!devtools_process_observer_);

  // Starts the UI Devtools server for browser UI (and Ash UI on Chrome OS).
  auto connector = std::make_unique<UiDevtoolsConnector>();
  base::FilePath output_dir;
  bool result = base::PathService::Get(chrome::DIR_USER_DATA, &output_dir);
  DCHECK(result);
  devtools_server_ = ui_devtools::CreateUiDevToolsServerForViews(
      content::GetIOThreadTaskRunner(), std::move(connector), output_dir);
  devtools_process_observer_ = std::make_unique<DevtoolsProcessObserver>(
      devtools_server_->tracing_agent());
}

const ui_devtools::UiDevToolsServer*
ChromeBrowserMainExtraPartsViews::GetUiDevToolsServerInstance() {
  return devtools_server_.get();
}

void ChromeBrowserMainExtraPartsViews::DestroyUiDevTools() {
  devtools_process_observer_.reset();
  devtools_server_.reset();
}