OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/browser_finder.h" | 5 #include "chrome/browser/ui/browser_finder.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
13 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 13 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
15 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
| 16 #include "ui/display/display.h" |
| 17 #include "ui/display/screen.h" |
16 | 18 |
17 #if defined(OS_CHROMEOS) | 19 #if defined(OS_CHROMEOS) |
18 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 20 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
19 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 21 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
20 #include "components/signin/core/account_id/account_id.h" | 22 #include "components/signin/core/account_id/account_id.h" |
21 #endif | 23 #endif |
22 | 24 |
23 using content::WebContents; | 25 using content::WebContents; |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 | 29 |
28 // Type used to indicate to match anything. | 30 // Type used to indicate to match anything. |
29 const int kMatchAny = 0; | 31 const int kMatchAny = 0; |
30 | 32 |
31 // See BrowserMatches for details. | 33 // See BrowserMatches for details. |
32 const int kMatchOriginalProfile = 1 << 0; | 34 const int kMatchOriginalProfile = 1 << 0; |
33 const int kMatchCanSupportWindowFeature = 1 << 1; | 35 const int kMatchCanSupportWindowFeature = 1 << 1; |
34 const int kMatchTabbed = 1 << 2; | 36 const int kMatchTabbed = 1 << 2; |
| 37 const int kMatchDisplayId = 1 << 3; |
35 | 38 |
36 // Returns true if the specified |browser| matches the specified arguments. | 39 // Returns true if the specified |browser| matches the specified arguments. |
37 // |match_types| is a bitmask dictating what parameters to match: | 40 // |match_types| is a bitmask dictating what parameters to match: |
38 // . If it contains kMatchOriginalProfile then the original profile of the | 41 // . If it contains kMatchOriginalProfile then the original profile of the |
39 // browser must match |profile->GetOriginalProfile()|. This is used to match | 42 // browser must match |profile->GetOriginalProfile()|. This is used to match |
40 // incognito windows. | 43 // incognito windows. |
41 // . If it contains kMatchCanSupportWindowFeature | 44 // . If it contains kMatchCanSupportWindowFeature |
42 // |CanSupportWindowFeature(window_feature)| must return true. | 45 // |CanSupportWindowFeature(window_feature)| must return true. |
43 // . If it contains kMatchTabbed, the browser must be a tabbed browser. | 46 // . If it contains kMatchTabbed, the browser must be a tabbed browser. |
44 bool BrowserMatches(Browser* browser, | 47 bool BrowserMatches(Browser* browser, |
45 Profile* profile, | 48 Profile* profile, |
46 Browser::WindowFeature window_feature, | 49 Browser::WindowFeature window_feature, |
47 uint32_t match_types) { | 50 uint32_t match_types, |
| 51 int64_t display_id) { |
48 if ((match_types & kMatchCanSupportWindowFeature) && | 52 if ((match_types & kMatchCanSupportWindowFeature) && |
49 !browser->CanSupportWindowFeature(window_feature)) { | 53 !browser->CanSupportWindowFeature(window_feature)) { |
50 return false; | 54 return false; |
51 } | 55 } |
52 | 56 |
53 #if defined(OS_CHROMEOS) | 57 #if defined(OS_CHROMEOS) |
54 // Get the profile on which the window is currently shown. | 58 // Get the profile on which the window is currently shown. |
55 // MultiUserWindowManager might be NULL under test scenario. | 59 // MultiUserWindowManager might be NULL under test scenario. |
56 chrome::MultiUserWindowManager* const window_manager = | 60 chrome::MultiUserWindowManager* const window_manager = |
57 chrome::MultiUserWindowManager::GetInstance(); | 61 chrome::MultiUserWindowManager::GetInstance(); |
(...skipping 20 matching lines...) Expand all Loading... |
78 #endif | 82 #endif |
79 } else { | 83 } else { |
80 if (browser->profile() != profile) | 84 if (browser->profile() != profile) |
81 return false; | 85 return false; |
82 #if defined(OS_CHROMEOS) | 86 #if defined(OS_CHROMEOS) |
83 if (shown_profile && shown_profile != profile) | 87 if (shown_profile && shown_profile != profile) |
84 return false; | 88 return false; |
85 #endif | 89 #endif |
86 } | 90 } |
87 | 91 |
88 if (match_types & kMatchTabbed) | 92 if ((match_types & kMatchTabbed) && !browser->is_type_tabbed()) |
89 return browser->is_type_tabbed(); | 93 return false; |
| 94 |
| 95 if (match_types & kMatchDisplayId) { |
| 96 return display::Screen::GetScreen() |
| 97 ->GetDisplayNearestWindow(browser->window()->GetNativeWindow()) |
| 98 .id() == display_id; |
| 99 } |
90 | 100 |
91 return true; | 101 return true; |
92 } | 102 } |
93 | 103 |
94 // Returns the first browser in the specified iterator that returns true from | 104 // Returns the first browser in the specified iterator that returns true from |
95 // |BrowserMatches|, or null if no browsers match the arguments. See | 105 // |BrowserMatches|, or null if no browsers match the arguments. See |
96 // |BrowserMatches| for details on the arguments. | 106 // |BrowserMatches| for details on the arguments. |
97 template <class T> | 107 template <class T> |
98 Browser* FindBrowserMatching(const T& begin, | 108 Browser* FindBrowserMatching(const T& begin, |
99 const T& end, | 109 const T& end, |
100 Profile* profile, | 110 Profile* profile, |
101 Browser::WindowFeature window_feature, | 111 Browser::WindowFeature window_feature, |
102 uint32_t match_types) { | 112 uint32_t match_types, |
| 113 int64_t display_id = display::kInvalidDisplayId) { |
103 for (T i = begin; i != end; ++i) { | 114 for (T i = begin; i != end; ++i) { |
104 if (BrowserMatches(*i, profile, window_feature, match_types)) | 115 if (BrowserMatches(*i, profile, window_feature, match_types, display_id)) |
105 return *i; | 116 return *i; |
106 } | 117 } |
107 return NULL; | 118 return NULL; |
108 } | 119 } |
109 | 120 |
110 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile, | 121 Browser* FindBrowserWithTabbedOrAnyType( |
111 bool match_tabbed, | 122 Profile* profile, |
112 bool match_original_profiles) { | 123 bool match_tabbed, |
| 124 bool match_original_profiles, |
| 125 int64_t display_id = display::kInvalidDisplayId) { |
113 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 126 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
114 if (!browser_list_impl) | 127 if (!browser_list_impl) |
115 return NULL; | 128 return NULL; |
116 uint32_t match_types = kMatchAny; | 129 uint32_t match_types = kMatchAny; |
117 if (match_tabbed) | 130 if (match_tabbed) |
118 match_types |= kMatchTabbed; | 131 match_types |= kMatchTabbed; |
119 if (match_original_profiles) | 132 if (match_original_profiles) |
120 match_types |= kMatchOriginalProfile; | 133 match_types |= kMatchOriginalProfile; |
121 Browser* browser = FindBrowserMatching(browser_list_impl->begin_last_active(), | 134 if (display_id != display::kInvalidDisplayId) |
122 browser_list_impl->end_last_active(), | 135 match_types |= kMatchDisplayId; |
123 profile, | 136 Browser* browser = |
124 Browser::FEATURE_NONE, | 137 FindBrowserMatching(browser_list_impl->begin_last_active(), |
125 match_types); | 138 browser_list_impl->end_last_active(), profile, |
| 139 Browser::FEATURE_NONE, match_types, display_id); |
126 // Fall back to a forward scan of all Browsers if no active one was found. | 140 // Fall back to a forward scan of all Browsers if no active one was found. |
127 return browser ? browser : FindBrowserMatching(browser_list_impl->begin(), | 141 return browser ? browser |
128 browser_list_impl->end(), | 142 : FindBrowserMatching( |
129 profile, | 143 browser_list_impl->begin(), browser_list_impl->end(), |
130 Browser::FEATURE_NONE, | 144 profile, Browser::FEATURE_NONE, match_types, display_id); |
131 match_types); | |
132 } | 145 } |
133 | 146 |
134 size_t GetBrowserCountImpl(Profile* profile, | 147 size_t GetBrowserCountImpl(Profile* profile, |
135 uint32_t match_types) { | 148 uint32_t match_types, |
| 149 int64_t display_id = display::kInvalidDisplayId) { |
136 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 150 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
137 size_t count = 0; | 151 size_t count = 0; |
138 if (browser_list_impl) { | 152 if (browser_list_impl) { |
139 for (BrowserList::const_iterator i = browser_list_impl->begin(); | 153 for (BrowserList::const_iterator i = browser_list_impl->begin(); |
140 i != browser_list_impl->end(); ++i) { | 154 i != browser_list_impl->end(); ++i) { |
141 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types)) | 155 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types, |
| 156 display_id)) |
142 count++; | 157 count++; |
143 } | 158 } |
144 } | 159 } |
145 return count; | 160 return count; |
146 } | 161 } |
147 | 162 |
148 } // namespace | 163 } // namespace |
149 | 164 |
150 namespace chrome { | 165 namespace chrome { |
151 | 166 |
152 Browser* FindTabbedBrowser(Profile* profile, | 167 Browser* FindTabbedBrowser(Profile* profile, |
153 bool match_original_profiles) { | 168 bool match_original_profiles, |
154 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles); | 169 int64_t display_id) { |
| 170 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, |
| 171 display_id); |
155 } | 172 } |
156 | 173 |
157 Browser* FindAnyBrowser(Profile* profile, | 174 Browser* FindAnyBrowser(Profile* profile, |
158 bool match_original_profiles) { | 175 bool match_original_profiles) { |
159 return FindBrowserWithTabbedOrAnyType(profile, | 176 return FindBrowserWithTabbedOrAnyType(profile, |
160 false, | 177 false, |
161 match_original_profiles); | 178 match_original_profiles); |
162 } | 179 } |
163 | 180 |
164 Browser* FindBrowserWithProfile(Profile* profile) { | 181 Browser* FindBrowserWithProfile(Profile* profile) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 230 |
214 size_t GetBrowserCount(Profile* profile) { | 231 size_t GetBrowserCount(Profile* profile) { |
215 return GetBrowserCountImpl(profile, kMatchAny); | 232 return GetBrowserCountImpl(profile, kMatchAny); |
216 } | 233 } |
217 | 234 |
218 size_t GetTabbedBrowserCount(Profile* profile) { | 235 size_t GetTabbedBrowserCount(Profile* profile) { |
219 return GetBrowserCountImpl(profile, kMatchTabbed); | 236 return GetBrowserCountImpl(profile, kMatchTabbed); |
220 } | 237 } |
221 | 238 |
222 } // namespace chrome | 239 } // namespace chrome |
OLD | NEW |