wmdrm/wmdrmengine/wmdrmserver/server/src/slotenumeratorcache.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2007 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:  WMDRM Server implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "slotenumeratorcache.h"
       
    20 #include "enumeratordata.h"
       
    21 #include "wmdrmserver.h"
       
    22 
       
    23 #define _LOGGING_FILE L"wmdrmserver.txt"
       
    24 
       
    25 #include "flogger.h"
       
    26 #include "logfn.h"
       
    27 
       
    28 CSlotEnumeratorCache* CSlotEnumeratorCache::NewL( CWmDrmServer* aServer )
       
    29     {
       
    30     CSlotEnumeratorCache* self = new (ELeave) CSlotEnumeratorCache( aServer );
       
    31     CleanupStack::PushL( self );
       
    32     self->ConstructL();
       
    33     CleanupStack::Pop();
       
    34     return self;
       
    35     }
       
    36 
       
    37 void CSlotEnumeratorCache::ConstructL()
       
    38     {
       
    39     LOGFN( "CSlotEnumeratorCache::ConstructL" );
       
    40     iMaxCachedEnumerators = KDefaultMaxCachedEnumerators;
       
    41     }
       
    42     
       
    43 CSlotEnumeratorCache::CSlotEnumeratorCache(
       
    44     CWmDrmServer* aServer):
       
    45     iServer( aServer )
       
    46     {
       
    47     LOGFN( "CSlotEnumeratorCache::CSlotEnumeratorCache" );
       
    48     }
       
    49 
       
    50 CSlotEnumeratorCache::~CSlotEnumeratorCache()
       
    51     {
       
    52     LOGFN( "CSlotEnumeratorCache::~CSlotEnumeratorCache" );
       
    53     iEnumeratorCache.ResetAndDestroy();
       
    54     iEnumeratorCache.Close();
       
    55     }
       
    56 
       
    57 CEnumeratorData* CSlotEnumeratorCache::GetEnumeratorDataL(
       
    58     const TDesC8& aStore,
       
    59     const TDesC8& aNamespace,
       
    60     const TDesC8& aHashKey )
       
    61     {
       
    62     CEnumeratorData* r = NULL;
       
    63     TInt i = 0;
       
    64     
       
    65     LOGFN( "CSlotEnumeratorCache::GetEnumeratorDataL" );
       
    66     LOG( aNamespace );
       
    67     LOG( aHashKey );
       
    68     for ( i = 0; r == NULL && i < iEnumeratorCache.Count(); i++ )
       
    69         {
       
    70         CEnumeratorData* data = iEnumeratorCache[i];
       
    71         if ( /*data->iStore == aStore &&*/
       
    72              data->iNamespace == aNamespace &&
       
    73              data->iHashKey == aHashKey )
       
    74             {
       
    75             r = iEnumeratorCache[i];
       
    76             Claim( r );
       
    77             }
       
    78         }
       
    79     if ( r == NULL )
       
    80         {
       
    81         Cleanup();
       
    82         r = CEnumeratorData::NewL( iServer, aStore, aNamespace, aHashKey );
       
    83         CleanupStack::PushL( r );
       
    84         iEnumeratorCache.AppendL( r );
       
    85         Claim( r );
       
    86         CleanupStack::Pop( r );
       
    87         }
       
    88     return r;
       
    89     }
       
    90 
       
    91 void CSlotEnumeratorCache::Claim( CEnumeratorData* aData )
       
    92     {
       
    93     LOGFN( "CSlotEnumeratorCache::ClaimL" );
       
    94     aData->iReferences++;
       
    95     }
       
    96 
       
    97 void CSlotEnumeratorCache::Release( CEnumeratorData* aData )
       
    98     {
       
    99     LOGFN( "CSlotEnumeratorCache::ReleaseL" );
       
   100     aData->iReferences--;
       
   101     }
       
   102 
       
   103 TInt CSlotEnumeratorCache::AddEntryL(
       
   104     const TDesC8& /*aStore*/,
       
   105     const TDesC8& aNamespace,
       
   106     const TDesC8& aHashKey,
       
   107     const TDesC8& aUniqueKey )
       
   108     {
       
   109     TInt r = KErrNone;
       
   110     TInt i;
       
   111     CEnumeratorData* entry = NULL;
       
   112     HBufC8* fileName;
       
   113     
       
   114     LOGFNR( "CSlotEnumeratorCache::AddEntryL", r );
       
   115     LOG( aNamespace );
       
   116     LOG( aHashKey );
       
   117     LOG( aUniqueKey );
       
   118     for ( i = 0; entry == NULL && i < iEnumeratorCache.Count(); i++ )
       
   119         {
       
   120         CEnumeratorData* data = iEnumeratorCache[i];
       
   121         if ( /*data->iStore == aStore &&*/
       
   122              data->iNamespace == aNamespace &&
       
   123              data->iHashKey == aHashKey )
       
   124             {
       
   125             entry = iEnumeratorCache[i];
       
   126             }
       
   127         }
       
   128     if ( entry != NULL )
       
   129         {
       
   130         i = 0;
       
   131         while ( i < entry->iEntries.Count() && *entry->iEntries[i] != aUniqueKey )
       
   132             {
       
   133             i++;
       
   134             }
       
   135         if ( i == entry->iEntries.Count() )
       
   136             {
       
   137             fileName = aUniqueKey.AllocLC();
       
   138             entry->iEntries.AppendL( fileName );
       
   139             CleanupStack::Pop( fileName );
       
   140             }
       
   141         }
       
   142     return r;
       
   143     }
       
   144 
       
   145 TInt CSlotEnumeratorCache::DeleteEntryL(
       
   146     const TDesC8& /*aStore*/,
       
   147     const TDesC8& aNamespace,
       
   148     const TDesC8& aHashKey,
       
   149     const TDesC8& aUniqueKey )
       
   150     {
       
   151     TInt r = KErrNone;
       
   152     TInt i;
       
   153     CEnumeratorData* entry = NULL;
       
   154     
       
   155     LOGFNR( "CSlotEnumeratorCache::DeleteEntryL", r );
       
   156     
       
   157     for ( i = 0; entry == NULL && i < iEnumeratorCache.Count(); i++ )
       
   158         {
       
   159         CEnumeratorData* data = iEnumeratorCache[i];
       
   160         if ( /*data->iStore == aStore &&*/
       
   161              data->iNamespace == aNamespace &&
       
   162              data->iHashKey == aHashKey )
       
   163             {
       
   164             entry = iEnumeratorCache[i];
       
   165             }
       
   166         }
       
   167     if ( entry != NULL )
       
   168         {
       
   169         i = 0;
       
   170         while ( i < entry->iEntries.Count() && *entry->iEntries[i] != aUniqueKey )
       
   171             {
       
   172             i++;
       
   173             }
       
   174         if ( i < entry->iEntries.Count() )
       
   175             {
       
   176             delete entry->iEntries[i];
       
   177             entry->iEntries.Remove( i );
       
   178             }
       
   179         }
       
   180     return r;
       
   181     }
       
   182 
       
   183 void CSlotEnumeratorCache::Cleanup()
       
   184     {
       
   185     TInt i = 0;
       
   186     TInt minReferencesCount = 0;
       
   187     TInt minReferencesIndex = 0;
       
   188     
       
   189     LOGFN( "CSlotEnumeratorCache::CleanupL" );
       
   190     LOG3( "Used: %d, max: %d", iEnumeratorCache.Count(), iMaxCachedEnumerators );
       
   191     
       
   192     if ( iEnumeratorCache.Count() >= iMaxCachedEnumerators )
       
   193         {
       
   194         minReferencesCount = iEnumeratorCache[minReferencesIndex]->iReferences;
       
   195         
       
   196         while ( iEnumeratorCache.Count() >= iMaxCachedEnumerators && i < iMaxCachedEnumerators ) 
       
   197             {
       
   198             if ( iEnumeratorCache[i]->iReferences == 0 )
       
   199                 {
       
   200                 LOG2( "Releasing %d", i );
       
   201                 delete iEnumeratorCache[i];
       
   202                 iEnumeratorCache.Remove( i );
       
   203                 }
       
   204             else if ( iEnumeratorCache[i]->iReferences < minReferencesCount )
       
   205                 {
       
   206                 minReferencesCount = iEnumeratorCache[i]->iReferences;
       
   207                 minReferencesIndex = i;
       
   208                 }
       
   209             i++;
       
   210             }
       
   211     
       
   212         if ( iEnumeratorCache.Count() >= iMaxCachedEnumerators ) 
       
   213             {
       
   214             LOG2( "Force Releasing %d", minReferencesIndex );
       
   215             delete iEnumeratorCache[minReferencesIndex];
       
   216             iEnumeratorCache.Remove( minReferencesIndex );
       
   217             }
       
   218         }
       
   219     }