Skip to content

Commit 4ba421f

Browse files
committed
Implemented CefCallback.
Implemented the RequestHandler::GetResourceHandler() callback. Implemented ResourceHandler. Implemented the Response object. See updated documentation on the wiki pages. Fixed errors in Python 3, comparison against the unicode type would result in NameError exception.
1 parent d398a63 commit 4ba421f

25 files changed

Lines changed: 693 additions & 18 deletions

cefpython/callback_cef3.pyx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
cdef PyCallback CreatePyCallback(
6+
CefRefPtr[CefCallback] cefCallback):
7+
cdef PyCallback pyCallback = PyCallback()
8+
pyCallback.cefCallback = cefCallback
9+
return pyCallback
10+
11+
cdef class PyCallback:
12+
cdef CefRefPtr[CefCallback] cefCallback
13+
14+
cpdef py_void Continue(self):
15+
self.cefCallback.get().Continue()
16+
17+
cpdef py_void Cancel(self):
18+
self.cefCallback.get().Cancel()

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 cookie_visitor.cpp
14+
SRC = client_handler.cpp cookie_visitor.cpp resource_handler.cpp
1515
OBJ = $(SRC:.cpp=.o)
1616
OUT = libclient_handler.a
1717

cefpython/cef3/client_handler/client_handler_py27.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@
9292
<File
9393
RelativePath=".\cookie_visitor.h"
9494
>
95+
</File>
96+
<File
97+
RelativePath=".\resource_handler.h"
98+
>
9599
</File>
96100
</Filter>
97101
<Filter
@@ -112,6 +116,10 @@
112116
<File
113117
RelativePath=".\cookie_visitor.cpp"
114118
>
119+
</File>
120+
<File
121+
RelativePath=".\resource_handler.cpp"
122+
>
115123
</File>
116124
</Filter>
117125
</Files>

cefpython/cef3/client_handler/client_handler_py32.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
<File
9292
RelativePath=".\cookie_visitor.h"
9393
>
94+
</File>
95+
<File
96+
RelativePath=".\resource_handler.h"
97+
>
9498
</File>
9599
</Filter>
96100
<Filter
@@ -111,6 +115,10 @@
111115
<File
112116
RelativePath=".\cookie_visitor.cpp"
113117
>
118+
</File>
119+
<File
120+
RelativePath=".\resource_handler.cpp"
121+
>
114122
</File>
115123
</Filter>
116124
</Files>

