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.
4 
5 #ifndef CHROME_BROWSER_UI_BROWSER_FINDER_H_
6 #define CHROME_BROWSER_UI_BROWSER_FINDER_H_
7 
8 #include <stddef.h>
9 
10 #include "chrome/browser/ui/browser.h"
11 #include "ui/display/types/display_constants.h"
12 #include "ui/gfx/native_widget_types.h"
13 
14 class Profile;
15 
16 namespace contents {
17 class WebContents;
18 }
19 
20 namespace tab_groups {
21 class TabGroupId;
22 }
23 
24 // Collection of functions to find Browsers based on various criteria.
25 
26 namespace chrome {
27 
28 // If you want to find the last active tabbed browser and create a new browser
29 // if there are no tabbed browsers, use ScopedTabbedBrowserDisplayer.
30 
31 // Retrieve the last active tabbed browser with a profile matching |profile|.
32 // If |match_original_profiles| is true, matching is done based on the
33 // original profile, eg profile->GetOriginalProfile() ==
34 // browser->profile()->GetOriginalProfile(). This has the effect of matching
35 // against both non-incognito and incognito profiles. If
36 // |match_original_profiles| is false, only an exact match may be returned.
37 // If |display_id| is not equal to display::kInvalidDisplayId, only the browsers
38 // in the corresponding display may be returned.
39 Browser* FindTabbedBrowser(Profile* profile,
40                            bool match_original_profiles,
41                            int64_t display_id = display::kInvalidDisplayId);
42 
43 // Finds an existing browser window of any kind.
44 Browser* FindAnyBrowser(Profile* profile, bool match_original_profiles);
45 
46 // Find an existing browser window with the provided profile. Searches in the
47 // order of last activation. Only browsers that have been active can be
48 // returned. Returns NULL if no such browser currently exists.
49 Browser* FindBrowserWithProfile(Profile* profile);
50 
51 // Find an existing browser with the provided ID. Returns NULL if no such
52 // browser currently exists.
53 Browser* FindBrowserWithID(SessionID desired_id);
54 
55 // Find the browser represented by |window| or NULL if not found.
56 Browser* FindBrowserWithWindow(gfx::NativeWindow window);
57 
58 // Find the browser with active window or NULL if not found.
59 Browser* FindBrowserWithActiveWindow();
60 
61 // Find the browser containing |web_contents| or NULL if none is found.
62 // |web_contents| must not be NULL.
63 Browser* FindBrowserWithWebContents(const content::WebContents* web_contents);
64 
65 // Find the browser containing the group with ID |group| or nullptr if none is
66 // found within the given |profile|. If the profile is not specified, find any
67 // browser containing the group.
68 Browser* FindBrowserWithGroup(tab_groups::TabGroupId group, Profile* profile);
69 
70 // Returns the Browser object owned by |profile| whose window was most recently
71 // active. If no such Browsers exist, returns NULL.
72 //
73 // WARNING: this is NULL until a browser becomes active. If during startup
74 // a browser does not become active (perhaps the user launches Chrome, then
75 // clicks on another app before the first browser window appears) then this
76 // returns NULL.
77 // WARNING #2: this will always be NULL in unit tests run on the bots.
78 Browser* FindLastActiveWithProfile(Profile* profile);
79 
80 // Returns the Browser object whose window was most recently active. If no such
81 // Browsers exist, returns NULL.
82 //
83 // WARNING: this is NULL until a browser becomes active. If during startup
84 // a browser does not become active (perhaps the user launches Chrome, then
85 // clicks on another app before the first browser window appears) then this
86 // returns NULL.
87 // WARNING #2: this will always be NULL in unit tests run on the bots.
88 Browser* FindLastActive();
89 
90 // Returns the number of browsers across all profiles.
91 size_t GetTotalBrowserCount();
92 
93 // Returns the number of browsers with the Profile |profile|.
94 size_t GetBrowserCount(Profile* profile);
95 
96 // Returns the number of tabbed browsers with the Profile |profile|.
97 size_t GetTabbedBrowserCount(Profile* profile);
98 
99 }  // namespace chrome
100 
101 #endif  // CHROME_BROWSER_UI_BROWSER_FINDER_H_
102