File: tab_cluster_ui_client.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,378 bytes parent folder | download | duplicates (6)
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
// 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/ash/wm/tab_cluster_ui_client.h"

#include "ash/public/cpp/tab_cluster/tab_cluster_ui_controller.h"
#include "ash/public/cpp/tab_cluster/tab_cluster_ui_item.h"
#include "base/containers/contains.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "content/public/browser/web_contents.h"

namespace {

// Generate tab item info from a web contents.
ash::TabClusterUIItem::Info GenerateTabItemInfo(
    content::WebContents* web_contents) {
  ash::TabClusterUIItem::Info info;
  info.title = base::UTF16ToUTF8(web_contents->GetTitle());
  info.source = web_contents->GetVisibleURL().possibly_invalid_spec();
  info.browser_window =
      chrome::FindBrowserWithTab(web_contents)->window()->GetNativeWindow();
  info.is_loading = web_contents->ShouldShowLoadingUI();
  return info;
}

}  //  namespace

TabClusterUIClient::TabClusterUIClient(ash::TabClusterUIController* controller)
    : controller_(controller), browser_tab_strip_tracker_(this, nullptr) {
  browser_tab_strip_tracker_.Init();
}

TabClusterUIClient::~TabClusterUIClient() = default;

void TabClusterUIClient::OnTabStripModelChanged(
    TabStripModel* tab_strip_model,
    const TabStripModelChange& change,
    const TabStripSelectionChange& selection) {
  switch (change.type()) {
    case TabStripModelChange::kInserted:
      // Add new items corresponding to the inserted web contents.
      for (const auto& contents : change.GetInsert()->contents) {
        content::WebContents* web_contents = contents.contents;
        auto* item =
            controller_->AddTabItem(std::make_unique<ash::TabClusterUIItem>(
                GenerateTabItemInfo(web_contents)));
        contents_item_map_[web_contents] = item;
      }
      break;
    case TabStripModelChange::kRemoved:
      // Remove the items corresponding to the removed web contents.
      for (const auto& contents : change.GetRemove()->contents) {
        content::WebContents* web_contents = contents.contents;
        DCHECK(base::Contains(contents_item_map_, web_contents));
        ash::TabClusterUIItem* item = contents_item_map_[web_contents];
        contents_item_map_.erase(web_contents);
        controller_->RemoveTabItem(item);
      }
      break;
    case TabStripModelChange::kReplaced:
      // Update the item whose corresponding contents are replaced.
      {
        auto* replace = change.GetReplace();
        DCHECK(base::Contains(contents_item_map_, replace->old_contents));
        auto* item = contents_item_map_[replace->old_contents].get();

        item->Init(GenerateTabItemInfo(replace->new_contents));
        controller_->UpdateTabItem(item);

        contents_item_map_[replace->new_contents] = item;
        contents_item_map_.erase(replace->old_contents);
      }
      break;
    case TabStripModelChange::kMoved:
      break;
    case TabStripModelChange::kSelectionOnly:
      break;
  }
  if (selection.active_tab_changed() && !tab_strip_model->empty()) {
    auto* old_active_item =
        base::Contains(contents_item_map_, selection.old_contents)
            ? contents_item_map_[selection.old_contents].get()
            : nullptr;
    auto* new_active_item = contents_item_map_[selection.new_contents].get();
    controller_->ChangeActiveCandidate(old_active_item, new_active_item);
  }
}

void TabClusterUIClient::TabChangedAt(content::WebContents* contents,
                                      int index,
                                      TabChangeType change_type) {
  // Some tests may manually add tabs to browser such that the newly added tabs
  // may start loading before being inserted into the tab strip.
  if (!base::Contains(contents_item_map_, contents)) {
    return;
  }
  auto* item = contents_item_map_[contents].get();

  // If there is only loading progress change, we only update item when the
  // state changes between loading and loaded.
  if (change_type == TabChangeType::kLoadingOnly &&
      item->current_info().is_loading == contents->ShouldShowLoadingUI()) {
    return;
  }
  item->Init(GenerateTabItemInfo(contents));
  controller_->UpdateTabItem(item);
}