1 // Copyright 2016 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_STARTUP_STARTUP_TAB_PROVIDER_H_
6 #define CHROME_BROWSER_UI_STARTUP_STARTUP_TAB_PROVIDER_H_
7 
8 #include <vector>
9 
10 #include "base/gtest_prod_util.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/first_run/first_run.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/startup/startup_browser_creator.h"
15 #include "chrome/browser/ui/startup/startup_tab.h"
16 #include "url/gurl.h"
17 
18 // Provides the sets of tabs to be shown at startup for given sets of policy.
19 // For instance, this class answers the question, "which tabs, if any, need to
20 // be shown for first run/onboarding?" Provided as a virtual interface to allow
21 // faking in unit tests.
22 class StartupTabProvider {
23  public:
24   // Gathers relevant system state and returns any tabs which should be
25   // shown according to onboarding/first run policy.
26   virtual StartupTabs GetOnboardingTabs(Profile* profile) const = 0;
27 
28   // Gathers URLs from a initial preferences file indicating first run logic
29   // specific to this distribution. Transforms any such URLs per policy and
30   // returns them. Also clears the value of first_run_urls_ in the provided
31   // BrowserCreator.
32   virtual StartupTabs GetDistributionFirstRunTabs(
33       StartupBrowserCreator* browser_creator) const = 0;
34 
35   // Returns a "welcome back" tab to be shown if requested for a specific
36   // launch.
37   virtual StartupTabs GetWelcomeBackTabs(Profile* profile,
38                                          StartupBrowserCreator* browser_creator,
39                                          bool process_startup) const = 0;
40 
41   // Checks for the presence of a trigger indicating the need to offer a Profile
42   // Reset on this profile. Returns any tabs which should be shown accordingly.
43   virtual StartupTabs GetResetTriggerTabs(Profile* profile) const = 0;
44 
45   // Returns the user's pinned tabs, if they should be shown.
46   virtual StartupTabs GetPinnedTabs(const base::CommandLine& command_line,
47                                     Profile* profile) const = 0;
48 
49   // Returns tabs, if any, specified in the user's preferences as the default
50   // content for a new window.
51   virtual StartupTabs GetPreferencesTabs(const base::CommandLine& command_line,
52                                          Profile* profile) const = 0;
53 
54   // Returns the New Tab Page, if the user's preferences indicate a
55   // configuration where it must be passed explicitly.
56   virtual StartupTabs GetNewTabPageTabs(const base::CommandLine& command_line,
57                                         Profile* profile) const = 0;
58 
59   // Returns the Incompatible Applications settings subpage if any incompatible
60   // applications exist.
61   virtual StartupTabs GetPostCrashTabs(
62       bool has_incompatible_applications) const = 0;
63 
64   // Returns tabs related to the extension checkup promo (if applicable).
65   virtual StartupTabs GetExtensionCheckupTabs(
66       bool serve_extensions_page) const = 0;
67 };
68 
69 class StartupTabProviderImpl : public StartupTabProvider {
70  public:
71   struct StandardOnboardingTabsParams {
72     bool is_first_run = false;
73     bool has_seen_welcome_page = false;
74     bool is_signin_allowed = false;
75     bool is_signed_in = false;
76     bool is_supervised_user = false;
77     bool is_force_signin_enabled = false;
78   };
79 
80   StartupTabProviderImpl() = default;
81   StartupTabProviderImpl(const StartupTabProviderImpl&) = delete;
82   StartupTabProviderImpl& operator=(const StartupTabProviderImpl&) = delete;
83 
84   // The static helper methods below implement the policies relevant to the
85   // respective Get*Tabs methods, but do not gather or interact with any
86   // system state relating to making those policy decisions. Exposed for
87   // testing.
88 
89   // Returns true if showing the standard welcome page is permissible.
90   static bool CanShowWelcome(bool is_signin_allowed,
91                              bool is_supervised_user,
92                              bool is_force_signin_enabled);
93 
94   // Returns true if the standard welcome page should be shown in a tab. This
95   // should only be used following a positive result from CanShowWelcome.
96   static bool ShouldShowWelcomeForOnboarding(bool has_seen_welcome_page,
97                                              bool is_signed_in);
98 
99   // Determines which tabs should be shown according to onboarding/first
100   // run policy.
101   static StartupTabs GetStandardOnboardingTabsForState(
102       const StandardOnboardingTabsParams& params);
103 
104   // Processes first run URLs specified in initial preferences file, replacing
105   // any "magic word" URL hosts with appropriate URLs.
106   static StartupTabs GetInitialPrefsTabsForState(
107       bool is_first_run,
108       const std::vector<GURL>& first_run_tabs);
109 
110   // Determines which tabs should be shown as a result of the presence/absence
111   // of a Reset Trigger on this profile.
112   static StartupTabs GetResetTriggerTabsForState(bool profile_has_trigger);
113 
114   // Determines whether the startup preference requires the contents of
115   // |pinned_tabs| to be shown. This is needed to avoid duplicates, as the
116   // session restore logic will also resurface pinned tabs on its own.
117   static StartupTabs GetPinnedTabsForState(
118       const SessionStartupPref& pref,
119       const StartupTabs& pinned_tabs,
120       bool profile_has_other_tabbed_browser);
121 
122   // Determines whether preferences and window state indicate that
123   // user-specified tabs should be shown as the default new window content, and
124   // returns the specified tabs if so.
125   static StartupTabs GetPreferencesTabsForState(
126       const SessionStartupPref& pref,
127       bool profile_has_other_tabbed_browser);
128 
129   // Determines whether startup preferences require the New Tab Page to be
130   // explicitly specified. Session Restore does not expect the NTP to be passed.
131   static StartupTabs GetNewTabPageTabsForState(const SessionStartupPref& pref);
132 
133   // Determines if the Incompatible Applications settings subpage should be
134   // shown.
135   static StartupTabs GetPostCrashTabsForState(
136       bool has_incompatible_applications);
137 
138   // Determines if the extensions page should be shown.
139   static StartupTabs GetExtensionCheckupTabsForState(
140       bool serve_extensions_page);
141 
142   // Gets the URL for the Welcome page. If |use_later_run_variant| is true, a
143   // URL parameter will be appended so as to access the variant page used when
144   // onboarding occurs after the first Chrome execution (e.g., when creating an
145   // additional profile).
146   // TODO(hcarmona): it might be possible to deprecate use_later_run_variant.
147   static GURL GetWelcomePageUrl(bool use_later_run_variant);
148 
149   // In branded Windows builds, adds the URL for the Incompatible Applications
150   // subpage of the Chrome settings.
151   static void AddIncompatibleApplicationsUrl(StartupTabs* tabs);
152 
153   // Gets the URL for the page which offers to reset the user's profile
154   // settings.
155   static GURL GetTriggeredResetSettingsUrl();
156 
157   // StartupTabProvider:
158   StartupTabs GetOnboardingTabs(Profile* profile) const override;
159   StartupTabs GetWelcomeBackTabs(Profile* profile,
160                                  StartupBrowserCreator* browser_creator,
161                                  bool process_startup) const override;
162   StartupTabs GetDistributionFirstRunTabs(
163       StartupBrowserCreator* browser_creator) const override;
164   StartupTabs GetResetTriggerTabs(Profile* profile) const override;
165   StartupTabs GetPinnedTabs(const base::CommandLine& command_line,
166                             Profile* profile) const override;
167   StartupTabs GetPreferencesTabs(const base::CommandLine& command_line,
168                                  Profile* profile) const override;
169   StartupTabs GetNewTabPageTabs(const base::CommandLine& command_line,
170                                 Profile* profile) const override;
171   StartupTabs GetPostCrashTabs(
172       bool has_incompatible_applications) const override;
173   StartupTabs GetExtensionCheckupTabs(
174       bool serve_extensions_page) const override;
175 };
176 
177 #endif  // CHROME_BROWSER_UI_STARTUP_STARTUP_TAB_PROVIDER_H_
178