File: tab_strip_page_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (939 lines) | stat: -rw-r--r-- 36,532 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/tab_strip/tab_strip_page_handler.h"

#include <algorithm>
#include <memory>
#include <optional>

#include "base/containers/fixed_flat_map.h"
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_utils.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/tabs/tab_group_theme.h"
#include "chrome/browser/ui/tabs/tab_menu_model.h"
#include "chrome/browser/ui/tabs/tab_renderer_data.h"
#include "chrome/browser/ui/tabs/tab_utils.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_embedder.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_metrics.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_util.h"
#include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/browser/ui/webui/util/image_util.h"
#include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/grit/generated_resources.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/tab_groups/tab_group_color.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tab_groups/tab_group_visual_data.h"
#include "components/tabs/public/tab_group.h"
#include "content/public/common/drop_data.h"
#include "third_party/blink/public/common/input/web_gesture_event.h"
#include "ui/aura/window_delegate.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/list_selection_model.h"
#include "ui/base/theme_provider.h"
#include "ui/color/color_id.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/gesture_event_details.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/range/range.h"
#include "ui/menus/simple_menu_model.h"
#include "url/gurl.h"

#include "base/win/windows_h_disallowed.h"

namespace {

// Delay in milliseconds of when the dragging UI should be shown for touch drag.
// Note: For better user experience, this is made shorter than
// EventType::kGestureLongPress delay, which is too long for this case, e.g.,
// about 650ms.
constexpr base::TimeDelta kTouchLongpressDelay = base::Milliseconds(300);

class WebUIBackgroundMenuModel : public ui::SimpleMenuModel {
 public:
  explicit WebUIBackgroundMenuModel(ui::SimpleMenuModel::Delegate* delegate)
      : ui::SimpleMenuModel(delegate) {
    AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
    AddItemWithStringId(IDC_RESTORE_TAB, IDS_RESTORE_TAB);
    AddItemWithStringId(IDC_BOOKMARK_ALL_TABS, IDS_BOOKMARK_ALL_TABS);
  }
};

class WebUIBackgroundContextMenu : public ui::SimpleMenuModel::Delegate,
                                   public WebUIBackgroundMenuModel {
 public:
  WebUIBackgroundContextMenu(
      Browser* browser,
      const ui::AcceleratorProvider* accelerator_provider)
      : WebUIBackgroundMenuModel(this),
        browser_(browser),
        accelerator_provider_(accelerator_provider) {}
  ~WebUIBackgroundContextMenu() override = default;

  bool IsCommandIdEnabled(int command_id) const override {
    return chrome::IsCommandEnabled(browser_, command_id);
  }

  void ExecuteCommand(int command_id, int event_flags) override {
    chrome::ExecuteCommand(browser_, command_id);
  }

  bool GetAcceleratorForCommandId(int command_id,
                                  ui::Accelerator* accelerator) const override {
    return accelerator_provider_->GetAcceleratorForCommandId(command_id,
                                                             accelerator);
  }

 private:
  const raw_ptr<Browser> browser_;
  const raw_ptr<const ui::AcceleratorProvider> accelerator_provider_;
};

class WebUITabContextMenu : public ui::SimpleMenuModel::Delegate,
                            public TabMenuModel {
 public:
  WebUITabContextMenu(Browser* browser,
                      const ui::AcceleratorProvider* accelerator_provider,
                      int tab_index)
      : TabMenuModel(this,
                     browser->GetFeatures().tab_menu_model_delegate(),
                     browser->tab_strip_model(),
                     tab_index),
        browser_(browser),
        accelerator_provider_(accelerator_provider),
        tab_index_(tab_index) {}
  ~WebUITabContextMenu() override = default;

  void ExecuteCommand(int command_id, int event_flags) override {
    DCHECK_LT(tab_index_, browser_->tab_strip_model()->count());
    browser_->tab_strip_model()->ExecuteContextMenuCommand(
        tab_index_, static_cast<TabStripModel::ContextMenuCommand>(command_id));
  }

  bool GetAcceleratorForCommandId(int command_id,
                                  ui::Accelerator* accelerator) const override {
    int real_command = -1;
    TabStripModel::ContextMenuCommandToBrowserCommand(command_id,
                                                      &real_command);

    if (real_command != -1) {
      return accelerator_provider_->GetAcceleratorForCommandId(real_command,
                                                               accelerator);
    } else {
      return false;
    }
  }

 private:
  const raw_ptr<Browser> browser_;
  const raw_ptr<const ui::AcceleratorProvider> accelerator_provider_;
  const int tab_index_;
};

}  // namespace

