OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_ | 5 #ifndef CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_ |
6 #define CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_ | 6 #define CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "chrome/browser/ui/browser_iterator.h" | 11 #include "chrome/browser/ui/browser_list.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 class WebContents; | 14 class WebContents; |
15 } | 15 } |
16 | 16 |
17 // Iterates through all web view hosts in all browser windows. Because the | 17 // Iterates through all web view hosts in all browser windows. Because the |
18 // renderers act asynchronously, getting a host through this interface does | 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 | 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. | 20 // browser windows or tabs while iterating may cause incorrect behavior. |
21 // | 21 // |
22 // Example: | 22 // Example: |
23 // for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) { | 23 // for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) { |
24 // WebContents* cur = *iterator; | 24 // WebContents* cur = *iterator; |
25 // -or- | 25 // -or- |
26 // iterator->OperationOnWebContents(); | 26 // iterator->OperationOnWebContents(); |
27 // ... | 27 // ... |
28 // } | 28 // } |
29 class TabContentsIterator { | 29 class TabContentsIterator { |
30 public: | 30 public: |
31 TabContentsIterator(); | 31 TabContentsIterator(); |
| 32 ~TabContentsIterator(); |
32 | 33 |
33 // Returns true if we are past the last Browser. | 34 // Returns true if we are past the last Browser. |
34 bool done() const { return cur_ == NULL; } | 35 bool done() const { return cur_ == NULL; } |
35 | 36 |
36 // Returns the Browser instance associated with the current | 37 // Returns the Browser instance associated with the current |
37 // WebContents. Valid as long as !done(). | 38 // WebContents. Valid as long as !done(). |
38 Browser* browser() const { | 39 Browser* browser() const { |
39 if (!browser_iterator_.done()) | 40 if (browser_iterator_ != BrowserList::GetInstance()->end()) |
40 return *browser_iterator_; | 41 return *browser_iterator_; |
41 return NULL; | 42 return NULL; |
42 } | 43 } |
43 | 44 |
44 // Returns the current WebContents, valid as long as !done(). | 45 // Returns the current WebContents, valid as long as !done(). |
45 content::WebContents* operator->() const { | 46 content::WebContents* operator->() const { |
46 return cur_; | 47 return cur_; |
47 } | 48 } |
48 content::WebContents* operator*() const { | 49 content::WebContents* operator*() const { |
49 return cur_; | 50 return cur_; |
50 } | 51 } |
51 | 52 |
52 // Loads the next host into |cur_|. This is designed so that for the initial | 53 // 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 // 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 // Browser and web_view_index_ is -1, it will fill the first host. |
55 void Next(); | 56 void Next(); |
56 | 57 |
57 private: | 58 private: |
58 // Tab index into the current Browser of the current web view. | 59 // Tab index into the current Browser of the current web view. |
59 int web_view_index_; | 60 int web_view_index_; |
60 | 61 |
61 // Current WebContents, or NULL if we're at the end of the list. This | 62 // 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 // 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 // cache this since the caller may access the current host many times. |
64 content::WebContents* cur_; | 65 content::WebContents* cur_; |
65 | 66 |
66 // An iterator over all the browsers. | 67 // An iterator over all the browsers. |
67 chrome::BrowserIterator browser_iterator_; | 68 BrowserList::const_iterator browser_iterator_; |
68 | 69 |
69 DISALLOW_COPY_AND_ASSIGN(TabContentsIterator); | 70 DISALLOW_COPY_AND_ASSIGN(TabContentsIterator); |
70 }; | 71 }; |
71 | 72 |
72 #endif // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_ | 73 #endif // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_ITERATOR_H_ |
OLD | NEW |