File: client_controlled_shell_surface.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (1511 lines) | stat: -rw-r--r-- 58,103 bytes parent folder | download
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
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/client_controlled_shell_surface.h"

#include <map>
#include <utility>

#include "ash/constants/ash_features.h"
#include "ash/frame/non_client_frame_view_ash.h"
#include "ash/frame/wide_frame_view.h"
#include "ash/public/cpp/arc_resize_lock_type.h"
#include "ash/public/cpp/ash_constants.h"
#include "ash/public/cpp/rounded_corner_utils.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/window_backdrop.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/root_window_controller.h"
#include "ash/screen_util.h"
#include "ash/shell.h"
#include "ash/wm/client_controlled_state.h"
#include "ash/wm/collision_detection/collision_detection_utils.h"
#include "ash/wm/drag_details.h"
#include "ash/wm/pip/pip_positioner.h"
#include "ash/wm/splitview/split_view_controller.h"
#include "ash/wm/toplevel_window_event_handler.h"
#include "ash/wm/window_positioning_utils.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/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "chromeos/ui/base/tablet_state.h"
#include "chromeos/ui/base/window_pin_type.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/base/window_state_type.h"
#include "chromeos/ui/frame/caption_buttons/caption_button_model.h"
#include "chromeos/ui/frame/default_frame_header.h"
#include "chromeos/ui/frame/header_view.h"
#include "chromeos/ui/frame/immersive/immersive_fullscreen_controller.h"
#include "components/exo/shell_surface_util.h"
#include "components/exo/surface.h"
#include "components/exo/wm_helper.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/scoped_window_event_targeting_blocker.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/class_property.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/compositor_lock.h"
#include "ui/compositor/layer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/display/tablet_state.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/core/window_util.h"

DEFINE_UI_CLASS_PROPERTY_TYPE(exo::ClientControlledShellSurface*)

namespace exo {

namespace {
using ::ash::screen_util::GetIdealBoundsForMaximizedOrFullscreenOrPinnedState;
using ::chromeos::WindowStateType;

// Client controlled specific accelerators.
const struct {
  ui::KeyboardCode keycode;
  int modifiers;
  ClientControlledAcceleratorAction action;
} kAccelerators[] = {
    {ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN,
     ClientControlledAcceleratorAction::ZOOM_OUT},
    {ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN,
     ClientControlledAcceleratorAction::ZOOM_IN},
    {ui::VKEY_0, ui::EF_CONTROL_DOWN,
     ClientControlledAcceleratorAction::ZOOM_RESET},
};

ClientControlledShellSurface::DelegateFactoryCallback& GetFactoryForTesting() {
  using CallbackType = ClientControlledShellSurface::DelegateFactoryCallback;
  static base::NoDestructor<CallbackType> factory;
  return *factory;
}

// Maximum amount of time to wait for contents that match the display's
// orientation in tablet mode.
// TODO(oshima): Looks like android is generating unnecessary frames.
// Fix it on Android side and reduce the timeout.
constexpr int kOrientationLockTimeoutMs = 2500;

Orientation SizeToOrientation(const gfx::Size& size) {
  DCHECK_NE(size.width(), size.height());
  return size.width() > size.height() ? Orientation::LANDSCAPE
                                      : Orientation::PORTRAIT;
}

// A ClientControlledStateDelegate that sends the state/bounds
// change request to exo client.
class ClientControlledStateDelegate
    : public ash::ClientControlledState::Delegate {
 public:
  explicit ClientControlledStateDelegate(
      ClientControlledShellSurface* shell_surface)
      : shell_surface_(shell_surface) {}

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

  ~ClientControlledStateDelegate() override {}

  // Overridden from ash::ClientControlledState::Delegate:
  void HandleWindowStateRequest(ash::WindowState* window_state,
                                chromeos::WindowStateType next_state) override {
    shell_surface_->OnWindowStateChangeEvent(window_state->GetStateType(),
                                             next_state);
  }
  void HandleBoundsRequest(ash::WindowState* window_state,
                           chromeos::WindowStateType requested_state,
                           const gfx::Rect& bounds_in_display,
                           int64_t display_id) override {
    shell_surface_->OnBoundsChangeEvent(
        window_state->GetStateType(), requested_state, display_id,
        bounds_in_display,
        window_state->drag_details() && shell_surface_->IsDragging()
            ? window_state->drag_details()->bounds_change
            : 0);
  }

 private:
  raw_ptr<ClientControlledShellSurface, ExperimentalAsh> shell_surface_;
};

// A WindowStateDelegate that implements ToggleFullscreen behavior for
// client controlled window.
class ClientControlledWindowStateDelegate : public ash::WindowStateDelegate {
 public:
  explicit ClientControlledWindowStateDelegate(
      ClientControlledShellSurface* shell_surface,
      ash::ClientControlledState::Delegate* delegate)
      : shell_surface_(shell_surface), delegate_(delegate) {}

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

  ~ClientControlledWindowStateDelegate() override {}

  // Overridden from ash::WindowStateDelegate:
  bool ToggleFullscreen(ash::WindowState* window_state) override {
    chromeos::WindowStateType next_state;
    aura::Window* window = window_state->window();
    switch (window_state->GetStateType()) {
      case chromeos::WindowStateType::kDefault:
      case chromeos::WindowStateType::kNormal:
        next_state = chromeos::WindowStateType::kFullscreen;
        break;
      case chromeos::WindowStateType::kMaximized:
        next_state = chromeos::WindowStateType::kFullscreen;
        break;
      case chromeos::WindowStateType::kFullscreen:
        switch (window->GetProperty(aura::client::kRestoreShowStateKey)) {
          case ui::SHOW_STATE_DEFAULT:
          case ui::SHOW_STATE_NORMAL:
            next_state = chromeos::WindowStateType::kNormal;
            break;
          case ui::SHOW_STATE_MAXIMIZED:
            next_state = chromeos::WindowStateType::kMaximized;
            break;
          case ui::SHOW_STATE_MINIMIZED:
            next_state = chromeos::WindowStateType::kMinimized;
            break;
          case ui::SHOW_STATE_FULLSCREEN:
          case ui::SHOW_STATE_INACTIVE:
          case ui::SHOW_STATE_END:
            NOTREACHED() << " unknown state :"
                         << window->GetProperty(
                                aura::client::kRestoreShowStateKey);
            return false;
        }
        break;
      case chromeos::WindowStateType::kMinimized: {
        next_state = chromeos::WindowStateType::kFullscreen;
        break;
      }
      default:
        // TODO(oshima|xdai): Handle SNAP state.
        return false;
    }
    delegate_->HandleWindowStateRequest(window_state, next_state);
    return true;
  }

  void ToggleLockedFullscreen(ash::WindowState*) override {
    // No special handling for locked ARC windows.
    return;
  }

  std::unique_ptr<ash::PresentationTimeRecorder> OnDragStarted(
      int component) override {
    shell_surface_->OnDragStarted(component);
    return nullptr;
  }

  void OnDragFinished(bool canceled, const gfx::PointF& location) override {
    shell_surface_->OnDragFinished(canceled, location);
  }

