Skip to content

Commit d11b2d2

Browse files
CzarekCzarek
authored andcommitted
Milestone 3 for Cyan Inc. completed.
Added Request class, used in RequestHandler.OnBeforeBrowse() and RequestHandler.OnBeforeResourceLoad(). Added WebRequest class, see an example of use in wxpython.py (in linux binaries only for the moment). Added StreamReader object, for use with OnBeforeResourceLoad(). Added ContentFilter object, for use with OnResourceResponse(). Added Frame.CallFunction(). Fixed memory leaks in JavascriptCallback and PyBrowser classes. The __del__ method does not exist in Cython Extension Types, __dealloc__ must be used instead. Documentation enhancements. New helper functions in string utils. Fixes to make it compile with latest Cython 0.19.1. Added support for debugging in setup/compile.py, read comments at the top of the file.
1 parent 1af1a98 commit d11b2d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1440
-91
lines changed

cefpython/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ ctags
2929

3030
# Inno setup files generated from a template
3131
*.generated
32+
33+
cython_debug/

cefpython/browser.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ cdef class PyBrowser:
113113
cdef public int gwlExStyle
114114
cdef public tuple windowRect
115115

116+
# C-level attributes are initialized to 0 automatically.
116117
cdef void* imageBuffer
117118

118119
cdef CefRefPtr[CefBrowser] GetCefBrowser(self) except *:
@@ -137,7 +138,7 @@ cdef class PyBrowser:
137138
self.allowedClientCallbacks = []
138139
self.userData = {}
139140

140-
def __del__(self):
141+
def __dealloc__(self):
141142
if self.imageBuffer:
142143
free(self.imageBuffer)
143144

cefpython/cef1/client_handler/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC = g++
22
CCFLAGS = -g
33

