How to launch a PWA in chrome OS

159 views
Skip to first unread message

Silin Liu

unread,
Dec 16, 2020, 6:33:47 PM12/16/20
to Chromium-dev
Hello,

We have a chrome app and we are planning to change it to a PWA in the future. One example I found how to launch the web app is https://source.chromium.org/chromium/chromium/src/+/master:chrome/browser/extensions/api/management/chrome_management_api_delegate.cc;l=322;drc=80e66447b7b515e9bf55d7ec41cc6ee623ea401c. I put my rough code below about how I do that. However, The web app doesn't show up. Did I find the right example to do that?

  1.   std::string id = web_app::GenerateAppIdFromURL(GURL("http://google.com"));
  2. Profile* profile = ProfileManager::GetActiveUserProfile();
  3. DCHECK(profile);
  4. auto* provider = web_app::WebAppProviderBase::GetProviderBase(profile);
  5. DCHECK(provider);
  6. blink::mojom::DisplayMode display_mode =
  7. provider->registrar().GetAppUserDisplayMode(id);
  8. auto launch_container = apps::mojom::LaunchContainer::kLaunchContainerWindow;
  9. if (display_mode == blink::mojom::DisplayMode::kBrowser)
  10. launch_container = apps::mojom::LaunchContainer::kLaunchContainerTab;
  11. apps::AppServiceProxyFactory::GetForProfile(profile)
  12. ->BrowserAppLauncher()
  13. ->LaunchAppWithParams(apps::AppLaunchParams(
  14. id, launch_container, WindowOpenDisposition::NEW_WINDOW,
  15. apps::mojom::AppLaunchSource::kSourceChromeInternal));

Eric Willigers

unread,
Dec 16, 2020, 8:39:47 PM12/16/20
to Chromium-dev, sili...@google.com
1. Note that the code is for desktop PWAs. The chrome/browser/web_applications/... code is not used by Android.

2. It only works if the relevant app with the URL - such as https://google.com/ - has already been installed. Various system web apps are pre-installed by Chrome OS. Otherwise, the user will have needed to have installed the desktop PWA, for example by using the (+) Install prompt in the omnibox.

Here is an example test that installs a PWA and then opens it: WebAppBrowserTest.ThemeColor . It uses some test helpers, but they contain logic similar to yours.

Silin Liu

unread,
Dec 23, 2020, 4:12:33 PM12/23/20
to Eric Willigers, Chromium-dev
I used InstallWebAppFromInfo (https://source.chromium.org/chromium/chromium/src/+/master:chrome/browser/web_applications/components/install_manager.h;l=142;drc=cf1eb4f9ccfe65063d92bd3c87ba6bde152931e8) to install the web app. However, I got result code kWebAppProviderNotReady (https://source.chromium.org/chromium/chromium/src/+/master:chrome/browser/web_applications/components/web_app_constants.h;drc=c7da897ce80b05744601fdba6edaf0427427dab6;l=109). I cannot find any solution to this error code. Any suggestions?

Here is my implementation,

ScopedObserver<web_app::AppRegistrar, web_app::AppRegistrarObserver> web_apps_observer_{this};
auto* provider = web_app::WebAppProvider::Get(profile);
DCHECK(provider);
web_apps_observer_.Add(&provider->registrar());
auto web_application_info = std::make_unique<WebApplicationInfo>();
web_application_info->start_url = GURL("https://airhorner.com");
web_application_info->display_mode = blink::mojom::DisplayMode::kBrowser;
    provider->install_manager().InstallWebAppFromInfo(
      std::move(web_application_info), web_app::ForInstallableSite::kYes,
      webapps::WebappInstallSource::INTERNAL_DEFAULT,
      base::BindOnce(
        [](const web_app::AppId& installed_app_id,
           web_app::InstallResultCode code) {
            LOG(WARNING) << "APP ID: " + installed_app_id;
            LOG(WARNING) << "RESULT CODE: " + std::to_string( (int) code);
     }));

Eric Willigers

unread,
Dec 27, 2020, 6:34:50 PM12/27/20
to Chromium-dev, sili...@google.com, Chromium-dev, Eric Willigers
kWebAppProviderNotReady can be avoided if you use on_registry_ready()

Reply all
Reply to author
Forward
0 new messages