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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 | 27 |
| 28 // Type used to indicate to match anything. | 28 // Type used to indicate to match anything. |
| 29 const int kMatchAny = 0; | 29 const int kMatchAny = 0; |
| 30 | 30 |
| 31 // See BrowserMatches for details. | 31 // See BrowserMatches for details. |
| 32 const int kMatchOriginalProfile = 1 << 0; | 32 const int kMatchOriginalProfile = 1 << 0; |
| 33 const int kMatchCanSupportWindowFeature = 1 << 1; | 33 const int kMatchCanSupportWindowFeature = 1 << 1; |
| 34 const int kMatchTabbed = 1 << 2; | 34 const int kMatchTabbed = 1 << 2; |
| 35 const int kMatchRootWindow = 1 << 3; | |
|
stevenjb
2017/05/22 17:55:29
Align '= 1 << 3' with lines above.
| |
| 35 | 36 |
| 36 // Returns true if the specified |browser| matches the specified arguments. | 37 // Returns true if the specified |browser| matches the specified arguments. |
| 37 // |match_types| is a bitmask dictating what parameters to match: | 38 // |match_types| is a bitmask dictating what parameters to match: |
| 38 // . If it contains kMatchOriginalProfile then the original profile of the | 39 // . If it contains kMatchOriginalProfile then the original profile of the |
| 39 // browser must match |profile->GetOriginalProfile()|. This is used to match | 40 // browser must match |profile->GetOriginalProfile()|. This is used to match |
| 40 // incognito windows. | 41 // incognito windows. |
| 41 // . If it contains kMatchCanSupportWindowFeature | 42 // . If it contains kMatchCanSupportWindowFeature |
| 42 // |CanSupportWindowFeature(window_feature)| must return true. | 43 // |CanSupportWindowFeature(window_feature)| must return true. |
| 43 // . If it contains kMatchTabbed, the browser must be a tabbed browser. | 44 // . If it contains kMatchTabbed, the browser must be a tabbed browser. |
| 44 bool BrowserMatches(Browser* browser, | 45 bool BrowserMatches(Browser* browser, |
| 45 Profile* profile, | 46 Profile* profile, |
| 46 Browser::WindowFeature window_feature, | 47 Browser::WindowFeature window_feature, |
| 47 uint32_t match_types) { | 48 uint32_t match_types, |
| 49 const aura::Window* root_window) { | |
| 48 if ((match_types & kMatchCanSupportWindowFeature) && | 50 if ((match_types & kMatchCanSupportWindowFeature) && |
| 49 !browser->CanSupportWindowFeature(window_feature)) { | 51 !browser->CanSupportWindowFeature(window_feature)) { |
| 50 return false; | 52 return false; |
| 51 } | 53 } |
| 52 | 54 |
| 53 #if defined(OS_CHROMEOS) | 55 #if defined(OS_CHROMEOS) |
| 54 // Get the profile on which the window is currently shown. | 56 // Get the profile on which the window is currently shown. |
| 55 // MultiUserWindowManager might be NULL under test scenario. | 57 // MultiUserWindowManager might be NULL under test scenario. |
| 56 chrome::MultiUserWindowManager* const window_manager = | 58 chrome::MultiUserWindowManager* const window_manager = |
| 57 chrome::MultiUserWindowManager::GetInstance(); | 59 chrome::MultiUserWindowManager::GetInstance(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 78 #endif | 80 #endif |
| 79 } else { | 81 } else { |
| 80 if (browser->profile() != profile) | 82 if (browser->profile() != profile) |
| 81 return false; | 83 return false; |
| 82 #if defined(OS_CHROMEOS) | 84 #if defined(OS_CHROMEOS) |
| 83 if (shown_profile && shown_profile != profile) | 85 if (shown_profile && shown_profile != profile) |
| 84 return false; | 86 return false; |
| 85 #endif | 87 #endif |
| 86 } | 88 } |
| 87 | 89 |
| 88 if (match_types & kMatchTabbed) | 90 if ((match_types & kMatchTabbed) && !browser->is_type_tabbed()) |
| 89 return browser->is_type_tabbed(); | 91 return false; |
| 92 | |
| 93 if (match_types & kMatchRootWindow) | |
| 94 return browser->window()->GetNativeWindow()->GetRootWindow() == root_window; | |
| 90 | 95 |
| 91 return true; | 96 return true; |
| 92 } | 97 } |
| 93 | 98 |
| 94 // Returns the first browser in the specified iterator that returns true from | 99 // Returns the first browser in the specified iterator that returns true from |
| 95 // |BrowserMatches|, or null if no browsers match the arguments. See | 100 // |BrowserMatches|, or null if no browsers match the arguments. See |
| 96 // |BrowserMatches| for details on the arguments. | 101 // |BrowserMatches| for details on the arguments. |
| 97 template <class T> | 102 template <class T> |
| 98 Browser* FindBrowserMatching(const T& begin, | 103 Browser* FindBrowserMatching(const T& begin, |
| 99 const T& end, | 104 const T& end, |
| 100 Profile* profile, | 105 Profile* profile, |
| 101 Browser::WindowFeature window_feature, | 106 Browser::WindowFeature window_feature, |
| 102 uint32_t match_types) { | 107 uint32_t match_types, |
| 108 const aura::Window* root_window) { | |
| 103 for (T i = begin; i != end; ++i) { | 109 for (T i = begin; i != end; ++i) { |
| 104 if (BrowserMatches(*i, profile, window_feature, match_types)) | 110 if (BrowserMatches(*i, profile, window_feature, match_types, root_window)) |
| 105 return *i; | 111 return *i; |
| 106 } | 112 } |
| 107 return NULL; | 113 return NULL; |
| 108 } | 114 } |
| 109 | 115 |
| 110 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile, | 116 Browser* FindBrowserWithTabbedOrAnyType(Profile* profile, |
| 111 bool match_tabbed, | 117 bool match_tabbed, |
| 112 bool match_original_profiles) { | 118 bool match_original_profiles, |
| 119 bool match_root_window, | |
| 120 const aura::Window* root_window) { | |
|
stevenjb
2017/05/22 17:55:29
|match_root_window| seems redundant, can we just t
weidongg
2017/05/23 16:37:46
Done
| |
| 113 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 121 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 114 if (!browser_list_impl) | 122 if (!browser_list_impl) |
| 115 return NULL; | 123 return NULL; |
| 116 uint32_t match_types = kMatchAny; | 124 uint32_t match_types = kMatchAny; |
| 117 if (match_tabbed) | 125 if (match_tabbed) |
| 118 match_types |= kMatchTabbed; | 126 match_types |= kMatchTabbed; |
| 119 if (match_original_profiles) | 127 if (match_original_profiles) |
| 120 match_types |= kMatchOriginalProfile; | 128 match_types |= kMatchOriginalProfile; |
| 121 Browser* browser = FindBrowserMatching(browser_list_impl->begin_last_active(), | 129 if (match_root_window) |
| 122 browser_list_impl->end_last_active(), | 130 match_types |= kMatchRootWindow; |
| 123 profile, | 131 Browser* browser = |
| 124 Browser::FEATURE_NONE, | 132 FindBrowserMatching(browser_list_impl->begin_last_active(), |
| 125 match_types); | 133 browser_list_impl->end_last_active(), profile, |
| 134 Browser::FEATURE_NONE, match_types, root_window); | |
| 126 // Fall back to a forward scan of all Browsers if no active one was found. | 135 // Fall back to a forward scan of all Browsers if no active one was found. |
| 127 return browser ? browser : FindBrowserMatching(browser_list_impl->begin(), | 136 return browser |
| 128 browser_list_impl->end(), | 137 ? browser |
| 129 profile, | 138 : FindBrowserMatching( |
| 130 Browser::FEATURE_NONE, | 139 browser_list_impl->begin(), browser_list_impl->end(), |
| 131 match_types); | 140 profile, Browser::FEATURE_NONE, match_types, root_window); |
| 132 } | 141 } |
| 133 | 142 |
| 134 size_t GetBrowserCountImpl(Profile* profile, | 143 size_t GetBrowserCountImpl(Profile* profile, |
| 135 uint32_t match_types) { | 144 uint32_t match_types, |
| 145 const aura::Window* root_window) { | |
| 136 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 146 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 137 size_t count = 0; | 147 size_t count = 0; |
| 138 if (browser_list_impl) { | 148 if (browser_list_impl) { |
| 139 for (BrowserList::const_iterator i = browser_list_impl->begin(); | 149 for (BrowserList::const_iterator i = browser_list_impl->begin(); |
| 140 i != browser_list_impl->end(); ++i) { | 150 i != browser_list_impl->end(); ++i) { |
| 141 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types)) | 151 if (BrowserMatches(*i, profile, Browser::FEATURE_NONE, match_types, |
| 152 root_window)) | |
| 142 count++; | 153 count++; |
| 143 } | 154 } |
| 144 } | 155 } |
| 145 return count; | 156 return count; |
| 146 } | 157 } |
| 147 | 158 |
| 148 } // namespace | 159 } // namespace |
| 149 | 160 |
| 150 namespace chrome { | 161 namespace chrome { |
| 151 | 162 |
| 152 Browser* FindTabbedBrowser(Profile* profile, | 163 Browser* FindTabbedBrowser(Profile* profile, |
| 153 bool match_original_profiles) { | 164 bool match_original_profiles) { |
| 154 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles); | 165 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, |
| 166 false, NULL); | |
|
stevenjb
2017/05/22 17:55:29
nullptr throughout
| |
| 167 } | |
| 168 | |
| 169 Browser* FindTabbedBrowserInRootWindow(Profile* profile, | |
| 170 bool match_original_profiles, | |
| 171 const aura::Window* root_window) { | |
| 172 return FindBrowserWithTabbedOrAnyType(profile, true, match_original_profiles, | |
| 173 true, root_window); | |
| 155 } | 174 } |
| 156 | 175 |
| 157 Browser* FindAnyBrowser(Profile* profile, | 176 Browser* FindAnyBrowser(Profile* profile, |
| 158 bool match_original_profiles) { | 177 bool match_original_profiles) { |
| 159 return FindBrowserWithTabbedOrAnyType(profile, | 178 return FindBrowserWithTabbedOrAnyType(profile, false, match_original_profiles, |
| 160 false, | 179 false, NULL); |
| 161 match_original_profiles); | |
| 162 } | 180 } |
| 163 | 181 |
| 164 Browser* FindBrowserWithProfile(Profile* profile) { | 182 Browser* FindBrowserWithProfile(Profile* profile) { |
| 165 return FindBrowserWithTabbedOrAnyType(profile, false, false); | 183 return FindBrowserWithTabbedOrAnyType(profile, false, false, false, NULL); |
| 166 } | 184 } |
| 167 | 185 |
| 168 Browser* FindBrowserWithID(SessionID::id_type desired_id) { | 186 Browser* FindBrowserWithID(SessionID::id_type desired_id) { |
| 169 for (auto* browser : *BrowserList::GetInstance()) { | 187 for (auto* browser : *BrowserList::GetInstance()) { |
| 170 if (browser->session_id().id() == desired_id) | 188 if (browser->session_id().id() == desired_id) |
| 171 return browser; | 189 return browser; |
| 172 } | 190 } |
| 173 return NULL; | 191 return NULL; |
| 174 } | 192 } |
| 175 | 193 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 190 return it.browser(); | 208 return it.browser(); |
| 191 } | 209 } |
| 192 return NULL; | 210 return NULL; |
| 193 } | 211 } |
| 194 | 212 |
| 195 Browser* FindLastActiveWithProfile(Profile* profile) { | 213 Browser* FindLastActiveWithProfile(Profile* profile) { |
| 196 BrowserList* list = BrowserList::GetInstance(); | 214 BrowserList* list = BrowserList::GetInstance(); |
| 197 // We are only interested in last active browsers, so we don't fall back to | 215 // We are only interested in last active browsers, so we don't fall back to |
| 198 // all browsers like FindBrowserWith* do. | 216 // all browsers like FindBrowserWith* do. |
| 199 return FindBrowserMatching(list->begin_last_active(), list->end_last_active(), | 217 return FindBrowserMatching(list->begin_last_active(), list->end_last_active(), |
| 200 profile, Browser::FEATURE_NONE, kMatchAny); | 218 profile, Browser::FEATURE_NONE, kMatchAny, NULL); |
| 201 } | 219 } |
| 202 | 220 |
| 203 Browser* FindLastActive() { | 221 Browser* FindLastActive() { |
| 204 BrowserList* browser_list_impl = BrowserList::GetInstance(); | 222 BrowserList* browser_list_impl = BrowserList::GetInstance(); |
| 205 if (browser_list_impl) | 223 if (browser_list_impl) |
| 206 return browser_list_impl->GetLastActive(); | 224 return browser_list_impl->GetLastActive(); |
| 207 return NULL; | 225 return NULL; |
| 208 } | 226 } |
| 209 | 227 |
| 210 size_t GetTotalBrowserCount() { | 228 size_t GetTotalBrowserCount() { |
| 211 return BrowserList::GetInstance()->size(); | 229 return BrowserList::GetInstance()->size(); |
| 212 } | 230 } |
| 213 | 231 |
| 214 size_t GetBrowserCount(Profile* profile) { | 232 size_t GetBrowserCount(Profile* profile) { |
| 215 return GetBrowserCountImpl(profile, kMatchAny); | 233 return GetBrowserCountImpl(profile, kMatchAny, NULL); |
| 216 } | 234 } |
| 217 | 235 |
| 218 size_t GetTabbedBrowserCount(Profile* profile) { | 236 size_t GetTabbedBrowserCount(Profile* profile) { |
| 219 return GetBrowserCountImpl(profile, kMatchTabbed); | 237 return GetBrowserCountImpl(profile, kMatchTabbed, NULL); |
| 220 } | 238 } |
| 221 | 239 |
| 222 } // namespace chrome | 240 } // namespace chrome |
| OLD | NEW |