Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / browser_finder.cc
blob4b5fe8eb6a6d62139e25f8a57567f22db9e1bb29
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"
18 #endif
20 using content::WebContents;
22 namespace {
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
37 // incognito windows.
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,
42 Profile* profile,
43 Browser::WindowFeature window_feature,
44 uint32 match_types) {
45 if (match_types & kMatchCanSupportWindowFeature &&
46 !browser->CanSupportWindowFeature(window_feature)) {
47 return false;
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();
56 if (window_manager) {
57 const std::string& shown_user_id = window_manager->GetUserPresentingWindow(
58 browser->window()->GetNativeWindow());
59 Profile* shown_profile =
60 shown_user_id.empty()
61 ? nullptr
62 : multi_user_util::GetProfileFromUserID(shown_user_id);
63 matches_profile &= !shown_profile || shown_profile == profile;
65 #endif
67 if (match_types & kMatchOriginalProfile) {
68 if (browser->profile()->GetOriginalProfile() !=
69 profile->GetOriginalProfile())
70 return false;
71 } else if (!matches_profile) {
72 return false;
75 if (match_types & kMatchTabbed)
76 return browser->is_type_tabbed();
78 return true;
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.
84 template <class T>
85 Browser* FindBrowserMatching(const T& begin,
86 const T& end,
87 Profile* profile,
88 Browser::WindowFeature window_feature,
89 uint32 match_types) {
90 for (T i = begin; i != end; ++i) {
91 if (BrowserMatches(*i, profile, window_feature, match_types))
92 return *i;
94 return NULL;
97 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile,
98 chrome::HostDesktopType desktop_type,
99 bool match_tabbed,
100 bool match_original_profiles) {
101 BrowserList* browser_list_impl = BrowserList::GetInstance(desktop_type);
102 if (!browser_list_impl)
103 return NULL;
104 uint32 match_types = kMatchAny;
105 if (match_tabbed)
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(),
111 profile,
112 Browser::FEATURE_NONE,
113 match_types);
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(),
117 profile,
118 Browser::FEATURE_NONE,
119 match_types);
122 size_t GetBrowserCountImpl(Profile* profile,
123 chrome::HostDesktopType desktop_type,
124 uint32 match_types) {
125 BrowserList* browser_list_impl = BrowserList::GetInstance(desktop_type);
126 size_t count = 0;
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))
131 count++;
134 return count;
137 } // namespace
139 namespace chrome {
141 Browser* FindTabbedBrowser(Profile* profile,
142 bool match_original_profiles,
143 HostDesktopType type) {
144 return FindBrowserWithTabbedOrAnyType(profile,
145 type,
146 true,
147 match_original_profiles);
150 Browser* FindAnyBrowser(Profile* profile,
151 bool match_original_profiles,
152 HostDesktopType type) {
153 return FindBrowserWithTabbedOrAnyType(profile,
154 type,
155 false,
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)
167 return *it;
169 return NULL;
172 Browser* FindBrowserWithWindow(gfx::NativeWindow window) {
173 if (!window)
174 return NULL;
175 for (BrowserIterator it; !it.done(); it.Next()) {
176 Browser* browser = *it;
177 if (browser->window() && browser->window()->GetNativeWindow() == window)
178 return browser;
180 return NULL;
183 Browser* FindBrowserWithWebContents(const WebContents* web_contents) {
184 DCHECK(web_contents);
185 for (TabContentsIterator it; !it.done(); it.Next()) {
186 if (*it == web_contents)
187 return it.browser();
189 return NULL;
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();
204 return NULL;
207 size_t GetTotalBrowserCount() {
208 size_t count = 0;
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();
213 return count;
216 size_t GetTotalBrowserCountForProfile(Profile* profile) {
217 size_t count = 0;
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);
222 return count;
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