From: csagan5 <32685696+csagan5@users.noreply.github.com> Date: Sat, 20 Nov 2021 15:36:54 +0000 Subject: Add option to use home page as NTP And allow use about:blank as default homepage License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html --- .../tab_management/TabGridDialogMediator.java | 9 ++++++++- .../tasks/tab_management/TabGroupUiMediator.java | 8 +++++++- .../java/res/xml/homepage_preferences.xml | 5 +++++ .../chrome/browser/homepage/HomepageManager.java | 16 ++++++++++++++++ .../homepage/settings/HomepageSettings.java | 12 ++++++++++++ .../chrome/browser/metrics/LaunchMetrics.java | 1 - .../browser/tabmodel/ChromeTabCreator.java | 8 ++++++++ .../preferences/ChromePreferenceKeys.java | 1 + .../preferences/LegacyChromePreferenceKeys.java | 1 + .../android/strings/android_chrome_strings.grd | 3 +++ chrome/browser/ui/browser_ui_prefs.cc | 2 ++ chrome/common/pref_names.h | 4 ++++ 12 files changed, 67 insertions(+), 3 deletions(-) diff --git a/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGridDialogMediator.java b/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGridDialogMediator.java --- a/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGridDialogMediator.java +++ b/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGridDialogMediator.java @@ -18,6 +18,7 @@ import org.chromium.base.Callback; import org.chromium.base.ResettersForTesting; import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.supplier.Supplier; +import org.chromium.chrome.browser.homepage.HomepageManager; import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.TabCreationState; import org.chromium.chrome.browser.tab.TabLaunchType; @@ -44,6 +45,7 @@ import org.chromium.ui.KeyboardVisibilityDelegate; import org.chromium.ui.base.DeviceFormFactor; import org.chromium.ui.modelutil.PropertyModel; import org.chromium.ui.text.EmptyTextWatcher; +import org.chromium.url.GURL; import java.util.ArrayList; import java.util.List; @@ -564,9 +566,14 @@ public class TabGridDialogMediator assert relatedTabs.size() > 0; + String url = UrlConstants.NTP_URL; + if (HomepageManager.getInstance().getPrefNTPIsHomepageEnabled()) { + GURL gurl = HomepageManager.getInstance().getHomepageGurl(); + url = gurl != null ? gurl.getSpec() : url; + } Tab parentTabToAttach = relatedTabs.get(relatedTabs.size() - 1); mTabCreatorManager.getTabCreator(currentTab.isIncognito()) - .createNewTab(new LoadUrlParams(UrlConstants.NTP_URL), + .createNewTab(new LoadUrlParams(url), TabLaunchType.FROM_TAB_GROUP_UI, parentTabToAttach); RecordUserAction.record("MobileNewTabOpened." + mComponentName); }; diff --git a/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGroupUiMediator.java b/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGroupUiMediator.java --- a/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGroupUiMediator.java +++ b/chrome/android/features/tab_ui/java/src/org/chromium/chrome/browser/tasks/tab_management/TabGroupUiMediator.java @@ -17,6 +17,7 @@ import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.supplier.ObservableSupplier; import org.chromium.base.supplier.ObservableSupplierImpl; import org.chromium.base.supplier.OneshotSupplier; +import org.chromium.chrome.browser.homepage.HomepageManager; import org.chromium.chrome.browser.layouts.LayoutStateProvider; import org.chromium.chrome.browser.layouts.LayoutStateProvider.LayoutStateObserver; import org.chromium.chrome.browser.layouts.LayoutType; @@ -345,8 +346,13 @@ public class TabGroupUiMediator implements BackPressHandler { assert relatedTabs.size() > 0; parentTabToAttach = relatedTabs.get(relatedTabs.size() - 1); + String url = UrlConstants.NTP_URL; + if (HomepageManager.getInstance().getPrefNTPIsHomepageEnabled()) { + GURL gurl = HomepageManager.getInstance().getHomepageGurl(); + url = gurl != null ? gurl.getSpec() : url; + } mTabCreatorManager.getTabCreator(currentTab.isIncognito()) - .createNewTab(new LoadUrlParams(UrlConstants.NTP_URL), + .createNewTab(new LoadUrlParams(url), TabLaunchType.FROM_TAB_GROUP_UI, parentTabToAttach); RecordUserAction.record("MobileNewTabOpened." + TabGroupUiCoordinator.COMPONENT_NAME); }; diff --git a/chrome/android/java/res/xml/homepage_preferences.xml b/chrome/android/java/res/xml/homepage_preferences.xml --- a/chrome/android/java/res/xml/homepage_preferences.xml +++ b/chrome/android/java/res/xml/homepage_preferences.xml @@ -14,6 +14,11 @@ found in the LICENSE file. android:summaryOn="@string/text_on" android:summaryOff="@string/text_off" /> + + { + mHomepageManager.setPrefNTPIsHomepageEnabled((boolean) newValue); + return true; + }); + RecordUserAction.record("Settings.Homepage.Opened"); } diff --git a/chrome/android/java/src/org/chromium/chrome/browser/metrics/LaunchMetrics.java b/chrome/android/java/src/org/chromium/chrome/browser/metrics/LaunchMetrics.java --- a/chrome/android/java/src/org/chromium/chrome/browser/metrics/LaunchMetrics.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/metrics/LaunchMetrics.java @@ -103,7 +103,6 @@ public class LaunchMetrics { public static void recordHomePageLaunchMetrics( boolean showHomeButton, boolean homepageIsNtp, GURL homepageGurl) { if (homepageGurl.isEmpty()) { - assert !showHomeButton : "Homepage should be disabled for an empty GURL"; } LaunchMetricsJni.get().recordHomePageLaunchMetrics( showHomeButton, homepageIsNtp, homepageGurl); diff --git a/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/ChromeTabCreator.java b/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/ChromeTabCreator.java --- a/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/ChromeTabCreator.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/tabmodel/ChromeTabCreator.java @@ -27,6 +27,7 @@ import org.chromium.chrome.browser.compositor.CompositorViewHolder; import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.incognito.IncognitoUtils; import org.chromium.chrome.browser.new_tab_url.DseNewTabUrlManager; +import org.chromium.chrome.browser.homepage.HomepageManager; import org.chromium.chrome.browser.ntp.NewTabPageLaunchOrigin; import org.chromium.chrome.browser.ntp.NewTabPageUtils; import org.chromium.chrome.browser.prefetch.settings.PreloadPagesSettingsBridge; @@ -502,6 +503,13 @@ public class ChromeTabCreator extends TabCreator { * @return the created tab. */ public Tab launchUrl(String url, @TabLaunchType int type, Intent intent, long intentTimestamp) { + if (!mIncognito && url.equals(UrlConstants.NTP_URL)) { + if (HomepageManager.getInstance().getPrefNTPIsHomepageEnabled()) { + GURL gurl = HomepageManager.getInstance().getHomepageGurl(); + url = gurl != null ? gurl.getSpec() : url; + } + } + LoadUrlParams loadUrlParams = new LoadUrlParams(url); loadUrlParams.setIntentReceivedTimestamp(intentTimestamp); return createNewTab(loadUrlParams, type, null, intent); diff --git a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeys.java b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeys.java --- a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeys.java +++ b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeys.java @@ -367,6 +367,7 @@ public final class ChromePreferenceKeys { public static final String HOMEPAGE_USE_CHROME_NTP = "Chrome.Homepage.UseNTP"; public static final String HOMEPAGE_USE_DEFAULT_URI = "homepage_partner_enabled"; + public static final String HOMEPAGE_NTP_IS_HOMEPAGE = "newtabpage_is_homepage"; /** * Key used to save homepage location set by enterprise policy */ diff --git a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/LegacyChromePreferenceKeys.java b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/LegacyChromePreferenceKeys.java --- a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/LegacyChromePreferenceKeys.java +++ b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/LegacyChromePreferenceKeys.java @@ -68,6 +68,7 @@ public class LegacyChromePreferenceKeys { ChromePreferenceKeys.HISTORY_SHOW_HISTORY_INFO, ChromePreferenceKeys.HOMEPAGE_ENABLED, ChromePreferenceKeys.HOMEPAGE_USE_DEFAULT_URI, + ChromePreferenceKeys.HOMEPAGE_NTP_IS_HOMEPAGE, ChromePreferenceKeys.INCOGNITO_SHORTCUT_ADDED, ChromePreferenceKeys.LATEST_UNSUPPORTED_VERSION, ChromePreferenceKeys.LOCALE_MANAGER_AUTO_SWITCH, diff --git a/chrome/browser/ui/android/strings/android_chrome_strings.grd b/chrome/browser/ui/android/strings/android_chrome_strings.grd --- a/chrome/browser/ui/android/strings/android_chrome_strings.grd +++ b/chrome/browser/ui/android/strings/android_chrome_strings.grd @@ -1540,6 +1540,9 @@ Your Google account may have other forms of browsing history like searches and a Last hour + + Use for new tabs + Last 24 hours diff --git a/chrome/browser/ui/browser_ui_prefs.cc b/chrome/browser/ui/browser_ui_prefs.cc --- a/chrome/browser/ui/browser_ui_prefs.cc +++ b/chrome/browser/ui/browser_ui_prefs.cc @@ -70,6 +70,8 @@ void RegisterBrowserUserPrefs(user_prefs::PrefRegistrySyncable* registry) { GetHomeButtonAndHomePageIsNewTabPageFlags()); registry->RegisterBooleanPref(prefs::kShowHomeButton, false, GetHomeButtonAndHomePageIsNewTabPageFlags()); + registry->RegisterBooleanPref(prefs::kNewTabPageIsHomePage, false, + GetHomeButtonAndHomePageIsNewTabPageFlags()); registry->RegisterInt64Pref(prefs::kDefaultBrowserLastDeclined, 0); bool reset_check_default = false; diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -1283,6 +1283,10 @@ inline constexpr char kUsedPolicyCertificates[] = // visible on the toolbar. inline constexpr char kShowHomeButton[] = "browser.show_home_button"; +// A boolean specifying whether opening a new tab should open the Home page +// instead of the New Tab page. +inline constexpr char kNewTabPageIsHomePage[] = "newtabpage_is_homepage"; + // Boolean pref to define the default setting for "block offensive words". // The old key value is kept to avoid unnecessary migration code. inline constexpr char kSpeechRecognitionFilterProfanities[] = -- 2.25.1