browserutilities/downloadmgr/DownloadMgrServEng/Src/BuffStorage.cpp
branchRCL_3
changeset 50 d96eed154187
parent 49 919f36ff910f
equal deleted inserted replaced
49:919f36ff910f 50:d96eed154187
    54 // ---------------------------------------------------------
    54 // ---------------------------------------------------------
    55 // CBuffStorage::CBuffStorage
    55 // CBuffStorage::CBuffStorage
    56 // ---------------------------------------------------------
    56 // ---------------------------------------------------------
    57 //
    57 //
    58 CBuffStorage::CBuffStorage( CHttpStorage* aHttpStorage )
    58 CBuffStorage::CBuffStorage( CHttpStorage* aHttpStorage )
    59 :CActive( EPriorityHigh ), 
    59 :iFile(aHttpStorage->iFile),
    60  iFile(aHttpStorage->iFile),
       
    61  iDownloadedSize(aHttpStorage->iDownloadedSize), 
    60  iDownloadedSize(aHttpStorage->iDownloadedSize), 
    62  iBufferedSize(aHttpStorage->iBufferedSize),
    61  iBufferedSize(aHttpStorage->iBufferedSize),
    63  iHttpStorageBufferSize(aHttpStorage->iBufferSize),
    62  iHttpStorageBufferSize(aHttpStorage->iBufferSize),
    64  iHttpStorageLength(aHttpStorage->iLength),
    63  iHttpStorageLength(aHttpStorage->iLength),
    65  iBufferingEnabled(aHttpStorage->iBufferingEnabled),
    64  iBufferingEnabled(aHttpStorage->iBufferingEnabled),
    75 // Symbian 2nd phase constructor can leave.
    74 // Symbian 2nd phase constructor can leave.
    76 // -----------------------------------------------------------------------------
    75 // -----------------------------------------------------------------------------
    77 //
    76 //
    78 void CBuffStorage::ConstructL()
    77 void CBuffStorage::ConstructL()
    79     {
    78     {
    80     LOGGER_ENTERFN( "ConstructL" );
       
    81     CActiveScheduler::Add( this );
       
    82     iWait = new (ELeave) CActiveSchedulerWait;
       
    83     }
    79     }
    84 
    80 
    85 // -----------------------------------------------------------------------------
    81 // -----------------------------------------------------------------------------
    86 // CBuffStorage::NewL
    82 // CBuffStorage::NewL
    87 // Two-phased constructor.
    83 // Two-phased constructor.
   102 // CBuffStorage::~CBuffStorage
    98 // CBuffStorage::~CBuffStorage
   103 // ---------------------------------------------------------
    99 // ---------------------------------------------------------
   104 //
   100 //
   105 CBuffStorage::~CBuffStorage()
   101 CBuffStorage::~CBuffStorage()
   106     {
   102     {
   107     Cancel();
       
   108 
       
   109 	ResetBuffers();
   103 	ResetBuffers();
   110 	
   104 	
   111 	delete iWritePtr; iWritePtr = 0;
   105 	delete iWritePtr; iWritePtr = 0;
   112 	if(iWait)
   106     }
   113 	    {
   107 
   114 	     delete iWait;
   108 
   115 	     iWait = NULL;
   109 
   116 	    }
       
   117     }
       
   118 
       
   119 
       
   120 
       
   121 // ---------------------------------------------------------
       
   122 // CBuffStorage::RunL
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 void CBuffStorage::RunL()
       
   126     {
       
   127     LOGGER_ENTERFN( "RunL" );
       
   128     // Save the error code
       
   129     iLastWriteErrorCode = iStatus.Int();
       
   130    	
       
   131     if(iLastWriteErrorCode==KErrNone && iWritePtr)
       
   132     	{
       
   133     	// Update how much was written on the file
       
   134     	iDownloadedSize += iWritePtr->Length();
       
   135     	
       
   136     	CLOG_WRITE_2( "(%08X) CBuffStorage::RunL: Async write finished, downloaded now: %d", this, iDownloadedSize);
       
   137     	}
       
   138     else
       
   139     	{
       
   140     	CLOG_WRITE_2( "(%08X) CBuffStorage::RunL DH-iStat: %d, ", this, iStatus.Int() );
       
   141     	}
       
   142     	
       
   143     if(iWait && iWait->IsStarted())
       
   144 		{
       
   145 		CLOG_WRITE_1 ( "(%08X) CBuffStorage::RunL() Stopping iWait", this );
       
   146 		iWait->AsyncStop();
       
   147 		}
       
   148     }
       
   149 
       
   150 void CBuffStorage::DoCancel()
       
   151 	{
       
   152 	CLOG_WRITE_1("(%08X) CBuffStorage::DoCancel", this);
       
   153 	
       
   154 	// This is ok, CActive::Cancel always waits for the async operation to finish in this case
       
   155 	iLastWriteErrorCode = KErrCancel;
       
   156 	}
       
   157 
   110 
   158 // ---------------------------------------------------------
   111 // ---------------------------------------------------------
   159 // CBuffStorage::ResetBuffer
   112 // CBuffStorage::ResetBuffer
   160 // ---------------------------------------------------------
   113 // ---------------------------------------------------------
   161 //
   114 //
   162 void CBuffStorage::ResetBuffers()
   115 void CBuffStorage::ResetBuffers()
   163 	{	
   116 	{	
   164 	CLOG_WRITE_1("(%08X) CBuffStorage::ResetBuffers >>", this);
   117 	CLOG_WRITE_1("(%08X) CBuffStorage::ResetBuffers >>", this);
   165 	
       
   166 	if(IsActive()&& iWait && !iWait->IsStarted())
       
   167 		{
       
   168 		// Make sure async writes are finished
       
   169 		iWait->Start();
       
   170 		}
       
   171 	
       
   172 	// Cleanup
   118 	// Cleanup
   173     delete iBuff1; iBuff1 = NULL;
   119     delete iBuff1; iBuff1 = NULL;
   174     delete iBuff2; iBuff2 = NULL;
   120     delete iBuff2; iBuff2 = NULL;
   175     iClientBuffer = NULL;
   121     iClientBuffer = NULL;
   176     iBufferSize = 0;
   122     iBufferSize = 0;
   229 		}
   175 		}
   230 		
   176 		
   231 	CLOG_WRITE_1("(%08X) CBuffStorage::FlushBuffersL >>", this);
   177 	CLOG_WRITE_1("(%08X) CBuffStorage::FlushBuffersL >>", this);
   232 	
   178 	
   233 	// Make sure async writes are finished before doing anything
   179 	// Make sure async writes are finished before doing anything
   234 	if(IsActive() && iWait && !iWait->IsStarted())
       
   235 		{
       
   236 		CLOG_WRITE_1("(%08X) CBuffStorage::FlushBuffersL: stalling >>", this);
       
   237 	 	iWait->Start();
       
   238 	 	CLOG_WRITE_1("(%08X) CBuffStorage::FlushBuffersL: stalling <<", this);
       
   239 		}
       
   240 	
   180 	
   241 	if(iLastWriteErrorCode != KErrNone)
   181 	if(iLastWriteErrorCode != KErrNone)
   242 		{
   182 		{
   243 		CLOG_WRITE_2("(%08X) CBuffStorage::FlushBuffersL: last error = %d", this, iLastWriteErrorCode);
   183 		CLOG_WRITE_2("(%08X) CBuffStorage::FlushBuffersL: last error = %d", this, iLastWriteErrorCode);
   244 		TInt err = iLastWriteErrorCode;
   184 		TInt err = iLastWriteErrorCode;
   336 		// Subtract how much we copied from the loop counter and advance source data pointer
   276 		// Subtract how much we copied from the loop counter and advance source data pointer
   337 		incomingBuffSize -= toFillUp;
   277 		incomingBuffSize -= toFillUp;
   338 		src+=toFillUp;
   278 		src+=toFillUp;
   339 	    
   279 	    
   340 	    // Now we have a full client buffer, better do something with it
   280 	    // Now we have a full client buffer, better do something with it
   341 	    
       
   342 	    // Check if previous async write is still ongoing
       
   343 	    // Done here so if somebody switched on progressive download midway through we don't mix buffers
       
   344 		if(IsActive()&& iWait && !iWait->IsStarted())
       
   345 			{
       
   346 			CLOG_WRITE_1("(%08X) CBuffStorage::DoBufferingWriteL: stalling >>", this);
       
   347 		 	iWait->Start();
       
   348 		 	CLOG_WRITE_1("(%08X) CBuffStorage::DoBufferingWriteL: stalling <<", this);
       
   349 			}
       
   350 		
   281 		
   351 		// In case of async writes we have to check if there was error previously
   282 		// In case of async writes we have to check if there was error previously
   352 		if(iLastWriteErrorCode != KErrNone)
   283 		if(iLastWriteErrorCode != KErrNone)
   353 			{
   284 			{
   354 			// Clear the error code and leave with it
   285 			// Clear the error code and leave with it
   391 		iFile->Seek(ESeekCurrent, currentPos);
   322 		iFile->Seek(ESeekCurrent, currentPos);
   392 		CLOG_WRITE_3("(%08X) CBuffStorage::DoBufferingWriteL - Start async write %d bytes, filepos=%d", this, iWritePtr->Length(), currentPos);
   323 		CLOG_WRITE_3("(%08X) CBuffStorage::DoBufferingWriteL - Start async write %d bytes, filepos=%d", this, iWritePtr->Length(), currentPos);
   393 #endif
   324 #endif
   394 
   325 
   395 		// Start the async write and set AO active
   326 		// Start the async write and set AO active
   396 	 	iFile->Write( *iWritePtr, iStatus ) ;
   327         iFile->Write( *iWritePtr ) ;
   397 	 	SetActive();
   328         iDownloadedSize += iWritePtr->Length();
   398 	 	
   329         // Swap buffer pointers (we can't use the same buffer here 
   399 	 	// Swap buffer pointers (we can't use the same buffer here 
       
   400 	 	// since it is not known when the buffer can be used again)
   330 	 	// since it is not known when the buffer can be used again)
   401 	 	if(iClientBuffer == iBuff1)
   331 	 	if(iClientBuffer == iBuff1)
   402 	 		{
   332 	 		{
   403 	 		iClientBuffer = iBuff2;
   333 	 		iClientBuffer = iBuff2;
   404 	 		}
   334 	 		}
   418 // ---------------------------------------------------------
   348 // ---------------------------------------------------------
   419 //	
   349 //	
   420 void CBuffStorage::DoNonbufferingWriteL(const TDesC8& aBuf)
   350 void CBuffStorage::DoNonbufferingWriteL(const TDesC8& aBuf)
   421 	{
   351 	{
   422 	CLOG_WRITE_2("(%08X) CBuffStorage::DoNonbufferingWriteL: %d bytes", this, aBuf.Length());
   352 	CLOG_WRITE_2("(%08X) CBuffStorage::DoNonbufferingWriteL: %d bytes", this, aBuf.Length());
   423 	
       
   424 	if(IsActive() && iWait && !iWait->IsStarted())
       
   425 		{
       
   426 		CLOG_WRITE_1("(%08X) CBuffStorage::DoNonbufferingWriteL: stalling >>", this);
       
   427 		iWait->Start();
       
   428 		CLOG_WRITE_1("(%08X) CBuffStorage::DoNonbufferingWriteL: stalling <<", this);
       
   429 		}
       
   430 		
   353 		
   431 	TInt len = aBuf.Length();
   354 	TInt len = aBuf.Length();
   432 	if(len)
   355 	if(len)
   433 		{
   356 		{
   434     	User::LeaveIfError( iFile->Write( aBuf ) );
   357     	User::LeaveIfError( iFile->Write( aBuf ) );