harvester/blacklistclient/src/blacklistclient.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 // USER INCLUDE
       
    18 #include "blacklistclient.h"
       
    19 #include "blacklistcommon.h"
       
    20 #include "mdcserializationbuffer.h"
       
    21 #include "blacklistitem.h"
       
    22 #include "harvesterlog.h"
       
    23 
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // RBlacklistClient::RBlacklistClient()
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 EXPORT_C RBlacklistClient::RBlacklistClient() : RSessionBase (),
       
    30     iSessionOk( EFalse )
       
    31     {
       
    32     }
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // RBlacklistClient::~RBlacklistClient()
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 EXPORT_C RBlacklistClient::~RBlacklistClient()
       
    39     {
       
    40     WRITELOG( "CBlacklistServer::~RBlacklistClient - begin" );
       
    41     
       
    42     RSessionBase::Close(); 
       
    43     iBlacklistMemoryTable.ResetAndDestroy();
       
    44     iBlacklistMemoryTable.Close();
       
    45 	iBlacklistChunk.Close();
       
    46 
       
    47     WRITELOG( "CBlacklistServer::~RBlacklistClient - end" );
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // RBlacklistClient::Connect()
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 EXPORT_C TInt RBlacklistClient::Connect()
       
    55     {
       
    56     WRITELOG( "CBlacklistServer::Connect - begin" );
       
    57 
       
    58     TInt retryCount = 2;
       
    59     TInt error = KErrNone;
       
    60     
       
    61     iSessionOk = EFalse;
       
    62     
       
    63     while ( retryCount )
       
    64         {
       
    65         // try create session, if ok, then break out and return KErrNone
       
    66         error = CreateSession( KBlacklistServerName, Version() );
       
    67         if( error != KErrNotFound && error != KErrServerTerminated )
       
    68             {
       
    69             iSessionOk = ETrue;
       
    70             break;
       
    71             }
       
    72         
       
    73         // Cannot create session, start server
       
    74         error = StartServer();
       
    75         
       
    76         if ( error != KErrNone && error != KErrAlreadyExists )
       
    77             {
       
    78             break;
       
    79             }
       
    80        
       
    81         --retryCount;
       
    82         }
       
    83     
       
    84     WRITELOG( "CBlacklistServer::Connect - end" );
       
    85 
       
    86     return error;
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // RBlacklistClient::Version()
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 TVersion RBlacklistClient::Version() const
       
    94     {
       
    95     WRITELOG( "CBlacklistServer::Version - begin" );
       
    96 
       
    97     return TVersion( KBlacklistServerMajorVersion, KBlacklistServerMinorVersion,
       
    98     	KBlacklistServerBuildVersion );
       
    99 
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // RBlacklistClient::StartServer()
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 TInt RBlacklistClient::StartServer()
       
   107     {
       
   108     WRITELOG( "CBlacklistServer::CustomSecurityCheckL - begin" );
       
   109 
       
   110     const TUidType serverUid = ( KNullUid, KNullUid, KUidKBlacklistServer );
       
   111 
       
   112     RProcess server;
       
   113     TInt error = server.Create( KBlacklistServerExe, KNullDesC );
       
   114     if( error != KErrNone )
       
   115         {
       
   116         return error;
       
   117         }
       
   118     
       
   119     // start server and wait for signal before proceeding    
       
   120     TRequestStatus status;
       
   121     server.Rendezvous( status );
       
   122     if ( status.Int() != KRequestPending )
       
   123         {
       
   124         server.Kill( 0 );
       
   125         }
       
   126     else
       
   127         {
       
   128         server.Resume();
       
   129         }
       
   130 
       
   131     User::WaitForRequest( status );
       
   132     error = server.ExitType() == EExitPanic ? KErrGeneral : status.Int();
       
   133     server.Close();
       
   134 
       
   135     WRITELOG( "CBlacklistServer::Version - end" );
       
   136 
       
   137     return error;
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // RBlacklistClient::RemoveFromDBL()
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void RBlacklistClient::RemoveFromDBL( const TDesC& aUri, TUint32 aMediaId ) const
       
   145     {
       
   146     WRITELOG( "CBlacklistServer::RemoveFromDBL - begin" );
       
   147 
       
   148     TPckgBuf<TUint32> mediaIdPckg( aMediaId );
       
   149     
       
   150     TIpcArgs ipcArgs;
       
   151     ipcArgs.Set( 1, &aUri );
       
   152     ipcArgs.Set( 2, &mediaIdPckg );
       
   153  
       
   154     TInt err = SendReceive( EBlacklistRemoveFromDB, ipcArgs );
       
   155     User::LeaveIfError( err );
       
   156 
       
   157     WRITELOG( "CBlacklistServer::RemoveFromDBL - end" );
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // RBlacklistClient::DoLoadBlacklistL()
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void RBlacklistClient::DoLoadBlacklistL( TInt& aHandle ) const
       
   165     {
       
   166     WRITELOG( "CBlacklistServer::DoLoadBlacklistL - begin" );
       
   167 
       
   168     TPckgBuf<TInt> handleBuf;
       
   169     TIpcArgs ipcArgs;
       
   170     ipcArgs.Set( 1, &handleBuf );
       
   171     TInt err = SendReceive( EGetBlacklistData, ipcArgs );
       
   172     User::LeaveIfError( err );
       
   173     aHandle = handleBuf();
       
   174 
       
   175     WRITELOG( "CBlacklistServer::DoLoadBlacklistL - end" );
       
   176     } 
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // RBlacklistClient::LoadBlacklistL()
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 EXPORT_C void RBlacklistClient::LoadBlacklistL()
       
   183     {
       
   184     WRITELOG( "CBlacklistServer::LoadBlacklistL - begin" );
       
   185 
       
   186     if ( !iSessionOk )
       
   187         {
       
   188         return;
       
   189         }
       
   190 
       
   191     // delete old data
       
   192     if( iBlacklistMemoryTable.Count() > 0 )
       
   193         {
       
   194         iBlacklistMemoryTable.ResetAndDestroy();
       
   195         }
       
   196     
       
   197     // Get handle to data
       
   198     TInt handle( 0 );
       
   199     DoLoadBlacklistL( handle );
       
   200     
       
   201    	// create memory chunk
       
   202    	HBufC*  name = HBufC::NewLC( 32 );
       
   203 	*name = KBlacklistChunkName;
       
   204 	name->Des().AppendNum( handle );
       
   205 	iBlacklistChunk.Close();
       
   206 
       
   207 	User::LeaveIfError ( iBlacklistChunk.OpenGlobal( *name, ETrue ) );
       
   208 
       
   209 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( iBlacklistChunk.Base(), iBlacklistChunk.Size() );
       
   210 
       
   211 	if ( buffer->Size() == 0 )
       
   212 	    {
       
   213 	    User::Leave( KErrNotFound );
       
   214 	    }
       
   215 	
       
   216 	// First get list count
       
   217 	TUint32 listCount ( 0 );
       
   218 	buffer->ReceiveL( listCount );
       
   219 	
       
   220 	TInt64 modified ( 0 );
       
   221 	TUint32 mediaId ( 0 );
       
   222     HBufC* uri = NULL;
       
   223     
       
   224     for( TInt i( 0 ); i < listCount; i++ )
       
   225         {
       
   226         // get modified and media id
       
   227 	    buffer->ReceiveL( modified );
       
   228 	    buffer->ReceiveL( mediaId );
       
   229         
       
   230         //Get uri
       
   231         uri = buffer->ReceiveDes16L();
       
   232        	CleanupStack::PushL( uri );
       
   233        	AddToMemoryTableL( modified, *uri, mediaId );
       
   234        	CleanupStack::PopAndDestroy( uri );
       
   235        	uri = NULL;
       
   236         }
       
   237 	
       
   238 	CleanupStack::PopAndDestroy( buffer );
       
   239 	CleanupStack::PopAndDestroy( name );
       
   240 
       
   241     WRITELOG( "CBlacklistServer::LoadBlacklistL - end" );
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // RBlacklistClient::AddToMemoryTableL()
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 void RBlacklistClient::AddToMemoryTableL( const TInt64& aModified,
       
   249         const TDesC& aUri, const TUint32 aMediaId )
       
   250     {
       
   251     WRITELOG( "CBlacklistServer::AddToMemoryTableL - begin" );
       
   252 
       
   253     CBlacklistItem* item = CBlacklistItem::NewL( aModified, aUri, aMediaId );
       
   254     
       
   255     const TInt err = iBlacklistMemoryTable.Append( item ); // ownership is transferred
       
   256     if ( err != KErrNone )
       
   257         {
       
   258         delete item;
       
   259         }
       
   260 
       
   261 
       
   262     WRITELOG( "CBlacklistServer::AddToMemoryTableL - end" );
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // RBlacklistClient::RemoveFromMemoryTableL()
       
   267 // ---------------------------------------------------------------------------
       
   268 //
       
   269 void RBlacklistClient::RemoveFromMemoryTableL( const TDesC& aUri, const TUint32 aMediaId )
       
   270     {
       
   271     WRITELOG( "CBlacklistServer::RemoveFromMemoryTableL - begin" );
       
   272 
       
   273     TInt index = GetListIndex( aUri, aMediaId );
       
   274     if ( index >= 0 )
       
   275         {
       
   276         CBlacklistItem* item = iBlacklistMemoryTable[index];
       
   277         delete item;
       
   278         iBlacklistMemoryTable.Remove( index );
       
   279         }
       
   280 
       
   281     WRITELOG( "CBlacklistServer::RemoveFromMemoryTableL - end" );
       
   282     }
       
   283  
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // RBlacklistClient::IsBlacklistedL()
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 EXPORT_C TBool RBlacklistClient::IsBlacklistedL( const TDesC& aUri, TUint32 aMediaId, TTime aLastModifiedTime )
       
   290     {
       
   291     WRITELOG( "CBlacklistServer::IsBlacklistedL - begin" );
       
   292 
       
   293     TInt index = GetListIndex( aUri, aMediaId );
       
   294     if ( index >= 0 )
       
   295         {
       
   296         TInt64 modified( 0 );
       
   297         modified = iBlacklistMemoryTable[index]->Modified();
       
   298         
       
   299         if( modified > 0 )
       
   300             {
       
   301             if ( modified == aLastModifiedTime.Int64() )
       
   302                 {
       
   303                 WRITELOG( "CBlacklistServer::IsBlacklistedL - file is blacklisted, modification time is different" );
       
   304                 return ETrue;
       
   305                 }
       
   306             else
       
   307                 {
       
   308                 // file might be different, so remove from blacklist
       
   309                 // and act like it wasn't found
       
   310                 RemoveFromMemoryTableL( aUri, aMediaId );
       
   311                 
       
   312                 // Remove from server DB
       
   313                 RemoveFromDBL( aUri, aMediaId );
       
   314                 }
       
   315             }
       
   316         else
       
   317             {
       
   318             WRITELOG( "CBlacklistServer::IsBlacklistedL - file is blacklisted, no modification time found" );
       
   319             return ETrue;
       
   320             }
       
   321         
       
   322         }
       
   323    
       
   324     WRITELOG( "CBlacklistServer::IsBlacklistedL - end" );
       
   325     return EFalse;
       
   326     }
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // RBlacklistClient::GetListIndex()
       
   330 // ---------------------------------------------------------------------------
       
   331 //
       
   332 TInt RBlacklistClient::GetListIndex( const TDesC& aUri, TUint32 aMediaId )
       
   333     {
       
   334     WRITELOG( "CBlacklistServer::GetListIndex - begin" );
       
   335 
       
   336     for ( TInt i( 0 ); i < iBlacklistMemoryTable.Count(); ++i )
       
   337         {
       
   338         if ( iBlacklistMemoryTable[i]->Compare( aUri, aMediaId ) )
       
   339             {
       
   340             return i;
       
   341             }
       
   342         }
       
   343 
       
   344     WRITELOG( "CBlacklistServer::GetListIndex - end" );
       
   345 
       
   346     return KErrNotFound;
       
   347     }
       
   348 
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 // RBlacklistClient::AddL()
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 EXPORT_C void RBlacklistClient::AddL( const TDesC& aUri, TUint32 aMediaId, TTime aLastModifiedTime ) const
       
   355     {
       
   356     WRITELOG( "CBlacklistServer::AddL - begin" );
       
   357 
       
   358     TPckgC<TUint32> mediaIdPckg( aMediaId );
       
   359     TPckgC<TTime> lastModifiedTimePckg( aLastModifiedTime );
       
   360 
       
   361 	TIpcArgs ipcArgs;
       
   362     ipcArgs.Set( 0, &mediaIdPckg );
       
   363     ipcArgs.Set( 1, &aUri );
       
   364     ipcArgs.Set( 2, &lastModifiedTimePckg );
       
   365  
       
   366     const TInt err = SendReceive( EBlacklistAdd, ipcArgs );
       
   367     User::LeaveIfError( err );
       
   368 
       
   369     WRITELOG( "CBlacklistServer::AddL - end" );
       
   370     }
       
   371 
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // RBlacklistClient::RemoveL()
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 EXPORT_C void RBlacklistClient::RemoveL( const TDesC& aUri, TUint32 aMediaId ) const
       
   378     {
       
   379     WRITELOG( "CBlacklistServer::RemoveL - begin" );
       
   380 
       
   381     TPckgBuf<TUint32> mediaIdPckg( aMediaId );
       
   382     
       
   383     TIpcArgs ipcArgs;
       
   384     ipcArgs.Set( 1, &aUri );
       
   385     ipcArgs.Set( 2, &mediaIdPckg );
       
   386  
       
   387     const TInt err = SendReceive( EBlacklistRemove, ipcArgs );
       
   388     User::LeaveIfError( err );
       
   389 
       
   390     WRITELOG( "CBlacklistServer::RemoveL - end" );
       
   391     }
       
   392 
       
   393 // ---------------------------------------------------------------------------
       
   394 // RBlacklistClient::CloseDBL()
       
   395 // ---------------------------------------------------------------------------
       
   396 //
       
   397 EXPORT_C void RBlacklistClient::CloseDBL()
       
   398     {
       
   399     WRITELOG( "CBlacklistServer::CloseDBL - begin" );
       
   400 
       
   401     if ( !iSessionOk )
       
   402         {
       
   403         User::Leave( KErrDisconnected );
       
   404         }
       
   405     else
       
   406         {
       
   407         Send( EBlacklistCloseDB );
       
   408         }
       
   409 
       
   410     WRITELOG( "CBlacklistServer::CloseDBL - end" );
       
   411     }
       
   412 
       
   413 
       
   414 // End of File