cefpython/cef3/client_handler/cookie_visitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ bool CookieVisitor::Visit(
1212
bool& deleteCookie
1313
) {
1414
REQUIRE_IO_THREAD();
15-
return CookieVisitor_Visit(cookieVisitorId_, cookie, count, total, deleteCookie);
15+
return CookieVisitor_Visit(cookieVisitorId_, cookie, count, total,
16+
deleteCookie);
1617
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 "resource_handler.h"
6+
7+
///
8+
// Begin processing the request. To handle the request return true and call
9+
// CefCallback::Continue() once the response header information is available
10+
// (CefCallback::Continue() can also be called from inside this method if
11+
// header information is available immediately). To cancel the request return
12+
// false.
13+
///
14+
/*--cef()--*/
15+
bool ResourceHandler::ProcessRequest(CefRefPtr<CefRequest> request,
16+
CefRefPtr<CefCallback> callback) {
17+
REQUIRE_IO_THREAD();
18+
return ResourceHandler_ProcessRequest(resourceHandlerId_, request,
19+
callback);
20+
}
21+
22+
///
23+
// Retrieve response header information. If the response length is not known
24+
// set |response_length| to -1 and ReadResponse() will be called until it
25+
// returns false. If the response length is known set |response_length|
26+
// to a positive value and ReadResponse() will be called until it returns
27+
// false or the specified number of bytes have been read. Use the |response|
28+
// object to set the mime type, http status code and other optional header
29+
// values. To redirect the request to a new URL set |redirectUrl| to the new
30+
// URL.
31+
///
32+
/*--cef()--*/
33+
void ResourceHandler::GetResponseHeaders(CefRefPtr<CefResponse> response,
34+
int64& response_length,
35+
CefString& redirectUrl) {
36+
REQUIRE_IO_THREAD();
37+
ResourceHandler_GetResponseHeaders(resourceHandlerId_, response,
38+
response_length, redirectUrl);
39+
}
40+
41+
///
42+
// Read response data. If data is available immediately copy up to
43+
// |bytes_to_read| bytes into |data_out|, set |bytes_read| to the number of
44+
// bytes copied, and return true. To read the data at a later time set
45+
// |bytes_read| to 0, return true and call CefCallback::Continue() when the
46+
// data is available. To indicate response completion return false.
47+
///
48+
/*--cef()--*/
49+
bool ResourceHandler::ReadResponse(void* data_out,
50+
int bytes_to_read,
51+
int& bytes_read,
52+
CefRefPtr<CefCallback> callback) {
53+
REQUIRE_IO_THREAD();
54+
return ResourceHandler_ReadResponse(resourceHandlerId_, data_out,
55+
bytes_to_read, bytes_read, callback);
56+
}
57+
58+
///
59+
// Return true if the specified cookie can be sent with the request or false
60+
// otherwise. If false is returned for any cookie then no cookies will be sent
61+
// with the request.
62+
///
63+
/*--cef()--*/
64+
bool ResourceHandler::CanGetCookie(const CefCookie& cookie) {
65+
REQUIRE_IO_THREAD();
66+
return ResourceHandler_CanGetCookie(resourceHandlerId_, cookie);
67+
}
68+
69+
///
70+
// Return true if the specified cookie returned with the response can be set
71+
// or false otherwise.
72+
///
73+
/*--cef()--*/
74+
bool ResourceHandler::CanSetCookie(const CefCookie& cookie) {
75+
REQUIRE_IO_THREAD();
76+
return ResourceHandler_CanSetCookie(resourceHandlerId_, cookie);
77+
}
78+
79+
///
80+
// Request processing has been canceled.
81+
///
82+
/*--cef()--*/
83+
void ResourceHandler::Cancel() {
84+
REQUIRE_IO_THREAD();
85+
return ResourceHandler_Cancel(resourceHandlerId_);
86+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
#pragma once
6+
7+
#if defined(_WIN32)
8+
#include "../windows/stdint.h"
9+
#endif
10+
11+
#include "cefpython_public_api.h"
12+
13+
class ResourceHandler : public CefResourceHandler
14+
{
15+
public:
16+
int resourceHandlerId_;
17+
public:
18+
ResourceHandler(int resourceHandlerId)
19+
: resourceHandlerId_(resourceHandlerId) {
20+
}
21+
22+
///
23+
// Begin processing the request. To handle the request return true and call
24+
// CefCallback::Continue() once the response header information is available
25+
// (CefCallback::Continue() can also be called from inside this method if
26+
// header information is available immediately). To cancel the request return
27+
// false.
28+
///
29+
/*--cef()--*/
30+
virtual bool ProcessRequest(CefRefPtr<CefRequest> request,
31+
CefRefPtr<CefCallback> callback) OVERRIDE;
32+
33+
///
34+
// Retrieve response header information. If the response length is not known
35+
// set |response_length| to -1 and ReadResponse() will be called until it
36+
// returns false. If the response length is known set |response_length|
37+
// to a positive value and ReadResponse() will be called until it returns
38+
// false or the specified number of bytes have been read. Use the |response|
39+
// object to set the mime type, http status code and other optional header
40+
// values. To redirect the request to a new URL set |redirectUrl| to the new
41+
// URL.
42+
///
43+
/*--cef()--*/
44+
virtual void GetResponseHeaders(CefRefPtr<CefResponse> response,
45+
int64& response_length,
46+
CefString& redirectUrl) OVERRIDE;
47+
48+
///
49+
// Read response data. If data is available immediately copy up to
50+
// |bytes_to_read| bytes into |data_out|, set |bytes_read| to the number of
51+
// bytes copied, and return true. To read the data at a later time set
52+
// |bytes_read| to 0, return true and call CefCallback::Continue() when the
53+
// data is available. To indicate response completion return false.
54+
///
55+
/*--cef()--*/
56+
virtual bool ReadResponse(void* data_out,
57+
int bytes_to_read,
58+
int& bytes_read,
59+
CefRefPtr<CefCallback> callback) OVERRIDE;
60+
61+
///
62+
// Return true if the specified cookie can be sent with the request or false
63+
// otherwise. If false is returned for any cookie then no cookies will be sent
64+
// with the request.
65+
///
66+
/*--cef()--*/
67+
virtual bool CanGetCookie(const CefCookie& cookie) OVERRIDE;
68+
69+
///
70+
// Return true if the specified cookie returned with the response can be set
71+
// or false otherwise.
72+
///
73+
/*--cef()--*/
74+
virtual bool CanSetCookie(const CefCookie& cookie) OVERRIDE;
75+
76+
///
77+
// Request processing has been canceled.
78+
///
79+
/*--cef()--*/
80+
virtual void Cancel() OVERRIDE;
81+
82+
protected:
83+
// Include the default reference counting implementation.
84+
IMPLEMENT_REFCOUNTING(ResourceHandler);
85+
};

cefpython/cef3/linux/setup/cefpython.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ __PYX_EXTERN_C DL_IMPORT(bool) DisplayHandler_OnConsoleMessage(CefRefPtr<CefBrow
2727
__PYX_EXTERN_C DL_IMPORT(bool) KeyboardHandler_OnPreKeyEvent(CefRefPtr<CefBrowser>, CefKeyEvent const &, CefEventHandle, bool *);
2828
__PYX_EXTERN_C DL_IMPORT(bool) KeyboardHandler_OnKeyEvent(CefRefPtr<CefBrowser>, CefKeyEvent const &, CefEventHandle);
2929
__PYX_EXTERN_C DL_IMPORT(bool) RequestHandler_OnBeforeResourceLoad(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest>);
30+
__PYX_EXTERN_C DL_IMPORT(CefRefPtr<CefResourceHandler>) RequestHandler_GetResourceHandler(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest>);
3031
__PYX_EXTERN_C DL_IMPORT(void) RequestHandler_OnResourceRedirect(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefString const &, CefString &);
3132
__PYX_EXTERN_C DL_IMPORT(bool) RequestHandler_GetAuthCredentials(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, bool, CefString const &, int, CefString const &, CefString const &, CefRefPtr<CefAuthCallback>);
3233
__PYX_EXTERN_C DL_IMPORT(bool) RequestHandler_OnQuotaRequest(CefRefPtr<CefBrowser>, CefString const &, int64, CefRefPtr<CefQuotaCallback>);
@@ -51,6 +52,12 @@ __PYX_EXTERN_C DL_IMPORT(void) RenderHandler_OnPopupSize(CefRefPtr<CefBrowser>,
5152
__PYX_EXTERN_C DL_IMPORT(void) RenderHandler_OnPaint(CefRefPtr<CefBrowser>, cef_paint_element_type_t, std::vector<CefRect> &, void const *, int, int);
5253
__PYX_EXTERN_C DL_IMPORT(void) RenderHandler_OnCursorChange(CefRefPtr<CefBrowser>, CefCursorHandle);
5354
__PYX_EXTERN_C DL_IMPORT(void) RenderHandler_OnScrollOffsetChanged(CefRefPtr<CefBrowser>);
55+
__PYX_EXTERN_C DL_IMPORT(bool) ResourceHandler_ProcessRequest(int, CefRefPtr<CefRequest>, CefRefPtr<CefCallback>);
56+
__PYX_EXTERN_C DL_IMPORT(void) ResourceHandler_GetResponseHeaders(int, CefRefPtr<CefResponse>, int64 &, CefString &);
57+
__PYX_EXTERN_C DL_IMPORT(bool) ResourceHandler_ReadResponse(int, void *, int, int &, CefRefPtr<CefCallback>);
58+
__PYX_EXTERN_C DL_IMPORT(bool) ResourceHandler_CanGetCookie(int, CefCookie const &);
59+
__PYX_EXTERN_C DL_IMPORT(bool) ResourceHandler_CanSetCookie(int, CefCookie const &);
60+
__PYX_EXTERN_C DL_IMPORT(void) ResourceHandler_Cancel(int);
5461

5562
#endif /* !__PYX_HAVE_API__cefpython_py27 */
5663

cefpython/cefpython.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ IF CEF_VERSION == 1:
131131
include "stream.pyx"
132132
include "content_filter.pyx"
133133
include "request_handler_cef1.pyx"
134-
include "response.pyx"
134+
include "response_cef1.pyx"
135135
include "display_handler_cef1.pyx"
136136
include "lifespan_handler_cef1.pyx"
137137
IF UNAME_SYSNAME == "Windows":
@@ -166,6 +166,9 @@ IF CEF_VERSION == 3:
166166
include "browser_process_handler_cef3.pyx"
167167
include "paint_buffer_cef3.pyx"
168168
include "render_handler_cef3.pyx"
169+
include "callback_cef3.pyx"
170+
include "resource_handler_cef3.pyx"
171+
include "response_cef3.pyx"
169172

170173
# Try not to run any of the CEF code until Initialize() is called.
171174
# Do not allocate any memory on the heap until Initialize() is called,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
from cef_base cimport CefBase
6+
7+
cdef extern from "include/cef_callback.h":
8+
cdef cppclass CefCallback(CefBase):
9+
void Continue()
10+
void Cancel()

0 commit comments

Comments
 (0)