File: native_widget_mac.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (1364 lines) | stat: -rw-r--r-- 44,092 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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/widget/native_widget_mac.h"

#include <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h>
#include <CoreFoundation/CoreFoundation.h>

#include <utility>
#include <vector>

#include "base/base64.h"
#include "base/check_op.h"
#include "base/debug/dump_without_crashing.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h"
#include "base/strings/sys_string_conversions.h"
#include "components/crash/core/common/crash_key.h"
#import "components/remote_cocoa/app_shim/bridged_content_view.h"
#import "components/remote_cocoa/app_shim/native_widget_mac_nswindow.h"
#import "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
#import "components/remote_cocoa/app_shim/views_nswindow_delegate.h"
#import "ui/base/cocoa/window_size_constants.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/color/color_provider_key.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/gestures/gesture_recognizer.h"
#include "ui/events/gestures/gesture_recognizer_impl_mac.h"
#include "ui/events/gestures/gesture_types.h"
#include "ui/gfx/font_list.h"
#import "ui/gfx/mac/coordinate_conversion.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/native_theme_mac.h"
#import "ui/views/cocoa/drag_drop_client_mac.h"
#import "ui/views/cocoa/native_widget_mac_ns_window_host.h"
#include "ui/views/cocoa/text_input_host.h"
#include "ui/views/views_features.h"
#include "ui/views/widget/drop_helper.h"
#include "ui/views/widget/native_widget_delegate.h"
#include "ui/views/widget/widget_aura_utils.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/native_frame_view_mac.h"

using remote_cocoa::mojom::WindowVisibilityState;

namespace views {

namespace {

base::LazyInstance<base::RepeatingCallbackList<void(NativeWidgetMac*)>>::
    DestructorAtExit g_init_native_widget_callbacks = LAZY_INSTANCE_INITIALIZER;

uint64_t StyleMaskForParams(const Widget::InitParams& params) {
  // If the Widget is modal, it will be displayed as a sheet. This works best if
  // it has NSWindowStyleMaskTitled. For example, with
  // NSWindowStyleMaskBorderless, the parent window still accepts input.
  // A sheet will not have a titlebar despite being NSWindowStyleMaskTitled.
  // NSWindowStyleMaskFullSizeContentView ensures that calculating the modal's
  // content rect doesn't account for a nonexistent title bar.
  // TODO(crbug.com/390441085): a window-modal should always have a parent.
  // Otherwise, it will not be displayed as a sheet. Then it will show a native
  // titlebar, which will cover the content.
  if (params.delegate &&
      params.delegate->GetModalType() == ui::mojom::ModalType::kWindow) {
    return NSWindowStyleMaskTitled | NSWindowStyleMaskFullSizeContentView;
  }

  // TODO(tapted): Determine better masks when there are use cases for it.
  if (params.remove_standard_frame) {
    return NSWindowStyleMaskBorderless;
  }

  if (params.type == Widget::InitParams::TYPE_WINDOW) {
    return NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
           NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
  }
  return NSWindowStyleMaskBorderless;
}

CGWindowLevel CGWindowLevelForZOrderLevel(ui::ZOrderLevel level,
                                          Widget::InitParams::Type type) {
  switch (level) {
    case ui::ZOrderLevel::kNormal:
      return kCGNormalWindowLevel;
    case ui::ZOrderLevel::kFloatingWindow:
      if (type == Widget::InitParams::TYPE_MENU) {
        return kCGPopUpMenuWindowLevel;
      } else {
        return kCGFloatingWindowLevel;
      }
    case ui::ZOrderLevel::kFloatingUIElement:
      if (type == Widget::InitParams::TYPE_DRAG) {
        return kCGDraggingWindowLevel;
      } else {
        return kCGStatusWindowLevel;
      }
    case ui::ZOrderLevel::kSecuritySurface:
      return kCGScreenSaverWindowLevel - 1;
  }
}

}  // namespace

// Implements zoom following focus for macOS accessibility zoom.
class NativeWidgetMac::ZoomFocusMonitor : public FocusChangeListener {
 public:
  ZoomFocusMonitor() = default;
  ~ZoomFocusMonitor() override = default;
  void OnWillChangeFocus(View* focused_before, View* focused_now) override {}
  void OnDidChangeFocus(View* focused_before, View* focused_now) override {
    if (!focused_now || !UAZoomEnabled()) {
      return;
    }
    // Web content handles its own zooming.
    if (focused_now->GetClassName() == "WebView") {
      return;
    }
    NSRect rect = NSRectFromCGRect(focused_now->GetBoundsInScreen().ToCGRect());
    UAZoomChangeFocus(&rect, nullptr, kUAZoomFocusTypeOther);
  }
};

////////////////////////////////////////////////////////////////////////////////
// NativeWidgetMac:

NativeWidgetMac::NativeWidgetMac(internal::NativeWidgetDelegate* delegate)
    // TODO(crbug.com/346814969): Investigate where a null `delegate` should
    // be allowed.
    : delegate_(delegate ? delegate->AsWidget()->GetWeakPtr() : nullptr),
      ns_window_host_(new NativeWidgetMacNSWindowHost(this)) {}

NativeWidgetMac::~NativeWidgetMac() {
  if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) {
    owned_delegate_.reset();
  } else {
    CloseNow();
  }
}

void NativeWidgetMac::WindowDestroying() {
  OnWindowDestroying(GetNativeWindow());
  if (delegate_) {
    delegate_->OnNativeWidgetDestroying();
  }
}

void NativeWidgetMac::WindowDestroyed() {
  DCHECK(GetNSWindowMojo());
  SetFocusManager(nullptr);
  ns_window_host_.reset();
  // |OnNativeWidgetDestroyed| may delete |this| if the object does not own
  // itself.
  bool should_delete_this =
      (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) ||
      (ownership_ == Widget::InitParams::CLIENT_OWNS_WIDGET);
  if (delegate_) {
    delegate_->OnNativeWidgetDestroyed();
  }
  if (should_delete_this) {
    delete this;
  }
}

void NativeWidgetMac::OnWindowKeyStatusChanged(
    bool is_key,
    bool is_content_first_responder) {
  Widget* widget = GetWidget();
  if (!widget || !widget->OnNativeWidgetActivationChanged(is_key)) {
    return;
  }
  // The contentView is the BridgedContentView hosting the views::RootView. The
  // focus manager will already know if a native subview has focus.
  if (!is_content_first_responder) {
    return;
  }

  if (is_key) {
    widget->OnNativeFocus();
    widget->GetFocusManager()->RestoreFocusedView();
  } else {
    widget->OnNativeBlur();
    widget->GetFocusManager()->StoreFocusedView(true);
    parent_key_lock_.reset();
  }
}

void NativeWidgetMac::OnWindowWillStartLiveResize() {
  if (delegate_) {
    delegate_->OnNativeWidgetUserResizeStarted();
  }
}

void NativeWidgetMac::OnWindowDidEndLiveResize() {
  if (delegate_) {
    delegate_->OnNativeWidgetUserResizeEnded();
  }
}

int32_t NativeWidgetMac::SheetOffsetY() {
  return 0;
}

void NativeWidgetMac::GetWindowFrameTitlebarHeight(
    bool* override_titlebar_height,
    float* titlebar_height) {
  *override_titlebar_height = false;
  *titlebar_height = 0;
}

bool NativeWidgetMac::WillExecuteCommand(
    int32_t command,
    WindowOpenDisposition window_open_disposition,
    bool is_before_first_responder) {
  // This is supported only by subclasses in chrome/browser/ui.
  NOTIMPLEMENTED();
  return false;
}

bool NativeWidgetMac::ExecuteCommand(
    int32_t command,
    WindowOpenDisposition window_open_disposition,
    bool is_before_first_responder) {
  // This is supported only by subclasses in chrome/browser/ui.
  NOTIMPLEMENTED();
  return false;
}

void NativeWidgetMac::InitNativeWidget(Widget::InitParams params) {
  ownership_ = params.ownership;
  if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) {
    owned_delegate_ = base::WrapUnique(delegate_.get());
  }
  name_ = params.name;
  type_ = params.type;
  NativeWidgetMacNSWindowHost* parent_host =
      NativeWidgetMacNSWindowHost::GetFromNativeView(params.parent);

  // Determine the factory through which to create the bridge
  remote_cocoa::ApplicationHost* application_host =
      parent_host ? parent_host->application_host()
                  : GetRemoteCocoaApplicationHost();

  // Compute the parameters to describe the NSWindow.
  auto create_window_params = remote_cocoa::mojom::CreateWindowParams::New();
  create_window_params->window_class =
      remote_cocoa::mojom::WindowClass::kDefault;
  create_window_params->style_mask = StyleMaskForParams(params);
  create_window_params->titlebar_appears_transparent = false;
  create_window_params->window_title_hidden = false;
  PopulateCreateWindowParams(params, create_window_params.get());

  if (application_host) {
    ns_window_host_->CreateRemoteNSWindow(application_host,
                                          std::move(create_window_params));
  } else {
    NativeWidgetMacNSWindow* window =
        CreateNSWindow(create_window_params.get());
    ns_window_host_->CreateInProcessNSWindowBridge(window);
  }

  // If the z-order wasn't specifically set to something other than `kNormal`,
  // then override it if it would leave the widget z-ordered incorrectly in some
  // platform-specific corner cases.
  if (params.parent &&
      (!params.z_order || params.z_order == ui::ZOrderLevel::kNormal)) {
    if (auto* parent_widget = Widget::GetWidgetForNativeView(params.parent)) {
      // If our parent is z-ordered above us, then float a bit higher.
      params.z_order =
          std::max(params.z_order.value_or(params.EffectiveZOrderLevel()),
                   parent_widget->GetZOrderLevel());
    }
  }

  ns_window_host_->SetParent(parent_host);
  ns_window_host_->InitWindow(params,
                              ConvertBoundsToScreenIfNeeded(params.bounds));

  OnWindowInitialized();

  // Only set the z-order here if it is non-default since setting it may affect
  // how the window is treated by Expose.
  if (params.EffectiveZOrderLevel() != ui::ZOrderLevel::kNormal) {
    SetZOrderLevel(params.EffectiveZOrderLevel());
  }

  GetNSWindowMojo()->SetIgnoresMouseEvents(!params.accept_events);
  GetNSWindowMojo()->SetVisibleOnAllSpaces(params.visible_on_all_workspaces);

  delegate_->OnNativeWidgetCreated();

  DCHECK(GetWidget()->GetRootView());
  ns_window_host_->SetRootView(GetWidget()->GetRootView());
  GetNSWindowMojo()->CreateContentView(ns_window_host_->GetRootViewNSViewId(),
                                       GetWidget()->GetRootView()->bounds(),
                                       params.corner_radius);
  if (auto* focus_manager = GetWidget()->GetFocusManager()) {
    GetNSWindowMojo()->MakeFirstResponder();
    // Only one ZoomFocusMonitor is needed per FocusManager, so create one only
    // for top-level widgets.
    if (GetWidget()->is_top_level()) {
      zoom_focus_monitor_ = std::make_unique<ZoomFocusMonitor>();
    }
    SetFocusManager(focus_manager);
  }
  ns_window_host_->CreateCompositor(params);

  g_init_native_widget_callbacks.Get().Notify(this);
}

void NativeWidgetMac::OnWidgetInitDone() {
  OnSizeConstraintsChanged();
  ns_window_host_->OnWidgetInitDone();
}

