Commit 70927ec9 authored by Alan Cutter's avatar Alan Cutter Committed by Commit Bot

Pull GetInstalledPwaForUrl() function into extension_util

This is a simple refactor CL that has no changes to behaviour.
We wish to use this function in a couple more places in the future
so bringing it up to this util file will avoid further duplication.

Change-Id: I448ddc47001e9755d514de16e1b2c056e0346886
Reviewed-on: https://chromium-review.googlesource.com/961567Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarBen Wells <benwells@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Commit-Queue: Alan Cutter <alancutter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#544924}
parent 03471a7a
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/launch_util.h" #include "chrome/browser/extensions/launch_util.h"
#include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/web_applications/web_app.h" #include "chrome/browser/web_applications/web_app.h"
...@@ -28,32 +29,8 @@ bool IsWindowedBookmarkApp(const Extension* app, ...@@ -28,32 +29,8 @@ bool IsWindowedBookmarkApp(const Extension* app,
if (!app || !app->from_bookmark()) if (!app || !app->from_bookmark())
return false; return false;
if (GetLaunchContainer(extensions::ExtensionPrefs::Get(context), app) != return GetLaunchContainer(extensions::ExtensionPrefs::Get(context), app) ==
LAUNCH_CONTAINER_WINDOW) { LAUNCH_CONTAINER_WINDOW;
return false;
}
return true;
}
scoped_refptr<const Extension> GetAppForURL(
const GURL& url,
const content::WebContents* web_contents) {
content::BrowserContext* context = web_contents->GetBrowserContext();
for (scoped_refptr<const extensions::Extension> app :
ExtensionRegistry::Get(context)->enabled_extensions()) {
if (!IsWindowedBookmarkApp(app.get(), context))
continue;
const UrlHandlerInfo* url_handler =
UrlHandlers::FindMatchingUrlHandler(app.get(), url);
if (!url_handler)
continue;
return app;
}
return nullptr;
} }
} // namespace } // namespace
...@@ -90,14 +67,19 @@ scoped_refptr<const Extension> GetAppForWindow(content::WebContents* source) { ...@@ -90,14 +67,19 @@ scoped_refptr<const Extension> GetAppForWindow(content::WebContents* source) {
scoped_refptr<const Extension> GetTargetApp(content::WebContents* source, scoped_refptr<const Extension> GetTargetApp(content::WebContents* source,
const GURL& target_url) { const GURL& target_url) {
SCOPED_UMA_HISTOGRAM_TIMER("Extensions.BookmarkApp.GetTargetAppDuration"); SCOPED_UMA_HISTOGRAM_TIMER("Extensions.BookmarkApp.GetTargetAppDuration");
return GetAppForURL(target_url, source); return extensions::util::GetInstalledPwaForUrl(
source->GetBrowserContext(), target_url,
extensions::LAUNCH_CONTAINER_WINDOW);
} }
scoped_refptr<const Extension> GetAppForMainFrameURL( scoped_refptr<const Extension> GetAppForMainFrameURL(
content::WebContents* source) { content::WebContents* source) {
SCOPED_UMA_HISTOGRAM_TIMER( SCOPED_UMA_HISTOGRAM_TIMER(
"Extensions.BookmarkApp.GetAppForCurrentURLDuration"); "Extensions.BookmarkApp.GetAppForCurrentURLDuration");
return GetAppForURL(source->GetMainFrame()->GetLastCommittedURL(), source); return extensions::util::GetInstalledPwaForUrl(
source->GetBrowserContext(),
source->GetMainFrame()->GetLastCommittedURL(),
extensions::LAUNCH_CONTAINER_WINDOW);
} }
void OpenNewForegroundTab(content::NavigationHandle* navigation_handle) { void OpenNewForegroundTab(content::NavigationHandle* navigation_handle) {
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "chrome/browser/banners/app_banner_manager.h" #include "chrome/browser/banners/app_banner_manager.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_sync_service.h" #include "chrome/browser/extensions/extension_sync_service.h"
#include "chrome/browser/extensions/launch_util.h"
#include "chrome/browser/extensions/permissions_updater.h" #include "chrome/browser/extensions/permissions_updater.h"
#include "chrome/browser/extensions/scripting_permissions_modifier.h" #include "chrome/browser/extensions/scripting_permissions_modifier.h"
#include "chrome/browser/extensions/shared_module_service.h" #include "chrome/browser/extensions/shared_module_service.h"
...@@ -22,6 +23,7 @@ ...@@ -22,6 +23,7 @@
#include "chrome/browser/ui/webui/extensions/extension_icon_source.h" #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/api/url_handlers/url_handlers_parser.h"
#include "chrome/common/extensions/sync_helper.h" #include "chrome/common/extensions/sync_helper.h"
#include "components/variations/variations_associated_data.h" #include "components/variations/variations_associated_data.h"
#include "content/public/browser/site_instance.h" #include "content/public/browser/site_instance.h"
...@@ -329,5 +331,24 @@ bool IsExtensionSupervised(const Extension* extension, Profile* profile) { ...@@ -329,5 +331,24 @@ bool IsExtensionSupervised(const Extension* extension, Profile* profile) {
profile->IsSupervised(); profile->IsSupervised();
} }
const Extension* GetInstalledPwaForUrl(
content::BrowserContext* context,
const GURL& url,
base::Optional<LaunchContainer> launch_container_filter) {
const ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
for (scoped_refptr<const Extension> app :
ExtensionRegistry::Get(context)->enabled_extensions()) {
if (!app->from_bookmark())
continue;
if (launch_container_filter &&
GetLaunchContainer(prefs, app.get()) != *launch_container_filter) {
continue;
}
if (UrlHandlers::FindMatchingUrlHandler(app.get(), url))
return app.get();
}
return nullptr;
}
} // namespace util } // namespace util
} // namespace extensions } // namespace extensions
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/optional.h"
#include "extensions/common/constants.h"
namespace base { namespace base {
class DictionaryValue; class DictionaryValue;
} }
...@@ -20,6 +23,7 @@ namespace gfx { ...@@ -20,6 +23,7 @@ namespace gfx {
class ImageSkia; class ImageSkia;
} }
class GURL;
class Profile; class Profile;
namespace extensions { namespace extensions {
...@@ -109,6 +113,13 @@ bool CanHostedAppsOpenInWindows(); ...@@ -109,6 +113,13 @@ bool CanHostedAppsOpenInWindows();
// Returns true for custodian-installed extensions in a supervised profile. // Returns true for custodian-installed extensions in a supervised profile.
bool IsExtensionSupervised(const Extension* extension, Profile* profile); bool IsExtensionSupervised(const Extension* extension, Profile* profile);
// Finds the first PWA with |url| in its scope, returns nullptr if there are
// none.
const Extension* GetInstalledPwaForUrl(
content::BrowserContext* context,
const GURL& url,
base::Optional<LaunchContainer> launch_container_filter = base::nullopt);
} // namespace util } // namespace util
} // namespace extensions } // namespace extensions
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/devtools/devtools_window.h" #include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/download/download_stats.h" #include "chrome/browser/download/download_stats.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/language/language_model_factory.h" #include "chrome/browser/language/language_model_factory.h"
#include "chrome/browser/media/router/media_router_dialog_controller.h" #include "chrome/browser/media/router/media_router_dialog_controller.h"
#include "chrome/browser/media/router/media_router_feature.h" #include "chrome/browser/media/router/media_router_feature.h"
...@@ -502,25 +503,6 @@ void OnProfileCreated(const GURL& link_url, ...@@ -502,25 +503,6 @@ void OnProfileCreated(const GURL& link_url,
} }
} }
// Returns a Bookmark App that has |url| in its scope.
const extensions::Extension* GetBookmarkAppForURL(
BrowserContext* browser_context,
const GURL& target_url) {
const extensions::ExtensionSet& enabled_extensions =
extensions::ExtensionRegistry::Get(browser_context)->enabled_extensions();
for (const auto extension : enabled_extensions) {
if (!extension->from_bookmark())
continue;
const extensions::UrlHandlerInfo* handler =
extensions::UrlHandlers::FindMatchingUrlHandler(extension.get(),
target_url);
if (handler)
return extension.get();
}
return nullptr;
}
} // namespace } // namespace
// static // static
...@@ -1137,26 +1119,26 @@ void RenderViewContextMenu::AppendOpenWithLinkItems() { ...@@ -1137,26 +1119,26 @@ void RenderViewContextMenu::AppendOpenWithLinkItems() {
} }
void RenderViewContextMenu::AppendOpenInBookmarkAppLinkItems() { void RenderViewContextMenu::AppendOpenInBookmarkAppLinkItems() {
const Extension* bookmark_app = const Extension* pwa = extensions::util::GetInstalledPwaForUrl(
GetBookmarkAppForURL(browser_context_, params_.link_url); browser_context_, params_.link_url);
if (!bookmark_app) if (!pwa)
return; return;
int open_in_app_string_id; int open_in_app_string_id;
if (GetBrowser()->app_name() == if (GetBrowser()->app_name() ==
web_app::GenerateApplicationNameFromExtensionId(bookmark_app->id())) { web_app::GenerateApplicationNameFromExtensionId(pwa->id())) {
open_in_app_string_id = IDS_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP_SAMEAPP; open_in_app_string_id = IDS_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP_SAMEAPP;
} else { } else {
open_in_app_string_id = IDS_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP; open_in_app_string_id = IDS_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP;
} }
menu_model_.AddItem(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP, menu_model_.AddItem(
l10n_util::GetStringFUTF16( IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP,
open_in_app_string_id, l10n_util::GetStringFUTF16(open_in_app_string_id,
base::ASCIIToUTF16(bookmark_app->short_name()))); base::ASCIIToUTF16(pwa->short_name())));
MenuManager* menu_manager = MenuManager::Get(browser_context_); MenuManager* menu_manager = MenuManager::Get(browser_context_);
gfx::Image icon = menu_manager->GetIconForExtension(bookmark_app->id()); gfx::Image icon = menu_manager->GetIconForExtension(pwa->id());
menu_model_.SetIcon(menu_model_.GetItemCount() - 1, icon); menu_model_.SetIcon(menu_model_.GetItemCount() - 1, icon);
} }
...@@ -2268,15 +2250,15 @@ bool RenderViewContextMenu::IsOpenLinkOTREnabled() const { ...@@ -2268,15 +2250,15 @@ bool RenderViewContextMenu::IsOpenLinkOTREnabled() const {
} }
void RenderViewContextMenu::ExecOpenBookmarkApp() { void RenderViewContextMenu::ExecOpenBookmarkApp() {
const extensions::Extension* bookmark_app = const Extension* pwa = extensions::util::GetInstalledPwaForUrl(
GetBookmarkAppForURL(browser_context_, params_.link_url); browser_context_, params_.link_url);
// |bookmark_app| could be null if it has been uninstalled since the user // |pwa| could be null if it has been uninstalled since the user
// opened the context menu. // opened the context menu.
if (!bookmark_app) if (!pwa)
return; return;
AppLaunchParams launch_params( AppLaunchParams launch_params(
GetProfile(), bookmark_app, extensions::LAUNCH_CONTAINER_WINDOW, GetProfile(), pwa, extensions::LAUNCH_CONTAINER_WINDOW,
WindowOpenDisposition::CURRENT_TAB, extensions::SOURCE_CONTEXT_MENU); WindowOpenDisposition::CURRENT_TAB, extensions::SOURCE_CONTEXT_MENU);
launch_params.override_url = params_.link_url; launch_params.override_url = params_.link_url;
OpenApplication(launch_params); OpenApplication(launch_params);
......
...@@ -170,12 +170,15 @@ class ContextMenuBrowserTest : public InProcessBrowserTest { ...@@ -170,12 +170,15 @@ class ContextMenuBrowserTest : public InProcessBrowserTest {
return profile_manager->GetProfile(profile_path); return profile_manager->GetProfile(profile_path);
} }
const extensions::Extension* InstallTestBookmarkApp(const GURL& app_url) { const extensions::Extension* InstallTestBookmarkApp(
const GURL& app_url,
bool open_as_window = true) {
WebApplicationInfo web_app_info; WebApplicationInfo web_app_info;
web_app_info.app_url = app_url; web_app_info.app_url = app_url;
web_app_info.scope = app_url; web_app_info.scope = app_url;
web_app_info.title = base::UTF8ToUTF16("Test app"); web_app_info.title = base::UTF8ToUTF16("Test app");
web_app_info.description = base::UTF8ToUTF16("Test description"); web_app_info.description = base::UTF8ToUTF16("Test description");
web_app_info.open_as_window = open_as_window;
return extensions::browsertest_util::InstallBookmarkApp( return extensions::browsertest_util::InstallBookmarkApp(
browser()->profile(), web_app_info); browser()->profile(), web_app_info);
...@@ -360,6 +363,23 @@ IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, ...@@ -360,6 +363,23 @@ IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
ASSERT_FALSE(menu->IsItemInRangePresent(IDC_OPEN_LINK_IN_PROFILE_FIRST, ASSERT_FALSE(menu->IsItemInRangePresent(IDC_OPEN_LINK_IN_PROFILE_FIRST,
IDC_OPEN_LINK_IN_PROFILE_LAST)); IDC_OPEN_LINK_IN_PROFILE_LAST));
} }
IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
OpenInAppPresentForURLsInScopeOfNonWindowedBookmarkApp) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kDesktopPWAWindowing);
InstallTestBookmarkApp(GURL(kAppUrl1), false);
std::unique_ptr<TestRenderViewContextMenu> menu =
CreateContextMenuMediaTypeNone(GURL(kAppUrl1), GURL(kAppUrl1));
ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE));
ASSERT_FALSE(menu->IsItemInRangePresent(IDC_OPEN_LINK_IN_PROFILE_FIRST,
IDC_OPEN_LINK_IN_PROFILE_LAST));
}
IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
OpenEntryInAppAbsentForURLsOutOfScopeOfBookmarkApp) { OpenEntryInAppAbsentForURLsOutOfScopeOfBookmarkApp) {
......
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