Commit 5ae1d025 authored by Robert Woods's avatar Robert Woods Committed by Commit Bot

WebApps: Add WebAppRegistrar* as a field on WebAppFileHandlerManager.

WebAppFileHandlerManager::GetAllFileHandlers involved a technically-
safe-but-suboptimal static downcast from AppRegistrar to
WebAppRegistrar. This CL implements an explicit safe downcast method
on AppRegistrar and its children.

Bug: 938103
Change-Id: I2363916cc6e78bc6cb612856eb29e5b84571749a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1978079Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Reviewed-by: default avatarMatt Giuca <mgiuca@chromium.org>
Commit-Queue: Robert Woods <robertwoods@google.com>
Cr-Commit-Position: refs/heads/master@{#727340}
parent 99908d51
......@@ -22,6 +22,7 @@ class Profile;
namespace web_app {
class AppRegistrarObserver;
class WebAppRegistrar;
enum class ExternalInstallSource;
......@@ -78,6 +79,9 @@ class AppRegistrar {
virtual std::vector<AppId> GetAppIds() const = 0;
// Safe downcast.
virtual WebAppRegistrar* AsWebAppRegistrar() = 0;
// Searches for the first app id in the registry for which the |url| is in
// scope.
base::Optional<AppId> FindAppWithUrlInScope(const GURL& url) const;
......
......@@ -185,6 +185,10 @@ std::vector<web_app::AppId> BookmarkAppRegistrar::GetAppIds() const {
return app_ids;
}
web_app::WebAppRegistrar* BookmarkAppRegistrar::AsWebAppRegistrar() {
return nullptr;
}
const Extension* BookmarkAppRegistrar::GetBookmarkApp(
const web_app::AppId& app_id) const {
const Extension* extension = GetExtension(app_id);
......
......@@ -42,6 +42,7 @@ class BookmarkAppRegistrar : public web_app::AppRegistrar,
std::vector<WebApplicationIconInfo> GetAppIconInfos(
const web_app::AppId& app_id) const override;
std::vector<web_app::AppId> GetAppIds() const override;
web_app::WebAppRegistrar* AsWebAppRegistrar() override;
// ExtensionRegistryObserver:
void OnExtensionUninstalled(content::BrowserContext* browser_context,
......
......@@ -134,4 +134,8 @@ std::vector<AppId> TestAppRegistrar::GetAppIds() const {
return result;
}
WebAppRegistrar* TestAppRegistrar::AsWebAppRegistrar() {
return nullptr;
}
} // namespace web_app
......@@ -55,6 +55,7 @@ class TestAppRegistrar : public AppRegistrar {
std::vector<WebApplicationIconInfo> GetAppIconInfos(
const AppId& app_id) const override;
std::vector<AppId> GetAppIds() const override;
WebAppRegistrar* AsWebAppRegistrar() override;
private:
std::map<AppId, AppInfo> installed_apps_;
......
......@@ -23,10 +23,9 @@ WebAppFileHandlerManager::GetAllFileHandlers(const AppId& app_id) {
if (it != file_handler_infos_.end())
return &it->second;
// TODO(crbug.com/938103): Implement explicit safe downcast from AppRegistrar
// to WebAppRegistrar rather than using static_cast.
WebAppRegistrar* web_registrar = static_cast<WebAppRegistrar*>(registrar());
const WebApp* web_app = web_registrar->GetAppById(app_id);
WebAppRegistrar* web_app_registrar = registrar()->AsWebAppRegistrar();
DCHECK(web_app_registrar);
const WebApp* web_app = web_app_registrar->GetAppById(app_id);
const std::vector<WebApp::FileHandler>& file_handlers =
web_app->file_handlers();
......
......@@ -110,6 +110,10 @@ std::vector<AppId> WebAppRegistrar::GetAppIds() const {
return app_ids;
}
WebAppRegistrar* WebAppRegistrar::AsWebAppRegistrar() {
return this;
}
WebAppRegistrar::AppSet::AppSet(const WebAppRegistrar* registrar)
: registrar_(registrar)
#if DCHECK_IS_ON()
......
......@@ -47,6 +47,7 @@ class WebAppRegistrar : public AppRegistrar {
std::vector<WebApplicationIconInfo> GetAppIconInfos(
const AppId& app_id) const override;
std::vector<AppId> GetAppIds() const override;
WebAppRegistrar* AsWebAppRegistrar() override;
// Only range-based |for| loop supported. Don't use AppSet directly.
// Doesn't support registration and unregistration of WebApp while iterating.
......
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