15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabContentsIterator::TabContentsIterator()
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : web_view_index_(-1),
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cur_(NULL) {
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Load the first WebContents into |cur_|.
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Next();
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabContentsIterator::Next() {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The current WebContents should be valid unless we are at the beginning.
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(cur_ || web_view_index_ == -1) << "Trying to advance past the end";
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Update |cur_| to the next WebContents in the list.
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (!browser_iterator_.done()) {
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (++web_view_index_ >= browser_iterator_->tab_strip_model()->count()) {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Advance to the next Browser in the list.
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser_iterator_.Next();
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      web_view_index_ = -1;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::WebContents* next_tab = browser_iterator_->tab_strip_model()
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ->GetWebContentsAt(web_view_index_);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (next_tab) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cur_ = next_tab;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Reached the end.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cur_ = NULL;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
42