Skip to content

Commit

Permalink
[M90-LTS] Don't call through a protocol handler if they've changed
Browse files Browse the repository at this point in the history
Have any protocol handler change invalidate the list of protocol
handlers in the context menu.

(cherry picked from commit 2738faa)

Bug: 1227315
Change-Id: Iab8e475454cd946f153102111b02f73de24648fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3017137
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Auto-Submit: Avi Drissman <avi@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#900104}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3041344
Owners-Override: Jana Grill <janagrill@google.com>
Commit-Queue: Roger Felipe Zanoni da Silva <rzanoni@google.com>
Reviewed-by: Artem Sumaneev <asumaneev@google.com>
Reviewed-by: Victor-Gabriel Savu <vsavu@google.com>
Cr-Commit-Position: refs/branch-heads/4430@{#1543}
Cr-Branched-From: e5ce7dc-refs/heads/master@{#857950}
  • Loading branch information
Roger Zanoni authored and Chromium LUCI CQ committed Jul 28, 2021
1 parent 0cd64b3 commit c722a52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
15 changes: 15 additions & 0 deletions chrome/browser/renderer_context_menu/render_view_context_menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,10 @@ void RenderViewContextMenu::AppendProtocolHandlerSubMenu() {
GetHandlersForLinkUrl();
if (handlers.empty())
return;

protocol_handler_registry_observation_.Observe(protocol_handler_registry_);
is_protocol_submenu_valid_ = true;

size_t max = IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST -
IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST;
for (size_t i = 0; i < handlers.size() && i <= max; i++) {
Expand Down Expand Up @@ -2598,6 +2602,10 @@ RenderViewContextMenu::GetHandlersForLinkUrl() {
return handlers;
}

void RenderViewContextMenu::OnProtocolHandlerRegistryChanged() {
is_protocol_submenu_valid_ = false;
}

void RenderViewContextMenu::NotifyMenuShown() {
auto* cb = GetMenuShownCallback();
if (!cb->is_null())
Expand Down Expand Up @@ -2894,6 +2902,13 @@ void RenderViewContextMenu::ExecOpenWebApp() {

void RenderViewContextMenu::ExecProtocolHandler(int event_flags,
int handler_index) {
if (!is_protocol_submenu_valid_) {
// A protocol was changed since the time that the menu was built, so the
// index passed in isn't valid. The only thing that can be done now safely
// is to bail.
return;
}

ProtocolHandlerRegistry::ProtocolHandlerList handlers =
GetHandlersForLinkUrl();
if (handlers.empty())
Expand Down
19 changes: 18 additions & 1 deletion chrome/browser/renderer_context_menu/render_view_context_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

#include "base/files/file_path.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "base/strings/string16.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/ui/browser.h"
Expand Down Expand Up @@ -68,7 +70,8 @@ namespace ui {
class DataTransferEndpoint;
}

class RenderViewContextMenu : public RenderViewContextMenuBase {
class RenderViewContextMenu : public RenderViewContextMenuBase,
public ProtocolHandlerRegistry::Observer {
public:
RenderViewContextMenu(content::RenderFrameHost* render_frame_host,
const content::ContextMenuParams& params);
Expand Down Expand Up @@ -256,15 +259,29 @@ class RenderViewContextMenu : public RenderViewContextMenuBase {
// on URL.
ProtocolHandlerRegistry::ProtocolHandlerList GetHandlersForLinkUrl();

// ProtocolHandlerRegistry::Observer:
void OnProtocolHandlerRegistryChanged() override;

// The destination URL to use if the user tries to search for or navigate to
// a text selection.
GURL selection_navigation_url_;

ui::SimpleMenuModel profile_link_submenu_model_;
std::vector<base::FilePath> profile_link_paths_;
bool multiple_profiles_open_;

// Protocol handling:
// - The submenu containing the installed protocol handlers.
ui::SimpleMenuModel protocol_handler_submenu_model_;
// - The registry with the protocols.
ProtocolHandlerRegistry* protocol_handler_registry_;
// - The observation of the registry.
base::ScopedObservation<ProtocolHandlerRegistry,
ProtocolHandlerRegistry::Observer>
protocol_handler_registry_observation_{this};
// - Whether or not the registered protocols have changed since the menu was
// built.
bool is_protocol_submenu_valid_ = false;

// An observer that handles spelling suggestions, "Add to dictionary", and
// "Use enhanced spell check" items.
Expand Down

0 comments on commit c722a52

Please sign in to comment.