httpfilters/cookie/ManagerSrc/CookieGroupData.cpp
changeset 27 974c3ee9bf20
equal deleted inserted replaced
23:ea9c9681bbaf 27:974c3ee9bf20
       
     1 /*
       
     2  * CCookieGroupData.cpp
       
     3  *
       
     4  *  Created on: Nov 24, 2009
       
     5  *      Author: mohanti
       
     6  */
       
     7 
       
     8 //System Includes
       
     9 #include <e32base.h>
       
    10 #include <sysutil.h>
       
    11 #include <e32cmn.h>
       
    12 
       
    13 //User Includes
       
    14 #include "CookieGroupData.h"
       
    15 #include "CookieArray.h"
       
    16 #include "cookieipc.h"
       
    17 #include "CookieLogger.h"
       
    18 //Constants
       
    19 
       
    20 // Literals
       
    21 _LIT( KDefaultCookieFolder, "C:\\Private\\" );
       
    22 _LIT( KDefaultCookieFile,   "\\Cookies" );
       
    23 _LIT( KDefaultExtension, ".dat");
       
    24 _LIT( KUnderScore, "_");
       
    25 
       
    26 //Member Functions
       
    27 
       
    28 // ---------------------------------------------------------
       
    29 // CCookieGroupData::NewL
       
    30 // ---------------------------------------------------------
       
    31 //
       
    32 CCookieGroupData* CCookieGroupData::NewL(TUint32 aGroupId, const RArray<TUint32>& aAppuid, TBool aCookieSharableFlag)
       
    33     {
       
    34     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::NewL" ) ) );
       
    35     
       
    36     CCookieGroupData* self = CCookieGroupData::NewLC(aGroupId,aAppuid,aCookieSharableFlag);
       
    37     CleanupStack::Pop();
       
    38     
       
    39     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::NewL" ) ) );
       
    40     return self;
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------
       
    44 // CCookieGroupData::NewLC
       
    45 // ---------------------------------------------------------
       
    46 //
       
    47 CCookieGroupData* CCookieGroupData::NewLC(TUint32 aGroupId,const RArray<TUint32>& aAppuid,TBool aCookieSharableFlag)
       
    48     {
       
    49     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::NewLC" ) ) );
       
    50     
       
    51     CCookieGroupData* self = new (ELeave) CCookieGroupData(aGroupId,aCookieSharableFlag);
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL(aAppuid);
       
    54     
       
    55     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::NewLC:" ) ) );
       
    56     return self;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 // CCookieGroupData::CCookieGroupData
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 CCookieGroupData::CCookieGroupData(TUint32 aGroupId, TBool aCookieSharableFlag)
       
    64   :iGroupId(aGroupId),iCookieSharableFlag(aCookieSharableFlag)
       
    65     {
       
    66     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::CCookieGroupData:" ) ) );
       
    67     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::CCookieGroupData:" ) ) );
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CCookieGroupData::CookieArray
       
    72 // ---------------------------------------------------------
       
    73 //
       
    74 CCookieArray* CCookieGroupData::CookieArray()
       
    75     {
       
    76     return iCookieArray;
       
    77     }
       
    78 TInt CCookieGroupData::TotalAppUid()
       
    79     {
       
    80     return iSharedUid.Count();
       
    81     }
       
    82 // ---------------------------------------------------------
       
    83 // CCookieGroupData::~CCookieGroupData
       
    84 // ---------------------------------------------------------
       
    85 //
       
    86 CCookieGroupData::~CCookieGroupData()
       
    87     {
       
    88     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::~CCookieGroupData:" ) ) );
       
    89     
       
    90     WriteCookiesToFile();
       
    91     delete iCookieArray;
       
    92     delete iCookieFileName;
       
    93     iStringPool.Close();
       
    94     delete iCookiePacker;
       
    95     iFs.Close();
       
    96     iSharedUid.Close();
       
    97     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::~CCookieGroupData:" ) ) );
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------
       
   101 // CCookieGroupData::StorePersistentCookieL
       
   102 // ---------------------------------------------------------
       
   103 //
       
   104 void CCookieGroupData::StorePersistentCookieL( CCookie* aCookie,
       
   105                                                  const TDesC8& aRequestUri,
       
   106                                                  const TInt aIndex )
       
   107     {
       
   108     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::StorePersistentCookieL:aRequestUri = %S" ), &aRequestUri) );
       
   109     
       
   110     if (aIndex == -1)
       
   111         {
       
   112         iCookieArray->AddL( aCookie, aRequestUri);
       
   113         }
       
   114     else
       
   115         {
       
   116         iCookieArray->InsertL( aCookie, aIndex);
       
   117         }
       
   118     
       
   119     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::StorePersistentCookieL:" ) ) );
       
   120 
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------
       
   124 // CCookieGroupData::Count
       
   125 // ---------------------------------------------------------
       
   126 //
       
   127 TInt CCookieGroupData::Count()const
       
   128     {
       
   129     return iCookieArray->Count();
       
   130     }
       
   131 
       
   132 TUint32 CCookieGroupData::operator[](TInt aIndex) const
       
   133     {
       
   134     return (iSharedUid[aIndex]);
       
   135     }
       
   136 
       
   137 // ---------------------------------------------------------
       
   138 // CGroupIdInfo::At
       
   139 // ---------------------------------------------------------
       
   140 //
       
   141 TUint32 CCookieGroupData::At(TInt aIndex) const
       
   142     {
       
   143     return (iSharedUid[ aIndex ]);
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------
       
   147 // CGroupIdInfo::GetSharedUid
       
   148 // ---------------------------------------------------------
       
   149 //
       
   150 TUint32 CCookieGroupData::GetSharedUid(TInt aIndex)
       
   151     {
       
   152     return At(aIndex);
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------
       
   156 // CCookieGroupData::ConstructL
       
   157 // ---------------------------------------------------------
       
   158 //
       
   159 void CCookieGroupData::ConstructL(const RArray<TUint32>& aAppuid)
       
   160     {
       
   161     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::ConstructL:" ) ) );
       
   162     TInt count = aAppuid.Count();
       
   163     CLOG( ( EClient, 0, _L( "-> CGroupIdArray::count = %d" ), count ) );
       
   164     for(TInt i=0; i<count; i++)
       
   165         {
       
   166         CLOG( ( EClient, 0, _L( "-> CGroupIdArray::aAppuid[%d] = %x " ), aAppuid[i] ) );
       
   167         iSharedUid.AppendL(aAppuid[i]);
       
   168         }
       
   169     iCookieArray = new (ELeave) CCookieArray;
       
   170     iStringPool.OpenL();
       
   171     iCookiePacker = new (ELeave) TCookiePacker( iStringPool );
       
   172     iCookieFileName = HBufC::NewL( KCookieMaxFileNameLength );
       
   173 
       
   174     TPtr fileName( iCookieFileName->Des() );
       
   175     fileName.Copy( KDefaultCookieFolder );
       
   176     //fileName.AppendNum( iGroupId, EHex );
       
   177     fileName.AppendNum( RProcess().SecureId(), EHex );
       
   178     TBuf<KMaxFileName> buf(KDefaultCookieFile);
       
   179     buf.Append(KUnderScore);
       
   180     buf.AppendNum(iGroupId,EHex);
       
   181     //For Widget case file name shobe becookie_<sid>_<appuid>.dat
       
   182     if(!iCookieSharableFlag && iSharedUid[0]!=0)
       
   183         {
       
   184         buf.Append(KUnderScore);
       
   185         buf.AppendNum(iSharedUid[0],EHex);
       
   186         }
       
   187     fileName.Append(buf);
       
   188     fileName.Append(KDefaultExtension);
       
   189     if ( iFs.Connect() == KErrNone )
       
   190         {
       
   191         ReadCookiesFromFile();
       
   192         }
       
   193 
       
   194     CLOG( ( EClient, 0, _L( "<- CCookieGroupData:::ConstructL:" ) ) );
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------
       
   198 // CCookieGroupData::ReadCookiesFromFile
       
   199 // ---------------------------------------------------------
       
   200 //
       
   201 TInt CCookieGroupData::ReadCookiesFromFile()
       
   202     {
       
   203     CLOG( ( EServer, 0,_L( "-> CCookieGroupData::ReadCookiesFromFile" ) ) );
       
   204     
       
   205     TInt err;
       
   206     if ( iCookieFileName->Length() != 0 )
       
   207         {
       
   208 //        RFs iFs;
       
   209 //        User::LeaveIfError(iFs.Connect());
       
   210         RFile file;
       
   211         err = file.Open( iFs, *iCookieFileName,
       
   212                             EFileShareExclusive | EFileStream | EFileRead );
       
   213             if ( err == KErrNone )  // the file does exist and could be opened
       
   214                 {
       
   215                 TInt size;
       
   216                 err = file.Size( size );
       
   217                 if ( err == KErrNone )  // size query was successful
       
   218                     {
       
   219                     HBufC8* fileBuffer = HBufC8::New( size );
       
   220                     if ( fileBuffer )// there was enough memory for fileBuffer
       
   221                         {
       
   222                         TPtr8 fileBufferDes( fileBuffer->Des() );
       
   223                         err = file.Read( fileBufferDes );
       
   224                         if ( err == KErrNone )
       
   225                             {
       
   226                             // unfortunately this method might leave, because
       
   227                             // it allocates memory for cookies dynamically
       
   228                             TRAP( err,
       
   229                                 iCookiePacker->UnpackCookiesFromBufferL
       
   230                                                 ( *fileBuffer, iCookieArray->CookieArray() ) );
       
   231                             if ( err != KErrNone )
       
   232                                 {
       
   233                                 delete fileBuffer;
       
   234                                 file.Close();
       
   235                                 iFs.Delete(*iCookieFileName); //Delete file.
       
   236                                 //iFs.Close();
       
   237                                 return KErrNotFound;
       
   238                                 }
       
   239                             }
       
   240 
       
   241                         delete fileBuffer;
       
   242                         }
       
   243                     else
       
   244                         {
       
   245                         err = KErrNoMemory;
       
   246                         }
       
   247                     }
       
   248 
       
   249                 file.Close();
       
   250                 //iFs.Close();
       
   251             }
       
   252         }
       
   253     else    // if iCookieFileName->Length() == 0
       
   254         {
       
   255         err = KErrNotFound;
       
   256         }
       
   257 
       
   258     CLOG( ( EServer, 0,_L( "<- CCookieGroupData::ReadCookiesFromFile, errcode%d"), err ) );
       
   259 
       
   260     return err;
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------
       
   264 // CCookieGroupData::WriteCookiesToFile
       
   265 // ---------------------------------------------------------
       
   266 //
       
   267 TInt CCookieGroupData::WriteCookiesToFile()
       
   268     {
       
   269     CLOG( ( EServer, 0,_L( "-> CCookieGroupData::WriteCookiesToFile" ) ) );
       
   270 //    RFs iFs;
       
   271 //    User::LeaveIfError(iFs.Connect());
       
   272 
       
   273     TInt err(KErrNone);
       
   274     if ( !iCookieArray->Count() )
       
   275         {
       
   276          CLOG( ( EServer, 0,_L( "<- CCookieGroupData::WriteCookiesToFile, errcode%d" ), KErrNone ));
       
   277 
       
   278          // delete cookie file
       
   279          err = iFs.Delete( *iCookieFileName );
       
   280          return err;
       
   281         }
       
   282 
       
   283     if ( iCookieFileName->Length() != 0 )
       
   284         {
       
   285         if ( CheckDiskSpace( iFs, *iCookieFileName ) )
       
   286             {
       
   287             iFs.CreatePrivatePath( EDriveC );
       
   288             RFile file;
       
   289             iFs.MkDirAll(*iCookieFileName);
       
   290             err = file.Replace( iFs, *iCookieFileName,
       
   291                             EFileShareExclusive | EFileStream | EFileWrite );
       
   292                 if ( err == KErrNone )
       
   293                     {
       
   294                     // get the maximum length of cookies
       
   295                     TInt cookieCount( iCookieArray->Count() );
       
   296                     TInt size( 0 );
       
   297                     TInt maxSize( 0 );
       
   298                     for( TInt i = 0; i < cookieCount; i++ )
       
   299                         {
       
   300                         if ( (*iCookieArray)[i]->Persistent() &&
       
   301                              !(*iCookieArray)[i]->Expired() )
       
   302                             {
       
   303                             size = (*iCookieArray)[i]->Size( EFalse );
       
   304                             if( size > maxSize )
       
   305                                 {
       
   306                                 maxSize = size;
       
   307                                 }
       
   308                             }
       
   309                         }
       
   310                     maxSize++;
       
   311                     CLOG( ( EServer, 0, _L("maxSize: %d"), maxSize ) );
       
   312                     // allocate buffer for it
       
   313                     HBufC8* fileBuffer = HBufC8::New( maxSize );
       
   314                     if ( fileBuffer )
       
   315                         {
       
   316                         TPtr8 fileBufferDes = fileBuffer->Des();
       
   317 
       
   318                         // we ignore a possible packing or file writing error
       
   319                         // in this loop as these kinds of errors are not fatal
       
   320                         // and may not reappear during the next iteration
       
   321                         for ( TInt i = 0; i < cookieCount; i++ )
       
   322                             {
       
   323                         if ( (*iCookieArray)[i]->Persistent() &&
       
   324                              !(*iCookieArray)[i]->Expired() )
       
   325                             {
       
   326                                 fileBufferDes.SetLength(0);
       
   327 
       
   328                                 // use CliPackCookie as SrvPackCookie will
       
   329                                 // suppress the defaulted domain attribute...
       
   330                                 err = iCookiePacker->CliPackCookie( fileBufferDes,
       
   331                                                                 (*(*iCookieArray)[i]) );
       
   332 
       
   333                                 if ( err == KErrNone )
       
   334                                     {
       
   335                                     err = file.Write( *fileBuffer );
       
   336                                     }
       
   337                                 }
       
   338                             }
       
   339 
       
   340                         delete fileBuffer;
       
   341                         }
       
   342                     else
       
   343                         {
       
   344                         err = KErrNoMemory;
       
   345                         }
       
   346 
       
   347                     file.Close();
       
   348                     //iFs.Close();
       
   349                     }
       
   350                 }
       
   351             else    // there is not enough disk space
       
   352                 {
       
   353                 err = KErrDiskFull;
       
   354             }
       
   355         }
       
   356     else    // if ( iCookieFileName->Length() == 0 )
       
   357         {
       
   358         err = KErrNotFound;
       
   359         }
       
   360 
       
   361     CLOG( ( EServer, 0,
       
   362         _L( "<- CCookieGroupData::WriteCookiesToFile, errcode%d" ), err ) );
       
   363 
       
   364     return err;
       
   365     }
       
   366 
       
   367 // ---------------------------------------------------------
       
   368 // CCookieGroupData::CheckDiskSpace
       
   369 // ---------------------------------------------------------
       
   370 //
       
   371 TBool CCookieGroupData::CheckDiskSpace( RFs& aFileSystem,
       
   372                                            const TDesC& aFileName ) const
       
   373     {
       
   374     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::CheckDiskSpace:" ) ) );
       
   375 
       
   376     TInt err;
       
   377     TParse parse;
       
   378     err = parse.SetNoWild( aFileName, NULL, NULL );
       
   379     if ( err == KErrNone )
       
   380         {
       
   381         // This is in the form : drive-letter: (letter + semi-colon)
       
   382         TBuf<2> driveBuf( parse.Drive() );
       
   383         TCharF driveLetter( driveBuf[0] );
       
   384         TCharF driveALetter( 'A' );
       
   385         TDriveNumber driveNum = (TDriveNumber)( (TUint)(driveLetter) -
       
   386                                                 (TUint)(driveALetter) );
       
   387 
       
   388         TBool noSpace = EFalse;
       
   389         TRAP( err, noSpace = SysUtil::DiskSpaceBelowCriticalLevelL
       
   390                     ( &aFileSystem, KCookieMaxFileLength, driveNum ) );
       
   391         if ( err == KErrNone && noSpace )
       
   392             {
       
   393             err = KErrDiskFull;
       
   394             }
       
   395         }
       
   396     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::CheckDiskSpace:" ) ) );
       
   397     
       
   398     return ( err == KErrNone ? ETrue : EFalse );
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------
       
   402 // CCookieGroupData::ClearCookies
       
   403 // ---------------------------------------------------------
       
   404 //
       
   405 TInt CCookieGroupData::ClearAllCookies()
       
   406     {
       
   407     CLOG( ( EClient, 0, _L( "-> CCookieGroupData::ClearAllCookies:" ) ) );
       
   408     
       
   409     TInt count = iCookieArray->ClearAllCookies();
       
   410     iFs.Delete( *iCookieFileName );
       
   411 
       
   412     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::ClearAllCookies:count = %d " ), count ) );
       
   413     return count;
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------
       
   417 // CCookieGroupData::GetFileName
       
   418 // ---------------------------------------------------------
       
   419 //
       
   420 const TDesC& CCookieGroupData::GetFileName() const
       
   421     {
       
   422     CLOG( ( EClient, 0, _L( "-> CCookieGroupData:::GetFileName" ) ) );
       
   423 
       
   424     if ( iCookieFileName )
       
   425         {
       
   426         CLOG( ( EClient, 0, _L( "<- CCookieGroupData::GetFileName:iCookieFileName = %S" ), &iCookieFileName ) );
       
   427         return *iCookieFileName;
       
   428         }
       
   429     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::GetFileName:iCookieFileName = %S" ), &iCookieFileName ) );
       
   430     return KNullDesC();
       
   431     }
       
   432 
       
   433 // ---------------------------------------------------------
       
   434 // CCookieGroupData::SetFileName
       
   435 // ---------------------------------------------------------
       
   436 //
       
   437 void CCookieGroupData::SetFileName(TUint32& aAppUid,TUint32& aSecureId)
       
   438     {
       
   439     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::SetFileName" ) ) );
       
   440     *iCookieFileName = KNullDesC;
       
   441     TPtr fileName( iCookieFileName->Des() );
       
   442     fileName.Copy( KDefaultCookieFolder );
       
   443     fileName.AppendNum( RProcess().SecureId(), EHex );
       
   444     TBuf<KMaxFileName> buf(KDefaultCookieFile);
       
   445     buf.Append(KUnderScore);
       
   446     buf.AppendNum(aSecureId,EHex);
       
   447     //For Widget case file name shobe becookie_<sid>_<appuid>.dat
       
   448     if(!iCookieSharableFlag && iSharedUid[0]!=0)
       
   449         {
       
   450         buf.Append(KUnderScore);
       
   451         buf.AppendNum(aAppUid,EHex);
       
   452         }
       
   453     fileName.Append(buf);
       
   454     fileName.Append(KDefaultExtension);
       
   455     CLOG( ( EClient, 0, _L( "<- CCookieGroupData::SetFileName" ) ) );
       
   456     }
       
   457 //eof