void NativeWidgetMac::ReparentNativeViewImpl(gfx::NativeView new_parent) {
  gfx::NativeView child = GetNativeView();
  DCHECK_NE(child, new_parent);
  if (new_parent) {
    DCHECK([new_parent.GetNativeNSView() window]);
    CHECK(new_parent);
    CHECK_NE([child.GetNativeNSView() superview], new_parent.GetNativeNSView());
  }

  NativeWidgetMacNSWindowHost* child_window_host =
      NativeWidgetMacNSWindowHost::GetFromNativeView(child);
  DCHECK(child_window_host);
  gfx::NativeView widget_view =
      child_window_host->native_widget_mac()->GetNativeView();
  DCHECK_EQ(child, widget_view);
  gfx::NativeWindow widget_window =
      child_window_host->native_widget_mac()->GetNativeWindow();
  DCHECK(
      [child.GetNativeNSView() isDescendantOf:widget_view.GetNativeNSView()]);
  DCHECK(widget_window);
  DCHECK(![widget_window.GetNativeNSWindow() isSheet]);

  NativeWidgetMacNSWindowHost* new_parent_window_host =
      new_parent ? NativeWidgetMacNSWindowHost::GetFromNativeView(new_parent)
                 : nullptr;

  // Early out for no-op changes.
  if (new_parent_window_host && child == widget_view &&
      child_window_host->parent() == new_parent_window_host) {
    return;
  }

  // First notify all the widgets that they are being disassociated from their
  // previous parent.
  Widget::Widgets widgets = GetAllChildWidgets(child);
  for (Widget* widget : widgets) {
    widget->NotifyNativeViewHierarchyWillChange();
  }

  child_window_host->SetParent(new_parent_window_host);

  // And now, notify them that they have a brand new parent.
  for (Widget* widget : widgets) {
    widget->NotifyNativeViewHierarchyChanged();
  }
}

std::unique_ptr<NonClientFrameView>
NativeWidgetMac::CreateNonClientFrameView() {
  return GetWidget() ? std::make_unique<NativeFrameViewMac>(GetWidget())
                     : nullptr;
}

bool NativeWidgetMac::ShouldUseNativeFrame() const {
  return true;
}

bool NativeWidgetMac::ShouldWindowContentsBeTransparent() const {
  // On Windows, this returns true when Aero is enabled which draws the titlebar
  // with translucency.
  return false;
}

void NativeWidgetMac::FrameTypeChanged() {
  if (!GetWidget()) {
    return;
  }
  // This is called when the Theme has changed; forward the event to the root
  // widget.
  GetWidget()->ThemeChanged();
  GetWidget()->GetRootView()->SchedulePaint();
}

Widget* NativeWidgetMac::GetWidget() {
  return delegate_ ? delegate_->AsWidget() : nullptr;
}

const Widget* NativeWidgetMac::GetWidget() const {
  return delegate_ ? delegate_->AsWidget() : nullptr;
}

gfx::NativeView NativeWidgetMac::GetNativeView() const {
  // When a widget becomes a subwidget, its contentView moves to an another
  // NSWindow. When this happens, the window's contentView will be nil.
  // Return the cached original contentView instead.
  NSView* contentView = (__bridge NSView*)GetNativeWindowProperty(
      views::NativeWidgetMacNSWindowHost::kMovedContentNSView);
  if (contentView) {
    return gfx::NativeView(contentView);
  }
  // Returns a BridgedContentView, unless there is no views::RootView set.
  return gfx::NativeView(GetNativeWindow().GetNativeNSWindow().contentView);
}

gfx::NativeWindow NativeWidgetMac::GetNativeWindow() const {
  return gfx::NativeWindow(
      ns_window_host_ ? ns_window_host_->GetInProcessNSWindow() : nil);
}

Widget* NativeWidgetMac::GetTopLevelWidget() {
  NativeWidgetPrivate* native_widget = GetTopLevelNativeWidget(GetNativeView());
  return native_widget ? native_widget->GetWidget() : nullptr;
}

const ui::Compositor* NativeWidgetMac::GetCompositor() const {
  return ns_window_host_ && ns_window_host_->layer()
             ? ns_window_host_->layer()->GetCompositor()
             : nullptr;
}

const ui::Layer* NativeWidgetMac::GetLayer() const {
  return ns_window_host_ ? ns_window_host_->layer() : nullptr;
}

void NativeWidgetMac::ReorderNativeViews() {
  if (ns_window_host_) {
    ns_window_host_->ReorderChildViews();
  }
}

void NativeWidgetMac::ViewRemoved(View* view) {
  DragDropClientMac* client =
      ns_window_host_ ? ns_window_host_->drag_drop_client() : nullptr;
  if (client) {
    client->drop_helper()->ResetTargetViewIfEquals(view);
  }
}

void NativeWidgetMac::SetNativeWindowProperty(const char* name, void* value) {
  if (ns_window_host_) {
    ns_window_host_->SetNativeWindowProperty(name, value);
  }
}

void* NativeWidgetMac::GetNativeWindowProperty(const char* name) const {
  if (ns_window_host_) {
    return ns_window_host_->GetNativeWindowProperty(name);
  }

  return nullptr;
}

TooltipManager* NativeWidgetMac::GetTooltipManager() const {
  if (ns_window_host_) {
    return ns_window_host_->tooltip_manager();
  }

  return nullptr;
}

void NativeWidgetMac::SetCapture() {
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->AcquireCapture();
  }
}

void NativeWidgetMac::ReleaseCapture() {
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->ReleaseCapture();
  }
}

bool NativeWidgetMac::HasCapture() const {
  return ns_window_host_ && ns_window_host_->IsMouseCaptureActive();
}

ui::InputMethod* NativeWidgetMac::GetInputMethod() {
  if (!input_method_) {
    input_method_ = ui::CreateInputMethod(this, gfx::kNullAcceleratedWidget);
    // For now, use always-focused mode on Mac for the input method.
    // TODO(tapted): Move this to OnWindowKeyStatusChangedTo() and balance.
    input_method_->OnFocus();
  }
  return input_method_.get();
}

