tab_contents_iterator.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
6#define CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "chrome/browser/ui/browser_list.h"
12
13class TabContents;
14
15// Iterates through all web view hosts in all browser windows. Because the
16// renderers act asynchronously, getting a host through this interface does
17// not guarantee that the renderer is ready to go. Doing anything to affect
18// browser windows or tabs while iterating may cause incorrect behavior.
19//
20// Example:
21//   for (TabContentsIterator iterator; !iterator.done(); ++iterator) {
22//     TabContents* cur = *iterator;
23//     -or-
24//     iterator->operationOnTabContents();
25//     ...
26//   }
27class TabContentsIterator {
28 public:
29  TabContentsIterator();
30
31  // Returns true if we are past the last Browser.
32  bool done() const { return cur_ == NULL; }
33
34  // Returns the Browser instance associated with the current
35  // TabContents. Valid as long as !done()
36  Browser* browser() const {
37    if (browser_iterator_ != BrowserList::end())
38      return *browser_iterator_;
39    return NULL;
40  }
41
42  // Returns the current TabContents, valid as long as !Done()
43  TabContents* operator->() const {
44    return cur_;
45  }
46  TabContents* operator*() const {
47    return cur_;
48  }
49
50  // Incrementing operators, valid as long as !Done()
51  TabContents* operator++() {  // ++preincrement
52    Advance();
53    return cur_;
54  }
55  TabContents* operator++(int) {  // postincrement++
56    TabContents* tmp = cur_;
57    Advance();
58    return tmp;
59  }
60
61 private:
62  // Loads the next host into Cur. This is designed so that for the initial
63  // call when browser_iterator_ points to the first browser and
64  // web_view_index_ is -1, it will fill the first host.
65  void Advance();
66
67  // Iterator over all the Browser objects.
68  BrowserList::const_iterator browser_iterator_;
69
70  // tab index into the current Browser of the current web view
71  int web_view_index_;
72
73  // iterator over the TabContentss doing background printing.
74  std::set<TabContents*>::const_iterator bg_printing_iterator_;
75
76  // Current TabContents, or NULL if we're at the end of the list. This
77  // can be extracted given the browser iterator and index, but it's nice to
78  // cache this since the caller may access the current host many times.
79  TabContents* cur_;
80
81  DISALLOW_COPY_AND_ASSIGN(TabContentsIterator);
82};
83
84#endif  // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
85