File: startup_browser_creator_impl.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (217 lines) | stat: -rw-r--r-- 9,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_IMPL_H_
#define CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_IMPL_H_

#include <memory>
#include <vector>

#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/ui/startup/startup_tab.h"
#include "chrome/browser/ui/startup/startup_types.h"
#include "url/gurl.h"

class Browser;
class OldLaunchModeRecorder;
class Profile;
class StartupBrowserCreator;
class StartupTabProvider;
struct SessionStartupPref;

namespace base {
class CommandLine;
class FilePath;
}

namespace internals {
GURL GetTriggeredResetSettingsURL();
GURL GetWelcomePageURL();
}  // namespace internals

// Assists launching the application and appending the initial tabs for a
// browser window.
class StartupBrowserCreatorImpl {
 public:
  // There are two ctors. The first one implies a NULL browser_creator object
  // and thus no access to distribution-specific first-run behaviors. The
  // second one is always called when the browser starts even if it is not
  // the first run.  |is_first_run| indicates that this is a new profile.
  StartupBrowserCreatorImpl(const base::FilePath& cur_dir,
                            const base::CommandLine& command_line,
                            chrome::startup::IsFirstRun is_first_run);
  StartupBrowserCreatorImpl(const base::FilePath& cur_dir,
                            const base::CommandLine& command_line,
                            StartupBrowserCreator* browser_creator,
                            chrome::startup::IsFirstRun is_first_run);
  StartupBrowserCreatorImpl(const StartupBrowserCreatorImpl&) = delete;
  StartupBrowserCreatorImpl& operator=(const StartupBrowserCreatorImpl&) =
      delete;
  ~StartupBrowserCreatorImpl() = default;

  // If command line specifies kiosk mode, or full screen mode, switch
  // to full screen.
  static void MaybeToggleFullscreen(Browser* browser);

  // Creates the necessary windows for startup. |process_startup| indicates
  // whether Chrome is just starting up or already running and the user wants to
  // launch another instance.
  void Launch(Profile* profile,
              chrome::startup::IsProcessStartup process_startup,
              std::unique_ptr<OldLaunchModeRecorder> launch_mode_recorder);

  // Convenience for OpenTabsInBrowser that converts |urls| into a set of
  // Tabs.
  Browser* OpenURLsInBrowser(Browser* browser,
                             chrome::startup::IsProcessStartup process_startup,
                             const std::vector<GURL>& urls);