void NativeWidgetMac::CenterWindow(const gfx::Size& size) {
  if (GetNSWindowMojo() && GetWidget()) {
    GetNSWindowMojo()->SetSizeAndCenter(size, GetWidget()->GetMinimumSize());
  }
}

void NativeWidgetMac::GetWindowPlacement(
    gfx::Rect* bounds,
    ui::mojom::WindowShowState* show_state) const {
  *bounds = GetRestoredBounds();
  if (IsFullscreen()) {
    *show_state = ui::mojom::WindowShowState::kFullscreen;
  } else if (IsMinimized()) {
    *show_state = ui::mojom::WindowShowState::kMinimized;
  } else {
    *show_state = ui::mojom::WindowShowState::kNormal;
  }
}

bool NativeWidgetMac::SetWindowTitle(const std::u16string& title) {
  if (!ns_window_host_) {
    return false;
  }
  return ns_window_host_->SetWindowTitle(title);
}

void NativeWidgetMac::SetWindowIcons(const gfx::ImageSkia& window_icon,
                                     const gfx::ImageSkia& app_icon) {
  // Per-window icons are not really a thing on Mac, so do nothing.
  // TODO(tapted): Investigate whether to use NSWindowDocumentIconButton to set
  // an icon next to the window title. See http://crbug.com/766897.
}

void NativeWidgetMac::InitModalType(ui::mojom::ModalType modal_type) {
  if (modal_type == ui::mojom::ModalType::kNone) {
    return;
  }

  // System modal windows not implemented (or used) on Mac.
  DCHECK_NE(ui::mojom::ModalType::kSystem, modal_type);

  // A peculiarity of the constrained window framework is that it permits a
  // dialog of MODAL_TYPE_WINDOW to have a null parent window; falling back to
  // a non-modal window in this case.
  DCHECK(ns_window_host_->parent() ||
         modal_type == ui::mojom::ModalType::kWindow);

  // Everything happens upon show.
}

void NativeWidgetMac::OnWidgetThemeChanged(ui::ColorProviderKey::ColorMode color_mode) {
  if (ns_window_host_) {
    ns_window_host_->SetColorMode(color_mode);
  }
}

gfx::Rect NativeWidgetMac::GetWindowBoundsInScreen() const {
  return ns_window_host_ ? ns_window_host_->GetWindowBoundsInScreen()
                         : gfx::Rect();
}

gfx::Rect NativeWidgetMac::GetClientAreaBoundsInScreen() const {
  return ns_window_host_ ? ns_window_host_->GetContentBoundsInScreen()
                         : gfx::Rect();
}

gfx::Rect NativeWidgetMac::GetRestoredBounds() const {
  return ns_window_host_ ? ns_window_host_->GetRestoredBounds() : gfx::Rect();
}

std::string NativeWidgetMac::GetWorkspace() const {
  return ns_window_host_ ? base::Base64Encode(
                               ns_window_host_->GetWindowStateRestorationData())
                         : std::string();
}

gfx::Rect NativeWidgetMac::ConvertBoundsToScreenIfNeeded(
    const gfx::Rect& bounds) const {
  // If there isn't a parent widget, then bounds cannot be relative to the
  // parent.
  if (!ns_window_host_ || !ns_window_host_->parent() || !GetWidget()) {
    return bounds;
  }

  // Replicate the logic in desktop_aura/desktop_screen_position_client.cc.
  if (GetAuraWindowTypeForWidgetType(type_) ==
          aura::client::WINDOW_TYPE_POPUP ||
      GetWidget()->is_top_level()) {
    return bounds;
  }

  // Empty bounds are only allowed to be specified at initialization and are
  // expected not to be translated.
  if (bounds.IsEmpty()) {
    return bounds;
  }

  gfx::Rect bounds_in_screen = bounds;
  bounds_in_screen.Offset(
      ns_window_host_->parent()->GetWindowBoundsInScreen().OffsetFromOrigin());
  return bounds_in_screen;
}

void NativeWidgetMac::SetBounds(const gfx::Rect& bounds) {
  if (!ns_window_host_) {
    return;
  }
  ns_window_host_->SetBoundsInScreen(ConvertBoundsToScreenIfNeeded(bounds));
}

void NativeWidgetMac::SetBoundsConstrained(const gfx::Rect& bounds) {
  if (!ns_window_host_) {
    return;
  }
  gfx::Rect new_bounds(bounds);
  if (ns_window_host_->parent()) {
    new_bounds.AdjustToFit(
        gfx::Rect(ns_window_host_->parent()->GetWindowBoundsInScreen().size()));
  } else {
    new_bounds = ConstrainBoundsToDisplayWorkArea(new_bounds);
  }
  SetBounds(new_bounds);
}

void NativeWidgetMac::SetSize(const gfx::Size& size) {
  if (!ns_window_host_) {
    return;
  }
  ns_window_host_->SetSize(size);
}

void NativeWidgetMac::StackAbove(gfx::NativeView native_view) {
  if (!GetNSWindowMojo()) {
    return;
  }

  auto* sibling_host =
      NativeWidgetMacNSWindowHost::GetFromNativeView(native_view);

  if (!sibling_host) {
    // This will only work if |this| is in-process.
    DCHECK(!ns_window_host_->application_host());
    NSInteger view_parent = native_view.GetNativeNSView().window.windowNumber;
    [GetNativeWindow().GetNativeNSWindow() orderWindow:NSWindowAbove
                                            relativeTo:view_parent];
    return;
  }

  CHECK_EQ(ns_window_host_->application_host(),
           sibling_host->application_host())
      << "|native_view|'s NativeWidgetMacNSWindowHost isn't same "
         "process |this|";
  // Check if |native_view|'s NativeWidgetMacNSWindowHost corresponds to the
  // same process as |this|.
  GetNSWindowMojo()->StackAbove(sibling_host->bridged_native_widget_id());
  return;
}

