~ubuntu-branches/debian/jessie/wot/jessie

« back to all changes in this revision

Viewing changes to chrome/wot.jar!/content/shared.js

  • Committer: Package Import Robot
  • Author(s): Fabrizio Regalli
  • Date: 2011-11-04 12:09:05 UTC
  • Revision ID: package-import@ubuntu.com-20111104120905-4q6rm66gb8rzwe15
Tags: upstream-20110704
ImportĀ upstreamĀ versionĀ 20110704

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        shared.js
 
3
        Copyright Ā© 2008-2011  WOT Services Oy <info@mywot.com>
 
4
 
 
5
        This file is part of WOT.
 
6
 
 
7
        WOT is free software: you can redistribute it and/or modify it
 
8
        under the terms of the GNU General Public License as published by
 
9
        the Free Software Foundation, either version 3 of the License, or
 
10
        (at your option) any later version.
 
11
 
 
12
        WOT is distributed in the hope that it will be useful, but WITHOUT
 
13
        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
        or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 
15
        License for more details.
 
16
 
 
17
        You should have received a copy of the GNU General Public License
 
18
        along with WOT. If not, see <http://www.gnu.org/licenses/>.
 
19
*/
 
20
 
 
21
const WOT_PREFIX_SHARED = "wot_shared";
 
22
 
 
23
var wot_shared =
 
