webengine/osswebengine/cache/src/HttpCacheEntry.cpp
changeset 0 dd21522fd290
child 1 7c90e6132015
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006 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 the License "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:  Implementation of CHttpCacheEntry
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "HttpCacheEntry.h"
       
    20 #include "HttpCacheEvictionHandler.h"
       
    21 #include "HttpCacheStreamHandler.h"
       
    22 #include "HttpCacheUtil.h"
       
    23 #include <s32file.h>
       
    24 
       
    25 // EXTERNAL DATA STRUCTURES
       
    26 
       
    27 // EXTERNAL FUNCTION PROTOTYPES
       
    28 
       
    29 // CONSTANTS
       
    30 const TInt CHttpCacheEntry::iOffset = _FOFF( CHttpCacheEntry, iSlink );
       
    31 
       
    32 // MACROS
       
    33 
       
    34 // LOCAL CONSTANTS AND MACROS
       
    35 
       
    36 // MODULE DATA STRUCTURES
       
    37 
       
    38 // LOCAL FUNCTION PROTOTYPES
       
    39 
       
    40 // FORWARD DECLARATIONS
       
    41 
       
    42 // ============================= LOCAL FUNCTIONS ===============================
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CHttpCacheEntry::CHttpCacheEntry
       
    48 // C++ default constructor can NOT contain any code, that
       
    49 // might leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CHttpCacheEntry::CHttpCacheEntry(
       
    53     CHttpCacheEvictionHandler& aEvictionHandler )
       
    54     : iState( ECacheUninitialized ),
       
    55       iEvictionHandler( &aEvictionHandler )
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CHttpCacheEntry::ConstructL
       
    61 // Symbian 2nd phase constructor can leave.
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 void CHttpCacheEntry::ConstructL(
       
    65     const TDesC8& aUrl )
       
    66     {
       
    67     iUrl = aUrl.AllocL();
       
    68     iFileName = KNullDesC().AllocL();
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CHttpCacheEntry::NewL
       
    73 // Two-phased constructor.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CHttpCacheEntry* CHttpCacheEntry::NewL(
       
    77     const TDesC8& aUrl,
       
    78     CHttpCacheEvictionHandler& aEvictionHandler )
       
    79     {
       
    80     CHttpCacheEntry* self = new( ELeave ) CHttpCacheEntry( aEvictionHandler );
       
    81 
       
    82     CleanupStack::PushL( self );
       
    83     self->ConstructL( aUrl );
       
    84     CleanupStack::Pop();
       
    85 
       
    86     return self;
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CHttpCacheEntry::NewLC
       
    91 // Two-phased constructor.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 CHttpCacheEntry* CHttpCacheEntry::NewLC(
       
    95     const TDesC8& aUrl,
       
    96     CHttpCacheEvictionHandler& aEvictionHandler )
       
    97     {
       
    98     CHttpCacheEntry* self = CHttpCacheEntry::NewL( aUrl, aEvictionHandler );
       
    99     CleanupStack::PushL( self );
       
   100     return self;
       
   101     }
       
   102 
       
   103 // Destructor
       
   104 CHttpCacheEntry::~CHttpCacheEntry()
       
   105     {
       
   106   if( iVictim && iEvictionHandler)
       
   107     {
       
   108     iEvictionHandler->Remove( *this );
       
   109     }
       
   110     delete iUrl;
       
   111     delete iFileName;
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CHttpCacheEntry::SetState
       
   116 //
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CHttpCacheEntry::SetState(
       
   120     TCacheEntryState aState )
       
   121     {
       
   122     // add entry to the eviction table once it gets completed
       
   123     if( aState == ECacheComplete && !iVictim )
       
   124         {
       
   125         // don't add it twice
       
   126         iEvictionHandler->Insert( *this );
       
   127         iVictim = ETrue;
       
   128         }
       
   129     iState = aState;
       
   130     }
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CHttpCacheEntry::SetFileNameL
       
   135 //
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CHttpCacheEntry::SetFileNameL(
       
   139     const TFileName& aFileName )
       
   140     {
       
   141     delete iFileName;
       
   142     iFileName = NULL;
       
   143     iFileName = aFileName.AllocL();
       
   144     }
       
   145 
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CHttpCacheEntry::Accessed
       
   149 //
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CHttpCacheEntry::Accessed()
       
   153     {
       
   154     TTime now;
       
   155     now.HomeTime();
       
   156 
       
   157     iLastAccessed = now.Int64();
       
   158 
       
   159     iRef++;
       
   160     if( iVictim )
       
   161         {
       
   162         iEvictionHandler->Accessed( *this );
       
   163         }
       
   164 #ifdef __CACHELOG__
       
   165     _LIT( KAccessFormat, "entry accessed: %d" );
       
   166     TBuf<100> buf;
       
   167     buf.Format( KAccessFormat, iRef );
       
   168     HttpCacheUtil::WriteUrlToLog( 0, buf, iUrl->Des() );
       
   169 #endif // __CACHELOG__
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CHttpCacheEntry::SetSize
       
   174 //
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CHttpCacheEntry::SetSize(
       
   178     TUint aSize )
       
   179     {
       
   180     if( iSize && !aSize )
       
   181         {
       
   182         // body removal
       
   183         // remove itself from the eviction table.
       
   184         // this is no longer a victim
       
   185       if( iVictim )
       
   186         {
       
   187         iEvictionHandler->Remove( *this );
       
   188             iVictim = EFalse;
       
   189         }
       
   190         }
       
   191     iSize = aSize;
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CHttpCacheEntry::SetProtected
       
   196 //
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 void CHttpCacheEntry::SetProtected()
       
   200     {
       
   201     iProtected = ETrue;
       
   202     // iRef
       
   203     iRef = 50;
       
   204     HttpCacheUtil::WriteLog( 0, _L( "protected item" ) );
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CHttpCacheEntry::Internalize
       
   209 //
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 TInt CHttpCacheEntry::Internalize(
       
   213     RFileReadStream& aReadStream )
       
   214     {
       
   215     TRAPD( err,
       
   216      TInt len;
       
   217     
       
   218     // url length
       
   219     len = aReadStream.ReadInt32L();
       
   220     delete iUrl;
       
   221     iUrl=NULL;
       
   222     iUrl = HBufC8::NewL( len );
       
   223     TPtr8 ptr8( iUrl->Des() );
       
   224     // url
       
   225     aReadStream.ReadL( ptr8, len );
       
   226     
       
   227     // filename length
       
   228     len = aReadStream.ReadInt32L();
       
   229     HBufC* filename = HBufC::NewLC( len );
       
   230     TPtr ptr( filename->Des() );
       
   231     // url
       
   232     aReadStream.ReadL( ptr, len );
       
   233     //
       
   234     SetFileNameL( filename->Des() );
       
   235     //
       
   236     CleanupStack::PopAndDestroy(); // filename
       
   237     // la
       
   238     TReal64 la;
       
   239     la = aReadStream.ReadReal64L();
       
   240     iLastAccessed = la;
       
   241     // ref
       
   242     iRef = aReadStream.ReadUint32L();
       
   243     // size
       
   244     iSize = aReadStream.ReadUint32L( );
       
   245     // size
       
   246     iHeaderSize = aReadStream.ReadUint32L( );
       
   247     // protected
       
   248     iProtected = aReadStream.ReadInt32L();
       
   249     //
       
   250     SetState( ECacheComplete ); );
       
   251     return err;
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CHttpCacheEntry::Externalize
       
   256 //
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 TInt CHttpCacheEntry::Externalize(
       
   260     RFileWriteStream& aWriteStream )
       
   261     {
       
   262     TRAPD( err,
       
   263     // url length
       
   264     aWriteStream.WriteInt32L( iUrl->Length() );
       
   265     // url
       
   266     aWriteStream.WriteL( iUrl->Des() );
       
   267     // filename length
       
   268     aWriteStream.WriteInt32L( iFileName->Length() );
       
   269     // filename
       
   270     aWriteStream.WriteL( iFileName->Des() );
       
   271     // la
       
   272     aWriteStream.WriteReal64L( iLastAccessed );
       
   273     // ref
       
   274     aWriteStream.WriteUint32L( iRef );
       
   275     // size
       
   276     aWriteStream.WriteUint32L( iSize );
       
   277     // size
       
   278     aWriteStream.WriteUint32L( iHeaderSize );
       
   279     // protected
       
   280     aWriteStream.WriteInt32L( iProtected ); );
       
   281     return err;
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CHttpCacheEntry::Accessed
       
   286 //
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CHttpCacheEntry::Accessed(TInt64 aLastAccessed, TUint16 aRef)
       
   290     {
       
   291     iLastAccessed = aLastAccessed;
       
   292     iRef = aRef;
       
   293     if( iVictim )
       
   294         {
       
   295         iEvictionHandler->Accessed( *this );
       
   296         }
       
   297 #ifdef __CACHELOG__
       
   298     _LIT( KAccessFormat, "entry accessed: %d" );
       
   299     TBuf<100> buf;
       
   300     buf.Format( KAccessFormat, iRef );
       
   301     HttpCacheUtil::WriteUrlToLog( 0, buf, iUrl->Des() );
       
   302 #endif // __CACHELOG__
       
   303 
       
   304     }
       
   305 
       
   306 //  End of File