void NativeWidgetMac::StackAtTop() {
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->StackAtTop();
  }
}

bool NativeWidgetMac::IsStackedAbove(gfx::NativeView native_view) {
  if (!GetNSWindowMojo()) {
    return false;
  }

  // -[NSApplication orderedWindows] are ordered front-to-back.
  NSWindow* first = GetNativeWindow().GetNativeNSWindow();
  NSWindow* second = [native_view.GetNativeNSView() window];

  for (NSWindow* window in [NSApp orderedWindows]) {
    if (window == second) {
      return !first;
    }

    if (window == first) {
      first = nil;
    }
  }

  return false;
}

void NativeWidgetMac::SetShape(std::unique_ptr<Widget::ShapeRects> shape) {
  NOTIMPLEMENTED();
}

void NativeWidgetMac::Close() {
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->CloseWindow();
  }
}

void NativeWidgetMac::CloseNow() {
  if (ns_window_host_) {
    ns_window_host_->CloseWindowNow();
  }
  // Note: |ns_window_host_| will be deleted here, and |this| will be deleted
  // here when ownership_ == NATIVE_WIDGET_OWNS_WIDGET,
}

void NativeWidgetMac::Show(ui::mojom::WindowShowState show_state,
                           const gfx::Rect& restore_bounds) {
  if (!GetNSWindowHost() || !delegate_) {
    return;
  }

  switch (show_state) {
    case ui::mojom::WindowShowState::kDefault:
    case ui::mojom::WindowShowState::kNormal:
    case ui::mojom::WindowShowState::kInactive:
    case ui::mojom::WindowShowState::kMinimized:
      break;
    case ui::mojom::WindowShowState::kMaximized:
    case ui::mojom::WindowShowState::kFullscreen:
      NOTIMPLEMENTED();
      break;
    case ui::mojom::WindowShowState::kEnd:
      NOTREACHED();
  }
  auto window_state = WindowVisibilityState::kShowAndActivateWindow;
  if (show_state == ui::mojom::WindowShowState::kInactive) {
    window_state = WindowVisibilityState::kShowInactive;
  } else if (show_state == ui::mojom::WindowShowState::kMinimized) {
    window_state = WindowVisibilityState::kMiniaturizeWindow;
  } else if (show_state == ui::mojom::WindowShowState::kDefault) {
    window_state = delegate_->CanActivate()
                       ? window_state
                       : WindowVisibilityState::kShowInactive;
  }
  GetNSWindowHost()->SetVisibilityState(window_state);

  // Ignore the SetInitialFocus() result. BridgedContentView should get
  // firstResponder status regardless.
  delegate_->SetInitialFocus(show_state);
}

void NativeWidgetMac::Hide() {
  if (!GetNSWindowHost()) {
    return;
  }
  GetNSWindowHost()->SetVisibilityState(WindowVisibilityState::kHideWindow);
}

bool NativeWidgetMac::IsVisible() const {
  return ns_window_host_ && ns_window_host_->IsVisible();
}

bool NativeWidgetMac::IsVisibleOnScreen() const {
  return ns_window_host_ && ns_window_host_->IsVisibleOnScreen();
}

void NativeWidgetMac::Activate() {
  if (!GetNSWindowHost()) {
    return;
  }
  GetNSWindowHost()->SetVisibilityState(
      WindowVisibilityState::kShowAndActivateWindow);
}

void NativeWidgetMac::Deactivate() {
  NOTIMPLEMENTED();
}

bool NativeWidgetMac::IsActive() const {
  return ns_window_host_ ? ns_window_host_->IsWindowKey() : false;
}

void NativeWidgetMac::SetZOrderLevel(ui::ZOrderLevel order) {
  if (!GetNSWindowMojo()) {
    return;
  }
  z_order_level_ = order;
  GetNSWindowMojo()->SetWindowLevel(CGWindowLevelForZOrderLevel(order, type_));
}

ui::ZOrderLevel NativeWidgetMac::GetZOrderLevel() const {
  return z_order_level_;
}

void NativeWidgetMac::SetActivationIndependence(bool independence) {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetActivationIndependence(independence);
}

void NativeWidgetMac::SetVisibleOnAllWorkspaces(bool always_visible) {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetVisibleOnAllSpaces(always_visible);
}

bool NativeWidgetMac::IsVisibleOnAllWorkspaces() const {
  return false;
}

void NativeWidgetMac::Maximize() {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetZoomed(true);
}

void NativeWidgetMac::Minimize() {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetMiniaturized(true);
}

bool NativeWidgetMac::IsMaximized() const {
  if (!ns_window_host_) {
    return false;
  }
  return ns_window_host_->IsZoomed();
}

bool NativeWidgetMac::IsMinimized() const {
  if (!ns_window_host_) {
    return false;
  }
  return ns_window_host_->IsMiniaturized();
}

void NativeWidgetMac::Restore() {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->ExitFullscreen();
  GetNSWindowMojo()->SetMiniaturized(false);
  GetNSWindowMojo()->SetZoomed(false);
}

void NativeWidgetMac::SetFullscreen(bool fullscreen,
                                    int64_t target_display_id) {
  if (!ns_window_host_) {
    return;
  }
  ns_window_host_->SetFullscreen(fullscreen, target_display_id);
}

bool NativeWidgetMac::IsFullscreen() const {
  return ns_window_host_ && ns_window_host_->target_fullscreen_state();
}

void NativeWidgetMac::SetCanAppearInExistingFullscreenSpaces(
    bool can_appear_in_existing_fullscreen_spaces) {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetCanAppearInExistingFullscreenSpaces(
      can_appear_in_existing_fullscreen_spaces);
}

void NativeWidgetMac::SetOpacity(float opacity) {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetOpacity(opacity);
}

void NativeWidgetMac::SetAspectRatio(const gfx::SizeF& aspect_ratio,
                                     const gfx::Size& excluded_margin) {
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->SetAspectRatio(aspect_ratio, excluded_margin);
}