TabStripPageHandler::~TabStripPageHandler() {
  ThemeServiceFactory::GetForProfile(browser_->profile())->RemoveObserver(this);
  theme_observation_.Reset();
}

TabStripPageHandler::TabStripPageHandler(
    mojo::PendingReceiver<tab_strip::mojom::PageHandler> receiver,
    mojo::PendingRemote<tab_strip::mojom::Page> page,
    content::WebUI* web_ui,
    Browser* browser,
    TabStripUIEmbedder* embedder)
    : receiver_(this, std::move(receiver)),
      page_(std::move(page)),
      web_ui_(web_ui),
      browser_(browser),
      embedder_(embedder),
      thumbnail_tracker_(
          base::BindRepeating(&TabStripPageHandler::HandleThumbnailUpdate,
                              base::Unretained(this))),
      tab_before_unload_tracker_(
          base::BindRepeating(&TabStripPageHandler::OnTabCloseCancelled,
                              base::Unretained(this))),
      context_menu_after_tap_(base::FeatureList::IsEnabled(
          features::kWebUITabStripContextMenuAfterTap)),
      long_press_timer_(std::make_unique<base::RetainingOneShotTimer>(
          FROM_HERE,
          kTouchLongpressDelay,
          base::BindRepeating(&TabStripPageHandler::OnLongPressTimer,
                              base::Unretained(this)))) {
  DCHECK(browser_);
  DCHECK(embedder_);
  web_ui_->GetWebContents()->SetDelegate(this);
  browser_->tab_strip_model()->AddObserver(this);

  // Listen for theme installation.
  ThemeServiceFactory::GetForProfile(browser_->profile())->AddObserver(this);

  // Or native theme change.
  theme_observation_.Observe(
      webui::GetNativeThemeDeprecated(web_ui_->GetWebContents()));
}

void TabStripPageHandler::NotifyLayoutChanged() {
  TRACE_EVENT0("browser", "TabStripPageHandler:NotifyLayoutChanged");
  page_->LayoutChanged(embedder_->GetLayout().AsDictionary());
}

void TabStripPageHandler::NotifyReceivedKeyboardFocus() {
  page_->ReceivedKeyboardFocus();
}

void TabStripPageHandler::NotifyContextMenuClosed() {
  page_->ContextMenuClosed();
}

// TabStripModelObserver:
void TabStripPageHandler::OnTabGroupChanged(const TabGroupChange& change) {
  TRACE_EVENT0("browser", "TabStripPageHandler:OnTabGroupChanged");
  DCHECK(browser_->tab_strip_model()->SupportsTabGroups());
  TabGroupModel* group_model = browser_->tab_strip_model()->group_model();

  if (!group_model) {
    return;
  }

  switch (change.type) {
    case TabGroupChange::kCreated: {
      if (change.GetCreateChange()->reason() ==
          TabGroupChange::TabGroupCreationReason::
              kInsertedFromAnotherTabstrip) {
        // Set the group information of all the tabs and create group webUI
        // object.
        for (tabs::TabInterface* tab :
             change.GetCreateChange()->GetDetachedTabs()) {
          const SessionID::id_type tab_id =
              extensions::ExtensionTabUtil::GetTabId(tab->GetContents());
          page_->TabGroupStateChanged(tab_id, change.model->GetIndexOfTab(tab),
                                      change.group.ToString());
        }

        // Notify webUI of initial visual information.
        page_->TabGroupVisualsChanged(
            change.group.ToString(),
            GetTabGroupData(group_model->GetTabGroup(change.group)));
      }
      break;
    }
    case TabGroupChange::kEditorOpened:
      break;
    case TabGroupChange::kVisualsChanged: {
        page_->TabGroupVisualsChanged(
            change.group.ToString(),
            GetTabGroupData(group_model->GetTabGroup(change.group)));
      break;
    }

    case TabGroupChange::kMoved: {
      DCHECK(browser_->tab_strip_model()->SupportsTabGroups());
      const int start_tab =
          group_model->GetTabGroup(change.group)->ListTabs().start();
      page_->TabGroupMoved(change.group.ToString(), start_tab);
      break;
    }

    case TabGroupChange::kClosed: {
      embedder_->HideEditDialogForGroup();
      page_->TabGroupClosed(change.group.ToString());
      break;
    }
  }
}

