PECengine/ListLibrary2/AuthSrc/CPEngAuthorizationManager.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 manager.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "CPEngAuthorizationManager.h"
       
    20 #include "MPEngAuthorizationObserver.h"
       
    21 
       
    22 #include "CPEngAuthorizationRequest.h"
       
    23 #include "CPEngAuthorizationResponse.h"
       
    24 #include "MPEngListLibFactory.h"
       
    25 
       
    26 #include "PresenceDebugPrint.h"
       
    27 #include <S32mem.h>
       
    28 
       
    29 
       
    30 // MACRO
       
    31 #define CLEAN_LOCK_CLEANUPSTACK( aPushed )\
       
    32     if( aPushed > 1 )\
       
    33         {\
       
    34         CleanupStack::PopAndDestroy();\
       
    35         }\
       
    36     if( aPushed == 1 )\
       
    37         {\
       
    38         CleanupStack::Pop();\
       
    39         }
       
    40 
       
    41 
       
    42 // CONSTANTS
       
    43 /**
       
    44  * Flag used in Authorization item to mark notification done
       
    45  */
       
    46 const TInt32 KAuthNotifyFlag = 0x00001;
       
    47 
       
    48 /**
       
    49  * Base part of the size of the buffer to stream responses
       
    50  */
       
    51 const TInt KBaseSizeForResponse = 20;
       
    52 
       
    53 
       
    54 
       
    55 // ============================ MEMBER FUNCTIONS ===============================
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CPEngAuthorizationManager::CPEngAuthorizationManager()
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CPEngAuthorizationManager::CPEngAuthorizationManager( MPEngListLibFactory& aFactory )
       
    62         : CPEngAuthorizationEngine( aFactory )
       
    63     {
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CPEngAuthorizationManager::ConstructL()
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 void CPEngAuthorizationManager::ConstructL( const CPEngSessionSlotId& aSessionId )
       
    71     {
       
    72     CPEngAuthorizationEngine::ConstructL( aSessionId );
       
    73     RebuildSupportingArraysL();
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CPEngAuthorizationManager::NewLC()
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 CPEngAuthorizationManager* CPEngAuthorizationManager::NewLC(
       
    81     MPEngListLibFactory& aFactory,
       
    82     const CPEngSessionSlotId& aSessionId )
       
    83     {
       
    84     CPEngAuthorizationManager* self =
       
    85         new( ELeave ) CPEngAuthorizationManager( aFactory );
       
    86 
       
    87     CleanupClosePushL( *self );
       
    88     self->ConstructL( aSessionId );
       
    89 
       
    90     return self;
       
    91     }
       
    92 
       
    93 
       
    94 // Destructor
       
    95 CPEngAuthorizationManager::~CPEngAuthorizationManager()
       
    96     {
       
    97     iResponses.ResetAndDestroy();
       
    98     iPendingAuth.Reset();
       
    99     iDeniedAuth.Reset();
       
   100     iObservers.Reset();
       
   101     }
       
   102 
       
   103 
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CPEngAuthorizationManager::Close()
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CPEngAuthorizationManager::Close()
       
   110     {
       
   111     iAccessCount--;
       
   112     if ( iAccessCount == 0 )
       
   113         {
       
   114         iFactory.RemoveAuthorizationManager( this );
       
   115         delete this;
       
   116         }
       
   117     }
       
   118 
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CPEngAuthorizationManager::AuthorizationsCount
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 TInt CPEngAuthorizationManager::AuthorizationsCount() const
       
   125     {
       
   126     return AuthorizationRequestCount();
       
   127     }
       
   128 
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CPEngAuthorizationManager::Authorization()
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 MPEngAuthorizationRequest& CPEngAuthorizationManager::Authorization( TInt aIndex )
       
   135     {
       
   136     CPEngAuthorizationRequest& authRq = AuthorizationRequest( aIndex );
       
   137     authRq.SetAuthorizationManager( this );
       
   138     return authRq;
       
   139     }
       
   140 
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CPEngAuthorizationManager::PendingAuthorizationsCount()
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TInt CPEngAuthorizationManager::PendingAuthorizationsCount() const
       
   147     {
       
   148     return iPendingAuth.Count();
       
   149     }
       
   150 
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CPEngAuthorizationManager::PendingAuthorization()
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 MPEngAuthorizationRequest& CPEngAuthorizationManager::PendingAuthorization(
       
   157     TInt aIndex )
       
   158     {
       
   159     CPEngAuthorizationRequest& authRq =  *iPendingAuth[ aIndex ];
       
   160     authRq.SetAuthorizationManager( this );
       
   161     return authRq;
       
   162     }
       
   163 
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CPEngAuthorizationManager::RespondedAuthorizationsCount()
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 TInt CPEngAuthorizationManager::RespondedAuthorizationsCount() const
       
   170     {
       
   171     return AuthorizationStatusCount();
       
   172     }
       
   173 
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CPEngAuthorizationManager::RespondedAuthorization()
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 const MPEngAuthorizationStatus& CPEngAuthorizationManager::RespondedAuthorization(
       
   180     TInt aIndex ) const
       
   181     {
       
   182     return const_cast<CPEngAuthorizationManager*>( this )->AuthorizationStatus( aIndex );
       
   183     }
       
   184 
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CPEngAuthorizationManager::ServerDeniedAuthorizationsCount()
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 TInt CPEngAuthorizationManager::ServerDeniedAuthorizationsCount() const
       
   191     {
       
   192     return iDeniedAuth.Count();
       
   193     }
       
   194 
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CPEngAuthorizationManager::ServerDeniedAuthorization()
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 const MPEngAuthorizationStatus&
       
   201 CPEngAuthorizationManager::ServerDeniedAuthorization( TInt aIndex ) const
       
   202     {
       
   203     return *( iDeniedAuth[ aIndex ] );
       
   204     }
       
   205 
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CPEngAuthorizationManager::RemoveAllAuthorizationResponses()
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 void CPEngAuthorizationManager::RemoveAllAuthorizationResponses()
       
   212     {
       
   213     iResponses.ResetAndDestroy();
       
   214     ReleaseLock();
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CPEngAuthorizationManager::BufferedTransactionsLC()
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 HBufC16* CPEngAuthorizationManager::BufferedTransactionsLC()
       
   222     {
       
   223     TInt count( iResponses.Count() );
       
   224     if ( !count )
       
   225         {
       
   226         HBufC16* buf = NULL;
       
   227         CleanupStack::PushL( buf );
       
   228         return NULL;
       
   229         }
       
   230 
       
   231     // stream responses to the buffer,
       
   232     // size iBufRespSize is defined for the 8 bit stream, therefore needs to be
       
   233     // divided by 2 for 16 bit buffer.
       
   234     HBufC16* buf = HBufC16::NewLC( ( iBufRespSize + KBaseSizeForResponse  ) / 2 );
       
   235     TPtr16 bufDes16( buf->Des() );
       
   236     // des is accessed as 8bit-> length is double
       
   237     TPtr8 bufDes8( ( TUint8* ) bufDes16.Ptr(), ( bufDes16.MaxLength() * 2 ) );
       
   238 
       
   239     RDesWriteStream	ws( bufDes8 );
       
   240     CleanupClosePushL( ws );
       
   241 
       
   242     // externalize
       
   243     ws.WriteInt32L( count );
       
   244     for ( TInt y( 0 ) ; y < count ; ++y )
       
   245         {
       
   246         iResponses[ y ]->ExternalizeL( ws );
       
   247         }
       
   248 
       
   249     // write 32 bit zero, to ensure stream won't be cut by 8/16 bit conversion
       
   250     ws.WriteInt32L( 0 );
       
   251 
       
   252     // clean stream
       
   253     CleanupStack::PopAndDestroy(); // ws
       
   254     TInt length( bufDes8.Length() / 2 );
       
   255     bufDes16.SetLength( length );
       
   256     return buf;
       
   257     }
       
   258 
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CPEngAuthorizationManager::RegisterAuthorizationObserverL()
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void CPEngAuthorizationManager::RegisterAuthorizationObserverL(
       
   265     MPEngAuthorizationObserver& aObserver )
       
   266     {
       
   267     User::LeaveIfError( iObservers.InsertInAddressOrder( &aObserver ) );
       
   268 
       
   269     // if this was first observer, then reset flags to all be notified
       
   270     if ( iObservers.Count() == 1 )
       
   271         {
       
   272         UpdateAllFlags();
       
   273         }
       
   274     }
       
   275 
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CPEngAuthorizationManager::UnRegisterAuthorizationObserver()
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void CPEngAuthorizationManager::UnRegisterAuthorizationObserver(
       
   282     MPEngAuthorizationObserver& aObserver )
       
   283     {
       
   284     TInt index( iObservers.FindInAddressOrder( &aObserver ) );
       
   285     if ( index != KErrNotFound )
       
   286         {
       
   287         iObservers.Remove( index );
       
   288         }
       
   289     }
       
   290 
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CPEngAuthorizationManager::SetAuthorizedAttributesL()
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 void CPEngAuthorizationManager::SetAuthorizedAttributesL(
       
   297     const CPEngAuthorizationRequest& aAuthRequest,
       
   298     TArray<TUint32>& aAttributes,
       
   299     TBool aKeepOldAuthorization /* ETrue */ )
       
   300     {
       
   301     TInt pushed( EnableLockLC() );
       
   302     CPEngAuthorizationResponse& authResponse =
       
   303         AuthorizationResponseL( aAuthRequest.Id() );
       
   304 
       
   305     authResponse.SetResponseType( MPEngAuthorizationRequest::EPEngAuthAccepted );
       
   306     if ( aKeepOldAuthorization )
       
   307         {
       
   308         authResponse.SetAttributesToAuthorizeL(
       
   309             aAttributes,
       
   310             &aAuthRequest.AuthorizedAttributes() );
       
   311         }
       
   312     else
       
   313         {
       
   314         authResponse.SetAttributesToAuthorizeL( aAttributes, NULL );
       
   315         }
       
   316 
       
   317     CLEAN_LOCK_CLEANUPSTACK( pushed );
       
   318     }
       
   319 
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CPEngAuthorizationManager::DenyAuthorizationL()
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 void CPEngAuthorizationManager::DenyAuthorizationL( const TDesC& aUserId )
       
   326     {
       
   327     TInt pushed( EnableLockLC() );
       
   328     CPEngAuthorizationResponse& authResponse = AuthorizationResponseL( aUserId );
       
   329     authResponse.SetResponseType( MPEngAuthorizationRequest::EPEngAuthDenied );
       
   330 
       
   331     CLEAN_LOCK_CLEANUPSTACK( pushed );
       
   332     }
       
   333 
       
   334 // -----------------------------------------------------------------------------
       
   335 // CPEngAuthorizationManager::CancelAuthorizationL()
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 void CPEngAuthorizationManager::CancelAuthorizationL( const TDesC& aUserId )
       
   339     {
       
   340     TInt pushed( EnableLockLC() );
       
   341     CPEngAuthorizationResponse& authResponse = AuthorizationResponseL( aUserId );
       
   342     authResponse.SetResponseType( MPEngAuthorizationRequest::EPEngAuthCanceled );
       
   343 
       
   344     CLEAN_LOCK_CLEANUPSTACK( pushed );
       
   345     }
       
   346 
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // CPEngAuthorizationManager::RemoveAuthorizationResponse()
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 void CPEngAuthorizationManager::RemoveAuthorizationResponse( const TDesC& aUserId )
       
   353     {
       
   354     TInt index( FindAuthResponse( iResponses, aUserId ) );
       
   355     if ( index == KErrNotFound )
       
   356         {
       
   357         return;
       
   358         }
       
   359     delete iResponses[ index  ];
       
   360     iResponses.Remove( index );
       
   361     if ( iResponses.Count() == 0 )
       
   362         {
       
   363         // release lock
       
   364         ReleaseLock();
       
   365         }
       
   366     }
       
   367 
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CPEngAuthorizationManager::HandleSIDsChangeL()
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CPEngAuthorizationManager::HandleSIDsChangeL( CPtrCArray& aChangedSIDs )
       
   374     {
       
   375     CPEngAuthorizationEngine::HandleSIDsChangeL( aChangedSIDs );
       
   376     RebuildSupportingArraysL();
       
   377     NotifyAllChangesToObservers();
       
   378     }
       
   379 
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CPEngAuthorizationManager::HandleSIDNotifyError()
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 void CPEngAuthorizationManager::HandleSIDNotifyError( TInt aError )
       
   386     {
       
   387     CPEngAuthorizationEngine::HandleSIDNotifyError( aError );
       
   388     TInt count( iObservers.Count() );
       
   389     for ( TInt x( 0 ) ; x < count ; ++x )
       
   390         {
       
   391         iObservers[ x ]->HandleAuthorizationEngineError( aError );
       
   392         }
       
   393     }
       
   394 
       
   395 
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // CPEngAuthorizationManager::RebuildSupportingArraysL()
       
   399 // -----------------------------------------------------------------------------
       
   400 //
       
   401 void CPEngAuthorizationManager::RebuildSupportingArraysL()
       
   402     {
       
   403     iPendingAuth.Reset();
       
   404     TInt count( AuthorizationRequestCount() );
       
   405     for ( TInt x( 0 ) ; x < count ; ++x )
       
   406         {
       
   407         if ( AuthorizationRequest( x ).AuthorizationState() ==
       
   408              MPEngAuthorizationRequest::EPEngAuthPending )
       
   409             {
       
   410             iPendingAuth.AppendL( &( AuthorizationRequest( x ) ) );
       
   411             }
       
   412         }
       
   413 
       
   414     iDeniedAuth.Reset();
       
   415 
       
   416     count = AuthorizationStatusCount();
       
   417     for ( TInt y( 0 ) ; y < count ; ++y )
       
   418         {
       
   419         if ( AuthorizationStatus( y ).AuthorizationStatus() ==
       
   420              MPEngAuthorizationStatus::EPEngAuthDenied )
       
   421             {
       
   422             iDeniedAuth.AppendL( &( AuthorizationStatus( y ) ) );
       
   423             }
       
   424         }
       
   425     }
       
   426 
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 // CPEngAuthorizationManager::NotifyAllChangesToObservers()
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 void CPEngAuthorizationManager::NotifyAllChangesToObservers()
       
   433     {
       
   434     // notify about new pending authorizations
       
   435     TInt count( AuthorizationRequestCount() );
       
   436     for ( TInt x( 0 ) ; x < count ; ++x )
       
   437         {
       
   438         CPEngAuthorizationRequest& authRequest = AuthorizationRequest( x );
       
   439         if ( ( authRequest.AuthorizationState() ==
       
   440                MPEngAuthorizationRequest::EPEngAuthPending ) &&
       
   441              !( authRequest.LocalFlags() & KAuthNotifyFlag ) )
       
   442             {
       
   443             // inform observers
       
   444             NotifyChangeToObservers(
       
   445                 &MPEngAuthorizationObserver::HandleNewPendingAuthorization,
       
   446                 static_cast<MPEngAuthorizationRequest&>( authRequest ) );
       
   447 
       
   448             authRequest.SetLocalFlags( KAuthNotifyFlag );
       
   449             }
       
   450         }
       
   451 
       
   452 
       
   453     count = AuthorizationStatusCount();
       
   454     for ( TInt x( 0 ) ; x < count ; ++x )
       
   455         {
       
   456         CPEngAuthorizationResponse& authResponse = AuthorizationStatus( x );
       
   457         if ( !( authResponse.LocalFlags() & KAuthNotifyFlag ) )
       
   458             {
       
   459             // inform observers
       
   460             NotifyChangeToObservers(
       
   461                 &MPEngAuthorizationObserver::HandlerNewAuthorizationStatus,
       
   462                 static_cast<const MPEngAuthorizationStatus&>( authResponse ) );
       
   463 
       
   464             authResponse.SetLocalFlags( KAuthNotifyFlag );
       
   465             }
       
   466         }
       
   467 
       
   468     // inform observers about global change
       
   469     count = iObservers.Count();
       
   470     for ( TInt x( 0 ) ; x < count ; ++x )
       
   471         {
       
   472         iObservers[ x ]->HandleAuthorizationEngineUpdate();
       
   473         }
       
   474     }
       
   475 
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // CPEngAuthorizationManager::NotifyChangeToObservers()
       
   479 // -----------------------------------------------------------------------------
       
   480 //
       
   481 template <class NotifyItem>
       
   482 void CPEngAuthorizationManager::NotifyChangeToObservers(
       
   483     void ( MPEngAuthorizationObserver:: *aNotifFunc )(  NotifyItem& ),
       
   484     NotifyItem& aArgument )
       
   485     {
       
   486     TInt count( iObservers.Count() );
       
   487     for ( TInt x( 0 ) ; x < count ; ++x )
       
   488         {
       
   489         MPEngAuthorizationObserver& observer = *( iObservers[ x ] );
       
   490         ( observer.*aNotifFunc )( aArgument );
       
   491         }
       
   492     }
       
   493 
       
   494 
       
   495 // -----------------------------------------------------------------------------
       
   496 // CPEngAuthorizationManager::UpdateAllFlags()
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 void CPEngAuthorizationManager::UpdateAllFlags()
       
   500     {
       
   501     TInt count( AuthorizationRequestCount() );
       
   502     for ( TInt x( 0 ) ; x < count ; ++x )
       
   503         {
       
   504         AuthorizationRequest( x ).SetLocalFlags( KAuthNotifyFlag );
       
   505         }
       
   506 
       
   507     count = AuthorizationStatusCount();
       
   508     for ( TInt x( 0 ) ; x < count ; ++x )
       
   509         {
       
   510         AuthorizationStatus( x ).SetLocalFlags( KAuthNotifyFlag );
       
   511         }
       
   512     }
       
   513 
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // CPEngAuthorizationManager::AuthorizationResponseL()
       
   517 // -----------------------------------------------------------------------------
       
   518 //
       
   519 CPEngAuthorizationResponse& CPEngAuthorizationManager::AuthorizationResponseL(
       
   520     const TDesC& aUserId )
       
   521     {
       
   522     TInt index( CPEngAuthorizationEngine::FindAuthResponse( iResponses, aUserId ) );
       
   523     CPEngAuthorizationResponse* authResponse = NULL;
       
   524     if ( index == KErrNotFound )
       
   525         {
       
   526         authResponse = CPEngAuthorizationResponse::NewLC( aUserId, iBufRespSize );
       
   527         iResponses.AppendL( authResponse );
       
   528         CleanupStack::Pop( authResponse );
       
   529         }
       
   530 
       
   531     else
       
   532         {
       
   533         authResponse = iResponses[ index ];
       
   534         }
       
   535 
       
   536     return *authResponse;
       
   537     }
       
   538 
       
   539 
       
   540 // -----------------------------------------------------------------------------
       
   541 // CPEngAuthorizationManager::EnableLockLC()
       
   542 // -----------------------------------------------------------------------------
       
   543 //
       
   544 TBool CPEngAuthorizationManager::EnableLockLC()
       
   545     {
       
   546     if ( iResponses.Count() == 0 )
       
   547         {
       
   548         CPEngStoreEntry::LockLC();
       
   549         return ETrue;
       
   550         }
       
   551 
       
   552     return EFalse;
       
   553     }
       
   554 
       
   555 
       
   556 // -----------------------------------------------------------------------------
       
   557 // CPEngAuthorizationManager::ReleaseLock()
       
   558 // -----------------------------------------------------------------------------
       
   559 //
       
   560 void CPEngAuthorizationManager::ReleaseLock()
       
   561     {
       
   562     CPEngStoreEntry::Unlock();
       
   563     }
       
   564 
       
   565 
       
   566 // -----------------------------------------------------------------------------
       
   567 // CPEngAuthorizationManager::Release()
       
   568 // -----------------------------------------------------------------------------
       
   569 //
       
   570 void CPEngAuthorizationManager::Release()
       
   571     {
       
   572     iResponses.ResetAndDestroy();
       
   573     CPEngStoreEntry::Release();
       
   574     }
       
   575 
       
   576 
       
   577 //  End of File
       
   578