OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Parts Copyright 2005-2009, the Dojo Foundation. Used under the terms of the |
| 6 // "New" BSD License: |
| 7 // |
| 8 // http://download.dojotoolkit.org/release-1.3.2/dojo-release-1.3.2/dojo/LICENS
E |
| 9 // |
| 10 |
| 11 /** |
| 12 * @fileoverview CFInstance.js provides a set of utilities for managing |
| 13 * ChromeFrame plugins, including creation, RPC services, and a singleton to |
| 14 * use for communicating from ChromeFrame hosted content to an external |
| 15 * CFInstance wrapper. CFInstance.js is stand-alone, designed to be served from |
| 16 * a CDN, and built to not create side-effects for other hosted content. |
| 17 * @author slightlyoff@google.com (Alex Russell) |
| 18 */ |
| 19 |
| 20 (function(scope) { |
| 21 // TODO: |
| 22 // * figure out if there's any way to install w/o a browser restart, and if |
| 23 // so, where and how |
| 24 // * slim down Deferred and RPC scripts |
| 25 // * determine what debugging APIs should be exposed and how they should be |
| 26 // surfaced. What about content authoring in Chrome instances? Stubbing |
| 27 // the other side of RPC's? |
| 28 |
| 29 // bail if we'd be over-writing an existing CFInstance object |
| 30 if (scope['CFInstance']) { |
| 31 return; |
| 32 } |
| 33 |
| 34 ///////////////////////////////////////////////////////////////////////////// |
| 35 // Utiliity and Cross-Browser Functions |
| 36 ///////////////////////////////////////////////////////////////////////////// |
| 37 |
| 38 // a monotonically incrementing counter |
| 39 var _counter = 0; |
| 40 |
| 41 var undefStr = 'undefined'; |
| 42 |
| 43 // |
| 44 // Browser detection: ua.isIE, ua.isSafari, ua.isOpera, etc. |
| 45 // |
| 46 |
| 47 /** |
| 48 * An object for User Agent detection |
| 49 * @type {!Object} |
| 50 * @protected |
| 51 */ |
| 52 var ua = {}; |
| 53 var n = navigator; |
| 54 var dua = String(n.userAgent); |
| 55 var dav = String(n.appVersion); |
| 56 var tv = parseFloat(dav); |
| 57 var duaParse = function(s){ |
| 58 var c = 0; |
| 59 try { |
| 60 return parseFloat( |
| 61 dua.split(s)[1].replace(/\./g, function() { |
| 62 c++; |
| 63 return (c > 1) ? '' : '.'; |
| 64 } ) |
| 65 ); |
| 66 } catch(e) { |
| 67 // squelch to intentionally return undefined |
| 68 } |
| 69 }; |
| 70 /** @type {number} */ |
| 71 ua.isOpera = dua.indexOf('Opera') >= 0 ? tv: undefined; |
| 72 /** @type {number} */ |
| 73 ua.isWebKit = duaParse('WebKit/'); |
| 74 /** @type {number} */ |
| 75 ua.isChrome = duaParse('Chrome/'); |
| 76 /** @type {number} */ |
| 77 ua.isKhtml = dav.indexOf('KHTML') >= 0 ? tv : undefined; |
| 78 |
| 79 var index = Math.max(dav.indexOf('WebKit'), dav.indexOf('Safari'), 0); |
| 80 |
| 81 if (index && !ua.isChrome) { |
| 82 /** @type {number} */ |
| 83 ua.isSafari = parseFloat(dav.split('Version/')[1]); |
| 84 if(!ua.isSafari || parseFloat(dav.substr(index + 7)) <= 419.3){ |
| 85 ua.isSafari = 2; |
| 86 } |
| 87 } |
| 88 |
| 89 if (dua.indexOf('Gecko') >= 0 && !ua.isKhtml) { |
| 90 /** @type {number} */ |
| 91 ua.isGecko = duaParse(' rv:'); |
| 92 } |
| 93 |
| 94 if (ua.isGecko) { |
| 95 /** @type {number} */ |
| 96 ua.isFF = parseFloat(dua.split('Firefox/')[1]) || undefined; |
| 97 } |
| 98 |
| 99 if (document.all && !ua.isOpera) { |
| 100 /** @type {number} */ |
| 101 ua.isIE = parseFloat(dav.split('MSIE ')[1]) || undefined; |
| 102 } |
| 103 |
| 104 |
| 105 /** |
| 106 * Log out varargs to a browser-provided console object (if available). Else |
| 107 * a no-op. |
| 108 * @param {*} var_args Optional Things to log. |
| 109 * @protected |
| 110 **/ |
| 111 var log = function() { |
| 112 if (window['console']) { |
| 113 try { |
| 114 if (ua.isSafari || ua.isChrome) { |
| 115 throw Error(); |
| 116 } |
| 117 console.log.apply(console, arguments); |
| 118 } catch(e) { |
| 119 try { |
| 120 console.log(toArray(arguments).join(' ')); |
| 121 } catch(e2) { |
| 122 // squelch |
| 123 } |
| 124 } |
| 125 } |
| 126 }; |
| 127 |
| 128 // |
| 129 // Language utility methods |
| 130 // |
| 131 |
| 132 /** |
| 133 * Determine if the passed item is a String |
| 134 * @param {*} item Item to test. |
| 135 * @protected |
| 136 **/ |
| 137 var isString = function(item) { |
| 138 return typeof item == 'string'; |
| 139 }; |
| 140 |
| 141 /** |
| 142 * Determine if the passed item is a Function object |
| 143 * @param {*} item Item to test. |
| 144 * @protected |
| 145 **/ |
| 146 var isFunction = function(item) { |
| 147 return ( |
| 148 item && ( |
| 149 typeof item == 'function' || item instanceof Function |
| 150 ) |
| 151 ); |
| 152 }; |
| 153 |
| 154 /** |
| 155 * Determine if the passed item is an array. |
| 156 * @param {*} item Item to test. |
| 157 * @protected |
| 158 **/ |
| 159 var isArray = function(item){ |
| 160 return ( |
| 161 item && ( |
| 162 item instanceof Array || ( |
| 163 typeof item == 'object' && |
| 164 typeof item.length != undefStr |
| 165 ) |
| 166 ) |
| 167 ); |
| 168 }; |
| 169 |
| 170 /** |
| 171 * A toArray version which takes advantage of builtins |
| 172 * @param {*} obj The array-like object to convert to a real array. |
| 173 * @param {number} opt_offset An index to being copying from in the source. |
| 174 * @param {Array} opt_startWith An array to extend with elements of obj in |
| 175 * lieu of creating a new array to return. |
| 176 * @private |
| 177 **/ |
| 178 var _efficientToArray = function(obj, opt_offset, opt_startWith){ |
| 179 return (opt_startWith || []).concat( |
| 180 Array.prototype.slice.call(obj, opt_offset || 0 ) |
| 181 ); |
| 182 }; |
| 183 |
| 184 /** |
| 185 * A version of toArray that iterates in lieu of using array generics. |
| 186 * @param {*} obj The array-like object to convert to a real array. |
| 187 * @param {number} opt_offset An index to being copying from in the source. |
| 188 * @param {Array} opt_startWith An array to extend with elements of obj in |
| 189 * @private |
| 190 **/ |
| 191 var _slowToArray = function(obj, opt_offset, opt_startWith){ |
| 192 var arr = opt_startWith || []; |
| 193 for(var x = opt_offset || 0; x < obj.length; x++){ |
| 194 arr.push(obj[x]); |
| 195 } |
| 196 return arr; |
| 197 }; |
| 198 |
| 199 /** |
| 200 * Converts an array-like object (e.g., an "arguments" object) to a real |
| 201 * Array. |
| 202 * @param {*} obj The array-like object to convert to a real array. |
| 203 * @param {number} opt_offset An index to being copying from in the source. |
| 204 * @param {Array} opt_startWith An array to extend with elements of obj in |
| 205 * @protected |
| 206 */ |
| 207 var toArray = ua.isIE ? |
| 208 function(obj){ |
| 209 return ( |
| 210 obj.item ? _slowToArray : _efficientToArray |
| 211 ).apply(this, arguments); |
| 212 } : |
| 213 _efficientToArray; |
| 214 |
| 215 var _getParts = function(arr, obj, cb){ |
| 216 return [ |
| 217 isString(arr) ? arr.split('') : arr, |
| 218 obj || window, |
| 219 isString(cb) ? new Function('item', 'index', 'array', cb) : cb |
| 220 ]; |
| 221 }; |
| 222 |
| 223 /** |
| 224 * like JS1.6 Array.forEach() |
| 225 * @param {Array} arr the array to iterate |
| 226 * @param {function(Object, number, Array)} callback the method to invoke for |
| 227 * each item in the array |
| 228 * @param {function?} thisObject Optional a scope to use with callback |
| 229 * @return {array} the original arr |
| 230 * @protected |
| 231 */ |
| 232 var forEach = function(arr, callback, thisObject) { |
| 233 if(!arr || !arr.length){ |
| 234 return arr; |
| 235 } |
| 236 var parts = _getParts(arr, thisObject, callback); |
| 237 // parts has a structure of: |
| 238 // [ |
| 239 // array, |
| 240 // scope, |
| 241 // function |
| 242 // ] |
| 243 arr = parts[0]; |
| 244 for (var i = 0, l = arr.length; i < l; ++i) { |
| 245 parts[2].call( parts[1], arr[i], i, arr ); |
| 246 } |
| 247 return arr; |
| 248 }; |
| 249 |
| 250 /** |
| 251 * returns a new function bound to scope with a variable number of positional |
| 252 * params pre-filled |
| 253 * @private |
| 254 */ |
| 255 var _hitchArgs = function(scope, method /*,...*/) { |
| 256 var pre = toArray(arguments, 2); |
| 257 var named = isString(method); |
| 258 return function() { |
| 259 var args = toArray(arguments); |
| 260 var f = named ? (scope || window)[method] : method; |
| 261 return f && f.apply(scope || this, pre.concat(args)); |
| 262 } |
| 263 }; |
| 264 |
| 265 /** |
| 266 * Like goog.bind(). Hitches the method (named or provided as a function |
| 267 * object) to scope, optionally partially applying positional arguments. |
| 268 * @param {Object} scope the object to hitch the method to |
| 269 * @param {string|function} method the method to be bound |
| 270 * @return {function} The bound method |
| 271 * @protected |
| 272 */ |
| 273 var hitch = function(scope, method){ |
| 274 if (arguments.length > 2) { |
| 275 return _hitchArgs.apply(window, arguments); // Function |
| 276 } |
| 277 |
| 278 if (!method) { |
| 279 method = scope; |
| 280 scope = null; |
| 281 } |
| 282 |
| 283 if (isString(method)) { |
| 284 scope = scope || window; |
| 285 if (!scope[method]) { |
| 286 throw( |
| 287 ['scope["', method, '"] is null (scope="', scope, '")'].join('') |
| 288 ); |
| 289 } |
| 290 return function() { |
| 291 return scope[method].apply(scope, arguments || []); |
| 292 }; |
| 293 } |
| 294 |
| 295 return !scope ? |
| 296 method : |
| 297 function() { |
| 298 return method.apply(scope, arguments || []); |
| 299 }; |
| 300 }; |
| 301 |
| 302 /** |
| 303 * A version of addEventListener that works on IE too. *sigh*. |
| 304 * @param {!Object} obj The object to attach to |
| 305 * @param {!String} type Name of the event to attach to |
| 306 * @param {!Function} handler The function to connect |
| 307 * @protected |
| 308 */ |
| 309 var listen = function(obj, type, handler) { |
| 310 if (obj['attachEvent']) { |
| 311 obj.attachEvent('on' + type, handler); |
| 312 } else { |
| 313 obj.addEventListener(type, handler, false); |
| 314 } |
| 315 }; |
| 316 |
| 317 /** |
| 318 * Adds "listen" and "_dispatch" methods to the passed object, taking |
| 319 * advantage of native event hanlding if it's available. |
| 320 * @param {Object} instance The object to install the event system on |
| 321 * @protected |
| 322 */ |
| 323 var installEvtSys = function(instance) { |
| 324 var eventsMap = {}; |
| 325 |
| 326 var isNative = ( |
| 327 (typeof instance.addEventListener != undefStr) && |
| 328 ((instance['tagName'] || '').toLowerCase() != 'iframe') |
| 329 ); |
| 330 |
| 331 instance.listen = function(type, func) { |
| 332 var t = eventsMap[type]; |
| 333 if (!t) { |
| 334 t = eventsMap[type] = []; |
| 335 if (isNative) { |
| 336 listen(instance, type, hitch(instance, "_dispatch", type)); |
| 337 } |
| 338 } |
| 339 t.push(func); |
| 340 return t; |
| 341 }; |
| 342 |
| 343 instance._dispatch = function(type, evt) { |
| 344 var stopped = false; |
| 345 var stopper = function() { |
| 346 stopped = true; |
| 347 }; |
| 348 |
| 349 forEach(eventsMap[type], function(f) { |
| 350 if (!stopped) { |
| 351 f(evt, stopper); |
| 352 } |
| 353 }); |
| 354 }; |
| 355 |
| 356 return instance; |
| 357 }; |
| 358 |
| 359 /** |
| 360 * Deserialize the passed JSON string |
| 361 * @param {!String} json A string to be deserialized |
| 362 * @return {Object} |
| 363 * @protected |
| 364 */ |
| 365 var fromJson = window['JSON'] ? function(json) { |
| 366 return JSON.parse(json); |
| 367 } : |
| 368 function(json) { |
| 369 return eval('(' + (json || undefStr) + ')'); |
| 370 }; |
| 371 |
| 372 /** |
| 373 * String escaping for use in JSON serialization |
| 374 * @param {string} str The string to escape |
| 375 * @return {string} |
| 376 * @private |
| 377 */ |
| 378 var _escapeString = function(str) { |
| 379 return ('"' + str.replace(/(["\\])/g, '\\$1') + '"'). |
| 380 replace(/[\f]/g, '\\f'). |
| 381 replace(/[\b]/g, '\\b'). |
| 382 replace(/[\n]/g, '\\n'). |
| 383 replace(/[\t]/g, '\\t'). |
| 384 replace(/[\r]/g, '\\r'). |
| 385 replace(/[\x0B]/g, '\\u000b'); // '\v' is not supported in JScript; |
| 386 }; |
| 387 |
| 388 /** |
| 389 * JSON serialization for arbitrary objects. Circular references or strong |
| 390 * typing information are not handled. |
| 391 * @param {Object} it Any valid JavaScript object or type |
| 392 * @return {string} the serialized representation of the passed object |
| 393 * @protected |
| 394 */ |
| 395 var toJson = window['JSON'] ? function(it) { |
| 396 return JSON.stringify(it); |
| 397 } : |
| 398 function(it) { |
| 399 |
| 400 if (it === undefined) { |
| 401 return undefStr; |
| 402 } |
| 403 |
| 404 var objtype = typeof it; |
| 405 if (objtype == 'number' || objtype == 'boolean') { |
| 406 return it + ''; |
| 407 } |
| 408 |
| 409 if (it === null) { |
| 410 return 'null'; |
| 411 } |
| 412 |
| 413 if (isString(it)) { |
| 414 return _escapeString(it); |
| 415 } |
| 416 |
| 417 // recurse |
| 418 var recurse = arguments.callee; |
| 419 |
| 420 if(it.nodeType && it.cloneNode){ // isNode |
| 421 // we can't seriailize DOM nodes as regular objects because they have |
| 422 // cycles DOM nodes could be serialized with something like outerHTML, |
| 423 // but that can be provided by users in the form of .json or .__json__ |
| 424 // function. |
| 425 throw new Error('Cannot serialize DOM nodes'); |
| 426 } |
| 427 |
| 428 // array |
| 429 if (isArray(it)) { |
| 430 var res = []; |
| 431 forEach(it, function(obj) { |
| 432 var val = recurse(obj); |
| 433 if (typeof val != 'string') { |
| 434 val = undefStr; |
| 435 } |
| 436 res.push(val); |
| 437 }); |
| 438 return '[' + res.join(',') + ']'; |
| 439 } |
| 440 |
| 441 if (objtype == 'function') { |
| 442 return null; |
| 443 } |
| 444 |
| 445 // generic object code path |
| 446 var output = []; |
| 447 for (var key in it) { |
| 448 var keyStr, val; |
| 449 if (typeof key == 'number') { |
| 450 keyStr = '"' + key + '"'; |
| 451 } else if(typeof key == 'string') { |
| 452 keyStr = _escapeString(key); |
| 453 } else { |
| 454 // skip non-string or number keys |
| 455 continue; |
| 456 } |
| 457 val = recurse(it[key]); |
| 458 if (typeof val != 'string') { |
| 459 // skip non-serializable values |
| 460 continue; |
| 461 } |
| 462 // TODO(slightlyoff): use += on Moz since it's faster there |
| 463 output.push(keyStr + ':' + val); |
| 464 } |
| 465 return '{' + output.join(',') + '}'; // String |
| 466 }; |
| 467 |
| 468 // code to register with the earliest safe onload-style handler |
| 469 |
| 470 var _loadedListenerList = []; |
| 471 var _loadedFired = false; |
| 472 |
| 473 /** |
| 474 * a default handler for document onload. When called (the first time), |
| 475 * iterates over the list of registered listeners, calling them in turn. |
| 476 * @private |
| 477 */ |
| 478 var documentLoaded = function() { |
| 479 if (!_loadedFired) { |
| 480 _loadedFired = true; |
| 481 forEach(_loadedListenerList, 'item();'); |
| 482 } |
| 483 }; |
| 484 |
| 485 if (document.addEventListener) { |
| 486 // NOTE: |
| 487 // due to a threading issue in Firefox 2.0, we can't enable |
| 488 // DOMContentLoaded on that platform. For more information, see: |
| 489 // http://trac.dojotoolkit.org/ticket/1704 |
| 490 if (ua.isWebKit > 525 || ua.isOpera || ua.isFF >= 3) { |
| 491 listen(document, 'DOMContentLoaded', documentLoaded); |
| 492 } |
| 493 // mainly for Opera 8.5, won't be fired if DOMContentLoaded fired already. |
| 494 // also used for FF < 3.0 due to nasty DOM race condition |
| 495 listen(window, 'load', documentLoaded); |
| 496 } else { |
| 497 // crazy hack for IE that relies on the "deferred" behavior of script |
| 498 // tags |
| 499 document.write( |
| 500 '<scr' + 'ipt defer src="//:" ' |
| 501 + 'onreadystatechange="if(this.readyState==\'complete\')' |
| 502 + '{ CFInstance._documentLoaded();}">' |
| 503 + '</scr' + 'ipt>' |
| 504 ); |
| 505 } |
| 506 |
| 507 // TODO(slightlyoff): known KHTML init issues are ignored for now |
| 508 |
| 509 // |
| 510 // DOM utility methods |
| 511 // |
| 512 |
| 513 /** |
| 514 * returns an item based on DOM ID. Optionally a doucment may be provided to |
| 515 * specify the scope to search in. If a node is passed, it's returned as-is. |
| 516 * @param {string|Node} id The ID of the node to be located or a node |
| 517 * @param {Node} doc Optional A document to search for id. |
| 518 * @return {Node} |
| 519 * @protected |
| 520 */ |
| 521 var byId = (ua.isIE || ua.isOpera) ? |
| 522 function(id, doc) { |
| 523 if (isString(id)) { |
| 524 doc = doc || document; |
| 525 var te = doc.getElementById(id); |
| 526 // attributes.id.value is better than just id in case the |
| 527 // user has a name=id inside a form |
| 528 if (te && te.attributes.id.value == id) { |
| 529 return te; |
| 530 } else { |
| 531 var elements = doc.all[id]; |
| 532 if (!elements || !elements.length) { |
| 533 return elements; |
| 534 } |
| 535 // if more than 1, choose first with the correct id |
| 536 var i=0; |
| 537 while (te = elements[i++]) { |
| 538 if (te.attributes.id.value == id) { |
| 539 return te; |
| 540 } |
| 541 } |
| 542 } |
| 543 } else { |
| 544 return id; // DomNode |
| 545 } |
| 546 } : |
| 547 function(id, doc) { |
| 548 return isString(id) ? (doc || document).getElementById(id) : id; |
| 549 }; |
| 550 |
| 551 |
| 552 /** |
| 553 * returns a unique DOM id which can be used to locate the node via byId(). |
| 554 * If the node already has an ID, it's used. If not, one is generated. Like |
| 555 * IE's uniqueID property. |
| 556 * @param {Node} node The element to create or fetch a unique ID for |
| 557 * @return {String} |
| 558 * @protected |
| 559 */ |
| 560 var getUid = function(node) { |
| 561 var u = 'cfUnique' + (_counter++); |
| 562 return (!node) ? u : ( node.id || node.uniqueID || (node.id = u) ); |
| 563 }; |
| 564 |
| 565 // |
| 566 // the Deferred class, borrowed from Twisted Python and Dojo |
| 567 // |
| 568 |
| 569 /** |
| 570 * A class that models a single response (past or future) to a question. |
| 571 * Multiple callbacks and error handlers may be added. If the response was |
| 572 * added in the past, adding callbacks has the effect of calling them |
| 573 * immediately. In this way, Deferreds simplify thinking about synchronous |
| 574 * vs. asynchronous programming in languages which don't have continuations |
| 575 * or generators which might otherwise provide syntax for deferring |
| 576 * operations. |
| 577 * @param {function} canceller Optional A function to be called when the |
| 578 * Deferred is canceled. |
| 579 * @param {number} timeout Optional How long to wait (in ms) before errback |
| 580 * is called with a timeout error. If no timeout is passed, the default |
| 581 * is 1hr. Passing -1 will disable any timeout. |
| 582 * @constructor |
| 583 * @public |
| 584 */ |
| 585 Deferred = function(/*Function?*/ canceller, timeout){ |
| 586 // example: |
| 587 // var deferred = new Deferred(); |
| 588 // setTimeout(function(){ deferred.callback({success: true}); }, 1000); |
| 589 // return deferred; |
| 590 this.chain = []; |
| 591 this.id = _counter++; |
| 592 this.fired = -1; |
| 593 this.paused = 0; |
| 594 this.results = [ null, null ]; |
| 595 this.canceller = canceller; |
| 596 // FIXME(slightlyoff): is it really smart to be creating this many timers? |
| 597 if (typeof timeout == 'number') { |
| 598 if (timeout <= 0) { |
| 599 timeout = 216000; // give it an hour |
| 600 } |
| 601 } |
| 602 this._timer = setTimeout( |
| 603 hitch(this, 'errback', new Error('timeout')), |
| 604 (timeout || 1000) |
| 605 ); |
| 606 this.silentlyCancelled = false; |
| 607 }; |
| 608 |
| 609 /** |
| 610 * Cancels a Deferred that has not yet received a value, or is waiting on |
| 611 * another Deferred as its value. If a canceller is defined, the canceller |
| 612 * is called. If the canceller did not return an error, or there was no |
| 613 * canceller, then the errback chain is started. |
| 614 * @public |
| 615 */ |
| 616 Deferred.prototype.cancel = function() { |
| 617 var err; |
| 618 if (this.fired == -1) { |
| 619 if (this.canceller) { |
| 620 err = this.canceller(this); |
| 621 } else { |
| 622 this.silentlyCancelled = true; |
| 623 } |
| 624 if (this.fired == -1) { |
| 625 if ( !(err instanceof Error) ) { |
| 626 var res = err; |
| 627 var msg = 'Deferred Cancelled'; |
| 628 if (err && err.toString) { |
| 629 msg += ': ' + err.toString(); |
| 630 } |
| 631 err = new Error(msg); |
| 632 err.dType = 'cancel'; |
| 633 err.cancelResult = res; |
| 634 } |
| 635 this.errback(err); |
| 636 } |
| 637 } else if ( |
| 638 (this.fired == 0) && |
| 639 (this.results[0] instanceof Deferred) |
| 640 ) { |
| 641 this.results[0].cancel(); |
| 642 } |
| 643 }; |
| 644 |
| 645 |
| 646 /** |
| 647 * internal function for providing a result. If res is an instance of Error, |
| 648 * we treat it like such and start the error chain. |
| 649 * @param {Object|Error} res the result |
| 650 * @private |
| 651 */ |
| 652 Deferred.prototype._resback = function(res) { |
| 653 if (this._timer) { |
| 654 clearTimeout(this._timer); |
| 655 } |
| 656 this.fired = res instanceof Error ? 1 : 0; |
| 657 this.results[this.fired] = res; |
| 658 this._fire(); |
| 659 }; |
| 660 |
| 661 /** |
| 662 * determine if the deferred has already been resolved |
| 663 * @return {boolean} |
| 664 * @private |
| 665 */ |
| 666 Deferred.prototype._check = function() { |
| 667 if (this.fired != -1) { |
| 668 if (!this.silentlyCancelled) { |
| 669 return 0; |
| 670 } |
| 671 this.silentlyCancelled = 0; |
| 672 return 1; |
| 673 } |
| 674 return 0; |
| 675 }; |
| 676 |
| 677 /** |
| 678 * Begin the callback sequence with a non-error value. |
| 679 * @param {Object|Error} res the result |
| 680 * @public |
| 681 */ |
| 682 Deferred.prototype.callback = function(res) { |
| 683 this._check(); |
| 684 this._resback(res); |
| 685 }; |
| 686 |
| 687 /** |
| 688 * Begin the callback sequence with an error result. |
| 689 * @param {Error|string} res the result. If not an Error, it's treated as the |
| 690 * message for a new Error. |
| 691 * @public |
| 692 */ |
| 693 Deferred.prototype.errback = function(res) { |
| 694 this._check(); |
| 695 if ( !(res instanceof Error) ) { |
| 696 res = new Error(res); |
| 697 } |
| 698 this._resback(res); |
| 699 }; |
| 700 |
| 701 /** |
| 702 * Add a single function as the handler for both callback and errback, |
| 703 * allowing you to specify a scope (unlike addCallbacks). |
| 704 * @param {function|Object} cb A function. If cbfn is passed, the value of cb |
| 705 * is treated as a scope |
| 706 * @param {function|string} cbfn Optional A function or name of a function in |
| 707 * the scope cb. |
| 708 * @return {Deferred} this |
| 709 * @public |
| 710 */ |
| 711 Deferred.prototype.addBoth = function(cb, cbfn) { |
| 712 var enclosed = hitch.apply(window, arguments); |
| 713 return this.addCallbacks(enclosed, enclosed); |
| 714 }; |
| 715 |
| 716 /** |
| 717 * Add a single callback to the end of the callback sequence. Add a function |
| 718 * as the handler for successful resolution of the Deferred. May be called |
| 719 * multiple times to register many handlers. Note that return values are |
| 720 * chained if provided, so it's best for callback handlers not to return |
| 721 * anything. |
| 722 * @param {function|Object} cb A function. If cbfn is passed, the value of cb |
| 723 * is treated as a scope |
| 724 * @param {function|string} cbfn Optional A function or name of a function in |
| 725 * the scope cb. |
| 726 * @return {Deferred} this |
| 727 * @public |
| 728 */ |
| 729 Deferred.prototype.addCallback = function(cb, cbfn /*...*/) { |
| 730 return this.addCallbacks(hitch.apply(window, arguments)); |
| 731 }; |
| 732 |
| 733 |
| 734 /** |
| 735 * Add a function as the handler for errors in the Deferred. May be called |
| 736 * multiple times to add multiple error handlers. |
| 737 * @param {function|Object} cb A function. If cbfn is passed, the value of cb |
| 738 * is treated as a scope |
| 739 * @param {function|string} cbfn Optional A function or name of a function in |
| 740 * the scope cb. |
| 741 * @return {Deferred} this |
| 742 * @public |
| 743 */ |
| 744 Deferred.prototype.addErrback = function(cb, cbfn) { |
| 745 return this.addCallbacks(null, hitch.apply(window, arguments)); |
| 746 }; |
| 747 |
| 748 /** |
| 749 * Add a functions as handlers for callback and errback in a single shot. |
| 750 * @param {function} callback A function |
| 751 * @param {function} errback A function |
| 752 * @return {Deferred} this |
| 753 * @public |
| 754 */ |
| 755 Deferred.prototype.addCallbacks = function(callback, errback) { |
| 756 this.chain.push([callback, errback]); |
| 757 if (this.fired >= 0) { |
| 758 this._fire(); |
| 759 } |
| 760 return this; |
| 761 }; |
| 762 |
| 763 /** |
| 764 * when this Deferred is satisfied, pass it on to def, allowing it to run. |
| 765 * @param {Deferred} def A deferred to add to the end of this Deferred in a ch
ain |
| 766 * @return {Deferred} this |
| 767 * @public |
| 768 */ |
| 769 Deferred.prototype.chain = function(def) { |
| 770 this.addCallbacks(def.callback, def.errback); |
| 771 return this; |
| 772 }; |
| 773 |
| 774 /** |
| 775 * Used internally to exhaust the callback sequence when a result is |
| 776 * available. |
| 777 * @private |
| 778 */ |
| 779 Deferred.prototype._fire = function() { |
| 780 var chain = this.chain; |
| 781 var fired = this.fired; |
| 782 var res = this.results[fired]; |
| 783 var cb = null; |
| 784 while ((chain.length > 0) && (this.paused == 0)) { |
| 785 var f = chain.shift()[fired]; |
| 786 if (!f) { |
| 787 continue; |
| 788 } |
| 789 var func = hitch(this, function() { |
| 790 var ret = f(res); |
| 791 //If no response, then use previous response. |
| 792 if (typeof ret != undefStr) { |
| 793 res = ret; |
| 794 } |
| 795 fired = res instanceof Error ? 1 : 0; |
| 796 if (res instanceof Deferred) { |
| 797 cb = function(res) { |
| 798 this._resback(res); |
| 799 // inlined from _pause() |
| 800 this.paused--; |
| 801 if ( (this.paused == 0) && (this.fired >= 0)) { |
| 802 this._fire(); |
| 803 } |
| 804 } |
| 805 // inlined from _unpause |
| 806 this.paused++; |
| 807 } |
| 808 }); |
| 809 |
| 810 try { |
| 811 func.call(this); |
| 812 } catch(err) { |
| 813 fired = 1; |
| 814 res = err; |
| 815 } |
| 816 } |
| 817 |
| 818 this.fired = fired; |
| 819 this.results[fired] = res; |
| 820 if (cb && this.paused ) { |
| 821 // this is for "tail recursion" in case the dependent |
| 822 // deferred is already fired |
| 823 res.addBoth(cb); |
| 824 } |
| 825 }; |
| 826 |
| 827 ///////////////////////////////////////////////////////////////////////////// |
| 828 // Plugin Initialization Class and Helper Functions |
| 829 ///////////////////////////////////////////////////////////////////////////// |
| 830 |
| 831 var returnFalse = function() { |
| 832 return false; |
| 833 }; |
| 834 |
| 835 var cachedHasVideo; |
| 836 var cachedHasAudio; |
| 837 |
| 838 var contentTests = { |
| 839 canvas: function() { |
| 840 return !!( |
| 841 ua.isChrome || ua.isSafari >= 3 || ua.isFF >= 3 || ua.isOpera >= 9.2 |
| 842 ); |
| 843 }, |
| 844 |
| 845 svg: function() { |
| 846 return !!(ua.isChrome || ua.isSafari || ua.isFF || ua.isOpera); |
| 847 }, |
| 848 |
| 849 postMessage: function() { |
| 850 return ( |
| 851 !!window['postMessage'] || |
| 852 ua.isChrome || |
| 853 ua.isIE >= 8 || |
| 854 ua.isSafari >= 3 || |
| 855 ua.isFF >= 3 || |
| 856 ua.isOpera >= 9.2 |
| 857 ); |
| 858 }, |
| 859 |
| 860 // the spec isn't settled and nothing currently supports it |
| 861 websocket: returnFalse, |
| 862 |
| 863 'css-anim': function() { |
| 864 // pretty much limited to WebKit's special transition and animation |
| 865 // properties. Need to figure out a better way to triangulate this as |
| 866 // FF3.x adds more of these properties in parallel. |
| 867 return ua.isWebKit > 500; |
| 868 }, |
| 869 |
| 870 // "working" video/audio tag? |
| 871 video: function() { |
| 872 if (typeof cachedHasVideo != undefStr) { |
| 873 return cachedHasVideo; |
| 874 } |
| 875 |
| 876 // We haven't figured it out yet, so probe the <video> tag and cache the |
| 877 // result. |
| 878 var video = document.createElement('video'); |
| 879 return cachedHasVideo = (typeof video['play'] != undefStr); |
| 880 }, |
| 881 |
| 882 audio: function() { |
| 883 if (typeof cachedHasAudio != undefStr) { |
| 884 return cachedHasAudio; |
| 885 } |
| 886 |
| 887 var audio = document.createElement('audio'); |
| 888 return cachedHasAudio = (typeof audio['play'] != undefStr); |
| 889 }, |
| 890 |
| 891 'video-theora': function() { |
| 892 return contentTests.video() && (ua.isChrome || ua.isFF > 3); |
| 893 }, |
| 894 |
| 895 'video-h264': function() { |
| 896 return contentTests.video() && (ua.isChrome || ua.isSafari >= 4); |
| 897 }, |
| 898 |
| 899 'audio-vorbis': function() { |
| 900 return contentTests.audio() && (ua.isChrome || ua.isFF > 3); |
| 901 }, |
| 902 |
| 903 'audio-mp3': function() { |
| 904 return contentTests.audio() && (ua.isChrome || ua.isSafari >= 4); |
| 905 }, |
| 906 |
| 907 // can we implement RPC over available primitives? |
| 908 rpc: function() { |
| 909 // on IE we need the src to be on the same domain or we need postMessage |
| 910 // to work. Since we can't count on the src being same-domain, we look |
| 911 // for things that have postMessage. We may re-visit this later and add |
| 912 // same-domain checking and cross-window-call-as-postMessage-replacement |
| 913 // code. |
| 914 |
| 915 // use "!!" to avoid null-is-an-object weirdness |
| 916 return !!window['postMessage']; |
| 917 }, |
| 918 |
| 919 sql: function() { |
| 920 // HTML 5 databases |
| 921 return !!window['openDatabase']; |
| 922 }, |
| 923 |
| 924 storage: function(){ |
| 925 // DOM storage |
| 926 |
| 927 // IE8, Safari, etc. support "localStorage", FF supported "globalStorage" |
| 928 return !!window['globalStorage'] || !!window['localStorage']; |
| 929 } |
| 930 }; |
| 931 |
| 932 // isIE, isFF, isWebKit, etc. |
| 933 forEach([ |
| 934 'isOpera', 'isWebKit', 'isChrome', 'isKhtml', 'isSafari', |
| 935 'isGecko', 'isFF', 'isIE' |
| 936 ], |
| 937 function(name) { |
| 938 contentTests[name] = function() { |
| 939 return !!ua[name]; |
| 940 }; |
| 941 } |
| 942 ); |
| 943 |
| 944 /** |
| 945 * Checks the list of requirements to determine if the current host browser |
| 946 * meets them natively. Primarialy relies on the contentTests array. |
| 947 * @param {Array} reqs A list of tests, either names of test functions in |
| 948 * contentTests or functions to execute. |
| 949 * @return {boolean} |
| 950 * @private |
| 951 */ |
| 952 var testRequirements = function(reqs) { |
| 953 // never use CF on Chrome or Safari |
| 954 if (ua.isChrome || ua.isSafari) { |
| 955 return true; |
| 956 } |
| 957 |
| 958 var allMatch = true; |
| 959 if (!reqs) { |
| 960 return false; |
| 961 } |
| 962 forEach(reqs, function(i) { |
| 963 var matches = false; |
| 964 if (isFunction(i)) { |
| 965 // support custom test functions |
| 966 matches = i(); |
| 967 } else { |
| 968 // else it's a lookup by name |
| 969 matches = (!!contentTests[i] && contentTests[i]()); |
| 970 } |
| 971 allMatch = allMatch && matches; |
| 972 }); |
| 973 return allMatch; |
| 974 }; |
| 975 |
| 976 var cachedAvailable; |
| 977 |
| 978 /** |
| 979 * Checks to find out if ChromeFrame is available as a plugin |
| 980 * @return {Boolean} |
| 981 * @private |
| 982 */ |
| 983 var isCfAvailable = function() { |
| 984 if (typeof cachedAvailable != undefStr) { |
| 985 return cachedAvailable; |
| 986 } |
| 987 |
| 988 cachedAvailable = false; |
| 989 var p = n.plugins; |
| 990 if (typeof window['ActiveXObject'] != undefStr) { |
| 991 try { |
| 992 var i = new ActiveXObject('ChromeTab.ChromeFrame'); |
| 993 if (i) { |
| 994 cachedAvailable = true; |
| 995 } |
| 996 } catch(e) { |
| 997 log('ChromeFrame not available, error:', e.message); |
| 998 // squelch |
| 999 } |
| 1000 } else { |
| 1001 for (var x = 0; x < p.length; x++) { |
| 1002 if (p[x].name.indexOf('Google Chrome Frame') == 0) { |
| 1003 cachedAvailable = true; |
| 1004 break; |
| 1005 } |
| 1006 } |
| 1007 } |
| 1008 return cachedAvailable; |
| 1009 }; |
| 1010 |
| 1011 /** |
| 1012 * Creates a <param> element with the specified name and value. If a parent |
| 1013 * is provided, the <param> element is appended to it. |
| 1014 * @param {string} name The name of the param |
| 1015 * @param {string} value The value |
| 1016 * @param {Node} parent Optional parent element |
| 1017 * @return {Boolean} |
| 1018 * @private |
| 1019 */ |
| 1020 var param = function(name, value, parent) { |
| 1021 var p = document.createElement('param'); |
| 1022 p.setAttribute('name', name); |
| 1023 p.setAttribute('value', value); |
| 1024 if (parent) { |
| 1025 parent.appendChild(p); |
| 1026 } |
| 1027 return p; |
| 1028 }; |
| 1029 |
| 1030 /** @type {boolean} */ |
| 1031 var cfStyleTagInjected = false; |
| 1032 |
| 1033 /** |
| 1034 * Creates a style sheet in the document which provides default styling for |
| 1035 * ChromeFrame instances. Successive calls should have no additive effect. |
| 1036 * @private |
| 1037 */ |
| 1038 var injectCFStyleTag = function() { |
| 1039 if (cfStyleTagInjected) { |
| 1040 // once and only once |
| 1041 return; |
| 1042 } |
| 1043 try { |
| 1044 var rule = ['.chromeFrameDefaultStyle {', |
| 1045 'width: 400px;', |
| 1046 'height: 300px;', |
| 1047 'padding: 0;', |
| 1048 'margin: 0;', |
| 1049 '}'].join(''); |
| 1050 var ss = document.createElement('style'); |
| 1051 ss.setAttribute('type', 'text/css'); |
| 1052 if (ss.styleSheet) { |
| 1053 ss.styleSheet.cssText = rule; |
| 1054 } else { |
| 1055 ss.appendChild(document.createTextNode(rule)); |
| 1056 } |
| 1057 var h = document.getElementsByTagName('head')[0]; |
| 1058 if (h.firstChild) { |
| 1059 h.insertBefore(ss, h.firstChild); |
| 1060 } else { |
| 1061 h.appendChild(ss); |
| 1062 } |
| 1063 cfStyleTagInjected = true; |
| 1064 } catch (e) { |
| 1065 // squelch |
| 1066 |
| 1067 // FIXME(slightlyoff): log? retry? |
| 1068 } |
| 1069 }; |
| 1070 |
| 1071 /** |
| 1072 * Plucks properties from the passed arguments and sets them on the passed |
| 1073 * DOM node |
| 1074 * @param {Node} node The node to set properties on |
| 1075 * @param {Object} args A map of user-specified properties to set |
| 1076 * @private |
| 1077 */ |
| 1078 var setProperties = function(node, args) { |
| 1079 injectCFStyleTag(); |
| 1080 |
| 1081 var srcNode = byId(args['node']); |
| 1082 |
| 1083 node.id = args['id'] || (srcNode ? srcNode['id'] || getUid(srcNode) : ''); |
| 1084 |
| 1085 // TODO(slightlyoff): Opera compat? need to test there |
| 1086 var cssText = args['cssText'] || ''; |
| 1087 node.style.cssText = ' ' + cssText; |
| 1088 |
| 1089 var classText = args['className'] || ''; |
| 1090 node.className = 'chromeFrameDefaultStyle ' + classText; |
| 1091 |
| 1092 // default if the browser doesn't so we don't show sad-tab |
| 1093 var src = args['src'] || 'about:blank'; |
| 1094 |
| 1095 if (ua.isIE || ua.isOpera) { |
| 1096 node.src = src; |
| 1097 } else { |
| 1098 // crazyness regarding when things are set in NPAPI |
| 1099 node.setAttribute('src', src); |
| 1100 } |
| 1101 |
| 1102 if (srcNode) { |
| 1103 srcNode.parentNode.replaceChild(node, srcNode); |
| 1104 } |
| 1105 }; |
| 1106 |
| 1107 /** |
| 1108 * Creates a plugin instance, taking named parameters from the passed args. |
| 1109 * @param {Object} args A bag of configuration properties, including values |
| 1110 * like 'node', 'cssText', 'className', 'id', 'src', etc. |
| 1111 * @return {Node} |
| 1112 * @private |
| 1113 */ |
| 1114 var makeCFPlugin = function(args) { |
| 1115 var el; // the element |
| 1116 if (!ua.isIE) { |
| 1117 el = document.createElement('object'); |
| 1118 el.setAttribute("type", "application/chromeframe"); |
| 1119 } else { |
| 1120 var dummy = document.createElement('span'); |
| 1121 dummy.innerHTML = [ |
| 1122 '<object codeBase="//www.google.com"', |
| 1123 "type='application/chromeframe'", |
| 1124 'classid="CLSID:E0A900DF-9611-4446-86BD-4B1D47E7DB2A"></object>' |
| 1125 ].join(' '); |
| 1126 el = dummy.firstChild; |
| 1127 } |
| 1128 setProperties(el, args); |
| 1129 return el; |
| 1130 }; |
| 1131 |
| 1132 /** |
| 1133 * Creates an iframe in lieu of a ChromeFrame plugin, taking named parameters |
| 1134 * from the passed args. |
| 1135 * @param {Object} args A bag of configuration properties, including values |
| 1136 * like 'node', 'cssText', 'className', 'id', 'src', etc. |
| 1137 * @return {Node} |
| 1138 * @private |
| 1139 */ |
| 1140 var makeCFIframe = function(args) { |
| 1141 var el = document.createElement('iframe'); |
| 1142 setProperties(el, args); |
| 1143 // FIXME(slightlyoff): |
| 1144 // This is where we'll need to slot in "don't fire load events for |
| 1145 // fallback URL" logic. |
| 1146 listen(el, 'load', hitch(el, '_dispatch', 'load')); |
| 1147 return el; |
| 1148 }; |
| 1149 |
| 1150 |
| 1151 var msgPrefix = 'CFInstance.rpc:'; |
| 1152 |
| 1153 /** |
| 1154 * A class that provides the ability for widget-mode hosted content to more |
| 1155 * easily call hosting-page exposed APIs (and vice versa). It builds upon the |
| 1156 * message-passing nature of ChromeFrame to route messages to the other |
| 1157 * side's RPC host and coordinate events such as 'RPC readyness', buffering |
| 1158 * calls until both sides indicate they are ready to participate. |
| 1159 * @constructor |
| 1160 * @public |
| 1161 */ |
| 1162 var RPC = function(instance) { |
| 1163 this.initDeferred = new Deferred(); |
| 1164 |
| 1165 this.instance = instance; |
| 1166 |
| 1167 instance.listen('message', hitch(this, '_handleMessage')); |
| 1168 |
| 1169 this._localExposed = {}; |
| 1170 this._doWithAckCallbacks = {}; |
| 1171 |
| 1172 this._open = false; |
| 1173 this._msgBacklog = []; |
| 1174 |
| 1175 this._initialized = false; |
| 1176 this._exposeMsgBacklog = []; |
| 1177 |
| 1178 this._exposed = false; |
| 1179 this._callRemoteMsgBacklog = []; |
| 1180 |
| 1181 this._inFlight = {}; |
| 1182 |
| 1183 var sendLoadMsg = hitch(this, function(evt) { |
| 1184 this.doWithAck('load').addCallback(this, function() { |
| 1185 this._open = true; |
| 1186 this._postMessageBacklog(); |
| 1187 }); |
| 1188 }); |
| 1189 |
| 1190 if (instance['tagName']) { |
| 1191 instance.listen('load', sendLoadMsg); |
| 1192 } else { |
| 1193 sendLoadMsg(); |
| 1194 } |
| 1195 }; |
| 1196 |
| 1197 RPC.prototype._postMessageBacklog = function() { |
| 1198 if (this._open) { |
| 1199 forEach(this._msgBacklog, this._postMessage, this); |
| 1200 this._msgBacklog = []; |
| 1201 } |
| 1202 }; |
| 1203 |
| 1204 RPC.prototype._postMessage = function(msg, force) { |
| 1205 if (!force && !this._open) { |
| 1206 this._msgBacklog.push(msg); |
| 1207 } else { |
| 1208 // FIXME(slightlyoff): need to check domains list here! |
| 1209 this.instance.postMessage(msgPrefix + msg, '*'); |
| 1210 } |
| 1211 }; |
| 1212 |
| 1213 // currently no-ops. We may need them in the future |
| 1214 // RPC.prototype._doWithAck_load = function() { }; |
| 1215 // RPC.prototype._doWithAck_init = function() { }; |
| 1216 |
| 1217 RPC.prototype._doWithAck = function(what) { |
| 1218 var f = this['_doWithAck_' + what]; |
| 1219 if (f) { |
| 1220 f.call(this); |
| 1221 } |
| 1222 |
| 1223 this._postMessage('doWithAckCallback:' + what, what == 'load'); |
| 1224 }; |
| 1225 |
| 1226 RPC.prototype.doWithAck = function(what) { |
| 1227 var d = new Deferred(); |
| 1228 this._doWithAckCallbacks[what] = d; |
| 1229 this._postMessage('doWithAck:' + what, what == 'load'); |
| 1230 return d; |
| 1231 }; |
| 1232 |
| 1233 RPC.prototype._handleMessage = function(evt, stopper) { |
| 1234 var d = String(evt.data); |
| 1235 |
| 1236 if (d.indexOf(msgPrefix) != 0) { |
| 1237 // not for us, allow the event dispatch to continue... |
| 1238 return; |
| 1239 } |
| 1240 |
| 1241 // ...else we're the end of the line for this event |
| 1242 stopper(); |
| 1243 |
| 1244 // see if we know what type of message it is |
| 1245 d = d.substr(msgPrefix.length); |
| 1246 |
| 1247 var cIndex = d.indexOf(':'); |
| 1248 |
| 1249 var type = d.substr(0, cIndex); |
| 1250 |
| 1251 if (type == 'doWithAck') { |
| 1252 this._doWithAck(d.substr(cIndex + 1)); |
| 1253 return; |
| 1254 } |
| 1255 |
| 1256 var msgBody = d.substr(cIndex + 1); |
| 1257 |
| 1258 if (type == 'doWithAckCallback') { |
| 1259 this._doWithAckCallbacks[msgBody].callback(1); |
| 1260 return; |
| 1261 } |
| 1262 |
| 1263 if (type == 'init') { |
| 1264 return; |
| 1265 } |
| 1266 |
| 1267 // All the other stuff we can do uses a JSON payload. |
| 1268 var obj = fromJson(msgBody); |
| 1269 |
| 1270 if (type == 'callRemote') { |
| 1271 |
| 1272 if (obj.method && obj.params && obj.id) { |
| 1273 |
| 1274 var ret = { |
| 1275 success: 0, |
| 1276 returnId: obj.id |
| 1277 }; |
| 1278 |
| 1279 try { |
| 1280 // Undefined isn't valid JSON, so use null as default value. |
| 1281 ret.value = this._localExposed[ obj.method ](evt, obj) || null; |
| 1282 ret.success = 1; |
| 1283 } catch(e) { |
| 1284 ret.error = e.message; |
| 1285 } |
| 1286 |
| 1287 this._postMessage('callReturn:' + toJson(ret)); |
| 1288 } |
| 1289 } |
| 1290 |
| 1291 if (type == 'callReturn') { |
| 1292 // see if we're waiting on an outstanding RPC call, which |
| 1293 // would be identified by returnId. |
| 1294 var rid = obj['returnId']; |
| 1295 if (!rid) { |
| 1296 // throw an error? |
| 1297 return; |
| 1298 } |
| 1299 var callWrap = this._inFlight[rid]; |
| 1300 if (!callWrap) { |
| 1301 return; |
| 1302 } |
| 1303 |
| 1304 if (obj.success) { |
| 1305 callWrap.d.callback(obj['value'] || 1); |
| 1306 } else { |
| 1307 callWrap.d.errback(new Error(obj['error'] || 'unspecified RPC error')); |
| 1308 } |
| 1309 delete this._inFlight[rid]; |
| 1310 } |
| 1311 |
| 1312 }; |
| 1313 |
| 1314 /** |
| 1315 * Makes a method visible to be called |
| 1316 * @param {string} name The name to expose the method at. |
| 1317 * @param {Function|string} method The function (or name of the function) to |
| 1318 * expose. If a name is provided, it's looked up from the passed scope. |
| 1319 * If no scope is provided, the global scope is queried for a function |
| 1320 * with that name. |
| 1321 * @param {Object} scope Optional A scope to bind the passed method to. If |
| 1322 * the method parameter is specified by a string, the method is both |
| 1323 * located on the passed scope and bound to it. |
| 1324 * @param {Array} domains Optional A list of domains in |
| 1325 * 'http://example.com:8080' format which may call the given method. |
| 1326 * Currently un-implemented. |
| 1327 * @public |
| 1328 */ |
| 1329 RPC.prototype.expose = function(name, method, scope, domains) { |
| 1330 scope = scope || window; |
| 1331 method = isString(method) ? scope[method] : method; |
| 1332 |
| 1333 // local call proxy that the other side will hit when calling our method |
| 1334 this._localExposed[name] = function(evt, obj) { |
| 1335 return method.apply(scope, obj.params); |
| 1336 }; |
| 1337 |
| 1338 if (!this._initialized) { |
| 1339 this._exposeMsgBacklog.push(arguments); |
| 1340 return; |
| 1341 } |
| 1342 |
| 1343 var a = [name, method, scope, domains]; |
| 1344 this._sendExpose.apply(this, a); |
| 1345 }; |
| 1346 |
| 1347 RPC.prototype._sendExpose = function(name) { |
| 1348 // now tell the other side that we're advertising this method |
| 1349 this._postMessage('expose:' + toJson({ name: name })); |
| 1350 }; |
| 1351 |
| 1352 |
| 1353 /** |
| 1354 * Calls a remote method asynchronously and returns a Deferred object |
| 1355 * representing the response. |
| 1356 * @param {string} method Name of the method to call. Should be the same name |
| 1357 * which the other side has expose()'d. |
| 1358 * @param {Array} params Optional A list of arguments to pass to the called |
| 1359 * method. All elements in the list must be cleanly serializable to |
| 1360 * JSON. |
| 1361 * @param {CFInstance.Deferred} deferred Optional A Deferred to use for |
| 1362 * reporting the response of the call. If no deferred is passed, a new |
| 1363 * Deferred is created and returned. |
| 1364 * @return {CFInstance.Deferred} |
| 1365 * @public |
| 1366 */ |
| 1367 RPC.prototype.callRemote = function(method, params, timeout, deferred) { |
| 1368 var d = deferred || new Deferred(null, timeout || -1); |
| 1369 |
| 1370 if (!this._exposed) { |
| 1371 var args = toArray(arguments); |
| 1372 args.length = 3; |
| 1373 args.push(d); |
| 1374 this._callRemoteMsgBacklog.push(args); |
| 1375 return d; |
| 1376 } |
| 1377 |
| 1378 |
| 1379 if (!method) { |
| 1380 d.errback('no method provided!'); |
| 1381 return d; |
| 1382 } |
| 1383 |
| 1384 var id = msgPrefix + (_counter++); |
| 1385 |
| 1386 // JSON-ify the whole bundle |
| 1387 var callWrapper = { |
| 1388 method: String(method), |
| 1389 params: params || [], |
| 1390 id: id |
| 1391 }; |
| 1392 var callJson = toJson(callWrapper); |
| 1393 callWrapper.d = d; |
| 1394 this._inFlight[id] = callWrapper; |
| 1395 this._postMessage('callRemote:' + callJson); |
| 1396 return d; |
| 1397 }; |
| 1398 |
| 1399 |
| 1400 /** |
| 1401 * Tells the other side of the connection that we're ready to start receiving |
| 1402 * calls. Returns a Deferred that is called back when both sides have |
| 1403 * initialized and any backlogged messages have been sent. RPC users should |
| 1404 * generally work to make sure that they call expose() on all of the methods |
| 1405 * they'd like to make available to the other side *before* calling init() |
| 1406 * @return {CFInstance.Deferred} |
| 1407 * @public |
| 1408 */ |
| 1409 RPC.prototype.init = function() { |
| 1410 var d = this.initDeferred; |
| 1411 this.doWithAck('init').addCallback(this, function() { |
| 1412 // once the init floodgates are open, send our backlogs one at a time, |
| 1413 // with a little lag in the middle to prevent ordering problems |
| 1414 |
| 1415 this._initialized = true; |
| 1416 while (this._exposeMsgBacklog.length) { |
| 1417 this.expose.apply(this, this._exposeMsgBacklog.shift()); |
| 1418 } |
| 1419 |
| 1420 setTimeout(hitch(this, function(){ |
| 1421 |
| 1422 this._exposed = true; |
| 1423 while (this._callRemoteMsgBacklog.length) { |
| 1424 this.callRemote.apply(this, this._callRemoteMsgBacklog.shift()); |
| 1425 } |
| 1426 |
| 1427 d.callback(1); |
| 1428 |
| 1429 }), 30); |
| 1430 |
| 1431 }); |
| 1432 return d; |
| 1433 }; |
| 1434 |
| 1435 // CFInstance design notes: |
| 1436 // |
| 1437 // The CFInstance constructor is only ever used in host environments. In |
| 1438 // content pages (things hosted by a ChromeFrame instance), CFInstance |
| 1439 // acts as a singleton which provides services like RPC for communicating |
| 1440 // with it's mirror-image object in the hosting environment. We want the |
| 1441 // same methods and properties to be available on *instances* of |
| 1442 // CFInstance objects in the host env as on the singleton in the hosted |
| 1443 // content, despite divergent implementation. |
| 1444 // |
| 1445 // Further complicating things, CFInstance may specialize behavior |
| 1446 // internally based on whether or not it is communicationg with a fallback |
| 1447 // iframe or a 'real' ChromeFrame instance. |
| 1448 |
| 1449 var CFInstance; // forward declaration |
| 1450 var h = window['externalHost']; |
| 1451 var inIframe = (window.parent != window); |
| 1452 |
| 1453 if (inIframe) { |
| 1454 h = window.parent; |
| 1455 } |
| 1456 |
| 1457 var normalizeTarget = function(targetOrigin) { |
| 1458 var l = window.location; |
| 1459 if (!targetOrigin) { |
| 1460 if (l.protocol != 'file:') { |
| 1461 targetOrigin = l.protocol + '//' + l.host + "/"; |
| 1462 } else { |
| 1463 // TODO(slightlyoff): |
| 1464 // is this secure enough? Is there another way to get messages |
| 1465 // flowing reliably across file-hosted documents? |
| 1466 targetOrigin = '*'; |
| 1467 } |
| 1468 } |
| 1469 return targetOrigin; |
| 1470 }; |
| 1471 |
| 1472 var postMessageToDest = function(dest, msg, targetOrigin) { |
| 1473 return dest.postMessage(msg, normalizeTarget(targetOrigin)); |
| 1474 }; |
| 1475 |
| 1476 if (h) { |
| 1477 // |
| 1478 // We're loaded inside a ChromeFrame widget (or something that should look |
| 1479 // like we were). |
| 1480 // |
| 1481 |
| 1482 CFInstance = {}; |
| 1483 |
| 1484 installEvtSys(CFInstance); |
| 1485 |
| 1486 // FIXME(slightlyoff): |
| 1487 // passing a target origin to externalHost's postMessage seems b0rked |
| 1488 // right now, so pass null instead. Will re-enable hitch()'d variant |
| 1489 // once that's fixed. |
| 1490 |
| 1491 // CFInstance.postMessage = hitch(null, postMessageToDest, h); |
| 1492 |
| 1493 CFInstance.postMessage = function(msg, targetOrigin) { |
| 1494 return h.postMessage(msg, |
| 1495 (inIframe ? normalizeTarget(targetOrigin) : null) ); |
| 1496 }; |
| 1497 |
| 1498 // Attach to the externalHost's onmessage to proxy it in to CFInstance's |
| 1499 // onmessage. |
| 1500 var dispatchMsg = function(evt) { |
| 1501 try { |
| 1502 CFInstance._dispatch('message', evt); |
| 1503 } catch(e) { |
| 1504 log(e); |
| 1505 // squelch |
| 1506 } |
| 1507 }; |
| 1508 if (inIframe) { |
| 1509 listen(window, 'message', dispatchMsg); |
| 1510 } else { |
| 1511 h.onmessage = dispatchMsg; |
| 1512 } |
| 1513 |
| 1514 CFInstance.rpc = new RPC(CFInstance); |
| 1515 |
| 1516 _loadedListenerList.push(function(evt) { |
| 1517 CFInstance._dispatch('load', evt); |
| 1518 }); |
| 1519 |
| 1520 } else { |
| 1521 // |
| 1522 // We're the host document. |
| 1523 // |
| 1524 |
| 1525 var installProperties = function(instance, args) { |
| 1526 var s = instance.supportedEvents = ['load', 'message']; |
| 1527 instance._msgPrefix = 'CFMessage:'; |
| 1528 |
| 1529 installEvtSys(instance); |
| 1530 |
| 1531 instance.log = log; |
| 1532 |
| 1533 // set up an RPC instance |
| 1534 instance.rpc = new RPC(instance); |
| 1535 |
| 1536 forEach(s, function(evt) { |
| 1537 var l = args['on' + evt]; |
| 1538 if (l) { |
| 1539 instance.listen(evt, l); |
| 1540 } |
| 1541 }); |
| 1542 |
| 1543 var contentWindow = instance.contentWindow; |
| 1544 |
| 1545 // if it doesn't have a postMessage, route to/from the iframe's built-in |
| 1546 if (typeof instance['postMessage'] == undefStr && !!contentWindow) { |
| 1547 |
| 1548 instance.postMessage = hitch(null, postMessageToDest, contentWindow); |
| 1549 |
| 1550 listen(window, 'message', function(evt) { |
| 1551 if (evt.source == contentWindow) { |
| 1552 instance._dispatch('message', evt); |
| 1553 } |
| 1554 }); |
| 1555 } |
| 1556 |
| 1557 return instance; |
| 1558 }; |
| 1559 |
| 1560 /** |
| 1561 * A class whose instances correspond to ChromeFrame instances. Passing an |
| 1562 * arguments object to CFInstance helps parameterize the instance. |
| 1563 * @constructor |
| 1564 * @public |
| 1565 */ |
| 1566 CFInstance = function(args) { |
| 1567 args = args || {}; |
| 1568 var instance; |
| 1569 var success = false; |
| 1570 |
| 1571 // If we've been passed a CFInstance object as our source node, just |
| 1572 // re-use it. |
| 1573 if (args['node']) { |
| 1574 var n = byId(args['node']); |
| 1575 // Look for CF-specific properties. |
| 1576 if (n && n.tagName == 'OBJECT' && n.success && n.rpc) { |
| 1577 // Navigate, set styles, etc. |
| 1578 setProperties(n, args); |
| 1579 return n; |
| 1580 } |
| 1581 } |
| 1582 |
| 1583 var force = !!args['forcePlugin']; |
| 1584 |
| 1585 if (!force && testRequirements(args['requirements'])) { |
| 1586 instance = makeCFIframe(args); |
| 1587 success = true; |
| 1588 } else if (isCfAvailable()) { |
| 1589 instance = makeCFPlugin(args); |
| 1590 success = true; |
| 1591 } else { |
| 1592 // else create an iframe but load the failure content and |
| 1593 // not the 'normal' content |
| 1594 |
| 1595 // grab the fallback URL, and if none, use the 'click here |
| 1596 // to install ChromeFrame' URL. Note that we only support |
| 1597 // polling for install success if we're using the default |
| 1598 // URL |
| 1599 |
| 1600 var fallback = '//www.google.com/chromeframe'; |
| 1601 |
| 1602 args.src = args['fallback'] || fallback; |
| 1603 instance = makeCFIframe(args); |
| 1604 |
| 1605 if (args.src == fallback) { |
| 1606 // begin polling for install success. |
| 1607 |
| 1608 // TODO(slightlyoff): need to prevent firing of onload hooks! |
| 1609 // TODO(slightlyoff): implement polling |
| 1610 // TODO(slightlyoff): replacement callback? |
| 1611 // TODO(slightlyoff): add flag to disable this behavior |
| 1612 } |
| 1613 } |
| 1614 instance.success = success; |
| 1615 |
| 1616 installProperties(instance, args); |
| 1617 |
| 1618 return instance; |
| 1619 }; |
| 1620 |
| 1621 // compatibility shims for development-time. These mirror the methods that |
| 1622 // are created on the CFInstance singleton if we detect that we're running |
| 1623 // inside of CF. |
| 1624 if (!CFInstance['postMessage']) { |
| 1625 CFInstance.postMessage = function() { |
| 1626 var args = toArray(arguments); |
| 1627 args.unshift('CFInstance.postMessage:'); |
| 1628 log.apply(null, args); |
| 1629 }; |
| 1630 CFInstance.listen = function() { |
| 1631 // this space intentionally left blank |
| 1632 }; |
| 1633 } |
| 1634 } |
| 1635 |
| 1636 // expose some properties |
| 1637 CFInstance.ua = ua; |
| 1638 CFInstance._documentLoaded = documentLoaded; |
| 1639 CFInstance.contentTests = contentTests; |
| 1640 CFInstance.isAvailable = function(requirements) { |
| 1641 var hasCf = isCfAvailable(); |
| 1642 return requirements ? (hasCf || testRequirements(requirements)) : hasCf; |
| 1643 |
| 1644 }; |
| 1645 CFInstance.Deferred = Deferred; |
| 1646 CFInstance.toJson = toJson; |
| 1647 CFInstance.fromJson = fromJson; |
| 1648 CFInstance.log = log; |
| 1649 |
| 1650 // expose CFInstance to the external scope. We've already checked to make |
| 1651 // sure we're not going to blow existing objects away. |
| 1652 scope.CFInstance = CFInstance; |
| 1653 |
| 1654 })( this['ChromeFrameScope'] || this ); |
| 1655 |
| 1656 // vim: shiftwidth=2:et:ai:tabstop=2 |
OLD | NEW |