Commit 27c8c826 authored by Phillis Tang's avatar Phillis Tang Committed by Chromium LUCI CQ

DPWA: override user_display_mode when install is user initiated

On WebAppInstallFinalizer::FinalizeInstall, if there is an existing app
but the install is user initiated, overwrite user controllable fields
like user_display_mode.

Bug: 1124300
Change-Id: I011fb0f9306b89d6de76256d3d6c7b197c310171
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2583202
Commit-Queue: Phillis Tang <phillis@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Reviewed-by: default avatarAlan Cutter <alancutter@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836795}
parent acc7ed27
...@@ -29,8 +29,8 @@ namespace web_app { ...@@ -29,8 +29,8 @@ namespace web_app {
class CreateShortcutBrowserTest : public WebAppControllerBrowserTest { class CreateShortcutBrowserTest : public WebAppControllerBrowserTest {
public: public:
AppId InstallShortcutAppForCurrentUrl() { AppId InstallShortcutAppForCurrentUrl(bool open_as_window = false) {
chrome::SetAutoAcceptWebAppDialogForTesting(true, false); chrome::SetAutoAcceptWebAppDialogForTesting(true, open_as_window);
WebAppInstallObserver observer(profile()); WebAppInstallObserver observer(profile());
CHECK(chrome::ExecuteCommand(browser(), IDC_CREATE_SHORTCUT)); CHECK(chrome::ExecuteCommand(browser(), IDC_CREATE_SHORTCUT));
AppId app_id = observer.AwaitNextInstall(); AppId app_id = observer.AwaitNextInstall();
...@@ -195,4 +195,20 @@ IN_PROC_BROWSER_TEST_F(CreateShortcutBrowserTest, IgnoreInvalidManifestData) { ...@@ -195,4 +195,20 @@ IN_PROC_BROWSER_TEST_F(CreateShortcutBrowserTest, IgnoreInvalidManifestData) {
EXPECT_EQ(registrar().GetAppStartUrl(app_id), url); EXPECT_EQ(registrar().GetAppStartUrl(app_id), url);
} }
IN_PROC_BROWSER_TEST_F(CreateShortcutBrowserTest,
CreateShortcutAgainOverwriteUserDisplayMode) {
base::UserActionTester user_action_tester;
NavigateToURLAndWait(browser(), GetInstallableAppURL());
AppId app_id = InstallShortcutAppForCurrentUrl();
EXPECT_EQ(registrar().GetAppShortName(app_id), GetInstallableAppName());
// Shortcut apps to PWAs should launch in a tab.
EXPECT_EQ(registrar().GetAppUserDisplayMode(app_id), DisplayMode::kBrowser);
InstallShortcutAppForCurrentUrl(/*open_as_window=*/true);
// Re-install with enabling open_as_window should update user display mode.
EXPECT_EQ(registrar().GetAppUserDisplayMode(app_id),
DisplayMode::kStandalone);
}
} // namespace web_app } // namespace web_app
...@@ -130,11 +130,8 @@ void WebAppInstallFinalizer::FinalizeInstall( ...@@ -130,11 +130,8 @@ void WebAppInstallFinalizer::FinalizeInstall(
const WebApp* existing_web_app = GetWebAppRegistrar().GetAppById(app_id); const WebApp* existing_web_app = GetWebAppRegistrar().GetAppById(app_id);
std::unique_ptr<WebApp> web_app; std::unique_ptr<WebApp> web_app;
if (existing_web_app) { if (existing_web_app) {
// There is an existing app from other source(s). Preserve // Prepare copy-on-write:
// |user_display_mode| and any user-controllable fields here, do not modify
// them. Prepare copy-on-write:
DCHECK_EQ(web_app_info.start_url, existing_web_app->start_url()); DCHECK_EQ(web_app_info.start_url, existing_web_app->start_url());
web_app = std::make_unique<WebApp>(*existing_web_app); web_app = std::make_unique<WebApp>(*existing_web_app);
...@@ -148,11 +145,18 @@ void WebAppInstallFinalizer::FinalizeInstall( ...@@ -148,11 +145,18 @@ void WebAppInstallFinalizer::FinalizeInstall(
web_app = std::make_unique<WebApp>(app_id); web_app = std::make_unique<WebApp>(app_id);
web_app->SetStartUrl(web_app_info.start_url); web_app->SetStartUrl(web_app_info.start_url);
web_app->SetIsLocallyInstalled(options.locally_installed); web_app->SetIsLocallyInstalled(options.locally_installed);
if (options.locally_installed)
web_app->SetInstallTime(base::Time::Now());
}
// Set |user_display_mode| and any user-controllable fields here if this
// install is user initiated or it's a new app.
if (webapps::InstallableMetrics::IsUserInitiatedInstallSource(
options.install_source) ||
!existing_web_app) {
web_app->SetUserDisplayMode(web_app_info.open_as_window web_app->SetUserDisplayMode(web_app_info.open_as_window
? DisplayMode::kStandalone ? DisplayMode::kStandalone
: DisplayMode::kBrowser); : DisplayMode::kBrowser);
if (options.locally_installed)
web_app->SetInstallTime(base::Time::Now());
} }
// `WebApp::chromeos_data` has a default value already. Only override if the // `WebApp::chromeos_data` has a default value already. Only override if the
......
...@@ -1633,7 +1633,9 @@ TEST_F(WebAppInstallManagerTest, ...@@ -1633,7 +1633,9 @@ TEST_F(WebAppInstallManagerTest,
EXPECT_TRUE(registrar().IsInstalled(app_id)); EXPECT_TRUE(registrar().IsInstalled(app_id));
EXPECT_TRUE(registrar().IsLocallyInstalled(app_id)); EXPECT_TRUE(registrar().IsLocallyInstalled(app_id));
EXPECT_EQ(DisplayMode::kStandalone, // InstallWebAppFromManifestWithFallback sets user_display_mode to kBrowser
// because TestAcceptDialogCallback doesn't set open_as_window to true.
EXPECT_EQ(DisplayMode::kBrowser,
registrar().GetAppEffectiveDisplayMode(app_id)); registrar().GetAppEffectiveDisplayMode(app_id));
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "components/webapps/installable/installable_metrics.h" #include "components/webapps/installable/installable_metrics.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/webapps/webapps_client.h" #include "components/webapps/webapps_client.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
...@@ -38,6 +39,36 @@ bool InstallableMetrics::IsReportableInstallSource(WebappInstallSource source) { ...@@ -38,6 +39,36 @@ bool InstallableMetrics::IsReportableInstallSource(WebappInstallSource source) {
source == WebappInstallSource::MENU_CREATE_SHORTCUT; source == WebappInstallSource::MENU_CREATE_SHORTCUT;
} }
// static
bool InstallableMetrics::IsUserInitiatedInstallSource(
WebappInstallSource source) {
switch (source) {
case WebappInstallSource::MENU_BROWSER_TAB:
case WebappInstallSource::MENU_CUSTOM_TAB:
case WebappInstallSource::AUTOMATIC_PROMPT_BROWSER_TAB:
case WebappInstallSource::AUTOMATIC_PROMPT_CUSTOM_TAB:
case WebappInstallSource::API_BROWSER_TAB:
case WebappInstallSource::API_CUSTOM_TAB:
case WebappInstallSource::AMBIENT_BADGE_BROWSER_TAB:
case WebappInstallSource::AMBIENT_BADGE_CUSTOM_TAB:
case WebappInstallSource::ARC:
case WebappInstallSource::OMNIBOX_INSTALL_ICON:
case WebappInstallSource::MENU_CREATE_SHORTCUT:
return true;
case WebappInstallSource::DEVTOOLS:
case WebappInstallSource::MANAGEMENT_API:
case WebappInstallSource::INTERNAL_DEFAULT:
case WebappInstallSource::EXTERNAL_DEFAULT:
case WebappInstallSource::EXTERNAL_POLICY:
case WebappInstallSource::SYSTEM_DEFAULT:
case WebappInstallSource::SYNC:
return false;
case WebappInstallSource::COUNT:
NOTREACHED();
return false;
}
}
// static // static
WebappInstallSource InstallableMetrics::GetInstallSource( WebappInstallSource InstallableMetrics::GetInstallSource(
content::WebContents* web_contents, content::WebContents* web_contents,
......
...@@ -112,6 +112,9 @@ class InstallableMetrics { ...@@ -112,6 +112,9 @@ class InstallableMetrics {
// TrackInstallEvent. // TrackInstallEvent.
static bool IsReportableInstallSource(WebappInstallSource source); static bool IsReportableInstallSource(WebappInstallSource source);
// Returns whether the install initiated by the user based on install source.
static bool IsUserInitiatedInstallSource(WebappInstallSource source);
// Returns the appropriate WebappInstallSource for |web_contents| when the // Returns the appropriate WebappInstallSource for |web_contents| when the
// install originates from |trigger|. // install originates from |trigger|.
static WebappInstallSource GetInstallSource( static WebappInstallSource GetInstallSource(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment