1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/*
* Copyright 2013-2014 Canonical Ltd.
*
* This file is part of webbrowser-app.
*
* webbrowser-app is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* webbrowser-app is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.UnityWebApps 0.1 as UnityWebApps
import Ubuntu.Web 0.2
import webcontainer.private 0.1
import ".."
BrowserWindow {
id: root
objectName: "webappContainer"
property bool backForwardButtonsVisible: true
property bool chromeVisible: true
property string localCookieStoreDbPath: ""
property var intentFilterHandler
property string url: ""
property string webappName: ""
property string webappModelSearchPath: ""
property var webappUrlPatterns
property string accountProvider: ""
property string popupRedirectionUrlPrefixPattern: ""
property url webviewOverrideFile: ""
property var __webappCookieStore: null
property bool sharedCookies: false
property string localUserAgentOverride: ""
property bool blockOpenExternalUrls: false
currentWebview: webappViewLoader.item ? webappViewLoader.item.currentWebview : null
property bool runningLocalApplication: false
title: getWindowTitle()
onActiveChanged: if (sharedCookies) {
if (!active) {
console.log("Getting inactive, saving cookies to " + sharedCookieStore.dbPath)
if (!__webappCookieStore && webappViewLoader.item) {
var context = webappViewLoader.item.currentWebview.context
__webappCookieStore = oxideCookieStoreComponent.createObject(this, {
"oxideStoreBackend": context.cookieManager,
"dbPath": context.dataPath + "/cookies.sqlite"
})
}
sharedCookieStore.moveFrom(__webappCookieStore)
} else {
loadCookies()
}
}
function loadCookies() {
if (!webappViewLoader.item) return
if (!__webappCookieStore) {
var context = webappViewLoader.item.currentWebview.context
__webappCookieStore = oxideCookieStoreComponent.createObject(this, {
"oxideStoreBackend": context.cookieManager,
"dbPath": context.dataPath + "/cookies.sqlite"
})
}
console.log("Loading cookies from " + sharedCookieStore.dbPath)
__webappCookieStore.moved.connect(onCookiesMoved)
__webappCookieStore.moveFrom(sharedCookieStore)
}
// Used for testing
signal intentUriHandleResult(string uri)
function getWindowTitle() {
var webappViewTitle =
webappViewLoader.item
? webappViewLoader.item.title : ""
var name = getWebappName()
if (typeof(name) === 'string' && name.length !== 0) {
return name
} else if (webappViewTitle) {
// TRANSLATORS: %1 refers to the current page’s title
return i18n.tr("%1 - Ubuntu Web Browser").arg(webappViewTitle)
} else {
return i18n.tr("Ubuntu Web Browser")
}
}
SharedCookieStore {
id: sharedCookieStore
dbPath: "account-" + (webappViewLoader.credentialsId ? webappViewLoader.credentialsId : "0")
}
Component {
id: webappViewComponent
WebApp {
id: browser
url: (sharedCookies || accountProvider.length !== 0) ? "" : root.url
dataPath: webappDataLocation
webappName: root.webappName
chromeVisible: root.chromeVisible
backForwardButtonsVisible: root.backForwardButtonsVisible
developerExtrasEnabled: root.developerExtrasEnabled
webappModelSearchPath: root.webappModelSearchPath
webappUrlPatterns: root.webappUrlPatterns
blockOpenExternalUrls: root.blockOpenExternalUrls
popupRedirectionUrlPrefixPattern: root.popupRedirectionUrlPrefixPattern
localUserAgentOverride: getLocalUserAgentOverrideIfAny()
runningLocalApplication: root.runningLocalApplication
webviewOverrideFile: root.webviewOverrideFile
anchors.fill: parent
webbrowserWindow: webbrowserWindowProxy
onWebappNameChanged: {
if (root.webappName !== browser.webappName) {
root.webappName = browser.webappName;
root.title = getWindowTitle();
}
}
}
}
function getWebappName() {
/**
Any webapp name coming from the command line takes over.
A webapp can also be defined by a specific drop-in webapp-properties.json
file that can bundle a few specific 'properties' (as the name implies)
instead of having them listed in the command line.
*/
if (webappName)
return webappName
return webappModelSearchPath && webappModel.providesSingleInlineWebapp()
? webappModel.getSingleInlineWebappName()
: ""
}
function getLocalUserAgentOverrideIfAny() {
if (localUserAgentOverride.length !== 0)
return localUserAgentOverride
var name = getWebappName()
if (name && webappModel.exists(name))
return webappModel.userAgentOverrideFor(name)
return ""
}
UnityWebApps.UnityWebappsAppModel {
id: webappModel
searchPath: root.webappModelSearchPath
onModelContentChanged: {
var name = getWebappName()
if (name && root.url.length === 0) {
var idx = webappModel.getWebappIndex(name)
root.url = webappModel.data(
idx, UnityWebApps.UnityWebappsAppModel.Homepage)
}
}
}
// Because of https://launchpad.net/bugs/1398046, it's important that this
// is the first child
Loader {
id: webappViewLoader
anchors.fill: parent
property var credentialsId: null
property var webContextSessionCookieMode: null
property var webappDataLocation: credentialsId != null ? dataLocation + "/id-" + credentialsId : dataLocation
}
Loader {
id: accountsPageComponentLoader
anchors.fill: parent
onStatusChanged: {
if (status == Loader.Error) {
// Happens on the desktop, if Ubuntu.OnlineAccounts.Client
// can't be imported
loadWebAppView()
} else if (status == Loader.Ready) {
item.visible = true
}
}
}
function onCookiesMoved(result) {
if (__webappCookieStore) {
__webappCookieStore.moved.disconnect(onCookiesMoved)
}
if (!result) {
console.log("Cookies were not moved")
}
if (!webappViewLoader.item.url) {
webappViewLoader.item.url = root.url
}
}
function moveCookies(credentialsId) {
if (!__webappCookieStore) {
var context = webappViewLoader.item.currentWebview.context
__webappCookieStore = oxideCookieStoreComponent.createObject(this, {
"oxideStoreBackend": context.cookieManager,
"dbPath": context.dataPath + "/cookies.sqlite"
})
}
var storeComponent = localCookieStoreDbPath.length !== 0 ?
localCookieStoreComponent : onlineAccountStoreComponent
var instance = storeComponent.createObject(root, { "accountId": credentialsId })
__webappCookieStore.moved.connect(onCookiesMoved)
__webappCookieStore.moveFrom(instance)
}
Connections {
target: accountsPageComponentLoader.item
onDone: {
if (successful) {
webappViewLoader.loaded.connect(function () {
if (webappViewLoader.status == Loader.Ready) {
moveCookies(webappViewLoader.credentialsId)
}
});
webappViewLoader.credentialsId = credentialsId
// If we need to preserve session cookies, make sure that the
// mode is "restored" and not "persistent", or the cookies
// transferred from OA would be lost.
// We check if the webContextSessionCookieMode is defined and, if so,
// we override it in the webapp loader.
if (typeof webContextSessionCookieMode === "string") {
webappViewLoader.webContextSessionCookieMode = "restored"
}
}
loadWebAppView()
}
}
Component {
id: oxideCookieStoreComponent
ChromeCookieStore {
}
}
Component {
id: localCookieStoreComponent
LocalCookieStore {
dbPath: localCookieStoreDbPath
}
}
Component.onCompleted: {
i18n.domain = "webbrowser-app"
// check if we are to display the login view
// or directly switch to the webapp view
if (!sharedCookies && accountProvider.length !== 0) {
loadLoginView();
} else {
loadWebAppView();
}
}
Component {
id: onlineAccountStoreComponent
OnlineAccountsCookieStore { }
}
function loadLoginView() {
accountsPageComponentLoader.setSource("AccountsPage.qml", {
"accountProvider": accountProvider,
"applicationName": unversionedAppId,
})
}
function loadWebAppView() {
if (accountsPageComponentLoader.item)
accountsPageComponentLoader.item.visible = false
webappViewLoader.loaded.connect(function () {
if (webappViewLoader.status === Loader.Ready) {
// As we use StateSaver to restore the URL, we need to check first if
// it has not been set previously before setting the URL to the default property
// homepage.
var webView = webappViewLoader.item.currentWebview
var current_url = webView.url.toString();
if (!current_url || current_url.length === 0) {
webView.url = root.url
}
}
});
webappViewLoader.sourceComponent = webappViewComponent
}
function makeUrlFromIntentResult(intentFilterResult) {
var scheme = null
var hostname = null
var url = root.currentWebview.url || root.url
if (intentFilterResult.host
&& intentFilterResult.host.length !== 0) {
hostname = intentFilterResult.host
}
else {
var matchHostname = url.toString().match(/.*:\/\/([^/]*)\/.*/)
if (matchHostname.length > 1) {
hostname = matchHostname[1]
}
}
if (intentFilterResult.scheme
&& intentFilterResult.scheme.length !== 0) {
scheme = intentFilterResult.scheme
}
else {
var matchScheme = url.toString().match(/(.*):\/\/[^/]*\/.*/)
if (matchScheme.length > 1) {
scheme = matchScheme[1]
}
}
return scheme
+ '://'
+ hostname
+ "/"
+ (intentFilterResult.uri
? intentFilterResult.uri : "")
}
/**
* Identity function for non-intent URIs.
*
* Otherwise if the URI is an intent, tries to apply a webapp
* local filter (or identity) and reconstruct the target URI based
* on its result.
*/
function handleIntentUri(uri) {
var _uri = uri;
if (webappIntentFilter
&& webappIntentFilter.isValidIntentUri(_uri)) {
var result = webappIntentFilter.applyFilter(_uri)
_uri = makeUrlFromIntentResult(result)
console.log("Intent URI '" + uri + "' was mapped to '" + _uri + "'")
}
// Report the result of the intent uri filtering (if any)
// Done for testing purposed. It is not possible at this point
// to have AP call a slot and retrieve its result synchronously.
intentUriHandleResult(_uri)
return _uri
}
// Handle runtime requests to open urls as defined
// by the freedesktop application dbus interface's open
// method for DBUS application activation:
// http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus
// The dispatch on the org.freedesktop.Application if is done per appId at the
// url-dispatcher/upstart level.
Connections {
target: UriHandler
onOpened: {
// only consider the first one (if multiple)
if (uris.length === 0 || !root.currentWebview) {
return;
}
var requestedUrl = uris[0].toString();
if (popupRedirectionUrlPrefixPattern.length !== 0
&& requestedUrl.match(popupRedirectionUrlPrefixPattern)) {
return;
}
requestedUrl = handleIntentUri(requestedUrl);
// Add a small guard to prevent browsing to invalid urls
if (currentWebview
&& currentWebview.shouldAllowNavigationTo
&& !currentWebview.shouldAllowNavigationTo(requestedUrl)) {
return;
}
root.url = requestedUrl
root.currentWebview.url = requestedUrl
}
}
}
|