Chromium Code Reviews| 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" | |
| 18 #include "ui/display/types/display_constants.h" | |
| 16 | 19 |
| 17 #if defined(OS_CHROMEOS) | 20 #if defined(OS_CHROMEOS) |
| 18 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 21 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 19 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 22 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 20 #include "components/signin/core/account_id/account_id.h" | 23 #include "components/signin/core/account_id/account_id.h" |
| 21 #endif | 24 #endif |
| 22 | 25 |
| 23 using content::WebContents; | 26 using content::WebContents; |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 | 30 |
| 28 // Type used to indicate to match anything. | 31 // Type used to indicate to match anything. |
| 29 const int kMatchAny = 0; | 32 const int kMatchAny = 0; |
| 30 | 33 |
| 31 // See BrowserMatches for details. | 34 // See BrowserMatches for details. |
| 32 const int kMatchOriginalProfile = 1 << 0; | 35 const int kMatchOriginalProfile = 1 << 0; |
| 33 const int kMatchCanSupportWindowFeature = 1 << 1; | 36 const int kMatchCanSupportWindowFeature = 1 << 1; |
| 34 const int kMatchTabbed = 1 << 2; | 37 const int kMatchTabbed = 1 << 2; |
| 38 const int kMatchDisplayId = 1 << 3; | |
| 35 | 39 |
| 36 // Returns true if the specified |browser| matches the specified arguments. | 40 // Returns true if the specified |browser| matches the specified arguments. |
| 37 // |match_types| is a bitmask dictating what parameters to match: | 41 // |match_types| is a bitmask dictating what parameters to match: |
| 38 // . If it contains kMatchOriginalProfile then the original profile of the | 42 // . If it contains kMatchOriginalProfile then the original profile of the |
| 39 // browser must match |profile->GetOriginalProfile()|. This is used to match | 43 // browser must match |profile->GetOriginalProfile()|. This is used to match |
| 40 // incognito windows. | 44 // incognito windows. |
| 41 // . If it contains kMatchCanSupportWindowFeature | 45 // . If it contains kMatchCanSupportWindowFeature |
| 42 // |CanSupportWindowFeature(window_feature)| must return true. | 46 // |CanSupportWindowFeature(window_feature)| must return true. |
| 43 // . If it contains kMatchTabbed, the browser must be a tabbed browser. | 47 // . If it contains kMatchTabbed, the browser must be a tabbed browser. |
| 44 bool BrowserMatches(Browser* browser, | 48 bool BrowserMatches(Browser* browser, |
| 45 Profile* profile, | 49 Profile* profile, |
| 46 Browser::WindowFeature window_feature, | 50 Browser::WindowFeature window_feature, |
| 47 uint32_t match_types) { | 51 uint32_t match_types, |
| 52 int64_t display_id) { | |
| 48 if ((match_types & kMatchCanSupportWindowFeature) && | 53 if ((match_types & kMatchCanSupportWindowFeature) && |
| 49 !browser->CanSupportWindowFeature(window_feature)) { | 54 !browser->CanSupportWindowFeature(window_feature)) { |
| 50 return false; | 55 return false; |
| 51 } | 56 } |
| 52 | 57 |
| 53 #if defined(OS_CHROMEOS) | 58 #if defined(OS_CHROMEOS) |
| 54 // Get the profile on which the window is currently shown. | 59 // Get the profile on which the window is currently shown. |
| 55 // MultiUserWindowManager might be NULL under test scenario. | 60 // MultiUserWindowManager might be NULL under test scenario. |
| 56 chrome::MultiUserWindowManager* const window_manager = | 61 chrome::MultiUserWindowManager* const window_manager = |
| 57 chrome::MultiUserWindowManager::GetInstance(); | 62 chrome::MultiUserWindowManager::GetInstance(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 78 #endif | 83 #endif |
| 79 } else { | 84 } else { |
| 80 if (browser->profile() != profile) | 85 if (browser->profile() != profile) |
| 81 return false; | 86 return false; |
| 82 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
| 83 if (shown_profile && shown_profile != profile) | 88 if (shown_profile && shown_profile != profile) |
| 84 return false; | 89 return false; |
| 85 #endif | 90 #endif |
| 86 } | 91 } |
| 87 | 92 |
| 88 if (match_types & kMatchTabbed) | 93 if ((match_types & kMatchTabbed) && !browser->is_type_tabbed()) |
| 89 return browser->is_type_tabbed(); | 94 return false; |
| 95 | |
| 96 if (match_types & kMatchDisplayId) { | |
| 97 return display::Screen::GetScreen() | |
| 98 ->GetDisplayNearestWindow(browser->window()->GetNativeWindow()) | |
| 99 .id() == display_id; | |
| 100 } | |
| 90 | 101 |
| 91 return true; | 102 return true; |
| 92 } | 103 } |
| 93 | 104 |
| 94 // Returns the first browser in the specified iterator that returns true from | 105 // Returns the first browser in the specified iterator that returns true from |
| 95 // |BrowserMatches|, or null if no browsers match the arguments. See | 106 // |BrowserMatches|, or null if no browsers match the arguments. See |
| 96 // |BrowserMatches| for details on the arguments. | 107 // |BrowserMatches| for details on the arguments. |
| 97 template <class T> | 108 template <class T> |
| 98 Browser* FindBrowserMatching(const T& begin, | 109 Browser* FindBrowserMatching(const T& begin, |
| 99 const T& end, | 110 const T& end, |
| 100 Profile* profile, | 111 Profile* profile, |
| 101 Browser::WindowFeature window_feature, | 112 Browser::WindowFeature window_feature, |
| 102 uint32_t match_types) { | 113 uint32_t match_types, |
| 114 int64_t display_id) { | |
| 103 for (T i = begin; i != end; ++i) { | 115 for (T i = begin; i != end; ++i) { |
| 104 if (BrowserMatches(*i, profile, window_feature, match_types)) | 116 if (BrowserMatches(*i, profile, window_feature, match_types, display_id)) |
| 105 return *i; | 117 return *i; |
| 106 } | 118 } |
| 107 return NULL; | 119 return NULL; |
| 108 } | 120 } |
| 109 | 121 |
| 110 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile, | 122 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile, |
| 111 bool match_tabbed, | 123 bool match_tabbed, |
| 112 bool match_original_profiles) { | 124 bool match_original_profiles, |
| 125 int64_t display_id) { | |
| 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) { | |
| 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 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, |
| 170 display::kInvalidDisplayId); | |
| 171 } | |
| 172 | |
| 173 Browser* FindTabbedBrowserOnDisplay(Profile* profile, | |
| 174 bool match_original_profiles, | |
| 175 int64_t display_id) { | |
| 176 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, | |
| 177 display_id); | |
| 155 } | 178 } |
| 156 | 179 |
| 157 Browser* FindAnyBrowser(Profile* profile, | 180 Browser* FindAnyBrowser(Profile* profile, |
| 158 bool match_original_profiles) { | 181 bool match_original_profiles) { |
| 159 return FindBrowserWithTabbedOrAnyType(profile, | 182 return FindBrowserWithTabbedOrAnyType(profile, false, match_original_profiles, |
| 160 false, | 183 display::kInvalidDisplayId); |
|
Peter Kasting
2017/05/26 01:25:03
Nit: We could reduce the number of callers restati
weidongg
2017/05/26 02:49:47
Done.
| |
| 161 match_original_profiles); | |
| 162 } | 184 } |
| 163 | 185 |
| 164 Browser* FindBrowserWithProfile(Profile* profile) { | 186 Browser* FindBrowserWithProfile(Profile* profile) { |
| 165 return FindBrowserWithTabbedOrAnyType(profile, false, false); | 187 return FindBrowserWithTabbedOrAnyType(profile, false, false, |
| 188 display::kInvalidDisplayId); | |
| 166 } | 189 } |
| 167 | 190 |
| 168 Browser* FindBrowserWithID(SessionID::id_type desired_id) { | 191 Browser* FindBrowserWithID(SessionID::id_type desired_id) { |
| 169 for (auto* browser : *BrowserList::GetInstance()) { | 192 for (auto* browser : *BrowserList::GetInstance()) { |
| 170 if (browser->session_id().id() == desired_id) | 193 if (browser->session_id().id() == desired_id) |
| 171 return browser; | 194 return browser; |
| 172 } | 195 } |
| 173 return NULL; | 196 return NULL; |
| 174 } | 197 } |
| 175 | 198 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 190 return it.browser(); | 213 return it.browser(); |
| 191 } | 214 } |
| 192 return NULL; | 215 return NULL; |
| 193 } | 216 } |
| 194 | 217 |
| 195 Browser* FindLastActiveWithProfile(Profile* profile) { | 218 Browser* FindLastActiveWithProfile(Profile* profile) { |
| 196 BrowserList* list = BrowserList::GetInstance(); | 219 BrowserList* list = BrowserList::GetInstance(); |
| 197 // We are only interested in last active browsers, so we don't fall back to | 220 // We are only interested in last active browsers, so we don't fall back to |
| 198 // all browsers like FindBrowserWith* do. | 221 // all browsers like FindBrowserWith* do. |
| 199 return FindBrowserMatching(list->begin_last_active(), list->end_last_active(), | 222 return FindBrowserMatching(list->begin_last_active(), list->end_last_active(), |
| 200 profile, Browser::FEATURE_NONE, kMatchAny); | 223 profile, Browser::FEATURE_NONE, kMatchAny, |
| 224 display::kInvalidDisplayId); | |
| 201 } | 225 } |
| 202 | 226 |
| 203 Browser* FindLastActive() { | 227 Browser* FindLastActive() { |
| 204 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 228 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 205 if (browser_list_impl) | 229 if (browser_list_impl) |
| 206 return browser_list_impl->GetLastActive(); | 230 return browser_list_impl->GetLastActive(); |
| 207 return NULL; | 231 return NULL; |
| 208 } | 232 } |
| 209 | 233 |
| 210 size_t GetTotalBrowserCount() { | 234 size_t GetTotalBrowserCount() { |
| 211 return BrowserList::GetInstance()->size(); | 235 return BrowserList::GetInstance()->size(); |
| 212 } | 236 } |
| 213 | 237 |
| 214 size_t GetBrowserCount(Profile* profile) { | 238 size_t GetBrowserCount(Profile* profile) { |
| 215 return GetBrowserCountImpl(profile, kMatchAny); | 239 return GetBrowserCountImpl(profile, kMatchAny, display::kInvalidDisplayId); |
| 216 } | 240 } |
| 217 | 241 |
| 218 size_t GetTabbedBrowserCount(Profile* profile) { | 242 size_t GetTabbedBrowserCount(Profile* profile) { |
| 219 return GetBrowserCountImpl(profile, kMatchTabbed); | 243 return GetBrowserCountImpl(profile, kMatchTabbed, display::kInvalidDisplayId); |
| 220 } | 244 } |
| 221 | 245 |
| 222 } // namespace chrome | 246 } // namespace chrome |
| OLD | NEW |