 private:
  raw_ptr<ClientControlledShellSurface, ExperimentalAsh> shell_surface_;
  raw_ptr<ash::ClientControlledState::Delegate,
          DanglingUntriaged | ExperimentalAsh>
      delegate_;
};

bool IsPinned(const ash::WindowState* window_state) {
  return window_state->IsPinned() || window_state->IsTrustedPinned();
}

class CaptionButtonModel : public chromeos::CaptionButtonModel {
 public:
  CaptionButtonModel(uint32_t visible_button_mask, uint32_t enabled_button_mask)
      : visible_button_mask_(visible_button_mask),
        enabled_button_mask_(enabled_button_mask) {}

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

  // Overridden from ash::CaptionButtonModel:
  bool IsVisible(views::CaptionButtonIcon icon) const override {
    return visible_button_mask_ & (1 << icon);
  }
  bool IsEnabled(views::CaptionButtonIcon icon) const override {
    return enabled_button_mask_ & (1 << icon);
  }
  bool InZoomMode() const override {
    return visible_button_mask_ & (1 << views::CAPTION_BUTTON_ICON_ZOOM);
  }

 private:
  uint32_t visible_button_mask_;
  uint32_t enabled_button_mask_;
};

// EventTargetingBlocker blocks the event targeting by setting NONE targeting
// policy to the window subtrees. It resets to the original policy upon
// deletion.
class EventTargetingBlocker : aura::WindowObserver {
 public:
  EventTargetingBlocker() = default;

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

  ~EventTargetingBlocker() override {
    if (window_)
      Unregister(window_);
  }

  void Block(aura::Window* window) {
    window_ = window;
    Register(window);
  }

 private:
  void Register(aura::Window* window) {
    window->AddObserver(this);
    event_targeting_blocker_map_[window] =
        std::make_unique<aura::ScopedWindowEventTargetingBlocker>(window);
    for (auto* child : window->children())
      Register(child);
  }

  void Unregister(aura::Window* window) {
    window->RemoveObserver(this);
    event_targeting_blocker_map_.erase(window);
    for (auto* child : window->children())
      Unregister(child);
  }

  void OnWindowDestroying(aura::Window* window) override {
    Unregister(window);
    if (window_ == window)
      window_ = nullptr;
  }

  std::map<aura::Window*,
           std::unique_ptr<aura::ScopedWindowEventTargetingBlocker>>
      event_targeting_blocker_map_;
  raw_ptr<aura::Window, ExperimentalAsh> window_ = nullptr;
};

}  // namespace

class ClientControlledShellSurface::ScopedSetBoundsLocally {
 public:
  explicit ScopedSetBoundsLocally(ClientControlledShellSurface* shell_surface)
      : state_(shell_surface->client_controlled_state_) {
    state_->set_bounds_locally(true);
  }

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

  ~ScopedSetBoundsLocally() { state_->set_bounds_locally(false); }

 private:
  const raw_ptr<ash::ClientControlledState, ExperimentalAsh> state_;
};

class ClientControlledShellSurface::ScopedLockedToRoot {
 public:
  explicit ScopedLockedToRoot(views::Widget* widget)
      : window_(widget->GetNativeWindow()) {
    window_->SetProperty(ash::kLockedToRootKey, true);
  }

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

  ~ScopedLockedToRoot() { window_->ClearProperty(ash::kLockedToRootKey); }

 private:
  const raw_ptr<aura::Window, ExperimentalAsh> window_;
};

////////////////////////////////////////////////////////////////////////////////
// ClientControlledShellSurface, public:

ClientControlledShellSurface::ClientControlledShellSurface(
    Surface* surface,
    bool can_minimize,
    int container,
    bool default_scale_cancellation,
    bool supports_floated_state)
    : ShellSurfaceBase(surface, gfx::Point(), can_minimize, container),
      use_default_scale_cancellation_(default_scale_cancellation),
      supports_floated_state_(supports_floated_state) {
  server_side_resize_ = true;
  set_client_submits_surfaces_in_pixel_coordinates(true);
}

ClientControlledShellSurface::~ClientControlledShellSurface() {
  // Reset the window delegate here so that we won't try to do any dragging
  // operation on a to-be-destroyed window. |widget_| can be nullptr in tests.
  if (GetWidget())
    GetWindowState()->SetDelegate(nullptr);
  if (client_controlled_state_)
    client_controlled_state_->ResetDelegate();
  wide_frame_.reset();
}

void ClientControlledShellSurface::SetBounds(int64_t display_id,
                                             const gfx::Rect& bounds) {
  TRACE_EVENT2("exo", "ClientControlledShellSurface::SetBounds", "display_id",
               display_id, "bounds", bounds.ToString());

  if (bounds.IsEmpty()) {
    DLOG(WARNING) << "Bounds must be non-empty";
    return;
  }

  SetDisplay(display_id);

  const gfx::Rect bounds_dp =
      gfx::ScaleToRoundedRect(bounds, GetClientToDpPendingScale());
  SetGeometry(bounds_dp);
}

void ClientControlledShellSurface::SetBoundsOrigin(int64_t display_id,
                                                   const gfx::Point& origin) {
  TRACE_EVENT2("exo", "ClientControlledShellSurface::SetBoundsOrigin",
               "display_id", display_id, "origin", origin.ToString());
  SetDisplay(display_id);
  const gfx::Point origin_dp =
      gfx::ScaleToRoundedPoint(origin, GetClientToDpPendingScale());
  pending_geometry_.set_origin(origin_dp);
}

void ClientControlledShellSurface::SetBoundsSize(const gfx::Size& size) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetBoundsSize", "size",
               size.ToString());

  if (size.IsEmpty()) {
    DLOG(WARNING) << "Bounds size must be non-empty";
    return;
  }

  const gfx::Size size_dp =
      gfx::ScaleToRoundedSize(size, GetClientToDpPendingScale());
  pending_geometry_.set_size(size_dp);
}

void ClientControlledShellSurface::SetMaximized() {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetMaximized");
  pending_window_state_ = chromeos::WindowStateType::kMaximized;
}

void ClientControlledShellSurface::SetMinimized() {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetMinimized");
  pending_window_state_ = chromeos::WindowStateType::kMinimized;
}

void ClientControlledShellSurface::SetRestored() {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetRestored");
  pending_window_state_ = chromeos::WindowStateType::kNormal;
}

void ClientControlledShellSurface::SetFullscreen(bool fullscreen,
                                                 int64_t display_id) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetFullscreen",
               "fullscreen", fullscreen);
  pending_window_state_ = fullscreen ? chromeos::WindowStateType::kFullscreen
                                     : chromeos::WindowStateType::kNormal;
  // TODO(crbug/1478300): `display_id` might need to be used here somewhere.
}

