Changeset 437

Show
Ignore:
Timestamp:
08/03/08 11:24:45 (5 months ago)
Author:
p3sho
Message:

XSubdomain support for Opera using postMessage. Still synchronous, unfortunately.

Location:
branches/0.5/daemon/orbited/static
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/0.5/daemon/orbited/static/XSubdomainBridge.html

    r349 r437  
    11<html> 
    22 <head> 
    3   <script> 
     3     <script src="JSON.js"></script> 
     4 
     5<script> 
     6 
    47var origDomain = document.domain 
    58var topDomain = null; 
    69var id = parseInt(location.hash.slice(1)); 
    7  
    810//var id = location.hash 
    911var parts = document.domain.split('.') 
     12var req = null; 
     13 
     14// Posts a message in JSON string format to the parent document, issuing the request 
     15function postMessageToParent(msg) 
     16{ 
     17        var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined); 
     18        if (typeof target != "undefined") { 
     19            target.postMessage(msg, "*"); 
     20        } 
     21}; 
     22 
     23 
     24function push(data) { 
     25    document.domain = topDomain 
     26 
     27    if (typeof(opera) != "undefined") 
     28        postMessageToParent("event " + id + " " + JSON.stringify(data)); 
     29    else 
     30        parent.XSubdomainRequest.prototype._event(id, data); 
     31         
     32    try { 
     33        document.domain = origDomain; 
     34    } 
     35    catch(e) { 
     36    } 
     37} 
     38 
    1039if (parts.length == 1) { 
    1140    try { 
    1241        document.domain = document.domain 
    13         parent.XSubdomainRequest.prototype 
     42        if (typeof(opera) == "undefined") 
     43            parent.XSubdomainRequest.prototype 
    1444        topDomain = document.domain 
    1545    } 
     
    2151        document.domain = parts.slice(i).join(".") 
    2252        try { 
    23             parent.XSubdomainRequest.prototype 
     53            if (typeof(opera) == "undefined") 
     54                parent.XSubdomainRequest.prototype 
    2455            topDomain = document.domain 
    2556            break; 
     
    3061    } 
    3162} 
     63 
    3264if (topDomain == null) 
    3365    throw new Error("Invalid document.domain for cross-frame communication") 
     
    4072catch(e) { 
    4173} 
     74 
     75 
    4276function getNext() { 
    4377    document.domain = topDomain 
     
    5488    return data 
    5589} 
    56 function push(data) { 
    57     document.domain = topDomain 
    58     parent.XSubdomainRequest.prototype._event(id, data); 
    59     try { 
    60         document.domain = origDomain; 
    61     } 
    62     catch(e) { 
    63     } 
     90 
     91// Sends a message to the parent, requesting it to send the next request on 
     92// the queue. The result is handled in the message receiver handler 
     93function sendGetNext() { 
     94    document.domain = topDomain; 
     95    postMessageToParent("queues " + id); 
    6496} 
    6597 
    6698function doNextRequest() { 
    67     var req = getNext(); 
     99    if (typeof(opera) != "undefined") 
     100        sendGetNext() 
     101    else 
     102        req = getNext(); 
     103 
    68104    if (req == null) { 
    69105        return setTimeout(doNextRequest, 10); 
    70106    } 
     107 
    71108    var smethod = req[0] 
    72109    var surl = req[1] 
     
    117154            payload['headers'] = headers 
    118155            push(["readystatechange", payload]) 
     156            req = null; 
    119157            return doNextRequest(); 
    120158        } 
     
    140178}; 
    141179 
     180 
     181// Message receiver handler (req. for Opera compatibility) 
     182document.addEventListener('message', function(e) { 
     183    var msg = e.data; 
     184    var response = JSON.parse(msg); 
     185    req = response; 
     186} 
     187, false 
     188); 
     189 
     190 
    142191</script> 
    143192</head> 
  • branches/0.5/daemon/orbited/static/XSubdomainRequest.js

    r350 r437  
    103103        } 
    104104    } 
     105 
    105106    self.abort = function() { 
    106107        // TODO implement.  this is needed by TCPSocket.disconnect 
     
    117118        ifr.style.visibility = 'hidden'; 
    118119    } 
    119  
    120120} 
    121  
    122121 
    123122 
     
    142141    receive(payload); 
    143142} 
     143 
     144 
     145// Message handler - parses messages posted by the 
     146// XSubdomainBridge 
     147document.addEventListener('message', function(e) { 
     148    var msg = e.data.split(" "); 
     149    var cmd = msg.shift(); 
     150    if (cmd == "event")  
     151    { 
     152        var id = msg.shift(); 
     153        var dataString = msg.join(" "); 
     154        var data = JSON.parse(dataString); 
     155 
     156        XSubdomainRequest.prototype._event(id, data); 
     157    } 
     158    if (cmd == "queues") 
     159    { 
     160        var id = msg.shift(); 
     161        var queue = XSubdomainRequest.prototype._state.queues[id]; 
     162        if (queue.length > 0) { 
     163            var data = queue.shift(); 
     164            e.source.postMessage(JSON.stringify(data), e.origin); 
     165        } 
     166    } 
     167}, false 
     168);