os/security/contentmgmt/referencedrmagent/contentiterator/contentiterator.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
sl@0
    20
#include "cafpanic.h"
sl@0
    21
#include "contentIterator.h"
sl@0
    22
#include "FileContentIterator.h"
sl@0
    23
#include "contentiteratordata.h"
sl@0
    24
sl@0
    25
using ContentAccess::CContentIterator;
sl@0
    26
using ContentAccess::TVirtualPathPtr;
sl@0
    27
sl@0
    28
_LIT(KIteratorThread,"ContentIterator");
sl@0
    29
EXPORT_C CContentIterator* CContentIterator::NewL(const TDesC& aPath, TBool aRecursive, const TDesC8& aMimeType) 
sl@0
    30
	{  
sl@0
    31
	CContentIterator* self = new (ELeave) CContentIterator();
sl@0
    32
	CleanupStack::PushL(self);
sl@0
    33
	self->ConstructL(aPath, aRecursive, aMimeType);
sl@0
    34
	CleanupStack::Pop();
sl@0
    35
	return self;
sl@0
    36
	}
sl@0
    37
sl@0
    38
CContentIterator::CContentIterator() : CActive(EPriorityStandard)
sl@0
    39
	{
sl@0
    40
	}
sl@0
    41
sl@0
    42
CContentIterator::~CContentIterator()
sl@0
    43
	{
sl@0
    44
	// tell thread to cancel and shutdown
sl@0
    45
	Cancel();
sl@0
    46
	
sl@0
    47
	// close thread handle
sl@0
    48
	iWorkerThread.Close();
sl@0
    49
	delete info; 
sl@0
    50
	}
sl@0
    51
sl@0
    52
void CContentIterator::ConstructL(const TDesC& aPath, TBool aRecursive, const TDesC8& aMimeType)
sl@0
    53
	{	
sl@0
    54
	// This data buffer will be shared between the client and the worker thread
sl@0
    55
	info = CContentIteratorData::NewL(aPath,aRecursive,aMimeType);
sl@0
    56
	
sl@0
    57
	// create the thread, need a big heap and stack for recursively searching through directories
sl@0
    58
	User::LeaveIfError(iWorkerThread.Create(KIteratorThread(),CContentIterator::ThreadEntry,32768, KMinHeapSize, 131072, (void *) info , EOwnerProcess));
sl@0
    59
sl@0
    60
	// add ourselves to active scheduler
sl@0
    61
	CActiveScheduler::Add(this);
sl@0
    62
sl@0
    63
	// Set up notification in case the thread panics etc
sl@0
    64
	iStatus = KRequestPending;
sl@0
    65
	iWorkerThread.Logon(iStatus);
sl@0
    66
	SetActive();
sl@0
    67
	
sl@0
    68
	// start the thread
sl@0
    69
	iWorkerThread.Resume();
sl@0
    70
	}
sl@0
    71
sl@0
    72
sl@0
    73
void CContentIterator::DoCancel()
sl@0
    74
	{
sl@0
    75
	// Wait until thread finishes whatever it's doing
sl@0
    76
	info->Lock();
sl@0
    77
	 
sl@0
    78
	// Signal for the thread to close
sl@0
    79
	info->RunThreadFunction(EIteratorShutdownThread);
sl@0
    80
	}
sl@0
    81
sl@0
    82
void CContentIterator::RunL()
sl@0
    83
	{
sl@0
    84
	// Thread must have completed
sl@0
    85
	if(iWorkerThread.ExitType() == EExitPanic)
sl@0
    86
		{
sl@0
    87
		// Thread panicd, better panic our thread
sl@0
    88
		User::Panic(KCafPanicString, ECafPanicContentIteratorThreadPanic);
sl@0
    89
		}
sl@0
    90
	}
sl@0
    91
sl@0
    92
EXPORT_C TVirtualPathPtr CContentIterator::VirtualPath()
sl@0
    93
	{
sl@0
    94
	// Wait until thread finishes whatever it's doing
sl@0
    95
	info->Lock(); 
sl@0
    96
	iFileName.Copy(info->Path());	
sl@0
    97
	iUniqueId.Copy(info->UniqueId());	
sl@0
    98
	info->Unlock(); 
sl@0
    99
	return TVirtualPathPtr(iFileName, iUniqueId);
sl@0
   100
	}
