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

ash / public / cpp / tab_cluster / tab_cluster_ui_controller.cc [blame]

// 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 "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/ranges/algorithm.h"

namespace ash {

TabClusterUIController::TabClusterUIController() = default;

TabClusterUIController::~TabClusterUIController() = default;

TabClusterUIItem* TabClusterUIController::AddTabItem(
    std::unique_ptr<TabClusterUIItem> tab_item) {
  auto* tab_item_raw = tab_item.get();
  tab_items_.push_back(std::move(tab_item));
  for (auto& observer : observers_)
    observer.OnTabItemAdded(tab_item_raw);
  return tab_item_raw;
}

void TabClusterUIController::UpdateTabItem(TabClusterUIItem* tab_item) {
  DCHECK(base::Contains(tab_items_, tab_item,
                        &std::unique_ptr<TabClusterUIItem>::get));
  for (auto& observer : observers_)
    observer.OnTabItemUpdated(tab_item);
}

void TabClusterUIController::RemoveTabItem(TabClusterUIItem* tab_item) {
  auto iter = base::ranges::find(tab_items_, tab_item,
                                 &std::unique_ptr<TabClusterUIItem>::get);
  DCHECK(iter != tab_items_.end());
  // Since observer may need to use item values, notify observer before removing
  // from item list.
  for (auto& observer : observers_)
    observer.OnTabItemRemoved(tab_item);
  tab_items_.erase(iter);
}

void TabClusterUIController::ChangeActiveCandidate(
    TabClusterUIItem* old_active_item,
    TabClusterUIItem* new_active_item) {
  for (auto* const tab_item : clusterer_.GetUpdatedClusterInfo(
           tab_items_, old_active_item, new_active_item)) {
    for (auto& observer : observers_)
      observer.OnTabItemUpdated(tab_item);
  }
}

void TabClusterUIController::AddObserver(
    TabClusterUIController::Observer* observer) {
  observers_.AddObserver(observer);
}

void TabClusterUIController::RemoveObserver(
    TabClusterUIController::Observer* observer) {
  observers_.RemoveObserver(observer);
}

}  // namespace ash