void ClientControlledShellSurface::SetPinned(chromeos::WindowPinType type) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetPinned", "type",
               static_cast<int>(type));

  if (!widget_)
    CreateShellSurfaceWidget(ui::SHOW_STATE_NORMAL);

  if (type == chromeos::WindowPinType::kNone) {
    // Set other window state mode will automatically cancelled pin mode.
    // TODO: Add NOTREACH() here after ARC side integration fully landed.
  } else {
    bool trusted = type == chromeos::WindowPinType::kTrustedPinned;
    pending_window_state_ = trusted ? chromeos::WindowStateType::kTrustedPinned
                                    : chromeos::WindowStateType::kPinned;
  }
}

void ClientControlledShellSurface::SetSystemUiVisibility(bool autohide) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetSystemUiVisibility",
               "autohide", autohide);

  if (!widget_)
    CreateShellSurfaceWidget(ui::SHOW_STATE_NORMAL);

  ash::window_util::SetAutoHideShelf(widget_->GetNativeWindow(), autohide);
}

void ClientControlledShellSurface::SetAlwaysOnTop(bool always_on_top) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetAlwaysOnTop",
               "always_on_top", always_on_top);
  pending_always_on_top_ = always_on_top;
}

void ClientControlledShellSurface::SetOrientation(Orientation orientation) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetOrientation",
               "orientation",
               orientation == Orientation::PORTRAIT ? "portrait" : "landscape");
  pending_orientation_ = orientation;
}

void ClientControlledShellSurface::SetShadowBounds(const gfx::Rect& bounds) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetShadowBounds", "bounds",
               bounds.ToString());
  auto shadow_bounds =
      bounds.IsEmpty() ? absl::nullopt : absl::make_optional(bounds);
  if (shadow_bounds_ != shadow_bounds) {
    shadow_bounds_ = shadow_bounds;
    shadow_bounds_changed_ = true;
  }
}

void ClientControlledShellSurface::OnWindowStateChangeEvent(
    chromeos::WindowStateType current_state,
    chromeos::WindowStateType next_state) {
  // Android already knows this state change. Don't send state change to Android
  // that it is about to do anyway.
  if (delegate_ && pending_window_state_ != next_state)
    delegate_->OnStateChanged(current_state, next_state);
}

void ClientControlledShellSurface::StartDrag(int component,
                                             const gfx::PointF& location) {
  TRACE_EVENT2("exo", "ClientControlledShellSurface::StartDrag", "component",
               component, "location", location.ToString());

  if (!widget_)
    return;
  AttemptToStartDrag(component, location);
}

void ClientControlledShellSurface::AttemptToStartDrag(
    int component,
    const gfx::PointF& location) {
  aura::Window* target = widget_->GetNativeWindow();
  ash::ToplevelWindowEventHandler* toplevel_handler =
      ash::Shell::Get()->toplevel_window_event_handler();
  aura::Window* mouse_pressed_handler =
      target->GetHost()->dispatcher()->mouse_pressed_handler();
  // Start dragging only if:
  // 1) touch guesture is in progress or
  // 2) mouse was pressed on the target or its subsurfaces.
  // If neither condition is met, we do not start the drag.
  gfx::PointF point_in_root;
  if (toplevel_handler->gesture_target()) {
    point_in_root = toplevel_handler->event_location_in_gesture_target();
    aura::Window::ConvertPointToTarget(
        toplevel_handler->gesture_target(),
        widget_->GetNativeWindow()->GetRootWindow(), &point_in_root);
  } else if (mouse_pressed_handler && target->Contains(mouse_pressed_handler)) {
    point_in_root = location;
    if (use_default_scale_cancellation_) {
      // When default scale cancellation is enabled, the client sends the
      // location in screen coordinates. Otherwise, the location should already
      // be in the display's coordinates.
      wm::ConvertPointFromScreen(target->GetRootWindow(), &point_in_root);
    }
  } else {
    return;
  }
  toplevel_handler->AttemptToStartDrag(
      target, point_in_root, component,
      ash::ToplevelWindowEventHandler::EndClosure());
}

bool ClientControlledShellSurface::IsDragging() {
  return in_drag_;
}

void ClientControlledShellSurface::SetCanMaximize(bool can_maximize) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetCanMaximize",
               "can_maximzie", can_maximize);
  can_maximize_ = can_maximize;
  if (widget_)
    widget_->OnSizeConstraintsChanged();
}

void ClientControlledShellSurface::UpdateAutoHideFrame() {
  if (immersive_fullscreen_controller_) {
    bool enabled = (frame_type_ == SurfaceFrameType::AUTOHIDE &&
                    (GetWindowState()->IsMaximizedOrFullscreenOrPinned() ||
                     GetWindowState()->IsSnapped()));
    chromeos::ImmersiveFullscreenController::EnableForWidget(widget_, enabled);
  }
}

void ClientControlledShellSurface::SetFrameButtons(
    uint32_t visible_button_mask,
    uint32_t enabled_button_mask) {
  if (frame_visible_button_mask_ == visible_button_mask &&
      frame_enabled_button_mask_ == enabled_button_mask) {
    return;
  }
  frame_visible_button_mask_ = visible_button_mask;
  frame_enabled_button_mask_ = enabled_button_mask;

  if (widget_)
    UpdateCaptionButtonModel();
}

void ClientControlledShellSurface::SetExtraTitle(
    const std::u16string& extra_title) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetExtraTitle",
               "extra_title", base::UTF16ToUTF8(extra_title));

  if (!widget_) {
    initial_extra_title_ = extra_title;
    return;
  }

  GetFrameView()->GetHeaderView()->GetFrameHeader()->SetFrameTextOverride(
      extra_title);
  if (wide_frame_) {
    wide_frame_->header_view()->GetFrameHeader()->SetFrameTextOverride(
        extra_title);
  }
}

void ClientControlledShellSurface::RebindRootSurface(
    Surface* root_surface,
    bool can_minimize,
    int container,
    bool default_scale_cancellation) {
  use_default_scale_cancellation_ = default_scale_cancellation;
  ShellSurfaceBase::RebindRootSurface(root_surface, can_minimize, container);
}

void ClientControlledShellSurface::DidReceiveCompositorFrameAck() {
  orientation_ = pending_orientation_;
  // Unlock the compositor after the frame is received by viz so that
  // screenshot contain the correct frame.
  if (expected_orientation_ == orientation_)
    orientation_compositor_lock_.reset();
  SurfaceTreeHost::DidReceiveCompositorFrameAck();
}