24
{
 
25
        set: "abcdefghijklmnopqrstuvwxyz234567",
 
26
        rev: null,
 
27
 
 
28
        load_delayed: function()
 
29
        {
 
30
                try {
 
31
                        this.sync();
 
32
                } catch (e) {
 
33
                        dump("wot_shared.load: failed with " + e + "\n");
 
34
                }
 
35
        },
 
36
 
 
37
        parse: function(shared)
 
38
        {
 
39
                try {
 
40
                        var i;
 
41
                        var data = {};
 
42
 
 
43
                        for (i = 0; i < shared.length; ++i) {
 
44
                                var attr = shared[i].attributes.getNamedItem(
 
45
                                                                WOT_SERVICE_XML_UPDATE_SHARED_LEVEL);
 
46
 
 
47
                                if (!attr || !attr.nodeValue) {
 
48
                                        return;
 
49
                                }
 
50
 
 
51
                                var level = Number(attr.nodeValue);
 
52
 
 
53
                                if (level < 1) {
 
54
                                        return;
 
55
                                }
 
56
 
 
57
                                var attr = shared[i].attributes.getNamedItem(
 
58
                                                                WOT_SERVICE_XML_UPDATE_SHARED_DOMAINS);
 
59
 
 
60
                                if (!attr || !attr.nodeValue) {
 
61
                                        return;
 
62
                                }
 
63
 
 
64
                                if (!data[level]) {
 
65
                                        data[level] = [];
 
66
                                }
 
67
 
 
68
                                data[level] = data[level].concat(attr.nodeValue.split(","));
 
69
                        }
 
70
 
 
71
                        for (i in data) {
 
72
                                wot_prefs.setChar("shared." + i, data[i].join(","));
 
73
                        }
 
74
 
 
75
                        this.sync();
 
76
                } catch (e) {
 
77
                        dump("wot_shared.parse: failed with " + e + "\n");
 
78
                }
 
79
        },
 
80
 
 
81
        sync: function()
 
82
        {
 
83
                try {
 
84
                        var branch = wot_prefs.ps.getBranch(WOT_PREF + "shared.");
 
85
                        var children = branch.getChildList("", {});
 
86
 
 
87
                        for (var i = 0; i < children.length; ++i) {
 
88
                                var level = Number(children[i]);
 
89
 
 
90
                                if (level < 1) {
 
91
                                        continue;
 
92
                                }
 
93
 
 
94
                                var data = wot_prefs.getChar("shared." + children[i]);
 
95
 
 
96
                                if (!data || !data.length) {
 
97
                                        continue;
 
98
                                }
 
99
 
 
100
                                var domains = data.split(",");
 
101
 
 
102
                                for (var j = 0; j < domains.length; ++j) {
 
103
                                        if (!domains[j].length) {
 
104
                                                continue;
 
105
                                        }
 
106
 
 
107
                                        var pn = wot_idn.utftoidn(domains[j]);
 
108
 
 
109
                                        if (!pn) {
 
110
                                                continue;
 
111
                                        }
 
112
 
 
113
                                        wot_hashtable.set(WOT_PREFIX_SHARED + ":" + pn, level);
 
114
                                }
 
115
                        }
 
116
                } catch (e) {
 
117
                        dump("wot_shared.load: failed with " + e + "\n");
 
118
                }
 
119
        },
 
120
 
 
121
        isshared: function(host)
 
122
        {
 
123
                try {
 
124
                        return wot_hashtable.get(WOT_PREFIX_SHARED + ":" + host);
 
125
                } catch (e) {
 
126
                        dump("wot_shared.isshared: failed with " + e + "\n");
 
127
                }
 
128
 
 
129
                return null;
 
130
        },
 
131
 
 
132
        isencodedhostname: function(host)
 
133
        {
 
134
                try {
 
135
                        return /^_p_[a-z2-7]+\..+$/.test(host);
 
136
                } catch (e) {
 
137
                        dump("url.isencodedhostname: failed with " + e + "\n");
 
138
                }
 
139
 
 
140
                return false;
 
141
        },
 
142
 
 
143
        encodehostname: function(host, path)
 
144
        {
 
145
                try {
 
146
                        if (!host || !path) {
 
147
                                return host;
 
148
                        }
 
149
 
 
150
                        /* Clean up the path, drop query string and hash */
 
151
                        path = path.replace(/^\s+/, "")
 
152
                                        .replace(/\s+$/, "")
 
153
                                        .replace(/[\?#].*$/, "");
 
154
 
 
155
                        if (path.length < 2 || path[0] != "/") {
 
156
                                return host;
 
157
                        }
 
158
 
 
159
                        var h = wot_idn.utftoidn(host);
 
160
 
 
161
                        if (!h) {
 
162
                                return host;
 
163
                        }
 
164
 
 
165
                        var c = path.split("/");
 
166
 
 
167
                        if (!c || !c.length) {
 
168
                                return host;
 
169
                        }
 
170
 
 
171
                        /* Drop a suspected filename from the end */
 
172
                        if (path[path.length - 1] != "/" &&
 
173
                                        /\.[^\.]{1,6}$/.test(c[c.length - 1])) {
 
174
                                c.pop();
 
175
                        }
 
176
 
 
177
                        var level = 0;
 
178
 
 
179
                        for (var i = c.length; !level && i > 0; --i) {
 
180
                                level = this.isshared(h + c.slice(0, i).join("/"));
 
181
                        }
 
182
 
 
183
                        if (!level) {
 
184
                                return host;
 
185
                        }
 
186
 
 
187
                        var p = c.slice(0, level + 1).join("/").replace(/^\//, "");
 
188
 
 
189
                        if (!p || !p.length) {
 
190
                                return host;
 
191
                        }
 
192
 
 
193
                        var encoded = this.base32encode(p);
 
194
 
 
195
                        if (encoded == null) {
 
196
                                return host;
 
197
                        }
 
198
 
 
199
                        return "_p_" + encoded + "." + host;
 
200
                } catch (e) {
 
201
                        dump("wot_shared.encodehostname: failed with " + e + "\n");
 
202
                }
 
203
 
 
204
                return host;
 
205
        },
 
206
 
 
207
        decodehostname: function(host)
 
208
        {
 
209
                try {
 
210
                        var m = /^_p_([a-z2-7]+)\.(.+)$/.exec(host);
 
211
 
 
212
                        if (!m || !m[1] || !m[2]) {
 
213
                                return host;
 
214
                        }
 
215
 
 
216
                        var decoded = this.base32decode(m[1]);
 
217
 
 
218
                        if (decoded == null) {
 
219
                                return host;
 
220
                        }
 
221
 
 
222
                        return m[2] + "/" + decoded;
 
223
                } catch (e) {
 
224
                        dump("wot_shared.decodehostname: failed with " + e + "\n");
 
225
                }
 
226
 
 
227
                return host;
 
228
        },
 
229
 
 
230
        base32encode: function(s)
 
231
        {
 
232
                try {
 
233
                        /* Unicode to UTF-8 */
 
234
                        s = unescape(encodeURIComponent(decodeURIComponent(s)));
 
235
 
 
236
                        var r = "";
 
237
                        var b = 0;
 
238
                        var l = 0;
 
239
 
 
240
                        for (var i = 0; i < s.length; ++i) {
 
241
                                var n = s.charCodeAt(i);
 
242
 
 
243
                                if (n > 255) {
 
244
                                        return null; /* Invalid input */
 
245
                                }
 
246
 
 
247
                                b = (b << 8) + n;
 
248
                                l += 8;
 
249
 
 
250
                                do {
 
251
                                        l -= 5;
 
252
                                        r += this.set[(b >> l) & 0x1F];
 
253
                                } while (l >= 5);
 
254
                        }
 
255
 
 
256
                        if (l > 0) {
 
257
                                r += this.set[(b << (5 - l)) & 0x1F];
 
258
                        }
 
259
 
 
260
                        return r;
 
261
                } catch (e) {
 
262
                        dump("wot_shared.base32encode: failed with " + e + "\n");
 
263
                }
 
264
                
 
265
                return null;
 
266
        },
 
267
 
 
268
        base32decode: function(s)
 
269
        {
 
270
                try {
 
271
                        /* Build a reverse lookup table */
 
272
                        if (!this.rev) {
 
273
                                this.rev = {};
 
274
 
 
275
                                for (var i = 0; i < this.set.length; ++i) {
 
276
                                        this.rev[this.set.charAt(i)] = i;
 
277
                                }
 
278
                        }
 
279
 
 
280
                        var r = "";
 
281
                        var b = 0;
 
282
                        var l = 0;
 
283
 
 
284
                        for (var i = 0; i < s.length; ++i) {
 
285
                                var n = this.rev[s.charAt(i)];
 
286
 
 
287
                                if (n == null) {
 
288
                                        return null; /* Invalid input */
 
289
                                }
 
290
 
 
291
                                b = (b << 5) + n;
 
292
                                l += 5;
 
293
 
 
294
                                while (l >= 8) {
 
295
                                        l -= 8;
 
296
                                        r += String.fromCharCode((b >> l) & 0xFF);
 
297
                                }
 
298
                        }
 
299
 
 
300
                        if (l >= 5) {
 
301
                                return null; /* Invalid input */
 
302
                        }
 
303
 
 
304
                        /* UTF-8 to Unicode */
 
305
                        return decodeURIComponent(escape(r));
 
306
                } catch (e) {
 
307
                        dump("wot_shared.base32decode: failed with " + e + "\n");
 
308
                }
 
309
 
 
310
                return null;
 
311
        },
 
312
};
 
313
 
 
314
wot_modules.push({ name: "wot_shared", obj: wot_shared });