File: tab_collection_storage.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (146 lines) | stat: -rw-r--r-- 5,182 bytes parent folder | download | duplicates (3)
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
// 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();
}

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();
}

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