4-
SRC = client_handler.cpp
4+
SRC = client_handler.cpp web_request_client.cpp content_filter_handler.cpp
55
OBJ = $(SRC:.cpp=.o)
66
OUT = libclient_handler.a
77

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2012-2013 CefPython Authors. All rights reserved.
2+
// License: New BSD License.
3+
// Website: http://code.google.com/p/cefpython/
4+
5+
#include "content_filter_handler.h"
6+
7+
void ContentFilterHandler::ProcessData(const void* data, int data_size,
8+
CefRefPtr<CefStreamReader>& substitute_data) {
9+
REQUIRE_UI_THREAD();
10+
ContentFilterHandler_ProcessData(contentFilterId_, data, data_size,
11+
substitute_data);
12+
}
13+
14+
void ContentFilterHandler::Drain(CefRefPtr<CefStreamReader>& remainder) {
15+
REQUIRE_UI_THREAD();
16+
ContentFilterHandler_Drain(contentFilterId_, remainder);
17+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2012-2013 CefPython Authors. All rights reserved.
2+
// License: New BSD License.
3+
// Website: http://code.google.com/p/cefpython/
4+
5+
#pragma once
6+
7+
// d:\cefpython\src\setup/cefpython.h(22) : warning C4190: 'RequestHandler_GetCookieManager'
8+
// has C-linkage specified, but returns UDT 'CefRefPtr<T>' which is incompatible with C
9+
#if defined(OS_WIN)
10+
#pragma warning(disable:4190)
11+
#endif
12+
13+
// "cef_client.h" will import CefBrowser and others that
14+
// are required when including "cefpython.h", CefWebURLRequest
15+
// is also declared in "cefpython.h".
16+
#include "include/cef_client.h"
17+
#include "include/cef_web_urlrequest.h"
18+
#include "util.h"
19+
20+
// To be able to use 'public' declarations you need to include Python.h and cefpython.h.
21+
#include "Python.h"
22+
23+
// Python 3.2 fix - DL_IMPORT is not defined in Python.h
24+
#ifndef DL_IMPORT /* declarations for DLL import/export */
25+
#define DL_IMPORT(RTYPE) RTYPE
26+
#endif
27+
#ifndef DL_EXPORT /* declarations for DLL import/export */
28+
#define DL_EXPORT(RTYPE) RTYPE
29+
#endif
30+
31+
#if defined(OS_WIN)
32+
#include "windows/setup/cefpython.h"
33+
#endif
34+
35+
#if defined(OS_LINUX)
36+
#include "linux/setup/cefpython.h"
37+
#endif
38+
39+
class ContentFilterHandler : public CefContentFilter
40+
{
41+
public:
42+
int contentFilterId_;
43+
public:
44+
ContentFilterHandler(int contentFilterId) :
45+
contentFilterId_(contentFilterId) {
46+
}
47+
virtual ~ContentFilterHandler(){}
48+
49+
virtual void ProcessData(const void* data, int data_size,
50+
CefRefPtr<CefStreamReader>& substitute_data) OVERRIDE;
51+
52+
virtual void Drain(CefRefPtr<CefStreamReader>& remainder) OVERRIDE;
53+
54+
protected:
55+
56+
// Include the default reference counting implementation.
57+
IMPLEMENT_REFCOUNTING(ContentFilterHandler);
58+
59+
// Include the default locking implementation.
60+
IMPLEMENT_LOCKING(ContentFilterHandler);
61+
62+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2012-2013 CefPython Authors. All rights reserved.
2+
// License: New BSD License.
3+
// Website: http://code.google.com/p/cefpython/
4+
5+
#include "web_request_client.h"
6+
7+
// Cython doesn't know nothing about 'const' so we need to remove it,
8+
// otherwise you get compile error.
9+
10+
void WebRequestClient::OnStateChange(CefRefPtr<CefWebURLRequest> requester,
11+
RequestState state) {
12+
REQUIRE_UI_THREAD();
13+
WebRequestClient_OnStateChange(webRequestId_, requester, state);
14+
}
15+
16+
void WebRequestClient::OnRedirect(CefRefPtr<CefWebURLRequest> requester,
17+
CefRefPtr<CefRequest> request,
18+
CefRefPtr<CefResponse> response) {
19+
REQUIRE_UI_THREAD();
20+
WebRequestClient_OnRedirect(webRequestId_, requester, request, response);
21+
}
22+
23+
void WebRequestClient::OnHeadersReceived(CefRefPtr<CefWebURLRequest> requester,
24+
CefRefPtr<CefResponse> response) {
25+
REQUIRE_UI_THREAD();
26+
WebRequestClient_OnHeadersReceived(webRequestId_, requester, response);
27+
}
28+
29+
void WebRequestClient::OnProgress(CefRefPtr<CefWebURLRequest> requester,
30+
uint64 bytesSent, uint64 totalBytesToBeSent) {
31+
REQUIRE_UI_THREAD();
32+
WebRequestClient_OnProgress(webRequestId_, requester, bytesSent,
33+
totalBytesToBeSent);
34+
}
35+
36+
void WebRequestClient::OnData(CefRefPtr<CefWebURLRequest> requester,
37+
const void* data, int dataLength) {
38+
REQUIRE_UI_THREAD();
39+
WebRequestClient_OnData(webRequestId_, requester, const_cast<void*>(data),
40+
dataLength);
41+
}
42+
43+
void WebRequestClient::OnError(CefRefPtr<CefWebURLRequest> requester,
44+
ErrorCode errorCode) {
45+
REQUIRE_UI_THREAD();
46+
WebRequestClient_OnError(webRequestId_, requester, errorCode);
47+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2012-2013 CefPython Authors. All rights reserved.
2+
// License: New BSD License.
3+
// Website: http://code.google.com/p/cefpython/
4+
5+
#pragma once
6+
7+
// d:\cefpython\src\setup/cefpython.h(22) : warning C4190: 'RequestHandler_GetCookieManager'
8+
// has C-linkage specified, but returns UDT 'CefRefPtr<T>' which is incompatible with C
9+
#if defined(OS_WIN)
10+
#pragma warning(disable:4190)
11+
#endif
12+
13+
// "cef_client.h" will import CefBrowser and others that
14+
// are required when including "cefpython.h"
15+
#include "include/cef_client.h"
16+
#include "include/cef_web_urlrequest.h"
17+
#include "util.h"
18+
19+
// To be able to use 'public' declarations you need to include Python.h and cefpython.h.
20+
#include "Python.h"
21+
22+
// Python 3.2 fix - DL_IMPORT is not defined in Python.h
23+
#ifndef DL_IMPORT /* declarations for DLL import/export */
24+
#define DL_IMPORT(RTYPE) RTYPE
25+
#endif
26+
#ifndef DL_EXPORT /* declarations for DLL import/export */
27+
#define DL_EXPORT(RTYPE) RTYPE
28+
#endif
29+
30+
#if defined(OS_WIN)
31+
#include "windows/setup/cefpython.h"
32+
#endif
33+
34+
#if defined(OS_LINUX)
35+
#include "linux/setup/cefpython.h"
36+
#endif
37+
38+
class WebRequestClient : public CefWebURLRequestClient
39+
{
40+
public:
41+
int webRequestId_;
42+
public:
43+
WebRequestClient(int webRequestId) :
44+
webRequestId_(webRequestId) {
45+
}
46+
virtual ~WebRequestClient(){}
47+
48+
virtual void OnStateChange(CefRefPtr<CefWebURLRequest> requester,
49+
RequestState state) OVERRIDE;
50+
51+
virtual void OnRedirect(CefRefPtr<CefWebURLRequest> requester,
52+
CefRefPtr<CefRequest> request,
53+
CefRefPtr<CefResponse> response) OVERRIDE;
54+
55+
virtual void OnHeadersReceived(CefRefPtr<CefWebURLRequest> requester,
56+
CefRefPtr<CefResponse> response) OVERRIDE;
57+
58+
virtual void OnProgress(CefRefPtr<CefWebURLRequest> requester,
59+
uint64 bytesSent, uint64 totalBytesToBeSent) OVERRIDE;
60+
61+
virtual void OnData(CefRefPtr<CefWebURLRequest> requester,
62+
const void* data, int dataLength) OVERRIDE;
63+
64+
virtual void OnError(CefRefPtr<CefWebURLRequest> requester,
65+
ErrorCode errorCode) OVERRIDE;
66+
protected:
67+
68+
// Include the default reference counting implementation.
69+
IMPLEMENT_REFCOUNTING(WebRequestClient);
70+
71+
// Include the default locking implementation.
72+
IMPLEMENT_LOCKING(WebRequestClient);
73+
74+
};
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset=utf-8>
5+
<title>CefSimple (utf-8: ąś)</title>
6+
<style>
7+
body { font: 13px Segoe UI, Arial; line-height: 1.4em; }
8+
pre { background: #ddd; font: 12px Consolas, Courier New; }
9+
</style>
10+
</head>
11+
<body>
12+
13+
Use mouse context menu to go Back/Forward in history navigation.
14+
15+
<h3>Google Search</h3>
16+
<a href="http://www.google.com/">http://www.google.com/</a>
17+
18+
<h3>User agent</h3>
19+
<script>document.write(navigator.userAgent)</script>
20+
21+
<h3>Popup</h3>
22+
<a href="javascript:window.open('cefsimple.html')">
23+
window.open('cefsimple.html')</a>
24+
25+
<h3>RequestHandler tests</h3>
26+
27+
<p>See messages in the console.</p>
28+
29+
OnBeforeBrowse() - navigate to see message:
30+
<a href="wxpython.html">wxpython.html</a>
31+
<br>
32+
33+
OnBeforeBrowse() / OnBeforeResourceLoad() - test request.GetPostData()
34+
and request.SetPostData():<br>
35+
submit a form:
36+
<a href="https://accounts.google.com/ServiceLogin">
37+
https://accounts.google.com/ServiceLogin</a>,
38+
upload a file:
39+
<a href="http://encodable.com/uploaddemo/">
40+
http://encodable.com/uploaddemo/</a>
41+
<br>
42+
43+
OnBeforeResourceLoad() - try loading nonexistent css file locally:
44+
<a href="javascript:LoadCssFile('nonexistent.css')">
45+
LoadCssFile('nonexistent.css')</a>
46+
<script>
47+
function LoadCssFile(url) {
48+
var cssFile=document.createElement("link")
49+
cssFile.setAttribute("rel", "stylesheet")
50+
cssFile.setAttribute("type", "text/css")
51+
cssFile.setAttribute("href", url)
52+
document.getElementsByTagName("head")[0].appendChild(cssFile)
53+
}
54+
</script>
55+
<br>
56+
57+
OnBeforeResourceLoad() - try replacing a resource on the fly:
58+
<a href="javascript:LoadCssFile('replace-on-the-fly.css')">
59+
LoadCssFile('replace-on-the-fly.css')</a><br>
60+
(css content: body { color: red; })<br>
61+
62+
OnResourceRedirect() - try loading this url:
63+
<a href="http://tinyurl.com/google404redirect">
64+
http://tinyurl.com/google404redirect</a>
65+
<br>
66+
67+
OnResourceResponse() - try loading nonexistent css file via http:
68+
<a href="javascript:LoadCssFile('http://www.google.com/404.css')">
69+
LoadCssFile('http://www.google.com/404.css')</a>
70+
<br>
71+
72+
OnResourceResponse() - try filtering content of the resource:
73+
<a href="javascript:LoadCssFile('content-filter/replace-on-the-fly.css')">
74+
LoadCssFile('content-filter/replace-on-the-fly.css')</a><br>
75+
(new css content: body { color: green; } and body h3 { color: orange; } )
76+
<br>
77+
78+
<h3>WebRequest test</h3>
79+
80+
See the console for messages.<br>
81+
82+
<a href="javascript:external.WebRequest('http://www.sitepoint.com/robots.txt')">
83+
external.WebRequest('http://www.sitepoint.com/robots.txt')</a>
84+
85+
<h3>Frame.CallFunction() test</h3>
86+
87+
<script>
88+
function MyFunction(param1, param2, param3, param4) {
89+
alert("MyFunction() called\nparam1="+JSON.stringify(param1)+"\nparam2="+JSON.stringify(param2)+"\nparam3="+JSON.stringify(param3)+"\nparam4="+JSON.stringify(param4))
90+
}
91+
</script>
92+
<a href="javascript:external.DoCallFunction()">
93+
external.DoCallFunction()</a>
94+
95+
<br><br><br><br><br><br><br><br><br><br><br>
96+
<br><br><br><br><br><br><br><br><br><br><br>
97+
<br><br><br><br><br><br><br><br><br><br><br>
98+
99+
</body>
100+
</html>

0 commit comments

Comments
 (0)