Merge lp:~mardy/webbrowser-app/export-cookies into lp:webbrowser-app

Proposed by David Barth
Status: Needs review
Proposed branch: lp:~mardy/webbrowser-app/export-cookies
Merge into: lp:webbrowser-app
Diff against target: 305 lines (+237/-1)
5 files modified
src/app/webcontainer/CMakeLists.txt (+1/-0)
src/app/webcontainer/json-cookie-store.cpp (+143/-0)
src/app/webcontainer/json-cookie-store.h (+50/-0)
src/app/webcontainer/webapp-container.cpp (+3/-0)
src/app/webcontainer/webapp-container.qml (+40/-1)
To merge this branch: bzr merge lp:~mardy/webbrowser-app/export-cookies
Reviewer Review Type Date Requested Status
system-apps-ci-bot continuous-integration Needs Fixing
PS Jenkins bot continuous-integration Approve
Ubuntu Phablet Team Pending
Review via email: mp+257337@code.launchpad.net

Commit message

Prototype for testing how to share cookies between webapps.

Description of the change

Prototype for testing how to share cookies between webapps.

To post a comment you must log in.
Revision history for this message
David Barth (dbarth) wrote :

Not for a real merge proposal review yet

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
system-apps-ci-bot (system-apps-ci-bot) wrote :
review: Needs Fixing (continuous-integration)

Unmerged revisions

974. By Alberto Mardegan

Test

973. By Alberto Mardegan

