2012-04-19 22:31:46 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "libcef/browser/menu_manager.h"
|
|
|
|
|
2022-01-25 20:40:24 +01:00
|
|
|
#include <tuple>
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
#include "libcef/browser/context_menu_params_impl.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "libcef/browser/menu_runner.h"
|
2015-09-09 16:05:39 +02:00
|
|
|
#include "libcef/browser/thread_util.h"
|
2020-06-28 23:05:36 +02:00
|
|
|
#include "libcef/common/app_manager.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
2016-07-14 03:35:07 +02:00
|
|
|
#include "cef/grit/cef_strings.h"
|
|
|
|
#include "chrome/grit/generated_resources.h"
|
2014-02-05 21:35:45 +01:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2021-04-21 00:52:34 +02:00
|
|
|
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
CefString GetLabel(int message_id) {
|
2021-04-21 00:52:34 +02:00
|
|
|
std::u16string label =
|
2020-06-28 23:05:36 +02:00
|
|
|
CefAppManager::Get()->GetContentClient()->GetLocalizedString(message_id);
|
2012-04-19 22:31:46 +02:00
|
|
|
DCHECK(!label.empty());
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
const int kInvalidCommandId = -1;
|
|
|
|
const cef_event_flags_t kEmptyEventFlags = static_cast<cef_event_flags_t>(0);
|
|
|
|
|
|
|
|
class CefRunContextMenuCallbackImpl : public CefRunContextMenuCallback {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
using Callback = base::OnceCallback<void(int, cef_event_flags_t)>;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2021-06-04 03:34:56 +02:00
|
|
|
explicit CefRunContextMenuCallbackImpl(Callback callback)
|
|
|
|
: callback_(std::move(callback)) {}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefRunContextMenuCallbackImpl(const CefRunContextMenuCallbackImpl&) = delete;
|
|
|
|
CefRunContextMenuCallbackImpl& operator=(
|
|
|
|
const CefRunContextMenuCallbackImpl&) = delete;
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
~CefRunContextMenuCallbackImpl() {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// The callback is still pending. Cancel it now.
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
2021-06-04 03:34:56 +02:00
|
|
|
RunNow(std::move(callback_), kInvalidCommandId, kEmptyEventFlags);
|
2015-09-09 16:05:39 +02:00
|
|
|
} else {
|
2021-06-04 03:34:56 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(&CefRunContextMenuCallbackImpl::RunNow,
|
|
|
|
std::move(callback_), kInvalidCommandId,
|
|
|
|
kEmptyEventFlags));
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue(int command_id, cef_event_flags_t event_flags) override {
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
if (!callback_.is_null()) {
|
2021-06-04 03:34:56 +02:00
|
|
|
RunNow(std::move(callback_), command_id, event_flags);
|
2015-09-09 16:05:39 +02:00
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefRunContextMenuCallbackImpl::Continue,
|
|
|
|
this, command_id, event_flags));
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Cancel() override { Continue(kInvalidCommandId, kEmptyEventFlags); }
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Disconnect() { callback_.Reset(); }
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
private:
|
2021-06-04 03:34:56 +02:00
|
|
|
static void RunNow(Callback callback,
|
2015-09-09 16:05:39 +02:00
|
|
|
int command_id,
|
|
|
|
cef_event_flags_t event_flags) {
|
|
|
|
CEF_REQUIRE_UIT();
|
2021-06-04 03:34:56 +02:00
|
|
|
std::move(callback).Run(command_id, event_flags);
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Callback callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefRunContextMenuCallbackImpl);
|
|
|
|
};
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
} // namespace
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefMenuManager::CefMenuManager(AlloyBrowserHostImpl* browser,
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<CefMenuRunner> runner)
|
2017-05-17 11:29:28 +02:00
|
|
|
: content::WebContentsObserver(browser->web_contents()),
|
|
|
|
browser_(browser),
|
|
|
|
runner_(std::move(runner)),
|
2020-01-15 14:36:24 +01:00
|
|
|
custom_menu_callback_(nullptr),
|
2017-05-17 11:29:28 +02:00
|
|
|
weak_ptr_factory_(this) {
|
2015-11-17 19:20:13 +01:00
|
|
|
DCHECK(web_contents());
|
2017-02-22 19:05:27 +01:00
|
|
|
model_ = new CefMenuModelImpl(this, nullptr, false);
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
CefMenuManager::~CefMenuManager() {
|
2012-04-19 22:31:46 +02:00
|
|
|
// The model may outlive the delegate if the context menu is visible when the
|
|
|
|
// application is closed.
|
2020-01-15 14:36:24 +01:00
|
|
|
model_->set_delegate(nullptr);
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::Destroy() {
|
|
|
|
CancelContextMenu();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (runner_) {
|
2020-01-15 14:36:24 +01:00
|
|
|
runner_.reset(nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefMenuManager::IsShowingContextMenu() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!web_contents()) {
|
2013-02-13 18:11:50 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-07-27 01:19:27 +02:00
|
|
|
return web_contents()->IsShowingContextMenu();
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
bool CefMenuManager::CreateContextMenu(
|
2012-04-19 22:31:46 +02:00
|
|
|
const content::ContextMenuParams& params) {
|
|
|
|
// The renderer may send the "show context menu" message multiple times, one
|
|
|
|
// for each right click mouse event it receives. Normally, this doesn't happen
|
|
|
|
// because mouse events are not forwarded once the context menu is showing.
|
|
|
|
// However, there's a race - the context menu may not yet be showing when
|
|
|
|
// the second mouse event arrives. In this case, |HandleContextMenu()| will
|
|
|
|
// get called multiple times - if so, don't create another context menu.
|
|
|
|
// TODO(asvitkine): Fix the renderer so that it doesn't do this.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsShowingContextMenu()) {
|
2012-04-19 22:31:46 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
params_ = params;
|
|
|
|
model_->Clear();
|
|
|
|
|
|
|
|
// Create the default menu model.
|
|
|
|
CreateDefaultModel();
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
bool custom_menu = false;
|
|
|
|
DCHECK(!custom_menu_callback_);
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
// Give the client a chance to modify the model.
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefContextMenuHandler> handler = client->GetContextMenuHandler();
|
2015-09-09 16:05:39 +02:00
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefContextMenuParamsImpl> paramsPtr(
|
|
|
|
new CefContextMenuParamsImpl(¶ms_));
|
|
|
|
CefRefPtr<CefFrame> frame = browser_->GetFocusedFrame();
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
handler->OnBeforeContextMenu(browser_, frame, paramsPtr.get(),
|
|
|
|
model_.get());
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
MenuWillShow(model_);
|
|
|
|
|
|
|
|
if (model_->GetCount() > 0) {
|
|
|
|
CefRefPtr<CefRunContextMenuCallbackImpl> callbackImpl(
|
|
|
|
new CefRunContextMenuCallbackImpl(
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefMenuManager::ExecuteCommandCallback,
|
|
|
|
weak_ptr_factory_.GetWeakPtr())));
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
// This reference will be cleared when the callback is executed or
|
|
|
|
// the callback object is deleted.
|
|
|
|
custom_menu_callback_ = callbackImpl.get();
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
if (handler->RunContextMenu(browser_, frame, paramsPtr.get(),
|
|
|
|
model_.get(), callbackImpl.get())) {
|
2015-09-09 16:05:39 +02:00
|
|
|
custom_menu = true;
|
|
|
|
} else {
|
|
|
|
// Callback should not be executed if the handler returns false.
|
|
|
|
DCHECK(custom_menu_callback_);
|
2020-01-15 14:36:24 +01:00
|
|
|
custom_menu_callback_ = nullptr;
|
2015-09-09 16:05:39 +02:00
|
|
|
callbackImpl->Disconnect();
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
// Do not keep references to the parameters in the callback.
|
2022-01-25 20:40:24 +01:00
|
|
|
std::ignore = paramsPtr->Detach(nullptr);
|
2015-09-09 16:05:39 +02:00
|
|
|
DCHECK(paramsPtr->HasOneRef());
|
|
|
|
DCHECK(model_->VerifyRefCount());
|
|
|
|
|
|
|
|
// Menu is empty so notify the client and return.
|
|
|
|
if (model_->GetCount() == 0 && !custom_menu) {
|
|
|
|
MenuClosed(model_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (custom_menu) {
|
2015-09-09 16:05:39 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-05-13 13:38:41 +02:00
|
|
|
|
|
|
|
if (!runner_ || !runner_->RunContextMenu(browser_, model_.get(), params_)) {
|
|
|
|
LOG(ERROR) << "Default context menu implementation is not available; "
|
|
|
|
"canceling the menu";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::CancelContextMenu() {
|
2015-09-09 16:05:39 +02:00
|
|
|
if (IsShowingContextMenu()) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (custom_menu_callback_) {
|
2015-09-09 16:05:39 +02:00
|
|
|
custom_menu_callback_->Cancel();
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (runner_) {
|
2015-09-09 16:05:39 +02:00
|
|
|
runner_->CancelContextMenu();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::ExecuteCommand(CefRefPtr<CefMenuModelImpl> source,
|
2012-04-19 22:31:46 +02:00
|
|
|
int command_id,
|
|
|
|
cef_event_flags_t event_flags) {
|
|
|
|
// Give the client a chance to handle the command.
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefContextMenuHandler> handler = client->GetContextMenuHandler();
|
2015-09-09 16:05:39 +02:00
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefContextMenuParamsImpl> paramsPtr(
|
|
|
|
new CefContextMenuParamsImpl(¶ms_));
|
|
|
|
|
|
|
|
bool handled = handler->OnContextMenuCommand(
|
2017-05-17 11:29:28 +02:00
|
|
|
browser_, browser_->GetFocusedFrame(), paramsPtr.get(), command_id,
|
2015-09-09 16:05:39 +02:00
|
|
|
event_flags);
|
|
|
|
|
|
|
|
// Do not keep references to the parameters in the callback.
|
2022-01-25 20:40:24 +01:00
|
|
|
std::ignore = paramsPtr->Detach(nullptr);
|
2015-09-09 16:05:39 +02:00
|
|
|
DCHECK(paramsPtr->HasOneRef());
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (handled) {
|
2015-09-09 16:05:39 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the default command handling.
|
|
|
|
ExecuteDefaultCommand(command_id);
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::MenuWillShow(CefRefPtr<CefMenuModelImpl> source) {
|
2012-04-19 23:18:22 +02:00
|
|
|
// May be called for sub-menus as well.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (source.get() != model_.get()) {
|
2012-04-19 23:18:22 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-19 23:18:22 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!web_contents()) {
|
2013-02-13 18:11:50 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-02-13 18:11:50 +01:00
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
// May be called multiple times.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsShowingContextMenu()) {
|
2015-09-09 16:05:39 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
// Notify the host before showing the context menu.
|
2017-07-27 01:19:27 +02:00
|
|
|
web_contents()->SetShowingContextMenu(true);
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::MenuClosed(CefRefPtr<CefMenuModelImpl> source) {
|
2012-04-19 23:18:22 +02:00
|
|
|
// May be called for sub-menus as well.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (source.get() != model_.get()) {
|
2012-04-19 23:18:22 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-19 23:18:22 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!web_contents()) {
|
2015-09-09 16:05:39 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
DCHECK(IsShowingContextMenu());
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
// Notify the client.
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefContextMenuHandler> handler = client->GetContextMenuHandler();
|
2015-09-09 16:05:39 +02:00
|
|
|
if (handler.get()) {
|
|
|
|
handler->OnContextMenuDismissed(browser_, browser_->GetFocusedFrame());
|
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
// Notify the host after closing the context menu.
|
2017-07-27 01:19:27 +02:00
|
|
|
web_contents()->SetShowingContextMenu(false);
|
2021-03-04 23:36:57 +01:00
|
|
|
web_contents()->NotifyContextMenuClosed(params_.link_followed);
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
bool CefMenuManager::FormatLabel(CefRefPtr<CefMenuModelImpl> source,
|
2021-04-21 00:52:34 +02:00
|
|
|
std::u16string& label) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!runner_) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-05-23 19:47:07 +02:00
|
|
|
return runner_->FormatLabel(label);
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::ExecuteCommandCallback(int command_id,
|
2015-09-09 16:05:39 +02:00
|
|
|
cef_event_flags_t event_flags) {
|
|
|
|
DCHECK(IsShowingContextMenu());
|
|
|
|
DCHECK(custom_menu_callback_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (command_id != kInvalidCommandId) {
|
2015-09-09 16:05:39 +02:00
|
|
|
ExecuteCommand(model_, command_id, event_flags);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
MenuClosed(model_);
|
2020-01-15 14:36:24 +01:00
|
|
|
custom_menu_callback_ = nullptr;
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::CreateDefaultModel() {
|
2015-09-09 16:05:39 +02:00
|
|
|
if (!params_.custom_items.empty()) {
|
|
|
|
// Custom menu items originating from the renderer process. For example,
|
2020-12-02 23:31:49 +01:00
|
|
|
// plugin placeholder menu items.
|
2021-03-04 23:36:57 +01:00
|
|
|
for (auto& item : params_.custom_items) {
|
|
|
|
auto new_item = item->Clone();
|
|
|
|
new_item->action += MENU_ID_CUSTOM_FIRST;
|
|
|
|
DCHECK_LE(static_cast<int>(new_item->action), MENU_ID_CUSTOM_LAST);
|
|
|
|
model_->AddMenuItem(*new_item);
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
if (params_.is_editable) {
|
|
|
|
// Editable node.
|
2017-02-01 21:24:20 +01:00
|
|
|
model_->AddItem(MENU_ID_UNDO, GetLabel(IDS_CONTENT_CONTEXT_UNDO));
|
|
|
|
model_->AddItem(MENU_ID_REDO, GetLabel(IDS_CONTENT_CONTEXT_REDO));
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2012-08-29 18:58:40 +02:00
|
|
|
model_->AddSeparator();
|
2017-02-01 21:24:20 +01:00
|
|
|
model_->AddItem(MENU_ID_CUT, GetLabel(IDS_CONTENT_CONTEXT_CUT));
|
|
|
|
model_->AddItem(MENU_ID_COPY, GetLabel(IDS_CONTENT_CONTEXT_COPY));
|
|
|
|
model_->AddItem(MENU_ID_PASTE, GetLabel(IDS_CONTENT_CONTEXT_PASTE));
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2012-08-29 18:58:40 +02:00
|
|
|
model_->AddSeparator();
|
2017-02-01 21:24:20 +01:00
|
|
|
model_->AddItem(MENU_ID_SELECT_ALL,
|
|
|
|
GetLabel(IDS_CONTENT_CONTEXT_SELECTALL));
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_UNDO)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_UNDO, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_REDO)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_REDO, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_CUT)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_CUT, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_COPY)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_COPY, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_PASTE)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_PASTE, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_DELETE)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_DELETE, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!(params_.edit_flags & CM_EDITFLAG_CAN_SELECT_ALL)) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_SELECT_ALL, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-10-07 22:44:33 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
if (!params_.misspelled_word.empty()) {
|
2014-10-19 03:47:27 +02:00
|
|
|
// Always add a separator before the list of dictionary suggestions or
|
|
|
|
// "No spelling suggestions".
|
|
|
|
model_->AddSeparator();
|
|
|
|
|
|
|
|
if (!params_.dictionary_suggestions.empty()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
for (size_t i = 0; i < params_.dictionary_suggestions.size() &&
|
|
|
|
MENU_ID_SPELLCHECK_SUGGESTION_0 + i <=
|
|
|
|
MENU_ID_SPELLCHECK_SUGGESTION_LAST;
|
2014-10-19 03:47:27 +02:00
|
|
|
++i) {
|
|
|
|
model_->AddItem(MENU_ID_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i),
|
2021-01-28 00:13:12 +01:00
|
|
|
params_.dictionary_suggestions[i]);
|
2014-10-19 03:47:27 +02:00
|
|
|
}
|
2014-10-07 22:44:33 +02:00
|
|
|
|
2014-10-19 03:47:27 +02:00
|
|
|
// When there are dictionary suggestions add a separator before "Add to
|
|
|
|
// dictionary".
|
|
|
|
model_->AddSeparator();
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
model_->AddItem(MENU_ID_NO_SPELLING_SUGGESTIONS,
|
|
|
|
GetLabel(IDS_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS));
|
2014-10-19 03:47:27 +02:00
|
|
|
model_->SetEnabled(MENU_ID_NO_SPELLING_SUGGESTIONS, false);
|
2014-10-07 22:44:33 +02:00
|
|
|
}
|
2014-10-19 03:47:27 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
model_->AddItem(MENU_ID_ADD_TO_DICTIONARY,
|
|
|
|
GetLabel(IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY));
|
2014-10-07 22:44:33 +02:00
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
} else if (!params_.selection_text.empty()) {
|
|
|
|
// Something is selected.
|
2017-02-01 21:24:20 +01:00
|
|
|
model_->AddItem(MENU_ID_COPY, GetLabel(IDS_CONTENT_CONTEXT_COPY));
|
2012-04-19 22:31:46 +02:00
|
|
|
} else if (!params_.page_url.is_empty() || !params_.frame_url.is_empty()) {
|
|
|
|
// Page or frame.
|
2017-02-01 21:24:20 +01:00
|
|
|
model_->AddItem(MENU_ID_BACK, GetLabel(IDS_CONTENT_CONTEXT_BACK));
|
|
|
|
model_->AddItem(MENU_ID_FORWARD, GetLabel(IDS_CONTENT_CONTEXT_FORWARD));
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2012-08-29 18:58:40 +02:00
|
|
|
model_->AddSeparator();
|
2017-02-01 21:24:20 +01:00
|
|
|
model_->AddItem(MENU_ID_PRINT, GetLabel(IDS_CONTENT_CONTEXT_PRINT));
|
|
|
|
model_->AddItem(MENU_ID_VIEW_SOURCE,
|
|
|
|
GetLabel(IDS_CONTENT_CONTEXT_VIEWPAGESOURCE));
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browser_->CanGoBack()) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_BACK, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!browser_->CanGoForward()) {
|
2012-04-19 22:31:46 +02:00
|
|
|
model_->SetEnabled(MENU_ID_FORWARD, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefMenuManager::ExecuteDefaultCommand(int command_id) {
|
2015-09-09 16:05:39 +02:00
|
|
|
if (IsCustomContextMenuCommand(command_id)) {
|
|
|
|
if (web_contents()) {
|
|
|
|
web_contents()->ExecuteCustomContextMenuCommand(
|
2021-03-04 23:36:57 +01:00
|
|
|
command_id - MENU_ID_CUSTOM_FIRST, params_.link_followed);
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 22:44:33 +02:00
|
|
|
// If the user chose a replacement word for a misspelling, replace it here.
|
|
|
|
if (command_id >= MENU_ID_SPELLCHECK_SUGGESTION_0 &&
|
|
|
|
command_id <= MENU_ID_SPELLCHECK_SUGGESTION_LAST) {
|
|
|
|
const size_t suggestion_index =
|
|
|
|
static_cast<size_t>(command_id) - MENU_ID_SPELLCHECK_SUGGESTION_0;
|
|
|
|
if (suggestion_index < params_.dictionary_suggestions.size()) {
|
|
|
|
browser_->ReplaceMisspelling(
|
|
|
|
params_.dictionary_suggestions[suggestion_index]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
switch (command_id) {
|
2017-05-17 11:29:28 +02:00
|
|
|
// Navigation.
|
|
|
|
case MENU_ID_BACK:
|
|
|
|
browser_->GoBack();
|
|
|
|
break;
|
|
|
|
case MENU_ID_FORWARD:
|
|
|
|
browser_->GoForward();
|
|
|
|
break;
|
|
|
|
case MENU_ID_RELOAD:
|
|
|
|
browser_->Reload();
|
|
|
|
break;
|
|
|
|
case MENU_ID_RELOAD_NOCACHE:
|
|
|
|
browser_->ReloadIgnoreCache();
|
|
|
|
break;
|
|
|
|
case MENU_ID_STOPLOAD:
|
|
|
|
browser_->StopLoad();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Editing.
|
|
|
|
case MENU_ID_UNDO:
|
|
|
|
browser_->GetFocusedFrame()->Undo();
|
|
|
|
break;
|
|
|
|
case MENU_ID_REDO:
|
|
|
|
browser_->GetFocusedFrame()->Redo();
|
|
|
|
break;
|
|
|
|
case MENU_ID_CUT:
|
|
|
|
browser_->GetFocusedFrame()->Cut();
|
|
|
|
break;
|
|
|
|
case MENU_ID_COPY:
|
|
|
|
browser_->GetFocusedFrame()->Copy();
|
|
|
|
break;
|
|
|
|
case MENU_ID_PASTE:
|
|
|
|
browser_->GetFocusedFrame()->Paste();
|
|
|
|
break;
|
|
|
|
case MENU_ID_DELETE:
|
|
|
|
browser_->GetFocusedFrame()->Delete();
|
|
|
|
break;
|
|
|
|
case MENU_ID_SELECT_ALL:
|
|
|
|
browser_->GetFocusedFrame()->SelectAll();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Miscellaneous.
|
|
|
|
case MENU_ID_FIND:
|
|
|
|
// TODO(cef): Implement.
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
break;
|
|
|
|
case MENU_ID_PRINT:
|
|
|
|
browser_->Print();
|
|
|
|
break;
|
|
|
|
case MENU_ID_VIEW_SOURCE:
|
|
|
|
browser_->GetFocusedFrame()->ViewSource();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Spell checking.
|
|
|
|
case MENU_ID_ADD_TO_DICTIONARY:
|
|
|
|
browser_->GetHost()->AddWordToDictionary(params_.misspelled_word);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-04-19 22:31:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
bool CefMenuManager::IsCustomContextMenuCommand(int command_id) {
|
2015-09-09 16:05:39 +02:00
|
|
|
// Verify that the command ID is in the correct range.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (command_id < MENU_ID_CUSTOM_FIRST || command_id > MENU_ID_CUSTOM_LAST) {
|
2015-09-09 16:05:39 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
command_id -= MENU_ID_CUSTOM_FIRST;
|
|
|
|
|
|
|
|
// Verify that the specific command ID was passed from the renderer process.
|
|
|
|
if (!params_.custom_items.empty()) {
|
|
|
|
for (size_t i = 0; i < params_.custom_items.size(); ++i) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (static_cast<int>(params_.custom_items[i]->action) == command_id) {
|
2015-09-09 16:05:39 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|