• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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_BROWSER_ITERATOR_H_
6 #define CHROME_BROWSER_UI_BROWSER_ITERATOR_H_
7 
8 #include "chrome/browser/ui/browser_list.h"
9 #include "chrome/browser/ui/host_desktop.h"
10 
11 class Browser;
12 
13 namespace chrome {
14 
15 // Iterates over all existing browsers (potentially across multiple desktops).
16 // Note: to iterate only over the browsers of a specific desktop, use the
17 // const_iterator of a given BrowserList instead.
18 //
19 // Example:
20 //   for (BrowserIterator iterator; !iterator.done(); iterator.Next()) {
21 //     Browser* cur = *iterator;
22 //     -or-
23 //     iterator->OperationOnBrowser();
24 //     ...
25 //   }
26 class BrowserIterator {
27  public:
28   BrowserIterator();
29   ~BrowserIterator();
30 
31   // Returns true if this iterator is past the last Browser.
done()32   bool done() const {
33     // |current_iterator_| is never at the end of a list unless it is done (it
34     // immediately moves to the next browser list upon hitting the end of the
35     // current list unless there are no remaining empty browser lists).
36     return current_iterator_ == current_browser_list_->end();
37   }
38 
39   // Returns the current Browser, valid as long as !done().
40   Browser* operator->() const {
41     return *current_iterator_;
42   }
43   Browser* operator*() const {
44     return *current_iterator_;
45   }
46 
47   // Advances |current_iterator_| to the next browser.
48   void Next();
49 
50  private:
51   // If |current_iterator_| is at |current_browser_list_->end()|, advance to the
52   // next non-empty browser list. After a call to this method: either
53   // |current_iterator_| is valid or done().
54   void NextBrowserListIfAtEnd();
55 
56   // The BrowserList currently being iterated over. Instances of this class do
57   // not own this pointer.
58   BrowserList* current_browser_list_;
59 
60   // The underlying iterator over browsers in |current_browser_list_|.
61   BrowserList::const_iterator current_iterator_;
62 
63   // The next HostDesktopType to iterate over when |current_iterator_| reaches
64   // |current_browser_list_->end()|.
65   HostDesktopType next_desktop_type_;
66 
67   DISALLOW_COPY_AND_ASSIGN(BrowserIterator);
68 };
69 
70 }  // namespace chrome
71 
72 #endif  // CHROME_BROWSER_UI_BROWSER_ITERATOR_H_
73