1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/contentmgmt/referencedrmagent/contentiterator/contentiterator.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,188 @@
1.4 +/*
1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +
1.23 +#include "cafpanic.h"
1.24 +#include "contentIterator.h"
1.25 +#include "FileContentIterator.h"
1.26 +#include "contentiteratordata.h"
1.27 +
1.28 +using ContentAccess::CContentIterator;
1.29 +using ContentAccess::TVirtualPathPtr;
1.30 +
1.31 +_LIT(KIteratorThread,"ContentIterator");
1.32 +EXPORT_C CContentIterator* CContentIterator::NewL(const TDesC& aPath, TBool aRecursive, const TDesC8& aMimeType)
1.33 + {
1.34 + CContentIterator* self = new (ELeave) CContentIterator();
1.35 + CleanupStack::PushL(self);
1.36 + self->ConstructL(aPath, aRecursive, aMimeType);
1.37 + CleanupStack::Pop();
1.38 + return self;
1.39 + }
1.40 +
1.41 +CContentIterator::CContentIterator() : CActive(EPriorityStandard)
1.42 + {
1.43 + }
1.44 +
1.45 +CContentIterator::~CContentIterator()
1.46 + {
1.47 + // tell thread to cancel and shutdown
1.48 + Cancel();
1.49 +
1.50 + // close thread handle
1.51 + iWorkerThread.Close();
1.52 + delete info;
1.53 + }
1.54 +
1.55 +void CContentIterator::ConstructL(const TDesC& aPath, TBool aRecursive, const TDesC8& aMimeType)
1.56 + {
1.57 + // This data buffer will be shared between the client and the worker thread
1.58 + info = CContentIteratorData::NewL(aPath,aRecursive,aMimeType);
1.59 +
1.60 + // create the thread, need a big heap and stack for recursively searching through directories
1.61 + User::LeaveIfError(iWorkerThread.Create(KIteratorThread(),CContentIterator::ThreadEntry,32768, KMinHeapSize, 131072, (void *) info , EOwnerProcess));
1.62 +
1.63 + // add ourselves to active scheduler
1.64 + CActiveScheduler::Add(this);
1.65 +
1.66 + // Set up notification in case the thread panics etc
1.67 + iStatus = KRequestPending;
1.68 + iWorkerThread.Logon(iStatus);
1.69 + SetActive();
1.70 +
1.71 + // start the thread
1.72 + iWorkerThread.Resume();
1.73 + }
1.74 +
1.75 +
1.76 +void CContentIterator::DoCancel()
1.77 + {
1.78 + // Wait until thread finishes whatever it's doing
1.79 + info->Lock();
1.80 +
1.81 + // Signal for the thread to close
1.82 + info->RunThreadFunction(EIteratorShutdownThread);
1.83 + }
1.84 +
1.85 +void CContentIterator::RunL()
1.86 + {
1.87 + // Thread must have completed
1.88 + if(iWorkerThread.ExitType() == EExitPanic)
1.89 + {
1.90 + // Thread panicd, better panic our thread
1.91 + User::Panic(KCafPanicString, ECafPanicContentIteratorThreadPanic);
1.92 + }
1.93 + }
1.94 +
1.95 +EXPORT_C TVirtualPathPtr CContentIterator::VirtualPath()
1.96 + {
1.97 + // Wait until thread finishes whatever it's doing
1.98 + info->Lock();
1.99 + iFileName.Copy(info->Path());
1.100 + iUniqueId.Copy(info->UniqueId());
1.101 + info->Unlock();
1.102 + return TVirtualPathPtr(iFileName, iUniqueId);
1.103 + }
1.104 +
1.105 +EXPORT_C const TDesC& CContentIterator::Name()
1.106 + {
1.107 + // Wait until thread finishes whatever it's doing
1.108 + info->Lock();
1.109 + iName.Copy(info->Name());
1.110 + info->Unlock();
1.111 + return iName;
1.112 + }
1.113 +
1.114 +EXPORT_C const TDesC8& CContentIterator::MimeType()
1.115 + {
1.116 + // Wait until thread finishes whatever it's doing
1.117 + info->Lock();
1.118 + iMimeType.Copy(info->MimeType());
1.119 + info->Unlock();
1.120 + return iMimeType;
1.121 + }
1.122 +
1.123 +EXPORT_C void CContentIterator::Next(TRequestStatus &aStatus)
1.124 + {
1.125 + // Wait until thread finishes whatever it's doing
1.126 + info->Lock();
1.127 +
1.128 + // Remember which thread and TRequestStatus to notify
1.129 + TThreadId id = RThread().Id();
1.130 + info->SetClientRequest(id, aStatus);
1.131 +
1.132 + // Tell it to find the next iteration
1.133 + info->RunThreadFunction(EIteratorFindNextContentObject);
1.134 + }
1.135 +
1.136 +
1.137 +TInt CContentIterator::ThreadEntry(TAny* aAny)
1.138 + {
1.139 + TBool exitNow = EFalse;
1.140 + TInt err;
1.141 + CFileContentIterator* iterator = NULL;
1.142 + RThread clientThread;
1.143 + CContentIteratorData* info = reinterpret_cast <CContentIteratorData *> (aAny);
1.144 +
1.145 + // create a trap handler
1.146 + CTrapCleanup* cleanup = CTrapCleanup::New();
1.147 +
1.148 + while(!exitNow)
1.149 + {
1.150 + // Thread will wait here until signaled by other CContentIterator functions
1.151 + switch(info->ThreadWait())
1.152 + {
1.153 + // Client thread has asked us to shutdown, exit loop
1.154 + case EIteratorShutdownThread:
1.155 + exitNow = ETrue;
1.156 + break;
1.157 + // Client thread is asking us to find the next object
1.158 + case EIteratorFindNextContentObject:
1.159 + if(!iterator)
1.160 + {
1.161 + TRAP(err, iterator = CFileContentIterator::NewL(info->Path(), info->IsRecursive(), info->MimeType()));
1.162 + info->SetData(KNullDesC(), KNullDesC(), KNullDesC(), KNullDesC8());
1.163 + }
1.164 + else
1.165 + {
1.166 + err = iterator->Next();
1.167 + }
1.168 +
1.169 + if(err == KErrNone)
1.170 + {
1.171 + // If a content object was found, write the value back
1.172 + // into the other thread while the info is still locked
1.173 + info->SetData(iterator->FileName(),iterator->UniqueId(), iterator->Name(), iterator->MimeType());
1.174 + }
1.175 +
1.176 + // Complete the clients asynchronous request
1.177 + info->CompleteClientRequest(err);
1.178 + break;
1.179 + default:
1.180 + User::Panic(KCafPanicString, ECafPanicContentIteratorUnknownRequest);
1.181 + break;
1.182 + };
1.183 +
1.184 + // Allow the client to post a new request
1.185 + info->Unlock();
1.186 + }
1.187 + delete iterator;
1.188 + delete cleanup;
1.189 + return KErrNone;
1.190 + }
1.191 +