File: tab_contents_iterator.h

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 (110 lines) | stat: -rw-r--r-- 3,390 bytes parent folder | download | duplicates (5)
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_