PECengine/ListLibrary2/AuthSrc/CPEngAuthorizationEngine.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Reactive authorization engine base class.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32std.h>
       
    20 #include "CPEngAuthorizationEngine.h"
       
    21 #include "CPEngAuthorizationResponse.h"
       
    22 #include "CPEngAuthorizationRequest.h"
       
    23 #include "CPEngAuthorizationItem.h"
       
    24 
       
    25 #include "PEngStorageGlobals.h"
       
    26 #include "MPEngStorageManager.h"
       
    27 #include "PEngStorageManager.h"
       
    28 #include "CPEngSessionSlotId.h"
       
    29 #include "RObjectArray.h"
       
    30 #include "PresenceDebugPrint.h"
       
    31 
       
    32 
       
    33 
       
    34 #define ARRAY_ORDER_REQ TLinearOrder<CPEngAuthorizationRequest> (CPEngAuthorizationRequest::Compare)
       
    35 #define ARRAY_ORDER_STAT TLinearOrder<CPEngAuthorizationResponse> (CPEngAuthorizationResponse::Compare)
       
    36 
       
    37 
       
    38 _LIT( KPEngAuthorizationsSId, "PEngAuthorizations" );
       
    39 
       
    40 // Init size for the store enry fo the authorization engine
       
    41 // 2x4 bytes for two array counts
       
    42 static const TInt KInitSizeOfStoreEntry = 8;
       
    43 
       
    44 
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CPEngAuthorizationEngine::CPEngAuthorizationEngine()
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CPEngAuthorizationEngine::CPEngAuthorizationEngine( MPEngListLibFactory& aFactory )
       
    52         : CPEngStoreEntry( EPEngMixedCachedVersionCon ),
       
    53         iFactory( aFactory ),
       
    54         iAccessCount( 1 ) // init ref count on 1
       
    55     {
       
    56     iSize = KInitSizeOfStoreEntry;
       
    57     }
       
    58 
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CPEngAuthorizationEngine::ConstructL()
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 void CPEngAuthorizationEngine::ConstructL( const CPEngSessionSlotId& aSessionSlot )
       
    65     {
       
    66     iSessionId = aSessionSlot.CloneL();
       
    67 
       
    68     MPEngStorageManager* storageManager =
       
    69         PEngStorageManager::GetStorageManagerL( *iSessionId );
       
    70     CleanupClosePushL( *storageManager );
       
    71 
       
    72     CPEngStoreEntry::BaseConstructL( *storageManager );
       
    73 
       
    74     CleanupStack::PopAndDestroy(); //storageManager
       
    75 
       
    76     iStorageManager->RetrieveL( *this );
       
    77     }
       
    78 
       
    79 
       
    80 // Destructor
       
    81 CPEngAuthorizationEngine::~CPEngAuthorizationEngine()
       
    82     {
       
    83     iAuthRequests.ResetAndDestroy();
       
    84     iAuthStatuses.ResetAndDestroy();
       
    85     delete iSessionId;
       
    86     }
       
    87 
       
    88 
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CPEngAuthorizationEngine::SessionId()
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 const CPEngSessionSlotId& CPEngAuthorizationEngine::SessionId() const
       
    95     {
       
    96     return *iSessionId;
       
    97     }
       
    98 
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CPEngAuthorizationEngine::Open()
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CPEngAuthorizationEngine::Open()
       
   105     {
       
   106     iAccessCount++;
       
   107     }
       
   108 
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CPEngAuthorizationEngine::UpdateAuthorizationStateL()
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CPEngAuthorizationEngine::UpdateAuthorizationStateL(
       
   115     CPEngAuthorizationResponse& aAuthResponse,
       
   116     TBool aPartialAcceptance )
       
   117     {
       
   118     TInt index( FindUserIdItem( iAuthRequests, aAuthResponse.Id() ) );
       
   119     CPEngAuthorizationRequest* authRq = NULL;
       
   120 
       
   121     if ( index == KErrNotFound )
       
   122         {
       
   123         CPEngAuthorizationRequest* authRq = CPEngAuthorizationRequest::NewLC(
       
   124                                                 aAuthResponse.Id(),
       
   125                                                 iSize );
       
   126 
       
   127         iAuthRequests.InsertInOrderL( authRq, ARRAY_ORDER_REQ );
       
   128         CleanupStack::Pop();
       
   129         }
       
   130     else
       
   131         {
       
   132         authRq = iAuthRequests[ index ];
       
   133         }
       
   134 
       
   135 
       
   136     // update authorization
       
   137     authRq->SetAuthResponse(
       
   138         static_cast<MPEngAuthorizationRequest::TPEngAuthorizationResponseType>
       
   139         ( aAuthResponse.AuthorizationStatus()  ) );
       
   140 
       
   141     authRq->SetAuthState( MPEngAuthorizationRequest::EPEngAuthAnswered );
       
   142 
       
   143     // copy authorized attributes
       
   144     if ( aPartialAcceptance )
       
   145         {
       
   146         authRq->UpdateAuthorizedAttributesL( aAuthResponse.AuthorizedAttributes() );
       
   147         }
       
   148     else
       
   149         {
       
   150         authRq->UpdateAuthorizedAttributesL( authRq->RequestedAttributes() );
       
   151         }
       
   152 
       
   153     StoreL();
       
   154     }
       
   155 
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CPEngAuthorizationEngine::RemoveAuthorizationRequestL()
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CPEngAuthorizationEngine::RemoveAuthorizationRequestL(
       
   162     CPEngAuthorizationResponse& aAuthResponse )
       
   163     {
       
   164     TInt index( FindUserIdItem( iAuthRequests, aAuthResponse.Id() ) );
       
   165     if ( index < 0 )
       
   166         {
       
   167         return;
       
   168         }
       
   169     delete iAuthRequests[ index ];
       
   170     iAuthRequests.Remove( index );
       
   171 
       
   172     StoreL();
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CPEngAuthorizationEngine::AddAuthRequestLX()
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CPEngAuthorizationEngine::AddAuthRequestLX(
       
   180     CPEngAuthorizationRequest* aAuthRequest )
       
   181     {
       
   182     TInt err( iAuthRequests.InsertInOrder( aAuthRequest, ARRAY_ORDER_REQ ) );
       
   183     if ( err == KErrNone )
       
   184         {
       
   185         CleanupStack::Pop( aAuthRequest );
       
   186         }
       
   187 
       
   188     if ( err == KErrAlreadyExists )
       
   189         {
       
   190         // it is there already, update set of requested attributes
       
   191         TInt index( FindUserIdItem( iAuthRequests, aAuthRequest->Id() ) );
       
   192         iAuthRequests[ index ]->UpdateRequestedAttributesL(
       
   193             aAuthRequest->RequestedAttributes() );
       
   194         CleanupStack::PopAndDestroy( aAuthRequest );
       
   195         err = KErrNone;
       
   196         }
       
   197 
       
   198     User::LeaveIfError( err );
       
   199     CPEngStoreEntry::StoreL();
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CPEngAuthorizationEngine::AddAuthStatusLX()
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 void CPEngAuthorizationEngine::AddAuthStatusLX(
       
   207     CPEngAuthorizationResponse* aAuthStatus )
       
   208     {
       
   209     TInt err( iAuthStatuses.InsertInOrder( aAuthStatus, ARRAY_ORDER_STAT ) );
       
   210     if ( err == KErrNone )
       
   211         {
       
   212         CleanupStack::Pop( aAuthStatus ); // aAuthRequest
       
   213         }
       
   214     if ( err == KErrAlreadyExists )
       
   215         {
       
   216         // it is there already, update set of requested attributes
       
   217         TInt index( FindUserIdItem( iAuthStatuses, aAuthStatus->Id() ) );
       
   218 
       
   219         iAuthStatuses[ index ]->SetAttributesToAuthorizeL(
       
   220             aAuthStatus->AuthorizedAttributes().Array(),
       
   221             NULL );
       
   222 
       
   223         CleanupStack::PopAndDestroy( aAuthStatus ); // aAuthRequest
       
   224         err = KErrNone;
       
   225         }
       
   226 
       
   227     User::LeaveIfError( err );
       
   228     CPEngStoreEntry::StoreL();
       
   229     }
       
   230 
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CPEngAuthorizationEngine::SizeCounter()
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 TInt& CPEngAuthorizationEngine::SizeCounter()
       
   237     {
       
   238     return iSize;
       
   239     }
       
   240 
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CPEngAuthorizationEngine::LockForNetworkPublishing()
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CPEngAuthorizationEngine::LockForNetworkPublishing()
       
   247     {
       
   248     CPEngStoreEntry::Lock( EStorageLockLevelHigh );
       
   249     }
       
   250 
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CPEngAuthorizationEngine::UnLockForNetworkPublishing()
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void CPEngAuthorizationEngine::UnLockForNetworkPublishing()
       
   257     {
       
   258     CPEngStoreEntry::Unlock();
       
   259     }
       
   260 
       
   261 
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CPEngContactListMainArray::ExternalizeL()
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 void CPEngAuthorizationEngine::ExternalizeL( RWriteStream& aStream,
       
   268                                              TPEngStorageType aStorageType ) const
       
   269     {
       
   270     switch ( aStorageType )
       
   271         {
       
   272         case EPEngStorageBasicCached:
       
   273             {
       
   274             TInt count( iAuthRequests.Count() );
       
   275             aStream.WriteInt32L( count );
       
   276             for ( TInt x( 0 ) ; x < count ; ++x )
       
   277                 {
       
   278                 iAuthRequests[ x ]->ExternalizeL( aStream );
       
   279                 }
       
   280 
       
   281             count = iAuthStatuses.Count();
       
   282             aStream.WriteInt32L( count );
       
   283             for ( TInt y( 0 ) ; y < count ; ++y )
       
   284                 {
       
   285                 iAuthStatuses[ y ]->ExternalizeL( aStream );
       
   286                 }
       
   287             break;
       
   288             }
       
   289 
       
   290 
       
   291         default:
       
   292             {
       
   293             break;
       
   294             }
       
   295         }
       
   296     }
       
   297 
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // CPEngAuthorizationEngine::InternalizeL()
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 void CPEngAuthorizationEngine::InternalizeL( RReadStream& aStream,
       
   304                                              TPEngStorageType aStorageType )
       
   305     {
       
   306     switch ( aStorageType )
       
   307         {
       
   308         case EPEngStorageBasicCached:
       
   309             {
       
   310             // authorization requests
       
   311             RObjectArray<CPEngAuthorizationRequest> authRequests;
       
   312             CleanupClosePushL( authRequests );
       
   313 
       
   314             TInt count( aStream.ReadInt32L() );
       
   315             for ( TInt x( 0 ) ; x < count ; ++x )
       
   316                 {
       
   317                 CPEngAuthorizationRequest* authRq =
       
   318                     CPEngAuthorizationRequest::NewLC( aStream,
       
   319                                                       iSize );
       
   320                 authRequests.AppendL( authRq );
       
   321                 CleanupStack::Pop( authRq ); // authRq
       
   322                 // adopt flag and clean array
       
   323                 AdoptAndClean( iAuthRequests, *authRq );
       
   324                 }
       
   325             AppendRemoveL( iAuthRequests, authRequests );
       
   326             CleanupStack::PopAndDestroy(); // authRequests
       
   327 
       
   328 
       
   329             // authorization statuses
       
   330             RObjectArray<CPEngAuthorizationResponse> authStatuses;
       
   331             CleanupClosePushL( authStatuses );
       
   332 
       
   333             count = aStream.ReadInt32L();
       
   334             for ( TInt y( 0 ) ; y < count ; ++y )
       
   335                 {
       
   336                 CPEngAuthorizationResponse* authRes =
       
   337                     CPEngAuthorizationResponse::NewLC( aStream,
       
   338                                                        iSize );
       
   339                 authStatuses.AppendL( authRes );
       
   340                 CleanupStack::Pop( authRes ); // authRes
       
   341                 // adopt flag and clean array
       
   342                 AdoptAndClean( iAuthStatuses, *authRes );
       
   343                 }
       
   344             AppendRemoveL( iAuthStatuses, authStatuses );
       
   345             CleanupStack::PopAndDestroy(); // authStatuses
       
   346             break;
       
   347             }
       
   348 
       
   349         default:
       
   350             {
       
   351             break;
       
   352             }
       
   353         }
       
   354     }
       
   355 
       
   356 // -----------------------------------------------------------------------------
       
   357 // CPEngAuthorizationEngine::StorageId()
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 const TDesC& CPEngAuthorizationEngine::StorageId() const
       
   361     {
       
   362     return KPEngAuthorizationsSId;
       
   363     }
       
   364 
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CPEngAuthorizationEngine::EntrySize()
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 TUint32 CPEngAuthorizationEngine::EntrySize() const
       
   371     {
       
   372     return iSize;
       
   373     }
       
   374 
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // CPEngAuthorizationEngine::HandleSIDsChangeL()
       
   378 // -----------------------------------------------------------------------------
       
   379 //
       
   380 void CPEngAuthorizationEngine::HandleSIDsChangeL( CPtrCArray& /* aChangedSIDs */ )
       
   381     {
       
   382     iStorageManager->RetrieveL( *this );
       
   383     }
       
   384 
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CPEngAuthorizationEngine::HandleSIDNotifyError()
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 void CPEngAuthorizationEngine::HandleSIDNotifyError( TInt /* aError */ )
       
   391     {
       
   392     }
       
   393 
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CPEngAuthorizationEngine::AuthorizationRequestCount()
       
   397 // -----------------------------------------------------------------------------
       
   398 //
       
   399 TInt CPEngAuthorizationEngine::AuthorizationRequestCount() const
       
   400     {
       
   401     return iAuthRequests.Count();
       
   402     }
       
   403 
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CPEngAuthorizationEngine::AuthorizationRequest()
       
   407 // -----------------------------------------------------------------------------
       
   408 //
       
   409 CPEngAuthorizationRequest& CPEngAuthorizationEngine::AuthorizationRequest(
       
   410     TInt aIndex )
       
   411     {
       
   412     return *iAuthRequests[ aIndex ];
       
   413     }
       
   414 
       
   415 
       
   416 // -----------------------------------------------------------------------------
       
   417 // CPEngAuthorizationEngine::AuthorizationStatusCount()
       
   418 // -----------------------------------------------------------------------------
       
   419 //
       
   420 TInt CPEngAuthorizationEngine::AuthorizationStatusCount() const
       
   421     {
       
   422     return iAuthStatuses.Count();
       
   423     }
       
   424 
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CPEngAuthorizationEngine::AuthorizationStatus()
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 CPEngAuthorizationResponse& CPEngAuthorizationEngine::AuthorizationStatus(
       
   431     TInt aIndex )
       
   432     {
       
   433     return * iAuthStatuses[ aIndex ];
       
   434     }
       
   435 
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // CPEngAuthorizationEngine::AuthorizationRequestL()
       
   439 // -----------------------------------------------------------------------------
       
   440 //
       
   441 CPEngAuthorizationRequest& CPEngAuthorizationEngine::AuthorizationRequestL(
       
   442     const TDesC& aUserId )
       
   443     {
       
   444     TInt index ( FindUserIdItem( iAuthRequests, aUserId ) );
       
   445     User::LeaveIfError( index );
       
   446     return *iAuthRequests[ index ];
       
   447     }
       
   448 
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 // CPEngAuthorizationEngine::FindAuthResponse()
       
   452 // -----------------------------------------------------------------------------
       
   453 //
       
   454 TInt CPEngAuthorizationEngine::FindAuthResponse(
       
   455     const RPointerArray<CPEngAuthorizationResponse>& aArray,
       
   456     const TDesC& aUserId  )
       
   457     {
       
   458     return FindUserIdItem( aArray, aUserId );
       
   459     }
       
   460 
       
   461 
       
   462 // -----------------------------------------------------------------------------
       
   463 // CPEngAuthorizationEngine::FindUserIdItem()
       
   464 // -----------------------------------------------------------------------------
       
   465 //
       
   466 template <class T>
       
   467 TInt CPEngAuthorizationEngine::FindUserIdItem( const RPointerArray<T>& aArray,
       
   468                                                const TDesC& aUserId )
       
   469     {
       
   470     TInt l( 0 );
       
   471     TInt r( aArray.Count() );
       
   472     while ( r > l )
       
   473         {
       
   474         TInt inx = ( l + r ) >> 1;
       
   475 
       
   476         TInt k ( aUserId.CompareF( aArray[ inx ]->Id() ) );
       
   477         if ( k == 0 )
       
   478             {
       
   479             return inx;
       
   480             }
       
   481         else if ( k > 0 )
       
   482             l = inx + 1;
       
   483         else
       
   484             r = inx;
       
   485         }
       
   486     return KErrNotFound;
       
   487     }
       
   488 
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CPEngAuthorizationEngine::AdoptAndClean()
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 template <class T>
       
   495 void CPEngAuthorizationEngine::AdoptAndClean( RPointerArray<T>& aArray,
       
   496                                               T& aNewItem )
       
   497     {
       
   498     TInt count( aArray.Count() );
       
   499     while ( count != 0 )
       
   500         {
       
   501         TInt err( aArray[ 0 ]->Id().CompareF( aNewItem.Id() ) );
       
   502         if ( err == KErrNone )
       
   503             {
       
   504             aNewItem.DoUpdateLocalFlags( *( aArray[ 0 ] ) );
       
   505             delete aArray[ 0 ];
       
   506             aArray.Remove( 0 );
       
   507             return;
       
   508             }
       
   509 
       
   510         if ( err > 0 ) // no sense to loop furder in the array
       
   511             {
       
   512             return;
       
   513             }
       
   514 
       
   515         // clean array
       
   516         delete aArray[ 0 ];
       
   517         aArray.Remove( 0 );
       
   518         count--;
       
   519         }
       
   520     }
       
   521 
       
   522 
       
   523 // -----------------------------------------------------------------------------
       
   524 // CPEngAuthorizationEngine::AppendRemoveL()
       
   525 // -----------------------------------------------------------------------------
       
   526 //
       
   527 template <class T>
       
   528 void CPEngAuthorizationEngine::AppendRemoveL( RPointerArray<T>& aTarget,
       
   529                                               RPointerArray<T>& aSource )
       
   530     {
       
   531     aTarget.ResetAndDestroy();
       
   532     TInt count( aSource.Count() );
       
   533     while ( count != 0 )
       
   534         {
       
   535         aTarget.AppendL( aSource[ 0 ] );
       
   536         aSource.Remove( 0 );
       
   537         count--;
       
   538         }
       
   539     }
       
   540 
       
   541 //  End of File
       
   542 
       
   543 
       
   544 
       
   545 
       
   546 
       
   547 
       
   548 
       
   549 
       
   550 
       
   551 
       
   552