~bfiller/webbrowser-app/restrict-arches

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/Extras/Browser/UserAgent01.qml

  • Committer: Olivier Tilloy
  • Date: 2014-03-17 22:16:59 UTC
  • mfrom: (468.1.18 with-oxide-api)
  • Revision ID: olivier.tilloy@canonical.com-20140317221659-b2jao4k44upufd2n
Version the new UbuntuWebView API as 0.2 (oxide).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This file is part of webbrowser-app.
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
import QtQuick 2.0
 
20
import QtQuick.Window 2.0
 
21
import Ubuntu.Components 0.1
 
22
import "ua-overrides.js" as Overrides
 
23
 
 
24
/*
 
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
 
31
 */
 
32
 
 
33
// This is an Item, not a QtObject, because it needs information about the Screen.
 
34
Item {
 
35
    // %1: form factor (Mobile, Tablet, Desktop)
 
36
    // %2: WebKit version
 
37
    readonly property string _template: "Mozilla/5.0 (Ubuntu; %1) WebKit/%2"
 
38
 
 
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"
 
42
 
 
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"
 
47
 
 
48
    property string defaultUA: _template.arg(_formFactor).arg(_webkitVersion)
 
49
 
 
50
    property var overrides: Overrides.overrides
 
51
 
 
52
    function getDomain(url) {
 
53
        var domain = url.toString()
 
54
        var indexOfScheme = domain.indexOf("://")
 
55
        if (indexOfScheme !== -1) {
 
56
            domain = domain.slice(indexOfScheme + 3)
 
57
        }
 
58
        var indexOfPath = domain.indexOf("/")
 
59
        if (indexOfPath !== -1) {
 
60
            domain = domain.slice(0, indexOfPath)
 
61
        }
 
62
        return domain
 
63
    }
 
64
 
 
65
    function getDomains(domain) {
 
66
        var components = domain.split(".")
 
67
        var domains = []
 
68
        for (var i = 0; i < components.length; i++) {
 
69
            domains.push(components.slice(i).join("."))
 
70
        }
 
71
        return domains
 
72
    }
 
73
 
 
74
    function getUAString(url) {
 
75
        var ua = defaultUA
 
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") {
 
82
                    return form
 
83
                } else if (typeof form == "object") {
 
84
                    return ua.replace(form[0], form[1])
 
85
                }
 
86
            }
 
87
        }
 
88
        return ua
 
89
    }
 
90
}