forked from jarrodek/ChromeRestClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
201 lines (201 loc) · 5.85 KB
/
Copy pathbackground.js
File metadata and controls
201 lines (201 loc) · 5.85 KB
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
'use strict';
/**
* Advanced Rest Client namespace.
*
* @namespace
*/
var arc = arc || {};
/**
* ARC background page namespace.
*
* @namespace
*/
arc.bg = arc.bg || {};
/**
* Open new window of the app.
* Every call will open new window even if another window is already created.
*/
arc.bg.openWindow = (url) => {
if (!url) {
url = 'index.html';
}
var all = chrome.app.window.getAll();
var length = all.length;
var id = 'arc-window-' + length;
chrome.app.window.create(
url, {
id: id,
bounds: {
width: 1200,
height: 800
}
},
function() {
arc.bg.recordSession();
}
);
};
/**
* Handler for `onLaunched` event.
* Depending on launch data it will open window in default view or requested view.
*/
arc.bg.onLaunched = (lunchData) => {
var url = 'index.html';
if (lunchData && lunchData.id) {
switch (lunchData.id) {
case 'google_drive_open':
let _url = lunchData.url;
_url = _url.substr(_url.indexOf('state') + 6);
let data = JSON.parse(decodeURIComponent(_url));
let id = data.ids[0];
url += '#!/request/drive/' + id;
break;
}
}
arc.bg.openWindow(url);
};
arc.bg.uuid = function() {
// jscs:disable
/* jshint ignore:start */
var lut = [];
for (var i = 0; i < 256; i++) {
lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
}
var fn = function() {
var d0 = Math.random() * 0xffffffff | 0;
var d1 = Math.random() * 0xffffffff | 0;
var d2 = Math.random() * 0xffffffff | 0;
var d3 = Math.random() * 0xffffffff | 0;
return lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] +
lut[d0 >> 24 & 0xff] + '-' + lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' +
lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' + lut[d2 & 0x3f | 0x80] +
lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
};
return fn();
// jscs:enable
/* jshint ignore:end */
};
arc.bg.getAppId = () => {
return new Promise((resolve) => {
chrome.storage.local.get({
'APP_ID': null
}, function(result) {
if (!result.APP_ID) {
result.APP_ID = arc.bg.uuid();
chrome.storage.local.set(result, function() {
resolve(result.APP_ID);
});
} else {
resolve(result.APP_ID);
}
});
});
};
arc.bg.getAppAnonumousId = () => {
return new Promise((resolve) => {
chrome.storage.local.get({
'appAnonymousId': null
}, function(result) {
resolve(result.appAnonymousId);
});
});
};
arc.bg.releaseChannel = () => {
var manifest = chrome.runtime.getManifest();
// jscs:disable
var manifestName = manifest.version_name;
// jscs:enable
var release = null;
if (manifestName.indexOf('beta') !== -1) {
release = 'beta';
} else if (manifestName.indexOf('dev') !== -1) {
release = 'dev';
} else if (manifestName.indexOf('canary') !== -1) {
release = 'canary';
} else {
release = 'stable';
}
return release;
};
/**
* To ensure anynomous app usage the app is using browser's build in crypto functions.
* It requires to generate some initial random data to encode a string.
*
* Generated values are not stored so they can't be used again. Clearing hashed app id
* (anonumousId) can't be restored.
*/
arc.bg.getAninimizedId = () => {
return arc.bg.getAppAnonumousId().then((anonymousId) => {
if (anonymousId) {
return anonymousId;
}
// The app id is kept locally and it's available to the app.
// It was used to identify user data on server storage.
// It's not used right now since there's no app's server data synchroznization
// but it's a subject to change.
let encodedDataBuffer;
return arc.bg.getAppId()
.then((appId) => {
let encoder = new TextEncoder('utf-8');
encodedDataBuffer = encoder.encode(appId);
let aesAlgorithmKeyGen = {
name: 'AES-CBC',
length: 128
};
return window.crypto.subtle.generateKey(aesAlgorithmKeyGen, false, ['encrypt']);
})
.then((cryptoKey) => {
let aesAlgorithmEncrypt = {
name: 'AES-CBC',
iv: window.crypto.getRandomValues(new Uint8Array(16))
};
return window.crypto.subtle.encrypt(aesAlgorithmEncrypt, cryptoKey, encodedDataBuffer);
})
.then((arrayBuffer) => {
let view = new Uint8Array(arrayBuffer);
let arr = Array.prototype.slice.call(view);
arr = arr.map(function(item) {
return String.fromCharCode(item);
});
return window.btoa(arr.join(''));
})
.then((base64) => {
return new Promise((resolve) => {
chrome.storage.local.set({
appAnonymousId: base64
}, function() {
resolve(base64);
});
});
});
});
};
/**
* Very base replacement for GA session counter.
* According to T&C of GA users must have ability to disable GA in the app.
* However usage (only usage which is # of app openings) stats are crutial for the app.
* This function will send generated an nonymous ID to the app's backed to record the session.
* This is the only information send to the backed (time and generated ID)
*/
arc.bg.recordSession = () => {
arc.bg.getAninimizedId().then((aid) => {
let d = new Date();
// let url = 'http://localhost:8080/analytics/record';
let url = 'https://advancedrestclient-1155.appspot.com/analytics/record';
let data = new FormData();
data.append('aid', aid); // anonymousId
data.append('tz', d.getTimezoneOffset()); //timezone
fetch(url, {
method: 'POST',
body: data
}).catch(() => {});
});
};
/**
* Listens for the app launching then creates the window.
*
* @see http://developer.chrome.com/apps/app.runtime.html
* @see http://developer.chrome.com/apps/app.window.html
*/
chrome.app.runtime.onLaunched.addListener(arc.bg.onLaunched);