 private:
  FRIEND_TEST_ALL_PREFIXES(BrowserTest, RestorePinnedTabs);
  FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest, DetermineStartupTabs);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_Incognito);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_Crash);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_InitialPrefs);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_CommandLine);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_Crosapi);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_NewTabPage);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_WelcomeBackPage);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineBrowserOpenBehavior_Startup);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineBrowserOpenBehavior_CmdLineTabs);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineBrowserOpenBehavior_PostCrash);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineBrowserOpenBehavior_NotStartup);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_NewFeaturesPage);
  FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorImplTest,
                           DetermineStartupTabs_PrivacySandbox);

  enum class LaunchResult {
    kNormally,
    kWithGivenUrls,  // URLs are given from platform, e.g. via command line.
  };

  struct DetermineStartupTabsResult {
    DetermineStartupTabsResult(StartupTabs tabs, LaunchResult launch_result);
    DetermineStartupTabsResult(DetermineStartupTabsResult&&);
    DetermineStartupTabsResult& operator=(DetermineStartupTabsResult&&);
    ~DetermineStartupTabsResult();

    StartupTabs tabs;  // List of startup tabs.
    LaunchResult launch_result;
  };

  enum class WelcomeRunType {
    NONE,                // Do not inject the welcome page for this run.
    FIRST_TAB,           // Inject the welcome page as the first tab.
    FIRST_RUN_LAST_TAB,  // Inject the welcome page as the last first-run tab.
  };

  // Window behaviors possible when opening Chrome.
  enum class BrowserOpenBehavior {
    NEW,                  // Open in a new browser.
    SYNCHRONOUS_RESTORE,  // Attempt a synchronous session restore.
    USE_EXISTING,         // Attempt to add to an existing tabbed browser.
  };

  // Boolean flags used to indicate state for DetermineBrowserOpenBehavior.
  enum BehaviorFlags {
    PROCESS_STARTUP = (1 << 0),
    IS_POST_CRASH_LAUNCH = (1 << 1),
    HAS_NEW_WINDOW_SWITCH = (1 << 2),
    HAS_CMD_LINE_TABS = (1 << 3),
  };

  using BrowserOpenBehaviorOptions = uint32_t;

  // Creates a tab for each of the Tabs in |tabs|. If browser is non-null
  // and a tabbed browser, the tabs are added to it. Otherwise a new tabbed
  // browser is created and the tabs are added to it. The browser the tabs
  // are added to is returned, which is either |browser|, the newly created
  // browser, or nullptr if browser could not be created.
  Browser* OpenTabsInBrowser(Browser* browser,
                             chrome::startup::IsProcessStartup process_startup,
                             const StartupTabs& tabs);

  // Determines the URLs to be shown at startup by way of various policies
  // (welcome, pinned tabs, etc.), determines whether a session restore
  // is necessary, and opens the URLs in a new or restored browser accordingly.
  LaunchResult DetermineURLsAndLaunch(
      chrome::startup::IsProcessStartup process_startup);

  // Returns a tuple of
  // - the tabs to be shown on startup, based on the policy functions in
  //   the given StartupTabProvider, the given tabs passed by the command line,
  //   and the interactions between those policies.
  // - Whether there's launch tabs.
  DetermineStartupTabsResult DetermineStartupTabs(
      const StartupTabProvider& provider,
      chrome::startup::IsProcessStartup process_startup,
      bool is_ephemeral_profile,
      bool is_post_crash_launch,
      bool has_incompatible_applications,
      bool promotional_tabs_enabled,
      bool welcome_enabled,
      bool whats_new_enabled,
      bool privacy_sandbox_confirmation_required);

  // Begins an asynchronous session restore if current state allows it (e.g.,
  // this is not process startup) and SessionService indicates that one is
  // necessary. Returns true if restore was initiated, or false if launch
  // should continue (either synchronously, or asynchronously without
  // restoring).
  bool MaybeAsyncRestore(const StartupTabs& tabs,
                         chrome::startup::IsProcessStartup process_startup,
                         bool is_post_crash_launch);

  // Returns a browser displaying the contents of |tabs|. Based on |behavior|,
  // this may attempt a session restore or create a new browser. May also allow
  // DOM Storage to begin cleanup once it's clear it is not needed anymore.
  Browser* RestoreOrCreateBrowser(
      const StartupTabs& tabs,
      BrowserOpenBehavior behavior,
      SessionRestore::BehaviorBitmask restore_options,
      chrome::startup::IsProcessStartup process_startup,
      bool is_post_crash_launch);

  // Determines how the launch flow should obtain a Browser.
  static BrowserOpenBehavior DetermineBrowserOpenBehavior(
      const SessionStartupPref& pref,
      BrowserOpenBehaviorOptions options);

  // Returns the relevant bitmask options which must be passed when restoring a
  // session.
  static SessionRestore::BehaviorBitmask DetermineSynchronousRestoreOptions(
      bool has_create_browser_default,
      bool has_create_browser_switch,
      bool was_mac_login_or_resume);

  // Returns whether `switches::kKioskMode` is set on the command line of
  // the current process. This is a static method to avoid accidentally reading
  // it from `command_line_`.
  static bool IsKioskModeEnabled();

  const base::FilePath cur_dir_;
  const raw_ref<const base::CommandLine> command_line_;
  raw_ptr<Profile> profile_ = nullptr;
  raw_ptr<StartupBrowserCreator> browser_creator_;
  chrome::startup::IsFirstRun is_first_run_;
};

#endif  // CHROME_BROWSER_UI_STARTUP_STARTUP_BROWSER_CREATOR_IMPL_H_