File: browser_desktop_window_tree_host_win.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 (659 lines) | stat: -rw-r--r-- 24,968 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// 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/frame/browser_desktop_window_tree_host_win.h"

#include <windows.h>

#include <memory>
#include <utility>

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted_delete_on_sequence.h"
#include "base/process/process_handle.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/win/windows_version.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.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_view.h"
#include "chrome/browser/ui/views/frame/browser_window_property_manager_win.h"
#include "chrome/browser/ui/views/frame/system_menu_insertion_delegate_win.h"
#include "chrome/browser/ui/views/frame/tab_strip_region_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/win/app_icon.h"
#include "chrome/browser/win/titlebar_config.h"
#include "chrome/common/chrome_constants.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/base/theme_provider.h"
#include "ui/base/win/hwnd_metrics.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/image/image_family.h"
#include "ui/views/controls/menu/native_menu_win.h"

class VirtualDesktopHelper
    : public base::RefCountedDeleteOnSequence<VirtualDesktopHelper> {
 public:
  using WorkspaceChangedCallback = base::OnceCallback<void()>;

  explicit VirtualDesktopHelper(const std::string& initial_workspace);
  VirtualDesktopHelper(const VirtualDesktopHelper&) = delete;
  VirtualDesktopHelper& operator=(const VirtualDesktopHelper&) = delete;

  // public methods are all called on the UI thread.
  void Init(HWND hwnd);

  std::string GetWorkspace();

  // |callback| is called when the task to get the desktop id of |hwnd|
  // completes, if the workspace has changed.
  void UpdateWindowDesktopId(HWND hwnd, WorkspaceChangedCallback callback);

  bool GetInitialWorkspaceRemembered() const;

  void SetInitialWorkspaceRemembered(bool remembered);

 private:
  friend class base::RefCountedDeleteOnSequence<VirtualDesktopHelper>;
  friend class base::DeleteHelper<VirtualDesktopHelper>;

  ~VirtualDesktopHelper();

  // Called on the UI thread as a task reply.
  void SetWorkspace(WorkspaceChangedCallback callback,
                    const std::string& workspace);

  void InitImpl(HWND hwnd, const std::string& initial_workspace);

  static std::string GetWindowDesktopIdImpl(
      HWND hwnd,
      Microsoft::WRL::ComPtr<IVirtualDesktopManager> virtual_desktop_manager);

  // All member variables, except where noted, are only accessed on the ui
  // thead.

  // Workspace browser window was opened on. This is used to tell the
  // BrowserWindowState about the initial workspace, which has to happen after
  // |this| is fully set up.
  const std::string initial_workspace_;

  // On Windows10, this is the virtual desktop the browser window was on,
  // last we checked. This is used to tell if the window has moved to a
  // different desktop, and notify listeners. It will only be set if
  // we created |virtual_desktop_helper_|.
  std::optional<std::string> workspace_;

  bool initial_workspace_remembered_ = false;

  // Only set on Windows 10. This is created and accessed on a separate
  // COMSTAT thread. It will be null if creation failed.
  Microsoft::WRL::ComPtr<IVirtualDesktopManager> virtual_desktop_manager_;

  base::WeakPtrFactory<VirtualDesktopHelper> weak_factory_{this};
};

VirtualDesktopHelper::VirtualDesktopHelper(const std::string& initial_workspace)
    : base::RefCountedDeleteOnSequence<VirtualDesktopHelper>(
          base::ThreadPool::CreateCOMSTATaskRunner(
              {base::MayBlock(),
               base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})),
      initial_workspace_(initial_workspace) {}

void VirtualDesktopHelper::Init(HWND hwnd) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  owning_task_runner()->PostTask(
      FROM_HERE, base::BindOnce(&VirtualDesktopHelper::InitImpl, this, hwnd,
                                initial_workspace_));
}

VirtualDesktopHelper::~VirtualDesktopHelper() = default;