void ClientControlledShellSurface::OnBoundsChangeEvent(
    chromeos::WindowStateType current_state,
    chromeos::WindowStateType requested_state,
    int64_t display_id,
    const gfx::Rect& window_bounds,
    int bounds_change) {
  if (ignore_bounds_change_request_)
    return;

  // 1) Do no update the bounds unless we have geometry from client.
  // 2) Do not update the bounds if window is minimized unless it
  // exiting the minimzied state.
  // The bounds will be provided by client when unminimized.
  if (geometry().IsEmpty() || window_bounds.IsEmpty() ||
      (widget_->IsMinimized() &&
       requested_state == chromeos::WindowStateType::kMinimized) ||
      !delegate_) {
    return;
  }

  // Sends the client bounds, which matches the geometry
  // when frame is enabled.
  const gfx::Rect client_bounds = GetClientBoundsForWindowBoundsAndWindowState(
      window_bounds, requested_state);

  gfx::Size current_size = GetFrameView()->GetBoundsForClientView().size();
  bool is_resize = client_bounds.size() != current_size &&
                   !widget_->IsMaximized() && !widget_->IsFullscreen();

  // Make sure to use the up-to-date scale factor.
  display::Display display;
  const bool display_exists =
      display::Screen::GetScreen()->GetDisplayWithDisplayId(display_id,
                                                            &display);
  DCHECK(display_exists && display.is_valid());
  const float scale =
      use_default_scale_cancellation_ ? 1.f : display.device_scale_factor();
  const gfx::Rect scaled_client_bounds =
      gfx::ScaleToRoundedRect(client_bounds, scale);
  delegate_->OnBoundsChanged(current_state, requested_state, display_id,
                             scaled_client_bounds, is_resize, bounds_change);

  auto* window_state = GetWindowState();
  if (server_reparent_window_ &&
      window_state->GetDisplay().id() != display_id) {
    ScopedSetBoundsLocally scoped_set_bounds(this);
    int container_id = window_state->window()->parent()->GetId();
    aura::Window* new_parent =
        ash::Shell::GetRootWindowControllerWithDisplayId(display_id)
            ->GetContainer(container_id);
    new_parent->AddChild(window_state->window());
  }
}

void ClientControlledShellSurface::ChangeZoomLevel(ZoomChange change) {
  if (delegate_)
    delegate_->OnZoomLevelChanged(change);
}

void ClientControlledShellSurface::OnDragStarted(int component) {
  in_drag_ = true;
  if (delegate_)
    delegate_->OnDragStarted(component);
}

void ClientControlledShellSurface::OnDragFinished(bool canceled,
                                                  const gfx::PointF& location) {
  in_drag_ = false;
  if (!delegate_)
    return;

  const float scale = 1.f / GetClientToDpScale();
  const gfx::PointF scaled = gfx::ScalePoint(location, scale);
  delegate_->OnDragFinished(scaled.x(), scaled.y(), canceled);
}

float ClientControlledShellSurface::GetClientToDpScale() const {
  // If the default_device_scale_factor is used for scale cancellation,
  // we expect the client will already send bounds in DP.
  if (use_default_scale_cancellation_)
    return 1.f;
  return 1.f / GetScale();
}

void ClientControlledShellSurface::SetResizeLockType(
    ash::ArcResizeLockType resize_lock_type) {
  TRACE_EVENT1("exo", "ClientControlledShellSurface::SetResizeLockType",
               "resize_lock_type", resize_lock_type);
  pending_resize_lock_type_ = resize_lock_type;
}

void ClientControlledShellSurface::UpdateResizability() {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::updateCanResize");
  widget_->GetNativeWindow()->SetProperty(ash::kArcResizeLockTypeKey,
                                          pending_resize_lock_type_);
  // If resize lock is enabled, the window is explicitly marded as unresizable.
  // Otherwise, the decision is deferred to the parent class.
  if (pending_resize_lock_type_ ==
           ash::ArcResizeLockType::RESIZE_DISABLED_TOGGLABLE ||
       pending_resize_lock_type_ ==
           ash::ArcResizeLockType::RESIZE_DISABLED_NONTOGGLABLE) {
    SetCanResize(false);
    return;
  }
  ShellSurfaceBase::UpdateResizability();
}

////////////////////////////////////////////////////////////////////////////////
// SurfaceDelegate overrides:

bool ClientControlledShellSurface::IsInputEnabled(Surface* surface) const {
  // Client-driven dragging/resizing relies on implicit grab, which ensures that
  // mouse/touch events are delivered to the focused surface until release, even
  // if they fall outside surface bounds. However, if the client destroys the
  // surface with implicit grab, the drag/resize is prematurely ended. Prevent
  // this by delivering all input events to the root surface, which shares the
  // lifetime of the shell surface.
  // TODO(domlaskowski): Remove once the client is provided with an API to hook
  // into server-driven dragging/resizing.
  return surface == root_surface();
}

void ClientControlledShellSurface::OnSetFrame(SurfaceFrameType type) {
  pending_frame_type_ = type;
}

void ClientControlledShellSurface::OnSetFrameColors(SkColor active_color,
                                                    SkColor inactive_color) {
  ShellSurfaceBase::OnSetFrameColors(active_color, inactive_color);
  if (wide_frame_) {
    aura::Window* window = wide_frame_->GetWidget()->GetNativeWindow();
    window->SetProperty(chromeos::kTrackDefaultFrameColors, false);
    window->SetProperty(chromeos::kFrameActiveColorKey, active_color);
    window->SetProperty(chromeos::kFrameInactiveColorKey, inactive_color);
  }
}

void ClientControlledShellSurface::SetSnapPrimary(float snap_ratio) {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetSnappedToPrimary");
  pending_window_state_ = chromeos::WindowStateType::kPrimarySnapped;
}

void ClientControlledShellSurface::SetSnapSecondary(float snap_ratio) {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetSnappedToSecondary");
  pending_window_state_ = chromeos::WindowStateType::kSecondarySnapped;
}

void ClientControlledShellSurface::SetPip() {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetPip");
  pending_window_state_ = chromeos::WindowStateType::kPip;
}

void ClientControlledShellSurface::UnsetPip() {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::UnsetPip");
  SetRestored();
}

void ClientControlledShellSurface::SetFloatToLocation(
    chromeos::FloatStartLocation float_start_location) {
  TRACE_EVENT0("exo", "ClientControlledShellSurface::SetFloatToLocation");
  pending_window_state_ = chromeos::WindowStateType::kFloated;
}

////////////////////////////////////////////////////////////////////////////////
// aura::WindowObserver overrides:
void ClientControlledShellSurface::OnWindowDestroying(aura::Window* window) {
  if (client_controlled_state_) {
    client_controlled_state_->ResetDelegate();
    client_controlled_state_ = nullptr;
  }
  ShellSurfaceBase::OnWindowDestroying(window);
}

void ClientControlledShellSurface::OnWindowAddedToRootWindow(
    aura::Window* window) {
  // Window dragging across display moves the window to target display when
  // dropped, but the actual window bounds comes later from android.  Update the
  // window bounds now so that the window stays where it is expected to be. (it
  // may still move if the android sends different bounds).
  if (client_controlled_state_->set_bounds_locally() ||
      !GetWindowState()->is_dragged()) {
    return;
  }

  ScopedLockedToRoot scoped_locked_to_root(widget_);
  UpdateWidgetBounds();
}

////////////////////////////////////////////////////////////////////////////////
// views::WidgetDelegate overrides:

bool ClientControlledShellSurface::CanMaximize() const {
  return can_maximize_;
}

