Preventing hash colisions in identifier of operations
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 7 Apr 2014 08:22:08 +0000 (10:22 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 7 Apr 2014 08:22:08 +0000 (10:22 +0200)
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/services/OperationsService.java

index ee9d9bc..fd0849e 100644 (file)
@@ -174,7 +174,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
     \r
     /// Identifier of operation in progress which result shouldn't be lost \r
 \r
     \r
     /// Identifier of operation in progress which result shouldn't be lost \r
-    private int mWaitingForOpId = -1;\r
+    private long mWaitingForOpId = Long.MAX_VALUE;\r
     \r
     \r
     /**\r
     \r
     \r
     /**\r
@@ -211,7 +211,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             initAuthTokenType();\r
         } else {\r
             mAuthTokenType = savedInstanceState.getString(KEY_AUTH_TOKEN_TYPE);\r
             initAuthTokenType();\r
         } else {\r
             mAuthTokenType = savedInstanceState.getString(KEY_AUTH_TOKEN_TYPE);\r
-            mWaitingForOpId = savedInstanceState.getInt(KEY_WAITING_FOR_OP_ID);\r
+            mWaitingForOpId = savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID);\r
         }\r
         \r
         /// load user interface\r
         }\r
         \r
         /// load user interface\r
@@ -355,7 +355,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
             findViewById(R.id.hostUrlFrame).setVisibility(View.GONE);\r
             mRefreshButton = findViewById(R.id.centeredRefreshButton);\r
         }\r
             findViewById(R.id.hostUrlFrame).setVisibility(View.GONE);\r
             mRefreshButton = findViewById(R.id.centeredRefreshButton);\r
         }\r
-        showRefreshButton(mServerIsChecked && !mServerIsValid && mWaitingForOpId == -1);\r
+        showRefreshButton(mServerIsChecked && !mServerIsValid && \r
+                mWaitingForOpId > Integer.MAX_VALUE);\r
         mServerStatusView = (TextView) findViewById(R.id.server_status_text);\r
         showServerStatus();\r
         \r
         mServerStatusView = (TextView) findViewById(R.id.server_status_text);\r
         showServerStatus();\r
         \r
@@ -541,7 +542,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
 \r
         /// global state\r
         outState.putString(KEY_AUTH_TOKEN_TYPE, mAuthTokenType);\r
 \r
         /// global state\r
         outState.putString(KEY_AUTH_TOKEN_TYPE, mAuthTokenType);\r
-        outState.putInt(KEY_WAITING_FOR_OP_ID, mWaitingForOpId);\r
+        outState.putLong(KEY_WAITING_FOR_OP_ID, mWaitingForOpId);\r
 \r
         /// Server PRE-fragment state\r
         outState.putInt(KEY_SERVER_STATUS_TEXT, mServerStatusText);\r
 \r
         /// Server PRE-fragment state\r
         outState.putInt(KEY_SERVER_STATUS_TEXT, mServerStatusText);\r
@@ -952,7 +953,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
     private void onGetUserNameFinish(RemoteOperationResult result) {\r
     }\r
 \r
     private void onGetUserNameFinish(RemoteOperationResult result) {\r
-        mWaitingForOpId = -1;\r
+        mWaitingForOpId = Long.MAX_VALUE;\r
         if (result.isSuccess()) {\r
             boolean success = false;\r
             String username = (String) result.getData().get(0);\r
         if (result.isSuccess()) {\r
             boolean success = false;\r
             String username = (String) result.getData().get(0);\r
@@ -985,7 +986,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     }\r
 \r
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
     }\r
 \r
     private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperationResult result) {\r
-        mWaitingForOpId = -1;\r
+        mWaitingForOpId = Long.MAX_VALUE;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 
         //if (result.isTemporalRedirection() && result.isIdPRedirection()) {\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 
         //if (result.isTemporalRedirection() && result.isIdPRedirection()) {\r
@@ -1020,7 +1021,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private void onGetServerInfoFinish(RemoteOperationResult result) {\r
         /// update activity state\r
         mServerIsChecked = true;\r
     private void onGetServerInfoFinish(RemoteOperationResult result) {\r
         /// update activity state\r
         mServerIsChecked = true;\r
-        mWaitingForOpId = -1;\r
+        mWaitingForOpId = Long.MAX_VALUE;\r
         \r
         // update server status, but don't show it yet\r
         updateServerStatusIconAndText(result);\r
         \r
         // update server status, but don't show it yet\r
         updateServerStatusIconAndText(result);\r
@@ -1287,7 +1288,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * @param result        Result of the operation.\r
      */\r
     private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {\r
      * @param result        Result of the operation.\r
      */\r
     private void onGetOAuthAccessTokenFinish(RemoteOperationResult result) {\r
-        mWaitingForOpId = -1;\r
+        mWaitingForOpId = Long.MAX_VALUE;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
         String webdav_path = AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
         String webdav_path = AccountUtils.getWebdavPath(mServerInfo.mVersion, mAuthTokenType);\r
@@ -1326,7 +1327,7 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
      * @param result        Result of the operation.\r
      */\r
     private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
      * @param result        Result of the operation.\r
      */\r
     private void onAuthorizationCheckFinish(RemoteOperationResult result) {\r
-        mWaitingForOpId = -1;\r
+        mWaitingForOpId = Long.MAX_VALUE;\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
         result = new RemoteOperationResult(new RuntimeException("FAKE"));\r
         dismissDialog(WAIT_DIALOG_TAG);\r
 \r
         result = new RemoteOperationResult(new RuntimeException("FAKE"));\r
@@ -1755,8 +1756,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
     private void doOnResumeAndBound() {\r
         Log.wtf(TAG, "registering to listen for operation callbacks" );\r
         mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);\r
     private void doOnResumeAndBound() {\r
         Log.wtf(TAG, "registering to listen for operation callbacks" );\r
         mOperationsServiceBinder.addOperationListener(AuthenticatorActivity.this, mHandler);\r
-        if (mWaitingForOpId != -1) {\r
-            mOperationsServiceBinder.dispatchResultIfFinished(mWaitingForOpId, this);\r
+        if (mWaitingForOpId <= Integer.MAX_VALUE) {\r
+            mOperationsServiceBinder.dispatchResultIfFinished((int)mWaitingForOpId, this);\r
         }\r
     }\r
 \r
         }\r
     }\r
 \r
index a9f92ed..bbecf21 100644 (file)
@@ -261,9 +261,9 @@ public class OperationsService extends Service {
          * Creates and adds to the queue a new operation, as described by operationIntent
          * 
          * @param operationIntent       Intent describing a new operation to queue and execute.
          * Creates and adds to the queue a new operation, as described by operationIntent
          * 
          * @param operationIntent       Intent describing a new operation to queue and execute.
-         * @return                      Identifier of the operation created, or -1 if failed.
+         * @return                      Identifier of the operation created, or null if failed.
          */
          */
-        public int newOperation(Intent operationIntent) {
+        public long newOperation(Intent operationIntent) {
             RemoteOperation operation = null;
             Target target = null;
             try {
             RemoteOperation operation = null;
             Target target = null;
             try {
@@ -347,24 +347,15 @@ public class OperationsService extends Service {
                 mPendingOperations.add(new Pair<Target , RemoteOperation>(target, operation));
                 startService(new Intent(OperationsService.this, OperationsService.class));
                 Log_OC.wtf(TAG, "New operation added, opId: " + operation.hashCode());
                 mPendingOperations.add(new Pair<Target , RemoteOperation>(target, operation));
                 startService(new Intent(OperationsService.this, OperationsService.class));
                 Log_OC.wtf(TAG, "New operation added, opId: " + operation.hashCode());
+                // better id than hash? ; should be good enough by the time being
                 return operation.hashCode();
                 
             } else {
                 return operation.hashCode();
                 
             } else {
-                Log_OC.wtf(TAG, "New operation failed, returned -1");
-                return -1;
+                Log_OC.wtf(TAG, "New operation failed, returned Long.MAX_VALUE");
+                return Long.MAX_VALUE;
             }
         }
 
             }
         }
 
-        public RemoteOperationResult getOperationResultIfFinished(int operationId) {
-            Pair<RemoteOperation, RemoteOperationResult> undispatched = 
-                    mUndispatchedFinishedOperations.remove(operationId);
-            if (undispatched != null) {
-                return undispatched.second;
-            }
-            return null;
-        }
-
-
         public void dispatchResultIfFinished(int operationId, OnRemoteOperationListener listener) {
             Pair<RemoteOperation, RemoteOperationResult> undispatched = 
                     mUndispatchedFinishedOperations.remove(operationId);
         public void dispatchResultIfFinished(int operationId, OnRemoteOperationListener listener) {
             Pair<RemoteOperation, RemoteOperationResult> undispatched = 
                     mUndispatchedFinishedOperations.remove(operationId);