std::string VirtualDesktopHelper::GetWorkspace() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  if (!workspace_.has_value()) {
    workspace_ = initial_workspace_;
  }

  return workspace_.value_or(std::string());
}

void VirtualDesktopHelper::UpdateWindowDesktopId(
    HWND hwnd,
    WorkspaceChangedCallback callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  owning_task_runner()->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&VirtualDesktopHelper::GetWindowDesktopIdImpl, hwnd,
                     virtual_desktop_manager_),
      base::BindOnce(&VirtualDesktopHelper::SetWorkspace, this,
                     std::move(callback)));
}

bool VirtualDesktopHelper::GetInitialWorkspaceRemembered() const {
  return initial_workspace_remembered_;
}

void VirtualDesktopHelper::SetInitialWorkspaceRemembered(bool remembered) {
  initial_workspace_remembered_ = remembered;
}

void VirtualDesktopHelper::SetWorkspace(WorkspaceChangedCallback callback,
                                        const std::string& workspace) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  // If GetWindowDesktopId() fails, |workspace| will be empty, and it's most
  // likely that the current value of |workspace_| is still correct, so don't
  // overwrite it.
  if (workspace.empty()) {
    return;
  }

  bool workspace_changed = workspace != workspace_.value_or(std::string());
  workspace_ = workspace;
  if (workspace_changed) {
    std::move(callback).Run();
  }
}

void VirtualDesktopHelper::InitImpl(HWND hwnd,
                                    const std::string& initial_workspace) {
  DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  // Virtual Desktops on Windows are best-effort and may not always be
  // available.
  if (FAILED(::CoCreateInstance(__uuidof(::VirtualDesktopManager), nullptr,
                                CLSCTX_ALL,
                                IID_PPV_ARGS(&virtual_desktop_manager_))) ||
      initial_workspace.empty()) {
    return;
  }
  GUID guid = GUID_NULL;
  HRESULT hr =
      CLSIDFromString(base::UTF8ToWide(initial_workspace).c_str(), &guid);
  if (SUCCEEDED(hr)) {
    // There are valid reasons MoveWindowToDesktop can fail, e.g.,
    // the desktop was deleted. If it fails, the window will open on the
    // current desktop.
    virtual_desktop_manager_->MoveWindowToDesktop(hwnd, guid);
  }
}