void TabStripPageHandler::TabGroupedStateChanged(
    TabStripModel* tab_strip_model,
    std::optional<tab_groups::TabGroupId> old_group,
    std::optional<tab_groups::TabGroupId> new_group,
    tabs::TabInterface* tab,
    int index) {
  TRACE_EVENT0("browser", "TabStripPageHandler:TabGroupedStateChanged");
  const SessionID::id_type tab_id =
      extensions::ExtensionTabUtil::GetTabId(tab->GetContents());
  if (new_group.has_value()) {
    page_->TabGroupStateChanged(tab_id, index, new_group.value().ToString());
  } else {
    page_->TabGroupStateChanged(tab_id, index, std::optional<std::string>());
  }
}

void TabStripPageHandler::OnTabStripModelChanged(
    TabStripModel* tab_strip_model,
    const TabStripModelChange& change,
    const TabStripSelectionChange& selection) {
  TRACE_EVENT0("browser", "TabStripPageHandler:OnTabStripModelChanged");
  if (tab_strip_model->empty()) {
    return;
  }

  // The context menu model is created when the menu is first shown. However, if
  // the tab strip model changes, the context menu model may not longer reflect
  // the current state of the tab strip. Actions then taken from the context
  // menu may leave the tab strip in an inconsistent state, or result in DCHECK
  // crashes. To ensure this does not occur close the context menu on a tab
  // strip model change.
  embedder_->CloseContextMenu();

  switch (change.type()) {
    case TabStripModelChange::kInserted: {
      for (const auto& contents : change.GetInsert()->contents) {
        page_->TabCreated(GetTabData(contents.contents, contents.index));
      }
      break;
    }
    case TabStripModelChange::kRemoved: {
      for (const auto& contents : change.GetRemove()->contents) {
        page_->TabRemoved(
            extensions::ExtensionTabUtil::GetTabId(contents.contents));
      }
      break;
    }
    case TabStripModelChange::kMoved: {
      auto* move = change.GetMove();
      page_->TabMoved(extensions::ExtensionTabUtil::GetTabId(move->contents),
                      move->to_index,
                      tab_strip_model->IsTabPinned(move->to_index));
      break;
    }
    case TabStripModelChange::kReplaced: {
      auto* replace = change.GetReplace();
      page_->TabReplaced(
          extensions::ExtensionTabUtil::GetTabId(replace->old_contents),
          extensions::ExtensionTabUtil::GetTabId(replace->new_contents));
      break;
    }
    case TabStripModelChange::kSelectionOnly:
      // Multi-selection is not supported for touch.
      break;
  }

  if (selection.active_tab_changed()) {
    content::WebContents* new_contents = selection.new_contents;
    if (new_contents && selection.new_model.active().has_value()) {
      page_->TabActiveChanged(
          extensions::ExtensionTabUtil::GetTabId(new_contents));
    }
  }
}

void TabStripPageHandler::TabChangedAt(content::WebContents* contents,
                                       int index,
                                       TabChangeType change_type) {
  TRACE_EVENT0("browser", "TabStripPageHandler:TabChangedAt");
  page_->TabUpdated(GetTabData(contents, index));
}

void TabStripPageHandler::TabPinnedStateChanged(TabStripModel* tab_strip_model,
                                                content::WebContents* contents,
                                                int index) {
  page_->TabUpdated(GetTabData(contents, index));
}

void TabStripPageHandler::TabBlockedStateChanged(content::WebContents* contents,
                                                 int index) {
  page_->TabUpdated(GetTabData(contents, index));
}