void NativeWidgetMac::FlashFrame(bool flash_frame) {
  NOTIMPLEMENTED();
}

void NativeWidgetMac::RunShellDrag(std::unique_ptr<ui::OSExchangeData> data,
                                   const gfx::Point& location,
                                   int operation,
                                   ui::mojom::DragEventSource source) {
  if (!ns_window_host_) {
    return;
  }
  ns_window_host_->drag_drop_client()->StartDragAndDrop(std::move(data),
                                                        operation, source);
}

void NativeWidgetMac::CancelShellDrag(View* view) {
  if (!ns_window_host_) {
    return;
  }
  ns_window_host_->drag_drop_client()->EndDrag();
}

void NativeWidgetMac::SchedulePaintInRect(const gfx::Rect& rect) {
  // |rect| is relative to client area of the window.
  NSWindow* window = GetNativeWindow().GetNativeNSWindow();
  NSRect client_rect = [window contentRectForFrameRect:[window frame]];
  NSRect target_rect = rect.ToCGRect();

  // Convert to Appkit coordinate system (origin at bottom left).
  target_rect.origin.y =
      NSHeight(client_rect) - target_rect.origin.y - NSHeight(target_rect);
  [GetNativeView().GetNativeNSView() setNeedsDisplayInRect:target_rect];
  if (ns_window_host_ && ns_window_host_->layer()) {
    ns_window_host_->layer()->SchedulePaint(rect);
  }
}

void NativeWidgetMac::ScheduleLayout() {
  ui::Compositor* compositor = GetCompositor();
  if (compositor) {
    compositor->ScheduleDraw();
  }
}

void NativeWidgetMac::SetCursor(const ui::Cursor& cursor) {
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->SetCursor(cursor);
  }
}

void NativeWidgetMac::ShowEmojiPanel() {
  // We must plumb the call to ui::ShowEmojiPanel() over the bridge so that it
  // is called from the correct process.
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->ShowEmojiPanel();
  }
}

bool NativeWidgetMac::IsMouseEventsEnabled() const {
  // On platforms with touch, mouse events get disabled and calls to this method
  // can affect hover states. Since there is no touch on desktop Mac, this is
  // always true. Touch on Mac is tracked in http://crbug.com/445520.
  return true;
}

bool NativeWidgetMac::IsMouseButtonDown() const {
  return [NSEvent pressedMouseButtons] != 0;
}

void NativeWidgetMac::ClearNativeFocus() {
  // To quote DesktopWindowTreeHostX11, "This method is weird and misnamed."
  // The goal is to set focus to the content window, thereby removing focus from
  // any NSView in the window that doesn't belong to toolkit-views.
  if (!GetNSWindowMojo()) {
    return;
  }
  GetNSWindowMojo()->MakeFirstResponder();
}

gfx::Rect NativeWidgetMac::GetWorkAreaBoundsInScreen() const {
  return ns_window_host_ ? ns_window_host_->GetCurrentDisplay().work_area()
                         : gfx::Rect();
}

Widget::MoveLoopResult NativeWidgetMac::RunMoveLoop(
    const gfx::Vector2d& drag_offset,
    Widget::MoveLoopSource source,
    Widget::MoveLoopEscapeBehavior escape_behavior) {
  if (!GetInProcessNSWindowBridge()) {
    return Widget::MoveLoopResult::kCanceled;
  }

  ReleaseCapture();
  return GetInProcessNSWindowBridge()->RunMoveLoop(drag_offset)
             ? Widget::MoveLoopResult::kSuccessful
             : Widget::MoveLoopResult::kCanceled;
}

void NativeWidgetMac::EndMoveLoop() {
  if (GetInProcessNSWindowBridge()) {
    GetInProcessNSWindowBridge()->EndMoveLoop();
  }
}

void NativeWidgetMac::SetVisibilityChangedAnimationsEnabled(bool value) {
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->SetAnimationEnabled(value);
  }
}

void NativeWidgetMac::SetVisibilityAnimationDuration(
    const base::TimeDelta& duration) {
  NOTIMPLEMENTED();
}

void NativeWidgetMac::SetVisibilityAnimationTransition(
    Widget::VisibilityTransition widget_transitions) {
  remote_cocoa::mojom::VisibilityTransition transitions =
      remote_cocoa::mojom::VisibilityTransition::kNone;
  switch (widget_transitions) {
    case Widget::ANIMATE_NONE:
      transitions = remote_cocoa::mojom::VisibilityTransition::kNone;
      break;
    case Widget::ANIMATE_SHOW:
      transitions = remote_cocoa::mojom::VisibilityTransition::kShow;
      break;
    case Widget::ANIMATE_HIDE:
      transitions = remote_cocoa::mojom::VisibilityTransition::kHide;
      break;
    case Widget::ANIMATE_BOTH:
      transitions = remote_cocoa::mojom::VisibilityTransition::kBoth;
      break;
  }
  if (GetNSWindowMojo()) {
    GetNSWindowMojo()->SetTransitionsToAnimate(transitions);
  }
}

ui::GestureRecognizer* NativeWidgetMac::GetGestureRecognizer() {
  static base::NoDestructor<ui::GestureRecognizerImplMac> recognizer;
  return recognizer.get();
}

ui::GestureConsumer* NativeWidgetMac::GetGestureConsumer() {
  NOTIMPLEMENTED();
  return nullptr;
}

void NativeWidgetMac::OnSizeConstraintsChanged() {
  if (!GetNSWindowMojo() || !GetWidget()) {
    return;
  }

  Widget* widget = GetWidget();
  GetNSWindowMojo()->SetSizeConstraints(
      widget->GetMinimumSize(), widget->GetMaximumSize(),
      widget->widget_delegate()->CanResize(),
      widget->widget_delegate()->CanMaximize());
}

