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/android/tab_model/tab_model_list.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/android/tab_android.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model.h"
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/web_contents.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Maintains and gives access to a static list of TabModel instances.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static TabModelList::TabModelVector& tab_models() {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CR_DEFINE_STATIC_LOCAL(TabModelList::TabModelVector,
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         tab_model_vector, ());
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return tab_model_vector;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabModelList::AddTabModel(TabModel* tab_model) {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(tab_model);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  tab_models().push_back(tab_model);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabModelList::RemoveTabModel(TabModel* tab_model) {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(tab_model);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TabModelList::iterator remove_tab_model =
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::find(tab_models().begin(), tab_models().end(), tab_model);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (remove_tab_model != tab_models().end())
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    tab_models().erase(remove_tab_model);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabModelList::HandlePopupNavigation(chrome::NavigateParams* params) {
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TabAndroid* tab = TabAndroid::FromWebContents(params->source_contents);
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // NOTE: If this fails contact dtrainor@.
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(tab);
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  tab->HandlePopupNavigation(params);
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TabModel* TabModelList::GetTabModelForWebContents(
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::WebContents* web_contents) {
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!web_contents)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (TabModelList::const_iterator i = TabModelList::begin();
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      i != TabModelList::end(); ++i) {
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    TabModel* model = *i;
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for (int index = 0; index < model->GetTabCount(); index++) {
55e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch      if (web_contents == model->GetWebContentsAt(index))
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        return model;
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabModel* TabModelList::FindTabModelWithId(
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SessionID::id_type desired_id) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (TabModelList::const_iterator i = TabModelList::begin();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      i != TabModelList::end(); i++) {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((*i)->GetSessionId() == desired_id)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return *i;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabModelList::IsOffTheRecordSessionActive() {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (TabModelList::const_iterator i = TabModelList::begin();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      i != TabModelList::end(); i++) {
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if ((*i)->IsOffTheRecord() && (*i)->GetTabCount() > 0)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabModelList::const_iterator TabModelList::begin() {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return tab_models().begin();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabModelList::const_iterator TabModelList::end() {
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return tab_models().end();
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabModelList::empty() {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return tab_models().empty();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)size_t TabModelList::size() {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return tab_models().size();
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TabModel* TabModelList::get(size_t index) {
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK_LT(index, size());
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return tab_models()[index];
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
104