Skip to content

Commit e0c0bb8

Browse files
CzarekCzarek
authored andcommitted
Added Cookie class. See the Cookie wiki page.
1 parent d11b2d2 commit e0c0bb8

7 files changed

Lines changed: 258 additions & 4 deletions

File tree

cefpython/cefpython.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ include "utils.pyx"
5858
include "string_utils.pyx"
5959
IF UNAME_SYSNAME == "Windows":
6060
include "string_utils_win.pyx"
61+
include "time_utils.pyx"
6162

6263
include "window_info.pyx"
6364
include "browser.pyx"
6465
include "frame.pyx"
6566

67+
IF CEF_VERSION == 1:
68+
include "cookie.pyx"
69+
6670
include "settings.pyx"
6771
IF UNAME_SYSNAME == "Windows":
6872
# Off-screen rendering currently supported only on Windows

cefpython/cookie.pyx

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
cdef Cookie cookie = Cookie()
6+
cookie.SetName("asd1")
7+
print("cookie.cefCookie: %s" % cookie.cefCookie)
8+
print("cookie.GetName(): %s" % cookie.GetName())
9+
print("cookie.GetCreation(): %s" % cookie.GetCreation())
10+
cookie.SetCreation(datetime.datetime(2013,5,23))
11+
print("cookie.GetCreation(): %s" % cookie.GetCreation())
12+
print("cookie: %s" % cookie.Get())
13+
14+
# ------------------------------------------------------------------------------
15+
# Cookie
16+
# ------------------------------------------------------------------------------
17+
18+
cdef class Cookie:
19+
cdef CefCookie cefCookie
20+
21+
cpdef py_void Set(self, dict cookie):
22+
for key in cookie:
23+
if key == "name":
24+
self.SetName(cookie[key])
25+
elif key == "value":
26+
self.SetValue(cookie[key])
27+
elif key == "domain":
28+
self.SetDomain(cookie[key])
29+
elif key == "path":
30+
self.SetPath(cookie[key])
31+
elif key == "secure":
32+
self.SetSecure(cookie[key])
33+
elif key == "httpOnly":
34+
self.SetHttpOnly(cookie[key])
35+
elif key == "creation":
36+
self.SetCreation(cookie[key])
37+
elif key == "lastAccess":
38+
self.SetLastAccess(cookie[key])
39+
elif key == "hasExpires":
40+
self.SetHasExpires(cookie[key])
41+
elif key == "expires":
42+
self.SetExpires(cookie[key])
43+
else:
44+
raise Exception("Invalid key: %s" % key)
45+
46+
cpdef dict Get(self):
47+
return {
48+
"name": self.GetName(),
49+
"value": self.GetValue(),
50+
"domain": self.GetDomain(),
51+
"path": self.GetPath(),
52+
"secure": self.GetSecure(),
53+
"httpOnly": self.GetHttpOnly(),
54+
"creation": self.GetCreation(),
55+
"lastAccess": self.GetLastAccess(),
56+
"hasExpires": self.GetHasExpires(),
57+
"expires": self.GetExpires(),
58+
}
59+
60+
cpdef py_void SetName(self, py_string name):
61+
# This works:
62+
# | CefString(&self.cefCookie.name).FromString(name)
63+
# This does not work (strange!):
64+
# | cdef CefString cefString = CefString(&self.cefCookie.name)
65+
# | PyToCefString(name, cefString)
66+
# But this works:
67+
# | cdef CefString* cefString = new CefString(&self.cefCookie.name)
68+
# | PyToCefStringPointer(name, cefString)
69+
# | del cefString
70+
CefString(&self.cefCookie.name).FromString(name)
71+
72+
cpdef str GetName(self):
73+
cdef CefString cefString = CefString(&self.cefCookie.name)
74+
return CefToPyString(cefString)
75+
76+
cpdef py_void SetValue(self, py_string value):
77+
CefString(&self.cefCookie.value).FromString(value)
78+
79+
cpdef str GetValue(self):
80+
cdef CefString cefString = CefString(&self.cefCookie.value)
81+
return CefToPyString(cefString)
82+
83+
cpdef py_void SetDomain(self, py_string domain):
84+
CefString(&self.cefCookie.domain).FromString(domain)
85+
86+
cpdef str GetDomain(self):
87+
cdef CefString cefString = CefString(&self.cefCookie.domain)
88+
return CefToPyString(cefString)
89+
90+
cpdef py_void SetPath(self, py_string path):
91+
CefString(&self.cefCookie.path).FromString(path)
92+
93+
cpdef str GetPath(self):
94+
cdef CefString cefString = CefString(&self.cefCookie.path)
95+
return CefToPyString(cefString)
96+
97+
cpdef py_void SetSecure(self, py_bool secure):
98+
self.cefCookie.secure = secure
99+
100+
cpdef py_bool GetSecure(self):
101+
return self.cefCookie.secure
102+
103+
cpdef py_void SetHttpOnly(self, py_bool httpOnly):
104+
self.cefCookie.httponly = httpOnly
105+
106+
cpdef py_bool GetHttpOnly(self):
107+
return self.cefCookie.httponly
108+
109+
cpdef py_void SetCreation(self, object creation):
110+
DatetimeToCefTimeT(creation, self.cefCookie.creation)
111+
112+
cpdef object GetCreation(self):
113+
return CefTimeTToDatetime(self.cefCookie.creation)
114+
115+
cpdef py_void SetLastAccess(self, object lastAccess):
116+
DatetimeToCefTimeT(lastAccess, self.cefCookie.last_access)
117+
118+
cpdef object GetLastAccess(self):
119+
return CefTimeTToDatetime(self.cefCookie.last_access)
120+
121+
cpdef py_void SetHasExpires(self, py_bool hasExpires):
122+
self.cefCookie.has_expires = hasExpires
123+
124+
cpdef py_bool GetHasExpires(self):
125+
return self.cefCookie.has_expires
126+
127+
cpdef py_void SetExpires(self, object expires):
128+
DatetimeToCefTimeT(expires, self.cefCookie.expires)
129+
130+
cpdef object GetExpires(self):
131+
return CefTimeTToDatetime(self.cefCookie.expires)
132+
133+
# ------------------------------------------------------------------------------
134+
# CookieManager
135+
# ------------------------------------------------------------------------------
136+
137+
class CookieManager:
138+
pass
139+
140+
# ------------------------------------------------------------------------------
141+
# PyCookieManager
142+
# ------------------------------------------------------------------------------
143+
144+
cdef class PyCookieManager:
145+
pass
146+
147+
# ------------------------------------------------------------------------------
148+
# PyCookieVisitor
149+
# ------------------------------------------------------------------------------
150+
151+
cdef class PyCookieVisitor:
152+
pass
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
# Copyright (c) 2012 CefPython Authors. All rights reserved.
1+
# Copyright (c) 2012-2013 CefPython Authors. All rights reserved.
22
# License: New BSD License.
33
# Website: http://code.google.com/p/cefpython/
44

