File: browser_frame_ash.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (275 lines) | stat: -rw-r--r-- 10,721 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
// Copyright 2013 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_frame_ash.h"

#include <memory>

#include "ash/public/cpp/window_properties.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_state_delegate.h"
#include "ash/wm/window_util.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/views/frame/browser_frame.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chromeos/ui/base/app_types.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/base/window_state_type.h"
#include "chromeos/ui/frame/frame_utils.h"
#include "components/app_restore/app_restore_info.h"
#include "components/app_restore/app_restore_utils.h"
#include "components/app_restore/full_restore_utils.h"
#include "components/app_restore/window_properties.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/views/view.h"

namespace {

// BrowserWindowStateDelegate class handles a user's fullscreen
// request (Shift+F4/F4).
class BrowserWindowStateDelegate : public ash::WindowStateDelegate {
 public:
  explicit BrowserWindowStateDelegate(Browser* browser) : browser_(browser) {
    DCHECK(browser_);
  }

  BrowserWindowStateDelegate(const BrowserWindowStateDelegate&) = delete;
  BrowserWindowStateDelegate& operator=(const BrowserWindowStateDelegate&) =
      delete;

  ~BrowserWindowStateDelegate() override = default;

  // Overridden from ash::WindowStateDelegate.
  bool ToggleFullscreen(ash::WindowState* window_state) override {
    DCHECK(window_state->IsFullscreen() || window_state->CanFullscreen());
    if (!window_state->IsFullscreen() && !window_state->CanFullscreen()) {
      return true;
    }
    chrome::ToggleFullscreenMode(browser_, /*user_initiated=*/true);
    return true;
  }

  // Overridden from ash::WindowStateDelegate.
  void ToggleLockedFullscreen(ash::WindowState* window_state) override {
    ash::Shell::Get()->shell_delegate()->SetUpEnvironmentForLockedFullscreen(
        *window_state);
  }

 private:
  raw_ptr<Browser> browser_;  // not owned.
};

}  // namespace

///////////////////////////////////////////////////////////////////////////////
// BrowserFrameAsh, public:

BrowserFrameAsh::BrowserFrameAsh(BrowserFrame* browser_frame,
                                 BrowserView* browser_view)
    : views::NativeWidgetAura(browser_frame), browser_view_(browser_view) {
  widget_observation_.Observe(browser_frame);
  GetNativeWindow()->SetName("BrowserFrameAsh");
  Browser* browser = browser_view->browser();

  created_from_drag_ = browser_frame->tab_drag_kind() != TabDragKind::kNone;

  // Turn on auto window management if we don't need an explicit bounds.
  // This way the requested bounds are honored.
  if (!browser->bounds_overridden() && !browser->is_session_restore()) {
    SetWindowAutoManaged();
  }
}

BrowserFrameAsh::~BrowserFrameAsh() = default;

///////////////////////////////////////////////////////////////////////////////
// BrowserFrameAsh, views::NativeWidgetAura overrides:

void BrowserFrameAsh::OnWidgetInitDone() {
  Browser* browser = browser_view_->browser();
  ash::WindowState* window_state = ash::WindowState::Get(GetNativeWindow());
  window_state->SetDelegate(
      std::make_unique<BrowserWindowStateDelegate>(browser));
  // For legacy reasons v1 apps (like Secure Shell) are allowed to consume keys
  // like brightness, volume, etc. Otherwise these keys are handled by the
  // Ash window manager.
  window_state->SetCanConsumeSystemKeys(browser->is_type_app() ||
                                        browser->is_type_app_popup());

  app_restore::AppRestoreInfo::GetInstance()->OnWidgetInitialized(GetWidget());
}

void BrowserFrameAsh::OnWindowTargetVisibilityChanged(bool visible) {
  if (visible) {
    // Once the window has been shown we know the requested bounds
    // (if provided) have been honored and we can switch on window management.
    SetWindowAutoManaged();
  }
  views::NativeWidgetAura::OnWindowTargetVisibilityChanged(visible);
}

////////////////////////////////////////////////////////////////////////////////
// BrowserFrameAsh, NativeBrowserFrame implementation:

bool BrowserFrameAsh::ShouldSaveWindowPlacement() const {
  return nullptr == GetWidget()->GetNativeWindow()->GetProperty(
                        ash::kRestoreBoundsOverrideKey);
}

