webengine/osswebengine/cache/src/HttpCacheLookupTable.cpp
changeset 8 7c90e6132015
parent 0 dd21522fd290
child 13 10e98eab6f85
equal deleted inserted replaced
0:dd21522fd290 8:7c90e6132015
    95     CleanupStack::Pop();
    95     CleanupStack::Pop();
    96 
    96 
    97     return self;
    97     return self;
    98     }
    98     }
    99 
    99 
       
   100 // -----------------------------------------------------------------------------
   100 // Destructor
   101 // Destructor
       
   102 // -----------------------------------------------------------------------------
   101 CHttpCacheLookupTable::~CHttpCacheLookupTable()
   103 CHttpCacheLookupTable::~CHttpCacheLookupTable()
   102     {
   104     {
   103     // do not call ResetAndDestroy on iEntries
   105     // do not call ResetAndDestroy on iEntries
   104     // as delete item are not set to NULL
   106     // as delete item are not set to NULL
   105     if (iEntries)
   107     if (iEntries)
   148 // -----------------------------------------------------------------------------
   150 // -----------------------------------------------------------------------------
   149 // CHttpCacheLookupTable::Find
   151 // CHttpCacheLookupTable::Find
   150 //
   152 //
   151 // -----------------------------------------------------------------------------
   153 // -----------------------------------------------------------------------------
   152 //
   154 //
   153 CHttpCacheEntry* CHttpCacheLookupTable::Find(
   155 CHttpCacheEntry* CHttpCacheLookupTable::Find( const TDesC8& aUrl )
   154     const TDesC8& aUrl )
       
   155     {
   156     {
   156     CHttpCacheEntry* entry = NULL;
   157     CHttpCacheEntry* entry = NULL;
   157   TInt pos( Probe( aUrl, EFalse ) );
   158     TInt pos( Probe( aUrl, EFalse ) );
   158     //
   159 
   159   if( Valid( pos ) )
   160     if ( Valid( pos ) )
   160         {
   161         {
   161         entry = iEntries->At( pos );
   162         entry = iEntries->At( pos );
   162         }
   163         
   163   return entry;
   164         if ( entry )
       
   165             {
       
   166             if ( entry->BodySize() == 0 &&
       
   167                  entry->State() == CHttpCacheEntry::ECacheComplete )
       
   168                 {
       
   169 
       
   170 #ifdef __CACHELOG__
       
   171                 HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   172                                            _L("CHttpCacheLookupTable::Find - Found ZERO size"),
       
   173                                            entry->Filename(),
       
   174                                            entry->Url(),
       
   175                                            pos,
       
   176                                            ELogLookupTablePos );
       
   177 #endif
       
   178                 return NULL;
       
   179                 }
       
   180             }
       
   181         }
       
   182 
       
   183     return entry;
   164     }
   184     }
   165 
   185 
   166 // -----------------------------------------------------------------------------
   186 // -----------------------------------------------------------------------------
   167 // CHttpCacheLookupTable::Remove
   187 // CHttpCacheLookupTable::Remove
   168 //
   188 //
   169 // -----------------------------------------------------------------------------
   189 // -----------------------------------------------------------------------------
   170 //
   190 //
   171 TInt CHttpCacheLookupTable::Remove(
   191 TInt CHttpCacheLookupTable::Remove( const TDesC8& aUrl )
   172     const TDesC8& aUrl )
       
   173     {
   192     {
   174     TInt status( KErrNotFound );
   193     TInt status( KErrNotFound );
   175   TInt pos( Probe( aUrl, EFalse ) );
   194     TInt pos( Probe( aUrl, EFalse ) );
   176     //
   195 
   177   if( Valid( pos ) )
   196     if( Valid( pos ) )
   178         {
   197         {
   179         // remove only nonactive entry
   198         // remove only nonactive entry
   180         CHttpCacheEntry* entry = iEntries->At( pos );
   199         CHttpCacheEntry* entry = iEntries->At( pos );
   181         if( entry->State() == CHttpCacheEntry::ECacheComplete )
   200         if ( entry->State() == CHttpCacheEntry::ECacheComplete )
   182             {
   201             {
       
   202 #ifdef __CACHELOG__
       
   203             HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   204                                            _L("CHttpCacheLookupTable::Remove - ERASING item"),
       
   205                                            entry->Filename(),
       
   206                                            entry->Url(),
       
   207                                            pos,
       
   208                                            ELogLookupTablePos );
       
   209 #endif
   183             Erase( pos );
   210             Erase( pos );
   184             status = KErrNone;
   211             status = KErrNone;
   185             HttpCacheUtil::WriteLog( 0, _L( "remove item" ), pos );
       
   186             }
   212             }
   187 #ifndef __CACHELOG__
   213 #ifndef __CACHELOG__
   188         }
   214         }
   189 #else // __CACHELOG__
   215 #else //__CACHELOG__
   190         else
   216         else
   191             {
   217             {
   192             HttpCacheUtil::WriteLog( 0, _L( "item is active. cannot be removed" ), pos );
   218             HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   219                                            _L("CHttpCacheLookupTable::Remove - can NOT remove item, is active state"),
       
   220                                            entry->Filename(),
       
   221                                            entry->Url(),
       
   222                                            pos,
       
   223                                            ELogLookupTablePos );
   193             }
   224             }
   194         }
   225         }
   195     else
   226     else
   196         {
   227         {
   197         HttpCacheUtil::WriteLog( 0, _L( "item is not valid. cannot be removed" ), pos );
   228         HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheLookupTable::Remove - item is not valid. cannot be removed" ), pos );
   198         }
   229         }
   199 #endif // __CACHELOG__
   230 #endif // __CACHELOG__
       
   231 
   200     return status;
   232     return status;
   201     }
   233     }
   202 
   234 
   203 // -----------------------------------------------------------------------------
   235 // -----------------------------------------------------------------------------
   204 // CHttpCacheLookupTable::EraseCorruptEntry
   236 // CHttpCacheLookupTable::EraseCacheEntry
   205 //
   237 //
   206 // -----------------------------------------------------------------------------
   238 // -----------------------------------------------------------------------------
   207 //
   239 //
   208 void CHttpCacheLookupTable::EraseCorruptEntry(
   240 void CHttpCacheLookupTable::EraseCacheEntry( const TDesC8& aUrl )
   209     const TDesC8& aUrl )
   241     {
   210     {
   242     TInt pos( Probe( aUrl, EFalse ) );
   211   TInt pos( Probe( aUrl, EFalse ) );
   243     
   212     //
   244     if ( Valid( pos ) )
   213   if( Valid( pos ) )
       
   214         {
   245         {
   215         Erase( pos );
   246         Erase( pos );
   216         HttpCacheUtil::WriteLog( 0, _L( "remove corrupt item" ), pos );
       
   217         }
   247         }
   218 #ifdef __CACHELOG__
   248 #ifdef __CACHELOG__
   219     else
   249     else
   220         {
   250         {
   221         // there must be a valid position for this entry
   251         // there must be a valid position for this entry
   222         __ASSERT_DEBUG( EFalse, User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   252         __ASSERT_DEBUG( EFalse, User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   223         HttpCacheUtil::WriteLog( 0, _L( "corrupt item is not valid" ), pos );
       
   224         }
   253         }
   225 #endif // __CACHELOG__
   254 #endif // __CACHELOG__
   226     }
   255     }
   227 
   256 
   228 // -----------------------------------------------------------------------------
   257 // -----------------------------------------------------------------------------
   231 // -----------------------------------------------------------------------------
   260 // -----------------------------------------------------------------------------
   232 //
   261 //
   233 TInt CHttpCacheLookupTable::RemoveAll()
   262 TInt CHttpCacheLookupTable::RemoveAll()
   234     {
   263     {
   235     TInt numberOfBytes( 0 );
   264     TInt numberOfBytes( 0 );
   236     HttpCacheUtil::WriteLog( 0, _L( "remove 'em all" ) );
   265 
   237     //
   266 #ifdef __CACHELOG__
   238     for( TInt i = 0; i < iEntries->Count(); i++ )
   267     HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheLookupTable::RemoveAll - remove 'em all" ) );
       
   268 #endif
       
   269 
       
   270     for ( TInt i = 0; i < iEntries->Count(); i++ )
   239         {
   271         {
   240         CHttpCacheEntry* entry = iEntries->At( i );
   272         CHttpCacheEntry* entry = iEntries->At( i );
   241         // remove all nonactive entries
   273         // Remove all nonactive entries
   242         if( Valid( i ) && entry->State() == CHttpCacheEntry::ECacheComplete )
   274         if ( Valid( i ) && entry->State() == CHttpCacheEntry::ECacheComplete )
   243             {
   275             {
   244             numberOfBytes+= ( entry->HeaderSize() + entry->Size() );
   276             numberOfBytes += ( entry->HeaderSize() + entry->BodySize() );
   245             Erase( i );
   277             Erase( i );
   246             }
   278             }
   247         }
   279         }
       
   280 
   248 #ifdef __CACHELOG__
   281 #ifdef __CACHELOG__
   249     HttpCacheUtil::WriteLog( 0, _L( "number of items left:" ), iCount );
   282     HttpCacheUtil::WriteLog( 0, _L( "number of items left:" ), iCount );
   250     // check if there are pending items -should not be though
   283     // check if there are pending items -should not be though
   251     __ASSERT_DEBUG( iCount == 0, User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   284     __ASSERT_DEBUG( iCount == 0, User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   252 #endif // __CACHELOG__
   285 #endif // __CACHELOG__
       
   286 
   253     return numberOfBytes;
   287     return numberOfBytes;
   254     }
   288     }
   255 
   289 
   256 
   290 
   257 // -----------------------------------------------------------------------------
   291 // -----------------------------------------------------------------------------
   292     return error;
   326     return error;
   293 
   327 
   294     }
   328     }
   295 
   329 
   296 // -----------------------------------------------------------------------------
   330 // -----------------------------------------------------------------------------
   297 // CHttpCacheLookupTable::Internalize
   331 // CHttpCacheLookupTable::InternalizeL
   298 //
   332 //
   299 // -----------------------------------------------------------------------------
   333 // -----------------------------------------------------------------------------
   300 //
   334 //
   301 void CHttpCacheLookupTable::InternalizeL(
   335 void CHttpCacheLookupTable::InternalizeL(
   302     RFileReadStream& aReadStream,
   336     RFileReadStream& aReadStream,
   303     const TDesC& /*aDirectory*/ )
   337     const TDesC& /*aDirectory*/ )
   304     {
   338     {
   305     // get number of entries
   339     // get number of entries
   306     TInt version = 0;
   340     TInt version = 0;
   307     TRAP_IGNORE( version = aReadStream.ReadInt32L() );
   341     version = aReadStream.ReadInt32L();
   308     if( version == KCacheVersionNumber )
   342     if( version == KCacheVersionNumber )
   309         {
   343         {
   310         TInt count( aReadStream.ReadInt32L() );
   344         TInt count( aReadStream.ReadInt32L() );
   311         TInt contentSize( 0 );
   345         TInt contentSize( 0 );
   312         TInt err;
   346         TInt err;
   319             // leave only on no memory
   353             // leave only on no memory
   320             if( err == KErrNone )
   354             if( err == KErrNone )
   321                 {
   355                 {
   322                 // insert to the table
   356                 // insert to the table
   323                 InsertL( entry );
   357                 InsertL( entry );
   324                 contentSize+=entry->HeaderSize();
   358                 contentSize += entry->HeaderSize();
   325                 contentSize+=entry->Size();
   359                 contentSize += entry->BodySize();
   326                 }
   360                 }
   327             else if( err == KErrNoMemory )
   361             else if ( err == KErrNoMemory )
   328                 {
   362                 {
   329                 User::Leave( KErrNoMemory );
   363                 User::Leave( KErrNoMemory );
   330                 }
   364                 }
   331             else
   365             else
   332                 {
   366                 {
   333                 // suggestions?
   367                 // suggestions
   334                 }
   368                 }
       
   369 
   335             // takes ownership
   370             // takes ownership
   336             CleanupStack::Pop(); // entry
   371             CleanupStack::Pop(); // entry
   337             }
   372             }
       
   373 
   338         // set startup cache size
   374         // set startup cache size
       
   375 #ifdef __CACHELOG__
   339         HttpCacheUtil::WriteLog( 0, _L( "startup content size" ), contentSize );
   376         HttpCacheUtil::WriteLog( 0, _L( "startup content size" ), contentSize );
   340         iStreamHandler->SetStartupCacheSize( contentSize );
   377 #endif
       
   378         iStreamHandler->SetSavedContentSize( contentSize );
   341         }
   379         }
   342     else
   380     else
   343         {
   381         {
   344         // cleanup index.dat?
   382         // cleanup index.dat
   345         }
   383         }
   346     }
   384     }
   347 
   385 
   348 // -----------------------------------------------------------------------------
   386 // -----------------------------------------------------------------------------
   349 // CHttpCacheLookupTable::Externalize
   387 // CHttpCacheLookupTable::Externalize
   382 TInt CHttpCacheLookupTable::InsertL(
   420 TInt CHttpCacheLookupTable::InsertL(
   383     CHttpCacheEntry* aCacheEntry )
   421     CHttpCacheEntry* aCacheEntry )
   384     {
   422     {
   385     __ASSERT_DEBUG( aCacheEntry != NULL,
   423     __ASSERT_DEBUG( aCacheEntry != NULL,
   386         User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   424         User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   387     //
   425 
   388     TInt pos( -1 );
   426     TInt pos( -1 );
   389 
   427 
   390     if( aCacheEntry )
   428     if ( aCacheEntry )
   391         {
   429         {
   392         pos = Probe( aCacheEntry->Url(), ETrue );
   430         pos = Probe( aCacheEntry->Url(), ETrue );
   393         // double check
   431         // double check
   394         if( Valid( pos ) )
   432         if( Valid( pos ) )
   395             {
   433             {
   401                 {
   439                 {
   402                 // completly failed
   440                 // completly failed
   403                 pos = -1;
   441                 pos = -1;
   404                 }
   442                 }
   405             }
   443             }
       
   444 
   406         // insert
   445         // insert
   407         if( pos != -1 )
   446         if( pos != -1 )
   408             {
   447             {
   409             iEntries->At( pos ) = aCacheEntry;
   448             iEntries->At( pos ) = aCacheEntry;
   410             iCount++;
   449             iCount++;
       
   450 
       
   451 #ifdef __CACHELOG__
   411             HttpCacheUtil::WriteLog( 0, _L( "insert new item to the lookuptable" ), pos );
   452             HttpCacheUtil::WriteLog( 0, _L( "insert new item to the lookuptable" ), pos );
       
   453 #endif
   412             // check if the hashtable is full
   454             // check if the hashtable is full
   413             if( iCount > ( iEntries->Count() >> 1 ) )
   455             if ( iCount > ( iEntries->Count() >> 1 ) )
   414                 {
   456                 {
   415                 ReHashL();
   457                 ReHashL();
   416                 }
   458                 }
   417             }
   459             }
   418         else
   460         else
   523 //
   565 //
   524 // -----------------------------------------------------------------------------
   566 // -----------------------------------------------------------------------------
   525 //
   567 //
   526 void CHttpCacheLookupTable::ReHashL()
   568 void CHttpCacheLookupTable::ReHashL()
   527     {
   569     {
       
   570 #ifdef __CACHELOG__
   528     HttpCacheUtil::WriteLog( 1, _L( "Resize lookuptable!" ) );
   571     HttpCacheUtil::WriteLog( 1, _L( "Resize lookuptable!" ) );
   529     HttpCacheUtil::WriteLog( 1, _L( "count=" ), iCount );
   572     HttpCacheUtil::WriteLog( 1, _L( "count=" ), iCount );
   530     HttpCacheUtil::WriteLog( 1, _L( "Table size=" ), iEntries->Count() );
   573     HttpCacheUtil::WriteLog( 1, _L( "Table size=" ), iEntries->Count() );
   531     //
   574 #endif
       
   575 
   532     TUint newSize( NextPrime( iEntries->Count() * 2 ) );
   576     TUint newSize( NextPrime( iEntries->Count() * 2 ) );
   533   CArrayPtrFlat<CHttpCacheEntry>* newEntries =
   577     CArrayPtrFlat<CHttpCacheEntry>* newEntries =
   534         new( ELeave )CArrayPtrFlat<CHttpCacheEntry>( newSize );
   578         new( ELeave )CArrayPtrFlat<CHttpCacheEntry>( newSize );
   535     // hash must operate on the new table
   579     // hash must operate on the new table
   536     CArrayPtrFlat<CHttpCacheEntry>* oldEntries = iEntries;
   580     CArrayPtrFlat<CHttpCacheEntry>* oldEntries = iEntries;
   537     iEntries = newEntries;
   581     iEntries = newEntries;
   538     CleanupStack::PushL( oldEntries );
   582     CleanupStack::PushL( oldEntries );
   606 // -----------------------------------------------------------------------------
   650 // -----------------------------------------------------------------------------
   607 // CHttpCacheLookupTable::Erase
   651 // CHttpCacheLookupTable::Erase
   608 //
   652 //
   609 // -----------------------------------------------------------------------------
   653 // -----------------------------------------------------------------------------
   610 //
   654 //
   611 void CHttpCacheLookupTable::Erase(
   655 void CHttpCacheLookupTable::Erase( TInt aPos )
   612     TInt aPos )
       
   613     {
   656     {
   614     // must be a valid pos
   657     // must be a valid pos
   615     __ASSERT_DEBUG( Valid( aPos ),  User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
   658     __ASSERT_DEBUG( Valid( aPos ),  User::Panic( _L("cacheHandler Panic"), KErrCorrupt ) );
       
   659 
   616     CHttpCacheEntry* entry = iEntries->At( aPos );
   660     CHttpCacheEntry* entry = iEntries->At( aPos );
   617 
   661 
   618     if( entry )
   662     if( entry )
   619         {
   663         {
   620         // delete file associated with this entry
   664         // delete file associated with this entry
   621         TBool attached( EFalse );
   665         TBool attached( EFalse );
       
   666         
       
   667 #ifdef __CACHELOG__
       
   668         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   669                                            _L("CHttpCacheLookupTable::Erase"),
       
   670                                            entry->Filename(),
       
   671                                            entry->Url(),
       
   672                                            aPos,
       
   673                                            ELogLookupTablePos );
       
   674 #endif        
   622         TRAPD( err, attached = iStreamHandler->AttachL( *entry ) );
   675         TRAPD( err, attached = iStreamHandler->AttachL( *entry ) );
   623         if( err == KErrNone && attached )
   676         if ( err == KErrNone && attached )
   624             {
   677             {
   625             iStreamHandler->Erase( *entry );
   678             iStreamHandler->EraseCacheFile( *entry );
   626             iStreamHandler->Detach( *entry );
   679             iStreamHandler->Detach( *entry );
   627             }
   680             }
   628         //
   681 
   629         SetDeleted( aPos );
   682         SetDeleted( aPos );
   630         delete entry;
   683         delete entry;
   631         iCount--;
   684         iCount--;
   632         }
   685         }
   633     }
   686     }