Commit e8da5098 authored by Eric Willigers's avatar Eric Willigers Committed by Commit Bot

GetAppIdForTab supports web apps

GetAppIdForTab now supports web apps, in addition to
bookmark apps and Chrome platform apps.

AppServiceAppWindowWebAppBrowserTest is now parameterized
by web_app::ProviderType.

Bug: 1048055
Change-Id: I2fc9f656f88634d6b0c2f03a2531ea7914452797
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2078377Reviewed-by: default avatarNancy Wang <nancylingwang@chromium.org>
Commit-Queue: Eric Willigers <ericwilligers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746668}
parent 3b84ad80
......@@ -8,6 +8,7 @@
#include "ash/public/cpp/shelf_model.h"
#include "ash/shell.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
......@@ -17,6 +18,7 @@
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/common/chrome_features.h"
#include "chrome/services/app_service/public/cpp/instance.h"
#include "chrome/services/app_service/public/cpp/instance_registry.h"
......@@ -338,6 +340,9 @@ IN_PROC_BROWSER_TEST_F(AppServiceAppWindowBrowserTest,
EXPECT_EQ(0u, windows.size());
}
// TODO(crbug.com/2078377): Parameterize this test, retire
// AppServiceWebAndBookmarkAppBrowserTest.
class AppServiceAppWindowWebAppBrowserTest
: public AppServiceAppWindowBrowserTest {
protected:
......@@ -427,6 +432,32 @@ IN_PROC_BROWSER_TEST_F(AppServiceAppWindowWebAppBrowserTest, WebAppsWindow) {
EXPECT_EQ(0u, windows.size());
}
class AppServiceWebAndBookmarkAppBrowserTest
: public AppServiceAppWindowWebAppBrowserTest,
public ::testing::WithParamInterface<web_app::ProviderType> {
protected:
AppServiceWebAndBookmarkAppBrowserTest() {
if (GetParam() == web_app::ProviderType::kWebApps) {
scoped_feature_list_.InitAndEnableFeature(
features::kDesktopPWAsWithoutExtensions);
} else if (GetParam() == web_app::ProviderType::kBookmarkApps) {
scoped_feature_list_.InitAndDisableFeature(
features::kDesktopPWAsWithoutExtensions);
}
}
~AppServiceWebAndBookmarkAppBrowserTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(AppServiceWebAndBookmarkAppBrowserTest, GetWindows) {
const std::string app_id = CreateWebApp();
auto windows = app_service_proxy_->InstanceRegistry().GetWindows(app_id);
EXPECT_EQ(1u, windows.size());
}
class AppServiceAppWindowArcAppBrowserTest
: public AppServiceAppWindowBrowserTest {
protected:
......@@ -610,3 +641,9 @@ IN_PROC_BROWSER_TEST_F(AppServiceAppWindowArcAppBrowserTest, ArcAppsWindow) {
StopInstance();
}
INSTANTIATE_TEST_SUITE_P(All,
AppServiceWebAndBookmarkAppBrowserTest,
::testing::Values(web_app::ProviderType::kBookmarkApps,
web_app::ProviderType::kWebApps),
web_app::ProviderTypeParamToString);
......@@ -28,7 +28,10 @@
#include "chrome/browser/ui/app_list/internal_app/internal_app_metadata.h"
#include "chrome/browser/ui/ash/launcher/arc_app_shelf_id.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/web_applications/components/app_registrar.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/browser/web_applications/components/web_app_id.h"
#include "chrome/browser/web_applications/components/web_app_provider_base.h"
#include "chrome/browser/web_applications/extensions/bookmark_app_util.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
......@@ -83,6 +86,36 @@ const extensions::Extension* GetExtensionForTab(Profile* profile,
return nullptr;
}
base::Optional<std::string> GetAppIdForTab(Profile* profile,
content::WebContents* tab) {
if (base::FeatureList::IsEnabled(features::kDesktopPWAsWithoutExtensions)) {
if (web_app::WebAppProviderBase* provider =
web_app::WebAppProviderBase::GetProviderBase(profile)) {
// Use the Browser's app name to determine the web app for app windows and
// use the tab's url for app tabs.
// Note: It is possible to come here after a tab got removed from the
// browser before it gets destroyed, in which case there is no browser.
if (Browser* browser = chrome::FindBrowserWithWebContents(tab)) {
if (browser->app_controller() && browser->app_controller()->HasAppId())
return browser->app_controller()->GetAppId();
}
base::Optional<web_app::AppId> app_id =
provider->registrar().FindAppWithUrlInScope(tab->GetURL());
if (app_id)
return app_id;
}
}
const extensions::Extension* extension = GetExtensionForTab(profile, tab);
if (extension &&
(!extension->from_bookmark() ||
!base::FeatureList::IsEnabled(features::kDesktopPWAsWithoutExtensions)))
return extension->id();
return base::nullopt;
}
} // namespace
LauncherControllerHelper::LauncherControllerHelper(Profile* profile)
......@@ -126,16 +159,17 @@ std::string LauncherControllerHelper::GetAppID(content::WebContents* tab) {
profile_manager->GetLoadedProfiles();
if (!profile_list.empty()) {
for (auto* i : profile_list) {
const extensions::Extension* extension = GetExtensionForTab(i, tab);
if (extension)
return extension->id();
base::Optional<std::string> app_id = GetAppIdForTab(i, tab);
if (app_id.has_value())
return *app_id;
}
return std::string();
}
}
// If there is no profile manager we only use the known profile.
const extensions::Extension* extension = GetExtensionForTab(profile_, tab);
return extension ? extension->id() : std::string();
base::Optional<std::string> app_id = GetAppIdForTab(profile_, tab);
return app_id.has_value() ? *app_id : std::string();
}
bool LauncherControllerHelper::IsValidIDForCurrentUser(
......
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