void NativeWidgetMac::OnNativeViewHierarchyWillChange() {
  // If this is not top-level, then the FocusManager may change, so remove our
  // listeners.
  if (GetWidget() && !GetWidget()->is_top_level()) {
    SetFocusManager(nullptr);
  }
  parent_key_lock_.reset();
}

void NativeWidgetMac::OnNativeViewHierarchyChanged() {
  if (GetWidget() && !GetWidget()->is_top_level()) {
    SetFocusManager(GetWidget()->GetFocusManager());
  }
}

bool NativeWidgetMac::SetAllowScreenshots(bool allow) {
  if (ns_window_host_) {
    ns_window_host_->SetAllowScreenshots(allow);
    return true;
  }
  return false;
}

bool NativeWidgetMac::AreScreenshotsAllowed() {
  if (ns_window_host_) {
    return ns_window_host_->AllowScreenshots();
  }
  return true;
}

std::string NativeWidgetMac::GetName() const {
  return name_;
}

base::WeakPtr<internal::NativeWidgetPrivate> NativeWidgetMac::GetWeakPtr() {
  return weak_factory.GetWeakPtr();
}

// static
base::CallbackListSubscription
NativeWidgetMac::RegisterInitNativeWidgetCallback(
    const base::RepeatingCallback<void(NativeWidgetMac*)>& callback) {
  DCHECK(!callback.is_null());
  return g_init_native_widget_callbacks.Get().Add(callback);
}

void NativeWidgetMac::PopulateCreateWindowParams(
    const Widget::InitParams& widget_params,
    remote_cocoa::mojom::CreateWindowParams* params) {
  if (widget_params.is_overlay) {
    params->window_class = remote_cocoa::mojom::WindowClass::kOverlay;
  }
  params->animation_enabled = widget_params.animation_enabled;
}

NativeWidgetMacNSWindow* NativeWidgetMac::CreateNSWindow(
    const remote_cocoa::mojom::CreateWindowParams* params) {
  return remote_cocoa::NativeWidgetNSWindowBridge::CreateNSWindow(params);
}

remote_cocoa::ApplicationHost*
NativeWidgetMac::GetRemoteCocoaApplicationHost() {
  return nullptr;
}

remote_cocoa::mojom::NativeWidgetNSWindow* NativeWidgetMac::GetNSWindowMojo()
    const {
  return ns_window_host_ ? ns_window_host_->GetNSWindowMojo() : nullptr;
}

remote_cocoa::NativeWidgetNSWindowBridge*
NativeWidgetMac::GetInProcessNSWindowBridge() const {
  return ns_window_host_ ? ns_window_host_->GetInProcessNSWindowBridge()
                         : nullptr;
}

void NativeWidgetMac::SetFocusManager(FocusManager* new_focus_manager) {
  if (focus_manager_) {
    if (View* old_focus = focus_manager_->GetFocusedView()) {
      OnDidChangeFocus(old_focus, nullptr);
    }
    focus_manager_->RemoveFocusChangeListener(this);
    if (zoom_focus_monitor_) {
      focus_manager_->RemoveFocusChangeListener(zoom_focus_monitor_.get());
    }
  }
  focus_manager_ = new_focus_manager;
  if (focus_manager_) {
    if (View* new_focus = focus_manager_->GetFocusedView()) {
      OnDidChangeFocus(nullptr, new_focus);
    }
    focus_manager_->AddFocusChangeListener(this);
    if (zoom_focus_monitor_) {
      focus_manager_->AddFocusChangeListener(zoom_focus_monitor_.get());
    }

    if (!widget_observation_.IsObserving()) {
      CHECK(GetWidget());
      widget_observation_.Observe(GetWidget());
    }
  }
}

void NativeWidgetMac::OnWillChangeFocus(View* focused_before,
                                        View* focused_now) {}

void NativeWidgetMac::OnDidChangeFocus(View* focused_before,
                                       View* focused_now) {
  ui::InputMethod* input_method = GetWidget()->GetInputMethod();
  if (!input_method) {
    return;
  }

  ui::TextInputClient* new_text_input_client =
      input_method->GetTextInputClient();
  // Sanity check: For a top level widget, when focus moves away from the widget
  // (i.e. |focused_now| is nil), then the textInputClient will be cleared.
  DCHECK(!!focused_now || !new_text_input_client ||
         !GetWidget()->is_top_level());
  if (ns_window_host_) {
    ns_window_host_->text_input_host()->SetTextInputClient(
        new_text_input_client);
  }
}

void NativeWidgetMac::OnFocusManagerDestroying(FocusManager* focus_manager) {
  // TODO(crbug.com/348369180): mac fullscreen overlay widget should be
  // destroyed before its parent widget, subsequently stopping observing the
  // parent's focus manager. However, this is not happening for unknown reasons.
  CHECK_EQ(focus_manager, focus_manager_);
  focus_manager->RemoveFocusChangeListener(this);
}

ui::EventDispatchDetails NativeWidgetMac::DispatchKeyEventPostIME(
    ui::KeyEvent* key) {
  DCHECK(focus_manager_);
  if (!focus_manager_->OnKeyEvent(*key)) {
    key->StopPropagation();
  } else {
    GetWidget()->OnKeyEvent(key);
  }
  return ui::EventDispatchDetails();
}

void NativeWidgetMac::OnWidgetDestroyed(Widget* widget) {
  widget_observation_.Reset();
  // The `widget` owns the `FocusManager`. As such, `NativeWidgetMac` must
  // unregister itself as a focus change listener here if it hasn't done so
  // already, or it may retain a dead focus manager pointer (risking
  // use-after-free).
  SetFocusManager(nullptr);
}

////////////////////////////////////////////////////////////////////////////////
// Widget:

