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.
5 #include "chrome/browser/ui/browser_finder.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/browser_iterator.h"
9 #include "chrome/browser/ui/browser_list.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "content/public/browser/navigation_controller.h"
15 using content::WebContents
;
20 // Type used to indicate to match anything.
21 const int kMatchAny
= 0;
23 // See BrowserMatches for details.
24 const int kMatchOriginalProfile
= 1 << 0;
25 const int kMatchCanSupportWindowFeature
= 1 << 1;
26 const int kMatchTabbed
= 1 << 2;
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
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
,
38 Browser::WindowFeature window_feature
,
40 if (match_types
& kMatchCanSupportWindowFeature
&&
41 !browser
->CanSupportWindowFeature(window_feature
)) {
45 if (match_types
& kMatchOriginalProfile
) {
46 if (browser
->profile()->GetOriginalProfile() !=
47 profile
->GetOriginalProfile())
49 } else if (browser
->profile() != profile
) {
53 if (match_types
& kMatchTabbed
)
54 return browser
->is_type_tabbed();
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.
63 Browser
* FindBrowserMatching(const T
& begin
,
66 Browser::WindowFeature window_feature
,
68 for (T i
= begin
; i
!= end
; ++i
) {
69 if (BrowserMatches(*i
, profile
, window_feature
, match_types
))
75 Browser
* FindBrowserWithTabbedOrAnyType(Profile
* profile
,
76 chrome::HostDesktopType desktop_type
,
78 bool match_original_profiles
) {
79 BrowserList
* browser_list_impl
= BrowserList::GetInstance(desktop_type
);
80 if (!browser_list_impl
)
82 uint32 match_types
= kMatchAny
;
84 match_types
|= kMatchTabbed
;
85 if (match_original_profiles
)
86 match_types
|= kMatchOriginalProfile
;
87 Browser
* browser
= FindBrowserMatching(browser_list_impl
->begin_last_active(),
88 browser_list_impl
->end_last_active(),
90 Browser::FEATURE_NONE
,
92 // Fall back to a forward scan of all Browsers if no active one was found.
93 return browser
? browser
: FindBrowserMatching(browser_list_impl
->begin(),
94 browser_list_impl
->end(),
96 Browser::FEATURE_NONE
,
100 size_t GetBrowserCountImpl(Profile
* profile
,
101 chrome::HostDesktopType desktop_type
,
102 uint32 match_types
) {
103 BrowserList
* browser_list_impl
= BrowserList::GetInstance(desktop_type
);
105 if (browser_list_impl
) {
106 for (BrowserList::const_iterator i
= browser_list_impl
->begin();
107 i
!= browser_list_impl
->end(); ++i
) {
108 if (BrowserMatches(*i
, profile
, Browser::FEATURE_NONE
, match_types
))
119 Browser
* FindTabbedBrowser(Profile
* profile
,
120 bool match_original_profiles
,
121 HostDesktopType type
) {
122 return FindBrowserWithTabbedOrAnyType(profile
,
125 match_original_profiles
);
128 Browser
* FindAnyBrowser(Profile
* profile
,
129 bool match_original_profiles
,
130 HostDesktopType type
) {
131 return FindBrowserWithTabbedOrAnyType(profile
,
134 match_original_profiles
);
137 Browser
* FindBrowserWithProfile(Profile
* profile
,
138 HostDesktopType desktop_type
) {
139 return FindBrowserWithTabbedOrAnyType(profile
, desktop_type
, false, false);
142 Browser
* FindBrowserWithID(SessionID::id_type desired_id
) {
143 for (BrowserIterator it
; !it
.done(); it
.Next()) {
144 if (it
->session_id().id() == desired_id
)
150 Browser
* FindBrowserWithWindow(gfx::NativeWindow window
) {
153 for (BrowserIterator it
; !it
.done(); it
.Next()) {
154 Browser
* browser
= *it
;
155 if (browser
->window() && browser
->window()->GetNativeWindow() == window
)
161 Browser
* FindBrowserWithWebContents(const WebContents
* web_contents
) {
162 DCHECK(web_contents
);
163 for (TabContentsIterator it
; !it
.done(); it
.Next()) {
164 if (*it
== web_contents
)
170 Browser
* FindLastActiveWithProfile(Profile
* profile
, HostDesktopType type
) {
171 BrowserList
* list
= BrowserList::GetInstance(type
);
172 // We are only interested in last active browsers, so we don't fall back to
173 // all browsers like FindBrowserWith* do.
174 return FindBrowserMatching(list
->begin_last_active(), list
->end_last_active(),
175 profile
, Browser::FEATURE_NONE
, kMatchAny
);
178 Browser
* FindLastActiveWithHostDesktopType(HostDesktopType type
) {
179 BrowserList
* browser_list_impl
= BrowserList::GetInstance(type
);
180 if (browser_list_impl
)
181 return browser_list_impl
->GetLastActive();
185 size_t GetTotalBrowserCount() {
187 for (HostDesktopType t
= HOST_DESKTOP_TYPE_FIRST
; t
< HOST_DESKTOP_TYPE_COUNT
;
188 t
= static_cast<HostDesktopType
>(t
+ 1)) {
189 count
+= BrowserList::GetInstance(t
)->size();
194 size_t GetTotalBrowserCountForProfile(Profile
* profile
) {
196 for (HostDesktopType t
= HOST_DESKTOP_TYPE_FIRST
; t
< HOST_DESKTOP_TYPE_COUNT
;
197 t
= static_cast<HostDesktopType
>(t
+ 1)) {
198 count
+= GetBrowserCount(profile
, t
);
203 size_t GetBrowserCount(Profile
* profile
, HostDesktopType type
) {
204 return GetBrowserCountImpl(profile
, type
, kMatchAny
);
207 size_t GetTabbedBrowserCount(Profile
* profile
, HostDesktopType type
) {
208 return GetBrowserCountImpl(profile
, type
, kMatchTabbed
);
211 } // namespace chrome