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
ash / clipboard / clipboard_history_controller_impl.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/clipboard/clipboard_history_controller_impl.h"
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <vector>
#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/clipboard/clipboard_history_controller_delegate.h"
#include "ash/clipboard/clipboard_history_item.h"
#include "ash/clipboard/clipboard_history_menu_model_adapter.h"
#include "ash/clipboard/clipboard_history_resource_manager.h"
#include "ash/clipboard/clipboard_history_url_title_fetcher.h"
#include "ash/clipboard/clipboard_history_util.h"
#include "ash/clipboard/clipboard_nudge_constants.h"
#include "ash/clipboard/clipboard_nudge_controller.h"
#include "ash/clipboard/scoped_clipboard_history_pause_impl.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/display/display_util.h"
#include "ash/public/cpp/clipboard_image_model_factory.h"
#include "ash/public/cpp/window_tree_host_lookup.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/color_util.h"
#include "ash/wm/window_util.h"
#include "base/barrier_closure.h"
#include "base/check.h"
#include "base/check_is_test.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/json/values_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/notreached.h"
#include "base/one_shot_event.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/system/sys_info.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "chromeos/crosapi/mojom/clipboard_history.mojom.h"
#include "components/prefs/pref_service.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/clipboard/clipboard_data.h"
#include "ui/base/clipboard/clipboard_non_backed.h"
#include "ui/base/clipboard/clipboard_util.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/base/data_transfer_policy/data_transfer_endpoint.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/models/image_model.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/color/color_provider_source.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/controls/menu/menu_controller.h"
#if BUILDFLAG(USE_XKBCOMMON)
#include "ui/events/keycodes/xkb_keysym.h"
#include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
#include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
#endif
namespace ash {
namespace {
// Encodes `bitmap` and maps the corresponding ClipboardHistoryItem ID, `id, to
// the resulting PNG in `encoded_pngs`. This function should run on a background
// thread.
void EncodeBitmapToPNG(
base::OnceClosure barrier_callback,
std::map<base::UnguessableToken, std::vector<uint8_t>>* const encoded_pngs,
base::UnguessableToken id,
SkBitmap bitmap) {
auto png = ui::clipboard_util::EncodeBitmapToPng(bitmap);
// Don't acquire the lock until after the image encoding has finished.
static base::NoDestructor<base::Lock> map_lock;
base::AutoLock lock(*map_lock);
encoded_pngs->emplace(id, std::move(png));
std::move(barrier_callback).Run();
}
// Returns the clipboard instance for the current thread.
ui::ClipboardNonBacked* GetClipboard() {
auto* clipboard = ui::ClipboardNonBacked::GetForCurrentThread();
DCHECK(clipboard);
return clipboard;
}
// Returns the last active user pref service or `nullptr` if one does not exist.
PrefService* GetLastActiveUserPrefService() {
return Shell::Get()->session_controller()->GetLastActiveUserPrefService();
}
// Returns the time when the menu was last shown for the user associated with
// the last active user pref service, or `std::nullopt` if the menu was not
// previously marked as having been shown.
std::optional<base::Time> GetMenuLastTimeShown() {
if (auto* prefs = GetLastActiveUserPrefService()) {
if (auto* pref = prefs->FindPreference(prefs::kMultipasteMenuLastTimeShown);
pref && !pref->IsDefaultValue()) {
return base::ValueToTime(pref->GetValue());
}
}
return std::nullopt;
}
// Marks the time when the menu was last shown for the user associated with the
// last active user pref service.
void MarkMenuLastTimeShown() {
if (auto* prefs = GetLastActiveUserPrefService()) {
prefs->SetTime(prefs::kMultipasteMenuLastTimeShown, base::Time::Now());
}
}
// Emits a user action indicating that the clipboard history item at menu index
// `command_id` was pasted.
void RecordMenuIndexPastedUserAction(int command_id) {
// Per guidance in user_metrics.h, use string literals for action names.
switch (command_id) {
case 1:
base::RecordAction(
base::UserMetricsAction("Ash_ClipboardHistory_PastedItem1"));
break;
case 2:
base::RecordAction(
base::UserMetricsAction("Ash_ClipboardHistory_PastedItem2"));
break;
case 3:
base::RecordAction(
base::UserMetricsAction("Ash_ClipboardHistory_PastedItem3"));
break;
case 4:
base::RecordAction(
base::UserMetricsAction("Ash_ClipboardHistory_PastedItem4"));
break;
case 5:
base::RecordAction(
base::UserMetricsAction("Ash_ClipboardHistory_PastedItem5"));
break;
default:
NOTREACHED();
}
}
void RecordPasteItemIndex(int index) {
CHECK_GE(index, clipboard_history_util::kFirstItemCommandId);
CHECK_LT(index, clipboard_history_util::kCommandIdBoundary);
// Record the paste item's history list index in a histogram to get a
// distribution of where in the list users paste from.
base::UmaHistogramExactLinear(
"Ash.ClipboardHistory.ContextMenu.MenuOptionSelected", index,
/*exclusive_max=*/clipboard_history_util::kCommandIdBoundary);
// Record the paste item's history list index as a user action to analyze
// usage patterns, e.g., how frequently the same index is pasted multiple
// times in a row.
RecordMenuIndexPastedUserAction(index);
}
#if BUILDFLAG(USE_XKBCOMMON)
// Looks up the DomCode assigned to the keysym. In some edge cases,
// such as Dvorak layout, the original DomCode may be different
// from US standard layout.
ui::DomCode LookUpXkbDomCode(int keysym) {
if (!base::SysInfo::IsRunningOnChromeOS()) {
// On linux-chromeos, stub layout engine is used.
return ui::DomCode::NONE;
}
auto* layout_engine =
ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine();
if (!layout_engine) {
return ui::DomCode::NONE;
}
return static_cast<ui::XkbKeyboardLayoutEngine*>(layout_engine)
->GetDomCodeByKeysym(keysym, /*modifiers=*/std::nullopt);
}
#endif
ui::KeyEvent SyntheticCtrlV(ui::EventType type) {
ui::DomCode dom_code = ui::DomCode::NONE;
#if BUILDFLAG(USE_XKBCOMMON)
dom_code = LookUpXkbDomCode(XKB_KEY_v);
#endif
return dom_code == ui::DomCode::NONE
? ui::KeyEvent(type, ui::VKEY_V, ui::EF_CONTROL_DOWN)
: ui::KeyEvent(type, ui::VKEY_V, dom_code, ui::EF_CONTROL_DOWN);
}
ui::KeyEvent SyntheticCtrl(ui::EventType type) {
int flags =
type == ui::EventType::kKeyPressed ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
ui::DomCode dom_code = ui::DomCode::NONE;
#if BUILDFLAG(USE_XKBCOMMON)
dom_code = LookUpXkbDomCode(XKB_KEY_Control_L);
#endif
return dom_code == ui::DomCode::NONE
? ui::KeyEvent(type, ui::VKEY_CONTROL, flags)
: ui::KeyEvent(type, ui::VKEY_CONTROL, dom_code, flags);
}
void SyntheticPaste(
crosapi::mojom::ClipboardHistoryControllerShowSource paste_source) {
auto* host = GetWindowTreeHostForDisplay(
display::Screen::GetScreen()->GetDisplayForNewWindows().id());
CHECK(host);
// Because we do not require the user to release Ctrl+V before selecting a
// clipboard history item to paste, the Ctrl+V event we synthesize below may
// be discarded as a perceived continuation of the long press. Preempt this
// scenario by issuing a Ctrl+V release to ensure that the press and release
// below are handled as an independent paste.
// TODO(http://b/283533126): Replace this workaround with a long-term fix.
if (paste_source == crosapi::mojom::ClipboardHistoryControllerShowSource::
kControlVLongpress) {
ui::KeyEvent v_release = SyntheticCtrlV(ui::EventType::kKeyReleased);
host->DeliverEventToSink(&v_release);
ui::KeyEvent ctrl_release = SyntheticCtrl(ui::EventType::kKeyReleased);
host->DeliverEventToSink(&ctrl_release);
}
ui::KeyEvent ctrl_press = SyntheticCtrl(ui::EventType::kKeyPressed);
host->DeliverEventToSink(&ctrl_press);
ui::KeyEvent v_press = SyntheticCtrlV(ui::EventType::kKeyPressed);
host->DeliverEventToSink(&v_press);
ui::KeyEvent v_release = SyntheticCtrlV(ui::EventType::kKeyReleased);
host->DeliverEventToSink(&v_release);
ui::KeyEvent ctrl_release = SyntheticCtrl(ui::EventType::kKeyReleased);
host->DeliverEventToSink(&ctrl_release);
}
using ClipboardHistoryPasteType =
ClipboardHistoryControllerImpl::ClipboardHistoryPasteType;
bool IsPlainTextPaste(ClipboardHistoryPasteType paste_type) {
switch (paste_type) {
case ClipboardHistoryPasteType::kPlainTextAccelerator:
case ClipboardHistoryPasteType::kPlainTextKeystroke:
case ClipboardHistoryPasteType::kPlainTextMouse:
case ClipboardHistoryPasteType::kPlainTextTouch:
case ClipboardHistoryPasteType::kPlainTextVirtualKeyboard:
case ClipboardHistoryPasteType::kPlainTextCtrlV:
return true;
case ClipboardHistoryPasteType::kRichTextAccelerator:
case ClipboardHistoryPasteType::kRichTextKeystroke:
case ClipboardHistoryPasteType::kRichTextMouse:
case ClipboardHistoryPasteType::kRichTextTouch:
case ClipboardHistoryPasteType::kRichTextVirtualKeyboard:
case ClipboardHistoryPasteType::kRichTextCtrlV:
return false;
}
}
ClipboardHistoryPasteType CalculatePasteType(
crosapi::mojom::ClipboardHistoryControllerShowSource paste_source,
int event_flags) {
// There are no specific flags that indicate a paste triggered by a
// keystroke, so assume by default that keystroke was the event source
// and then check for the other known possibilities. This assumption may
// cause pastes from unknown sources to be incorrectly captured as
// keystroke pastes, but we do not expect such cases to significantly
// alter metrics.
const bool paste_plain_text = event_flags & ui::EF_SHIFT_DOWN;
if (paste_source ==
crosapi::mojom::ClipboardHistoryControllerShowSource::kVirtualKeyboard) {
return paste_plain_text
? ClipboardHistoryPasteType::kPlainTextVirtualKeyboard
: ClipboardHistoryPasteType::kRichTextVirtualKeyboard;
}
ClipboardHistoryPasteType paste_type =
paste_plain_text ? ClipboardHistoryPasteType::kPlainTextKeystroke
: ClipboardHistoryPasteType::kRichTextKeystroke;
if (event_flags & ui::EF_MOUSE_BUTTON) {
paste_type = paste_plain_text ? ClipboardHistoryPasteType::kPlainTextMouse
: ClipboardHistoryPasteType::kRichTextMouse;
} else if (event_flags & ui::EF_FROM_TOUCH) {
paste_type = paste_plain_text ? ClipboardHistoryPasteType::kPlainTextTouch
: ClipboardHistoryPasteType::kRichTextTouch;
}
return paste_type;
}
} // namespace
// ClipboardHistoryControllerImpl::AcceleratorTarget ---------------------------
class ClipboardHistoryControllerImpl::AcceleratorTarget
: public ui::AcceleratorTarget {
public:
explicit AcceleratorTarget(ClipboardHistoryControllerImpl* controller)
: controller_(controller),
delete_selected_(ui::Accelerator(
/*key_code=*/ui::VKEY_BACK,
/*modifiers=*/ui::EF_NONE,
/*key_state=*/ui::Accelerator::KeyState::PRESSED)),
tab_navigation_(ui::Accelerator(
/*key_code=*/ui::VKEY_TAB,
/*modifiers=*/ui::EF_NONE,
/*key_state=*/ui::Accelerator::KeyState::PRESSED)),
shift_tab_navigation_(ui::Accelerator(
/*key_code=*/ui::VKEY_TAB,
/*modifiers=*/ui::EF_SHIFT_DOWN,
/*key_state=*/ui::Accelerator::KeyState::PRESSED)),
paste_first_item_(ui::Accelerator(
/*key_code=*/ui::VKEY_V,
/*modifiers=*/ui::EF_CONTROL_DOWN,
/*key_state=*/ui::Accelerator::KeyState::PRESSED)),
paste_first_item_plaintext_(ui::Accelerator(
/*key_code=*/ui::VKEY_V,
/*modifiers=*/ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN,
/*key_state=*/ui::Accelerator::KeyState::PRESSED)) {}
AcceleratorTarget(const AcceleratorTarget&) = delete;
AcceleratorTarget& operator=(const AcceleratorTarget&) = delete;
~AcceleratorTarget() override = default;
void OnMenuShown() {
Shell::Get()->accelerator_controller()->Register(
{delete_selected_, tab_navigation_, shift_tab_navigation_,
paste_first_item_, paste_first_item_plaintext_},
/*target=*/this);
}
void OnMenuClosed() {
Shell::Get()->accelerator_controller()->UnregisterAll(/*target=*/this);
}
private:
// ui::AcceleratorTarget:
bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
CHECK(controller_->IsMenuShowing());
if (accelerator == delete_selected_) {
HandleDeleteSelected();
} else if (accelerator == tab_navigation_) {
HandleTab();
} else if (accelerator == shift_tab_navigation_) {
HandleShiftTab();
} else if (accelerator == paste_first_item_) {
HandlePasteFirstItem(ClipboardHistoryPasteType::kRichTextCtrlV);
} else if (accelerator == paste_first_item_plaintext_) {
HandlePasteFirstItem(ClipboardHistoryPasteType::kPlainTextCtrlV);
} else {
NOTREACHED();
}
return true;
}
bool CanHandleAccelerators() const override {
return controller_->IsMenuShowing() ||
controller_->HasAvailableHistoryItems();
}
void HandleDeleteSelected() { controller_->DeleteSelectedMenuItemIfAny(); }
void HandleTab() { controller_->AdvancePseudoFocus(/*reverse=*/false); }
void HandleShiftTab() { controller_->AdvancePseudoFocus(/*reverse=*/true); }
void HandlePasteFirstItem(ClipboardHistoryPasteType paste_type) {
const auto first_item_command_id =
controller_->context_menu_->GetFirstMenuItemCommand();
CHECK(first_item_command_id);
controller_->PasteClipboardItemByCommandId(*first_item_command_id,
paste_type);
}
// The controller responsible for showing the clipboard history menu.
const raw_ptr<ClipboardHistoryControllerImpl> controller_;
// Deletes the selected menu item.
const ui::Accelerator delete_selected_;
// Moves the pseudo focus forward.
const ui::Accelerator tab_navigation_;
// Moves the pseudo focus backward.
const ui::Accelerator shift_tab_navigation_;
// Pastes the first item in the clipboard history menu.
const ui::Accelerator paste_first_item_;
// Pastes the plain text data of the first item in the clipboard history menu.
const ui::Accelerator paste_first_item_plaintext_;
};
// ClipboardHistoryControllerImpl::MenuDelegate --------------------------------
class ClipboardHistoryControllerImpl::MenuDelegate
: public ui::SimpleMenuModel::Delegate {
public:
explicit MenuDelegate(ClipboardHistoryControllerImpl* controller)
: controller_(controller) {}
MenuDelegate(const MenuDelegate&) = delete;
MenuDelegate& operator=(const MenuDelegate&) = delete;
// ui::SimpleMenuModel::Delegate:
void ExecuteCommand(int command_id, int event_flags) override {
controller_->ExecuteCommand(command_id, event_flags);
}
private:
// The controller responsible for showing the Clipboard History menu.
const raw_ptr<ClipboardHistoryControllerImpl> controller_;
};
// ClipboardHistoryControllerImpl ----------------------------------------------
ClipboardHistoryControllerImpl::ClipboardHistoryControllerImpl(
std::unique_ptr<ClipboardHistoryControllerDelegate> delegate)
: delegate_(std::move(delegate)),
image_model_factory_(delegate_->CreateImageModelFactory()),
url_title_fetcher_(delegate_->CreateUrlTitleFetcher()),
clipboard_history_(std::make_unique<ClipboardHistory>()),
resource_manager_(std::make_unique<ClipboardHistoryResourceManager>(
clipboard_history_.get())),
accelerator_target_(std::make_unique<AcceleratorTarget>(this)),
nudge_controller_(
std::make_unique<ClipboardNudgeController>(clipboard_history_.get())),
menu_delegate_(std::make_unique<MenuDelegate>(this)) {
if (!image_model_factory_ || !url_title_fetcher_) {
CHECK_IS_TEST();
}
clipboard_history_->AddObserver(this);
resource_manager_->AddObserver(this);
SessionController::Get()->AddObserver(this);
}
ClipboardHistoryControllerImpl::~ClipboardHistoryControllerImpl() {
SessionController::Get()->RemoveObserver(this);
resource_manager_->RemoveObserver(this);
clipboard_history_->RemoveObserver(this);
}
// static
void ClipboardHistoryControllerImpl::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
ClipboardNudgeController::RegisterProfilePrefs(registry);
registry->RegisterTimePref(prefs::kMultipasteMenuLastTimeShown, base::Time());
}
void ClipboardHistoryControllerImpl::Shutdown() {
if (IsMenuShowing()) {
context_menu_->Cancel(/*will_paste_item=*/false);
}
nudge_controller_.reset();
}
bool ClipboardHistoryControllerImpl::IsMenuShowing() const {
return context_menu_ && context_menu_->IsRunning();
}
void ClipboardHistoryControllerImpl::ToggleMenuShownByAccelerator(
bool is_plain_text_paste) {
if (IsMenuShowing()) {
// Before hiding the menu, paste the selected menu item, or the first item
// if none is selected.
PasteClipboardItemByCommandId(
context_menu_->GetSelectedMenuItemCommand().value_or(
clipboard_history_util::kFirstItemCommandId),
is_plain_text_paste ? ClipboardHistoryPasteType::kPlainTextAccelerator
: ClipboardHistoryPasteType::kRichTextAccelerator);
return;
}
// Do not allow the plain text shortcut to open the menu.
if (is_plain_text_paste) {
return;
}
if (clipboard_history_util::IsEnabledInCurrentMode() && IsEmpty()) {
nudge_controller_->ShowNudge(ClipboardNudgeType::kZeroStateNudge);
return;
}
ShowMenu(CalculateAnchorRect(), ui::mojom::MenuSourceType::kKeyboard,
crosapi::mojom::ClipboardHistoryControllerShowSource::kAccelerator);
}
void ClipboardHistoryControllerImpl::AddObserver(
ClipboardHistoryController::Observer* observer) {
observers_.AddObserver(observer);
}
void ClipboardHistoryControllerImpl::RemoveObserver(
ClipboardHistoryController::Observer* observer) {
observers_.RemoveObserver(observer);
}
bool ClipboardHistoryControllerImpl::ShowMenu(
const gfx::Rect& anchor_rect,
ui::mojom::MenuSourceType source_type,
crosapi::mojom::ClipboardHistoryControllerShowSource show_source) {
return ShowMenu(anchor_rect, source_type, show_source,
OnMenuClosingCallback());
}
bool ClipboardHistoryControllerImpl::ShowMenu(
const gfx::Rect& anchor_rect,
ui::mojom::MenuSourceType source_type,
crosapi::mojom::ClipboardHistoryControllerShowSource show_source,
OnMenuClosingCallback callback) {
if (IsMenuShowing() || !HasAvailableHistoryItems()) {
return false;
}
// Close the running context menu, if any, before showing the clipboard
// history menu.
if (auto* active_menu_instance = views::MenuController::GetActiveInstance()) {
active_menu_instance->Cancel(views::MenuController::ExitType::kAll);
}
last_menu_source_ = show_source;
// `Unretained()` is safe because `this` owns `context_menu_`.
context_menu_ = ClipboardHistoryMenuModelAdapter::Create(
menu_delegate_.get(), std::move(callback),
base::BindRepeating(&ClipboardHistoryControllerImpl::OnMenuClosed,
base::Unretained(this)),
clipboard_history_.get());
context_menu_->Run(anchor_rect, source_type, show_source,
GetMenuLastTimeShown(),
nudge_controller_->GetNudgeLastTimeShown());
CHECK(IsMenuShowing());
accelerator_target_->OnMenuShown();
// The first menu item should be selected by default after the clipboard
// history menu shows. Note that the menu item is selected asynchronously
// to avoid the interference from synthesized mouse events.
menu_task_timer_.Start(
FROM_HERE, base::TimeDelta(),
base::BindOnce(
[](const base::WeakPtr<ClipboardHistoryControllerImpl>&
controller_weak_ptr) {
if (!controller_weak_ptr) {
return;
}
controller_weak_ptr->context_menu_->SelectMenuItemWithCommandId(
clipboard_history_util::kFirstItemCommandId);
if (controller_weak_ptr->initial_item_selected_callback_for_test_) {
controller_weak_ptr->initial_item_selected_callback_for_test_
.Run();
}
},
weak_ptr_factory_.GetWeakPtr()));
MarkMenuLastTimeShown();
base::UmaHistogramEnumeration("Ash.ClipboardHistory.ContextMenu.ShowMenu",
show_source);
for (auto& observer : observers_) {
observer.OnClipboardHistoryMenuShown(show_source);
}
return true;
}
bool ClipboardHistoryControllerImpl::IsEmpty() const {
return clipboard_history_->IsEmpty();
}
void ClipboardHistoryControllerImpl::FireItemUpdateNotificationTimerForTest() {
item_update_notification_timer_.FireNow();
}
void ClipboardHistoryControllerImpl::GetHistoryValues(
GetHistoryValuesCallback callback) const {
// Map of `ClipboardHistoryItem` IDs to their corresponding bitmaps.
std::map<base::UnguessableToken, SkBitmap> bitmaps_to_be_encoded;
for (auto& item : clipboard_history_->GetItems()) {
if (item.display_format() ==
crosapi::mojom::ClipboardHistoryDisplayFormat::kPng) {
const auto& maybe_png = item.data().maybe_png();
if (!maybe_png.has_value()) {
// The clipboard contains an image which has not yet been encoded to a
// PNG.
auto maybe_bitmap = item.data().GetBitmapIfPngNotEncoded();
DCHECK(maybe_bitmap.has_value());
bitmaps_to_be_encoded.emplace(item.id(),
std::move(maybe_bitmap.value()));
}
}
}
// Map of `ClipboardHistoryItem` IDs to their encoded PNGs.
auto encoded_pngs = std::make_unique<
std::map<base::UnguessableToken, std::vector<uint8_t>>>();
auto* encoded_pngs_ptr = encoded_pngs.get();
// Post back to this sequence once all images have been encoded.
base::RepeatingClosure barrier = base::BarrierClosure(
bitmaps_to_be_encoded.size(),
base::BindPostTaskToCurrentDefault(base::BindOnce(
&ClipboardHistoryControllerImpl::GetHistoryValuesWithEncodedPNGs,
weak_ptr_factory_.GetMutableWeakPtr(), std::move(callback),
std::move(encoded_pngs))));
// Encode images on background threads.
for (auto id_and_bitmap : bitmaps_to_be_encoded) {
base::ThreadPool::PostTask(
FROM_HERE, base::BindOnce(&EncodeBitmapToPNG, barrier, encoded_pngs_ptr,
std::move(id_and_bitmap.first),
std::move(id_and_bitmap.second)));
}
if (!new_bitmap_to_write_while_encoding_for_test_.isNull()) {
ui::ScopedClipboardWriter scw(ui::ClipboardBuffer::kCopyPaste);
scw.WriteImage(new_bitmap_to_write_while_encoding_for_test_);
new_bitmap_to_write_while_encoding_for_test_.reset();
}
}
gfx::Rect ClipboardHistoryControllerImpl::GetMenuBoundsInScreenForTest() const {
return context_menu_->GetMenuBoundsInScreenForTest(); // IN-TEST
}
void ClipboardHistoryControllerImpl::BlockGetHistoryValuesForTest() {
get_history_values_blocker_for_test_.reset();
get_history_values_blocker_for_test_ = std::make_unique<base::OneShotEvent>();
}
void ClipboardHistoryControllerImpl::ResumeGetHistoryValuesForTest() {
DCHECK(get_history_values_blocker_for_test_);
get_history_values_blocker_for_test_->Signal();
}
void ClipboardHistoryControllerImpl::OnScreenshotNotificationCreated() {
nudge_controller_->MarkScreenshotNotificationShown();
}
bool ClipboardHistoryControllerImpl::HasAvailableHistoryItems() const {
return clipboard_history_util::IsEnabledInCurrentMode() && !IsEmpty();
}
std::unique_ptr<ScopedClipboardHistoryPause>
ClipboardHistoryControllerImpl::CreateScopedPause() {
return std::make_unique<ScopedClipboardHistoryPauseImpl>(
clipboard_history_.get());
}
void ClipboardHistoryControllerImpl::GetHistoryValuesWithEncodedPNGs(
GetHistoryValuesCallback callback,
std::unique_ptr<std::map<base::UnguessableToken, std::vector<uint8_t>>>
encoded_pngs) {
// If a test is performing some work that must be done before history values
// are returned, wait to run this function until that work is finished.
if (get_history_values_blocker_for_test_ &&
!get_history_values_blocker_for_test_->is_signaled()) {
get_history_values_blocker_for_test_->Post(
FROM_HERE,
base::BindOnce(
&ClipboardHistoryControllerImpl::GetHistoryValuesWithEncodedPNGs,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(encoded_pngs)));
return;
}
std::vector<ClipboardHistoryItem> item_results;
// Check after asynchronous PNG encoding finishes to make sure we have not
// entered a state where clipboard history is disabled, e.g., a locked screen.
if (!clipboard_history_util::IsEnabledInCurrentMode()) {
std::move(callback).Run(std::move(item_results));
return;
}
bool all_images_encoded = true;
for (auto& item : clipboard_history_->GetItems()) {
if (item.display_format() ==
crosapi::mojom::ClipboardHistoryDisplayFormat::kPng &&
!item.data().maybe_png().has_value()) {
// The clipboard contains an image which has not yet been encoded to a
// PNG. Hopefully we just finished encoding and the PNG can be found
// in `encoded_pngs`; otherwise this item was added while other PNGs
// were being encoded.
auto png_it = encoded_pngs->find(item.id());
if (png_it == encoded_pngs->end()) {
// Can't find the encoded PNG. We'll need to restart
// `GetHistoryValues()` from the top, but allow this for loop to finish
// to let PNGs we've already encoded get set to their appropriate
// clipboards, to avoid re-encoding.
all_images_encoded = false;
} else {
item.data().SetPngDataAfterEncoding(std::move(png_it->second));
}
}
item_results.emplace_back(item);
}
if (!all_images_encoded) {
GetHistoryValues(std::move(callback));
return;
}
std::move(callback).Run(std::move(item_results));
}
std::vector<std::string> ClipboardHistoryControllerImpl::GetHistoryItemIds()
const {
std::vector<std::string> item_ids;
if (HasAvailableHistoryItems()) {
for (const auto& item : history()->GetItems()) {
item_ids.push_back(item.id().ToString());
}
}
return item_ids;
}
bool ClipboardHistoryControllerImpl::PasteClipboardItemById(
const std::string& item_id,
int event_flags,
crosapi::mojom::ClipboardHistoryControllerShowSource paste_source) {
const std::list<ClipboardHistoryItem>& history_items = history()->GetItems();
auto iter_by_id = std::find_if(history_items.cbegin(), history_items.cend(),
[&item_id](const ClipboardHistoryItem& item) {
return item.id().ToString() == item_id;
});
if (iter_by_id == history_items.cend()) {
return false;
}
RecordPasteItemIndex(std::distance(history_items.cbegin(), iter_by_id) +
clipboard_history_util::kFirstItemCommandId);
MaybePostPasteTask(*iter_by_id, CalculatePasteType(paste_source, event_flags),
paste_source);
return true;
}
bool ClipboardHistoryControllerImpl::DeleteClipboardItemById(
const std::string& item_id) {
for (const auto& item : history()->GetItems()) {
if (item.id().ToString() == item_id) {
DeleteClipboardHistoryItem(item);
return true;
}
}
return false;
}
void ClipboardHistoryControllerImpl::OnClipboardHistoryItemAdded(
const ClipboardHistoryItem& item,
bool is_duplicate) {
PostItemUpdateNotificationTask();
}
void ClipboardHistoryControllerImpl::OnClipboardHistoryItemRemoved(
const ClipboardHistoryItem& item) {
PostItemUpdateNotificationTask();
}
void ClipboardHistoryControllerImpl::OnClipboardHistoryCleared() {
// Prevent clipboard contents from being restored if the clipboard history is
// cleared shortly after pasting an item.
weak_ptr_factory_.InvalidateWeakPtrs();
// Notify observers of the history being cleared after invalidating weak
// pointers.
PostItemUpdateNotificationTask();
// Make sure the menu is closed now that there are no items to show.
if (IsMenuShowing()) {
context_menu_->Cancel(/*will_paste_item=*/false);
}
}
void ClipboardHistoryControllerImpl::OnOperationConfirmed(bool copy) {
static int confirmed_paste_count = 0;
// Here we assume that a paste operation from the clipboard history menu never
// interleaves with a user-initiated copy or paste operation from another
// source, such as pressing the ctrl-v accelerator or clicking a context menu
// option. In other words, when `pastes_to_be_confirmed_` is positive, the
// next confirmed operation is expected to be a paste from clipboard history.
// This assumption should hold in most cases given that the clipboard history
// menu is always closed after one paste, and it usually takes a relatively
// long time for a user to perform the next copy or paste. For this metric, we
// tolerate a small margin of error.
if (pastes_to_be_confirmed_ > 0 && !copy) {
++confirmed_paste_count;
--pastes_to_be_confirmed_;
} else {
// Note that both copies and pastes from the standard clipboard cause the
// clipboard history consecutive paste count to be emitted and reset.
if (confirmed_paste_count > 0) {
base::UmaHistogramCounts100("Ash.ClipboardHistory.ConsecutivePastes",
confirmed_paste_count);
confirmed_paste_count = 0;
}
if (copy) {
// Record copy actions once they are confirmed, rather than when clipboard
// data first changes, to allow multiple data changes to be debounced into
// a single copy operation. This ensures that each user-initiated copy is
// recorded only once. See `ClipboardHistory::OnDataChanged()` for further
// explanation.
base::RecordAction(base::UserMetricsAction("Ash_Clipboard_CopiedItem"));
} else {
// Pastes from clipboard history are already recorded in
// `PasteMenuItemData()`. Here, we record just pastes from the standard
// clipboard, to see how standard clipboard pastes interleave with
// clipboard history pastes.
base::RecordAction(base::UserMetricsAction("Ash_Clipboard_PastedItem"));
}
// Verify that this operation did not interleave with a clipboard history
// paste.
DCHECK_EQ(pastes_to_be_confirmed_, 0);
// Whether or not the non-interleaving assumption has held, always reset
// `pastes_to_be_confirmed_` to prevent standard clipboard pastes from
// possibly being counted as clipboard history pastes, which could
// significantly affect the clipboard history consecutive pastes metric.
pastes_to_be_confirmed_ = 0;
}
// Callback will be run after clipboard data restoration.
if (confirmed_operation_callback_for_test_ && !clipboard_data_replaced_) {
confirmed_operation_callback_for_test_.Run(/*success=*/true);
}
}
void ClipboardHistoryControllerImpl::OnCachedImageModelUpdated(
const std::vector<base::UnguessableToken>& menu_item_ids) {
PostItemUpdateNotificationTask();
}
void ClipboardHistoryControllerImpl::OnSessionStateChanged(
session_manager::SessionState state) {
PostItemUpdateNotificationTask();
}
void ClipboardHistoryControllerImpl::OnLoginStatusChanged(
LoginStatus login_status) {
PostItemUpdateNotificationTask();
}
void ClipboardHistoryControllerImpl::PostItemUpdateNotificationTask() {
// Uses the async task to debounce multiple clipboard history changes in
// short duration. Restart the timer if it is running.
// This is done to avoid notifying observers multiple times if there are
// multiple clipboard history changes in a short period. For example, if the
// clipboard history reaches the cache limit and a new clipboard history item
// arrives at the same time, there would be two clipboard history changes: the
// addition of the new item and the removal of an obsolete item. In this case,
// this class should only notify observers only once.
item_update_notification_timer_.Start(
FROM_HERE, base::TimeDelta(),
base::BindOnce(
&ClipboardHistoryControllerImpl::MaybeNotifyObserversOfItemUpdate,
weak_ptr_factory_.GetWeakPtr()));
}
void ClipboardHistoryControllerImpl::MaybeNotifyObserversOfItemUpdate() {
const bool has_available_items = HasAvailableHistoryItems();
if (!has_available_items && !has_available_items_in_last_update_) {
// There are no available items, and there were none in the last
// notification either. Nothing has changed, so return early.
return;
}
for (auto& observer : observers_) {
observer.OnClipboardHistoryItemsUpdated();
}
has_available_items_in_last_update_ = has_available_items;
}
void ClipboardHistoryControllerImpl::ExecuteCommand(int command_id,
int event_flags) {
DCHECK(context_menu_);
DCHECK_GE(command_id, clipboard_history_util::kFirstItemCommandId);
DCHECK_LE(command_id, clipboard_history_util::kMaxItemCommandId);
using Action = clipboard_history_util::Action;
Action action = context_menu_->GetActionForCommandId(command_id);
switch (action) {
case Action::kPaste:
PasteClipboardItemByCommandId(
command_id, CalculatePasteType(last_menu_source_, event_flags));
return;
case Action::kDelete:
DeleteItemWithCommandId(command_id);
return;
case Action::kSelect:
context_menu_->SelectMenuItemWithCommandId(command_id);
return;
case Action::kSelectItemHoveredByMouse:
context_menu_->SelectMenuItemHoveredByMouse();
return;
case Action::kEmpty:
DUMP_WILL_BE_NOTREACHED();
return;
}
}
void ClipboardHistoryControllerImpl::PasteClipboardItemByCommandId(
int command_id,
ClipboardHistoryPasteType paste_type) {
// Force close the context menu. Failure to do so before dispatching our
// synthetic key event will result in the context menu consuming the event.
// When closing the menu, indicate that the menu is closing because of an
// imminent paste. Note that in some cases, this will indicate paste intent
// for pastes that ultimately fail. For now, this is an acceptable inaccuracy.
CHECK(context_menu_);
context_menu_->Cancel(/*will_paste_item=*/true);
// `command_id` should match the pasted item's index in `context_menu_`.
RecordPasteItemIndex(command_id);
MaybePostPasteTask(context_menu_->GetItemFromCommandId(command_id),
paste_type, last_menu_source_);
}
void ClipboardHistoryControllerImpl::MaybePostPasteTask(
const ClipboardHistoryItem& item,
ClipboardHistoryPasteType paste_type,
crosapi::mojom::ClipboardHistoryControllerShowSource paste_source) {
// Deactivate ClipboardImageModelFactory prior to pasting to ensure that any
// modifications to the clipboard for HTML rendering purposes are reversed.
// This factory may be nullptr in tests.
if (auto* clipboard_image_factory = ClipboardImageModelFactory::Get()) {
clipboard_image_factory->Deactivate();
}
if (auto* active_window = window_util::GetActiveWindow()) {
// Paste asynchronously to ensure ARC windows handle paste events correctly.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
&ClipboardHistoryControllerImpl::PasteClipboardHistoryItem,
weak_ptr_factory_.GetWeakPtr(), active_window, item, paste_type,
paste_source));
}
}
void ClipboardHistoryControllerImpl::PasteClipboardHistoryItem(
aura::Window* intended_window,
ClipboardHistoryItem item,
ClipboardHistoryPasteType paste_type,
crosapi::mojom::ClipboardHistoryControllerShowSource paste_source) {
// Return early if any of these conditions occur:
// 1. The original clipboard data has been replaced by an in-progress
// clipboard history paste.
// 2. The active window has changed.
// 3. The clipboard history feature is disabled under the current mode.
if (clipboard_data_replaced_ || !intended_window ||
intended_window != window_util::GetActiveWindow() ||
!clipboard_history_util::IsEnabledInCurrentMode()) {
if (confirmed_operation_callback_for_test_)
confirmed_operation_callback_for_test_.Run(/*success=*/false);
return;
}
// Get information about the data to be pasted.
bool paste_plain_text = IsPlainTextPaste(paste_type);
auto* clipboard = GetClipboard();
ui::DataTransferEndpoint data_dst(ui::EndpointType::kClipboardHistory);
const auto* current_clipboard_data = clipboard->GetClipboardData(&data_dst);
// Clipboard history pastes are performed by temporarily writing data to the
// system clipboard, if necessary, and then issuing a standard paste.
// Determine the data we should temporarily write to the clipboard, if any, so
// that we can paste the selected history item.
std::unique_ptr<ui::ClipboardData> data_to_paste;
if (paste_plain_text) {
data_to_paste = std::make_unique<ui::ClipboardData>();
data_to_paste->set_commit_time(item.data().commit_time());
data_to_paste->set_text(item.data().text());
auto data_src = item.data().source();
if (data_src) {
data_to_paste->set_source(data_src);
}
} else if (!current_clipboard_data ||
*current_clipboard_data != item.data()) {
data_to_paste = std::make_unique<ui::ClipboardData>(item.data());
}
// Pause changes to clipboard history while manipulating the clipboard.
std::unique_ptr<ui::ClipboardData> replaced_data;
// If necessary, replace the clipboard's current data before issuing a paste.
if (data_to_paste) {
ScopedClipboardHistoryPauseImpl scoped_pause(
clipboard_history_.get(),
clipboard_history_util::PauseBehavior::kDefault);
replaced_data =
GetClipboard()->WriteClipboardData(std::move(data_to_paste));
clipboard_data_replaced_ = !!replaced_data;
}
++pastes_to_be_confirmed_;
// Use synthetic pastes as a fallback solution.
if (!delegate_->Paste()) {
SyntheticPaste(paste_source);
}
clipboard_history_util::RecordClipboardHistoryItemPasted(item);
base::UmaHistogramEnumeration("Ash.ClipboardHistory.PasteType", paste_type);
base::UmaHistogramEnumeration("Ash.ClipboardHistory.PasteSource",
paste_source);
for (auto& observer : observers_) {
observer.OnClipboardHistoryPasted();
}
// If the clipboard was not changed--i.e., we pasted the full data on the
// clipboard--then we are done modifying the clipboard buffer.
if (!replaced_data) {
return;
}
// Restore the clipboard data asynchronously. Some apps take a long time to
// receive the paste event, and some apps will read from the clipboard
// multiple times per paste. Wait a bit before writing `data_to_restore` back
// to the clipboard.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](const base::WeakPtr<ClipboardHistoryControllerImpl>& weak_ptr,
std::unique_ptr<ui::ClipboardData> data_to_restore) {
std::unique_ptr<ScopedClipboardHistoryPauseImpl> scoped_pause;
if (weak_ptr) {
// When restoring the original clipboard content, pause clipboard
// history to avoid committing data already at the top of the
// clipboard history list.
scoped_pause = std::make_unique<ScopedClipboardHistoryPauseImpl>(
weak_ptr->clipboard_history_.get(),
clipboard_history_util::PauseBehavior::kDefault);
}
GetClipboard()->WriteClipboardData(std::move(data_to_restore));
if (weak_ptr) {
weak_ptr->clipboard_data_replaced_ = false;
// Confirm the operation after data restoration if needed.
if (auto& callback_for_test =
weak_ptr->confirmed_operation_callback_for_test_;
callback_for_test && !weak_ptr->pastes_to_be_confirmed_) {
callback_for_test.Run(/*success=*/true);
}
}
},
weak_ptr_factory_.GetWeakPtr(), std::move(replaced_data)),
buffer_restoration_delay_for_test_.value_or(base::Milliseconds(200)));
}
void ClipboardHistoryControllerImpl::DeleteSelectedMenuItemIfAny() {
DCHECK(context_menu_);
auto selected_command = context_menu_->GetSelectedMenuItemCommand();
// Return early if no item is selected.
if (!selected_command.has_value())
return;
DeleteItemWithCommandId(*selected_command);
}
void ClipboardHistoryControllerImpl::DeleteItemWithCommandId(int command_id) {
DCHECK(context_menu_);
// Pressing VKEY_DELETE is handled here via AcceleratorTarget because the
// contextual menu consumes the key event. Record the "pressing the delete
// button" histogram here because this action does the same thing as
// activating the button directly via click/tap. There is no special handling
// for pasting an item via VKEY_RETURN because in that case the menu does not
// process the key event.
const auto& to_be_deleted_item =
context_menu_->GetItemFromCommandId(command_id);
DeleteClipboardHistoryItem(to_be_deleted_item);
// If the item to be deleted is the last one, close the whole menu.
if (context_menu_->GetMenuItemsCount() == 1) {
context_menu_->Cancel(/*will_paste_item=*/false);
return;
}
context_menu_->RemoveMenuItemWithCommandId(command_id);
}
void ClipboardHistoryControllerImpl::DeleteClipboardHistoryItem(
const ClipboardHistoryItem& item) {
clipboard_history_util::RecordClipboardHistoryItemDeleted(item);
clipboard_history_->RemoveItemForId(item.id());
}
void ClipboardHistoryControllerImpl::AdvancePseudoFocus(bool reverse) {
DCHECK(context_menu_);
context_menu_->AdvancePseudoFocus(reverse);
}
gfx::Rect ClipboardHistoryControllerImpl::CalculateAnchorRect() const {
display::Display display = display::Screen::GetScreen()->GetPrimaryDisplay();
auto* host = GetWindowTreeHostForDisplay(display.id());
// Some web apps render the caret in an IFrame, and we will not get the
// bounds in that case.
// TODO(crbug.com/40137728): Show the menu in the middle of the
// webview if the bounds are empty.
ui::TextInputClient* text_input_client =
host->GetInputMethod()->GetTextInputClient();
// `text_input_client` may be null. For example, in clamshell mode and without
// any window open.
const gfx::Rect textfield_bounds =
text_input_client ? text_input_client->GetCaretBounds() : gfx::Rect();
// Note that the width of caret's bounds may be zero in some views (such as
// the search bar of Google search web page). So we cannot use
// gfx::Size::IsEmpty() here. In addition, the applications using IFrame may
// provide unreliable `textfield_bounds` which are not fully contained by the
// display bounds.
const bool textfield_bounds_are_valid =
textfield_bounds.size() != gfx::Size() &&
IsRectContainedByAnyDisplay(textfield_bounds);
if (textfield_bounds_are_valid)
return textfield_bounds;
return gfx::Rect(display::Screen::GetScreen()->GetCursorScreenPoint(),
gfx::Size());
}
void ClipboardHistoryControllerImpl::OnMenuClosed() {
accelerator_target_->OnMenuClosed();
// Reset `context_menu_` in the asynchronous way. Because the menu may be
// accessed after `OnMenuClosed()` is called.
menu_task_timer_.Start(
FROM_HERE, base::TimeDelta(),
base::BindOnce(
[](const base::WeakPtr<ClipboardHistoryControllerImpl>&
controller_weak_ptr) {
if (controller_weak_ptr)
controller_weak_ptr->context_menu_.reset();
},
weak_ptr_factory_.GetWeakPtr()));
}
} // namespace ash