Merge pull request #2683 from vyv03354/bug824601

Replace getUserData()/setUserData() with CustomEvents
This commit is contained in:
Yury Delendik 2013-02-06 07:16:30 -08:00
commit ef1be8ee33
2 changed files with 23 additions and 27 deletions

View File

@ -455,9 +455,9 @@ function RequestListener(actions) {
RequestListener.prototype.receive = function(event) {
var message = event.target;
var doc = message.ownerDocument;
var action = message.getUserData('action');
var data = message.getUserData('data');
var sync = message.getUserData('sync');
var action = event.detail.action;
var data = event.detail.data;
var sync = event.detail.sync;
var actions = this.actions;
if (!(action in actions)) {
log('Unknown action: ' + action);
@ -465,25 +465,27 @@ RequestListener.prototype.receive = function(event) {
}
if (sync) {
var response = actions[action].call(this.actions, data);
message.setUserData('response', response, null);
var detail = event.detail;
detail.__exposedProps__ = {response: 'r'};
detail.response = response;
} else {
var response;
if (!message.getUserData('callback')) {
if (!event.detail.callback) {
doc.documentElement.removeChild(message);
response = null;
} else {
response = function sendResponse(response) {
try {
message.setUserData('response', response, null);
var listener = doc.createEvent('CustomEvent');
listener.initCustomEvent('pdf.js.response', true, false,
{response: response,
__exposedProps__: {response: 'r'}});
return message.dispatchEvent(listener);
} catch (e) {
// message is no longer accessible because the sender is already
// gone. the unloaded sender cannot receive the response anyway.
// doc is no longer accessible because the requestor is already
// gone. unloaded content cannot receive the response anyway.
return false;
}
var listener = doc.createEvent('HTMLEvents');
listener.initEvent('pdf.js.response', true, false);
return message.dispatchEvent(listener);
};
}
actions[action].call(this.actions, data, response);

View File

@ -28,15 +28,13 @@ var FirefoxCom = (function FirefoxComClosure() {
*/
requestSync: function(action, data) {
var request = document.createTextNode('');
request.setUserData('action', action, null);
request.setUserData('data', data, null);
request.setUserData('sync', true, null);
document.documentElement.appendChild(request);
var sender = document.createEvent('Events');
sender.initEvent('pdf.js.message', true, false);
var sender = document.createEvent('CustomEvent');
sender.initCustomEvent('pdf.js.message', true, false,
{action: action, data: data, sync: true});
request.dispatchEvent(sender);
var response = request.getUserData('response');
var response = sender.detail.response;
document.documentElement.removeChild(request);
return response;
},
@ -50,16 +48,10 @@ var FirefoxCom = (function FirefoxComClosure() {
*/
request: function(action, data, callback) {
var request = document.createTextNode('');
request.setUserData('action', action, null);
request.setUserData('data', data, null);
request.setUserData('sync', false, null);
if (callback) {
request.setUserData('callback', callback, null);
document.addEventListener('pdf.js.response', function listener(event) {
var node = event.target,
callback = node.getUserData('callback'),
response = node.getUserData('response');
response = event.detail.response;
document.documentElement.removeChild(node);
@ -69,8 +61,10 @@ var FirefoxCom = (function FirefoxComClosure() {
}
document.documentElement.appendChild(request);
var sender = document.createEvent('HTMLEvents');
sender.initEvent('pdf.js.message', true, false);
var sender = document.createEvent('CustomEvent');
sender.initCustomEvent('pdf.js.message', true, false,
{action: action, data: data, sync: false,
callback: callback});
return request.dispatchEvent(sender);
}
};