incubator-callback-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jim Jarvis (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CB-1318) Cordova Callback Logic Not Implemented Correctly - Native Asynchronous Completion Can Call Wrong Logic in Javascript - Requires Non-recurring Unique ID as Outlined
Date Mon, 24 Sep 2012 03:11:08 GMT

    [ https://issues.apache.org/jira/browse/CB-1318?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13461576#comment-13461576
] 

Jim Jarvis commented on CB-1318:
--------------------------------

Braden,

Here is the fix we are using:

cordova.ios.js

REPLACE THIS LINE:
callbackId = service + cordova.callbackId++;

WITH THIS LINE:
callbackId = service + ":" + (new Date().getTime()).toString() + "-" + (Math.floor(Math.random()*90000)
+ 10000).toString(); // Insure context-safe async callbacks

EXAMPLE ID: 1348454513273-31295

The callbackId is still human readable with service name prefix and replayed callbacks are
highly unlikely. It's been working for us without any issues for some time now.

                
> Cordova Callback Logic Not Implemented Correctly -  Native Asynchronous Completion Can
Call Wrong Logic in Javascript - Requires Non-recurring Unique ID as Outlined
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CB-1318
>                 URL: https://issues.apache.org/jira/browse/CB-1318
>             Project: Apache Cordova
>          Issue Type: Bug
>          Components: Android, iOS
>    Affects Versions: 2.0.0
>            Reporter: Jim Jarvis
>            Assignee: Braden Shepherdson
>            Priority: Critical
>             Fix For: 2.2.0
>
>
> This bug creates very difficult to find execution anomalies for any asynchronous callback
that occurs when the calling context has changed.
> PROBLEM: Cordova callback logic can trigger the callback function of an unrelated call
from a different context. When a call from Javascript is made, Cordova generates a new callbackId,
adds it to the cordova.callbacks array, and passes it along to the native code. Some time
later, when the native code completes the request, it injects javascript to call the callbackSuccess
or callbackError function. The respective callback handler looks up the callback attributes
from the cordova.callbacks array and executes the original caller's completion function. The
problem is each page, or reload of a page, resets the callbackId counter to zero and new calls
end up reusing the same callbackId, even though the native code haS not yet completed the
prior request. This can occur upon a new page load or reload upon an empty href in an anchor
tag. A call to native code from page1.html could trigger the completion routine on page2.html
because the native code has no context of the change on the other side of the bridge.
> SOLUTION: Replace the incrementing callbackId with a unique identifier to insure only
the caller's completion logic will be called. I have done this with a Pseudo-GUID generator.
Fix outlined below:
> // REPLACE THIS LINE in both cordova.2.0.0.js and cordova.ios.js
>         callbackId = service + cordova.callbackId++;    // BUG: incrementing Id's can
call unrelated callback logic
> // WITH THIS LINE
>         callbackId = service + ":" + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
// Generate unique callbackID with Pseudo-GUID
> This fix insures no code will execute if the caller's context has changed and is a more
well formatted callbackId.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message