appinstaller/AppinstUi/Daemon/Src/previouslyInstalledAppsCache.cpp
changeset 0 ba25891c3a9e
child 25 98b66e4fb0be
child 65 7333d7932ef7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006-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 
       
    18 #include <f32file.h>
       
    19 #include <s32file.h>
       
    20 #include <pathinfo.h>
       
    21 
       
    22 #include "sisregistrysession.h"
       
    23 #include "sisregistryentry.h"
       
    24 #include "sisregistrypackage.h"
       
    25 #include "SWInstDebug.h"
       
    26 
       
    27 
       
    28 using namespace Swi;
       
    29 
       
    30 // @released
       
    31 // @publishedPartner
       
    32 
       
    33 #include "previouslyInstalledAppsCache.h"
       
    34 
       
    35 _LIT(KPreInstalledApps, "c:\\private\\10202DCE\\preInstalledAppsCache.dat");
       
    36 
       
    37 
       
    38 
       
    39 // ----------------------------
       
    40 // Borrowed from cleanuputils.h:-
       
    41 /** 
       
    42  * Template class CleanupResetAndDestroy to clean up the array
       
    43  * of implementation information from the cleanup stack.
       
    44  * @released
       
    45  * @publishedPartner
       
    46  *
       
    47  */
       
    48 
       
    49 template <class T>
       
    50 class CleanupResetAndDestroy
       
    51 	{
       
    52 public:
       
    53 	inline static void PushL(T& aRef);
       
    54 private:
       
    55 	static void ResetAndDestroy(TAny *aPtr);
       
    56 	};
       
    57 	
       
    58 template <class T>
       
    59 inline void CleanupResetAndDestroyPushL(T& aRef);
       
    60 
       
    61 template <class T>
       
    62 inline void CleanupResetAndDestroy<T>::PushL(T& aRef)
       
    63 	{
       
    64 	CleanupStack::PushL(TCleanupItem(&ResetAndDestroy,&aRef));
       
    65 	}
       
    66 
       
    67 template <class T>
       
    68 void CleanupResetAndDestroy<T>::ResetAndDestroy(TAny *aPtr)
       
    69 	{
       
    70 	static_cast<T*>(aPtr)->ResetAndDestroy();
       
    71 	}
       
    72 
       
    73 template <class T>
       
    74 inline void CleanupResetAndDestroyPushL(T& aRef)
       
    75 	{
       
    76 	CleanupResetAndDestroy<T>::PushL(aRef);
       
    77 	}
       
    78 
       
    79 // End of code from swi/inc/cleanuputils.h
       
    80 // ----------------------------
       
    81 
       
    82 
       
    83 CPreviouslyInstalledAppsCache *CPreviouslyInstalledAppsCache::NewL()
       
    84 	{
       
    85 	CPreviouslyInstalledAppsCache *self = new(ELeave)CPreviouslyInstalledAppsCache;
       
    86 	CleanupStack::PushL(self);
       
    87 	self->ConstructL();
       
    88 	CleanupStack::Pop(self);
       
    89 	return self;
       
    90 	}
       
    91 
       
    92 CPreviouslyInstalledAppsCache::~CPreviouslyInstalledAppsCache()
       
    93 	{
       
    94 	iPrevPkgUids.Reset();
       
    95 	}
       
    96 
       
    97 
       
    98 void CPreviouslyInstalledAppsCache::UpdateAllL()
       
    99 	{
       
   100 	RThread ourThread; // nb. Default constructor gives handle to current thread.
       
   101 	if(!ourThread.HasCapability(ECapabilityReadUserData, 0))
       
   102 		{
       
   103 		// Have not got ReadUserData - if we try and enumerate package
       
   104 		// UIDs the registry server will panic us!
       
   105 		User::Leave(KErrAccessDenied);
       
   106 		}
       
   107 	ourThread.Close();
       
   108 	
       
   109 
       
   110 	RSisRegistrySession registrySession;
       
   111 	User::LeaveIfError(registrySession.Connect());
       
   112 	CleanupClosePushL(registrySession);
       
   113 	
       
   114 	RPointerArray<CSisRegistryPackage> packages;
       
   115 	registrySession.InstalledPackagesL(packages);
       
   116    	CleanupResetAndDestroy<RPointerArray<CSisRegistryPackage> >::PushL(packages);
       
   117 
       
   118 	for (TInt i=0; i< packages.Count(); ++i)
       
   119 		{
       
   120 		(void)iPrevPkgUids.InsertInSignedKeyOrder(packages[i]->Uid());
       
   121 		}
       
   122 	
       
   123 	CleanupStack::PopAndDestroy(&packages);
       
   124 	CleanupStack::PopAndDestroy(&registrySession);
       
   125 	}
       
   126 
       
   127 void CPreviouslyInstalledAppsCache::UpdateAddL(TUid aUid)
       
   128 	{
       
   129 	RSisRegistrySession registrySession;
       
   130 	User::LeaveIfError(registrySession.Connect());
       
   131 	CleanupClosePushL(registrySession);
       
   132 	
       
   133 	RSisRegistryEntry registryEntry;
       
   134 	TInt err = registryEntry.Open(registrySession, aUid);
       
   135 	if( err == KErrNone )
       
   136 		{
       
   137 		registryEntry.Close();
       
   138 		(void)iPrevPkgUids.InsertInSignedKeyOrder(aUid);
       
   139 		}
       
   140 
       
   141 	FLOG_1( _L("Daemon: UpdateAddL: UID add error = %d"), err );
       
   142 	
       
   143 	CleanupStack::PopAndDestroy(&registrySession);
       
   144 	}
       
   145 
       
   146 TBool CPreviouslyInstalledAppsCache::HasBeenPreviouslyInstalled(TUid aPackageUid) const
       
   147 	{
       
   148 	if(iPrevPkgUids.FindInSignedKeyOrder(aPackageUid) == KErrNotFound)
       
   149 		{
       
   150 		return EFalse;
       
   151 		}
       
   152 	return ETrue;
       
   153 	}
       
   154 
       
   155 CPreviouslyInstalledAppsCache::CPreviouslyInstalledAppsCache()
       
   156 	{
       
   157 	}
       
   158 
       
   159 void CPreviouslyInstalledAppsCache::ConstructL()
       
   160 	{
       
   161 	User::LeaveIfError(iFs.Connect());
       
   162 	TInt drive = 0;
       
   163     iFs.CharToDrive( TParsePtrC( PathInfo::PhoneMemoryRootPath() ).Drive()[0], drive );
       
   164     iFs.CreatePrivatePath( drive );
       
   165 
       
   166 	// Read cache file
       
   167 	TRAP_IGNORE(InitFromCacheFileL());
       
   168 		
       
   169     TRAPD( err, UpdateAllL() );
       
   170     if(err == KErrNone)
       
   171         {
       
   172         // If we managed to scan the registry, and update the cache, flush to disk.
       
   173         TRAP_IGNORE(FlushToDiskL());
       
   174         }	
       
   175 
       
   176 	}
       
   177 
       
   178 
       
   179 void CPreviouslyInstalledAppsCache::InitFromCacheFileL()
       
   180 	{
       
   181 	// Read in existing cache file.
       
   182 	RFile cacheFile;
       
   183 	TInt err = cacheFile.Open(iFs, KPreInstalledApps, EFileStream|EFileRead);
       
   184 	if(err != KErrNone)
       
   185 		{
       
   186 			return; // No existing cache file to read.
       
   187 		}
       
   188 	CleanupClosePushL(cacheFile);
       
   189 	
       
   190 	// Now read the cache
       
   191 	RFileReadStream cacheReadStream(cacheFile);
       
   192 	cacheReadStream.PushL();
       
   193 
       
   194 	iPrevPkgUids.Reset();
       
   195 	TInt32 count(cacheReadStream.ReadInt32L());
       
   196 
       
   197 	for (TInt i = 0; i < count; i++)
       
   198 		{
       
   199 		TUid packageId;
       
   200 		packageId.iUid = cacheReadStream.ReadInt32L();
       
   201 		(void)iPrevPkgUids.InsertInSignedKeyOrder(packageId);
       
   202 		}	
       
   203 	
       
   204 	CleanupStack::PopAndDestroy(&cacheReadStream);
       
   205 	CleanupStack::PopAndDestroy(&cacheFile);
       
   206 }
       
   207 
       
   208 void CPreviouslyInstalledAppsCache::FlushToDiskL()
       
   209 	{
       
   210 	// Write to disk
       
   211 	RFile cacheFile;
       
   212 	TInt err = cacheFile.Open(iFs, KPreInstalledApps, EFileStream|EFileWrite);
       
   213 	if(err != KErrNone)
       
   214 		{
       
   215 			User::LeaveIfError(cacheFile.Create(iFs, KPreInstalledApps, EFileStream|EFileWrite));
       
   216 		}
       
   217 	CleanupClosePushL(cacheFile);
       
   218 
       
   219 	// Truncate file.
       
   220 	User::LeaveIfError(cacheFile.SetSize(0));
       
   221 	
       
   222 	// Now write the cache
       
   223 	RFileWriteStream cacheWriteStream(cacheFile);
       
   224 	cacheWriteStream.PushL();
       
   225 
       
   226 	TInt32 count(iPrevPkgUids.Count());
       
   227 	cacheWriteStream.WriteInt32L(count);
       
   228 
       
   229 	for (TInt i = 0; i < count; i++)
       
   230 		{
       
   231 		cacheWriteStream.WriteInt32L(iPrevPkgUids[i].iUid);
       
   232 		}	
       
   233 	
       
   234 	cacheWriteStream.CommitL();
       
   235 	CleanupStack::PopAndDestroy(&cacheWriteStream);
       
   236 	CleanupStack::PopAndDestroy(&cacheFile);
       
   237 	}
       
   238 
       
   239 // End of file