Add JsonCookieStore

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/app/webcontainer/CMakeLists.txt'
2--- src/app/webcontainer/CMakeLists.txt 2015-01-26 14:53:18 +0000
3+++ src/app/webcontainer/CMakeLists.txt 2015-04-24 09:22:18 +0000
4@@ -13,6 +13,7 @@
5 cookie-store.cpp
6 online-accounts-cookie-store.cpp
7 oxide-cookie-helper.cpp
8+ json-cookie-store.cpp
9 local-cookie-store.cpp
10 webapp-container.cpp
11 webapp-container-helper.cpp
12
13=== added file 'src/app/webcontainer/json-cookie-store.cpp'
14--- src/app/webcontainer/json-cookie-store.cpp 1970-01-01 00:00:00 +0000
15+++ src/app/webcontainer/json-cookie-store.cpp 2015-04-24 09:22:18 +0000
16@@ -0,0 +1,143 @@
17+/*
18+ * Copyright 2014 Canonical Ltd.
19+ *
20+ * This file is part of webbrowser-app.
21+ *
22+ * webbrowser-app is free software; you can redistribute it and/or modify
23+ * it under the terms of the GNU General Public License as published by
24+ * the Free Software Foundation; version 3.
25+ *
26+ * webbrowser-app is distributed in the hope that it will be useful,
27+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29+ * GNU General Public License for more details.
30+ *
31+ * You should have received a copy of the GNU General Public License
32+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33+ */
34+
35+#include "json-cookie-store.h"
36+
37+#include <QByteArray>
38+#include <QDebug>
39+#include <QFile>
40+#include <QFileInfo>
41+#include <QJsonArray>
42+#include <QJsonDocument>
43+#include <QNetworkCookie>
44+#include <QStandardPaths>
45+#include "oxide-cookie-helper.h"
46+
47+static QList<QNetworkCookie> cookiesFromVariant(const QVariantList &cl)
48+{
49+ QList<QNetworkCookie> cookies;
50+ Q_FOREACH(QVariant cookie, cl) {
51+ if (!cookie.canConvert(QVariant::Map)) {
52+ continue;
53+ }
54+
55+ QNetworkCookie nc;
56+ QVariantMap vm = cookie.toMap();
57+ if (!vm.contains("name") || !vm.contains("value")) {
58+ continue;
59+ }
60+
61+ nc.setName(vm.value("name").toByteArray());
62+ nc.setValue(vm.value("value").toByteArray());
63+ nc.setDomain(vm.value("domain").toString());
64+ nc.setPath(vm.value("path").toString());
65+ if (vm.contains("httponly") &&
66+ vm.value("httponly").canConvert(QVariant::Bool)) {
67+ nc.setHttpOnly(vm.value("httponly").toBool());
68+ }
69+
70+ if (vm.contains("issecure") &&
71+ vm.value("issecure").canConvert(QVariant::Bool)) {
72+ nc.setSecure(vm.value("issecure").toBool());
73+ }
74+
75+ if (vm.contains("expirationdate") &&
76+ vm.value("expirationdate").canConvert(QMetaType::QDateTime)) {
77+ nc.setExpirationDate(vm.value("expirationdate").toDateTime());
78+ }
79+
80+ cookies.append(nc);
81+ }
82+ return cookies;
83+}
84+
85+JsonCookieStore::JsonCookieStore(QObject* parent):
86+ CookieStore(parent)
87+{
88+}
89+
90+void JsonCookieStore::doGetCookies()
91+{
92+ Cookies cookies;
93+
94+ QFile file(m_dbPath);
95+
96+ if (Q_UNLIKELY(!file.open(QIODevice::ReadOnly))) {
97+ qCritical() << "Could not open cookie database:" << m_dbPath;
98+ return;
99+ }
100+
101+ QByteArray contents = file.readAll();
102+ QJsonDocument doc = QJsonDocument::fromJson(contents);
103+ if (doc.isEmpty() || !doc.isArray()) {
104+ qCritical() << "Json DB invalid or empty" << m_dbPath;
105+ return;
106+ }
107+
108+ QVariantList cookieVariants = doc.array().toVariantList();
109+
110+ emit gotCookies(cookiesFromVariant(cookieVariants));
111+}
112+
113+QDateTime JsonCookieStore::lastUpdateTimeStamp() const
114+{
115+ //QFileInfo dbFileInfo(m_dbPath);
116+ //return dbFileInfo.lastModified();
117+ return QDateTime();
118+}
119+
120+void JsonCookieStore::doSetCookies(const Cookies& parsedCookies)
121+{
122+ QFile file (m_dbPath);
123+ if (!file.open(QIODevice::WriteOnly)) {
124+ qCritical() << "Could not open cookie database:" << m_dbPath;
125+ return;
126+ }
127+
128+ QVariant cookies = OxideCookieHelper::variantFromCookies(parsedCookies);
129+ QJsonArray jsonCookies =
130+ QJsonArray::fromVariantList(cookies.toList());
131+ QJsonDocument doc(jsonCookies);
132+ file.write(doc.toJson());
133+ file.close();
134+
135+ emit cookiesSet(true);
136+}
137+
138+void JsonCookieStore::setDbPath(const QString &path)
139+{
140+ // If path is a URL, strip the initial "file://"
141+ QString normalizedPath = path.startsWith("file://") ? path.mid(7) : path;
142+
143+ if (normalizedPath != m_dbPath) {
144+ if (Q_UNLIKELY(!normalizedPath.startsWith('/'))) {
145+ qWarning() << "Invalid database path (must be absolute):" << path;
146+ return;
147+ }
148+ m_dbPath = normalizedPath;
149+
150+ qDebug() << "Using local cookie db: " << m_dbPath;
151+
152+ Q_EMIT dbPathChanged();
153+ }
154+}
155+
156+QString JsonCookieStore::dbPath () const
157+{
158+ return m_dbPath;
159+}
160
161=== added file 'src/app/webcontainer/json-cookie-store.h'
162--- src/app/webcontainer/json-cookie-store.h 1970-01-01 00:00:00 +0000
163+++ src/app/webcontainer/json-cookie-store.h 2015-04-24 09:22:18 +0000
164@@ -0,0 +1,50 @@
165+/*
166+ * Copyright 2014 Canonical Ltd.
167+ *
168+ * This file is part of webbrowser-app.
169+ *
170+ * webbrowser-app is free software; you can redistribute it and/or modify
171+ * it under the terms of the GNU General Public License as published by
172+ * the Free Software Foundation; version 3.
173+ *
174+ * webbrowser-app is distributed in the hope that it will be useful,
175+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
176+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
177+ * GNU General Public License for more details.
178+ *
179+ * You should have received a copy of the GNU General Public License
180+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
181+ */
182+
183+#ifndef JSON_COOKIE_STORE_H
184+#define JSON_COOKIE_STORE_H
185+
186+#include "cookie-store.h"
187+
188+class JsonCookieStore : public CookieStore
189+{
190+ Q_OBJECT
191+ Q_PROPERTY(QString dbPath READ dbPath WRITE setDbPath NOTIFY dbPathChanged)
192+
193+public:
194+ JsonCookieStore(QObject* parent = 0);
195+
196+ void setDbPath(const QString& path);
197+ QString dbPath() const;
198+
199+ QDateTime lastUpdateTimeStamp() const Q_DECL_OVERRIDE;
200+
201+Q_SIGNALS:
202+ void dbPathChanged();
203+
204+private:
205+ virtual void doGetCookies() Q_DECL_OVERRIDE;
206+ virtual void doSetCookies(const Cookies& cookies) Q_DECL_OVERRIDE;
207+
208+ bool createDb();
209+
210+private:
211+ QString m_dbPath;
212+};
213+
214+#endif // JSON_COOKIE_STORE_H
215
216=== modified file 'src/app/webcontainer/webapp-container.cpp'
217--- src/app/webcontainer/webapp-container.cpp 2015-04-10 13:33:19 +0000
218+++ src/app/webcontainer/webapp-container.cpp 2015-04-24 09:22:18 +0000
219@@ -21,6 +21,7 @@
220
221 #include "chrome-cookie-store.h"
222 #include "intent-filter.h"
223+#include "json-cookie-store.h"
224 #include "local-cookie-store.h"
225 #include "online-accounts-cookie-store.h"
226 #include "session-utils.h"
227@@ -233,6 +234,8 @@
228 if (engine) {
229 qmlRegisterType<ChromeCookieStore>(privateModuleUri, 0, 1,
230 "ChromeCookieStore");
231+ qmlRegisterType<JsonCookieStore>(privateModuleUri, 0, 1,
232+ "JsonCookieStore");
233 qmlRegisterType<LocalCookieStore>(privateModuleUri, 0, 1,
234 "LocalCookieStore");
235 qmlRegisterType<OnlineAccountsCookieStore>(privateModuleUri, 0, 1,
236
237=== modified file 'src/app/webcontainer/webapp-container.qml'
238--- src/app/webcontainer/webapp-container.qml 2015-04-07 14:30:52 +0000
239+++ src/app/webcontainer/webapp-container.qml 2015-04-24 09:22:18 +0000
240@@ -49,10 +49,42 @@
241 property bool runningLocalApplication: false
242
243 title: getWindowTitle()
244+ onActiveChanged: {
245+ if (!active) {
246+ console.log("Getting inactive, saving cookies to " + jsonCookieStore.dbPath)
247+
248+ if (!__webappCookieStore && webappViewLoader.item) {
249+ var context = webappViewLoader.item.currentWebview.context
250+ __webappCookieStore = oxideCookieStoreComponent.createObject(this, {
251+ "oxideStoreBackend": context.cookieManager,
252+ "dbPath": context.dataPath + "/cookies.sqlite"
253+ })
254+ }
255+
256+ jsonCookieStore.moveFrom(__webappCookieStore)
257+ } else {
258+ loadCookies()
259+ }
260+ }
261
262 // Used for testing
263 signal intentUriHandleResult(string uri)
264
265+ function loadCookies() {
266+ if (!webappViewLoader.item) return
267+ if (!__webappCookieStore) {
268+ var context = webappViewLoader.item.currentWebview.context
269+ __webappCookieStore = oxideCookieStoreComponent.createObject(this, {
270+ "oxideStoreBackend": context.cookieManager,
271+ "dbPath": context.dataPath + "/cookies.sqlite"
272+ })
273+ }
274+
275+ console.log("Loading cookies from " + jsonCookieStore.dbPath)
276+ __webappCookieStore.moved.connect(onCookiesMoved)
277+ __webappCookieStore.moveFrom(jsonCookieStore)
278+ }
279+
280 function getWindowTitle() {
281 var webappViewTitle =
282 webappViewLoader.item
283@@ -68,6 +100,11 @@
284 }
285 }
286
287+ JsonCookieStore {
288+ id: jsonCookieStore
289+ dbPath: "/tmp/cookie-" + Qt.application.name + ".json"
290+ }
291+
292 Component {
293 id: webappViewComponent
294
295@@ -176,7 +213,9 @@
296 if (!result) {
297 console.log("Cookies were not moved")
298 }
299- webappViewLoader.item.url = root.url
300+ if (!webappViewLoader.item.url) {
301+ webappViewLoader.item.url = root.url
302+ }
303 }
304
305 function moveCookies(credentialsId) {

Subscribers

People subscribed via source and target branches

to status/vote changes: