Skip to content

Commit ecfe4de

Browse files
CzarekCzarek
authored andcommitted
Implemented Request Handler + Request object in CEF Python 3.
Implemented Cookie Manager and Visitor in CEF Python 3.
1 parent 112daba commit ecfe4de

36 files changed

Lines changed: 2091 additions & 83 deletions

cefpython/browser.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ cdef class PyBrowser:
206206
"OnStatusMessage", "OnConsoleMessage"]
207207
# CefKeyboardHandler
208208
self.allowedClientCallbacks += ["OnPreKeyEvent", "OnKeyEvent"];
209+
# CefRequestHandler
210+
# Note: "OnCertificateError" and "OnBeforePluginLoad" must be
211+
# set using cefpython.SetGlobalClientCallback()
212+
self.allowedClientCallbacks += ["OnBeforeResourceLoad",
213+
"OnResourceRedirect", "GetAuthCredentials",
214+
"OnQuotaRequest", "GetCookieManager",
215+
"OnProtocolExecution"]
209216
if name not in self.allowedClientCallbacks:
210217
raise Exception("Browser.SetClientCallback() failed: unknown "
211218
"callback: %s" % name)

cefpython/cef3/client_handler/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
CC = g++
1212
CCFLAGS = -g -fPIC -Wall -Werror
1313

14-
SRC = client_handler.cpp
14+
SRC = client_handler.cpp cookie_visitor.cpp
1515
OBJ = $(SRC:.cpp=.o)
1616
OUT = libclient_handler.a
1717

