OLD | NEW |
(Empty) | |
| 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/browser_finder.h" |
| 6 |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/ui/browser_list.h" |
| 9 #include "chrome/browser/ui/browser_window.h" |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "content/public/browser/navigation_controller.h" |
| 13 |
| 14 using content::WebContents; |
| 15 |
| 16 namespace browser { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Type used to indicate to match anything. |
| 21 const int kMatchAny = 0; |
| 22 |
| 23 // See BrowserMatches for details. |
| 24 const int kMatchOriginalProfile = 1 << 0; |
| 25 const int kMatchCanSupportWindowFeature = 1 << 1; |
| 26 const int kMatchTabbed = 1 << 2; |
| 27 |
| 28 // Returns true if the specified |browser| matches the specified arguments. |
| 29 // |match_types| is a bitmask dictating what parameters to match: |
| 30 // . If it contains kMatchOriginalProfile then the original profile of the |
| 31 // browser must match |profile->GetOriginalProfile()|. This is used to match |
| 32 // incognito windows. |
| 33 // . If it contains kMatchCanSupportWindowFeature |
| 34 // |CanSupportWindowFeature(window_feature)| must return true. |
| 35 // . If it contains kMatchTabbed, the browser must be a tabbed browser. |
| 36 bool BrowserMatches(Browser* browser, |
| 37 Profile* profile, |
| 38 Browser::WindowFeature window_feature, |
| 39 uint32 match_types) { |
| 40 if (match_types & kMatchCanSupportWindowFeature && |
| 41 !browser->CanSupportWindowFeature(window_feature)) { |
| 42 return false; |
| 43 } |
| 44 |
| 45 if (match_types & kMatchOriginalProfile) { |
| 46 if (browser->profile()->GetOriginalProfile() != |
| 47 profile->GetOriginalProfile()) |
| 48 return false; |
| 49 } else if (browser->profile() != profile) { |
| 50 return false; |
| 51 } |
| 52 |
| 53 if (match_types & kMatchTabbed) |
| 54 return browser->is_type_tabbed(); |
| 55 |
| 56 return true; |
| 57 } |
| 58 |
| 59 // Returns the first browser in the specified iterator that returns true from |
| 60 // |BrowserMatches|, or null if no browsers match the arguments. See |
| 61 // |BrowserMatches| for details on the arguments. |
| 62 template <class T> |
| 63 Browser* FindBrowserMatching(const T& begin, |
| 64 const T& end, |
| 65 Profile* profile, |
| 66 Browser::WindowFeature window_feature, |
| 67 uint32 match_types) { |
| 68 for (T i = begin; i != end; ++i) { |
| 69 if (BrowserMatches(*i, profile, window_feature, match_types)) |
| 70 return *i; |
| 71 } |
| 72 return NULL; |
| 73 } |
| 74 |
| 75 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile, |
| 76 bool match_tabbed, |
| 77 bool match_original_profiles) { |
| 78 uint32 match_types = kMatchAny; |
| 79 if (match_tabbed) |
| 80 match_types |= kMatchTabbed; |
| 81 if (match_original_profiles) |
| 82 match_types |= kMatchOriginalProfile; |
| 83 Browser* browser = FindBrowserMatching( |
| 84 BrowserList::begin_last_active(), BrowserList::end_last_active(), |
| 85 profile, Browser::FEATURE_NONE, match_types); |
| 86 // Fall back to a forward scan of all Browsers if no active one was found. |
| 87 return browser ? browser : |
| 88 FindBrowserMatching(BrowserList::begin(), BrowserList::end(), profile, |
| 89 Browser::FEATURE_NONE, match_types); |
| 90 } |
| 91 |
| 92 size_t GetBrowserCountImpl(Profile* profile, uint32 match_types) { |
| 93 size_t count = 0; |
| 94 for (BrowserList::const_iterator i = BrowserList::begin(); |
| 95 i != BrowserList::end(); ++i) { |
| 96 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types)) |
| 97 count++; |
| 98 } |
| 99 return count; |
| 100 } |
| 101 |
| 102 } // namespace |
| 103 |
| 104 Browser* FindTabbedBrowser(Profile* profile, bool match_original_profiles) { |
| 105 return FindBrowserWithTabbedOrAnyType(profile, |
| 106 true, |
| 107 match_original_profiles); |
| 108 } |
| 109 |
| 110 Browser* FindOrCreateTabbedBrowser(Profile* profile) { |
| 111 Browser* browser = FindTabbedBrowser(profile, false); |
| 112 if (!browser) |
| 113 browser = Browser::Create(profile); |
| 114 return browser; |
| 115 } |
| 116 |
| 117 Browser* FindAnyBrowser(Profile* profile, bool match_original_profiles) { |
| 118 return FindBrowserWithTabbedOrAnyType(profile, |
| 119 false, |
| 120 match_original_profiles); |
| 121 } |
| 122 |
| 123 Browser* FindBrowserWithFeature(Profile* profile, |
| 124 Browser::WindowFeature feature) { |
| 125 Browser* browser = FindBrowserMatching( |
| 126 BrowserList::begin_last_active(), BrowserList::end_last_active(), |
| 127 profile, feature, kMatchCanSupportWindowFeature); |
| 128 // Fall back to a forward scan of all Browsers if no active one was found. |
| 129 return browser ? browser : |
| 130 FindBrowserMatching(BrowserList::begin(), BrowserList::end(), profile, |
| 131 feature, kMatchCanSupportWindowFeature); |
| 132 } |
| 133 |
| 134 Browser* FindBrowserWithProfile(Profile* profile) { |
| 135 return FindAnyBrowser(profile, false); |
| 136 } |
| 137 |
| 138 Browser* FindBrowserWithID(SessionID::id_type desired_id) { |
| 139 for (BrowserList::const_iterator i = BrowserList::begin(); |
| 140 i != BrowserList::end(); ++i) { |
| 141 if ((*i)->session_id().id() == desired_id) |
| 142 return *i; |
| 143 } |
| 144 return NULL; |
| 145 } |
| 146 |
| 147 Browser* FindBrowserWithWindow(gfx::NativeWindow window) { |
| 148 for (BrowserList::const_iterator it = BrowserList::begin(); |
| 149 it != BrowserList::end(); ++it) { |
| 150 Browser* browser = *it; |
| 151 if (browser->window() && browser->window()->GetNativeHandle() == window) |
| 152 return browser; |
| 153 } |
| 154 return NULL; |
| 155 } |
| 156 |
| 157 Browser* FindBrowserWithWebContents(WebContents* web_contents) { |
| 158 DCHECK(web_contents); |
| 159 for (TabContentsIterator it; !it.done(); ++it) { |
| 160 if (it->web_contents() == web_contents) |
| 161 return it.browser(); |
| 162 } |
| 163 return NULL; |
| 164 } |
| 165 |
| 166 Browser* FindBrowserForController( |
| 167 const content::NavigationController* controller, |
| 168 int* index_result) { |
| 169 for (BrowserList::const_iterator it = BrowserList::begin(); |
| 170 it != BrowserList::end(); ++it) { |
| 171 int index = (*it)->GetIndexOfController(controller); |
| 172 if (index != TabStripModel::kNoTab) { |
| 173 if (index_result) |
| 174 *index_result = index; |
| 175 return *it; |
| 176 } |
| 177 } |
| 178 return NULL; |
| 179 } |
| 180 |
| 181 |
| 182 Browser* FindLastActiveWithProfile(Profile* profile) { |
| 183 // We are only interested in last active browsers, so we don't fall back to |
| 184 // all browsers like FindBrowserWith* do. |
| 185 return FindBrowserMatching( |
| 186 BrowserList::begin_last_active(), BrowserList::end_last_active(), profile, |
| 187 Browser::FEATURE_NONE, kMatchAny); |
| 188 } |
| 189 |
| 190 size_t GetBrowserCount(Profile* profile) { |
| 191 return GetBrowserCountImpl(profile, kMatchAny); |
| 192 } |
| 193 |
| 194 size_t GetTabbedBrowserCount(Profile* profile) { |
| 195 return GetBrowserCountImpl(profile, kMatchTabbed); |
| 196 } |
| 197 |
| 198 } // namespace browser |
OLD | NEW |