Right click->View source

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Right click->View source

Postby lkitrossky » Tue May 12, 2015 1:39 am

Hi! We are developing an application based on CEF/Chromium with a stress on security, more strict than usual browser. We found that the right click on the frame opens a context menu with an option View source, and HTML/JavaScript can be really viewed. Can it be prevented somehow? So, right click will not show this menu item and the source cannot be seen at least so easily. Thanks, Levi
lkitrossky
Mentor
 
Posts: 66
Joined: Tue Mar 24, 2015 7:26 am

Re: Right click->View source

Postby magreenblatt » Tue May 12, 2015 2:44 pm

You can change what is displayed in the context menu by implementing CefContextMenuHandler::OnBeforeContextMenu and modifying the |model| object.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Right click->View source

Postby lkitrossky » Mon Jun 08, 2015 7:56 am

Hi! I still have some troubles with it. Our application inherits:
Code: Select all
class MyApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler

In order to use
Code: Select all
CefContextMenuHandler::OnBeforeContextMenu
I added
Code: Select all
class MyApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler, public CefContextMenuHandler

In header file I added:
Code: Select all
virtual bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    CefRefPtr<CefContextMenuParams> params,
                                    int command_id,
                                    EventFlags event_flags) OVERRIDE;
virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
                                   CefRefPtr<CefFrame> frame,
                                   CefRefPtr<CefContextMenuParams> params,
                                   CefRefPtr<CefMenuModel> model) OVERRIDE;

And in source file implementations:
Code: Select all
bool MyApp::OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    CefRefPtr<CefContextMenuParams> params,
                                    int command_id,
                                    EventFlags event_flags)
{
   if(command_id == MENU_ID_VIEW_SOURCE)
      return true;
   return false;
}
void MyApp::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
                                   CefRefPtr<CefFrame> frame,
                                   CefRefPtr<CefContextMenuParams> params,
                                   CefRefPtr<CefMenuModel> model) 
{
   model->Remove(MENU_ID_VIEW_SOURCE); //tried also Clear() and RemoveAt(3)
   return;
}

Still "View source" appears on right click and enables to see the source.
I tried also to added
Code: Select all
virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() OVERRIDE;

but compiler says correctly that this method is not in base classes, it is in CefClient that we do not use.
We found a brutal method: go to chromium\src\cef\libcef\browser\menu_creator.cc and comment out there:
Code: Select all
model_->AddItem(MENU_ID_VIEW_SOURCE, GetLabel(IDS_MENU_VIEW_SOURCE));

This way it works but the method used is clumsy.
What is a correct recommended way to do it and why
Code: Select all
 CefContextMenuHandler::OnBeforeContextMenu
did not work? What was wrong?
Thanks, Levi
lkitrossky
Mentor
 
Posts: 66
Joined: Tue Mar 24, 2015 7:26 am

Re: Right click->View source

Postby magreenblatt » Mon Jun 08, 2015 9:16 am

CefContextMenuHandler is retrieved via CefClient::GetContextMenuHandler().
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Right click->View source

Postby lkitrossky » Mon Jun 08, 2015 9:19 am

But our main class is not derived from CefClient:
Code: Select all
class MyApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler

So, is there some other way to do it still?
lkitrossky
Mentor
 
Posts: 66
Joined: Tue Mar 24, 2015 7:26 am

Re: Right click->View source

Postby magreenblatt » Mon Jun 08, 2015 10:03 am

lkitrossky wrote:But our main class is not derived from CefClient. So, is there some other way to do it still?

Not without changing CEF.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Right click->View source

Postby OPstriker » Tue Nov 22, 2022 4:21 pm

Super Old Reply But, The Default Model Options can be cleared by Calling
Code: Select all
void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
                                        CefRefPtr<CefFrame> frame,
                                        CefRefPtr<CefContextMenuParams> params,
                                        CefRefPtr<CefMenuModel> model) {
  CEF_REQUIRE_UI_THREAD();

  model->Clear();

...


Then Add all your model options under.
OPstriker
Newbie
 
Posts: 2
Joined: Fri Sep 30, 2022 7:31 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 35 guests