// static
void Widget::CloseAllSecondaryWidgets() {
  NSArray* starting_windows = [NSApp windows];  // Creates an autoreleased copy.
  for (NSWindow* window in starting_windows) {
    // Ignore any windows that couldn't have been created by NativeWidgetMac or
    // a subclass. GetNativeWidgetForNativeWindow() will later interrogate the
    // NSWindow delegate, but we can't trust that delegate to be a valid object.
    if (![window isKindOfClass:[NativeWidgetMacNSWindow class]]) {
      continue;
    }

    // Record a crash key to detect when client code may destroy a
    // WidgetObserver without removing it (possibly leaking the Widget).
    // A crash can occur in generic Widget teardown paths when trying to notify.
    // See http://crbug.com/808318.
    static crash_reporter::CrashKeyString<256> window_info_key("windowInfo");
    std::string value = base::SysNSStringToUTF8(
        [NSString stringWithFormat:@"Closing %@ (%@)", [window title],
                                   [window className]]);
    crash_reporter::ScopedCrashKeyString scopedWindowKey(&window_info_key,
                                                         value);

    Widget* widget = GetWidgetForNativeWindow(gfx::NativeWindow(window));
    if (widget && widget->is_secondary_widget()) {
      [window close];
    }
  }
}

namespace internal {

////////////////////////////////////////////////////////////////////////////////
// internal::NativeWidgetPrivate:

// static
NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget(
    internal::NativeWidgetDelegate* delegate) {
  return new NativeWidgetMac(delegate);
}

// static
NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView(
    gfx::NativeView native_view) {
  return GetNativeWidgetForNativeWindow(
      gfx::NativeWindow([native_view.GetNativeNSView() window]));
}

// static
NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
    gfx::NativeWindow window) {
  if (NativeWidgetMacNSWindowHost* ns_window_host_impl =
          NativeWidgetMacNSWindowHost::GetFromNativeWindow(window)) {
    return ns_window_host_impl->native_widget_mac();
  }
  return nullptr;  // Not created by NativeWidgetMac.
}

// static
NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget(
    gfx::NativeView native_view) {
  NativeWidgetMacNSWindowHost* window_host =
      NativeWidgetMacNSWindowHost::GetFromNativeView(native_view);
  if (!window_host) {
    return nullptr;
  }
  while (window_host->parent()) {
    if (!window_host->native_widget_mac()->GetWidget()) {
      return nullptr;
    }

    if (window_host->native_widget_mac()->GetWidget()->is_top_level()) {
      break;
    }
    window_host = window_host->parent();
  }
  return window_host->native_widget_mac();
}

// static
Widget::Widgets NativeWidgetPrivate::GetAllChildWidgets(
    gfx::NativeView native_view) {
  Widget::Widgets children;

  NativeWidgetMacNSWindowHost* window_host =
      NativeWidgetMacNSWindowHost::GetFromNativeView(native_view);
  if (!window_host) {
    NSView* ns_view = native_view.GetNativeNSView();
    // The NSWindow is not itself a views::Widget, but it may have children that
    // are. Support returning Widgets that are parented to the NSWindow, except:
    // - Ignore requests for children of an NSView that is not a contentView.
    // - We do not add a Widget for |native_view| to |children| (there is none).
    if (ns_view.window.contentView != ns_view) {
      return children;
    }

    // Collect -sheets and -childWindows. A window should never appear in both,
    // since that causes AppKit to glitch.
    NSArray* sheet_children = ns_view.window.sheets;
    for (NSWindow* native_child in sheet_children) {
      children.merge(
          GetAllChildWidgets(gfx::NativeView(native_child.contentView)));
    }

    for (NSWindow* native_child in ns_view.window.childWindows) {
      DCHECK(![sheet_children containsObject:native_child]);
      children.merge(
          GetAllChildWidgets(gfx::NativeView(native_child.contentView)));
    }
    return children;
  }

  // If |native_view| is a subview of the contentView, it will share an
  // NSWindow, but will itself be a native child of the Widget. That is, adding
  // window_host->..->GetWidget() to |children| would be adding the _parent_ of
  // |native_view|, not the Widget for |native_view|. |native_view| doesn't have
  // a corresponding Widget of its own in this case (and so can't have Widget
  // children of its own on Mac).
  if (window_host->native_widget_mac()->GetNativeView() != native_view) {
    return children;
  }

  // Code expects widget for |native_view| to be added to |children|.
  if (window_host->native_widget_mac()->GetWidget()) {
    children.insert(window_host->native_widget_mac()->GetWidget());
  }

  // When the NSWindow *is* a Widget, only consider children(). I.e. do not
  // look through -[NSWindow childWindows] as done for the (!window_host) case
  // above. -childWindows does not support hidden windows, and anything in there
  // which is not in children() would have been added by AppKit.
  for (NativeWidgetMacNSWindowHost* child : window_host->children()) {
    children.merge(
        GetAllChildWidgets(child->native_widget_mac()->GetNativeView()));
  }

  return children;
}

// static
Widget::Widgets NativeWidgetPrivate::GetAllOwnedWidgets(
    gfx::NativeView native_view) {
  NativeWidgetMacNSWindowHost* window_host =
      NativeWidgetMacNSWindowHost::GetFromNativeView(native_view);
  if (!window_host) {
    return GetAllChildWidgets(native_view);
  }
  if (window_host->native_widget_mac()->GetNativeView() != native_view) {
    return Widget::Widgets();
  }
  Widget::Widgets owned;
  for (NativeWidgetMacNSWindowHost* child : window_host->children()) {
    owned.merge(
        GetAllChildWidgets(child->native_widget_mac()->GetNativeView()));
  }
  return owned;
}

// static
void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView child,
                                             gfx::NativeView new_parent) {
  Widget::GetWidgetForNativeView(child)
      ->native_widget_private()
      ->ReparentNativeViewImpl(new_parent);
}

// static
gfx::NativeView NativeWidgetPrivate::GetGlobalCapture(
    gfx::NativeView native_view) {
  return gfx::NativeView(NativeWidgetMacNSWindowHost::GetGlobalCaptureView());
}

}  // namespace internal
}  // namespace views