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 #if defined(OS_CHROMEOS)
16 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
17 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
20 using content::WebContents
;
25 // Type used to indicate to match anything.
26 const int kMatchAny
= 0;
28 // See BrowserMatches for details.
29 const int kMatchOriginalProfile
= 1 << 0;
30 const int kMatchCanSupportWindowFeature
= 1 << 1;
31 const int kMatchTabbed
= 1 << 2;
33 // Returns true if the specified |browser| matches the specified arguments.
34 // |match_types| is a bitmask dictating what parameters to match:
35 // . If it contains kMatchOriginalProfile then the original profile of the
36 // browser must match |profile->GetOriginalProfile()|. This is used to match
38 // . If it contains kMatchCanSupportWindowFeature
39 // |CanSupportWindowFeature(window_feature)| must return true.
40 // . If it contains kMatchTabbed, the browser must be a tabbed browser.
41 bool BrowserMatches(Browser
* browser
,
43 Browser::WindowFeature window_feature
,
45 if (match_types
& kMatchCanSupportWindowFeature
&&
46 !browser
->CanSupportWindowFeature(window_feature
)) {
50 bool matches_profile
= browser
->profile() == profile
;
51 #if defined(OS_CHROMEOS)
52 // Get the profile on which the window is currently shown.
53 // MultiUserWindowManager might be NULL under test scenario.
54 chrome::MultiUserWindowManager
* const window_manager
=
55 chrome::MultiUserWindowManager::GetInstance();
57 const std::string
& shown_user_id
= window_manager
->GetUserPresentingWindow(
58 browser
->window()->GetNativeWindow());
59 Profile
* shown_profile
=
62 : multi_user_util::GetProfileFromUserID(shown_user_id
);
63 matches_profile
&= !shown_profile
|| shown_profile
== profile
;
67 if (match_types
& kMatchOriginalProfile
) {
68 if (browser
->profile()->GetOriginalProfile() !=
69 profile
->GetOriginalProfile())
71 } else if (!matches_profile
) {
75 if (match_types
& kMatchTabbed
)
76 return browser
->is_type_tabbed();
81 // Returns the first browser in the specified iterator that returns true from
82 // |BrowserMatches|, or null if no browsers match the arguments. See
83 // |BrowserMatches| for details on the arguments.
85 Browser
* FindBrowserMatching(const T
& begin
,
88 Browser::WindowFeature window_feature
,
90 for (T i
= begin
; i
!= end
; ++i
) {
91 if (BrowserMatches(*i
, profile
, window_feature
, match_types
))
97 Browser
* FindBrowserWithTabbedOrAnyType(Profile
* profile
,
98 chrome::HostDesktopType desktop_type
,
100 bool match_original_profiles
) {
101 BrowserList
* browser_list_impl
= BrowserList::GetInstance(desktop_type
);
102 if (!browser_list_impl
)
104 uint32 match_types
= kMatchAny
;
106 match_types
|= kMatchTabbed
;
107 if (match_original_profiles
)
108 match_types
|= kMatchOriginalProfile
;
109 Browser
* browser
= FindBrowserMatching(browser_list_impl
->begin_last_active(),
110 browser_list_impl
->end_last_active(),
112 Browser::FEATURE_NONE
,
114 // Fall back to a forward scan of all Browsers if no active one was found.
115 return browser
? browser
: FindBrowserMatching(browser_list_impl
->begin(),
116 browser_list_impl
->end(),
118 Browser::FEATURE_NONE
,
122 size_t GetBrowserCountImpl(Profile
* profile
,
123 chrome::HostDesktopType desktop_type
,
124 uint32 match_types
) {
125 BrowserList
* browser_list_impl
= BrowserList::GetInstance(desktop_type
);
127 if (browser_list_impl
) {
128 for (BrowserList::const_iterator i
= browser_list_impl
->begin();
129 i
!= browser_list_impl
->end(); ++i
) {
130 if (BrowserMatches(*i
, profile
, Browser::FEATURE_NONE
, match_types
))
141 Browser
* FindTabbedBrowser(Profile
* profile
,
142 bool match_original_profiles
,
143 HostDesktopType type
) {
144 return FindBrowserWithTabbedOrAnyType(profile
,
147 match_original_profiles
);
150 Browser
* FindAnyBrowser(Profile
* profile
,
151 bool match_original_profiles
,
152 HostDesktopType type
) {
153 return FindBrowserWithTabbedOrAnyType(profile
,
156 match_original_profiles
);
159 Browser
* FindBrowserWithProfile(Profile
* profile
,
160 HostDesktopType desktop_type
) {
161 return FindBrowserWithTabbedOrAnyType(profile
, desktop_type
, false, false);
164 Browser
* FindBrowserWithID(SessionID::id_type desired_id
) {
165 for (BrowserIterator it
; !it
.done(); it
.Next()) {
166 if (it
->session_id().id() == desired_id
)
172 Browser
* FindBrowserWithWindow(gfx::NativeWindow window
) {
175 for (BrowserIterator it
; !it
.done(); it
.Next()) {
176 Browser
* browser
= *it
;
177 if (browser
->window() && browser
->window()->GetNativeWindow() == window
)
183 Browser
* FindBrowserWithWebContents(const WebContents
* web_contents
) {
184 DCHECK(web_contents
);
185 for (TabContentsIterator it
; !it
.done(); it
.Next()) {
186 if (*it
== web_contents
)
192 Browser
* FindLastActiveWithProfile(Profile
* profile
, HostDesktopType type
) {
193 BrowserList
* list
= BrowserList::GetInstance(type
);
194 // We are only interested in last active browsers, so we don't fall back to
195 // all browsers like FindBrowserWith* do.
196 return FindBrowserMatching(list
->begin_last_active(), list
->end_last_active(),
197 profile
, Browser::FEATURE_NONE
, kMatchAny
);
200 Browser
* FindLastActiveWithHostDesktopType(HostDesktopType type
) {
201 BrowserList
* browser_list_impl
= BrowserList::GetInstance(type
);
202 if (browser_list_impl
)
203 return browser_list_impl
->GetLastActive();
207 size_t GetTotalBrowserCount() {
209 for (HostDesktopType t
= HOST_DESKTOP_TYPE_FIRST
; t
< HOST_DESKTOP_TYPE_COUNT
;
210 t
= static_cast<HostDesktopType
>(t
+ 1)) {
211 count
+= BrowserList::GetInstance(t
)->size();
216 size_t GetTotalBrowserCountForProfile(Profile
* profile
) {
218 for (HostDesktopType t
= HOST_DESKTOP_TYPE_FIRST
; t
< HOST_DESKTOP_TYPE_COUNT
;
219 t
= static_cast<HostDesktopType
>(t
+ 1)) {
220 count
+= GetBrowserCount(profile
, t
);
225 size_t GetBrowserCount(Profile
* profile
, HostDesktopType type
) {
226 return GetBrowserCountImpl(profile
, type
, kMatchAny
);
229 size_t GetTabbedBrowserCount(Profile
* profile
, HostDesktopType type
) {
230 return GetBrowserCountImpl(profile
, type
, kMatchTabbed
);
233 } // namespace chrome