|
8 | 8 | #include "cefpython_public_api.h" |
9 | 9 | #include "DebugLog.h" |
10 | 10 |
|
| 11 | +#if defined(OS_WIN) |
| 12 | +#include <Shellapi.h> |
| 13 | +#pragma comment(lib, "Shell32.lib") |
| 14 | +#elif defined(OS_LINUX) |
| 15 | +#include <unistd.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +// ---------------------------------------------------------------------------- |
| 20 | +// Linux equivalent of ShellExecute |
| 21 | +// ---------------------------------------------------------------------------- |
| 22 | + |
| 23 | +#if defined(OS_LINUX) |
| 24 | +void OpenInExternalBrowser(const std::string& url) |
| 25 | +{ |
| 26 | + if (url.empty()) { |
| 27 | + DebugLog("Browser: OpenInExternalBrowser() FAILED: url is empty"); |
| 28 | + return; |
| 29 | + } |
| 30 | + std::string msg = "Browser: OpenInExternalBrowser(): url="; |
| 31 | + msg.append(url.c_str()); |
| 32 | + DebugLog(msg.c_str()); |
| 33 | + |
| 34 | + // xdg-open is a desktop-independent tool for running |
| 35 | + // default applications. Installed by default on Ubuntu. |
| 36 | + // xdg-open process is running in the backround until |
| 37 | + // cefpython app closes. |
| 38 | + std::string prog = "xdg-open"; |
| 39 | + |
| 40 | + // Using system() opens up for bugs and exploits, not |
| 41 | + // recommended. |
| 42 | + |
| 43 | + // Fork yourself and run in parallel, do not block the |
| 44 | + // current proces. |
| 45 | + char *args[3]; |
| 46 | + args[0] = (char*) prog.c_str(); |
| 47 | + args[1] = (char*) url.c_str(); |
| 48 | + args[2] = 0; |
| 49 | + pid_t pid = fork(); |
| 50 | + if (!pid) { |
| 51 | + execvp(prog.c_str(), args); |
| 52 | + } |
| 53 | +} |
| 54 | +#endif |
| 55 | + |
11 | 56 | // ---------------------------------------------------------------------------- |
12 | 57 | // CefClient |
13 | 58 | // ---------------------------------------------------------------------------- |
@@ -832,3 +877,126 @@ void ClientHandler::OnDownloadUpdated( |
832 | 877 | DebugLog("Browser: Download was cancelled"); |
833 | 878 | } |
834 | 879 | } |
| 880 | + |
| 881 | +// ---------------------------------------------------------------------------- |
| 882 | +// CefContextMenuHandler |
| 883 | +// ---------------------------------------------------------------------------- |
| 884 | + |
| 885 | +#define _MENU_ID_DEVTOOLS MENU_ID_USER_FIRST + 1 |
| 886 | +#define _MENU_ID_RELOAD_PAGE MENU_ID_USER_FIRST + 2 |
| 887 | +#define _MENU_ID_OPEN_PAGE_IN_EXTERNAL_BROWSER MENU_ID_USER_FIRST + 3 |
| 888 | +#define _MENU_ID_OPEN_FRAME_IN_EXTERNAL_BROWSER MENU_ID_USER_FIRST + 4 |
| 889 | + |
| 890 | +/// |
| 891 | +// Called before a context menu is displayed. |params| provides information |
| 892 | +// about the context menu state. |model| initially contains the default |
| 893 | +// context menu. The |model| can be cleared to show no context menu or |
| 894 | +// modified to show a custom menu. Do not keep references to |params| or |
| 895 | +// |model| outside of this callback. |
| 896 | +/// |
| 897 | +/*--cef()--*/ |
| 898 | +void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, |
| 899 | + CefRefPtr<CefFrame> frame, |
| 900 | + CefRefPtr<CefContextMenuParams> params, |
| 901 | + CefRefPtr<CefMenuModel> model) { |
| 902 | + bool enabled = ApplicationSettings_GetBoolFromDict(\ |
| 903 | + "context_menu", "enabled"); |
| 904 | + bool navigation = ApplicationSettings_GetBoolFromDict(\ |
| 905 | + "context_menu", "navigation"); |
| 906 | + bool print = ApplicationSettings_GetBoolFromDict(\ |
| 907 | + "context_menu", "print"); |
| 908 | + bool view_source = ApplicationSettings_GetBoolFromDict(\ |
| 909 | + "context_menu", "view_source"); |
| 910 | + bool external_browser = ApplicationSettings_GetBoolFromDict(\ |
| 911 | + "context_menu", "external_browser"); |
| 912 | + bool devtools = ApplicationSettings_GetBoolFromDict(\ |
| 913 | + "context_menu", "devtools"); |
| 914 | + |
| 915 | + if (!enabled) { |
| 916 | + model->Clear(); |
| 917 | + return; |
| 918 | + } |
| 919 | + if (!navigation) { |
| 920 | + model->Remove(MENU_ID_BACK); |
| 921 | + model->Remove(MENU_ID_FORWARD); |
| 922 | + // Remove separator |
| 923 | + model->RemoveAt(0); |
| 924 | + } |
| 925 | + if (!print) { |
| 926 | + model->Remove(MENU_ID_PRINT); |
| 927 | + } |
| 928 | + if (!view_source) { |
| 929 | + model->Remove(MENU_ID_VIEW_SOURCE); |
| 930 | + } |
| 931 | + if (!params->IsEditable() && params->GetSelectionText().empty() |
| 932 | + && (params->GetPageUrl().length() |
| 933 | + || params->GetFrameUrl().length())) { |
| 934 | + if (external_browser) { |
| 935 | + model->AddItem(_MENU_ID_OPEN_PAGE_IN_EXTERNAL_BROWSER, |
| 936 | + "Open in external browser"); |
| 937 | + if (params->GetFrameUrl().length() |
| 938 | + && params->GetPageUrl() != params->GetFrameUrl()) { |
| 939 | + model->AddItem(_MENU_ID_OPEN_FRAME_IN_EXTERNAL_BROWSER, |
| 940 | + "Open frame in external browser"); |
| 941 | + } |
| 942 | + } |
| 943 | + if (navigation) { |
| 944 | + model->InsertItemAt(2, _MENU_ID_RELOAD_PAGE, "Reload"); |
| 945 | + } |
| 946 | + if (devtools) { |
| 947 | + model->AddSeparator(); |
| 948 | + model->AddItem(_MENU_ID_DEVTOOLS, "Developer Tools"); |
| 949 | + } |
| 950 | + } |
| 951 | +} |
| 952 | + |
| 953 | +/// |
| 954 | +// Called to execute a command selected from the context menu. Return true if |
| 955 | +// the command was handled or false for the default implementation. See |
| 956 | +// cef_menu_id_t for the command ids that have default implementations. All |
| 957 | +// user-defined command ids should be between MENU_ID_USER_FIRST and |
| 958 | +// MENU_ID_USER_LAST. |params| will have the same values as what was passed to |
| 959 | +// OnBeforeContextMenu(). Do not keep a reference to |params| outside of this |
| 960 | +// callback. |
| 961 | +/// |
| 962 | +/*--cef()--*/ |
| 963 | +bool ClientHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser, |
| 964 | + CefRefPtr<CefFrame> frame, |
| 965 | + CefRefPtr<CefContextMenuParams> params, |
| 966 | + int command_id, |
| 967 | + EventFlags event_flags) { |
| 968 | + if (command_id == _MENU_ID_OPEN_PAGE_IN_EXTERNAL_BROWSER) { |
| 969 | +#if defined(OS_WIN) |
| 970 | + ShellExecute(0, L"open", params->GetPageUrl().ToWString().c_str(), |
| 971 | + 0, 0, SW_SHOWNORMAL); |
| 972 | +#elif defined(OS_LINUX) |
| 973 | + OpenInExternalBrowser(params->GetPageUrl().ToString()); |
| 974 | +#endif |
| 975 | + return true; |
| 976 | + } else if (command_id == _MENU_ID_OPEN_FRAME_IN_EXTERNAL_BROWSER) { |
| 977 | +#if defined(OS_WIN) |
| 978 | + ShellExecute(0, L"open", params->GetFrameUrl().ToWString().c_str(), |
| 979 | + 0, 0, SW_SHOWNORMAL); |
| 980 | +#elif defined(OS_LINUX) |
| 981 | + OpenInExternalBrowser(params->GetFrameUrl().ToString()); |
| 982 | +#endif |
| 983 | + return true; |
| 984 | + } else if (command_id == _MENU_ID_RELOAD_PAGE) { |
| 985 | + browser->ReloadIgnoreCache(); |
| 986 | + return true; |
| 987 | + } else if (command_id == _MENU_ID_DEVTOOLS) { |
| 988 | + PyBrowser_ShowDevTools(browser); |
| 989 | + return true; |
| 990 | + } |
| 991 | + return false; |
| 992 | +} |
| 993 | + |
| 994 | +/// |
| 995 | +// Called when the context menu is dismissed irregardless of whether the menu |
| 996 | +// was empty or a command was selected. |
| 997 | +/// |
| 998 | +/*--cef()--*/ |
| 999 | +void ClientHandler::OnContextMenuDismissed(CefRefPtr<CefBrowser> browser, |
| 1000 | + CefRefPtr<CefFrame> frame) { |
| 1001 | +} |
| 1002 | + |
0 commit comments