void BrowserFrameAsh::GetWindowPlacement(
    gfx::Rect* bounds,
    ui::mojom::WindowShowState* show_state) const {
  aura::Window* window = GetWidget()->GetNativeWindow();
  gfx::Rect* override_bounds =
      window->GetProperty(ash::kRestoreBoundsOverrideKey);
  if (override_bounds && !override_bounds->IsEmpty()) {
    *bounds = *override_bounds;
    *show_state = chromeos::ToWindowShowState(
        window->GetProperty(ash::kRestoreWindowStateTypeOverrideKey));
  } else {
    // Snapped state is a ash only state which is normally not restored except
    // when the full restore feature is turned on. `Widget::GetRestoreBounds()`
    // will not return the restore bounds for a snapped window because to
    // Widget/NativeWidgetAura the window is a normal window, so get the restore
    // bounds directly from the ash window state.
    bool used_window_state_restore_bounds = false;
    auto* window_state = ash::WindowState::Get(window);
    if (window_state->IsSnapped() && window_state->HasRestoreBounds()) {
      // Additionally, if the window is closed, and not from logging out we
      // want to use the regular restore bounds, otherwise the next time the
      // user opens a window it will be in a different place than closed,
      // since session restore does not restore ash snapped state.
      if (browser_shutdown::IsTryingToQuit() || !GetWidget()->IsClosed()) {
        used_window_state_restore_bounds = true;
        *bounds = window_state->GetRestoreBoundsInScreen();
      }
    }

    if (!used_window_state_restore_bounds) {
      *bounds = GetWidget()->GetRestoredBounds();
    }
    *show_state = window->GetProperty(aura::client::kShowStateKey);
  }

  // Session restore might be unable to correctly restore other states.
  // For the record, https://crbug.com/396272
  if (*show_state != ui::mojom::WindowShowState::kMaximized &&
      *show_state != ui::mojom::WindowShowState::kMinimized) {
    *show_state = ui::mojom::WindowShowState::kNormal;
  }
}

content::KeyboardEventProcessingResult BrowserFrameAsh::PreHandleKeyboardEvent(
    const input::NativeWebKeyboardEvent& event) {
  return content::KeyboardEventProcessingResult::NOT_HANDLED;
}

bool BrowserFrameAsh::HandleKeyboardEvent(
    const input::NativeWebKeyboardEvent& event) {
  return false;
}

views::Widget::InitParams BrowserFrameAsh::GetWidgetParams(
    views::Widget::InitParams::Ownership ownership) {
  views::Widget::InitParams params(ownership);
  params.native_widget = this;
  params.context = ash::Shell::GetPrimaryRootWindow();

  Browser* browser = browser_view_->browser();
  const int32_t restore_id = browser->create_params().restore_id;
  params.init_properties_container.SetProperty(app_restore::kWindowIdKey,
                                               browser->session_id().id());
  params.init_properties_container.SetProperty(app_restore::kRestoreWindowIdKey,
                                               restore_id);

  params.init_properties_container.SetProperty(
      app_restore::kAppTypeBrowser,
      (browser->is_type_app() || browser->is_type_app_popup()));

  params.init_properties_container.SetProperty(app_restore::kBrowserAppNameKey,
                                               browser->app_name());
  params.init_properties_container.SetProperty(
      chromeos::kShouldHaveHighlightBorderOverlay, true);

  bool is_app = browser->is_type_app() || browser->is_type_app_popup();
  web_app::AppBrowserController* controller = browser->app_controller();
  if (controller && controller->system_app()) {
    params.init_properties_container.SetProperty(chromeos::kAppTypeKey,
                                                 chromeos::AppType::SYSTEM_APP);
  } else {
    params.init_properties_container.SetProperty(
        chromeos::kAppTypeKey,
        is_app ? chromeos::AppType::CHROME_APP : chromeos::AppType::BROWSER);
  }

  app_restore::ModifyWidgetParams(restore_id, &params);
  // Override session restore bounds with Full Restore bounds if they exist.
  if (!params.bounds.IsEmpty()) {
    browser->set_override_bounds(params.bounds);
  } else {
    params.bounds = browser->create_params().initial_bounds;
  }
  params.display_id = browser->create_params().display_id;
  params.rounded_corners = chromeos::GetWindowRoundedCorners();

  return params;
}

bool BrowserFrameAsh::UseCustomFrame() const {
  return true;
}

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

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

bool BrowserFrameAsh::ShouldRestorePreviousBrowserWidgetState() const {
  CHECK(browser_view_);
  // If there is no window info from full restore, maybe use the session
  // restore.
  const int32_t restore_id =
      browser_view_->browser()->create_params().restore_id;
  // Don't restore unresizable browser apps, because they can get stuck at a
  // broken size, or the browser being dragged because it should use the
  // specified bounds.
  return !app_restore::HasWindowInfo(restore_id) &&
         browser_view_->browser()->create_params().can_resize &&
         !browser_view_->browser()->create_params().in_tab_dragging;
}

bool BrowserFrameAsh::ShouldUseInitialVisibleOnAllWorkspaces() const {
  return !created_from_drag_;
}

void BrowserFrameAsh::OnWidgetDestroyed(views::Widget* widget) {
  browser_view_ = nullptr;
  widget_observation_.Reset();
}

///////////////////////////////////////////////////////////////////////////////
// BrowserFrameAsh, private:

void BrowserFrameAsh::SetWindowAutoManaged() {
  // For browser window in Chrome OS, we should only enable the auto window
  // management logic for tabbed browser.
  CHECK(browser_view_);
  if (browser_view_->browser()->is_type_normal()) {
    GetNativeWindow()->SetProperty(ash::kWindowPositionManagedTypeKey, true);
  }
}