sl@0
   101
sl@0
   102
EXPORT_C const TDesC&  CContentIterator::Name()
sl@0
   103
	{
sl@0
   104
	// Wait until thread finishes whatever it's doing
sl@0
   105
	info->Lock(); 
sl@0
   106
	iName.Copy(info->Name());	
sl@0
   107
	info->Unlock(); 
sl@0
   108
	return iName;
sl@0
   109
	}
sl@0
   110
sl@0
   111
EXPORT_C const TDesC8& CContentIterator::MimeType()
sl@0
   112
	{
sl@0
   113
	// Wait until thread finishes whatever it's doing
sl@0
   114
	info->Lock(); 
sl@0
   115
	iMimeType.Copy(info->MimeType());
sl@0
   116
	info->Unlock(); 
sl@0
   117
	return iMimeType;
sl@0
   118
	}
sl@0
   119
sl@0
   120
EXPORT_C void CContentIterator::Next(TRequestStatus &aStatus)
sl@0
   121
	{
sl@0
   122
	// Wait until thread finishes whatever it's doing
sl@0
   123
	info->Lock(); 
sl@0
   124
sl@0
   125
	// Remember which thread and TRequestStatus to notify
sl@0
   126
	TThreadId id = RThread().Id();
sl@0
   127
	info->SetClientRequest(id, aStatus);
sl@0
   128
	
sl@0
   129
	// Tell it to find the next iteration
sl@0
   130
	info->RunThreadFunction(EIteratorFindNextContentObject); 
sl@0
   131
	}
sl@0
   132
	
sl@0
   133
sl@0
   134
TInt CContentIterator::ThreadEntry(TAny* aAny) 
sl@0
   135
	{
sl@0
   136
	TBool exitNow = EFalse;
sl@0
   137
	TInt err;
sl@0
   138
	CFileContentIterator* iterator = NULL;
sl@0
   139
	RThread clientThread;
sl@0
   140
	CContentIteratorData* info = reinterpret_cast <CContentIteratorData *> (aAny);
sl@0
   141
	
sl@0
   142
	// create a trap handler
sl@0
   143
	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0
   144
	
sl@0
   145
	while(!exitNow)
sl@0
   146
		{
sl@0
   147
		// Thread will wait here until signaled by other CContentIterator functions
sl@0
   148
		switch(info->ThreadWait())
sl@0
   149
			{
sl@0
   150
		// Client thread has asked us to shutdown, exit loop
sl@0
   151
		case EIteratorShutdownThread:
sl@0
   152
			exitNow = ETrue;
sl@0
   153
			break;
sl@0
   154
		// Client thread is asking us to find the next object	
sl@0
   155
		case EIteratorFindNextContentObject:
sl@0
   156
			if(!iterator)
sl@0
   157
				{
sl@0
   158
				TRAP(err, iterator = CFileContentIterator::NewL(info->Path(), info->IsRecursive(), info->MimeType()));
sl@0
   159
				info->SetData(KNullDesC(), KNullDesC(), KNullDesC(), KNullDesC8());
sl@0
   160
				}
sl@0
   161
			else
sl@0
   162
				{
sl@0
   163
				err = iterator->Next();
sl@0
   164
				}
sl@0
   165
	
sl@0
   166
			if(err == KErrNone)
sl@0
   167
				{
sl@0
   168
				// If a content object was found, write the value back 
sl@0
   169
				// into the other thread while the info is still locked
sl@0
   170
				info->SetData(iterator->FileName(),iterator->UniqueId(), iterator->Name(), iterator->MimeType());
sl@0
   171
				}
sl@0
   172
sl@0
   173
			// Complete the clients asynchronous request
sl@0
   174
			info->CompleteClientRequest(err);
sl@0
   175
			break;
sl@0
   176
		default:
sl@0
   177
			User::Panic(KCafPanicString, ECafPanicContentIteratorUnknownRequest);
sl@0
   178
			break;
sl@0
   179
			};
sl@0
   180
sl@0
   181
		// Allow the client to post a new request
sl@0
   182
		info->Unlock(); 
sl@0
   183
		}
sl@0
   184
	delete iterator;
sl@0
   185
	delete cleanup;
sl@0
   186
	return KErrNone;
sl@0
   187
	}
sl@0
   188