~abreu-alexandre/unity-webapps-qml/location-specific-homepage

« back to all changes in this revision

Viewing changes to src/Ubuntu/UnityWebApps/bindings/tools/backend/tools.js

  • Committer: CI Train Bot
  • Author(s): Alexandre Abreu
  • Date: 2015-03-19 14:06:48 UTC
  • mfrom: (144.1.15 hmac-tool)
  • Revision ID: ci-train-bot@canonical.com-20150319140648-bqiejete2pkzuoc3
Add tools API that exposed helpers to webaps
Approved by: PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 20145 Canonical Ltd.
 
3
 *
 
4
 * This file is part of unity-webapps-qml.
 
5
 *
 
6
 * unity-webapps-qml 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
 * unity-webapps-qml 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 Ubuntu.UnityWebApps 0.2 as UnityWebAppsBridge
 
20
 
 
21
 
 
22
/**
 
23
 *
 
24
 * Tools API backend binding
 
25
 *
 
26
 */
 
27
function createToolsApi(backendDelegate) {
 
28
    var PLUGIN_URI = 'Ubuntu.UnityWebApps';
 
29
    var VERSION = 0.2;
 
30
 
 
31
    var toolsApiInstance = UnityWebAppsBridge.ToolsApi;
 
32
 
 
33
    function isValidAlgorithm(algorithm) {
 
34
        var algos = ["MD5", "SHA1", "SHA256", "SHA512"]
 
35
        return algos.some(function(e) { return e === algorithm; })
 
36
    };
 
37
 
 
38
    function stringToCryptoAlgorithm(algorithm) {
 
39
        var assoc = {
 
40
            "MD5": toolsApiInstance.MD5
 
41
            , "SHA1": toolsApiInstance.SHA1
 
42
            , "SHA256": toolsApiInstance.SHA256
 
43
            , "SHA512": toolsApiInstance.SHA512
 
44
        };
 
45
        return assoc[algorithm]
 
46
    };
 
47
 
 
48
    return {
 
49
        getHmacHash: function(message, algorithm, key, callback) {
 
50
            if ( ! isValidAlgorithm(algorithm)) {
 
51
                callback({errorMsg: "Invalid algorithm",
 
52
                             result: null});
 
53
                return;
 
54
            }
 
55
            callback({errorMsg: "",
 
56
                 result: toolsApiInstance.getHmacHash(
 
57
                             message, stringToCryptoAlgorithm(algorithm), key)});
 
58
        },
 
59
        sendHttpRequest: function(url, location, request, payload, callback) {
 
60
            if ( ! toolsApiInstance.areCompatibleCorsUrl(url, location)) {
 
61
                console.error('sendHttpRequest: incompatible CORS request urls')
 
62
                return;
 
63
            }
 
64
 
 
65
            var xmlrequest = new XMLHttpRequest();
 
66
 
 
67
            var verb = payload && payload.length !== 0
 
68
                    ? "POST" : "GET"
 
69
 
 
70
            xmlrequest.open(verb, url, true);
 
71
 
 
72
            xmlrequest.onreadystatechange = function() {
 
73
                if (xmlrequest.readyState === XMLHttpRequest.DONE) {
 
74
                    callback({
 
75
                        errorMsg: xmlrequest.statusText,
 
76
                        success: xmlrequest.status == 200,
 
77
                        response: xmlrequest.responseText
 
78
                    });
 
79
                }
 
80
            };
 
81
 
 
82
            for (var header in request.headers) {
 
83
                if (request.headers.hasOwnProperty(header)) {
 
84
                    xmlrequest.setRequestHeader(header, request.headers[header])
 
85
                }
 
86
            }
 
87
 
 
88
            xmlrequest.setRequestHeader(
 
89
                "Content-Length",
 
90
                String(payload.length));
 
91
 
 
92
            xmlrequest.send(payload);
 
93
        }
 
94
    };
 
95
}