cef/patch/patches/chrome_browser_context_menus.patch
2024-04-25 18:43:05 -04:00

249 lines
10 KiB
Diff

diff --git chrome/browser/renderer_context_menu/render_view_context_menu.cc chrome/browser/renderer_context_menu/render_view_context_menu.cc
index 72a2b681d5b98..8f7368add83fb 100644
--- chrome/browser/renderer_context_menu/render_view_context_menu.cc
+++ chrome/browser/renderer_context_menu/render_view_context_menu.cc
@@ -358,6 +358,18 @@ base::OnceCallback<void(RenderViewContextMenu*)>* GetMenuShownCallback() {
return callback.get();
}
+RenderViewContextMenu::MenuCreatedCallback* GetMenuCreatedCallback() {
+ static base::NoDestructor<RenderViewContextMenu::MenuCreatedCallback>
+ callback;
+ return callback.get();
+}
+
+RenderViewContextMenu::MenuShowHandlerCallback* GetMenuShowHandlerCallback() {
+ static base::NoDestructor<RenderViewContextMenu::MenuShowHandlerCallback>
+ callback;
+ return callback.get();
+}
+
enum class UmaEnumIdLookupType {
GeneralEnumId,
ContextSpecificEnumId,
@@ -615,6 +627,10 @@ int FindUMAEnumValueForCommand(int id, UmaEnumIdLookupType type) {
if (ContextMenuMatcher::IsExtensionsCustomCommandId(id))
return 1;
+ // Match the MENU_ID_USER_FIRST to MENU_ID_USER_LAST range from cef_types.h.
+ if (id >= 26500 && id <= 28500)
+ return 1;
+
id = CollapseCommandsForUMA(id);
const auto& map = GetIdcToUmaMap(type);
auto it = map.find(id);
@@ -865,6 +881,14 @@ RenderViewContextMenu::RenderViewContextMenu(
pdf_ocr_submenu_model_ = std::make_unique<ui::SimpleMenuModel>(this);
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
+ auto* cb = GetMenuCreatedCallback();
+ if (!cb->is_null()) {
+ first_observer_ = cb->Run(this);
+ if (first_observer_) {
+ observers_.AddObserver(first_observer_.get());
+ }
+ }
+
observers_.AddObserver(&autofill_context_menu_manager_);
}
@@ -1339,6 +1363,12 @@ void RenderViewContextMenu::InitMenu() {
autofill::PopupHidingReason::kContextMenuOpened);
}
}
+
+ if (first_observer_) {
+ // Do this last so that the observer can optionally modify previously
+ // created items.
+ first_observer_->InitMenu(params_);
+ }
}
Profile* RenderViewContextMenu::GetProfile() const {
@@ -3572,6 +3602,26 @@ void RenderViewContextMenu::RegisterExecutePluginActionCallbackForTesting(
execute_plugin_action_callback_ = std::move(cb);
}
+// static
+void RenderViewContextMenu::RegisterMenuCreatedCallback(
+ MenuCreatedCallback cb) {
+ *GetMenuCreatedCallback() = cb;
+}
+
+// static
+void RenderViewContextMenu::RegisterMenuShowHandlerCallback(
+ MenuShowHandlerCallback cb) {
+ *GetMenuShowHandlerCallback() = cb;
+}
+
+bool RenderViewContextMenu::UseShowHandler() {
+ auto* cb = GetMenuShowHandlerCallback();
+ if (!cb->is_null() && cb->Run(this)) {
+ return true;
+ }
+ return false;
+}
+
custom_handlers::ProtocolHandlerRegistry::ProtocolHandlerList
RenderViewContextMenu::GetHandlersForLinkUrl() {
custom_handlers::ProtocolHandlerRegistry::ProtocolHandlerList handlers =
diff --git chrome/browser/renderer_context_menu/render_view_context_menu.h chrome/browser/renderer_context_menu/render_view_context_menu.h
index 548004a597fa0..f63405fa8badc 100644
--- chrome/browser/renderer_context_menu/render_view_context_menu.h
+++ chrome/browser/renderer_context_menu/render_view_context_menu.h
@@ -155,7 +155,21 @@ class RenderViewContextMenu
}
#endif
+ // Registers a callback that will be called each time a context menu is
+ // created.
+ using MenuCreatedCallback = base::RepeatingCallback<
+ std::unique_ptr<RenderViewContextMenuObserver>(RenderViewContextMenu*)>;
+ static void RegisterMenuCreatedCallback(MenuCreatedCallback cb);
+
+ // Registers a callback that will be called each time before context menu is
+ // shown. The callback should return true if the show has handled.
+ using MenuShowHandlerCallback = base::RepeatingCallback<
+ bool(RenderViewContextMenu*)>;
+ static void RegisterMenuShowHandlerCallback(MenuShowHandlerCallback cb);
+
protected:
+ bool UseShowHandler();
+
Profile* GetProfile() const;
// This may return nullptr (e.g. for WebUI dialogs). Virtual to allow tests to
@@ -465,6 +479,9 @@ class RenderViewContextMenu
// built.
bool is_protocol_submenu_valid_ = false;
+ // An observer returned via MenuCreatedCallback that will be called first.
+ std::unique_ptr<RenderViewContextMenuObserver> first_observer_;
+
// An observer that handles spelling suggestions, "Add to dictionary", and
// "Use enhanced spell check" items.
std::unique_ptr<SpellingMenuObserver> spelling_suggestions_menu_observer_;
diff --git chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac_cocoa.mm chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac_cocoa.mm
index b130e9768c2d6..049c5fb235d87 100644
--- chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac_cocoa.mm
+++ chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac_cocoa.mm
@@ -68,6 +68,10 @@ RenderViewContextMenuMacCocoa::~RenderViewContextMenuMacCocoa() {
}
void RenderViewContextMenuMacCocoa::Show() {
+ if (UseShowHandler()) {
+ return;
+ }
+
views::Widget* widget = views::Widget::GetTopLevelWidgetForNativeView(
source_web_contents_->GetNativeView());
diff --git chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.cc chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.cc
index c88a77a0b49e2..d1af9a85c4ec6 100644
--- chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.cc
+++ chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.cc
@@ -149,6 +149,9 @@ void RenderViewContextMenuViews::RunMenuAt(views::Widget* parent,
bool RenderViewContextMenuViews::GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accel) const {
+ if (RenderViewContextMenu::GetAcceleratorForCommandId(command_id, accel))
+ return true;
+
// There are no formally defined accelerators we can query so we assume
// that Ctrl+C, Ctrl+V, Ctrl+X, Ctrl-A, etc do what they normally do.
switch (command_id) {
@@ -385,6 +388,10 @@ void RenderViewContextMenuViews::AppendPlatformEditableItems() {
}
void RenderViewContextMenuViews::Show() {
+ if (UseShowHandler()) {
+ return;
+ }
+
if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
return;
diff --git components/renderer_context_menu/render_view_context_menu_base.cc components/renderer_context_menu/render_view_context_menu_base.cc
index 8e45cecb17039..e40115e23ee82 100644
--- components/renderer_context_menu/render_view_context_menu_base.cc
+++ components/renderer_context_menu/render_view_context_menu_base.cc
@@ -374,6 +374,17 @@ bool RenderViewContextMenuBase::IsCommandIdChecked(int id) const {
return false;
}
+bool RenderViewContextMenuBase::GetAcceleratorForCommandId(
+ int id,
+ ui::Accelerator* accelerator) const {
+ for (auto& observer : observers_) {
+ if (observer.IsCommandIdSupported(id))
+ return observer.GetAccelerator(id, accelerator);
+ }
+
+ return false;
+}
+
void RenderViewContextMenuBase::ExecuteCommand(int id, int event_flags) {
command_executed_ = true;
RecordUsedItem(id);
diff --git components/renderer_context_menu/render_view_context_menu_base.h components/renderer_context_menu/render_view_context_menu_base.h
index 57b288bc885e6..112990e3a9ad3 100644
--- components/renderer_context_menu/render_view_context_menu_base.h
+++ components/renderer_context_menu/render_view_context_menu_base.h
@@ -87,6 +87,9 @@ class RenderViewContextMenuBase : public ui::SimpleMenuModel::Delegate,
const ui::SimpleMenuModel& menu_model() const { return menu_model_; }
const content::ContextMenuParams& params() const { return params_; }
+ content::WebContents* source_web_contents() const {
+ return source_web_contents_;
+ }
// Returns true if the specified command id is known and valid for
// this menu. If the command is known |enabled| is set to indicate
@@ -95,6 +98,9 @@ class RenderViewContextMenuBase : public ui::SimpleMenuModel::Delegate,
// SimpleMenuModel::Delegate implementation.
bool IsCommandIdChecked(int command_id) const override;
+ bool GetAcceleratorForCommandId(
+ int command_id,
+ ui::Accelerator* accelerator) const override;
void ExecuteCommand(int command_id, int event_flags) override;
void OnMenuWillShow(ui::SimpleMenuModel* source) override;
void MenuClosed(ui::SimpleMenuModel* source) override;
diff --git components/renderer_context_menu/render_view_context_menu_observer.cc components/renderer_context_menu/render_view_context_menu_observer.cc
index 9fdda1636003d..538bd05a41296 100644
--- components/renderer_context_menu/render_view_context_menu_observer.cc
+++ components/renderer_context_menu/render_view_context_menu_observer.cc
@@ -15,3 +15,8 @@ bool RenderViewContextMenuObserver::IsCommandIdChecked(int command_id) {
bool RenderViewContextMenuObserver::IsCommandIdEnabled(int command_id) {
return false;
}
+
+bool RenderViewContextMenuObserver::GetAccelerator(int command_id,
+ ui::Accelerator* accel) {
+ return false;
+}
diff --git components/renderer_context_menu/render_view_context_menu_observer.h components/renderer_context_menu/render_view_context_menu_observer.h
index 0527c57abd51c..70bebcbb50461 100644
--- components/renderer_context_menu/render_view_context_menu_observer.h
+++ components/renderer_context_menu/render_view_context_menu_observer.h
@@ -11,6 +11,10 @@ namespace content {
struct ContextMenuParams;
}
+namespace ui {
+class Accelerator;
+}
+
// The interface used for implementing context-menu items. The following
// instruction describe how to implement a context-menu item with this
// interface.
@@ -100,6 +104,8 @@ class RenderViewContextMenuObserver {
virtual bool IsCommandIdChecked(int command_id);
virtual bool IsCommandIdEnabled(int command_id);
+ virtual bool GetAccelerator(int command_id, ui::Accelerator* accel);
+
// Called when a user selects the specified context-menu item. This is
// only called when the observer returns true for IsCommandIdSupported()
// for that |command_id|.