// static
std::string VirtualDesktopHelper::GetWindowDesktopIdImpl(
    HWND hwnd,
    Microsoft::WRL::ComPtr<IVirtualDesktopManager> virtual_desktop_manager) {
  DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  if (!virtual_desktop_manager) {
    return std::string();
  }

  GUID workspace_guid;
  HRESULT hr =
      virtual_desktop_manager->GetWindowDesktopId(hwnd, &workspace_guid);
  if (FAILED(hr) || workspace_guid == GUID_NULL) {
    return std::string();
  }

  LPOLESTR workspace_widestr;
  StringFromCLSID(workspace_guid, &workspace_widestr);
  std::string workspace_id = base::WideToUTF8(workspace_widestr);
  CoTaskMemFree(workspace_widestr);
  return workspace_id;
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, public:

BrowserDesktopWindowTreeHostWin::BrowserDesktopWindowTreeHostWin(
    views::internal::NativeWidgetDelegate* native_widget_delegate,
    views::DesktopNativeWidgetAura* desktop_native_widget_aura,
    BrowserView* browser_view,
    BrowserFrame* browser_frame)
    : DesktopWindowTreeHostWin(native_widget_delegate,
                               desktop_native_widget_aura),
      browser_view_(browser_view),
      browser_frame_(browser_frame),
      virtual_desktop_helper_(nullptr) {
  profile_observation_.Observe(
      &g_browser_process->profile_manager()->GetProfileAttributesStorage());

  // TODO(crbug.com/40118412) Make turning off this policy turn off
  // native window occlusion on this browser win.
  if (!g_browser_process->local_state()->GetBoolean(
          policy::policy_prefs::kNativeWindowOcclusionEnabled)) {
    SetNativeWindowOcclusionEnabled(false);
  }
}

BrowserDesktopWindowTreeHostWin::~BrowserDesktopWindowTreeHostWin() = default;

views::NativeMenuWin* BrowserDesktopWindowTreeHostWin::GetSystemMenu() {
  if (!system_menu_.get()) {
    CHECK(browser_frame_);
    SystemMenuInsertionDelegateWin insertion_delegate;
    system_menu_ = std::make_unique<views::NativeMenuWin>(
        browser_frame_->GetSystemMenuModel(), GetHWND());
    system_menu_->Rebuild(&insertion_delegate);
  }
  return system_menu_.get();
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, BrowserDesktopWindowTreeHost implementation:

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

int BrowserDesktopWindowTreeHostWin::GetMinimizeButtonOffset() const {
  return minimize_button_metrics_.GetMinimizeButtonOffsetX();
}

bool BrowserDesktopWindowTreeHostWin::UsesNativeSystemMenu() const {
  return true;
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, views::DesktopWindowTreeHostWin overrides:

void BrowserDesktopWindowTreeHostWin::Init(
    const views::Widget::InitParams& params) {
  DesktopWindowTreeHostWin::Init(params);
  virtual_desktop_helper_ = new VirtualDesktopHelper(params.workspace);
  virtual_desktop_helper_->Init(GetHWND());
}

void BrowserDesktopWindowTreeHostWin::Show(
    ui::mojom::WindowShowState show_state,
    const gfx::Rect& restore_bounds) {
  // This will make BrowserWindowState remember the initial workspace.
  // It has to be called after DesktopNativeWidgetAura is observing the host
  // and the session service is tracking the window.
  if (virtual_desktop_helper_ &&
      !virtual_desktop_helper_->GetInitialWorkspaceRemembered()) {
    // If |virtual_desktop_helper_| has an empty workspace, kick off an update,
    // which will eventually call OnHostWorkspaceChanged.
    if (virtual_desktop_helper_->GetWorkspace().empty()) {
      UpdateWorkspace();
    } else {
      OnHostWorkspaceChanged();
    }
  }
  DesktopWindowTreeHostWin::Show(show_state, restore_bounds);
}

void BrowserDesktopWindowTreeHostWin::HandleWindowMinimizedOrRestored(
    bool restored) {
  DesktopWindowTreeHostWin::HandleWindowMinimizedOrRestored(restored);

  if (browser_view_) {
    browser_view_->UpdateLoadingAnimations(restored);
  }
}

std::string BrowserDesktopWindowTreeHostWin::GetWorkspace() const {
  return virtual_desktop_helper_ ? virtual_desktop_helper_->GetWorkspace()
                                 : std::string();
}

int BrowserDesktopWindowTreeHostWin::GetInitialShowState() const {
  STARTUPINFO si = {0};
  si.cb = sizeof(si);
  si.dwFlags = STARTF_USESHOWWINDOW;
  GetStartupInfo(&si);
  return si.wShowWindow;
}

bool BrowserDesktopWindowTreeHostWin::GetClientAreaInsets(
    gfx::Insets* insets,
    int frame_thickness) const {
  // Always use default insets for opaque frame.
  if (!browser_view_ || !ShouldUseNativeFrame()) {
    return false;
  }

  // Use default insets for popups and apps, unless we are custom drawing the
  // titlebar.
  if (!ShouldBrowserCustomDrawTitlebar(browser_view_) &&
      !browser_view_->GetIsNormalType()) {
    return false;
  }

  if (GetWidget()->IsFullscreen()) {
    // In fullscreen mode there is no frame.
    *insets = gfx::Insets();
  } else {
    // Reduce the non-client border size; UpdateDWMFrame() will instead extend
    // the border into the window client area. For maximized windows, Windows
    // outdents the window rect from the screen's client rect by
    // |frame_thickness| on each edge, meaning |insets| must contain
    // |frame_thickness| on all sides (including the top) to avoid the client
    // area extending onto adjacent monitors. For non-maximized windows,
    // however, the top inset must be zero, since if there is any nonclient
    // area, Windows will draw a full native titlebar outside the client area.
    // (This doesn't occur in the maximized case.)
    int top_thickness = 0;
    if (ShouldBrowserCustomDrawTitlebar(browser_view_) &&
        GetWidget()->IsMaximized()) {
      top_thickness = frame_thickness;
    }
    *insets = gfx::Insets::TLBR(top_thickness, frame_thickness, frame_thickness,
                                frame_thickness);
  }
  return true;
}

bool BrowserDesktopWindowTreeHostWin::GetDwmFrameInsetsInPixels(
    gfx::Insets* insets) const {
  // For "normal" windows on Aero, we always need to reset the glass area
  // correctly, even if we're not currently showing the native frame (e.g.
  // because a theme is showing), so we explicitly check for that case rather
  // than checking ShouldUseNativeFrame() here.  Using that here would mean we
  // wouldn't reset the glass area to zero when moving from the native frame to
  // an opaque frame, leading to graphical glitches behind the opaque frame.
  // Instead, we use that function below to tell us whether the frame is
  // currently native or opaque.
  if (!browser_view_ || !browser_frame_ || !GetWidget()->client_view() ||
      !browser_view_->GetIsNormalType() ||
      !DesktopWindowTreeHostWin::ShouldUseNativeFrame()) {
    return false;
  }

  // Don't extend the glass in at all if it won't be visible.
  if (!ShouldUseNativeFrame() || GetWidget()->IsFullscreen() ||
      ShouldBrowserCustomDrawTitlebar(browser_view_)) {
    *insets = gfx::Insets();
  } else {
    // The glass should extend to the bottom of the tabstrip.
    gfx::Rect tabstrip_region_bounds(browser_frame_->GetBoundsForTabStripRegion(
        browser_view_->tab_strip_region_view()->GetMinimumSize()));
    tabstrip_region_bounds = display::win::GetScreenWin()->DIPToClientRect(
        GetHWND(), tabstrip_region_bounds);

    *insets = gfx::Insets::TLBR(tabstrip_region_bounds.bottom(), 0, 0, 0);
  }
  return true;
}

void BrowserDesktopWindowTreeHostWin::HandleCreate() {
  if (!browser_view_) {
    return;
  }

  DesktopWindowTreeHostWin::HandleCreate();
  browser_window_property_manager_ =
      BrowserWindowPropertyManager::CreateBrowserWindowPropertyManager(
          browser_view_, GetHWND());

  // Use the profile icon as the browser window icon, if there is more
  // than one profile. This makes alt-tab preview tabs show the profile-specific
  // icon in the multi-profile case.
  if (g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetNumberOfProfiles() > 1) {
    SetWindowIcon(/*badged=*/true);
  }
}

void BrowserDesktopWindowTreeHostWin::HandleDestroying() {
  browser_window_property_manager_.reset();
  DesktopWindowTreeHostWin::HandleDestroying();
}

void BrowserDesktopWindowTreeHostWin::HandleWindowScaleFactorChanged(
    float window_scale_factor) {
  DesktopWindowTreeHostWin::HandleWindowScaleFactorChanged(window_scale_factor);
  minimize_button_metrics_.OnDpiChanged();
}

bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
                                                   WPARAM w_param,
                                                   LPARAM l_param,
                                                   LRESULT* result) {
  switch (message) {
    case WM_ACTIVATE:
      if (LOWORD(w_param) != WA_INACTIVE) {
        minimize_button_metrics_.OnHWNDActivated();
      }
      return false;
    case WM_ENDSESSION:
      chrome::SessionEnding();
      return true;
    case WM_INITMENUPOPUP:
      GetSystemMenu()->UpdateStates();
      return true;
  }
  return DesktopWindowTreeHostWin::PreHandleMSG(message, w_param, l_param,
                                                result);
}

void BrowserDesktopWindowTreeHostWin::PostHandleMSG(UINT message,
                                                    WPARAM w_param,
                                                    LPARAM l_param) {
  if (!GetWidget()) {
    return;
  }

  switch (message) {
    case WM_SETFOCUS: {
      UpdateWorkspace();
      break;
    }
    case WM_CREATE:
      minimize_button_metrics_.Init(GetHWND());
      break;
    case WM_WINDOWPOSCHANGED: {
      // Windows lies to us about the position of the minimize button before a
      // window is visible. We use this position to place the incognito avatar
      // in RTL mode, so when the window is shown, we need to re-layout and
      // schedule a paint for the non-client frame view so that the icon top has
      // the correct position when the window becomes visible. This fixes bugs
      // where the icon appears to overlay the minimize button. Note that we
      // will call Layout every time SetWindowPos is called with SWP_SHOWWINDOW,
      // however callers typically are careful about not specifying this flag
      // unless necessary to avoid flicker. This may be invoked during creation
      // on XP and before the non_client_view has been created.
      WINDOWPOS* window_pos = reinterpret_cast<WINDOWPOS*>(l_param);
      views::NonClientView* non_client_view = GetWidget()->non_client_view();
      if (window_pos->flags & SWP_SHOWWINDOW && non_client_view) {
        non_client_view->DeprecatedLayoutImmediately();
        non_client_view->SchedulePaint();
      }
      break;
    }
    case WM_DWMCOLORIZATIONCOLORCHANGED: {
      // The activation border may have changed color.
      views::NonClientView* non_client_view = GetWidget()->non_client_view();
      if (non_client_view) {
        non_client_view->SchedulePaint();
      }
      break;
    }
  }
}

views::FrameMode BrowserDesktopWindowTreeHostWin::GetFrameMode() const {
  if (!browser_view_) {
    // If there is no browser view the frame should be system drawn.
    return views::FrameMode::SYSTEM_DRAWN;
  }

  const views::FrameMode system_frame_mode =
      ShouldBrowserCustomDrawTitlebar(browser_view_)
          ? views::FrameMode::SYSTEM_DRAWN_NO_CONTROLS
          : views::FrameMode::SYSTEM_DRAWN;

  // We don't theme popup or app windows, so regardless of whether or not a
  // theme is active for normal browser windows, we don't want to use the custom
  // frame for popups/apps.
  if (!browser_view_->GetIsNormalType() &&
      DesktopWindowTreeHostWin::GetFrameMode() ==
          views::FrameMode::SYSTEM_DRAWN) {
    return system_frame_mode;
  }

  // Otherwise, we use the native frame when we're told we should by the theme
  // provider (e.g. no custom theme is active).
  return GetWidget()->GetThemeProvider()->ShouldUseNativeFrame()
             ? system_frame_mode
             : views::FrameMode::CUSTOM_DRAWN;
}

bool BrowserDesktopWindowTreeHostWin::ShouldUseNativeFrame() const {
  if (!browser_view_ ||
      !views::DesktopWindowTreeHostWin::ShouldUseNativeFrame()) {
    return false;
  }
  // This function can get called when the Browser window is closed i.e. in the
  // context of the BrowserView destructor.
  if (!browser_view_->browser()) {
    return false;
  }

  // We don't theme popup or app windows, so regardless of whether or not a
  // theme is active for normal browser windows, we don't want to use the custom
  // frame for popups/apps.
  if (!browser_view_->GetIsNormalType()) {
    return true;
  }
  // Otherwise, we use the native frame when we're told we should by the theme
  // provider (e.g. no custom theme is active).
  return GetWidget()->GetThemeProvider()->ShouldUseNativeFrame();
}

bool BrowserDesktopWindowTreeHostWin::ShouldWindowContentsBeTransparent()
    const {
  CHECK(browser_view_);
  return !ShouldBrowserCustomDrawTitlebar(browser_view_) &&
         views::DesktopWindowTreeHostWin::ShouldWindowContentsBeTransparent();
}

void BrowserDesktopWindowTreeHostWin::ClientDestroyedWidget() {
  profile_observation_.Reset();
  system_menu_.reset();
  browser_window_property_manager_.reset();
  browser_frame_ = nullptr;
  browser_view_ = nullptr;
  DesktopWindowTreeHostWin::ClientDestroyedWidget();
}

////////////////////////////////////////////////////////////////////////////////
// ProfileAttributesStorage::Observer overrides:

void BrowserDesktopWindowTreeHostWin::OnProfileAvatarChanged(
    const base::FilePath& profile_path) {
  // If we're currently badging the window icon (>1 available profile),
  // and this window's profile's avatar changed, update the window icon.
  CHECK(browser_view_);
  if (browser_view_->browser()->profile()->GetPath() == profile_path &&
      g_browser_process->profile_manager()
              ->GetProfileAttributesStorage()
              .GetNumberOfProfiles() > 1) {
    // If we went from 1 to 2 profiles, window icons should be badged.
    SetWindowIcon(/*badged=*/true);
  }
}

void BrowserDesktopWindowTreeHostWin::OnProfileAdded(
    const base::FilePath& profile_path) {
  if (g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetNumberOfProfiles() == 2) {
    SetWindowIcon(/*badged=*/true);
  }
}

void BrowserDesktopWindowTreeHostWin::OnProfileWasRemoved(
    const base::FilePath& profile_path,
    const std::u16string& profile_name) {
  if (g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetNumberOfProfiles() == 1) {
    // If we went from 2 profiles to 1, window icons should not be badged.
    SetWindowIcon(/*badged=*/false);
  }
}

////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, private:

void BrowserDesktopWindowTreeHostWin::UpdateWorkspace() {
  if (!virtual_desktop_helper_) {
    return;
  }
  virtual_desktop_helper_->UpdateWindowDesktopId(
      GetHWND(),
      base::BindOnce(&BrowserDesktopWindowTreeHostWin::OnHostWorkspaceChanged,
                     weak_factory_.GetWeakPtr()));
}

SkBitmap GetBadgedIconBitmapForProfile(Profile* profile) {
  std::unique_ptr<gfx::ImageFamily> family = GetAppIconImageFamily();
  if (!family) {
    return SkBitmap();
  }

  SkBitmap app_icon_bitmap = family
                                 ->CreateExact(profiles::kShortcutIconSizeWin,
                                               profiles::kShortcutIconSizeWin)
                                 .AsBitmap();
  if (app_icon_bitmap.isNull()) {
    return SkBitmap();
  }

  ProfileAttributesEntry* entry =
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetProfileAttributesWithPath(profile->GetPath());
  if (!entry) {
    return SkBitmap();
  }

  SkBitmap avatar_bitmap_2x = profiles::GetWin2xAvatarImage(entry);
  return profiles::GetBadgedWinIconBitmapForAvatar(app_icon_bitmap,
                                                   avatar_bitmap_2x);
}

void BrowserDesktopWindowTreeHostWin::SetWindowIcon(bool badged) {
  // Hold onto the previous icon so that the currently displayed
  // icon is valid until replaced with the new icon.
  base::win::ScopedGDIObject<HICON> previous_icon = std::move(icon_handle_);
  if (badged) {
    CHECK(browser_view_);
    icon_handle_ = IconUtil::CreateHICONFromSkBitmap(
        GetBadgedIconBitmapForProfile(browser_view_->browser()->profile()));
  } else {
    icon_handle_.reset(GetAppIcon());
  }
  SendMessage(GetHWND(), WM_SETICON, ICON_SMALL,
              reinterpret_cast<LPARAM>(icon_handle_.get()));
  SendMessage(GetHWND(), WM_SETICON, ICON_BIG,
              reinterpret_cast<LPARAM>(icon_handle_.get()));
}

////////////////////////////////////////////////////////////////////////////////
// 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 BrowserDesktopWindowTreeHostWin(native_widget_delegate,
                                             desktop_native_widget_aura,
                                             browser_view, browser_frame);
}