cefpython/cef3/client_handler/client_handler.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,161 @@ bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser,
339339
return KeyboardHandler_OnKeyEvent(browser, event, os_event);
340340
// Default: return false;
341341
}
342+
343+
// --------------------------------------------------------------------------
344+
// CefRequestHandler
345+
// --------------------------------------------------------------------------
346+
347+
///
348+
// Implement this interface to handle events related to browser requests. The
349+
// methods of this class will be called on the thread indicated.
350+
///
351+
352+
///
353+
// Called on the IO thread before a resource request is loaded. The |request|
354+
// object may be modified. To cancel the request return true otherwise return
355+
// false.
356+
///
357+
/*--cef()--*/
358+
bool ClientHandler::OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
359+
CefRefPtr<CefFrame> frame,
360+
CefRefPtr<CefRequest> request) {
361+
REQUIRE_IO_THREAD();
362+
return RequestHandler_OnBeforeResourceLoad(browser, frame, request);
363+
// Default: return false;
364+
}
365+
366+
///
367+
// Called on the IO thread before a resource is loaded. To allow the resource
368+
// to load normally return NULL. To specify a handler for the resource return
369+
// a CefResourceHandler object. The |request| object should not be modified in
370+
// this callback.
371+
///
372+
/*--cef()--*/
373+
CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(
374+
CefRefPtr<CefBrowser> browser,
375+
CefRefPtr<CefFrame> frame,
376+
CefRefPtr<CefRequest> request) {
377+
REQUIRE_IO_THREAD();
378+
// Not yet implemented.
379+
return NULL;
380+
}
381+
382+
///
383+
// Called on the IO thread when a resource load is redirected. The |old_url|
384+
// parameter will contain the old URL. The |new_url| parameter will contain
385+
// the new URL and can be changed if desired.
386+
///
387+
/*--cef()--*/
388+
void ClientHandler::OnResourceRedirect(CefRefPtr<CefBrowser> browser,
389+
CefRefPtr<CefFrame> frame,
390+
const CefString& old_url,
391+
CefString& new_url) {
392+
REQUIRE_IO_THREAD();
393+
RequestHandler_OnResourceRedirect(browser, frame, old_url, new_url);
394+
}
395+
396+
///
397+
// Called on the IO thread when the browser needs credentials from the user.
398+
// |isProxy| indicates whether the host is a proxy server. |host| contains the
399+
// hostname and |port| contains the port number. Return true to continue the
400+
// request and call CefAuthCallback::Continue() when the authentication
401+
// information is available. Return false to cancel the request.
402+
///
403+
/*--cef(optional_param=realm)--*/
404+
bool ClientHandler::GetAuthCredentials(CefRefPtr<CefBrowser> browser,
405+
CefRefPtr<CefFrame> frame,
406+
bool isProxy,
407+
const CefString& host,
408+
int port,
409+
const CefString& realm,
410+
const CefString& scheme,
411+
CefRefPtr<CefAuthCallback> callback) {
412+
REQUIRE_IO_THREAD();
413+
return RequestHandler_GetAuthCredentials(browser, frame, isProxy, host,
414+
port, realm, scheme, callback);
415+
// Default: return false;
416+
}
417+
418+
///
419+
// Called on the IO thread when JavaScript requests a specific storage quota
420+
// size via the webkitStorageInfo.requestQuota function. |origin_url| is the
421+
// origin of the page making the request. |new_size| is the requested quota
422+
// size in bytes. Return true and call CefQuotaCallback::Continue() either in
423+
// this method or at a later time to grant or deny the request. Return false
424+
// to cancel the request.
425+
///
426+
/*--cef(optional_param=realm)--*/
427+
bool ClientHandler::OnQuotaRequest(CefRefPtr<CefBrowser> browser,
428+
const CefString& origin_url,
429+
int64 new_size,
430+
CefRefPtr<CefQuotaCallback> callback) {
431+
REQUIRE_IO_THREAD();
432+
return RequestHandler_OnQuotaRequest(browser, origin_url, new_size,
433+
callback);
434+
// Default: return false;
435+
}
436+
437+
///
438+
// Called on the IO thread to retrieve the cookie manager. |main_url| is the
439+
// URL of the top-level frame. Cookies managers can be unique per browser or
440+
// shared across multiple browsers. The global cookie manager will be used if
441+
// this method returns NULL.
442+
///
443+
/*--cef()--*/
444+
CefRefPtr<CefCookieManager> ClientHandler::GetCookieManager(
445+
CefRefPtr<CefBrowser> browser,
446+
const CefString& main_url) {
447+
REQUIRE_IO_THREAD();
448+
return RequestHandler_GetCookieManager(browser, main_url);
449+
// Default: return NULL.
450+
}
451+
452+
///
453+
// Called on the UI thread to handle requests for URLs with an unknown
454+
// protocol component. Set |allow_os_execution| to true to attempt execution
455+
// via the registered OS protocol handler, if any.
456+
// SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS BASED
457+
// ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
458+
///
459+
/*--cef()--*/
460+
void ClientHandler::OnProtocolExecution(CefRefPtr<CefBrowser> browser,
461+
const CefString& url,
462+
bool& allow_os_execution) {
463+
REQUIRE_UI_THREAD();
464+
RequestHandler_OnProtocolExecution(browser, url, allow_os_execution);
465+
}
466+
467+
///
468+
// Called on the browser process IO thread before a plugin is loaded. Return
469+
// true to block loading of the plugin.
470+
///
471+
/*--cef(optional_param=url,optional_param=policy_url)--*/
472+
bool ClientHandler::OnBeforePluginLoad(CefRefPtr<CefBrowser> browser,
473+
const CefString& url,
474+
const CefString& policy_url,
475+
CefRefPtr<CefWebPluginInfo> info) {
476+
REQUIRE_IO_THREAD();
477+
return RequestHandler_OnBeforePluginLoad(browser, url, policy_url, info);
478+
// Default: return false;
479+
}
480+
481+
///
482+
// Called on the UI thread to handle requests for URLs with an invalid
483+
// SSL certificate. Return true and call CefAllowCertificateErrorCallback::
484+
// Continue() either in this method or at a later time to continue or cancel
485+
// the request. Return false to cancel the request immediately. If |callback|
486+
// is empty the error cannot be recovered from and the request will be
487+
// canceled automatically. If CefSettings.ignore_certificate_errors is set
488+
// all invalid certificates will be accepted without calling this method.
489+
///
490+
/*--cef()--*/
491+
bool ClientHandler::OnCertificateError(
492+
cef_errorcode_t cert_error,
493+
const CefString& request_url,
494+
CefRefPtr<CefAllowCertificateErrorCallback> callback) {
495+
REQUIRE_UI_THREAD();
496+
return RequestHandler_OnCertificateError(
497+
cert_error, request_url, callback);
498+
// Default: return false;
499+
}

