|
1 /** @file |
|
2 * Copyright (c) 2007 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: Base class for downloader / uploader classes |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // System include files |
|
20 #include <escapeutils.h> |
|
21 |
|
22 // User include files |
|
23 #include "httptransferbase.h" |
|
24 #include "httptransferobserver.h" |
|
25 #include "httpfile.h" |
|
26 #include "httptransferworker.h" |
|
27 #include "httpworkerobserver.h" |
|
28 |
|
29 // Definitions |
|
30 enum TTransferStates |
|
31 { |
|
32 ETransferNextFile |
|
33 }; |
|
34 |
|
35 // ======== MEMBER FUNCTIONS ======== |
|
36 |
|
37 // -------------------------------------------------------------------------- |
|
38 // CHttpTransferBase::CHttpTransferBase() |
|
39 // (See comments in header file) |
|
40 // -------------------------------------------------------------------------- |
|
41 // |
|
42 CHttpTransferBase::CHttpTransferBase() : CActive( EPriorityStandard ) |
|
43 { |
|
44 CActiveScheduler::Add( this ); |
|
45 } |
|
46 |
|
47 // -------------------------------------------------------------------------- |
|
48 // CHttpTransferBase::~CHttpTransferBase() |
|
49 // (See comments in header file) |
|
50 // -------------------------------------------------------------------------- |
|
51 // |
|
52 CHttpTransferBase::~CHttpTransferBase() |
|
53 { |
|
54 if( IsActive() ) |
|
55 { |
|
56 CActive::Cancel(); |
|
57 } |
|
58 |
|
59 // Reset array and destroy all the items in it |
|
60 iFileQueue.ResetAndDestroy(); |
|
61 iWorkerArray.ResetAndDestroy(); |
|
62 iWaitQueue.ResetAndDestroy(); |
|
63 } |
|
64 |
|
65 // -------------------------------------------------------------------------- |
|
66 // CHttpDownloader::CancelAll |
|
67 // (See comments in header file) |
|
68 // -------------------------------------------------------------------------- |
|
69 // |
|
70 EXPORT_C void CHttpTransferBase::CancelAll() |
|
71 { |
|
72 // Reset arrays and destroy all the items in them |
|
73 iFileQueue.ResetAndDestroy(); |
|
74 iWaitQueue.ResetAndDestroy(); |
|
75 |
|
76 for( TInt i=0; i < iWorkerArray.Count(); i++ ) |
|
77 { |
|
78 if ( iWorkerArray[i]->ProcessOnGoing() ) |
|
79 { |
|
80 iWorkerArray[i]->CancelTransfer(); |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 // -------------------------------------------------------------------------- |
|
86 // CHttpTransferBase::Cancel |
|
87 // (See comments in header file) |
|
88 // -------------------------------------------------------------------------- |
|
89 // |
|
90 EXPORT_C TInt CHttpTransferBase::CancelTransfer( TAny* aKey ) |
|
91 { |
|
92 TInt retval = KErrNotFound; |
|
93 |
|
94 // Find the item corresponding aKey and delete it and |
|
95 // remove from the wait queue |
|
96 for ( TInt i = 0; i < iWaitQueue.Count(); i++ ) |
|
97 { |
|
98 if ( iWaitQueue[i]->Key() == aKey ) |
|
99 { |
|
100 retval = KErrNone; |
|
101 delete iWaitQueue[i]; |
|
102 iWaitQueue.Remove(i); |
|
103 iWaitQueue.Compress(); |
|
104 } |
|
105 } |
|
106 |
|
107 // Find the item corresponding aKey and delete it and |
|
108 // remove from the file queue |
|
109 for ( TInt i = 0; i < iFileQueue.Count(); i++ ) |
|
110 { |
|
111 if ( iFileQueue[i]->Key() == aKey ) |
|
112 { |
|
113 retval = KErrNone; |
|
114 delete iFileQueue[i]; |
|
115 iFileQueue.Remove(i); |
|
116 iFileQueue.Compress(); |
|
117 } |
|
118 } |
|
119 |
|
120 // Check if some of the ongoing processes has to be canceled |
|
121 for ( TInt i = 0; i < iWorkerArray.Count(); i++ ) |
|
122 { |
|
123 if ( iWorkerArray[i]->ProcessOnGoing() ) |
|
124 { |
|
125 if ( iWorkerArray[i]->Key() == aKey ) |
|
126 { |
|
127 retval = KErrNone; |
|
128 iWorkerArray[i]->CancelTransfer(); |
|
129 } |
|
130 } |
|
131 } |
|
132 |
|
133 return retval; |
|
134 } |
|
135 |
|
136 // -------------------------------------------------------------------------- |
|
137 // CHttpTransferBase::TrackProgress |
|
138 // (See comments in header file) |
|
139 // -------------------------------------------------------------------------- |
|
140 // |
|
141 EXPORT_C TInt CHttpTransferBase::TrackProgress( TAny* aKey, TBool aValue ) |
|
142 { |
|
143 TInt retval = KErrNotFound; |
|
144 |
|
145 // Find the corresponding file from iWaitQueue |
|
146 for ( TInt i = 0; i < iWaitQueue.Count(); i++ ) |
|
147 { |
|
148 if ( iWaitQueue[i]->Key() == aKey ) |
|
149 { |
|
150 retval = KErrNone; |
|
151 iWaitQueue[i]->TrackProgress( aValue ); |
|
152 } |
|
153 } |
|
154 |
|
155 // Find the corresponding instance of CHttpFile from iFileQueue and |
|
156 // switch the tracking of the progress on |
|
157 for ( TInt i = 0; i < iFileQueue.Count(); i++) |
|
158 { |
|
159 if ( iFileQueue[i]->Key() == aKey ) |
|
160 { |
|
161 retval = KErrNone; |
|
162 iFileQueue[i]->TrackProgress( aValue ); |
|
163 } |
|
164 } |
|
165 |
|
166 // Check if processed files have to be tracked |
|
167 for ( TInt i = 0; i < iWorkerArray.Count(); i++ ) |
|
168 { |
|
169 if ( iWorkerArray[i]->ProcessOnGoing() && |
|
170 iWorkerArray[i]->Key() == aKey ) |
|
171 { |
|
172 retval = KErrNone; |
|
173 iWorkerArray[i]->TrackProgress( aValue ); |
|
174 } |
|
175 } |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 // -------------------------------------------------------------------------- |
|
181 // CHttpTransferBase::SetHeaderL |
|
182 // (See comments in header file) |
|
183 // -------------------------------------------------------------------------- |
|
184 // |
|
185 EXPORT_C void CHttpTransferBase::SetHeaderL( TAny* aKey, |
|
186 const TDesC8& aFieldName, |
|
187 const TDesC8& aFieldValue ) |
|
188 { |
|
189 TBool keyExists = EFalse; |
|
190 |
|
191 // Go through iWaitQueue and if there is file mathich the key set headers |
|
192 for ( TInt i = 0; i < iWaitQueue.Count(); i++ ) |
|
193 { |
|
194 if ( iWaitQueue[i]->Key() == aKey ) |
|
195 { |
|
196 keyExists = ETrue; |
|
197 iWaitQueue[i]->SetHeaderL( aFieldName, aFieldValue ); |
|
198 } |
|
199 } |
|
200 |
|
201 // Go through workers and set the headers to the downloaded file which |
|
202 // has matching key |
|
203 for ( TInt i = 0; i < iWorkerArray.Count(); i++ ) |
|
204 { |
|
205 if ( iWorkerArray[i]->Key() == aKey ) |
|
206 { |
|
207 // if the worker is not waiting -> leave |
|
208 if ( !iWorkerArray[i]->IsWaiting() ) |
|
209 { |
|
210 User::Leave( KErrGeneral ); |
|
211 } |
|
212 |
|
213 ( iWorkerArray[i]->ProcessedFile() )-> |
|
214 SetHeaderL( aFieldName, aFieldValue ); |
|
215 keyExists = ETrue; |
|
216 } |
|
217 } |
|
218 |
|
219 if ( !keyExists ) |
|
220 { |
|
221 User::Leave( KErrNotFound ); |
|
222 } |
|
223 } |
|
224 |
|
225 // -------------------------------------------------------------------------- |
|
226 // CHttpTransferBase::CreateAndQueueHttpFile() |
|
227 // (See comments in header file) |
|
228 // -------------------------------------------------------------------------- |
|
229 // |
|
230 void CHttpTransferBase::CreateAndQueueHttpFileL( const TDesC8& aUri, |
|
231 const TDesC& aTargetPath, |
|
232 TAny* aKey ) |
|
233 { |
|
234 CHttpFile* httpfile = CHttpFile::NewL( aKey, aUri, aTargetPath ); |
|
235 iFileQueue.Append( httpfile ); |
|
236 } |
|
237 |
|
238 // -------------------------------------------------------------------------- |
|
239 // CHttpTransferBase::CreateAndQueueHttpFile() |
|
240 // (See comments in header file) |
|
241 // -------------------------------------------------------------------------- |
|
242 // |
|
243 void CHttpTransferBase::CreateAndQueueHttpFileL( const TDesC8& aUri, |
|
244 const RFile& aFileHandle, |
|
245 TAny* aKey ) |
|
246 { |
|
247 CHttpFile* httpfile = CHttpFile::NewL( aKey, aUri, aFileHandle ); |
|
248 iFileQueue.Append( httpfile ); |
|
249 } |
|
250 |
|
251 // -------------------------------------------------------------------------- |
|
252 // CHttpTransferBase::NextFile |
|
253 // (See comments in header file) |
|
254 // -------------------------------------------------------------------------- |
|
255 // |
|
256 CHttpFile* CHttpTransferBase::NextFile() |
|
257 { |
|
258 CHttpFile* newFile = NULL; |
|
259 |
|
260 // If iFileQueue has files |
|
261 if ( iFileQueue.Count() > 0 ) |
|
262 { |
|
263 // Set the new processed file |
|
264 newFile = iFileQueue[0]; |
|
265 |
|
266 // Set the queue |
|
267 iFileQueue.Remove(0); |
|
268 iFileQueue.Compress(); |
|
269 } |
|
270 |
|
271 return newFile; |
|
272 } |
|
273 |
|
274 // -------------------------------------------------------------------------- |
|
275 // CHttpTransferBase::StartTransferL() |
|
276 // (See comments in header file) |
|
277 // -------------------------------------------------------------------------- |
|
278 // |
|
279 EXPORT_C void CHttpTransferBase::StartTransferL( TAny* aKey ) |
|
280 { |
|
281 TBool keyExists = EFalse; |
|
282 |
|
283 // go through workerarray and if there is worker which has |
|
284 // corresponding key -> start the transfer |
|
285 for(TInt i=0; i < iWorkerArray.Count(); i++ ) |
|
286 { |
|
287 if ( iWorkerArray[i]->Key() == aKey ) |
|
288 { |
|
289 iWorkerArray[i]->StartProcessL(); |
|
290 keyExists = ETrue; |
|
291 } |
|
292 } |
|
293 |
|
294 // Leave if the key does not exist |
|
295 if ( !keyExists ) |
|
296 { |
|
297 User::Leave( KErrNotFound ); |
|
298 } |
|
299 } |
|
300 |
|
301 // -------------------------------------------------------------------------- |
|
302 // CHttpTransferBase::SetProperty() |
|
303 // (See comments in header file) |
|
304 // -------------------------------------------------------------------------- |
|
305 // |
|
306 EXPORT_C void CHttpTransferBase::SetPropertyL( TAny* aKey, |
|
307 THttpPropertyType aProperty, |
|
308 const TDesC& aValue ) |
|
309 { |
|
310 |
|
311 switch ( aProperty ) |
|
312 { |
|
313 // This case is used to set targetUri. This is used in upnp upload |
|
314 case ETargetURI: |
|
315 { |
|
316 TBool keyExists = EFalse; |
|
317 HBufC8* uri = EscapeUtils::ConvertFromUnicodeToUtf8L( aValue ); |
|
318 CleanupStack::PushL( uri ); |
|
319 |
|
320 for ( TInt i=0; i < iWorkerArray.Count(); i++ ) |
|
321 { |
|
322 if ( iWorkerArray[i]->Key() == aKey ) |
|
323 { |
|
324 ( iWorkerArray[i]->ProcessedFile() )-> |
|
325 SetUriL( uri->Des() ); |
|
326 keyExists = ETrue; |
|
327 } |
|
328 } |
|
329 |
|
330 CleanupStack::PopAndDestroy( uri ); |
|
331 |
|
332 // the key was not found |
|
333 if ( !keyExists ) |
|
334 { |
|
335 User::Leave( KErrNotFound ); |
|
336 } |
|
337 |
|
338 break; |
|
339 } |
|
340 |
|
341 default: |
|
342 { |
|
343 |
|
344 break; |
|
345 } |
|
346 } |
|
347 } |
|
348 |
|
349 // -------------------------------------------------------------------------- |
|
350 // CHttpTransferBase::GetPropertyL() |
|
351 // (See comments in header file) |
|
352 // -------------------------------------------------------------------------- |
|
353 // |
|
354 EXPORT_C const HBufC* CHttpTransferBase::GetPropertyL( |
|
355 TAny* aKey, |
|
356 THttpPropertyType aProperty ) |
|
357 { |
|
358 const HBufC* retval = NULL; |
|
359 |
|
360 switch ( aProperty ) |
|
361 { |
|
362 // Client can receive the path of the transferred file using this |
|
363 case ETargetPath: |
|
364 { |
|
365 TBool keyExists = EFalse; |
|
366 |
|
367 // Goes through workerarray and returns the path of the file |
|
368 // which matches the key |
|
369 for ( TInt i = 0; i < iWorkerArray.Count(); i++ ) |
|
370 { |
|
371 if ( iWorkerArray[i]->Key() == aKey ) |
|
372 { |
|
373 retval = iWorkerArray[i]->Path(); |
|
374 keyExists = ETrue; |
|
375 } |
|
376 } |
|
377 |
|
378 // the key was not found |
|
379 if ( !keyExists ) |
|
380 { |
|
381 User::Leave( KErrNotFound ); |
|
382 } |
|
383 |
|
384 break; |
|
385 } |
|
386 |
|
387 default: |
|
388 { |
|
389 |
|
390 break; |
|
391 } |
|
392 } |
|
393 |
|
394 return retval; |
|
395 } |
|
396 |
|
397 // -------------------------------------------------------------------------- |
|
398 // CHttpTransferBase::InsertFileIntoWaitQueueL() |
|
399 // (See comments in header file) |
|
400 // -------------------------------------------------------------------------- |
|
401 // |
|
402 EXPORT_C void CHttpTransferBase::InsertFileIntoWaitQueueL( |
|
403 TAny* aKey, |
|
404 const TDesC& aTargetPath, |
|
405 const TDesC8& aUri ) |
|
406 { |
|
407 // Checks first if the key already exists in iWaitQueue or in iFileQueue |
|
408 // if does -> leave |
|
409 for ( TInt i = 0; i < iWaitQueue.Count(); i++ ) |
|
410 { |
|
411 if ( iWaitQueue[i]->Key() == aKey ) |
|
412 { |
|
413 User::Leave( KErrAlreadyExists ); |
|
414 } |
|
415 } |
|
416 |
|
417 for ( TInt i = 0; i < iFileQueue.Count(); i++ ) |
|
418 { |
|
419 if ( iFileQueue[i]->Key() == aKey ) |
|
420 { |
|
421 User::Leave( KErrAlreadyExists ); |
|
422 } |
|
423 } |
|
424 |
|
425 // if key is valid create CHttpFile and insert it to iWaitQueue |
|
426 CHttpFile* httpfile = CHttpFile::NewL( aKey, aUri, aTargetPath ); |
|
427 iWaitQueue.Append( httpfile ); |
|
428 } |
|
429 |
|
430 // -------------------------------------------------------------------------- |
|
431 // CHttpTransferBase::InsertFileIntoWaitQueueL() |
|
432 // (See comments in header file) |
|
433 // -------------------------------------------------------------------------- |
|
434 // |
|
435 EXPORT_C void CHttpTransferBase::InsertFileIntoWaitQueueL( |
|
436 TAny* aKey, |
|
437 const RFile& aFileHandle, |
|
438 const TDesC8& aUri ) |
|
439 { |
|
440 // Checks first if the key already exists in iWaitQueue or in iFileQueue |
|
441 // if does -> leave |
|
442 for ( TInt i = 0; i < iWaitQueue.Count(); i++ ) |
|
443 { |
|
444 if ( iWaitQueue[i]->Key() == aKey ) |
|
445 { |
|
446 User::Leave( KErrAlreadyExists ); |
|
447 } |
|
448 } |
|
449 |
|
450 for ( TInt i = 0; i < iFileQueue.Count(); i++ ) |
|
451 { |
|
452 if ( iFileQueue[i]->Key() == aKey ) |
|
453 { |
|
454 User::Leave( KErrAlreadyExists ); |
|
455 } |
|
456 } |
|
457 |
|
458 // if key is valid create CHttpFile and insert it to iWaitQueue |
|
459 CHttpFile* httpfile = CHttpFile::NewL( aKey, aUri, aFileHandle ); |
|
460 iWaitQueue.Append( httpfile ); |
|
461 } |
|
462 |
|
463 // -------------------------------------------------------------------------- |
|
464 // CHttpTransferBase::MoveToFileQueue() |
|
465 // (See comments in header file) |
|
466 // -------------------------------------------------------------------------- |
|
467 // |
|
468 EXPORT_C void CHttpTransferBase::MoveToTransferQueueL( TAny* aKey ) |
|
469 { |
|
470 TBool keyExists = EFalse; |
|
471 CHttpFile* waitFile = NULL; |
|
472 |
|
473 for ( TInt i = 0; i < iWaitQueue.Count(); i++ ) |
|
474 { |
|
475 if ( iWaitQueue[i]->Key() == aKey ) |
|
476 { |
|
477 keyExists = ETrue; |
|
478 |
|
479 // Removes the file from iWaitQueue |
|
480 waitFile = iWaitQueue[i]; |
|
481 |
|
482 // Set the queue |
|
483 iWaitQueue.Remove(i); |
|
484 iWaitQueue.Compress(); |
|
485 } |
|
486 } |
|
487 |
|
488 // If the key does not exist -> leaves |
|
489 if ( !keyExists ) |
|
490 { |
|
491 User::Leave( KErrNotFound ); |
|
492 } |
|
493 |
|
494 // Appends the file to iFileQueue |
|
495 iFileQueue.Append( waitFile ); |
|
496 |
|
497 // start the transfer |
|
498 ActivateNewTransferRequest(); |
|
499 } |
|
500 |
|
501 // -------------------------------------------------------------------------- |
|
502 // CHttpTransferBase::ActivateNewTransferRequest() |
|
503 // (See comments in header file) |
|
504 // -------------------------------------------------------------------------- |
|
505 // |
|
506 void CHttpTransferBase::ActivateNewTransferRequest() |
|
507 { |
|
508 if ( !IsActive() ) |
|
509 { |
|
510 SetActive(); |
|
511 TRequestStatus* pStatus = &iStatus; |
|
512 User::RequestComplete( pStatus, ETransferNextFile ); |
|
513 } |
|
514 } |
|
515 |
|
516 // -------------------------------------------------------------------------- |
|
517 // CHttpTransferBase::ProcessNextFileL() |
|
518 // (See comments in header file) |
|
519 // -------------------------------------------------------------------------- |
|
520 // |
|
521 void CHttpTransferBase::ProcessNextFileL() |
|
522 { |
|
523 // Check that there is workers available |
|
524 for ( TInt i = 0; i < iWorkerArray.Count(); i++ ) |
|
525 { |
|
526 if ( !iWorkerArray[i]->ProcessOnGoing() ) |
|
527 { |
|
528 CHttpFile* nextFile = NULL; |
|
529 |
|
530 nextFile = NextFile(); |
|
531 |
|
532 if ( nextFile ) |
|
533 { |
|
534 // sets the file and sets the state to EWaitingForStart |
|
535 iWorkerArray[i]->SetFileL( *nextFile ); |
|
536 |
|
537 // Set worker which is handled to pointer so that |
|
538 // if it leaves the worker can be terminated |
|
539 iActiveWorker = iWorkerArray[i]; |
|
540 |
|
541 iObserver->ReadyForTransferL( nextFile->Key() ); |
|
542 |
|
543 iActiveWorker = NULL; |
|
544 } |
|
545 } |
|
546 } |
|
547 } |
|
548 |
|
549 |
|
550 // From base class CActive |
|
551 |
|
552 // -------------------------------------------------------------------------- |
|
553 // CHttpTransferBase::DoCancel() |
|
554 // (See comments in header file) |
|
555 // -------------------------------------------------------------------------- |
|
556 // |
|
557 void CHttpTransferBase::DoCancel() |
|
558 { |
|
559 CancelAll(); |
|
560 } |
|
561 |
|
562 // -------------------------------------------------------------------------- |
|
563 // CHttpTransferBase::RunL() |
|
564 // (See comments in header file) |
|
565 // -------------------------------------------------------------------------- |
|
566 // |
|
567 void CHttpTransferBase::RunL() |
|
568 { |
|
569 ProcessNextFileL(); |
|
570 } |
|
571 |
|
572 // -------------------------------------------------------------------------- |
|
573 // CHttpTransferBase::RunError() |
|
574 // (See comments in header file) |
|
575 // -------------------------------------------------------------------------- |
|
576 // |
|
577 TInt CHttpTransferBase::RunError( TInt aError ) |
|
578 { |
|
579 // Terminate worker which leaved |
|
580 if ( iActiveWorker ) |
|
581 { |
|
582 // Cancels the transfer and |
|
583 // informs the client through TransferCompleted() |
|
584 iObserver->TransferCompleted( iActiveWorker->Key(), aError ); |
|
585 iActiveWorker->CancelTransfer(); |
|
586 iActiveWorker = NULL; |
|
587 } |
|
588 |
|
589 return KErrNone; |
|
590 } |
|
591 |
|
592 |
|
593 // From base class MHttpWorkerObserver |
|
594 |
|
595 // -------------------------------------------------------------------------- |
|
596 // CHttpTransferBase::ProcessNextFileL() |
|
597 // (See comments in header file) |
|
598 // -------------------------------------------------------------------------- |
|
599 // |
|
600 void CHttpTransferBase::WorkerCompleted() |
|
601 { |
|
602 ActivateNewTransferRequest(); |
|
603 } |
|
604 |
|
605 // end of file |