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_iterator.h"
12
13namespace content {
14class WebContents;
15}
16
17// Iterates through all web view hosts in all browser windows. Because the
18// renderers act asynchronously, getting a host through this interface does
19// not guarantee that the renderer is ready to go. Doing anything to affect
20// browser windows or tabs while iterating may cause incorrect behavior.
21//
22// Example:
23//   for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) {
24//     WebContents* cur = *iterator;
25//     -or-
26//     iterator->OperationOnWebContents();
27//     ...
28//   }
29class TabContentsIterator {
30 public:
31  TabContentsIterator();
32
33  // Returns true if we are past the last Browser.
34  bool done() const { return cur_ == NULL; }
35
36  // Returns the Browser instance associated with the current
37  // WebContents. Valid as long as !done().
38  Browser* browser() const {
39    if (!browser_iterator_.done())
40      return *browser_iterator_;
41    return NULL;
42  }
43
44  // Returns the current WebContents, valid as long as !done().
45  content::WebContents* operator->() const {
46    return cur_;
47  }
48  content::WebContents* operator*() const {
49    return cur_;
50  }
51
52  // Loads the next host into |cur_|. This is designed so that for the initial
53  // call from the constructor, when browser_iterator_ points to the first
54  // Browser and web_view_index_ is -1, it will fill the first host.
55  void Next();
56
57 private:
58  // Tab index into the current Browser of the current web view.
59  int web_view_index_;
60
61  // Current WebContents, or NULL if we're at the end of the list. This
62  // can be extracted given the browser iterator and index, but it's nice to
63  // cache this since the caller may access the current host many times.
64  content::WebContents* cur_;
65
66  // An iterator over all the browsers.
67  chrome::BrowserIterator browser_iterator_;
68
69  DISALLOW_COPY_AND_ASSIGN(TabContentsIterator);
70};
71
72#endif  // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_
73