File: tab_collection_storage.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 (160 lines) | stat: -rw-r--r-- 5,613 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/tabs/public/tab_collection_storage.h"

#include <memory>

#include "base/notimplemented.h"
#include "components/tabs/public/tab_collection.h"
#include "components/tabs/public/tab_interface.h"

namespace tabs {

TabCollectionStorage::TabCollectionStorage(TabCollection& owner)
    : owning_collection_(owner) {}

TabCollectionStorage::~TabCollectionStorage() = default;

bool TabCollectionStorage::ContainsTab(const TabInterface* tab) const {
  CHECK(tab);
  return GetIndexOfTab(tab).has_value();
}

TabInterface* TabCollectionStorage::GetTabAtIndex(size_t index) const {
  CHECK(index < GetChildrenCount() && index >= 0);
  CHECK(std::holds_alternative<std::unique_ptr<tabs::TabInterface>>(
      children_[index]));
  const std::unique_ptr<tabs::TabInterface>& tab =
      std::get<std::unique_ptr<tabs::TabInterface>>(children_[index]);
  return tab.get();
}

bool TabCollectionStorage::ContainsCollection(
    TabCollection* tab_collection) const {
  CHECK(tab_collection);
  return GetIndexOfCollection(tab_collection).has_value();
}

TabInterface* TabCollectionStorage::AddTab(std::unique_ptr<TabInterface> tab,
                                           size_t index) {
  CHECK(index <= GetChildrenCount() && index >= 0);
  CHECK(tab);

  TabInterface* tab_ptr = tab.get();
  children_.insert(children_.begin() + index, std::move(tab));
  owning_collection_->OnTabAddedToTree();
  return tab_ptr;
}

void TabCollectionStorage::MoveTab(TabInterface* tab, size_t dst_index) {
  CHECK(tab);
  CHECK(dst_index < GetChildrenCount() && dst_index >= 0);
  std::unique_ptr<TabInterface> tab_to_move = RemoveTab(tab);
  CHECK(tab_to_move);
  AddTab(std::move(tab_to_move), dst_index);
}

std::unique_ptr<TabInterface> TabCollectionStorage::RemoveTab(
    TabInterface* tab) {
  CHECK(tab);
  for (size_t i = 0; i < children_.size(); ++i) {
    if (std::holds_alternative<std::unique_ptr<TabInterface>>(children_[i])) {
      std::unique_ptr<TabInterface>& stored_tab =
          std::get<std::unique_ptr<TabInterface>>(children_[i]);
      if (stored_tab.get() == tab) {
        auto removed_tab = std::move(stored_tab);
        children_.erase(children_.begin() + i);
        owning_collection_->OnTabRemovedFromTree();
        return removed_tab;
      }
    }
  }
  NOTREACHED();
}

void TabCollectionStorage::CloseTab(TabInterface* tab) {
  CHECK(tab);
  std::unique_ptr<TabInterface> removed_tab = RemoveTab(tab);
  removed_tab.reset();
}

TabCollection* TabCollectionStorage::AddCollection(
    std::unique_ptr<TabCollection> collection,
    size_t index) {
  CHECK(index <= GetChildrenCount() && index >= 0);
  CHECK(collection);

  TabCollection* collection_ptr = collection.get();
  children_.insert(children_.begin() + index, std::move(collection));
  owning_collection_->OnCollectionAddedToTree(collection_ptr);
  return collection_ptr;
}

void TabCollectionStorage::MoveCollection(TabCollection* collection,
                                          size_t dst_index) {
  CHECK(collection);
  CHECK(dst_index < GetChildrenCount() && dst_index >= 0);
  std::unique_ptr<TabCollection> tab_collection_to_move =
      RemoveCollection(collection);
  CHECK(tab_collection_to_move);
  AddCollection(std::move(tab_collection_to_move), dst_index);
}

std::unique_ptr<TabCollection> TabCollectionStorage::RemoveCollection(
    TabCollection* collection) {
  CHECK(collection);
  for (size_t i = 0; i < children_.size(); ++i) {
    if (std::holds_alternative<std::unique_ptr<TabCollection>>(children_[i])) {
      std::unique_ptr<TabCollection>& stored_tab_collection =
          std::get<std::unique_ptr<TabCollection>>(children_[i]);
      if (stored_tab_collection.get() == collection) {
        auto removed_tab_collection = std::move(stored_tab_collection);
        children_.erase(children_.begin() + i);
        owning_collection_->OnCollectionRemovedFromTree(
            removed_tab_collection.get());
        return removed_tab_collection;
      }
    }
  }
  NOTREACHED();
}

void TabCollectionStorage::CloseCollection(TabCollection* collection) {
  // This should free all the children as well.
  CHECK(collection);
  std::unique_ptr<TabCollection> removed_tab_collection =
      RemoveCollection(collection);
  removed_tab_collection.reset();
}

std::optional<size_t> TabCollectionStorage::GetIndexOfTab(
    const TabInterface* const tab) const {
  CHECK(tab);
  const auto it = std::find_if(
      children_.begin(), children_.end(), [tab](const auto& child) {
        return std::holds_alternative<std::unique_ptr<TabInterface>>(child) &&
               std::get<std::unique_ptr<TabInterface>>(child).get() == tab;
      });
  return it == children_.end() ? std::nullopt
                               : std::optional<size_t>(it - children_.begin());
}

std::optional<size_t> TabCollectionStorage::GetIndexOfCollection(
    TabCollection* tab_collection) const {
  CHECK(tab_collection);
  const auto it = std::find_if(
      children_.begin(), children_.end(), [tab_collection](const auto& child) {
        return std::holds_alternative<std::unique_ptr<TabCollection>>(child) &&
               std::get<std::unique_ptr<TabCollection>>(child).get() ==
                   tab_collection;
      });
  return it == children_.end() ? std::nullopt
                               : std::optional<size_t>(it - children_.begin());
}

size_t TabCollectionStorage::GetChildrenCount() const {
  return children_.size();
}
}  // namespace tabs