forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_menu_handler.cpp
More file actions
158 lines (140 loc) · 5.55 KB
/
context_menu_handler.cpp
File metadata and controls
158 lines (140 loc) · 5.55 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2016 CEF Python, see the Authors file.
// All rights reserved. Licensed under BSD 3-clause license.
// Project website: https://github.com/cztomczak/cefpython
#include "context_menu_handler.h"
#include "include/base/cef_logging.h"
#define _MENU_ID_DEVTOOLS MENU_ID_USER_FIRST + 1
#define _MENU_ID_RELOAD_PAGE MENU_ID_USER_FIRST + 2
#define _MENU_ID_OPEN_PAGE_IN_EXTERNAL_BROWSER MENU_ID_USER_FIRST + 3
#define _MENU_ID_OPEN_FRAME_IN_EXTERNAL_BROWSER MENU_ID_USER_FIRST + 4
// Forward declarations
void OpenInExternalBrowser(const std::string& url);
void ContextMenuHandler::OnBeforeContextMenu(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model)
{
bool enabled = ApplicationSettings_GetBoolFromDict(
"context_menu", "enabled");
bool navigation = ApplicationSettings_GetBoolFromDict(
"context_menu", "navigation");
bool print = ApplicationSettings_GetBoolFromDict(
"context_menu", "print");
bool view_source = ApplicationSettings_GetBoolFromDict(
"context_menu", "view_source");
bool external_browser = ApplicationSettings_GetBoolFromDict(
"context_menu", "external_browser");
bool devtools = ApplicationSettings_GetBoolFromDict(
"context_menu", "devtools");
if (!enabled) {
model->Clear();
return;
}
if (!navigation && model->IsVisible(MENU_ID_BACK)
&& model->IsVisible(MENU_ID_FORWARD)) {
model->Remove(MENU_ID_BACK);
model->Remove(MENU_ID_FORWARD);
// Remove separator
if (model->GetTypeAt(0) == MENUITEMTYPE_SEPARATOR) {
model->RemoveAt(0);
}
}
if (!print) {
model->Remove(MENU_ID_PRINT);
}
if (!view_source) {
model->Remove(MENU_ID_VIEW_SOURCE);
}
if (!params->IsEditable() && params->GetSelectionText().empty()
&& (params->GetPageUrl().length()
|| params->GetFrameUrl().length())) {
if (external_browser) {
model->AddItem(_MENU_ID_OPEN_PAGE_IN_EXTERNAL_BROWSER,
"Open in external browser");
if (params->GetFrameUrl().length()
&& params->GetPageUrl() != params->GetFrameUrl()) {
model->AddItem(_MENU_ID_OPEN_FRAME_IN_EXTERNAL_BROWSER,
"Open frame in external browser");
}
}
if (navigation) {
model->InsertItemAt(2, _MENU_ID_RELOAD_PAGE, "Reload");
}
if (devtools) {
model->AddSeparator();
model->AddItem(_MENU_ID_DEVTOOLS, "Developer Tools");
}
}
}
bool ContextMenuHandler::OnContextMenuCommand(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id,
EventFlags event_flags)
{
if (command_id == _MENU_ID_OPEN_PAGE_IN_EXTERNAL_BROWSER) {
#if defined(OS_WIN)
ShellExecuteA(0, "open", params->GetPageUrl().ToString().c_str(),
0, 0, SW_SHOWNORMAL);
#elif defined(OS_LINUX)
OpenInExternalBrowser(params->GetPageUrl().ToString());
#endif // OS_WIN
return true;
} else if (command_id == _MENU_ID_OPEN_FRAME_IN_EXTERNAL_BROWSER) {
#if defined(OS_WIN)
ShellExecuteA(0, "open", params->GetFrameUrl().ToString().c_str(),
0, 0, SW_SHOWNORMAL);
#elif defined(OS_LINUX)
OpenInExternalBrowser(params->GetFrameUrl().ToString());
#endif // OS_WIN
return true;
} else if (command_id == _MENU_ID_RELOAD_PAGE) {
browser->ReloadIgnoreCache();
return true;
} else if (command_id == _MENU_ID_DEVTOOLS) {
PyBrowser_ShowDevTools(browser);
return true;
}
return false;
}
void ContextMenuHandler::OnContextMenuDismissed(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame)
{
// PASS
}
// ----------------------------------------------------------------------------
// Context menu utility functions
// ----------------------------------------------------------------------------
#if defined(OS_LINUX)
void OpenInExternalBrowser(const std::string& url)
{
// Linux equivalent of ShellExecute
if (url.empty()) {
LOG(ERROR) << "[Browser process] OpenInExternalBrowser():"
" url is empty";
return;
}
std::string msg = "[Browser process] OpenInExternalBrowser(): url=";
msg.append(url.c_str());
LOG(INFO) << msg.c_str();
// xdg-open is a desktop-independent tool for running
// default applications. Installed by default on Ubuntu.
// xdg-open process is running in the backround until
// cefpython app closes.
std::string prog = "xdg-open";
// Using system() opens up for bugs and exploits, not
// recommended.
// Fork yourself and run in parallel, do not block the
// current proces.
char *args[3];
args[0] = (char*) prog.c_str();
args[1] = (char*) url.c_str();
args[2] = 0;
pid_t pid = fork();
if (!pid) {
execvp(prog.c_str(), args);
}
}
#endif