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.
5 // Parts Copyright 2005-2009, the Dojo Foundation. Used under the terms of the
8 // http://download.dojotoolkit.org/release-1.3.2/dojo-release-1.3.2/dojo/LICENSE
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)
22 // * figure out if there's any way to install w/o a browser restart, and if
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?
29 // bail if we'd be over-writing an existing CFInstance object
30 if (scope['CFInstance']) {
34 /////////////////////////////////////////////////////////////////////////////
35 // Utiliity and Cross-Browser Functions
36 /////////////////////////////////////////////////////////////////////////////
38 // a monotonically incrementing counter
41 var undefStr = 'undefined';
44 // Browser detection: ua.isIE, ua.isSafari, ua.isOpera, etc.
48 * An object for User Agent detection
54 var dua = String(n.userAgent);
55 var dav = String(n.appVersion);
56 var tv = parseFloat(dav);
57 var duaParse = function(s){
61 dua.split(s)[1].replace(/\./g, function() {
63 return (c > 1) ? '' : '.';
67 // squelch to intentionally return undefined
71 ua.isOpera = dua.indexOf('Opera') >= 0 ? tv: undefined;
73 ua.isWebKit = duaParse('WebKit/');
75 ua.isChrome = duaParse('Chrome/');
77 ua.isKhtml = dav.indexOf('KHTML') >= 0 ? tv : undefined;
79 var index = Math.max(dav.indexOf('WebKit'), dav.indexOf('Safari'), 0);
81 if (index && !ua.isChrome) {
83 ua.isSafari = parseFloat(dav.split('Version/')[1]);
84 if(!ua.isSafari || parseFloat(dav.substr(index + 7)) <= 419.3){
89 if (dua.indexOf('Gecko') >= 0 && !ua.isKhtml) {
91 ua.isGecko = duaParse(' rv:');
96 ua.isFF = parseFloat(dua.split('Firefox/')[1]) || undefined;
99 if (document.all && !ua.isOpera) {
100 /** @type {number} */
101 ua.isIE = parseFloat(dav.split('MSIE ')[1]) || undefined;
106 * Log out varargs to a browser-provided console object (if available). Else
108 * @param {*} var_args Optional Things to log.
111 var log = function() {
112 if (window['console']) {
114 if (ua.isSafari || ua.isChrome) {
117 console.log.apply(console, arguments);
120 console.log(toArray(arguments).join(' '));
129 // Language utility methods
133 * Determine if the passed item is a String
134 * @param {*} item Item to test.
137 var isString = function(item) {
138 return typeof item == 'string';
142 * Determine if the passed item is a Function object
143 * @param {*} item Item to test.
146 var isFunction = function(item) {
149 typeof item == 'function' || item instanceof Function
155 * Determine if the passed item is an array.
156 * @param {*} item Item to test.
159 var isArray = function(item){
162 item instanceof Array || (
163 typeof item == 'object' &&
164 typeof item.length != undefStr
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.
178 var _efficientToArray = function(obj, opt_offset, opt_startWith){
179 return (opt_startWith || []).concat(
180 Array.prototype.slice.call(obj, opt_offset || 0 )
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
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++){
200 * Converts an array-like object (e.g., an "arguments" object) to a real
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
207 var toArray = ua.isIE ?
210 obj.item ? _slowToArray : _efficientToArray
211 ).apply(this, arguments);
215 var _getParts = function(arr, obj, cb){
217 isString(arr) ? arr.split('') : arr,
219 isString(cb) ? new Function('item', 'index', 'array', cb) : cb
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
232 var forEach = function(arr, callback, thisObject) {
233 if(!arr || !arr.length){
236 var parts = _getParts(arr, thisObject, callback);
237 // parts has a structure of:
244 for (var i = 0, l = arr.length; i < l; ++i) {
245 parts[2].call( parts[1], arr[i], i, arr );
251 * returns a new function bound to scope with a variable number of positional
255 var _hitchArgs = function(scope, method /*,...*/) {
256 var pre = toArray(arguments, 2);
257 var named = isString(method);
259 var args = toArray(arguments);
260 var f = named ? (scope || window)[method] : method;
261 return f && f.apply(scope || this, pre.concat(args));
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
273 var hitch = function(scope, method){
274 if (arguments.length > 2) {
275 return _hitchArgs.apply(window, arguments); // Function
283 if (isString(method)) {
284 scope = scope || window;
285 if (!scope[method]) {
287 ['scope["', method, '"] is null (scope="', scope, '")'].join('')
291 return scope[method].apply(scope, arguments || []);
298 return method.apply(scope, arguments || []);
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
309 var listen = function(obj, type, handler) {
310 if (obj['attachEvent']) {
311 obj.attachEvent('on' + type, handler);
313 obj.addEventListener(type, handler, false);
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
323 var installEvtSys = function(instance) {
327 (typeof instance.addEventListener != undefStr) &&
328 ((instance['tagName'] || '').toLowerCase() != 'iframe')
331 instance.listen = function(type, func) {
332 var t = eventsMap[type];
334 t = eventsMap[type] = [];
336 listen(instance, type, hitch(instance, "_dispatch", type));
343 instance._dispatch = function(type, evt) {
345 var stopper = function() {
349 forEach(eventsMap[type], function(f) {
360 * Deserialize the passed JSON string
361 * @param {!String} json A string to be deserialized
365 var fromJson = window['JSON'] ? function(json) {
366 return JSON.parse(json);
369 return eval('(' + (json || undefStr) + ')');
373 * String escaping for use in JSON serialization
374 * @param {string} str The string to escape
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;
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
395 var toJson = window['JSON'] ? function(it) {
396 return JSON.stringify(it);
400 if (it === undefined) {
404 var objtype = typeof it;
405 if (objtype == 'number' || objtype == 'boolean') {
414 return _escapeString(it);
418 var recurse = arguments.callee;
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__
425 throw new Error('Cannot serialize DOM nodes');
431 forEach(it, function(obj) {
432 var val = recurse(obj);
433 if (typeof val != 'string') {
438 return '[' + res.join(',') + ']';
441 if (objtype == 'function') {
445 // generic object code path
447 for (var key in it) {
449 if (typeof key == 'number') {
450 keyStr = '"' + key + '"';
451 } else if(typeof key == 'string') {
452 keyStr = _escapeString(key);
454 // skip non-string or number keys
457 val = recurse(it[key]);
458 if (typeof val != 'string') {
459 // skip non-serializable values
462 // TODO(slightlyoff): use += on Moz since it's faster there
463 output.push(keyStr + ':' + val);
465 return '{' + output.join(',') + '}'; // String
468 // code to register with the earliest safe onload-style handler
470 var _loadedListenerList = [];
471 var _loadedFired = false;
474 * a default handler for document onload. When called (the first time),
475 * iterates over the list of registered listeners, calling them in turn.
478 var documentLoaded = function() {
481 forEach(_loadedListenerList, 'item();');
485 if (document.addEventListener) {
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);
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);
497 // crazy hack for IE that relies on the "deferred" behavior of script
500 '<scr' + 'ipt defer src="//:" '
501 + 'onreadystatechange="if(this.readyState==\'complete\')'
502 + '{ CFInstance._documentLoaded();}">'
507 // TODO(slightlyoff): known KHTML init issues are ignored for now
510 // DOM utility methods
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.
521 var byId = (ua.isIE || ua.isOpera) ?
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) {
531 var elements = doc.all[id];
532 if (!elements || !elements.length) {
535 // if more than 1, choose first with the correct id
537 while (te = elements[i++]) {
538 if (te.attributes.id.value == id) {
544 return id; // DomNode
548 return isString(id) ? (doc || document).getElementById(id) : id;
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
560 var getUid = function(node) {
561 var u = 'cfUnique' + (_counter++);
562 return (!node) ? u : ( node.id || node.uniqueID || (node.id = u) );
566 // the Deferred class, borrowed from Twisted Python and Dojo
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
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.
585 Deferred = function(/*Function?*/ canceller, timeout){
587 // var deferred = new Deferred();
588 // setTimeout(function(){ deferred.callback({success: true}); }, 1000);
591 this.id = _counter++;
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') {
599 timeout = 216000; // give it an hour
602 this._timer = setTimeout(
603 hitch(this, 'errback', new Error('timeout')),
606 this.silentlyCancelled = false;
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.
616 Deferred.prototype.cancel = function() {
618 if (this.fired == -1) {
619 if (this.canceller) {
620 err = this.canceller(this);
622 this.silentlyCancelled = true;
624 if (this.fired == -1) {
625 if ( !(err instanceof Error) ) {
627 var msg = 'Deferred Cancelled';
628 if (err && err.toString) {
629 msg += ': ' + err.toString();
631 err = new Error(msg);
632 err.dType = 'cancel';
633 err.cancelResult = res;
639 (this.results[0] instanceof Deferred)
641 this.results[0].cancel();
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
652 Deferred.prototype._resback = function(res) {
654 clearTimeout(this._timer);
656 this.fired = res instanceof Error ? 1 : 0;
657 this.results[this.fired] = res;
662 * determine if the deferred has already been resolved
666 Deferred.prototype._check = function() {
667 if (this.fired != -1) {
668 if (!this.silentlyCancelled) {
671 this.silentlyCancelled = 0;
678 * Begin the callback sequence with a non-error value.
679 * @param {Object|Error} res the result
682 Deferred.prototype.callback = function(res) {
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.
693 Deferred.prototype.errback = function(res) {
695 if ( !(res instanceof Error) ) {
696 res = new Error(res);
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
708 * @return {Deferred} this
711 Deferred.prototype.addBoth = function(cb, cbfn) {
712 var enclosed = hitch.apply(window, arguments);
713 return this.addCallbacks(enclosed, enclosed);
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
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
726 * @return {Deferred} this
729 Deferred.prototype.addCallback = function(cb, cbfn /*...*/) {
730 return this.addCallbacks(hitch.apply(window, arguments));
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
741 * @return {Deferred} this
744 Deferred.prototype.addErrback = function(cb, cbfn) {
745 return this.addCallbacks(null, hitch.apply(window, arguments));
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
755 Deferred.prototype.addCallbacks = function(callback, errback) {
756 this.chain.push([callback, errback]);
757 if (this.fired >= 0) {
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 chain
766 * @return {Deferred} this
769 Deferred.prototype.chain = function(def) {
770 this.addCallbacks(def.callback, def.errback);
775 * Used internally to exhaust the callback sequence when a result is
779 Deferred.prototype._fire = function() {
780 var chain = this.chain;
781 var fired = this.fired;
782 var res = this.results[fired];
784 while ((chain.length) && (!this.paused)) {
785 var f = chain.shift()[fired];
789 var func = hitch(this, function() {
791 //If no response, then use previous response.
792 if (typeof ret != undefStr) {
795 fired = res instanceof Error ? 1 : 0;
796 if (res instanceof Deferred) {
799 // inlined from _pause()
801 if ( (this.paused == 0) && (this.fired >= 0)) {
805 // inlined from _unpause
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
827 /////////////////////////////////////////////////////////////////////////////
828 // Plugin Initialization Class and Helper Functions
829 /////////////////////////////////////////////////////////////////////////////
831 var returnFalse = function() {
841 ua.isChrome || ua.isSafari >= 3 || ua.isFF >= 3 || ua.isOpera >= 9.2
846 return !!(ua.isChrome || ua.isSafari || ua.isFF || ua.isOpera);
849 postMessage: function() {
851 !!window['postMessage'] ||
860 // the spec isn't settled and nothing currently supports it
861 websocket: returnFalse,
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;
870 // "working" video/audio tag?
872 if (typeof cachedHasVideo != undefStr) {
873 return cachedHasVideo;
876 // We haven't figured it out yet, so probe the <video> tag and cache the
878 var video = document.createElement('video');
879 return cachedHasVideo = (typeof video['play'] != undefStr);
883 if (typeof cachedHasAudio != undefStr) {
884 return cachedHasAudio;
887 var audio = document.createElement('audio');
888 return cachedHasAudio = (typeof audio['play'] != undefStr);
891 'video-theora': function() {
892 return contentTests.video() && (ua.isChrome || ua.isFF > 3);
895 'video-h264': function() {
896 return contentTests.video() && (ua.isChrome || ua.isSafari >= 4);
899 'audio-vorbis': function() {
900 return contentTests.audio() && (ua.isChrome || ua.isFF > 3);
903 'audio-mp3': function() {
904 return contentTests.audio() && (ua.isChrome || ua.isSafari >= 4);
907 // can we implement RPC over available primitives?
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
915 // use "!!" to avoid null-is-an-object weirdness
916 return !!window['postMessage'];
921 return !!window['openDatabase'];
927 // IE8, Safari, etc. support "localStorage", FF supported "globalStorage"
928 return !!window['globalStorage'] || !!window['localStorage'];
932 // isIE, isFF, isWebKit, etc.
934 'isOpera', 'isWebKit', 'isChrome', 'isKhtml', 'isSafari',
935 'isGecko', 'isFF', 'isIE'
938 contentTests[name] = function() {
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.
952 var testRequirements = function(reqs) {
953 // never use CF on Chrome or Safari
954 if (ua.isChrome || ua.isSafari) {
962 forEach(reqs, function(i) {
965 // support custom test functions
968 // else it's a lookup by name
969 matches = (!!contentTests[i] && contentTests[i]());
971 allMatch = allMatch && matches;
979 * Checks to find out if ChromeFrame is available as a plugin
983 var isCfAvailable = function() {
984 if (typeof cachedAvailable != undefStr) {
985 return cachedAvailable;
988 cachedAvailable = false;
990 if (typeof window['ActiveXObject'] != undefStr) {
992 var i = new ActiveXObject('ChromeTab.ChromeFrame');
994 cachedAvailable = true;
997 log('ChromeFrame not available, error:', e.message);
1001 for (var x = 0; x < p.length; x++) {
1002 if (p[x].name.indexOf('Google Chrome Frame') == 0) {
1003 cachedAvailable = true;
1008 return cachedAvailable;
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
1020 var param = function(name, value, parent) {
1021 var p = document.createElement('param');
1022 p.setAttribute('name', name);
1023 p.setAttribute('value', value);
1025 parent.appendChild(p);
1030 /** @type {boolean} */
1031 var cfStyleTagInjected = false;
1034 * Creates a style sheet in the document which provides default styling for
1035 * ChromeFrame instances. Successive calls should have no additive effect.
1038 var injectCFStyleTag = function() {
1039 if (cfStyleTagInjected) {
1040 // once and only once
1044 var rule = ['.chromeFrameDefaultStyle {',
1050 var ss = document.createElement('style');
1051 ss.setAttribute('type', 'text/css');
1052 if (ss.styleSheet) {
1053 ss.styleSheet.cssText = rule;
1055 ss.appendChild(document.createTextNode(rule));
1057 var h = document.getElementsByTagName('head')[0];
1059 h.insertBefore(ss, h.firstChild);
1063 cfStyleTagInjected = true;
1067 // FIXME(slightlyoff): log? retry?
1072 * Plucks properties from the passed arguments and sets them on the passed
1074 * @param {Node} node The node to set properties on
1075 * @param {Object} args A map of user-specified properties to set
1078 var setProperties = function(node, args) {
1081 var srcNode = byId(args['node']);
1083 node.id = args['id'] || (srcNode ? srcNode['id'] || getUid(srcNode) : '');
1085 // TODO(slightlyoff): Opera compat? need to test there
1086 var cssText = args['cssText'] || '';
1087 node.style.cssText = ' ' + cssText;
1089 var classText = args['className'] || '';
1090 node.className = 'chromeFrameDefaultStyle ' + classText;
1092 // default if the browser doesn't so we don't show sad-tab
1093 var src = args['src'] || 'about:blank';
1095 if (ua.isIE || ua.isOpera) {
1098 // crazyness regarding when things are set in NPAPI
1099 node.setAttribute('src', src);
1103 srcNode.parentNode.replaceChild(node, srcNode);
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.
1114 var makeCFPlugin = function(args) {
1115 var el; // the element
1117 el = document.createElement('object');
1118 el.setAttribute("type", "application/chromeframe");
1120 var dummy = document.createElement('span');
1122 '<object codeBase="//www.google.com"',
1123 "type='application/chromeframe'",
1124 'classid="CLSID:E0A900DF-9611-4446-86BD-4B1D47E7DB2A"></object>'
1126 el = dummy.firstChild;
1128 setProperties(el, args);
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.
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'));
1151 var msgPrefix = 'CFInstance.rpc:';
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.
1162 var RPC = function(instance) {
1163 this.initDeferred = new Deferred();
1165 this.instance = instance;
1167 instance.listen('message', hitch(this, '_handleMessage'));
1169 this._localExposed = {};
1170 this._doWithAckCallbacks = {};
1173 this._msgBacklog = [];
1175 this._initialized = false;
1176 this._exposeMsgBacklog = [];
1178 this._exposed = false;
1179 this._callRemoteMsgBacklog = [];
1181 this._inFlight = {};
1183 var sendLoadMsg = hitch(this, function(evt) {
1184 this.doWithAck('load').addCallback(this, function() {
1186 this._postMessageBacklog();
1190 if (instance['tagName']) {
1191 instance.listen('load', sendLoadMsg);
1197 RPC.prototype._postMessageBacklog = function() {
1199 // forEach(this._msgBacklog, this._postMessage, this);
1200 // this._msgBacklog = [];
1201 while (this._msgBacklog.length) {
1202 var msg = this._msgBacklog.shift();
1203 this._postMessage(msg);
1208 RPC.prototype._postMessage = function(msg, force) {
1209 if (!force && !this._open) {
1210 this._msgBacklog.push(msg);
1212 // FIXME(slightlyoff): need to check domains list here!
1213 this.instance.postMessage(msgPrefix + msg, '*');
1217 RPC.prototype._doWithAck = function(what) {
1218 this._postMessage('doWithAckCallback:' + what, what == 'load');
1221 RPC.prototype.doWithAck = function(what) {
1222 var d = new Deferred();
1223 this._doWithAckCallbacks[what] = d;
1224 this._postMessage('doWithAck:' + what, what == 'load');
1228 RPC.prototype._handleMessage = function(evt, stopper) {
1229 var d = String(evt.data);
1231 if (d.indexOf(msgPrefix) != 0) {
1232 // not for us, allow the event dispatch to continue...
1236 // ...else we're the end of the line for this event
1239 // see if we know what type of message it is
1240 d = d.substr(msgPrefix.length);
1242 var cIndex = d.indexOf(':');
1244 var type = d.substr(0, cIndex);
1246 if (type == 'doWithAck') {
1247 this._doWithAck(d.substr(cIndex + 1));
1251 var msgBody = d.substr(cIndex + 1);
1253 if (type == 'doWithAckCallback') {
1254 this._doWithAckCallbacks[msgBody].callback(1);
1258 if (type == 'init') {
1262 // All the other stuff we can do uses a JSON payload.
1263 var obj = fromJson(msgBody);
1265 if (type == 'callRemote') {
1267 if (obj.method && obj.params && obj.id) {
1275 // Undefined isn't valid JSON, so use null as default value.
1276 ret.value = this._localExposed[ obj.method ](evt, obj) || null;
1279 ret.error = e.message;
1282 this._postMessage('callReturn:' + toJson(ret));
1286 if (type == 'callReturn') {
1287 // see if we're waiting on an outstanding RPC call, which
1288 // would be identified by returnId.
1289 var rid = obj['returnId'];
1294 var callWrap = this._inFlight[rid];
1300 callWrap.d.callback(obj['value'] || 1);
1302 callWrap.d.errback(new Error(obj['error'] || 'unspecified RPC error'));
1304 delete this._inFlight[rid];
1310 * Makes a method visible to be called
1311 * @param {string} name The name to expose the method at.
1312 * @param {Function|string} method The function (or name of the function) to
1313 * expose. If a name is provided, it's looked up from the passed scope.
1314 * If no scope is provided, the global scope is queried for a function
1316 * @param {Object} scope Optional A scope to bind the passed method to. If
1317 * the method parameter is specified by a string, the method is both
1318 * located on the passed scope and bound to it.
1319 * @param {Array} domains Optional A list of domains in
1320 * 'http://example.com:8080' format which may call the given method.
1321 * Currently un-implemented.
1324 RPC.prototype.expose = function(name, method, scope, domains) {
1325 scope = scope || window;
1326 method = isString(method) ? scope[method] : method;
1328 // local call proxy that the other side will hit when calling our method
1329 this._localExposed[name] = function(evt, obj) {
1330 return method.apply(scope, obj.params);
1333 if (!this._initialized) {
1334 this._exposeMsgBacklog.push(arguments);
1338 var a = [name, method, scope, domains];
1339 this._sendExpose.apply(this, a);
1342 RPC.prototype._sendExpose = function(name) {
1343 // now tell the other side that we're advertising this method
1344 this._postMessage('expose:' + toJson({ name: name }));
1349 * Calls a remote method asynchronously and returns a Deferred object
1350 * representing the response.
1351 * @param {string} method Name of the method to call. Should be the same name
1352 * which the other side has expose()'d.
1353 * @param {Array} params Optional A list of arguments to pass to the called
1354 * method. All elements in the list must be cleanly serializable to
1356 * @param {CFInstance.Deferred} deferred Optional A Deferred to use for
1357 * reporting the response of the call. If no deferred is passed, a new
1358 * Deferred is created and returned.
1359 * @return {CFInstance.Deferred}
1362 RPC.prototype.callRemote = function(method, params, timeout, deferred) {
1363 var d = deferred || new Deferred(null, timeout || -1);
1365 if (!this._exposed) {
1366 var args = toArray(arguments);
1369 this._callRemoteMsgBacklog.push(args);
1375 d.errback('no method provided!');
1379 var id = msgPrefix + (_counter++);
1381 // JSON-ify the whole bundle
1383 method: String(method),
1384 params: params || [],
1387 var callJson = toJson(callWrapper);
1389 this._inFlight[id] = callWrapper;
1390 this._postMessage('callRemote:' + callJson);
1396 * Tells the other side of the connection that we're ready to start receiving
1397 * calls. Returns a Deferred that is called back when both sides have
1398 * initialized and any backlogged messages have been sent. RPC users should
1399 * generally work to make sure that they call expose() on all of the methods
1400 * they'd like to make available to the other side *before* calling init()
1401 * @return {CFInstance.Deferred}
1404 RPC.prototype.init = function() {
1405 var d = this.initDeferred;
1406 this.doWithAck('init').addCallback(this, function() {
1407 // once the init floodgates are open, send our backlogs one at a time,
1408 // with a little lag in the middle to prevent ordering problems
1410 this._initialized = true;
1411 while (this._exposeMsgBacklog.length) {
1412 this.expose.apply(this, this._exposeMsgBacklog.shift());
1415 setTimeout(hitch(this, function(){
1417 this._exposed = true;
1418 while (this._callRemoteMsgBacklog.length) {
1419 this.callRemote.apply(this, this._callRemoteMsgBacklog.shift());
1430 // CFInstance design notes:
1432 // The CFInstance constructor is only ever used in host environments. In
1433 // content pages (things hosted by a ChromeFrame instance), CFInstance
1434 // acts as a singleton which provides services like RPC for communicating
1435 // with it's mirror-image object in the hosting environment. We want the
1436 // same methods and properties to be available on *instances* of
1437 // CFInstance objects in the host env as on the singleton in the hosted
1438 // content, despite divergent implementation.
1440 // Further complicating things, CFInstance may specialize behavior
1441 // internally based on whether or not it is communicationg with a fallback
1442 // iframe or a 'real' ChromeFrame instance.
1444 var CFInstance; // forward declaration
1445 var h = window['externalHost'];
1446 var inIframe = (window.parent != window);
1452 var normalizeTarget = function(targetOrigin) {
1453 var l = window.location;
1454 if (!targetOrigin) {
1455 if (l.protocol != 'file:') {
1456 targetOrigin = l.protocol + '//' + l.host + "/";
1458 // TODO(slightlyoff):
1459 // is this secure enough? Is there another way to get messages
1460 // flowing reliably across file-hosted documents?
1464 return targetOrigin;
1467 var postMessageToDest = function(dest, msg, targetOrigin) {
1468 return dest.postMessage(msg, normalizeTarget(targetOrigin));
1473 // We're loaded inside a ChromeFrame widget (or something that should look
1479 installEvtSys(CFInstance);
1481 // FIXME(slightlyoff):
1482 // passing a target origin to externalHost's postMessage seems b0rked
1483 // right now, so pass null instead. Will re-enable hitch()'d variant
1484 // once that's fixed.
1486 // CFInstance.postMessage = hitch(null, postMessageToDest, h);
1488 CFInstance.postMessage = function(msg, targetOrigin) {
1489 return h.postMessage(msg,
1490 (inIframe ? normalizeTarget(targetOrigin) : null) );
1493 // Attach to the externalHost's onmessage to proxy it in to CFInstance's
1495 var dispatchMsg = function(evt) {
1497 CFInstance._dispatch('message', evt);
1504 listen(window, 'message', dispatchMsg);
1506 h.onmessage = dispatchMsg;
1509 CFInstance.rpc = new RPC(CFInstance);
1511 _loadedListenerList.push(function(evt) {
1512 CFInstance._dispatch('load', evt);
1517 // We're the host document.
1520 var installProperties = function(instance, args) {
1521 var s = instance.supportedEvents = ['load', 'message'];
1522 instance._msgPrefix = 'CFMessage:';
1524 installEvtSys(instance);
1528 // set up an RPC instance
1529 instance.rpc = new RPC(instance);
1531 forEach(s, function(evt) {
1532 var l = args['on' + evt];
1534 instance.listen(evt, l);
1538 var contentWindow = instance.contentWindow;
1540 // if it doesn't have a postMessage, route to/from the iframe's built-in
1541 if (typeof instance['postMessage'] == undefStr && !!contentWindow) {
1543 instance.postMessage = hitch(null, postMessageToDest, contentWindow);
1545 listen(window, 'message', function(evt) {
1546 if (evt.source == contentWindow) {
1547 instance._dispatch('message', evt);
1556 * A class whose instances correspond to ChromeFrame instances. Passing an
1557 * arguments object to CFInstance helps parameterize the instance.
1561 CFInstance = function(args) {
1564 var success = false;
1566 // If we've been passed a CFInstance object as our source node, just
1569 var n = byId(args['node']);
1570 // Look for CF-specific properties.
1571 if (n && n.tagName == 'OBJECT' && n.success && n.rpc) {
1572 // Navigate, set styles, etc.
1573 setProperties(n, args);
1578 var force = !!args['forcePlugin'];
1580 if (!force && testRequirements(args['requirements'])) {
1581 instance = makeCFIframe(args);
1583 } else if (isCfAvailable()) {
1584 instance = makeCFPlugin(args);
1587 // else create an iframe but load the failure content and
1588 // not the 'normal' content
1590 // grab the fallback URL, and if none, use the 'click here
1591 // to install ChromeFrame' URL. Note that we only support
1592 // polling for install success if we're using the default
1595 var fallback = '//www.google.com/chromeframe';
1597 args.src = args['fallback'] || fallback;
1598 instance = makeCFIframe(args);
1600 if (args.src == fallback) {
1601 // begin polling for install success.
1603 // TODO(slightlyoff): need to prevent firing of onload hooks!
1604 // TODO(slightlyoff): implement polling
1605 // TODO(slightlyoff): replacement callback?
1606 // TODO(slightlyoff): add flag to disable this behavior
1609 instance.success = success;
1611 installProperties(instance, args);
1616 // compatibility shims for development-time. These mirror the methods that
1617 // are created on the CFInstance singleton if we detect that we're running
1619 if (!CFInstance['postMessage']) {
1620 CFInstance.postMessage = function() {
1621 var args = toArray(arguments);
1622 args.unshift('CFInstance.postMessage:');
1623 log.apply(null, args);
1625 CFInstance.listen = function() {
1626 // this space intentionally left blank
1631 // expose some properties
1633 CFInstance._documentLoaded = documentLoaded;
1634 CFInstance.contentTests = contentTests;
1635 CFInstance.isAvailable = function(requirements) {
1636 var hasCf = isCfAvailable();
1637 return requirements ? (hasCf || testRequirements(requirements)) : hasCf;
1640 CFInstance.Deferred = Deferred;
1641 CFInstance.toJson = toJson;
1642 CFInstance.fromJson = fromJson;
1643 CFInstance.log = log;
1645 // expose CFInstance to the external scope. We've already checked to make
1646 // sure we're not going to blow existing objects away.
1647 scope.CFInstance = CFInstance;
1649 })( this['ChromeFrameScope'] || this );
1651 // vim: shiftwidth=2:et:ai:tabstop=2