Commit 692e32e4 authored by Alan Cutter's avatar Alan Cutter Committed by Commit Bot

Reland: Fix crash when right clicking link in CastUI

This CL adds a null check to RenderViewContextMenu::AppendLinkItems()
ensuring we have an associated Browser before attempting to use it.

This is a reland of https://chromium-review.googlesource.com/c/chromium/src/+/1158110
without using TestWebContents (which results in a bad downcast).

Bug: 869280
Change-Id: I6799f0c746c0cdca941e1235ac476f4e1d8a1f0e
Reviewed-on: https://chromium-review.googlesource.com/1160121Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Alan Cutter <alancutter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580405}
parent 98a481dd
...@@ -1032,13 +1032,16 @@ void RenderViewContextMenu::AppendDevtoolsForUnpackedExtensions() { ...@@ -1032,13 +1032,16 @@ void RenderViewContextMenu::AppendDevtoolsForUnpackedExtensions() {
void RenderViewContextMenu::AppendLinkItems() { void RenderViewContextMenu::AppendLinkItems() {
if (!params_.link_url.is_empty()) { if (!params_.link_url.is_empty()) {
if (base::FeatureList::IsEnabled(features::kDesktopPWAWindowing)) { if (base::FeatureList::IsEnabled(features::kDesktopPWAWindowing)) {
const Browser* browser = GetBrowser();
const bool is_app = browser && browser->is_app();
AppendOpenInBookmarkAppLinkItems(); AppendOpenInBookmarkAppLinkItems();
menu_model_.AddItemWithStringId( menu_model_.AddItemWithStringId(
IDC_CONTENT_CONTEXT_OPENLINKNEWTAB, IDC_CONTENT_CONTEXT_OPENLINKNEWTAB,
GetBrowser()->is_app() ? IDS_CONTENT_CONTEXT_OPENLINKNEWTAB_INAPP is_app ? IDS_CONTENT_CONTEXT_OPENLINKNEWTAB_INAPP
: IDS_CONTENT_CONTEXT_OPENLINKNEWTAB); : IDS_CONTENT_CONTEXT_OPENLINKNEWTAB);
if (!GetBrowser()->is_app()) { if (!is_app) {
menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW, menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW,
IDS_CONTENT_CONTEXT_OPENLINKNEWWINDOW); IDS_CONTENT_CONTEXT_OPENLINKNEWWINDOW);
} }
...@@ -1049,9 +1052,8 @@ void RenderViewContextMenu::AppendLinkItems() { ...@@ -1049,9 +1052,8 @@ void RenderViewContextMenu::AppendLinkItems() {
menu_model_.AddItemWithStringId( menu_model_.AddItemWithStringId(
IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD, IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD,
GetBrowser()->is_app() is_app ? IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD_INAPP
? IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD_INAPP : IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD);
: IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD);
} else { } else {
menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB, menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB,
...@@ -1193,8 +1195,10 @@ void RenderViewContextMenu::AppendOpenInBookmarkAppLinkItems() { ...@@ -1193,8 +1195,10 @@ void RenderViewContextMenu::AppendOpenInBookmarkAppLinkItems() {
return; return;
int open_in_app_string_id; int open_in_app_string_id;
if (GetBrowser()->app_name() == const Browser* browser = GetBrowser();
web_app::GenerateApplicationNameFromExtensionId(pwa->id())) { if (browser &&
browser->app_name() ==
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;
......
...@@ -81,6 +81,8 @@ class RenderViewContextMenu : public RenderViewContextMenuBase { ...@@ -81,6 +81,8 @@ class RenderViewContextMenu : public RenderViewContextMenuBase {
protected: protected:
Profile* GetProfile() const; Profile* GetProfile() const;
// This may return nullptr (e.g. for WebUI dialogs).
Browser* GetBrowser() const; Browser* GetBrowser() const;
// Returns a (possibly truncated) version of the current selection text // Returns a (possibly truncated) version of the current selection text
......
...@@ -1244,4 +1244,18 @@ IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, ...@@ -1244,4 +1244,18 @@ IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
EXPECT_TRUE(menu.IsItemChecked(IDC_CONTENT_CONTEXT_PICTUREINPICTURE)); EXPECT_TRUE(menu.IsItemChecked(IDC_CONTENT_CONTEXT_PICTUREINPICTURE));
} }
// This test checks that we don't crash when creating a context menu for a
// WebContents with no Browser.
IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, BrowserlessWebContentsCrash) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kDesktopPWAWindowing);
std::unique_ptr<content::WebContents> web_contents =
content::WebContents::Create(
content::WebContents::CreateParams(browser()->profile()));
CreateContextMenuInWebContents(
web_contents.get(), GURL("http://www.google.com/"),
GURL("http://www.google.com/"), base::ASCIIToUTF16("Google"),
blink::WebContextMenuData::kMediaTypeNone, ui::MENU_SOURCE_MOUSE);
}
} // namespace } // namespace
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