5-
from cef_base cimport CefBase
5+
from cef_string cimport cef_string_t
6+
from libcpp cimport bool as cpp_bool
7+
from cef_time cimport cef_time_t
68

79
cdef extern from "include/cef_cookie.h":
8-
9-
cdef cppclass CefCookieManager(CefBase):
10+
cdef cppclass CefCookieManager:
1011
pass
12+
13+
ctypedef struct CefCookie:
14+
cef_string_t name
15+
cef_string_t value
16+
cef_string_t domain
17+
cef_string_t path
18+
cpp_bool secure
19+
cpp_bool httponly
20+
cef_time_t creation
21+
cef_time_t last_access
22+
cpp_bool has_expires
23+
cef_time_t expires
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
from ctime cimport time_t
6+
7+
cdef extern from "include/internal/cef_time.h":
8+
ctypedef struct cef_time_t:
9+
int year
10+
int month
11+
int day_of_week
12+
int day_of_month
13+
int hour
14+
int minute
15+
int second
16+
int millisecond
17+
18+
cdef extern from "include/internal/cef_types_wrappers.h":
19+
cdef cppclass CefTime:
20+
CefTime()
21+
CefTime(cef_time_t&)
22+
void SetTimeT(time_t r)
23+
time_t GetTimeT()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
cdef extern from "time.h":
6+
ctypedef struct time_t:
7+
pass
8+
ctypedef struct tm:
9+
int tm_sec
10+
int tm_min
11+
int tm_hour
12+
int tm_mday
13+
int tm_mon
14+
int tm_year
15+
int tm_wday
16+
int tm_yday
17+
int tm_isdst
18+
19+
size_t strftime (char* ptr, size_t maxsize, const char* format,
20+
const tm* timeptr )
21+
tm* localtime (const time_t* timer)
22+
time_t mktime (tm* timeptr)

cefpython/imports.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import inspect # used by JavascriptBindings.__SetObjectMethods()
1515
import urlparse
1616
import urllib
1717
import json
18+
import datetime
1819

1920
if sys.version_info.major == 2:
2021
from urllib import pathname2url as urllib_pathname2url
@@ -66,6 +67,8 @@ from libc.stdlib cimport atoi
6667
from libc.stdint cimport uint64_t
6768
from libc.stdint cimport uintptr_t
6869

70+
cimport ctime
71+
6972
IF UNAME_SYSNAME == "Windows":
7073
from windows cimport *
7174

@@ -119,6 +122,7 @@ IF CEF_VERSION == 1:
119122
from content_filter_handler cimport *
120123
from cef_download_handler cimport *
121124
from cef_cookie cimport *
125+
from cef_time cimport *
122126
from cef_render_handler cimport *
123127

124128
IF UNAME_SYSNAME == "Windows":

cefpython/time_utils.pyx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
cdef void DatetimeToCefTimeT(object pyDatetime, cef_time_t& timeT) except *:
6+
if not isinstance(pyDatetime, datetime.datetime):
7+
raise Exception("Expected object of type datetime.datetime")
8+
timeT.year = pyDatetime.year
9+
timeT.month = pyDatetime.month
10+
timeT.day_of_week = pyDatetime.weekday()
11+
timeT.day_of_month = pyDatetime.day
12+
timeT.hour = pyDatetime.hour
13+
timeT.minute = pyDatetime.minute
14+
timeT.second = pyDatetime.second
15+
# Milliseconds/microseconds are ignored.
16+
timeT.millisecond = 0
17+
18+
cdef object CefTimeTToDatetime(cef_time_t timeT):
19+
cdef int year = timeT.year
20+
if year < datetime.MINYEAR:
21+
year = datetime.MINYEAR
22+
if year > datetime.MAXYEAR:
23+
year = datetime.MAXYEAR
24+
cdef int month = timeT.month
25+
if month == 0:
26+
month = 1
27+
cdef int day_of_month = timeT.day_of_month
28+
if day_of_month == 0:
29+
day_of_month = 1
30+
cdef int second = timeT.second
31+
if second > 59:
32+
# Ignore leap seconds as datetime.datetime() allows 0-59 only.
33+
second = 59
34+
# Milliseconds/microseconds are ignored.
35+
return datetime.datetime(year, month, day_of_month,
36+
timeT.hour, timeT.minute, second)

0 commit comments

Comments
 (0)