browserui/browser/BrowserAppSrc/BrowserPopupEngine.cpp
branchRCL_3
changeset 65 8e6fa1719340
parent 0 84ad3b177aa3
equal deleted inserted replaced
64:6385c4c93049 65:8e6fa1719340
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *      Implementation of popupblocking
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include <Uri16.h>
       
    23 #include "BrowserPopupEngine.h"
       
    24 #include <f32file.h>
       
    25 #include "logger.h"
       
    26 #include "BrowserUtil.h"
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT( KWhiteListFileName, "bpopupwl.db" );
       
    30 _LIT( KWhiteTmpFileName, "bpopwtmp.db" );
       
    31 _LIT( KWhiteBkpFileName, "bpopbbkp.db" );
       
    32 _LIT( KEndMark, "\n" );
       
    33 const TInt KArrayGranularity = 10;
       
    34 const TInt KCacheSize = 32;//The maximum size of the memory cache
       
    35 const TInt KMaxDbSize = 16384;// (255/max length of the domain/+1/\n/)*64
       
    36 
       
    37 // ================= MEMBER FUNCTIONS =======================
       
    38 
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CBrowserPopupEngine::CBrowserPopupEngine()
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CBrowserPopupEngine::CBrowserPopupEngine()
       
    45     {
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CBrowserPopupEngine::~CBrowserPopupEngine
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CBrowserPopupEngine::~CBrowserPopupEngine()
       
    53     {
       
    54     iWhiteListFile.Flush();
       
    55     iWhiteListFile.Close();
       
    56     iFs.Close();
       
    57     delete iCachedWhiteUrls;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CBrowserPopupEngine::NewLC
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CBrowserPopupEngine* CBrowserPopupEngine::NewLC()
       
    65     {
       
    66     CBrowserPopupEngine* self = new (ELeave) CBrowserPopupEngine;
       
    67     CleanupStack::PushL(self);
       
    68     self->ConstructL();
       
    69     return self;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CBrowserPopupEngine::NewL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CBrowserPopupEngine* CBrowserPopupEngine::NewL()
       
    77     {
       
    78     CBrowserPopupEngine* self = new (ELeave) CBrowserPopupEngine;
       
    79     CleanupStack::PushL(self);
       
    80     self->ConstructL();
       
    81     CleanupStack::Pop();
       
    82     return self;
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CBrowserPopupEngine::ConstructL
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CBrowserPopupEngine::ConstructL()
       
    90     {
       
    91 LOG_ENTERFN("CBrowserPopupEngine::ConstructL");
       
    92     User::LeaveIfError(iFs.Connect());
       
    93 //open databases
       
    94     BROWSER_LOG( ( _L( "<-Popup engine-> RFs Connected." ) ) );
       
    95     OpenDatabaseL( &iWhiteListFile );
       
    96     BROWSER_LOG( ( _L( "<-Popup engine-> white file opened." ) ) );
       
    97 //Create memory cache 
       
    98     iCachedWhiteUrls = new(ELeave) CDesCArrayFlat(KArrayGranularity);
       
    99     BROWSER_LOG( ( _L( "<-Popup engine-> Cache created." ) ) );
       
   100     iCachedWhiteUrls->Reset();
       
   101 //Load some to memory cache
       
   102     LoadUrlsToCacheL( &iWhiteListFile, iCachedWhiteUrls, &iWhiteCacheNo);
       
   103     BROWSER_LOG( ( _L( "<-Popup engine-> Urls loaded to cache." ) ) );
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CBrowserPopupEngine::OpenDatabaseL
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CBrowserPopupEngine::OpenDatabaseL(RFile* aFile)
       
   111     {
       
   112 LOG_ENTERFN("CBrowserPopupEngine::OpenDatabaseL");
       
   113 
       
   114 	__ASSERT_DEBUG( (aFile != NULL), Util::Panic( Util::EUninitializedData ));
       
   115 
       
   116     TInt err;
       
   117     RFile tmpFile;
       
   118     TPtrC fileNamePtr;
       
   119     TPtrC bkpFileNamePtr;
       
   120     TBuf<KMaxFileName> privatePath;
       
   121     TBuf<KMaxFileName> listFileName;
       
   122     TBuf<KMaxFileName> bkpFileName;
       
   123     TBuf<KMaxFileName> tmpFileName;
       
   124     iFs.PrivatePath( privatePath );
       
   125     listFileName.Copy( privatePath );
       
   126     listFileName.Append( KWhiteListFileName );
       
   127     bkpFileName.Copy( privatePath );
       
   128     bkpFileName.Append( KWhiteBkpFileName );
       
   129     tmpFileName.Copy( privatePath );
       
   130     tmpFileName.Append( KWhiteTmpFileName );
       
   131     err = aFile->Open( iFs, listFileName, EFileWrite | EFileStream | EFileShareExclusive );
       
   132     BROWSER_LOG( ( _L( "<-Popup engine-> File open error: %d" ), err ) );
       
   133     if ( err == KErrNotFound ) 
       
   134         {
       
   135         err = tmpFile.Open( iFs, bkpFileName, EFileWrite | EFileShareExclusive );
       
   136         if( err != KErrNone )
       
   137             {
       
   138             err = aFile->Create( iFs, listFileName, EFileWrite | EFileShareExclusive );
       
   139             BROWSER_LOG( ( _L( "<-Popup engine-> white file created." ) ) );
       
   140             }
       
   141         else 
       
   142             {
       
   143 //check validity of tmp file if the last char is \n it is probably OK
       
   144             if( !CheckDbValidity( &tmpFile ) )
       
   145                 {
       
   146                 User::LeaveIfError( 
       
   147                     aFile->Create( iFs, listFileName, 
       
   148                         EFileWrite | EFileShareExclusive ) );
       
   149                 }
       
   150             else
       
   151                 {
       
   152 //rename tmp to origin
       
   153                 err = tmpFile.Rename( listFileName );
       
   154                 if(err == KErrNone)
       
   155                     {
       
   156                     tmpFile.Close();
       
   157                     User::LeaveIfError(aFile->Open( iFs, listFileName, EFileWrite | EFileStream | EFileShareExclusive ) );
       
   158                     }
       
   159                 else
       
   160                     {
       
   161                     User::LeaveIfError( 
       
   162                         aFile->Create( iFs, listFileName, 
       
   163                             EFileWrite | EFileShareExclusive ) );
       
   164                     }
       
   165                 }
       
   166             }
       
   167         }
       
   168     else if ( err == KErrPathNotFound )
       
   169         {
       
   170         User::LeaveIfError( iFs.CreatePrivatePath( EDriveC ) );
       
   171         User::LeaveIfError(
       
   172             aFile->Create( iFs, listFileName, EFileWrite | EFileShareExclusive ) );
       
   173         }
       
   174     else if((err != KErrInUse)&&( err != KErrNone ))
       
   175         {
       
   176         User::Leave(err);
       
   177         }
       
   178     else if( !CheckDbValidity( aFile ) )
       
   179         {
       
   180 //if the file is not valid delete it and create a new one
       
   181         aFile->Close();
       
   182         User::LeaveIfError( iFs.Delete( listFileName ) );
       
   183         User::LeaveIfError(
       
   184             aFile->Create( iFs, listFileName, EFileWrite | EFileShareExclusive ) );
       
   185         }
       
   186 //delete tmp and bkp files if they exist
       
   187     iFs.Delete( tmpFileName );
       
   188     iFs.Delete( bkpFileName );
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CBrowserPopupEngine::AddUrlToWhiteListL
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 void CBrowserPopupEngine::AddUrlToWhiteListL(const TDesC& aUrl)
       
   196     {
       
   197 LOG_ENTERFN("CBrowserPopupEngine::AddUrlToWhiteListL");
       
   198     TUriParser16 urlParser;
       
   199     BROWSER_LOG( ( _L( "<-Popup engine-> url to add to white list : %S" ), &aUrl ) );
       
   200 //check whether file allready exists in list
       
   201     /*if( IsUrlOnWhiteListL( aUrl ) )
       
   202         {
       
   203         RDebug::Print(_L("<-Popup engine-> url is in white cache allready.\n"));
       
   204         return;
       
   205         }*/
       
   206 //get the domain from the url
       
   207     urlParser.Parse(aUrl);
       
   208     if( urlParser.IsSchemeValid() )
       
   209         {
       
   210 //write domain to file
       
   211         HandleUrlOrderChangeL( &iWhiteListFile, urlParser.Extract( EUriHost), EFalse, ETrue );
       
   212 //add domain to memory cache
       
   213         AddUrlToCacheL( iCachedWhiteUrls, urlParser.Extract( EUriHost) );
       
   214         }
       
   215     else
       
   216         {
       
   217         User::Leave( KErrBadName );
       
   218         }
       
   219     }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CBrowserPopupEngine::IsUrlOnWhiteListL
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 TBool CBrowserPopupEngine::IsUrlOnWhiteListL(const TDesC& aUrl)
       
   226     {
       
   227 LOG_ENTERFN("CBrowserPopupEngine::IsUrlOnWhiteListL");
       
   228     TUriParser16 urlParser;
       
   229     urlParser.Parse(aUrl);
       
   230     if( !urlParser.IsSchemeValid() )
       
   231         {
       
   232         User::Leave( KErrBadName );
       
   233         }
       
   234 //look for URL in memory cache
       
   235     if( IsUrlInWhiteCacheL( urlParser.Extract( EUriHost) ) )
       
   236         {
       
   237         BROWSER_LOG( ( _L( "<-Popup engine-> url is in white cache." ) ) );
       
   238         HandleUrlOrderChangeL( &iWhiteListFile, urlParser.Extract( EUriHost), ETrue, ETrue );
       
   239         return ETrue;
       
   240         }
       
   241 //look for URL in file
       
   242     if( IsUrlInWhiteFileL( urlParser.Extract( EUriHost) ) )
       
   243         {
       
   244         HandleUrlOrderChangeL( &iWhiteListFile, urlParser.Extract( EUriHost), ETrue, ETrue );
       
   245 //if found and not in memory cache put it there
       
   246         AddUrlToCacheL( iCachedWhiteUrls, urlParser.Extract( EUriHost) );
       
   247         BROWSER_LOG( ( _L( "<-Popup engine-> url is in white file." ) ) );
       
   248         return ETrue;
       
   249         }
       
   250     BROWSER_LOG( ( _L( "<-Popup engine-> url is not on white list." ) ) );
       
   251     return EFalse;
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CBrowserPopupEngine::IsUrlInWhiteCacheL
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 TBool CBrowserPopupEngine::IsUrlInWhiteCacheL(const TDesC& aUrl)
       
   259     {
       
   260     return IsUrlInCacheL( iCachedWhiteUrls, aUrl, iWhiteCacheNo );
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CBrowserPopupEngine::IsUrlInWhiteFileL
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 TBool CBrowserPopupEngine::IsUrlInWhiteFileL(const TDesC& aUrl)
       
   268     {
       
   269     return IsUrlInFileL( &iWhiteListFile , aUrl );
       
   270     }
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CBrowserPopupEngine::IsUrlInFileL
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 TBool CBrowserPopupEngine::IsUrlInFileL(const RFile* aFile, const TDesC& aUrl)
       
   277     {
       
   278     
       
   279    	__ASSERT_DEBUG( (aFile != NULL), Util::Panic( Util::EUninitializedData ));
       
   280     
       
   281     HBufC8* line = HBufC8::NewL( 256 );
       
   282     CleanupStack::PushL( line );
       
   283     HBufC8* parturl = HBufC8::NewL( 256 );
       
   284     CleanupStack::PushL( parturl );
       
   285     TInt match = 1;
       
   286     TInt pos = 0;
       
   287     TInt result;
       
   288     TPtrC16 linePtr16;
       
   289     HBufC8* url8 = TDesC16ToHBufC8LC( aUrl );
       
   290     User::LeaveIfError( aFile->Seek( ESeekStart, pos ) );
       
   291     TPtr8 linePtr = line->Des();
       
   292     result = aFile->Read( linePtr );
       
   293     parturl->Des().Zero();
       
   294     while ( ( line->Des().Length() > 0 ) && ( result == KErrNone ) ) 
       
   295         {
       
   296         if(parturl->Des().Length() != 0 )
       
   297             {
       
   298             pos = linePtr.Locate( '\n' );
       
   299             parturl->Des().Append( linePtr.Left( pos ) );
       
   300             match = url8->Des().Compare( parturl->Des() );
       
   301             if( match == 0 )
       
   302                 {
       
   303                 CleanupStack::PopAndDestroy( 3 );
       
   304                 return ETrue;
       
   305                 }
       
   306             parturl->Des().Zero();
       
   307             }
       
   308         match = linePtr.Find( url8->Des() );
       
   309         if( match == KErrNotFound )
       
   310             {
       
   311             pos = linePtr.LocateReverse( '\n' );
       
   312             if(( linePtr.Length() != pos )&&( linePtr.Length() > 1 ))
       
   313                 {
       
   314                 parturl->Des().Append( linePtr.Mid( pos + 1, linePtr.Length() - pos - 1 ));
       
   315                 }
       
   316             }
       
   317         else
       
   318             {
       
   319             CleanupStack::PopAndDestroy( 3 );
       
   320             return ETrue;
       
   321             }
       
   322         result = aFile->Read( linePtr, 256 );
       
   323         }
       
   324     CleanupStack::PopAndDestroy( 3 );
       
   325     User::LeaveIfError( result );
       
   326     return EFalse;
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CBrowserPopupEngine::LoadUrlsToCacheL
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 void CBrowserPopupEngine::LoadUrlsToCacheL(const RFile* aFile, CDesCArrayFlat* aCacheArray, TInt* aLoadedUrlNo)
       
   334     {
       
   335 LOG_ENTERFN( "PopupEngine::LoadUrlsToCacheL" );
       
   336 
       
   337 	__ASSERT_DEBUG( (aFile != NULL), Util::Panic( Util::EUninitializedData ));
       
   338 	__ASSERT_DEBUG( (aCacheArray != NULL), Util::Panic( Util::EUninitializedData ));
       
   339 	__ASSERT_DEBUG( (aLoadedUrlNo != NULL), Util::Panic( Util::EUninitializedData ));
       
   340 
       
   341     TPtrC8 test;
       
   342     HBufC8* line = HBufC8::NewL( 256 );
       
   343     CleanupStack::PushL( line );
       
   344     HBufC8* parturl = HBufC8::NewL( 256  );
       
   345     CleanupStack::PushL( parturl );
       
   346     TInt result;
       
   347     TInt itemno = 0;
       
   348     TInt offset = 0;
       
   349     TInt prevOffset = 0;
       
   350     TPtr8 linePtr = line->Des();
       
   351     parturl->Des().Zero();
       
   352     User::LeaveIfError( aFile->Seek( ESeekStart, offset ) );
       
   353     result = aFile->Read( linePtr, 256 );
       
   354     while ( ( line->Des().Length() > 0 ) &&( result == KErrNone ) && ( itemno < KCacheSize )) 
       
   355         {
       
   356         offset = linePtr.Locate('\n' );
       
   357         if( parturl->Length() != 0 )
       
   358             {
       
   359             parturl->Des().Append( linePtr.Mid( prevOffset, offset ) );
       
   360             aCacheArray->AppendL(  *TDesC8ToHBufC16LC( parturl->Des() ) );
       
   361             CleanupStack::PopAndDestroy( );
       
   362             prevOffset = prevOffset + offset +1;
       
   363             itemno++;
       
   364             if( itemno == KCacheSize )
       
   365                 {
       
   366                 break;
       
   367                 }
       
   368             offset = ( linePtr.Right( linePtr.Length() - prevOffset ) ).Locate('\n');
       
   369             }
       
   370         while(( offset != KErrNotFound ) && ( itemno < KCacheSize ))
       
   371             {
       
   372             aCacheArray->AppendL( *TDesC8ToHBufC16LC( linePtr.Mid( prevOffset, offset ) ) );
       
   373             CleanupStack::PopAndDestroy( );
       
   374             prevOffset = prevOffset + offset +1;
       
   375             itemno++;
       
   376             offset = ( linePtr.Right( linePtr.Length() - prevOffset ) ).Locate('\n' );
       
   377             }
       
   378         if( prevOffset != linePtr.Length() )
       
   379             {
       
   380             parturl->Des().Zero();
       
   381             parturl->Des().Append( linePtr.Right( linePtr.Length() - prevOffset ));
       
   382             }
       
   383         prevOffset = 0;
       
   384         result = aFile->Read( linePtr, 256 );
       
   385         }
       
   386     *aLoadedUrlNo = itemno;
       
   387     CleanupStack::PopAndDestroy( 2 );
       
   388     User::LeaveIfError( result );
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CBrowserPopupEngine::IsUrlInCacheL
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 TBool CBrowserPopupEngine::IsUrlInCacheL( const CDesCArrayFlat* aCacheArray, const TDesC& aUrl, const TInt aLoadedurlno )
       
   396     {
       
   397 LOG_ENTERFN("CBrowserPopupEngine::IsUrlInCacheL");
       
   398 
       
   399 	__ASSERT_DEBUG( (aCacheArray != NULL), Util::Panic( Util::EUninitializedData ));
       
   400 
       
   401     TInt itemno=0;
       
   402     BROWSER_LOG( ( _L( "<-Popup engine-> url to find in cache : %S" ), &aUrl ) );
       
   403     while( ( itemno < aLoadedurlno ) && ( aUrl.Compare((*aCacheArray)[ itemno ]) != 0 ) )
       
   404         {
       
   405         itemno++;
       
   406         }
       
   407     if( itemno != aLoadedurlno )
       
   408         {
       
   409         BROWSER_LOG( ( _L( "<-Popup engine-> url found in cache" ) ) );
       
   410         return ETrue;
       
   411         }
       
   412     BROWSER_LOG( ( _L( "<-Popup engine-> url not found in cache" ) ) );
       
   413     return EFalse;
       
   414     }
       
   415 
       
   416 // -----------------------------------------------------------------------------
       
   417 // CBrowserPopupEngine::AddUrlToCacheL
       
   418 // -----------------------------------------------------------------------------
       
   419 //
       
   420 void CBrowserPopupEngine::AddUrlToCacheL( CDesCArrayFlat* aCacheArray, const TDesC& aUrl )
       
   421     {
       
   422 LOG_ENTERFN("CBrowserPopupEngine::AddUrlToCacheL");
       
   423 
       
   424 	__ASSERT_DEBUG( (aCacheArray != NULL), Util::Panic( Util::EUninitializedData ));
       
   425 
       
   426     if( iWhiteCacheNo < KCacheSize )
       
   427         {
       
   428         aCacheArray->AppendL( aUrl );
       
   429         iWhiteCacheNo++;
       
   430         BROWSER_LOG( ( _L( "<-Popup engine-> url added to cache : %S" ), &aUrl ) );
       
   431         }
       
   432     else
       
   433         {
       
   434         BROWSER_LOG( ( _L( "<-Popup engine-> url not added to cache : %S" ), &aUrl ) );
       
   435         }
       
   436     }
       
   437 
       
   438 // -----------------------------------------------------------------------------
       
   439 // CBrowserPopupEngine::TDesC16ToTPtrC8
       
   440 // -----------------------------------------------------------------------------
       
   441 //
       
   442 TPtrC8 CBrowserPopupEngine::TDesC16ToTPtrC8(const TDesC16 &aString)
       
   443     {
       
   444     TPtrC8 ptr8(reinterpret_cast<const TUint8*>(aString.Ptr()),(aString.Length()*2));
       
   445     return ptr8;
       
   446     }
       
   447 
       
   448 // -----------------------------------------------------------------------------
       
   449 // CBrowserPopupEngine::TDesC8ToTPtrC16
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 TPtrC16 CBrowserPopupEngine::TDesC8ToTPtrC16(const TDesC8 &aString)
       
   453     {
       
   454     TPtrC16 ptr16(reinterpret_cast<const TUint16*>(aString.Ptr()),(aString.Length()/2));
       
   455     return ptr16;
       
   456     }
       
   457 
       
   458 // -----------------------------------------------------------------------------
       
   459 // CBrowserPopupEngine::HandleUrlOrderChange
       
   460 // -----------------------------------------------------------------------------
       
   461 //
       
   462 void CBrowserPopupEngine::HandleUrlOrderChangeL(RFile* aFile, const TDesC& aUrl, const TBool aExists,const TBool aToAdd )
       
   463     {
       
   464 LOG_ENTERFN("CBrowserPopupEngine::HandleUrlOrderChangeL");
       
   465 
       
   466 	__ASSERT_DEBUG( (aFile != NULL), Util::Panic( Util::EUninitializedData ));
       
   467 
       
   468     RFile tmpFile;
       
   469     TInt err, match;
       
   470     HBufC8* line = HBufC8::NewL( 256 );
       
   471     CleanupStack::PushL( line );
       
   472     HBufC8* parturl = HBufC8::NewL( 256 );
       
   473     CleanupStack::PushL( parturl );
       
   474     HBufC8* url8;
       
   475     parturl->Des().Zero();
       
   476     TPtr8 linePtr = line->Des();
       
   477     TPtrC8 linePtrOffset;
       
   478     TPtrC8 tmpPtr;
       
   479     TInt result;
       
   480     TInt pos = 0;
       
   481     TInt offset;
       
   482     TInt writtenSize = 0;
       
   483     TBuf<KMaxFileName> privatePath;
       
   484     TBuf<KMaxFileName> listFileName;
       
   485     TBuf<KMaxFileName> bkpFileName;
       
   486     TBuf<KMaxFileName> tmpFileName;
       
   487 //set the filenames
       
   488     iFs.PrivatePath( privatePath );
       
   489     listFileName.Copy( privatePath );
       
   490     listFileName.Append( KWhiteListFileName );
       
   491     bkpFileName.Copy( privatePath );
       
   492     bkpFileName.Append( KWhiteBkpFileName );
       
   493     tmpFileName.Copy( privatePath );
       
   494     tmpFileName.Append( KWhiteTmpFileName );
       
   495 //create a tmp file
       
   496     err = tmpFile.Create( iFs, tmpFileName, EFileWrite | EFileShareExclusive );
       
   497     if( err != KErrNone )
       
   498         {
       
   499         RDebug::Print(_L("Creation of tmp file failed.\n"));
       
   500         BROWSER_LOG( ( _L( "<-Popup engine-> Creation of tmp file failed." ) ) ); 
       
   501         User::LeaveIfError( err );
       
   502         }
       
   503 //convert url to 8bit
       
   504     url8 = TDesC16ToHBufC8LC( aUrl );
       
   505 //add url to tmp file
       
   506     if( aToAdd )
       
   507         {
       
   508 //by url removal it is not needed to write the url
       
   509         tmpFile.Seek( ESeekStart, pos );
       
   510         tmpFile.Write( url8->Des() );
       
   511         tmpFile.Write( *TDesC16ToHBufC8LC( KEndMark )  );
       
   512         CleanupStack::PopAndDestroy();
       
   513         tmpFile.Flush();
       
   514         writtenSize = url8->Des().Size() + 1;
       
   515         }
       
   516 //write all urls to tmp file except the if needed
       
   517     parturl->Des().Zero();
       
   518     User::LeaveIfError( aFile->Seek( ESeekStart, pos ) );
       
   519     result = aFile->Read( linePtr, 256 );
       
   520     linePtr.Set(line->Des());
       
   521     writtenSize += linePtr.Length();
       
   522     if ( aExists )
       
   523        {
       
   524        while ( ( line->Des().Length() > 0 ) &&( result == KErrNone ))
       
   525             {
       
   526             offset=0;
       
   527 //if there was a part of an url check it too
       
   528             if( parturl->Length() != 0 )
       
   529                 {
       
   530                 offset = linePtr.Locate('\n' );
       
   531                 parturl->Des().Append( linePtr.Left( offset ) );
       
   532                 match = url8->Des().Compare( parturl->Des() );
       
   533                 if( match != 0 )
       
   534                     {
       
   535                     tmpFile.Write( parturl->Des() );
       
   536                     tmpFile.Write( *TDesC16ToHBufC8LC( KEndMark )  );
       
   537                     CleanupStack::PopAndDestroy();
       
   538                     }
       
   539                 offset++;
       
   540                 parturl->Des().Zero();
       
   541                 }
       
   542 //find the new url in the old file
       
   543             linePtrOffset.Set( linePtr.Right( linePtr.Length() - offset ));
       
   544             match = linePtrOffset.Find( url8->Des()  );
       
   545             if( match == KErrNotFound )
       
   546                 {
       
   547                 pos =  linePtrOffset.LocateReverse('\n' );
       
   548 //store the remaining part of the last url
       
   549                 if( pos != KErrNotFound )
       
   550                     {
       
   551                     if( pos == linePtrOffset.Length() -1 )
       
   552                         {
       
   553                         tmpFile.Write( linePtrOffset );
       
   554                         }
       
   555                     else
       
   556                         {
       
   557                         parturl->Des( ).Append( linePtrOffset.Mid( pos + 1, linePtrOffset.Length() - pos - 1 ));
       
   558                         tmpFile.Write( linePtrOffset.Left( pos+1 ));
       
   559                         }
       
   560                     }
       
   561                 else if( linePtrOffset.Length() > 0 )
       
   562                     {
       
   563                     parturl->Des( ).Append( linePtrOffset );
       
   564                     }
       
   565                 }
       
   566             else
       
   567                 {
       
   568                 if( match == 0)
       
   569                     {
       
   570                     tmpFile.Write( linePtrOffset.Right( linePtrOffset.Length() - url8->Des().Length() - 1 ) );
       
   571                     }
       
   572                 else
       
   573                     {
       
   574                     pos =  linePtrOffset.LocateReverse('\n' );
       
   575                     tmpFile.Write( linePtrOffset, match );
       
   576                     if( pos < linePtrOffset.Length() - 1  )
       
   577                         {
       
   578                         if( pos == linePtrOffset.Length() - url8->Des().Length() - 1 )
       
   579                             {
       
   580                             parturl->Des( ).Append( linePtrOffset.Right( url8->Des().Length() ) );
       
   581                             }
       
   582                         else//add to offset
       
   583                             {
       
   584                             tmpFile.Write( linePtrOffset.Right( linePtrOffset.Length() - match - url8->Des().Length() - 1 ) );
       
   585                             }
       
   586                         }
       
   587                     }
       
   588                 }
       
   589             result = aFile->Read( linePtr, 256 );
       
   590             linePtr.Set(line->Des());
       
   591             }
       
   592        }
       
   593     else
       
   594         {
       
   595 //copy the whole file
       
   596         while ( ( line->Des().Length() > 0 ) &&( result == KErrNone ))
       
   597             {
       
   598             result = tmpFile.Write( linePtr );
       
   599             if ( result == KErrNone )
       
   600                 {
       
   601                 result = aFile->Read( linePtr, 256 );
       
   602                 linePtr.Set( line->Des() );
       
   603                 writtenSize += linePtr.Length();
       
   604                 if( writtenSize > KMaxDbSize )
       
   605                     {
       
   606                     offset = linePtr.Length();
       
   607                     while( writtenSize > KMaxDbSize )
       
   608                         {
       
   609                         offset = linePtr.Left( offset ).LocateReverse('\n' );
       
   610                         writtenSize -= linePtr.Length() - offset ;
       
   611                         }
       
   612                     tmpFile.Write( linePtr, offset );
       
   613                     tmpFile.Write( *TDesC16ToHBufC8LC( KEndMark )  );
       
   614                     CleanupStack::PopAndDestroy();
       
   615                     break;
       
   616                     }
       
   617                 }
       
   618             }
       
   619         }
       
   620     tmpFile.Flush();
       
   621 //rename original file to bkp
       
   622     User::LeaveIfError( aFile->Rename( bkpFileName ) );
       
   623     aFile->Close();
       
   624 //rename temp file to original
       
   625     User::LeaveIfError( tmpFile.Rename( listFileName ) );
       
   626     tmpFile.Close();
       
   627     User::LeaveIfError( aFile->Open( iFs, listFileName, EFileWrite | EFileStream | EFileShareExclusive ) );
       
   628 //delete bkp file
       
   629     User::LeaveIfError( iFs.Delete( bkpFileName ) );
       
   630     CleanupStack::PopAndDestroy( 3 );
       
   631     }
       
   632 
       
   633 // -----------------------------------------------------------------------------
       
   634 // CBrowserPopupEngine::RemoveUrlFromWhiteListL
       
   635 // -----------------------------------------------------------------------------
       
   636 //
       
   637 void CBrowserPopupEngine::RemoveUrlFromWhiteListL(const TDesC& aUrl)
       
   638     {
       
   639 LOG_ENTERFN("CBrowserPopupEngine::RemoveUrlFromWhiteListL");
       
   640     TUriParser16 urlParser;
       
   641     urlParser.Parse(aUrl);
       
   642     if( !urlParser.IsSchemeValid() )
       
   643         {
       
   644         BROWSER_LOG( ( _L( "<-Popup engine-> Url is not valid." ) ) );
       
   645         User::Leave( KErrBadName );
       
   646         }
       
   647     HandleUrlOrderChangeL( &iWhiteListFile, urlParser.Extract( EUriHost), ETrue, EFalse );
       
   648     BROWSER_LOG( ( _L( "<-Popup engine-> Url removed from database." ) ) );
       
   649     if( iWhiteCacheNo > 0)
       
   650         {
       
   651         RemoveUrlFromCacheL( iCachedWhiteUrls, urlParser.Extract( EUriHost), iWhiteCacheNo );
       
   652         }
       
   653     }
       
   654 
       
   655 // -----------------------------------------------------------------------------
       
   656 // CBrowserPopupEngine::RemoveUrlFromCacheL
       
   657 // -----------------------------------------------------------------------------
       
   658 //
       
   659 void CBrowserPopupEngine::RemoveUrlFromCacheL( CDesCArrayFlat* aCacheArray, const TDesC& aUrl, TInt &aCacheNo )
       
   660     {
       
   661 LOG_ENTERFN("CBrowserPopupEngine::RemoveUrlFromCacheL");
       
   662 
       
   663 	__ASSERT_DEBUG( (aCacheArray != NULL), Util::Panic( Util::EUninitializedData ));
       
   664 
       
   665     TInt pos;
       
   666     TInt ret;
       
   667     ret = aCacheArray->Find( aUrl, pos );
       
   668     if( ret == 0 )
       
   669         {
       
   670         aCacheArray->Delete( pos );
       
   671         aCacheArray->Compress();
       
   672         aCacheNo--;
       
   673         BROWSER_LOG( ( _L( "<-Popup engine-> Url is found in cache and removed." ) ) );
       
   674         }
       
   675     else
       
   676         {
       
   677         BROWSER_LOG( ( _L( "<-Popup engine-> Url is not in cache." ) ) );
       
   678         }
       
   679     }
       
   680 
       
   681 // -----------------------------------------------------------------------------
       
   682 // CBrowserPopupEngine::CheckDbValidity
       
   683 // -----------------------------------------------------------------------------
       
   684 //
       
   685 TBool CBrowserPopupEngine::CheckDbValidity(RFile* aFile)
       
   686 {
       
   687 LOG_ENTERFN("CBrowserPopupEngine::CheckDbValidity");
       
   688 
       
   689 	__ASSERT_DEBUG( (aFile != NULL), Util::Panic( Util::EUninitializedData ));
       
   690 
       
   691     TInt size;
       
   692     TBuf8<1> tmp;
       
   693     aFile->Size( size );
       
   694     if( size == 0)
       
   695         {
       
   696 //empty file is ok
       
   697         BROWSER_LOG( ( _L( "<-Popup engine-> File is valid." ) ) );
       
   698         return ETrue;
       
   699         }
       
   700     else
       
   701         {
       
   702 //check whether the last char is \n
       
   703         size = -1;
       
   704         aFile->Seek( ESeekEnd, size );
       
   705         aFile->Read( tmp, 1 );
       
   706         if( tmp.Compare( *TDesC16ToHBufC8LC( KEndMark ) ) == 0 )
       
   707             {
       
   708             CleanupStack::PopAndDestroy( );
       
   709             BROWSER_LOG( ( _L( "<-Popup engine-> File is valid." ) ) );
       
   710             return ETrue;
       
   711             }
       
   712         else
       
   713             {
       
   714             CleanupStack::PopAndDestroy( );
       
   715             BROWSER_LOG( ( _L( "<-Popup engine-> File is not valid." ) ) );
       
   716             return EFalse;
       
   717             }
       
   718         }
       
   719     }
       
   720 
       
   721 // -----------------------------------------------------------------------------
       
   722 // CBrowserPopupEngine::ConvertTDesC16ToHBufC8LC
       
   723 // -----------------------------------------------------------------------------
       
   724 //
       
   725 HBufC8 *CBrowserPopupEngine::TDesC16ToHBufC8LC(const TDesC16 &string)
       
   726     {
       
   727     HBufC8 *buff = HBufC8::NewLC(string.Length());
       
   728     buff->Des().Copy(string);
       
   729     return buff;
       
   730     }
       
   731 
       
   732 // -----------------------------------------------------------------------------
       
   733 // CBrowserPopupEngine::TDesC8ToHBufC16LC
       
   734 // -----------------------------------------------------------------------------
       
   735 //
       
   736 HBufC16 *CBrowserPopupEngine::TDesC8ToHBufC16LC(const TDesC8 &aString)
       
   737     {
       
   738     HBufC16 *buff = HBufC16::NewLC(aString.Length());
       
   739     buff->Des().Copy(aString);
       
   740     return buff;
       
   741     }
       
   742 
       
   743 // End of File