browser/extensions/shumway/content/web/preview.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil; tab-width: 2 -*- */
michael@0 2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
michael@0 3 /*
michael@0 4 * Copyright 2013 Mozilla Foundation
michael@0 5 *
michael@0 6 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 7 * you may not use this file except in compliance with the License.
michael@0 8 * You may obtain a copy of the License at
michael@0 9 *
michael@0 10 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 11 *
michael@0 12 * Unless required by applicable law or agreed to in writing, software
michael@0 13 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 15 * See the License for the specific language governing permissions and
michael@0 16 * limitations under the License.
michael@0 17 */
michael@0 18
michael@0 19
michael@0 20 // Extenstion communication object
michael@0 21 var FirefoxCom = (function FirefoxComClosure() {
michael@0 22 return {
michael@0 23 /**
michael@0 24 * Creates an event that the extension is listening for and will
michael@0 25 * synchronously respond to.
michael@0 26 * NOTE: It is reccomended to use request() instead since one day we may not
michael@0 27 * be able to synchronously reply.
michael@0 28 * @param {String} action The action to trigger.
michael@0 29 * @param {String} data Optional data to send.
michael@0 30 * @return {*} The response.
michael@0 31 */
michael@0 32 requestSync: function(action, data) {
michael@0 33 var request = document.createTextNode('');
michael@0 34 document.documentElement.appendChild(request);
michael@0 35
michael@0 36 var sender = document.createEvent('CustomEvent');
michael@0 37 sender.initCustomEvent('shumway.message', true, false,
michael@0 38 {action: action, data: data, sync: true});
michael@0 39 request.dispatchEvent(sender);
michael@0 40 var response = sender.detail.response;
michael@0 41 document.documentElement.removeChild(request);
michael@0 42 return response;
michael@0 43 },
michael@0 44 /**
michael@0 45 * Creates an event that the extension is listening for and will
michael@0 46 * asynchronously respond by calling the callback.
michael@0 47 * @param {String} action The action to trigger.
michael@0 48 * @param {String} data Optional data to send.
michael@0 49 * @param {Function} callback Optional response callback that will be called
michael@0 50 * with one data argument.
michael@0 51 */
michael@0 52 request: function(action, data, callback) {
michael@0 53 var request = document.createTextNode('');
michael@0 54 request.setUserData('action', action, null);
michael@0 55 request.setUserData('data', data, null);
michael@0 56 request.setUserData('sync', false, null);
michael@0 57 if (callback) {
michael@0 58 request.setUserData('callback', callback, null);
michael@0 59
michael@0 60 document.addEventListener('shumway.response', function listener(event) {
michael@0 61 var node = event.target,
michael@0 62 response = event.detail.response;
michael@0 63
michael@0 64 document.documentElement.removeChild(node);
michael@0 65
michael@0 66 document.removeEventListener('shumway.response', listener, false);
michael@0 67 return callback(response);
michael@0 68 }, false);
michael@0 69 }
michael@0 70 document.documentElement.appendChild(request);
michael@0 71
michael@0 72 var sender = document.createEvent('CustomEvent');
michael@0 73 sender.initCustomEvent('shumway.message', true, false,
michael@0 74 {action: action, data: data, sync: false});
michael@0 75 return request.dispatchEvent(sender);
michael@0 76 }
michael@0 77 };
michael@0 78 })();
michael@0 79
michael@0 80 function fallback() {
michael@0 81 FirefoxCom.requestSync('fallback', null)
michael@0 82 }
michael@0 83
michael@0 84 var BYTES_TO_LOAD = 32768;
michael@0 85 var BYTES_TO_PARSE = 32768;
michael@0 86
michael@0 87 function runSniffer() {
michael@0 88 var flashParams = JSON.parse(FirefoxCom.requestSync('getPluginParams', null));
michael@0 89 document.head.getElementsByTagName('base')[0].href = flashParams.baseUrl;
michael@0 90 movieUrl = flashParams.url;
michael@0 91 document.getElementById('playbutton').addEventListener('click', function() {
michael@0 92 switchToFullMode();
michael@0 93 });
michael@0 94 document.getElementById('fullmode').addEventListener('click', function() {
michael@0 95 switchToFullMode();
michael@0 96 return false;
michael@0 97 });
michael@0 98 document.getElementById('fallback').addEventListener('click', function() {
michael@0 99 fallback();
michael@0 100 return false;
michael@0 101 });
michael@0 102 FirefoxCom.requestSync('loadFile', {url: movieUrl, sessionId: 0, limit: BYTES_TO_LOAD});
michael@0 103 }
michael@0 104
michael@0 105 var subscription, movieUrl, buffers = [];;
michael@0 106
michael@0 107 addEventListener("message", function handlerMessage(e) {
michael@0 108 var args = e.data;
michael@0 109 switch (args.callback) {
michael@0 110 case "loadFile":
michael@0 111 if (args.sessionId != 0) {
michael@0 112 return;
michael@0 113 }
michael@0 114 switch (args.topic) {
michael@0 115 case "progress":
michael@0 116 buffers.push(args.array);
michael@0 117 break;
michael@0 118 case "error":
michael@0 119 console.error('Unable to download ' + movieUrl + ': ' + args.error);
michael@0 120 break;
michael@0 121 case "close":
michael@0 122 parseSwf();
michael@0 123 break;
michael@0 124 }
michael@0 125 break;
michael@0 126 }
michael@0 127 }, true);
michael@0 128
michael@0 129 function inflateData(bytes, outputLength) {
michael@0 130 verifyDeflateHeader(bytes);
michael@0 131 var stream = new Stream(bytes, 2);
michael@0 132 var output = {
michael@0 133 data: new Uint8Array(outputLength),
michael@0 134 available: 0,
michael@0 135 completed: false
michael@0 136 };
michael@0 137 var state = {};
michael@0 138 // inflate while we can
michael@0 139 try {
michael@0 140 do {
michael@0 141 inflateBlock(stream, output, state);
michael@0 142 } while (!output.completed && stream.pos < stream.end
michael@0 143 && output.available < outputLength);
michael@0 144 } catch (e) {
michael@0 145 console.log('inflate aborted: ' + e);
michael@0 146 }
michael@0 147 return new Stream(output.data, 0, Math.min(output.available, outputLength));
michael@0 148 }
michael@0 149
michael@0 150 function parseSwf() {
michael@0 151 var sum = 0;
michael@0 152 for (var i = 0; i < buffers.length; i++)
michael@0 153 sum += buffers[i].length;
michael@0 154 var data = new Uint8Array(sum), j = 0;
michael@0 155 for (var i = 0; i < buffers.length; i++) {
michael@0 156 data.set(buffers[i], j); j += buffers[i].length;
michael@0 157 }
michael@0 158
michael@0 159 var backgroundColor;
michael@0 160 try {
michael@0 161 var magic1 = data[0];
michael@0 162 var magic2 = data[1];
michael@0 163 var magic3 = data[2];
michael@0 164 if ((magic1 !== 70 && magic1 !== 67) || magic2 !== 87 || magic3 !== 83)
michael@0 165 throw new Error('unsupported file format');
michael@0 166
michael@0 167 var compressed = magic1 === 67;
michael@0 168 var stream = compressed ? inflateData(data.subarray(8), BYTES_TO_PARSE) :
michael@0 169 new Stream(data, 8, data.length - 8);
michael@0 170 var bytes = stream.bytes;
michael@0 171
michael@0 172 var SWF_TAG_CODE_SET_BACKGROUND_COLOR = 9;
michael@0 173 var PREFETCH_SIZE = 17 /* RECT */ +
michael@0 174 4 /* Frames rate and count */;;
michael@0 175 stream.ensure(PREFETCH_SIZE);
michael@0 176 var rectFieldSize = bytes[stream.pos] >> 3;
michael@0 177 stream.pos += ((5 + 4 * rectFieldSize + 7) >> 3) + 4; // skipping other header fields
michael@0 178
michael@0 179 // for now just sniffing background color
michael@0 180 while (stream.pos < stream.end &&
michael@0 181 !backgroundColor) {
michael@0 182 stream.ensure(2);
michael@0 183 var tagCodeAndLength = stream.getUint16(stream.pos, true);
michael@0 184 stream.pos += 2;
michael@0 185 var tagCode = tagCodeAndLength >> 6;
michael@0 186 var length = tagCodeAndLength & 0x3F;
michael@0 187 if (length == 0x3F) {
michael@0 188 stream.ensure(4);
michael@0 189 length = stream.getInt32(stream.pos, true);
michael@0 190 stream.pos += 4;
michael@0 191 if (length < 0) throw new Error('invalid length');
michael@0 192 }
michael@0 193 stream.ensure(length);
michael@0 194 switch (tagCode) {
michael@0 195 case SWF_TAG_CODE_SET_BACKGROUND_COLOR:
michael@0 196 backgroundColor = 'rgb(' + bytes[stream.pos] + ', ' +
michael@0 197 bytes[stream.pos + 1] + ', ' +
michael@0 198 bytes[stream.pos + 2] + ')';
michael@0 199 break;
michael@0 200 }
michael@0 201 stream.pos += length;
michael@0 202 }
michael@0 203 } catch (e) {
michael@0 204 console.log('parsing aborted: ' + e);
michael@0 205 }
michael@0 206 if (backgroundColor) {
michael@0 207 document.body.style.backgroundColor = backgroundColor;
michael@0 208 }
michael@0 209 }
michael@0 210
michael@0 211 document.addEventListener('keydown', function (e) {
michael@0 212 if (e.keyCode == 119 && e.ctrlKey) { // Ctrl+F8
michael@0 213 window.location.replace("data:application/x-moz-playpreview;,application/x-shockwave-flash,full,paused=true");
michael@0 214 }
michael@0 215 }, false);
michael@0 216
michael@0 217 function switchToFullMode() {
michael@0 218 window.location.replace("data:application/x-moz-playpreview;,application/x-shockwave-flash,full");
michael@0 219 }

mercurial