2
* Copyright 20145 Canonical Ltd.
4
* This file is part of unity-webapps-qml.
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.
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.
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/>.
19
.import Ubuntu.UnityWebApps 0.2 as UnityWebAppsBridge
24
* Tools API backend binding
27
function createToolsApi(backendDelegate) {
28
var PLUGIN_URI = 'Ubuntu.UnityWebApps';
31
var toolsApiInstance = UnityWebAppsBridge.ToolsApi;
33
function isValidAlgorithm(algorithm) {
34
var algos = ["MD5", "SHA1", "SHA256", "SHA512"]
35
return algos.some(function(e) { return e === algorithm; })
38
function stringToCryptoAlgorithm(algorithm) {
40
"MD5": toolsApiInstance.MD5
41
, "SHA1": toolsApiInstance.SHA1
42
, "SHA256": toolsApiInstance.SHA256
43
, "SHA512": toolsApiInstance.SHA512
45
return assoc[algorithm]
49
getHmacHash: function(message, algorithm, key, callback) {
50
if ( ! isValidAlgorithm(algorithm)) {
51
callback({errorMsg: "Invalid algorithm",
55
callback({errorMsg: "",
56
result: toolsApiInstance.getHmacHash(
57
message, stringToCryptoAlgorithm(algorithm), key)});
59
sendHttpRequest: function(url, location, request, payload, callback) {
60
if ( ! toolsApiInstance.areCompatibleCorsUrl(url, location)) {
61
console.error('sendHttpRequest: incompatible CORS request urls')
65
var xmlrequest = new XMLHttpRequest();
67
var verb = payload && payload.length !== 0
70
xmlrequest.open(verb, url, true);
72
xmlrequest.onreadystatechange = function() {
73
if (xmlrequest.readyState === XMLHttpRequest.DONE) {
75
errorMsg: xmlrequest.statusText,
76
success: xmlrequest.status == 200,
77
response: xmlrequest.responseText
82
for (var header in request.headers) {
83
if (request.headers.hasOwnProperty(header)) {
84
xmlrequest.setRequestHeader(header, request.headers[header])
88
xmlrequest.setRequestHeader(
90
String(payload.length));
92
xmlrequest.send(payload);