File: browser_desktop_window_tree_host_linux.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (439 lines) | stat: -rw-r--r-- 16,155 bytes parent folder | download | duplicates (3)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2019 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/frame/browser_desktop_window_tree_host_linux.h"

#include <optional>
#include <utility>

#include "base/no_destructor.h"
#include "build/build_config.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_frame.h"
#include "chrome/browser/ui/views/frame/browser_frame_view_layout_linux.h"
#include "chrome/browser/ui/views/frame/browser_frame_view_linux.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/desktop_browser_frame_aura_linux.h"
#include "chrome/browser/ui/views/frame/picture_in_picture_browser_frame_view.h"
#include "chrome/browser/ui/views/tabs/dragging/tab_drag_controller.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/linux/linux_ui.h"
#include "ui/native_theme/native_theme.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/platform_window/extensions/wayland_extension.h"
#include "ui/platform_window/extensions/x11_extension.h"
#include "ui/platform_window/platform_window_init_properties.h"

namespace {

#if BUILDFLAG(USE_DBUS)
bool ShouldCreateGlobalMenuBar() {
  return ui::OzonePlatform::GetInstance()
      ->GetPlatformRuntimeProperties()
      .supports_global_application_menus;
}
#endif

std::unordered_set<std::string>& SentStartupIds() {
  static base::NoDestructor<std::unordered_set<std::string>> sent_startup_ids;
  return *sent_startup_ids;
}

// Returns the event source for the active tab drag session.
std::optional<ui::mojom::DragEventSource> GetCurrentTabDragEventSource() {
  if (auto* source_context = TabDragController::GetSourceContext()) {
    if (auto* drag_controller = source_context->GetDragController()) {
      return drag_controller->event_source();
    }
  }
  return std::nullopt;
}

bool IsShowingFrame(bool use_custom_frame,
                    ui::PlatformWindowState window_state) {
  return use_custom_frame &&
         window_state != ui::PlatformWindowState::kMaximized &&
         window_state != ui::PlatformWindowState::kMinimized &&
         window_state != ui::PlatformWindowState::kFullScreen;
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostLinux, public:

BrowserDesktopWindowTreeHostLinux::BrowserDesktopWindowTreeHostLinux(
    views::internal::NativeWidgetDelegate* native_widget_delegate,
    views::DesktopNativeWidgetAura* desktop_native_widget_aura,
    BrowserView* browser_view,
    BrowserFrame* browser_frame)
    : DesktopWindowTreeHostLinux(native_widget_delegate,
                                 desktop_native_widget_aura),
      browser_view_(browser_view),
      browser_frame_(browser_frame) {
  native_frame_ = static_cast<DesktopBrowserFrameAuraLinux*>(
      browser_frame->native_browser_frame());
  native_frame_->set_host(this);

  browser_frame->set_frame_type(browser_frame->UseCustomFrame()
                                    ? views::Widget::FrameType::kForceCustom
                                    : views::Widget::FrameType::kForceNative);

  theme_observation_.Observe(ui::NativeTheme::GetInstanceForNativeUi());
  if (auto* linux_ui = ui::LinuxUi::instance()) {
    scale_observation_.Observe(linux_ui);
  }
}

BrowserDesktopWindowTreeHostLinux::~BrowserDesktopWindowTreeHostLinux() {
  native_frame_->set_host(nullptr);
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostLinux,
//     BrowserDesktopWindowTreeHost implementation:

void BrowserDesktopWindowTreeHostLinux::AddAdditionalInitProperties(
    const views::Widget::InitParams& params,
    ui::PlatformWindowInitProperties* properties) {
  views::DesktopWindowTreeHostLinux::AddAdditionalInitProperties(params,
                                                                 properties);
  auto* profile = browser_view_->browser()->profile();
  const auto* linux_ui_theme = ui::LinuxUiTheme::GetForProfile(profile);
  properties->prefer_dark_theme =
      linux_ui_theme && linux_ui_theme->PreferDarkTheme();
}

views::DesktopWindowTreeHost*
BrowserDesktopWindowTreeHostLinux::AsDesktopWindowTreeHost() {
  return this;
}

int BrowserDesktopWindowTreeHostLinux::GetMinimizeButtonOffset() const {
  return 0;
}

bool BrowserDesktopWindowTreeHostLinux::UsesNativeSystemMenu() const {
  return false;
}

void BrowserDesktopWindowTreeHostLinux::FrameTypeChanged() {
  DesktopWindowTreeHostPlatform::FrameTypeChanged();
  UpdateFrameHints();
}

bool BrowserDesktopWindowTreeHostLinux::SupportsMouseLock() {
  auto* wayland_extension = ui::GetWaylandToplevelExtension(*platform_window());
  if (!wayland_extension) {
    return false;
  }

  return wayland_extension->SupportsPointerLock();
}

void BrowserDesktopWindowTreeHostLinux::LockMouse(aura::Window* window) {
  DesktopWindowTreeHostLinux::LockMouse(window);

  if (SupportsMouseLock()) {
    auto* wayland_extension =
        ui::GetWaylandToplevelExtension(*platform_window());
    wayland_extension->LockPointer(true /*enabled*/);
  }
}

void BrowserDesktopWindowTreeHostLinux::UnlockMouse(aura::Window* window) {
  DesktopWindowTreeHostLinux::UnlockMouse(window);

  if (SupportsMouseLock()) {
    auto* wayland_extension =
        ui::GetWaylandToplevelExtension(*platform_window());
    wayland_extension->LockPointer(false /*enabled*/);
  }
}

void BrowserDesktopWindowTreeHostLinux::TabDraggingKindChanged(
    TabDragKind tab_drag_kind) {
  CHECK(browser_frame_);
  CHECK(browser_view_);
  // If there's no tabs left, the browser window is about to close, so don't
  // call SetOverrideRedirect() to prevent the window from flashing.
  if (!browser_view_->tabstrip()->GetModelCount()) {
    return;
  }

  auto* x11_extension = GetX11Extension();
  if (x11_extension && x11_extension->IsWmTiling() &&
      x11_extension->CanResetOverrideRedirect()) {
    bool was_dragging_window =
        browser_frame_->tab_drag_kind() == TabDragKind::kAllTabs;
    bool is_dragging_window = tab_drag_kind == TabDragKind::kAllTabs;
    if (is_dragging_window != was_dragging_window) {
      x11_extension->SetOverrideRedirect(is_dragging_window);
    }
  }

  if (auto* wayland_extension =
          ui::GetWaylandToplevelExtension(*platform_window())) {
    if (tab_drag_kind != TabDragKind::kNone) {
      if (auto event_source = GetCurrentTabDragEventSource()) {
        const auto allow_system_drag = base::FeatureList::IsEnabled(
            features::kAllowWindowDragUsingSystemDragDrop);
        wayland_extension->StartWindowDraggingSessionIfNeeded(
            *event_source, allow_system_drag);
      }
    }
  }
}

bool BrowserDesktopWindowTreeHostLinux::SupportsClientFrameShadow() const {
  return platform_window()->CanSetDecorationInsets() &&
         views::Widget::IsWindowCompositingSupported();
}

void BrowserDesktopWindowTreeHostLinux::UpdateFrameHints() {
  if (!browser_frame_) {
    return;
  }

  auto* window = platform_window();
  auto window_state = window->GetPlatformWindowState();
  float scale = device_scale_factor();
  auto* view =
      static_cast<BrowserNonClientFrameView*>(browser_frame_->GetFrameView());
  const gfx::Size widget_size =
      view->GetWidget()->GetWindowBoundsInScreen().size();

  if (SupportsClientFrameShadow()) {
    auto insets = CalculateInsetsInDIP(window_state);
    if (insets.IsEmpty()) {
      window->SetInputRegion(std::nullopt);
    } else {
      gfx::Rect input_bounds(widget_size);
      input_bounds.Inset(insets - view->GetInputInsets());
      input_bounds = gfx::ScaleToEnclosingRect(input_bounds, scale);
      window->SetInputRegion(
          std::optional<std::vector<gfx::Rect>>({input_bounds}));
    }
  }

  if (ui::OzonePlatform::GetInstance()->IsWindowCompositingSupported()) {
    // Set the opaque region.
    std::vector<gfx::Rect> opaque_region;
    if (IsShowingFrame(browser_frame_->native_browser_frame()->UseCustomFrame(),
                       window_state)) {
      // The opaque region is a list of rectangles that contain only fully
      // opaque pixels of the window.  We need to convert the clipping
      // rounded-rect into this format.
      SkRRect rrect = view->GetRestoredClipRegion();
      gfx::RectF rectf = gfx::SkRectToRectF(rrect.rect());
      rectf.Scale(scale);
      // It is acceptable to omit some pixels that are opaque, but the region
      // must not include any translucent pixels.  Therefore, we must
      // conservatively scale to the enclosed rectangle.
      gfx::Rect rect = gfx::ToEnclosedRect(rectf);

      // Create the initial region from the clipping rectangle without rounded
      // corners.
      SkRegion region(gfx::RectToSkIRect(rect));

      // Now subtract out the small rectangles that cover the corners.
      struct {
        SkRRect::Corner corner;
        bool left;
        bool upper;
      } kCorners[] = {
          {SkRRect::kUpperLeft_Corner, true, true},
          {SkRRect::kUpperRight_Corner, false, true},
          {SkRRect::kLowerLeft_Corner, true, false},
          {SkRRect::kLowerRight_Corner, false, false},
      };
      for (const auto& corner : kCorners) {
        auto radii = rrect.radii(corner.corner);
        auto rx = std::ceil(scale * radii.x());
        auto ry = std::ceil(scale * radii.y());
        auto corner_rect = SkIRect::MakeXYWH(
            corner.left ? rect.x() : rect.right() - rx,
            corner.upper ? rect.y() : rect.bottom() - ry, rx, ry);
        region.op(corner_rect, SkRegion::kDifference_Op);
      }

      auto translucent_top_area_rect = SkIRect::MakeXYWH(
          rect.x(), rect.y(), rect.width(),
          std::ceil(view->GetTranslucentTopAreaHeight() * scale - rect.y()));
      region.op(translucent_top_area_rect, SkRegion::kDifference_Op);

      // Convert the region to a list of rectangles.
      for (SkRegion::Iterator i(region); !i.done(); i.next()) {
        opaque_region.push_back(gfx::SkIRectToRect(i.rect()));
      }
    } else {
      // The entire window except for the translucent top is opaque.
      gfx::Rect opaque_region_dip(widget_size);
      gfx::Insets insets;
      insets.set_top(view->GetTranslucentTopAreaHeight());
      opaque_region_dip.Inset(insets);
      opaque_region.push_back(
          gfx::ScaleToEnclosingRect(opaque_region_dip, scale));
    }
    window->SetOpaqueRegion(opaque_region);
  }

  SizeConstraintsChanged();
}

void BrowserDesktopWindowTreeHostLinux::ClientDestroyedWidget() {
#if BUILDFLAG(USE_DBUS)
  dbus_appmenu_.reset();
#endif
  browser_frame_ = nullptr;
  browser_view_ = nullptr;
  DesktopWindowTreeHostLinux::ClientDestroyedWidget();
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostLinux,
//     DesktopWindowTreeHostLinuxImpl implementation:

void BrowserDesktopWindowTreeHostLinux::Init(
    const views::Widget::InitParams& params) {
  DesktopWindowTreeHostLinux::Init(std::move(params));

#if BUILDFLAG(USE_DBUS)
  // We have now created our backing window. We now need to (possibly)
  // alert the desktop environment that there's a menu bar attached to it.
  if (ShouldCreateGlobalMenuBar()) {
    dbus_appmenu_ = std::make_unique<DbusAppmenu>(
        browser_view_, platform_window(), GetAcceleratedWidget());
  }
#endif
}

void BrowserDesktopWindowTreeHostLinux::OnWidgetInitDone() {
  DesktopWindowTreeHostLinux::OnWidgetInitDone();

  UpdateFrameHints();
}

void BrowserDesktopWindowTreeHostLinux::CloseNow() {
#if BUILDFLAG(USE_DBUS)
  dbus_appmenu_.reset();
#endif
  DesktopWindowTreeHostLinux::CloseNow();
}

void BrowserDesktopWindowTreeHostLinux::Show(
    ui::mojom::WindowShowState show_state,
    const gfx::Rect& restore_bounds) {
  CHECK(browser_view_);
  DesktopWindowTreeHostLinux::Show(show_state, restore_bounds);

  const std::string& startup_id =
      browser_view_->browser()->create_params().startup_id;
  if (!startup_id.empty() && !SentStartupIds().count(startup_id)) {
    platform_window()->NotifyStartupComplete(startup_id);
    SentStartupIds().insert(startup_id);
  }
}

bool BrowserDesktopWindowTreeHostLinux::IsOverrideRedirect(
    const ui::X11Extension& x11_extension) const {
  return (browser_frame_ &&
          browser_frame_->tab_drag_kind() == TabDragKind::kAllTabs) &&
         x11_extension.IsWmTiling() && x11_extension.CanResetOverrideRedirect();
}

gfx::Insets BrowserDesktopWindowTreeHostLinux::CalculateInsetsInDIP(
    ui::PlatformWindowState window_state) const {
  // If we are not showing frame, the insets should be zero.
  if (!browser_frame_ ||
      !IsShowingFrame(browser_frame_->native_browser_frame()->UseCustomFrame(),
                      window_state)) {
    return gfx::Insets();
  }

  return static_cast<BrowserNonClientFrameView*>(browser_frame_->GetFrameView())
      ->RestoredMirroredFrameBorderInsets();
}

void BrowserDesktopWindowTreeHostLinux::OnWindowStateChanged(
    ui::PlatformWindowState old_state,
    ui::PlatformWindowState new_state) {
  if (!browser_view_) {
    return;
  }

  DesktopWindowTreeHostLinux::OnWindowStateChanged(old_state, new_state);

  bool fullscreen_changed = new_state == ui::PlatformWindowState::kFullScreen ||
                            old_state == ui::PlatformWindowState::kFullScreen;
  if (old_state != new_state && fullscreen_changed) {
    // If the browser view initiated this state change,
    // BrowserView::ProcessFullscreen will no-op, so this call is harmless.
    browser_view_->FullscreenStateChanging();

    // In sync fullscreen window state mode, the FullscreenStateChanged()
    // is called just after requesting to set fullscreen property
    // to the platform, so not to call here to avoid duplicated invocation.
    if (base::FeatureList::IsEnabled(features::kAsyncFullscreenWindowState)) {
      browser_view_->FullscreenStateChanged();
    }
  }

  UpdateFrameHints();
}

void BrowserDesktopWindowTreeHostLinux::OnWindowTiledStateChanged(
    ui::WindowTiledEdges new_tiled_edges) {
  if (!browser_frame_) {
    return;
  }

  bool maximized = new_tiled_edges.top && new_tiled_edges.left &&
                   new_tiled_edges.bottom && new_tiled_edges.right;
  bool tiled = new_tiled_edges.top || new_tiled_edges.left ||
               new_tiled_edges.bottom || new_tiled_edges.right;
  browser_frame_->set_tiled(tiled && !maximized);
  UpdateFrameHints();
  if (SupportsClientFrameShadow()) {
    // Trigger a re-layout as the insets will change even if the bounds don't.
    ScheduleRelayout();
    if (GetWidget()->non_client_view()) {
      // This is needed for the decorated regions, borders etc. to be repainted.
      GetWidget()->non_client_view()->SchedulePaint();
    }
  }
}

void BrowserDesktopWindowTreeHostLinux::OnNativeThemeUpdated(
    ui::NativeTheme* observed_theme) {
  UpdateFrameHints();
}

void BrowserDesktopWindowTreeHostLinux::OnDeviceScaleFactorChanged() {
  UpdateFrameHints();
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHost, public:

// static
BrowserDesktopWindowTreeHost*
BrowserDesktopWindowTreeHost::CreateBrowserDesktopWindowTreeHost(
    views::internal::NativeWidgetDelegate* native_widget_delegate,
    views::DesktopNativeWidgetAura* desktop_native_widget_aura,
    BrowserView* browser_view,
    BrowserFrame* browser_frame) {
  return new BrowserDesktopWindowTreeHostLinux(native_widget_delegate,
                                               desktop_native_widget_aura,
                                               browser_view, browser_frame);
}