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; | |
|
stevenjb
2017/05/23 21:56:43
{}
weidongg
2017/05/24 17:49:40
Done.
| |
| 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) { | |
| 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(Profile* profile, |
| 111 bool match_tabbed, | 122 bool match_tabbed, |
| 112 bool match_original_profiles) { | 123 bool match_original_profiles, |
| 124 int64_t display_id) { | |
| 113 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 125 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 114 if (!browser_list_impl) | 126 if (!browser_list_impl) |
| 115 return NULL; | 127 return NULL; |
| 116 uint32_t match_types = kMatchAny; | 128 uint32_t match_types = kMatchAny; |
| 117 if (match_tabbed) | 129 if (match_tabbed) |
| 118 match_types |= kMatchTabbed; | 130 match_types |= kMatchTabbed; |
| 119 if (match_original_profiles) | 131 if (match_original_profiles) |
| 120 match_types |= kMatchOriginalProfile; | 132 match_types |= kMatchOriginalProfile; |
| 121 Browser* browser = FindBrowserMatching(browser_list_impl->begin_last_active(), | 133 if (display_id != display::kInvalidDisplayId) |
| 122 browser_list_impl->end_last_active(), | 134 match_types |= kMatchDisplayId; |
| 123 profile, | 135 Browser* browser = |
| 124 Browser::FEATURE_NONE, | 136 FindBrowserMatching(browser_list_impl->begin_last_active(), |
| 125 match_types); | 137 browser_list_impl->end_last_active(), profile, |
| 138 Browser::FEATURE_NONE, match_types, display_id); | |
| 126 // Fall back to a forward scan of all Browsers if no active one was found. | 139 // Fall back to a forward scan of all Browsers if no active one was found. |
| 127 return browser ? browser : FindBrowserMatching(browser_list_impl->begin(), | 140 return browser ? browser |
| 128 browser_list_impl->end(), | 141 : FindBrowserMatching( |
| 129 profile, | 142 browser_list_impl->begin(), browser_list_impl->end(), |
| 130 Browser::FEATURE_NONE, | 143 profile, Browser::FEATURE_NONE, match_types, display_id); |
| 131 match_types); | |
| 132 } | 144 } |
| 133 | 145 |
| 134 size_t GetBrowserCountImpl(Profile* profile, | 146 size_t GetBrowserCountImpl(Profile* profile, |
| 135 uint32_t match_types) { | 147 uint32_t match_types, |
| 148 int64_t display_id) { | |
| 136 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 149 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 137 size_t count = 0; | 150 size_t count = 0; |
| 138 if (browser_list_impl) { | 151 if (browser_list_impl) { |
| 139 for (BrowserList::const_iterator i = browser_list_impl->begin(); | 152 for (BrowserList::const_iterator i = browser_list_impl->begin(); |
| 140 i != browser_list_impl->end(); ++i) { | 153 i != browser_list_impl->end(); ++i) { |
| 141 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types)) | 154 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types, |
| 155 display_id)) | |
| 142 count++; | 156 count++; |
| 143 } | 157 } |
| 144 } | 158 } |
| 145 return count; | 159 return count; |
| 146 } | 160 } |
| 147 | 161 |
| 148 } // namespace | 162 } // namespace |
| 149 | 163 |
| 150 namespace chrome { | 164 namespace chrome { |
| 151 | 165 |
| 152 Browser* FindTabbedBrowser(Profile* profile, | 166 Browser* FindTabbedBrowser(Profile* profile, |
| 153 bool match_original_profiles) { | 167 bool match_original_profiles) { |
| 154 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles); | 168 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, |
| 169 display::kInvalidDisplayId); | |
| 170 } | |
| 171 | |
| 172 Browser* FindTabbedBrowserOnDisplay(Profile* profile, | |
| 173 bool match_original_profiles, | |
| 174 int64_t display_id) { | |
| 175 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, | |
| 176 display_id); | |
| 155 } | 177 } |
| 156 | 178 |
| 157 Browser* FindAnyBrowser(Profile* profile, | 179 Browser* FindAnyBrowser(Profile* profile, |
| 158 bool match_original_profiles) { | 180 bool match_original_profiles) { |
| 159 return FindBrowserWithTabbedOrAnyType(profile, | 181 return FindBrowserWithTabbedOrAnyType(profile, false, match_original_profiles, |
| 160 false, | 182 display::kInvalidDisplayId); |
| 161 match_original_profiles); | |
| 162 } | 183 } |
| 163 | 184 |
| 164 Browser* FindBrowserWithProfile(Profile* profile) { | 185 Browser* FindBrowserWithProfile(Profile* profile) { |
| 165 return FindBrowserWithTabbedOrAnyType(profile, false, false); | 186 return FindBrowserWithTabbedOrAnyType(profile, false, false, |
| 187 display::kInvalidDisplayId); | |
| 166 } | 188 } |
| 167 | 189 |
| 168 Browser* FindBrowserWithID(SessionID::id_type desired_id) { | 190 Browser* FindBrowserWithID(SessionID::id_type desired_id) { |
| 169 for (auto* browser : *BrowserList::GetInstance()) { | 191 for (auto* browser : *BrowserList::GetInstance()) { |
| 170 if (browser->session_id().id() == desired_id) | 192 if (browser->session_id().id() == desired_id) |
| 171 return browser; | 193 return browser; |
| 172 } | 194 } |
| 173 return NULL; | 195 return NULL; |
| 174 } | 196 } |
| 175 | 197 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 190 return it.browser(); | 212 return it.browser(); |
| 191 } | 213 } |
| 192 return NULL; | 214 return NULL; |
| 193 } | 215 } |
| 194 | 216 |
| 195 Browser* FindLastActiveWithProfile(Profile* profile) { | 217 Browser* FindLastActiveWithProfile(Profile* profile) { |
| 196 BrowserList* list = BrowserList::GetInstance(); | 218 BrowserList* list = BrowserList::GetInstance(); |
| 197 // We are only interested in last active browsers, so we don't fall back to | 219 // We are only interested in last active browsers, so we don't fall back to |
| 198 // all browsers like FindBrowserWith* do. | 220 // all browsers like FindBrowserWith* do. |
| 199 return FindBrowserMatching(list->begin_last_active(), list->end_last_active(), | 221 return FindBrowserMatching(list->begin_last_active(), list->end_last_active(), |
| 200 profile, Browser::FEATURE_NONE, kMatchAny); | 222 profile, Browser::FEATURE_NONE, kMatchAny, |
| 223 display::kInvalidDisplayId); | |
| 201 } | 224 } |
| 202 | 225 |
| 203 Browser* FindLastActive() { | 226 Browser* FindLastActive() { |
| 204 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 227 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 205 if (browser_list_impl) | 228 if (browser_list_impl) |
| 206 return browser_list_impl->GetLastActive(); | 229 return browser_list_impl->GetLastActive(); |
| 207 return NULL; | 230 return NULL; |
| 208 } | 231 } |
| 209 | 232 |
| 210 size_t GetTotalBrowserCount() { | 233 size_t GetTotalBrowserCount() { |
| 211 return BrowserList::GetInstance()->size(); | 234 return BrowserList::GetInstance()->size(); |
| 212 } | 235 } |
| 213 | 236 |
| 214 size_t GetBrowserCount(Profile* profile) { | 237 size_t GetBrowserCount(Profile* profile) { |
| 215 return GetBrowserCountImpl(profile, kMatchAny); | 238 return GetBrowserCountImpl(profile, kMatchAny, display::kInvalidDisplayId); |
| 216 } | 239 } |
| 217 | 240 |
| 218 size_t GetTabbedBrowserCount(Profile* profile) { | 241 size_t GetTabbedBrowserCount(Profile* profile) { |
| 219 return GetBrowserCountImpl(profile, kMatchTabbed); | 242 return GetBrowserCountImpl(profile, kMatchTabbed, display::kInvalidDisplayId); |
| 220 } | 243 } |
| 221 | 244 |
| 222 } // namespace chrome | 245 } // namespace chrome |
| OLD | NEW |