cefpython/cef3/client_handler/client_handler.h

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class ClientHandler :
1414
public CefClient,
1515
public CefLifeSpanHandler,
1616
public CefDisplayHandler,
17-
public CefKeyboardHandler
17+
public CefKeyboardHandler,
18+
public CefRequestHandler
1819
{
1920
public:
2021
ClientHandler(){}
@@ -126,7 +127,7 @@ class ClientHandler :
126127
///
127128
/*--cef()--*/
128129
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE {
129-
return NULL;
130+
return this;
130131
}
131132

132133
///
@@ -354,6 +355,128 @@ class ClientHandler :
354355
const CefKeyEvent& event,
355356
CefEventHandle os_event) OVERRIDE;
356357

358+
// --------------------------------------------------------------------------
359+
// CefRequestHandler
360+
// --------------------------------------------------------------------------
361+
362+
///
363+
// Implement this interface to handle events related to browser requests. The
364+
// methods of this class will be called on the thread indicated.
365+
///
366+
367+
///
368+
// Called on the IO thread before a resource request is loaded. The |request|
369+
// object may be modified. To cancel the request return true otherwise return
370+
// false.
371+
///
372+
/*--cef()--*/
373+
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
374+
CefRefPtr<CefFrame> frame,
375+
CefRefPtr<CefRequest> request) OVERRIDE;
376+
377+
///
378+
// Called on the IO thread before a resource is loaded. To allow the resource
379+
// to load normally return NULL. To specify a handler for the resource return
380+
// a CefResourceHandler object. The |request| object should not be modified in
381+
// this callback.
382+
///
383+
/*--cef()--*/
384+
virtual CefRefPtr<CefResourceHandler> GetResourceHandler(
385+
CefRefPtr<CefBrowser> browser,
386+
CefRefPtr<CefFrame> frame,
387+
CefRefPtr<CefRequest> request) OVERRIDE;
388+
389+
///
390+
// Called on the IO thread when a resource load is redirected. The |old_url|
391+
// parameter will contain the old URL. The |new_url| parameter will contain
392+
// the new URL and can be changed if desired.
393+
///
394+
/*--cef()--*/
395+
virtual void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
396+
CefRefPtr<CefFrame> frame,
397+
const CefString& old_url,
398+
CefString& new_url) OVERRIDE;
399+
400+
///
401+
// Called on the IO thread when the browser needs credentials from the user.
402+
// |isProxy| indicates whether the host is a proxy server. |host| contains the
403+
// hostname and |port| contains the port number. Return true to continue the
404+
// request and call CefAuthCallback::Continue() when the authentication
405+
// information is available. Return false to cancel the request.
406+
///
407+
/*--cef(optional_param=realm)--*/
408+
virtual bool GetAuthCredentials(CefRefPtr<CefBrowser> browser,
409+
CefRefPtr<CefFrame> frame,
410+
bool isProxy,
411+
const CefString& host,
412+
int port,
413+
const CefString& realm,
414+
const CefString& scheme,
415+
CefRefPtr<CefAuthCallback> callback)
416+
OVERRIDE;
417+
418+
///
419+
// Called on the IO thread when JavaScript requests a specific storage quota
420+
// size via the webkitStorageInfo.requestQuota function. |origin_url| is the
421+
// origin of the page making the request. |new_size| is the requested quota
422+
// size in bytes. Return true and call CefQuotaCallback::Continue() either in
423+
// this method or at a later time to grant or deny the request. Return false
424+
// to cancel the request.
425+
///
426+
/*--cef(optional_param=realm)--*/
427+
virtual bool OnQuotaRequest(CefRefPtr<CefBrowser> browser,
428+
const CefString& origin_url,
429+
int64 new_size,
430+
CefRefPtr<CefQuotaCallback> callback) OVERRIDE;
431+
432+
///
433+
// Called on the IO thread to retrieve the cookie manager. |main_url| is the
434+
// URL of the top-level frame. Cookies managers can be unique per browser or
435+
// shared across multiple browsers. The global cookie manager will be used if
436+
// this method returns NULL.
437+
///
438+
/*--cef()--*/
439+
virtual CefRefPtr<CefCookieManager> GetCookieManager(
440+
CefRefPtr<CefBrowser> browser,
441+
const CefString& main_url) OVERRIDE;
442+
443+
///
444+
// Called on the UI thread to handle requests for URLs with an unknown
445+
// protocol component. Set |allow_os_execution| to true to attempt execution
446+
// via the registered OS protocol handler, if any.
447+
// SECURITY WARNING: YOU SHOULD USE THIS METHOD TO ENFORCE RESTRICTIONS BASED
448+
// ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
449+
///
450+
/*--cef()--*/
451+
virtual void OnProtocolExecution(CefRefPtr<CefBrowser> browser,
452+
const CefString& url,
453+
bool& allow_os_execution) OVERRIDE;
454+
455+
///
456+
// Called on the browser process IO thread before a plugin is loaded. Return
457+
// true to block loading of the plugin.
458+
///
459+
/*--cef(optional_param=url,optional_param=policy_url)--*/
460+
virtual bool OnBeforePluginLoad(CefRefPtr<CefBrowser> browser,
461+
const CefString& url,
462+
const CefString& policy_url,
463+
CefRefPtr<CefWebPluginInfo> info) OVERRIDE;
464+
465+
///
466+
// Called on the UI thread to handle requests for URLs with an invalid
467+
// SSL certificate. Return true and call CefAllowCertificateErrorCallback::
468+
// Continue() either in this method or at a later time to continue or cancel
469+
// the request. Return false to cancel the request immediately. If |callback|
470+
// is empty the error cannot be recovered from and the request will be
471+
// canceled automatically. If CefSettings.ignore_certificate_errors is set
472+
// all invalid certificates will be accepted without calling this method.
473+
///
474+
/*--cef()--*/
475+
virtual bool OnCertificateError(
476+
cef_errorcode_t cert_error,
477+
const CefString& request_url,
478+
CefRefPtr<CefAllowCertificateErrorCallback> callback) OVERRIDE;
479+
357480
private:
358481

