1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
content / shell / browser / shell_web_contents_view_delegate_views.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/web_contents.h"
#include "content/shell/browser/shell_devtools_frontend.h"
#include "content/shell/browser/shell_web_contents_view_delegate.h"
#include "content/shell/common/shell_switches.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/window.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"
namespace content {
namespace {
// Model for the "Debug" menu
class ContextMenuModel : public ui::SimpleMenuModel,
public ui::SimpleMenuModel::Delegate {
public:
ContextMenuModel(WebContents* web_contents, const ContextMenuParams& params)
: ui::SimpleMenuModel(this),
web_contents_(web_contents),
params_(params) {
AddItem(COMMAND_OPEN_DEVTOOLS, u"Inspect Element");
}
ContextMenuModel(const ContextMenuModel&) = delete;
ContextMenuModel& operator=(const ContextMenuModel&) = delete;
~ContextMenuModel() override {}
// ui::SimpleMenuModel::Delegate:
bool IsCommandIdChecked(int command_id) const override { return false; }
bool IsCommandIdEnabled(int command_id) const override { return true; }
void ExecuteCommand(int command_id, int event_flags) override {
switch (command_id) {
case COMMAND_OPEN_DEVTOOLS:
ShellDevToolsFrontend* devtools_frontend =
ShellDevToolsFrontend::Show(web_contents_);
devtools_frontend->Activate();
devtools_frontend->InspectElementAt(params_.x, params_.y);
break;
};
}
private:
enum CommandID { COMMAND_OPEN_DEVTOOLS };
raw_ptr<WebContents> web_contents_;
ContextMenuParams params_;
};
} // namespace
std::unique_ptr<WebContentsViewDelegate> CreateShellWebContentsViewDelegate(
WebContents* web_contents) {
return std::make_unique<ShellWebContentsViewDelegate>(web_contents);
}
ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
WebContents* web_contents)
: web_contents_(web_contents) {}
ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {}
void ShellWebContentsViewDelegate::ShowContextMenu(
RenderFrameHost& render_frame_host,
const ContextMenuParams& params) {
if (switches::IsRunWebTestsSwitchPresent())
return;
gfx::Point screen_point(params.x, params.y);
// Convert from content coordinates to window coordinates.
// This code copied from chrome_web_contents_view_delegate_views.cc
aura::Window* web_contents_window = web_contents_->GetNativeView();
aura::Window* root_window = web_contents_window->GetRootWindow();
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(root_window);
if (screen_position_client) {
screen_position_client->ConvertPointToScreen(web_contents_window,
&screen_point);
}
context_menu_model_ =
std::make_unique<ContextMenuModel>(web_contents_, params);
context_menu_runner_ = std::make_unique<views::MenuRunner>(
context_menu_model_.get(), views::MenuRunner::CONTEXT_MENU);
views::Widget* widget = views::Widget::GetWidgetForNativeView(
web_contents_->GetTopLevelNativeWindow());
context_menu_runner_->RunMenuAt(
widget, nullptr, gfx::Rect(screen_point, gfx::Size()),
views::MenuAnchorPosition::kTopRight, ui::mojom::MenuSourceType::kNone);
}
} // namespace content