Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / tab_model / tab_model_list.cc
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 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
6
7 #include "chrome/browser/android/tab_android.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/android/tab_model/tab_model.h"
10 #include "chrome/browser/ui/browser_navigator.h"
11 #include "content/public/browser/web_contents.h"
12
13 namespace {
14
15 // Maintains and gives access to a static list of TabModel instances.
16 static TabModelList::TabModelVector& tab_models() {
17   CR_DEFINE_STATIC_LOCAL(TabModelList::TabModelVector,
18                          tab_model_vector, ());
19   return tab_model_vector;
20 }
21
22 }  // namespace
23
24 void TabModelList::AddTabModel(TabModel* tab_model) {
25   DCHECK(tab_model);
26   tab_models().push_back(tab_model);
27 }
28
29 void TabModelList::RemoveTabModel(TabModel* tab_model) {
30   DCHECK(tab_model);
31   TabModelList::iterator remove_tab_model =
32       std::find(tab_models().begin(), tab_models().end(), tab_model);
33
34   if (remove_tab_model != tab_models().end())
35     tab_models().erase(remove_tab_model);
36 }
37
38 void TabModelList::HandlePopupNavigation(chrome::NavigateParams* params) {
39   TabAndroid* tab = TabAndroid::FromWebContents(params->source_contents);
40
41   // NOTE: If this fails contact dtrainor@.
42   DCHECK(tab);
43   tab->HandlePopupNavigation(params);
44 }
45
46 TabModel* TabModelList::GetTabModelForWebContents(
47     content::WebContents* web_contents) {
48   if (!web_contents)
49     return NULL;
50
51   for (TabModelList::const_iterator i = TabModelList::begin();
52       i != TabModelList::end(); ++i) {
53     TabModel* model = *i;
54     for (int index = 0; index < model->GetTabCount(); index++) {
55       if (web_contents == model->GetWebContentsAt(index))
56         return model;
57     }
58   }
59
60   return NULL;
61 }
62
63 TabModel* TabModelList::FindTabModelWithId(
64     SessionID::id_type desired_id) {
65   for (TabModelList::const_iterator i = TabModelList::begin();
66       i != TabModelList::end(); i++) {
67     if ((*i)->GetSessionId() == desired_id)
68       return *i;
69   }
70
71   return NULL;
72 }
73
74 bool TabModelList::IsOffTheRecordSessionActive() {
75   for (TabModelList::const_iterator i = TabModelList::begin();
76       i != TabModelList::end(); i++) {
77     if ((*i)->IsOffTheRecord() && (*i)->GetTabCount() > 0)
78       return true;
79   }
80
81   return false;
82 }
83
84 TabModelList::const_iterator TabModelList::begin() {
85   return tab_models().begin();
86 }
87
88 TabModelList::const_iterator TabModelList::end() {
89   return tab_models().end();
90 }
91
92 bool TabModelList::empty() {
93   return tab_models().empty();
94 }
95
96 size_t TabModelList::size() {
97   return tab_models().size();
98 }
99
100 TabModel* TabModelList::get(size_t index) {
101   DCHECK_LT(index, size());
102   return tab_models()[index];
103 }