359482
// Include the default reference counting implementation.

cefpython/cef3/client_handler/client_handler_py27.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@
150150
RelativePath=".\client_handler.h"
151151
>
152152
</File>
153+
<File
154+
RelativePath=".\cookie_visitor.h"
155+
>
156+
</File>
153157
</Filter>
154158
<Filter
155159
Name="Resource Files"
@@ -166,6 +170,10 @@
166170
RelativePath=".\client_handler.cpp"
167171
>
168172
</File>
173+
<File
174+
RelativePath=".\cookie_visitor.cpp"
175+
>
176+
</File>
169177
</Filter>
170178
</Files>
171179
<Globals>

cefpython/cef3/client_handler/client_handler_py32.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@
149149
RelativePath=".\client_handler.h"
150150
>
151151
</File>
152+
<File
153+
RelativePath=".\cookie_visitor.h"
154+
>
155+
</File>
152156
</Filter>
153157
<Filter
154158
Name="Resource Files"
@@ -165,6 +169,10 @@
165169
RelativePath=".\client_handler.cpp"
166170
>
167171
</File>
172+
<File
173+
RelativePath=".\cookie_visitor.cpp"
174+
>
175+
</File>
168176
</Filter>
169177
</Files>
170178
<Globals>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2012-2013 The CEF Python authors. All rights reserved.
2+
// License: New BSD License.
3+
// Website: http://code.google.com/p/cefpython/
4+
5+
#include "cookie_visitor.h"
6+
#include <stdio.h>
7+
8+
bool CookieVisitor::Visit(
9+
const CefCookie& cookie,
10+
int count,
11+
int total,
12+
bool& deleteCookie
13+
) {
14+
REQUIRE_IO_THREAD();
15+
return CookieVisitor_Visit(cookieVisitorId_, cookie, count, total, deleteCookie);
16+
}

0 commit comments

Comments
 (0)