2
* Copyright 2013 Canonical Ltd.
4
* This file is part of webbrowser-app.
6
* webbrowser-app is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 3.
10
* webbrowser-app is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
import QtQuick.Window 2.0
21
import Ubuntu.Components 0.1
22
import "ua-overrides.js" as Overrides
25
* Useful documentation:
26
* http://en.wikipedia.org/wiki/User_agent#Format
27
* https://developer.mozilla.org/en-US/docs/Gecko_user_agent_string_reference
28
* https://wiki.mozilla.org/B2G/User_Agent
29
* https://github.com/mozilla-b2g/gaia/blob/master/build/ua-override-prefs.js
30
* https://developers.google.com/chrome/mobile/docs/user-agent
33
// This is an Item, not a QtObject, because it needs information about the Screen.
35
// %1: form factor (Mobile, Tablet, Desktop)
37
readonly property string _template: "Mozilla/5.0 (Ubuntu; %1) WebKit/%2"
39
// See Source/WebCore/Configurations/Version.xcconfig in QtWebKit’s source tree
40
// TODO: determine this value at runtime
41
readonly property string _webkitVersion: "537.21"
43
// FIXME: this is a quick hack that will become increasingly unreliable
44
// as we support more devices, so we need a better solution for this
45
// FIXME: only handling phone and tablet for now, need to handle desktop too
46
readonly property string _formFactor: (Screen.width >= units.gu(60)) ? "Tablet" : "Mobile"
48
property string defaultUA: _template.arg(_formFactor).arg(_webkitVersion)
50
property var overrides: Overrides.overrides
52
function getDomain(url) {
53
var domain = url.toString()
54
var indexOfScheme = domain.indexOf("://")
55
if (indexOfScheme !== -1) {
56
domain = domain.slice(indexOfScheme + 3)
58
var indexOfPath = domain.indexOf("/")
59
if (indexOfPath !== -1) {
60
domain = domain.slice(0, indexOfPath)
65
function getDomains(domain) {
66
var components = domain.split(".")
68
for (var i = 0; i < components.length; i++) {
69
domains.push(components.slice(i).join("."))
74
function getUAString(url) {
76
var domains = getDomains(getDomain(url))
77
for (var i = 0; i < domains.length; i++) {
78
var domain = domains[i]
79
if (domain in overrides) {
80
var form = overrides[domain]
81
if (typeof form == "string") {
83
} else if (typeof form == "object") {
84
return ua.replace(form[0], form[1])