std::unique_ptr<views::NonClientFrameView>
ClientControlledShellSurface::CreateNonClientFrameView(views::Widget* widget) {
  ash::WindowState* window_state = GetWindowState();
  std::unique_ptr<ash::ClientControlledState::Delegate> delegate =
      GetFactoryForTesting()
          ? GetFactoryForTesting().Run()
          : std::make_unique<ClientControlledStateDelegate>(this);

  auto window_delegate = std::make_unique<ClientControlledWindowStateDelegate>(
      this, delegate.get());
  auto state =
      std::make_unique<ash::ClientControlledState>(std::move(delegate));
  client_controlled_state_ = state.get();
  window_state->SetStateObject(std::move(state));
  window_state->SetDelegate(std::move(window_delegate));
  auto frame_view = CreateNonClientFrameViewInternal(widget);
  immersive_fullscreen_controller_ =
      std::make_unique<chromeos::ImmersiveFullscreenController>();
  static_cast<ash::NonClientFrameViewAsh*>(frame_view.get())
      ->InitImmersiveFullscreenControllerForView(
          immersive_fullscreen_controller_.get());
  return frame_view;
}

bool ClientControlledShellSurface::ShouldSaveWindowPlacement() const {
  return false;
}

void ClientControlledShellSurface::SaveWindowPlacement(
    const gfx::Rect& bounds,
    ui::WindowShowState show_state) {}

bool ClientControlledShellSurface::GetSavedWindowPlacement(
    const views::Widget* widget,
    gfx::Rect* bounds,
    ui::WindowShowState* show_state) const {
  return false;
}

////////////////////////////////////////////////////////////////////////////////
// views::View overrides:

gfx::Size ClientControlledShellSurface::GetMaximumSize() const {
  if (can_maximize_) {
    // On ChromeOS, a window with non empty maximum size is non-maximizable,
    // even if CanMaximize() returns true. ClientControlledShellSurface
    // sololy depends on |can_maximize_| to determine if it is maximizable,
    // so just return empty size.
    return gfx::Size();
  } else {
    return ShellSurfaceBase::GetMaximumSize();
  }
}

void ClientControlledShellSurface::OnDeviceScaleFactorChanged(float old_dsf,
                                                              float new_dsf) {
  views::View::OnDeviceScaleFactorChanged(old_dsf, new_dsf);

  UpdateFrameWidth();
}

////////////////////////////////////////////////////////////////////////////////
// display::DisplayObserver overrides:

void ClientControlledShellSurface::OnDisplayMetricsChanged(
    const display::Display& new_display,
    uint32_t changed_metrics) {
  SurfaceTreeHost::OnDisplayMetricsChanged(new_display, changed_metrics);

  if (!widget_)
    return;

  // The PIP window bounds is adjusted in Ash when the screen is rotated, but
  // Android has an obsolete bounds for a while and applies it incorrectly.
  // We need to ignore those bounds change until the states are completely
  // synced on both sides.
  if (GetWindowState()->IsPip() &&
      changed_metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION) {
    gfx::Rect bounds_after_rotation =
        ash::PipPositioner::GetSnapFractionAppliedBounds(GetWindowState());
    display_rotating_with_pip_ =
        bounds_after_rotation !=
        GetWindowState()->window()->GetBoundsInScreen();
  }

  const display::Screen* screen = display::Screen::GetScreen();
  display::Display current_display =
      screen->GetDisplayNearestWindow(widget_->GetNativeWindow());
  if (current_display.id() != new_display.id())
    return;

  bool in_tablet_mode = WMHelper::GetInstance()->InTabletMode();

  if (!in_tablet_mode || !widget_->IsActive() ||
      !(changed_metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION)) {
    return;
  }

  Orientation target_orientation = SizeToOrientation(new_display.size());
  if (orientation_ == target_orientation)
    return;
  expected_orientation_ = target_orientation;
  EnsureCompositorIsLockedForOrientationChange();
}

////////////////////////////////////////////////////////////////////////////////
// ui::CompositorLockClient overrides:

void ClientControlledShellSurface::CompositorLockTimedOut() {
  orientation_compositor_lock_.reset();
}

////////////////////////////////////////////////////////////////////////////////
// ShellSurfaceBase overrides:

void ClientControlledShellSurface::SetSystemModal(bool system_modal) {
  // System modal container is used by clients to implement client side
  // managed system modal dialogs using a single ShellSurface instance.
  // Hit-test region will be non-empty when at least one dialog exists on
  // the client side. Here we detect the transition between no client side
  // dialog and at least one dialog so activatable state is properly
  // updated.
  if (container_ != ash::kShellWindowId_SystemModalContainer) {
    LOG(ERROR)
        << "Only a window in SystemModalContainer can change the modality";
    return;
  }

  ShellSurfaceBase::SetSystemModal(system_modal);
}

void ClientControlledShellSurface::SetWidgetBounds(const gfx::Rect& bounds,
                                                   bool adjusted_by_server) {
  set_bounds_is_dirty(true);
  const auto* screen = display::Screen::GetScreen();
  aura::Window* window = widget_->GetNativeWindow();
  display::Display current_display = screen->GetDisplayNearestWindow(window);

  bool is_display_move_pending = false;
  display::Display target_display = current_display;

  display::Display display;
  if (screen->GetDisplayWithDisplayId(display_id_, &display)) {
    bool is_display_stale = display_id_ != current_display.id();

    // Preserve widget bounds until client acknowledges display move.
    if (preserve_widget_bounds_ && is_display_stale)
      return;

    // True if the window has just been reparented to another root window, and
    // the move was initiated by the server.
    // TODO(oshima): Improve the window moving logic. https://crbug.com/875047
    is_display_move_pending =
        window->GetProperty(ash::kLockedToRootKey) && is_display_stale;

    if (!is_display_move_pending)
      target_display = display;

    preserve_widget_bounds_ = is_display_move_pending;
  } else {
    preserve_widget_bounds_ = false;
  }

  // Calculate a minimum window visibility required bounds.
  // TODO(oshima): Move this to ComputeAdjustedBounds.
  gfx::Rect adjusted_bounds = bounds;
  if (!is_display_move_pending) {
    const gfx::Rect& restriction = GetWindowState()->IsFullscreen()
                                       ? target_display.bounds()
                                       : target_display.work_area();
    ash::ClientControlledState::AdjustBoundsForMinimumWindowVisibility(
        restriction, &adjusted_bounds);
    // Collision detection to the bounds set by Android should be applied only
    // to initial bounds and any client-requested bounds (I.E. Double-Tap to
    // resize). Do not adjust new bounds for fling/display rotation as it can be
    // obsolete or in transit during animation, which results in incorrect
    // resting postiion. The resting position should be fully controlled by
    // chrome afterwards because Android isn't aware of Chrome OS System UI.
    bool is_resizing_without_rotation =
        !display_rotating_with_pip_ &&
        GetWindowState()->GetCurrentBoundsInScreen().size() != bounds.size();
    if (GetWindowState()->IsPip() &&
        (!ash::PipPositioner::HasSnapFraction(GetWindowState()) ||
         is_resizing_without_rotation)) {
      adjusted_bounds = ash::CollisionDetectionUtils::GetRestingPosition(
          target_display, adjusted_bounds,
          ash::CollisionDetectionUtils::RelativePriority::kPictureInPicture);

      // Only if the window is resizing with a double tap, the bounds should
      // be applied via a scaling animation. Position changes will be applied
      // via kAnimate.
      if (is_resizing_without_rotation && !IsDragging()) {
        client_controlled_state_->set_next_bounds_change_animation_type(
            ash::WindowState::BoundsChangeAnimationType::kCrossFade);
      }
    }
  }

  if (adjusted_bounds == widget_->GetWindowBoundsInScreen() &&
      target_display.id() == current_display.id()) {
    return;
  }

  bool set_bounds_locally =
      display_rotating_with_pip_ ||
      (GetWindowState()->is_dragged() && !is_display_move_pending);

  if (set_bounds_locally || client_controlled_state_->set_bounds_locally()) {
    // Convert from screen to display coordinates.
    gfx::Point origin = bounds.origin();
    wm::ConvertPointFromScreen(window->parent(), &origin);

    // Move the window relative to the current display.
    {
      ScopedSetBoundsLocally scoped_set_bounds(this);
      window->SetBounds(gfx::Rect(origin, adjusted_bounds.size()));
    }
    UpdateHostWindowOrigin();
    return;
  }

  {
    ScopedSetBoundsLocally scoped_set_bounds(this);
    window->SetBoundsInScreen(adjusted_bounds, target_display);
  }

  if (bounds != adjusted_bounds || is_display_move_pending) {
    // Notify client that bounds were adjusted or window moved across displays.
    auto state_type = GetWindowState()->GetStateType();
    gfx::Rect adjusted_bounds_in_display(adjusted_bounds);

    adjusted_bounds_in_display.Offset(
        -target_display.bounds().OffsetFromOrigin());

    OnBoundsChangeEvent(state_type, state_type, target_display.id(),
                        adjusted_bounds_in_display, 0);
  }

  UpdateHostWindowOrigin();
}
gfx::Rect ClientControlledShellSurface::GetVisibleBounds() const {
  const auto* screen = display::Screen::GetScreen();
  display::Display display;

  if (geometry_.IsEmpty() ||
      !screen->GetDisplayWithDisplayId(display_id_, &display)) {
    return ShellSurfaceBase::GetVisibleBounds();
  }
  // ARC sends geometry_ in screen coordinates.
  return geometry_ + display.bounds().OffsetFromOrigin();
}

gfx::Rect ClientControlledShellSurface::GetShadowBounds() const {
  gfx::Rect shadow_bounds = ShellSurfaceBase::GetShadowBounds();
  const ash::NonClientFrameViewAsh* frame_view = GetFrameView();
  if (frame_view->GetFrameEnabled() && !shadow_bounds_->IsEmpty() &&
      !geometry_.IsEmpty() && !frame_view->GetFrameOverlapped()) {
    // The client controlled geometry is only for the client
    // area. When the chrome side frame is enabled, the shadow height
    // has to include the height of the frame, and the total height is
    // equals to the window height computed by
    // |GetWindowBoundsForClientBounds|.
    // But when the frame is overlapped with the client area, shadow bounds
    // should be the same as the client area bounds.
    shadow_bounds.set_size(
        frame_view->GetWindowBoundsForClientBounds(shadow_bounds).size());
  }

  return shadow_bounds;
}

void ClientControlledShellSurface::InitializeWindowState(
    ash::WindowState* window_state) {
  // Allow the client to request bounds that do not fill the entire work area
  // when maximized, or the entire display when fullscreen.
  window_state->set_allow_set_bounds_direct(true);
  window_state->set_ignore_keyboard_bounds_change(true);
  if (container_ == ash::kShellWindowId_SystemModalContainer ||
      container_ == ash::kShellWindowId_ArcVirtualKeyboardContainer) {
    DisableMovement();
  }
  ash::NonClientFrameViewAsh* frame_view = GetFrameView();
  frame_view->SetCaptionButtonModel(std::make_unique<CaptionButtonModel>(
      frame_visible_button_mask_, frame_enabled_button_mask_));
  UpdateAutoHideFrame();
  UpdateFrameWidth();
  if (initial_orientation_lock_ != chromeos::OrientationType::kAny)
    SetOrientationLock(initial_orientation_lock_);
  if (initial_extra_title_ != std::u16string())
    SetExtraTitle(initial_extra_title_);

  // Register Client controlled accelerators.
  views::FocusManager* focus_manager = widget_->GetFocusManager();
  accelerator_target_ =
      std::make_unique<ClientControlledAcceleratorTarget>(this);

  // These shortcuts are same as ones used in chrome.
  // TODO: investigate if we need to reassign.
  for (const auto& entry : kAccelerators) {
    focus_manager->RegisterAccelerator(
        ui::Accelerator(entry.keycode, entry.modifiers),
        ui::AcceleratorManager::kNormalPriority, accelerator_target_.get());
    accelerator_target_->RegisterAccelerator(
        ui::Accelerator(entry.keycode, entry.modifiers), entry.action);
  }

  auto* window = widget_->GetNativeWindow();
  GrantPermissionToActivateIndefinitely(window);

  window->SetProperty(chromeos::kSupportsFloatedStateKey,
                      supports_floated_state_);
}

float ClientControlledShellSurface::GetScale() const {
  return !use_default_scale_cancellation_
             ? ShellSurfaceBase::GetScaleFactor()
             : ::exo::GetDefaultDeviceScaleFactor();
}

float ClientControlledShellSurface::GetScaleFactor() const {
  // TODO(andreaorru): consolidate Scale and ScaleFactor.
  return GetScale();
}

absl::optional<gfx::Rect> ClientControlledShellSurface::GetWidgetBounds()
    const {
  const ash::NonClientFrameViewAsh* frame_view = GetFrameView();
  if (frame_view->GetFrameEnabled() && !frame_view->GetFrameOverlapped()) {
    gfx::Rect visible_bounds = GetVisibleBounds();
    if (widget_->IsMaximized() && frame_type_ == SurfaceFrameType::NORMAL) {
      // When the widget is maximized in clamshell mode, client sends
      // |geometry_| without taking caption height into account.
      visible_bounds.Offset(0, frame_view->NonClientTopBorderHeight());
    }
    return frame_view->GetWindowBoundsForClientBounds(visible_bounds);
  }

  // When frame is overlapped with the client window, widget bounds is the same
  // as the |geometry_| from client.
  return GetVisibleBounds();
}

gfx::Point ClientControlledShellSurface::GetSurfaceOrigin() const {
  return gfx::Point();
}

bool ClientControlledShellSurface::OnPreWidgetCommit() {
  if (!widget_) {
    // Modify the |origin_| to the |pending_geometry_| to place the window on
    // the intended display. See b/77472684 for details.
    // TODO(domlaskowski): Remove this once clients migrate to geometry API with
    // explicit target display.
    if (!pending_geometry_.IsEmpty())
      origin_ = pending_geometry_.origin();
    CreateShellSurfaceWidget(
        chromeos::ToWindowShowState(pending_window_state_));
  }

  // Finish ignoring obsolete bounds update as the state changes caused by
  // display rotation are synced.
  // TODO(takise): This assumes no other bounds update happens during screen
  // rotation. Implement more robust logic to handle synchronization for
  // screen rotation.
  if (pending_geometry_ != geometry_)
    display_rotating_with_pip_ = false;

  ash::WindowState* window_state = GetWindowState();
  state_changed_ = window_state->GetStateType() != pending_window_state_;
  if (!state_changed_) {
    // Animate PIP window movement unless it is being dragged.
    client_controlled_state_->set_next_bounds_change_animation_type(
        window_state->IsPip() && !window_state->is_dragged()
            ? ash::WindowState::BoundsChangeAnimationType::kAnimate
            : ash::WindowState::BoundsChangeAnimationType::kNone);
    return true;
  }

  if (IsPinned(window_state) &&
      (pending_window_state_ == chromeos::WindowStateType::kPinned ||
       pending_window_state_ == chromeos::WindowStateType::kTrustedPinned)) {
    VLOG(1) << "Pinned was requested while pinned";
    return true;
  }

  auto animation_type = ash::WindowState::BoundsChangeAnimationType::kNone;
  switch (pending_window_state_) {
    case chromeos::WindowStateType::kNormal:
      if (widget_->IsMaximized() || widget_->IsFullscreen()) {
        animation_type =
            ash::WindowState::BoundsChangeAnimationType::kCrossFade;
      }
      break;
    case chromeos::WindowStateType::kFloated:
      animation_type =
          ash::WindowState::BoundsChangeAnimationType::kCrossFadeFloat;
      break;
    case chromeos::WindowStateType::kMaximized:
    case chromeos::WindowStateType::kFullscreen:
      if (!window_state->IsPip())
        animation_type =
            ash::WindowState::BoundsChangeAnimationType::kCrossFade;
      break;
    default:
      break;
  }

  if (window_state->IsFloated()) {
    animation_type =
        ash::WindowState::BoundsChangeAnimationType::kCrossFadeUnfloat;
  }

  bool wasPip = window_state->IsPip();

  // As the bounds of the widget is updated later, ensure that no bounds change
  // happens with this state change (e.g. updatePipBounds can be triggered).
  base::AutoReset<bool> resetter(&ignore_bounds_change_request_, true);
  if (client_controlled_state_->EnterNextState(window_state,
                                               pending_window_state_)) {
    client_controlled_state_->set_next_bounds_change_animation_type(
        animation_type);
  }

  if (wasPip && !window_state->IsMinimized()) {
    // Expanding PIP should end tablet split view (see crbug.com/941788).
    // Clamshell split view does not require special handling. We activate the
    // PIP window, and so overview ends, which means clamshell split view ends.
    // TODO(edcourtney): Consider not ending tablet split view on PIP expand.
    // See crbug.com/950827.
    ash::SplitViewController* split_view_controller =
        ash::SplitViewController::Get(ash::Shell::GetPrimaryRootWindow());
    if (split_view_controller->InTabletSplitViewMode())
      split_view_controller->EndSplitView();
    // As Android doesn't activate PIP tasks after they are expanded, we need
    // to do it here explicitly.
    // TODO(937738): Investigate if we can activate PIP windows inside commit.
    window_state->Activate();
  }

  return true;
}

void ClientControlledShellSurface::OnPostWidgetCommit() {
  DCHECK(widget_);

  UpdateFrame();
  UpdateBackdrop();

  if (delegate_) {
    // Since the visible bounds are in screen coordinates, do not scale these
    // bounds with the display's scale before sending them.
    // TODO(b/167286795): Instead of sending bounds in screen coordinates, send
    // the bounds in the display along with the display information, similar to
    // the bounds_changed_callback_.
    delegate_->OnGeometryChanged(GetVisibleBounds());
  }

  // Apply new top inset height.
  if (pending_top_inset_height_ != top_inset_height_) {
    widget_->GetNativeWindow()->SetProperty(aura::client::kTopViewInset,
                                            pending_top_inset_height_);
    top_inset_height_ = pending_top_inset_height_;
  }

  widget_->GetNativeWindow()->SetProperty(aura::client::kZOrderingKey,
                                          pending_always_on_top_
                                              ? ui::ZOrderLevel::kFloatingWindow
                                              : ui::ZOrderLevel::kNormal);

  UpdateResizability();

  ash::WindowState* window_state = GetWindowState();
  // For PIP, the snap fraction is used to specify the ideal position. Usually
  // this value is set in CompleteDrag, but for the initial position, we need
  // to set it here, when the transition is completed.
  if (window_state->IsPip() &&
      !ash::PipPositioner::HasSnapFraction(window_state)) {
    ash::PipPositioner::SaveSnapFraction(
        window_state, window_state->window()->GetBoundsInScreen());
  }

  ShellSurfaceBase::OnPostWidgetCommit();
}

void ClientControlledShellSurface::OnSurfaceDestroying(Surface* surface) {
  if (client_controlled_state_) {
    client_controlled_state_->ResetDelegate();
    client_controlled_state_ = nullptr;
  }
  ShellSurfaceBase::OnSurfaceDestroying(surface);
}

////////////////////////////////////////////////////////////////////////////////
// ClientControlledShellSurface, private:

void ClientControlledShellSurface::UpdateFrame() {
  if (!widget_)
    return;
  ash::WindowState* window_state = GetWindowState();
  bool enable_wide_frame = false;
  if (GetFrameView()->GetFrameEnabled() &&
      window_state->IsMaximizedOrFullscreenOrPinned()) {
    gfx::Rect ideal_bounds =
        GetIdealBoundsForMaximizedOrFullscreenOrPinnedState(
            widget_->GetNativeWindow());
    enable_wide_frame = ideal_bounds.width() != geometry().width();
  }
  bool update_frame = state_changed_;
  state_changed_ = false;
  if (enable_wide_frame) {
    if (!wide_frame_) {
      update_frame = true;
      wide_frame_ = std::make_unique<ash::WideFrameView>(widget_);
      chromeos::ImmersiveFullscreenController::EnableForWidget(widget_, false);
      wide_frame_->Init(immersive_fullscreen_controller_.get());
      wide_frame_->header_view()->GetFrameHeader()->SetFrameTextOverride(
          GetFrameView()
              ->GetHeaderView()
              ->GetFrameHeader()
              ->frame_text_override());
      wide_frame_->GetWidget()->Show();

      // Restoring window targeter replaced by ImmersiveFullscreenController.
      InstallCustomWindowTargeter();

      UpdateCaptionButtonModel();
    }
    DCHECK_EQ(chromeos::FrameHeader::Get(widget_),
              wide_frame_->header_view()->GetFrameHeader());
  } else {
    if (wide_frame_) {
      update_frame = true;
      chromeos::ImmersiveFullscreenController::EnableForWidget(widget_, false);
      wide_frame_.reset();
      GetFrameView()->InitImmersiveFullscreenControllerForView(
          immersive_fullscreen_controller_.get());
      // Restoring window targeter replaced by ImmersiveFullscreenController.
      InstallCustomWindowTargeter();

      UpdateCaptionButtonModel();
    }
    DCHECK_EQ(chromeos::FrameHeader::Get(widget_),
              GetFrameView()->GetHeaderView()->GetFrameHeader());
    UpdateFrameWidth();
  }
  // The autohide should be applied when the window state is in
  // maximzied, fullscreen or pinned. Update the auto hide state
  // inside commit.
  if (update_frame)
    UpdateAutoHideFrame();
}

void ClientControlledShellSurface::UpdateCaptionButtonModel() {
  auto model = std::make_unique<CaptionButtonModel>(frame_visible_button_mask_,
                                                    frame_enabled_button_mask_);
  if (wide_frame_)
    wide_frame_->SetCaptionButtonModel(std::move(model));
  else
    GetFrameView()->SetCaptionButtonModel(std::move(model));
}

void ClientControlledShellSurface::UpdateBackdrop() {
  aura::Window* window = widget_->GetNativeWindow();

  // Always create a backdrop regardless of the geometry because
  // maximized/fullscreen widget's geometry can be cropped.
  bool enable_backdrop = widget_->IsFullscreen() || widget_->IsMaximized();

  ash::WindowBackdrop::BackdropMode target_backdrop_mode =
      enable_backdrop ? ash::WindowBackdrop::BackdropMode::kEnabled
                      : ash::WindowBackdrop::BackdropMode::kAuto;

  ash::WindowBackdrop* window_backdrop = ash::WindowBackdrop::Get(window);
  if (window_backdrop->mode() != target_backdrop_mode)
    window_backdrop->SetBackdropMode(target_backdrop_mode);
}

void ClientControlledShellSurface::UpdateFrameWidth() {
  int width = -1;
  if (shadow_bounds_) {
    float device_scale_factor =
        GetWidget()->GetNativeWindow()->layer()->device_scale_factor();
    float dsf_to_default_dsf = device_scale_factor / GetScale();
    width = base::ClampRound(shadow_bounds_->width() * dsf_to_default_dsf);
  }
  static_cast<chromeos::HeaderView*>(GetFrameView()->GetHeaderView())
      ->SetWidthInPixels(width);
}

void ClientControlledShellSurface::UpdateFrameType() {
  if (container_ == ash::kShellWindowId_SystemModalContainer &&
      pending_frame_type_ != SurfaceFrameType::NONE) {
    LOG(WARNING)
        << "A surface in system modal container should not have a frame:"
        << static_cast<int>(pending_frame_type_);
    return;
  }

  // TODO(oshima): We shouldn't send the synthesized motion event when just
  // changing the frame type. The better solution would be to keep the window
  // position regardless of the frame state, but that won't be available until
  // next arc version.
  // This is a stopgap solution not to generate the event until it is resolved.
  EventTargetingBlocker blocker;
  bool suppress_mouse_event = frame_type_ != pending_frame_type_ && widget_;
  if (suppress_mouse_event)
    blocker.Block(widget_->GetNativeWindow());
  ShellSurfaceBase::OnSetFrame(pending_frame_type_);
  UpdateAutoHideFrame();

  if (suppress_mouse_event)
    UpdateHostWindowOrigin();
}

bool ClientControlledShellSurface::GetCanResizeFromSizeConstraints() const {
  // Both min and max bounds of unresizable, maximized ARC windows are empty
  // because Ash requires maximizable apps have empty max bounds.
  // This assumes that ARC sets non-empty min sizes to all resizable apps.
  //
  // Example values of size constraints:
  // ----------------------------------------------------------------------
  // |           |          resizable           |      non-resizable      |
  // ----------------------------------------------------------------------
  // | freeform  | min: (400, 400), max: (0, 0) | min = max = window size |
  // ----------------------------------------------------------------------
  // | maximized | min: (400, 400), max: (0, 0) |   min = max = (0, 0)    |
  // ----------------------------------------------------------------------

  return minimum_size_ != maximum_size_;
}

void ClientControlledShellSurface::
    EnsureCompositorIsLockedForOrientationChange() {
  if (!orientation_compositor_lock_) {
    ui::Compositor* compositor =
        widget_->GetNativeWindow()->layer()->GetCompositor();
    orientation_compositor_lock_ = compositor->GetCompositorLock(
        this, base::Milliseconds(kOrientationLockTimeoutMs));
  }
}

ash::WindowState* ClientControlledShellSurface::GetWindowState() {
  return ash::WindowState::Get(widget_->GetNativeWindow());
}

ash::NonClientFrameViewAsh* ClientControlledShellSurface::GetFrameView() {
  return static_cast<ash::NonClientFrameViewAsh*>(
      widget_->non_client_view()->frame_view());
}

const ash::NonClientFrameViewAsh* ClientControlledShellSurface::GetFrameView()
    const {
  return static_cast<const ash::NonClientFrameViewAsh*>(
      widget_->non_client_view()->frame_view());
}

float ClientControlledShellSurface::GetClientToDpPendingScale() const {
  // When the client is scale-aware, we expect that it will resize windows when
  // reacting to scale changes. Since we do not commit the scale until the
  // buffer size changes, any bounds sent after a scale change and before the
  // scale commit will result in mismatched sizes between widget and the buffer.
  // To work around this, we use pending scale factor to calculate bounds in DP
  // instead of GetClientToDpScale().
  return use_default_scale_cancellation_ ? 1.f : 1.f / GetPendingScaleFactor();
}

gfx::Rect
ClientControlledShellSurface::GetClientBoundsForWindowBoundsAndWindowState(
    const gfx::Rect& window_bounds,
    chromeos::WindowStateType window_state) const {
  // The client's geometry uses fullscreen in client controlled,
  // (but the surface is placed under the frame), so just use
  // the window bounds instead for maximixed state.
  // Snapped window states in tablet mode do not include the caption height.
  const bool is_snapped =
      window_state == chromeos::WindowStateType::kPrimarySnapped ||
      window_state == chromeos::WindowStateType::kSecondarySnapped;
  const bool is_maximized =
      window_state == chromeos::WindowStateType::kMaximized;
  const display::TabletState tablet_state =
      chromeos::TabletState::Get()->state();
  const bool is_tablet_mode = WMHelper::GetInstance()->InTabletMode();

  if (is_maximized || (is_snapped && is_tablet_mode))
    return window_bounds;

  gfx::Rect client_bounds =
      GetFrameView()->GetFrameOverlapped()
          ? window_bounds
          : GetFrameView()->GetClientBoundsForWindowBounds(window_bounds);

  if (is_snapped && tablet_state == display::TabletState::kExitingTabletMode) {
    // Until the next commit, the frame view is in immersive mode, and the above
    // GetClientBoundsForWindowBounds doesn't return bounds taking the caption
    // height into account.
    client_bounds.Inset(gfx::Insets().set_top(
        GetFrameView()->NonClientTopBorderPreferredHeight()));
  }
  return client_bounds;
}

// static
void ClientControlledShellSurface::
    SetClientControlledStateDelegateFactoryForTest(
        const DelegateFactoryCallback& callback) {
  auto& factory = GetFactoryForTesting();
  factory = callback;
}

}  // namespace exo