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 12 namespace { 13 14 // Maintains and gives access to a static list of TabModel instances. 15 static TabModelList::TabModelVector& tab_models() { 16 CR_DEFINE_STATIC_LOCAL(TabModelList::TabModelVector, 17 tab_model_vector, ()); 18 return tab_model_vector; 19 } 20 21 } // namespace 22 23 void TabModelList::AddTabModel(TabModel* tab_model) { 24 DCHECK(tab_model); 25 tab_models().push_back(tab_model); 26 } 27 28 void TabModelList::RemoveTabModel(TabModel* tab_model) { 29 DCHECK(tab_model); 30 TabModelList::iterator remove_tab_model = 31 std::find(tab_models().begin(), tab_models().end(), tab_model); 32 33 if (remove_tab_model != tab_models().end()) 34 tab_models().erase(remove_tab_model); 35 } 36 37 void TabModelList::HandlePopupNavigation(chrome::NavigateParams* params) { 38 TabAndroid* tab = TabAndroid::FromWebContents(params->source_contents); 39 40 // NOTE: If this fails contact dtrainor@. 41 DCHECK(tab); 42 tab->HandlePopupNavigation(params); 43 } 44 45 46 TabModel* TabModelList::GetTabModelWithProfile( 47 Profile* profile) { 48 if (!profile) 49 return NULL; 50 51 for (TabModelList::const_iterator i = TabModelList::begin(); 52 i != TabModelList::end(); ++i) { 53 Profile* model_profile = (*i)->GetProfile(); 54 if (profile->IsSameProfile(model_profile) && 55 profile->IsOffTheRecord() == model_profile->IsOffTheRecord()) { 56 return *i; 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