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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
#define CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
#include <iterator>
#include <optional>
#include "base/memory/stack_allocated.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
namespace content {
class WebContents;
}
// Iterates through all tab contents in all browser windows. Because the
// renderers act asynchronously, getting a tab contents through this interface
// does not guarantee that the renderer is ready to go. Doing anything to affect
// browser windows or tabs while iterating may cause incorrect behavior.
//
// Examples:
//
// for (auto* web_contents : AllTabContentses()) {
// SomeFunctionTakingWebContents(web_contents);
// -or-
// web_contents->OperationOnWebContents();
// ...
// }
//
// auto& all_tabs = AllTabContentses();
// auto it = some_std_algorithm(all_tabs.begin(), all_tabs.end(), ...);
class AllTabContentsesList {
public:
class Iterator {
STACK_ALLOCATED();
public:
using iterator_category = std::forward_iterator_tag;
using value_type = content::WebContents*;
using difference_type = ptrdiff_t;
using pointer = value_type*;
using reference = const value_type&;
Iterator();
Iterator(const Iterator& iterator);
~Iterator();
value_type operator->() const { return cur_; }
reference operator*() const { return cur_; }
Iterator& operator++() {
Next();
return *this;
}
Iterator operator++(int) {
Iterator it(*this);
Next();
return it;
}
bool operator==(const Iterator& other) const { return cur_ == other.cur_; }
// Returns the Browser instance associated with the current tab contents.
// Valid as long as this iterator != the AllTabContentses().end() iterator.
Browser* browser() const {
return browser_iterator_ == BrowserList::GetInstance()->end()
? nullptr
: *browser_iterator_;
}
private:
friend class AllTabContentsesList;
explicit Iterator(bool is_end_iter);
// Loads the next tab contents into |cur_|. This is designed so that for the
// initial call from the constructor, when |browser_iterator_| points to the
// first Browser and |tab_index_| is -1, it will fill the first tab
// contents.
void Next();
// Current WebContents, or null if we're at the end of the list. This can be
// extracted given the browser iterator and index, but it's nice to cache
// this since the caller may access the current tab contents many times.
content::WebContents* cur_;
// An iterator over all the browsers.
BrowserList::const_iterator browser_iterator_;
// An iterator for tabs in a tabstrip.
std::optional<TabStripModel::TabIterator> tab_iterator_;
};
using iterator = Iterator;
using const_iterator = Iterator;
const_iterator begin() const { return Iterator(false); }
const_iterator end() const { return Iterator(true); }
AllTabContentsesList() = default;
~AllTabContentsesList() = default;
};
const AllTabContentsesList& AllTabContentses();
#endif // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
|