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; ...@@ -22,6 +22,7 @@ class Profile;
namespace web_app { namespace web_app {
class AppRegistrarObserver; class AppRegistrarObserver;
class WebAppRegistrar;
enum class ExternalInstallSource; enum class ExternalInstallSource;
...@@ -78,6 +79,9 @@ class AppRegistrar { ...@@ -78,6 +79,9 @@ class AppRegistrar {
virtual std::vector<AppId> GetAppIds() const = 0; 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 // Searches for the first app id in the registry for which the |url| is in
// scope. // scope.
base::Optional<AppId> FindAppWithUrlInScope(const GURL& url) const; base::Optional<AppId> FindAppWithUrlInScope(const GURL& url) const;
......
...@@ -185,6 +185,10 @@ std::vector<web_app::AppId> BookmarkAppRegistrar::GetAppIds() const { ...@@ -185,6 +185,10 @@ std::vector<web_app::AppId> BookmarkAppRegistrar::GetAppIds() const {
return app_ids; return app_ids;
} }
web_app::WebAppRegistrar* BookmarkAppRegistrar::AsWebAppRegistrar() {
return nullptr;
}
const Extension* BookmarkAppRegistrar::GetBookmarkApp( const Extension* BookmarkAppRegistrar::GetBookmarkApp(
const web_app::AppId& app_id) const { const web_app::AppId& app_id) const {
const Extension* extension = GetExtension(app_id); const Extension* extension = GetExtension(app_id);
......
...@@ -42,6 +42,7 @@ class BookmarkAppRegistrar : public web_app::AppRegistrar, ...@@ -42,6 +42,7 @@ class BookmarkAppRegistrar : public web_app::AppRegistrar,
std::vector<WebApplicationIconInfo> GetAppIconInfos( std::vector<WebApplicationIconInfo> GetAppIconInfos(
const web_app::AppId& app_id) const override; const web_app::AppId& app_id) const override;
std::vector<web_app::AppId> GetAppIds() const override; std::vector<web_app::AppId> GetAppIds() const override;
web_app::WebAppRegistrar* AsWebAppRegistrar() override;
// ExtensionRegistryObserver: // ExtensionRegistryObserver:
void OnExtensionUninstalled(content::BrowserContext* browser_context, void OnExtensionUninstalled(content::BrowserContext* browser_context,
......
...@@ -134,4 +134,8 @@ std::vector<AppId> TestAppRegistrar::GetAppIds() const { ...@@ -134,4 +134,8 @@ std::vector<AppId> TestAppRegistrar::GetAppIds() const {
return result; return result;
} }
WebAppRegistrar* TestAppRegistrar::AsWebAppRegistrar() {
return nullptr;
}
} // namespace web_app } // namespace web_app
...@@ -55,6 +55,7 @@ class TestAppRegistrar : public AppRegistrar { ...@@ -55,6 +55,7 @@ class TestAppRegistrar : public AppRegistrar {
std::vector<WebApplicationIconInfo> GetAppIconInfos( std::vector<WebApplicationIconInfo> GetAppIconInfos(
const AppId& app_id) const override; const AppId& app_id) const override;
std::vector<AppId> GetAppIds() const override; std::vector<AppId> GetAppIds() const override;
WebAppRegistrar* AsWebAppRegistrar() override;
private: private:
std::map<AppId, AppInfo> installed_apps_; std::map<AppId, AppInfo> installed_apps_;
......
...@@ -23,10 +23,9 @@ WebAppFileHandlerManager::GetAllFileHandlers(const AppId& app_id) { ...@@ -23,10 +23,9 @@ WebAppFileHandlerManager::GetAllFileHandlers(const AppId& app_id) {
if (it != file_handler_infos_.end()) if (it != file_handler_infos_.end())
return &it->second; return &it->second;
// TODO(crbug.com/938103): Implement explicit safe downcast from AppRegistrar WebAppRegistrar* web_app_registrar = registrar()->AsWebAppRegistrar();
// to WebAppRegistrar rather than using static_cast. DCHECK(web_app_registrar);
WebAppRegistrar* web_registrar = static_cast<WebAppRegistrar*>(registrar()); const WebApp* web_app = web_app_registrar->GetAppById(app_id);
const WebApp* web_app = web_registrar->GetAppById(app_id);
const std::vector<WebApp::FileHandler>& file_handlers = const std::vector<WebApp::FileHandler>& file_handlers =
web_app->file_handlers(); web_app->file_handlers();
......
...@@ -110,6 +110,10 @@ std::vector<AppId> WebAppRegistrar::GetAppIds() const { ...@@ -110,6 +110,10 @@ std::vector<AppId> WebAppRegistrar::GetAppIds() const {
return app_ids; return app_ids;
} }
WebAppRegistrar* WebAppRegistrar::AsWebAppRegistrar() {
return this;
}
WebAppRegistrar::AppSet::AppSet(const WebAppRegistrar* registrar) WebAppRegistrar::AppSet::AppSet(const WebAppRegistrar* registrar)
: registrar_(registrar) : registrar_(registrar)
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
......
...@@ -47,6 +47,7 @@ class WebAppRegistrar : public AppRegistrar { ...@@ -47,6 +47,7 @@ class WebAppRegistrar : public AppRegistrar {
std::vector<WebApplicationIconInfo> GetAppIconInfos( std::vector<WebApplicationIconInfo> GetAppIconInfos(
const AppId& app_id) const override; const AppId& app_id) const override;
std::vector<AppId> GetAppIds() const override; std::vector<AppId> GetAppIds() const override;
WebAppRegistrar* AsWebAppRegistrar() override;
// Only range-based |for| loop supported. Don't use AppSet directly. // Only range-based |for| loop supported. Don't use AppSet directly.
// Doesn't support registration and unregistration of WebApp while iterating. // 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