bool TabStripPageHandler::PreHandleGestureEvent(
    content::WebContents* source,
    const blink::WebGestureEvent& event) {
  switch (event.GetType()) {
    case blink::WebInputEvent::Type::kGestureScrollBegin:
      // Drag and drop for the WebUI tab strip is currently only supported for
      // Aura platforms.
#if defined(USE_AURA)
      // If we are passed the `kTouchLongpressDelay` threshold since the initial
      // tap down initiate a drag on scroll start.
      if (should_drag_on_gesture_scroll_ && !long_press_timer_->IsRunning()) {
        handling_gesture_scroll_ = true;

        // If we are about to start a drag ensure the context menu is closed.
        embedder_->CloseContextMenu();

        // Synthesize a long press event to start the drag and drop session.
        // TODO(tluk): Replace this with a better drag and drop trigger when
        // available.
        ui::GestureEventDetails press_details(ui::EventType::kGestureLongPress);
        press_details.set_device_type(
            ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
        ui::GestureEvent press_event(
            touch_drag_start_point_.x(), touch_drag_start_point_.y(),
            ui::EF_IS_SYNTHESIZED, base::TimeTicks::Now(), press_details);

        auto* window = web_ui_->GetWebContents()->GetContentNativeView();
        window->delegate()->OnGestureEvent(&press_event);

        // Following the long press we need to dispatch a scroll end event to
        // ensure the gesture stream is not left in an inconsistent state.
        ui::GestureEventDetails scroll_end_details(
            ui::EventType::kGestureScrollEnd);
        scroll_end_details.set_device_type(
            ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
        ui::GestureEvent scroll_end_event(
            touch_drag_start_point_.x(), touch_drag_start_point_.y(),
            ui::EF_IS_SYNTHESIZED, base::TimeTicks::Now(), scroll_end_details);
        window->delegate()->OnGestureEvent(&scroll_end_event);
        return true;
      }
      long_press_timer_->Stop();
#endif  // defined(USE_AURA)
      return false;
    case blink::WebInputEvent::Type::kGestureScrollEnd:
      should_drag_on_gesture_scroll_ = false;
      handling_gesture_scroll_ = false;
      return false;
    case blink::WebInputEvent::Type::kGestureTapDown:
      // We should only trigger a drag as part of the gesture event stream if
      // the stream begins with a tap down gesture event.
      should_drag_on_gesture_scroll_ = true;
      touch_drag_start_point_ =
          gfx::ToRoundedPoint(event.PositionInRootFrame());
      long_press_timer_->Reset();
      return false;
    case blink::WebInputEvent::Type::kGestureLongPress:
      // Do not block the long press if handling a scroll gesture. This ensures
      // the long press gesture event emitted during a scroll begin event
      // reaches the WebContents and triggers a drag session.
      if (handling_gesture_scroll_) {
        should_drag_on_gesture_scroll_ = false;
        return false;
      }
      if (!context_menu_after_tap_) {
        page_->ShowContextMenu();
      }
      return true;
    case blink::WebInputEvent::Type::kGestureTwoFingerTap:
      page_->ShowContextMenu();
      return true;
    case blink::WebInputEvent::Type::kGestureLongTap:
      if (context_menu_after_tap_) {
        page_->ShowContextMenu();
      }

      should_drag_on_gesture_scroll_ = false;
      long_press_timer_->Stop();
      return true;
    case blink::WebInputEvent::Type::kGestureTap:
      // Ensure that we reset `should_drag_on_gesture_scroll_` when we encounter
      // a gesture tap event (i.e. an event triggered after the user lifts their
      // finger following a press or long press).
      should_drag_on_gesture_scroll_ = false;
      long_press_timer_->Stop();
      return false;
    default:
      break;
  }
  return false;
}

bool TabStripPageHandler::CanDragEnter(
    content::WebContents* source,
    const content::DropData& data,
    blink::DragOperationsMask operations_allowed) {
  // TODO(crbug.com/40110968): Prevent dragging across Chromium instances.
  if (auto it = data.custom_data.find(kWebUITabIdDataType);
      it != data.custom_data.end()) {
    int tab_id;
    bool found_tab_id = base::StringToInt(it->second, &tab_id);
    return found_tab_id && extensions::ExtensionTabUtil::GetTabById(
                               tab_id, browser_->profile(), false, nullptr);
  }

  if (auto it = data.custom_data.find(kWebUITabGroupIdDataType);
      it != data.custom_data.end()) {
    std::string group_id = base::UTF16ToUTF8(it->second);
    Browser* found_browser = tab_strip_ui::GetBrowserWithGroupId(
        Profile::FromBrowserContext(browser_->profile()), group_id);
    return found_browser != nullptr;
  }

  return false;
}

bool TabStripPageHandler::IsPrivileged() {
  return true;
}

void TabStripPageHandler::OnLongPressTimer() {
  page_->LongPress();
}

tab_strip::mojom::TabPtr TabStripPageHandler::GetTabData(
    content::WebContents* contents,
    int index) {
  DCHECK(index >= 0);
  auto tab_data = tab_strip::mojom::Tab::New();

  tab_data->active = browser_->tab_strip_model()->active_index() == index;
  tab_data->id = extensions::ExtensionTabUtil::GetTabId(contents);
  DCHECK(tab_data->id > 0);
  tab_data->index = index;

  const std::optional<tab_groups::TabGroupId> group_id =
      browser_->tab_strip_model()->GetTabGroupForTab(index);
  if (group_id.has_value()) {
    tab_data->group_id = group_id.value().ToString();
  }

  TabRendererData tab_renderer_data =
      TabRendererData::FromTabInModel(browser_->tab_strip_model(), index);
  tab_data->pinned = tab_renderer_data.pinned;
  tab_data->title = base::UTF16ToUTF8(tab_renderer_data.title);
  tab_data->url = tab_renderer_data.visible_url;

  const ui::ColorProvider& provider =
      web_ui_->GetWebContents()->GetColorProvider();
  const gfx::ImageSkia default_favicon =
      favicon::GetDefaultFaviconModel().Rasterize(&provider);
  const gfx::ImageSkia raster_favicon =
      tab_renderer_data.favicon.Rasterize(&provider);

  if (!tab_renderer_data.favicon.IsEmpty()) {
    // Themified icons only apply to a few select chrome URLs.
    if (tab_renderer_data.should_themify_favicon) {
      tab_data->favicon_url = GURL(
          webui::EncodePNGAndMakeDataURI(ThemeFavicon(raster_favicon, false),
                                         web_ui_->GetDeviceScaleFactor()));
      tab_data->active_favicon_url = GURL(webui::EncodePNGAndMakeDataURI(
          ThemeFavicon(raster_favicon, true), web_ui_->GetDeviceScaleFactor()));
    } else {
      tab_data->favicon_url = GURL(webui::EncodePNGAndMakeDataURI(
          tab_renderer_data.favicon.Rasterize(&provider),
          web_ui_->GetDeviceScaleFactor()));
    }

    tab_data->is_default_favicon =
        raster_favicon.BackedBySameObjectAs(default_favicon);
  } else {
    tab_data->is_default_favicon = true;
  }
  tab_data->show_icon = tab_renderer_data.show_icon;
  tab_data->network_state = tab_renderer_data.network_state;
  tab_data->should_hide_throbber = tab_renderer_data.should_hide_throbber;
  tab_data->blocked = tab_renderer_data.blocked;
  tab_data->crashed = tab_renderer_data.IsCrashed();
  // TODO(johntlee): Add the rest of TabRendererData

  for (const auto alert_state : GetTabAlertStatesForContents(contents)) {
    tab_data->alert_states.push_back(alert_state);
  }

  return tab_data;
}

tab_strip::mojom::TabGroupVisualDataPtr TabStripPageHandler::GetTabGroupData(
    TabGroup* group) {
  const tab_groups::TabGroupVisualData* visual_data = group->visual_data();

  auto tab_group = tab_strip::mojom::TabGroupVisualData::New();
  tab_group->title = base::UTF16ToUTF8(visual_data->title());

  // TODO the tab strip should support toggles between inactive and active frame
  // states. Currently the webui tab strip only uses active frame colors
  // (https://crbug.com/1060398).
  const int group_color_id =
      GetThumbnailTabStripTabGroupColorId(visual_data->color(), true);
  const SkColor group_color = embedder_->GetColorProviderColor(group_color_id);
  tab_group->color = color_utils::SkColorToRgbString(group_color);
  // TODO(tluk): Incorporate the text color into the ColorProvider.
  tab_group->text_color = color_utils::SkColorToRgbString(
      color_utils::GetColorWithMaxContrast(group_color));
  return tab_group;
}

void TabStripPageHandler::GetTabs(GetTabsCallback callback) {
  TRACE_EVENT0("browser", "TabStripPageHandler:HandleGetTabs");
  std::vector<tab_strip::mojom::TabPtr> tabs;
  TabStripModel* tab_strip_model = browser_->tab_strip_model();
  for (int i = 0; i < tab_strip_model->count(); ++i) {
    tabs.push_back(GetTabData(tab_strip_model->GetWebContentsAt(i), i));
  }
  std::move(callback).Run(std::move(tabs));
}

void TabStripPageHandler::GetGroupVisualData(
    GetGroupVisualDataCallback callback) {
  TRACE_EVENT0("browser", "TabStripPageHandler:HandleGetGroupVisualData");
  base::flat_map<std::string, tab_strip::mojom::TabGroupVisualDataPtr>
      group_visual_datas;
  std::vector<tab_groups::TabGroupId> groups =
      browser_->tab_strip_model()->group_model()->ListTabGroups();
  for (const tab_groups::TabGroupId& group : groups) {
    group_visual_datas[group.ToString()] = GetTabGroupData(
        browser_->tab_strip_model()->group_model()->GetTabGroup(group));
  }
  std::move(callback).Run(std::move(group_visual_datas));
}

void TabStripPageHandler::GroupTab(int32_t tab_id,
                                   const std::string& group_id_string) {
  int tab_index = -1;
  if (!extensions::ExtensionTabUtil::GetTabById(tab_id, browser_->profile(),
                                                /*include_incognito=*/true,
                                                /*window=*/nullptr,
                                                /*contents=*/nullptr,
                                                &tab_index)) {
    return;
  }

  std::optional<tab_groups::TabGroupId> group_id =
      tab_strip_ui::GetTabGroupIdFromString(
          browser_->tab_strip_model()->group_model(), group_id_string);
  if (group_id.has_value()) {
    browser_->tab_strip_model()->AddToExistingGroup({tab_index},
                                                    group_id.value());
  }
}

void TabStripPageHandler::UngroupTab(int32_t tab_id) {
  int tab_index = -1;
  if (!extensions::ExtensionTabUtil::GetTabById(tab_id, browser_->profile(),
                                                /*include_incognito=*/true,
                                                /*window=*/nullptr,
                                                /*contents=*/nullptr,
                                                &tab_index)) {
    return;
  }

  browser_->tab_strip_model()->RemoveFromGroup({tab_index});
}

void TabStripPageHandler::MoveGroup(const std::string& group_id_string,
                                    int32_t to_index) {
  if (to_index == -1) {
    to_index = browser_->tab_strip_model()->count();
  }

  auto* target_browser = browser_.get();
  Browser* source_browser =
      tab_strip_ui::GetBrowserWithGroupId(browser_->profile(), group_id_string);
  if (!source_browser) {
    return;
  }

  std::optional<tab_groups::TabGroupId> group_id =
      tab_strip_ui::GetTabGroupIdFromString(
          source_browser->tab_strip_model()->group_model(), group_id_string);
  TabGroup* group =
      source_browser->tab_strip_model()->group_model()->GetTabGroup(
          group_id.value());
  const gfx::Range tabs_in_group = group->ListTabs();

  if (source_browser == target_browser) {
    if (static_cast<int>(tabs_in_group.start()) == to_index) {
      // If the group is already in place, don't move it. This may happen
      // if multiple drag events happen while the tab group is still
      // being moved.
      return;
    }

    // When a group is moved, all the tabs in it need to be selected at the same
    // time. This mimics the way the native tab strip works and also allows
    // this handler to ignore the events for each individual tab moving.
    ui::ListSelectionModel group_selection;
    group_selection.SetSelectedIndex(tabs_in_group.start());
    group_selection.SetSelectionFromAnchorTo(tabs_in_group.end() - 1);
    group_selection.set_active(
        target_browser->tab_strip_model()->selection_model().active());
    target_browser->tab_strip_model()->SetSelectionFromModel(group_selection);

    target_browser->tab_strip_model()->MoveGroupTo(group_id.value(), to_index);
    return;
  }

  tab_strip_ui::MoveGroupAcrossWindows(source_browser, target_browser, to_index,
                                       group_id.value());
}

void TabStripPageHandler::MoveTab(int32_t tab_id, int32_t to_index) {
  if (to_index == -1) {
    to_index = browser_->tab_strip_model()->count();
  }

  extensions::WindowController* source_window = nullptr;
  int from_index = -1;
  if (!extensions::ExtensionTabUtil::GetTabById(tab_id, browser_->profile(),
                                                true, &source_window, nullptr,
                                                &from_index)) {
    return;
  }

  if (source_window->profile() != browser_->profile()) {
    return;
  }

  Browser* source_browser = source_window->GetBrowser();
  if (source_browser == browser_) {
    browser_->tab_strip_model()->MoveWebContentsAt(from_index, to_index, false);
    return;
  }

  std::optional<tab_groups::TabGroupId> to_group_id = std::nullopt;

  TabStripModel* target_tab_strip = browser_->GetTabStripModel();

  // If the tab is being inserted in the middle of a group, the tab should be a
  // part of the group.
  if (target_tab_strip->SupportsTabGroups()) {
    std::optional<tab_groups::TabGroupId> next_tab_dst_group =
        target_tab_strip->GetTabGroupForTab(to_index);
    std::optional<tab_groups::TabGroupId> prev_tab_dst_group =
        target_tab_strip->GetTabGroupForTab(to_index - 1);

    if (next_tab_dst_group.has_value() && prev_tab_dst_group.has_value() &&
        next_tab_dst_group == prev_tab_dst_group) {
      to_group_id = next_tab_dst_group;
    }
  }

  tab_strip_ui::MoveTabAcrossWindows(source_browser, from_index, browser_,
                                     to_index, to_group_id);
}

void TabStripPageHandler::CloseContainer() {
  // We only autoclose for tab selection.
  RecordTabStripUICloseHistogram(TabStripUICloseAction::kTabSelected);
  DCHECK(embedder_);
  embedder_->CloseContainer();
}

void TabStripPageHandler::CloseTab(int32_t tab_id, bool tab_was_swiped) {
  content::WebContents* tab = nullptr;
  if (!extensions::ExtensionTabUtil::GetTabById(tab_id, browser_->profile(),
                                                true, &tab)) {
    // ID didn't refer to a valid tab.
    DVLOG(1) << "Invalid tab ID";
    return;
  }

  if (tab_was_swiped) {
    // The unload tracker will automatically unobserve the tab when it
    // successfully closes.
    tab_before_unload_tracker_.Observe(tab);
  }
  tab->Close();
}

void TabStripPageHandler::ShowBackgroundContextMenu(int32_t location_x,
                                                    int32_t location_y) {
  gfx::PointF point(location_x, location_y);
  DCHECK(embedder_);
  embedder_->ShowContextMenuAtPoint(
      gfx::ToRoundedPoint(point),
      std::make_unique<WebUIBackgroundContextMenu>(
          browser_, embedder_->GetAcceleratorProvider()),
      base::BindRepeating(&TabStripPageHandler::NotifyContextMenuClosed,
                          weak_ptr_factory_.GetWeakPtr()));
}

void TabStripPageHandler::ShowEditDialogForGroup(
    const std::string& group_id_string,
    int32_t location_x,
    int32_t location_y,
    int32_t width,
    int32_t height) {
  std::optional<tab_groups::TabGroupId> group_id =
      tab_strip_ui::GetTabGroupIdFromString(
          browser_->tab_strip_model()->group_model(), group_id_string);
  if (!group_id.has_value()) {
    return;
  }

  gfx::Point point(location_x, location_y);
  gfx::Rect rect(width, height);
  DCHECK(embedder_);
  embedder_->ShowEditDialogForGroupAtPoint(point, rect, group_id.value());
}

void TabStripPageHandler::ShowTabContextMenu(int32_t tab_id,
                                             int32_t location_x,
                                             int32_t location_y) {
  extensions::WindowController* window = nullptr;
  int tab_index = -1;
  if (!extensions::ExtensionTabUtil::GetTabById(tab_id, browser_->profile(),
                                                /*include_incognito=*/true,
                                                &window, nullptr, &tab_index)) {
    return;
  }
  CHECK(window);  // Shouldn't be trying to do this for a prerender window.
  Browser* browser = window->GetBrowser();

  if (browser != browser_) {
    // TODO(crbug.com/40727240): Investigate how a context menu is being opened
    // for a tab that is no longer in the tab strip. Until then, fire a
    // tab-removed event so the tab is removed from this tab strip.
    page_->TabRemoved(tab_id);
    return;
  }

  DCHECK(embedder_);
  gfx::PointF point(location_x, location_y);
  embedder_->ShowContextMenuAtPoint(
      gfx::ToRoundedPoint(point),
      std::make_unique<WebUITabContextMenu>(
          browser, embedder_->GetAcceleratorProvider(), tab_index),
      base::BindRepeating(&TabStripPageHandler::NotifyContextMenuClosed,
                          weak_ptr_factory_.GetWeakPtr()));
  base::UmaHistogramEnumeration("TabStrip.Tab.WebUI.ActivationAction",
                                TabActivationTypes::kContextMenu);
}

void TabStripPageHandler::GetLayout(GetLayoutCallback callback) {
  TRACE_EVENT0("browser", "TabStripPageHandler:HandleGetLayout");
  std::move(callback).Run(std::move(embedder_->GetLayout().AsDictionary()));
}

void TabStripPageHandler::SetThumbnailTracked(int32_t tab_id,
                                              bool thumbnail_tracked) {
  TRACE_EVENT0("browser", "TabStripPageHandler:HandleSetThumbnailTracked");
  content::WebContents* tab = nullptr;
  if (!extensions::ExtensionTabUtil::GetTabById(tab_id, browser_->profile(),
                                                true, &tab)) {
    // ID didn't refer to a valid tab.
    DVLOG(1) << "Invalid tab ID";
    return;
  }

  if (thumbnail_tracked) {
    thumbnail_tracker_.AddTab(tab);
  } else {
    thumbnail_tracker_.RemoveTab(tab);
  }
}

void TabStripPageHandler::ReportTabActivationDuration(uint32_t duration_ms) {
  UMA_HISTOGRAM_TIMES("WebUITabStrip.TabActivation",
                      base::Milliseconds(duration_ms));
  base::UmaHistogramEnumeration("TabStrip.Tab.WebUI.ActivationAction",
                                TabActivationTypes::kTab);
}

void TabStripPageHandler::ReportTabDataReceivedDuration(uint32_t tab_count,
                                                        uint32_t duration_ms) {
  ReportTabDurationHistogram("TabDataReceived", tab_count,
                             base::Milliseconds(duration_ms));
}

void TabStripPageHandler::ReportTabCreationDuration(uint32_t tab_count,
                                                    uint32_t duration_ms) {
  ReportTabDurationHistogram("TabCreation", tab_count,
                             base::Milliseconds(duration_ms));
}

// Callback passed to |thumbnail_tracker_|. Called when a tab's thumbnail
// changes, or when we start watching the tab.
void TabStripPageHandler::HandleThumbnailUpdate(
    content::WebContents* tab,
    ThumbnailTracker::CompressedThumbnailData image) {
  // Send base-64 encoded image to JS side. If |image| is blank (i.e.
  // there is no data), send a blank URI.
  TRACE_EVENT0("browser", "TabStripPageHandler:HandleThumbnailUpdate");
  std::string data_uri;
  if (image) {
    data_uri = webui::MakeDataURIForImage(base::span(image->data), "jpeg");
  }

  const SessionID::id_type tab_id = extensions::ExtensionTabUtil::GetTabId(tab);
  page_->TabThumbnailUpdated(tab_id, data_uri);
}

void TabStripPageHandler::OnTabCloseCancelled(content::WebContents* tab) {
  tab_before_unload_tracker_.Unobserve(tab);
  const SessionID::id_type tab_id = extensions::ExtensionTabUtil::GetTabId(tab);
  page_->TabCloseCancelled(tab_id);
}

// Reports a histogram using the format
// WebUITabStrip.|histogram_fragment|.[tab count bucket].
void TabStripPageHandler::ReportTabDurationHistogram(
    const char* histogram_fragment,
    int tab_count,
    base::TimeDelta duration) {
  if (tab_count <= 0) {
    return;
  }

  // It isn't possible to report both a number of tabs and duration datapoint
  // together in a histogram or to correlate two histograms together. As a
  // result the histogram is manually bucketed.
  const char* tab_count_bucket = "01_05";
  if (6 <= tab_count && tab_count <= 20) {
    tab_count_bucket = "06_20";
  } else if (20 < tab_count) {
    tab_count_bucket = "21_";
  }

  std::string histogram_name = base::JoinString(
      {"WebUITabStrip", histogram_fragment, tab_count_bucket}, ".");
  base::UmaHistogramTimes(histogram_name, duration);
}

gfx::ImageSkia TabStripPageHandler::ThemeFavicon(const gfx::ImageSkia& source,
                                                 bool active_tab_icon) {
  if (active_tab_icon) {
    return favicon::ThemeFavicon(
        source, embedder_->GetColorProviderColor(kColorThumbnailTabForeground),
        embedder_->GetColorProviderColor(kColorThumbnailTabBackground),
        embedder_->GetColorProviderColor(kColorThumbnailTabBackground));
  }

  return favicon::ThemeFavicon(
      source, embedder_->GetColorProviderColor(kColorToolbarButtonIcon),
      embedder_->GetColorProviderColor(kColorTabBackgroundActiveFrameActive),
      embedder_->GetColorProviderColor(kColorTabBackgroundInactiveFrameActive));
}

void TabStripPageHandler::ActivateTab(int32_t tab_id) {
  TabStripModel* tab_strip_model = browser_->tab_strip_model();
  for (int index = 0; index < tab_strip_model->count(); ++index) {
    content::WebContents* contents = tab_strip_model->GetWebContentsAt(index);
    if (extensions::ExtensionTabUtil::GetTabId(contents) == tab_id) {
      tab_strip_model->ActivateTabAt(index);
    }
  }
}

void TabStripPageHandler::OnThemeChanged() {
  page_->ThemeChanged();
}

void TabStripPageHandler::OnNativeThemeUpdated(
    ui::NativeTheme* observed_theme) {
  // There are two types of theme update. a) The observed theme change. e.g.
  // switch between light/dark mode. b) A different theme is enabled. e.g.
  // switch between GTK and classic theme on Linux. Reset observer in case b).
  ui::NativeTheme* current_theme =
      webui::GetNativeThemeDeprecated(web_ui_->GetWebContents());
  if (observed_theme != current_theme) {
    theme_observation_.Reset();
    theme_observation_.Observe(current_theme);
  }
  page_->ThemeChanged();
}