contextframework/cfw/src/cfscriptengine/CFScriptHandler.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2002-2008 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:  The main interface class providing services of ScriptEngine module
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32math.h>
       
    20 #include <e32svr.h>
       
    21 #include <s32mem.h>
       
    22 #include <bautils.h>
       
    23 #include <s32file.h>
       
    24 #include <centralrepository.h>
       
    25 #include <gmxmldocument.h>
       
    26 #include <gmxmlelement.h>
       
    27 
       
    28 #ifdef RD_MULTIPLE_DRIVE
       
    29 #include <driveinfo.h>
       
    30 #endif
       
    31 
       
    32 #include "CFScriptHandler.h"
       
    33 #include "CFScript.h"
       
    34 #include "ContextFrameworkPrivateCRKeys.h"
       
    35 #include "cftrace.h"
       
    36 #include "cfextendedcontextinterface.h"
       
    37 #include "cfoperationpluginmanager.h"
       
    38 #include "cfpersistentdata.h"
       
    39 #include "cfcommon.h"
       
    40 #include "cfscriptowner.h"
       
    41 
       
    42 // CONSTANTS
       
    43 
       
    44 #ifdef USE_TEMP_RULE_FOLDER
       
    45     _LIT( KDefaultRuleFilePath, "c:\\Silmaril\\Rules\\" );
       
    46 #else
       
    47     _LIT( KDefaultRuleFilePath, "!:\\private\\10282BC4\\Rules\\" );
       
    48 #endif
       
    49 
       
    50 // Default script search pattern
       
    51 _LIT( KDefaultRuleFileSearchPattern, "*.rul" );
       
    52 _LIT( KWildCardChar, "*" );
       
    53 
       
    54 const TInt KInitialScriptId = 1;
       
    55 const TInt KUidLength = 8;
       
    56 const TInt KTempScriptId = 0;
       
    57 
       
    58 const TUint KLimit = 0xFFFFFFFF;
       
    59 
       
    60 // ============================ MEMBER FUNCTIONS ===============================
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CCFScriptHandler::CCFScriptHandler
       
    64 // C++ default constructor can NOT contain any code, that might leave.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CCFScriptHandler::CCFScriptHandler(
       
    68 	MCFExtendedContextInterface& aCF,
       
    69     RFs& aFs,
       
    70     MCFActionHandler& aScriptEventListener,
       
    71     MCFSecurityChecker& aSecurityChecker )
       
    72     :   iParserDataProvidingState( KError ),
       
    73         iNextId( KInitialScriptId ),
       
    74         iCF( aCF ),
       
    75         iFs( aFs ),
       
    76         iScriptEventListener( aScriptEventListener ),
       
    77         iSecurityChecker( aSecurityChecker )
       
    78     {
       
    79     FUNC_LOG;
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CCFScriptHandler::ConstructL
       
    84 // Symbian 2nd phase constructor can leave.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CCFScriptHandler::ConstructL()
       
    88     {
       
    89     FUNC_LOG;
       
    90     
       
    91     iParser = CMDXMLParser::NewL( this );
       
    92     iWaitParsing = new( ELeave ) CActiveSchedulerWait;
       
    93     iOperationPluginManager = CCFOperationPluginManager::NewL( *this );
       
    94 
       
    95 #ifdef RD_MULTIPLE_DRIVE
       
    96     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
    97         DriveInfo::EDefaultRom, iDefaultRomDrive ) );
       
    98     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
    99         DriveInfo::EDefaultSystem, iDefaultSystemDrive ) );
       
   100 #else
       
   101     iDefaultRomDrive = 'Z';
       
   102     iDefaultSystemDrive = 'C';
       
   103 #endif
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CCFScriptHandler::NewL
       
   108 // Two-phased constructor.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CCFScriptHandler* CCFScriptHandler::NewL(
       
   112 	MCFExtendedContextInterface& aCF,
       
   113     RFs& aFs,
       
   114     MCFActionHandler& aScriptEventListener,
       
   115     MCFSecurityChecker& aSecurityChecker )
       
   116     {
       
   117     FUNC_LOG;
       
   118 
       
   119     CCFScriptHandler* self = NewLC( aCF, aFs, aScriptEventListener,
       
   120     	aSecurityChecker );
       
   121     CleanupStack::Pop( self );
       
   122     return self;
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CCFScriptHandler::NewLC
       
   127 // Two-phased constructor.
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C CCFScriptHandler* CCFScriptHandler::NewLC(
       
   131 	MCFExtendedContextInterface& aCF,
       
   132     RFs& aFs,
       
   133     MCFActionHandler& aScriptEventListener,
       
   134     MCFSecurityChecker& aSecurityChecker )
       
   135     {
       
   136     FUNC_LOG;
       
   137 
       
   138     CCFScriptHandler* self =
       
   139         new( ELeave ) CCFScriptHandler( aCF, aFs, aScriptEventListener,
       
   140         aSecurityChecker );
       
   141     CleanupStack::PushL( self );
       
   142     self->ConstructL();
       
   143     return self;
       
   144     }
       
   145 
       
   146 // Destructor
       
   147 EXPORT_C CCFScriptHandler::~CCFScriptHandler()
       
   148     {
       
   149     FUNC_LOG;
       
   150 
       
   151     iRollbackList.ResetAndDestroy();
       
   152     iLoadedScripts.Close();
       
   153     iScripts.ResetAndDestroy();
       
   154     iScriptIds.Close();
       
   155     delete iParser;
       
   156     delete iWaitParsing;
       
   157     delete iOperationPluginManager;
       
   158     }
       
   159 
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CCFScriptHandler::AddScript
       
   163 // Adds a new script. Calls AddScriptL that does the actual work.
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 TInt CCFScriptHandler::AddScript( const TDesC& aName,
       
   167     const TDesC8& aScript,
       
   168     const TUid& aOwner,
       
   169     const RThread& aOwnerThread,
       
   170     MCFActionHandler& aActionHandler,
       
   171     MCFScriptOwner* aScriptOwner )
       
   172     {
       
   173     FUNC_LOG;
       
   174     
       
   175     TInt err = KErrNone;
       
   176 
       
   177     // Check that script with the name does not exist - notice script object
       
   178     // ownership is not transfered
       
   179     RPointerArray< CCFScript > scripts;
       
   180     GetScriptsByUid( aOwner, scripts );
       
   181     for( TInt i = 0; i < scripts.Count(); ++i )
       
   182         {
       
   183         // Compare names only if we are not updating existing script.
       
   184         if ( scripts[ i ]->ScriptId() != iNextId )
       
   185             {
       
   186             TPtrC scriptName( scripts[ i ]->Name() );
       
   187             if( scriptName.CompareF( aName ) == KErrNone )
       
   188                 {
       
   189                 err = KErrAlreadyExists;
       
   190                 ERROR_2( err, "Script name %S already in use in client 0x%x",
       
   191                     &aName, aOwner );
       
   192                 break;
       
   193                 }
       
   194             }
       
   195         }
       
   196     scripts.Close();
       
   197 
       
   198     // Add script
       
   199     TInt ret = err;
       
   200     if( err == KErrNone )
       
   201         {
       
   202         TRAP( err, ret = AddScriptL( aName,
       
   203             aScript,
       
   204             aOwner,
       
   205             aOwnerThread,
       
   206             aActionHandler,
       
   207             ETrue,
       
   208             aScriptOwner ) );
       
   209         if ( err != KErrNone )
       
   210             {
       
   211             ERROR( ret, "Unable to add script" );
       
   212             ret = err;
       
   213             }
       
   214         }
       
   215 
       
   216     // return script Id ( >0 ) or error code if error ( <0 )
       
   217     return ret;
       
   218     }
       
   219 
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CCFScriptHandler::NumberOfScriptsByOwner
       
   223 //
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 TInt CCFScriptHandler::NumberOfScriptsByOwner( const TUid& aOwner )
       
   227     {
       
   228     FUNC_LOG;
       
   229 
       
   230     TInt result( KErrNone );
       
   231 
       
   232     // iterate thru all the existing scripts
       
   233     for ( TInt i = 0; i < iScripts.Count(); i++  )
       
   234         {
       
   235         // check if the owner Uid matches
       
   236         if ( iScripts[ i ]->OwnerUid() == aOwner )
       
   237             {
       
   238             // increment script count
       
   239             result++;
       
   240             }
       
   241         }
       
   242 
       
   243     return result;
       
   244     }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CCFScriptHandler::NumberOfScripts
       
   248 //
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 TInt CCFScriptHandler::NumberOfScripts()
       
   252     {
       
   253     FUNC_LOG;
       
   254 
       
   255     return iScripts.Count();
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CCFScriptHandler::ScriptLength
       
   260 //
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 TInt CCFScriptHandler::ScriptLength( TInt aScriptId )
       
   264     {
       
   265     FUNC_LOG;
       
   266 
       
   267     TInt result ( KErrNotFound );
       
   268 
       
   269     // iterate thru all the existing scripts
       
   270     for ( TInt i = 0; i < iScripts.Count(); i++  )
       
   271         {
       
   272         // check if the script id matches
       
   273         if ( iScripts[ i ]->ScriptId() == aScriptId )
       
   274             {
       
   275             // get the descriptor length of the script
       
   276             result = iScripts[ i ]->Length();
       
   277             }
       
   278         }
       
   279     return result;
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CCFScriptHandler::GetEveryScriptId
       
   284 //
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 const RArray< TInt >& CCFScriptHandler::GetEveryScriptId()
       
   288     {
       
   289     FUNC_LOG;
       
   290 
       
   291     // local variable declarations
       
   292     TInt err( KErrNone );
       
   293 
       
   294     // reset array
       
   295     iScriptIds.Reset();
       
   296 
       
   297     // go thru all the scripts
       
   298     for ( TInt i = 0; i < iScripts.Count(); i++ )
       
   299         {
       
   300         // add Id to result array
       
   301         err = iScriptIds.Append( iScripts[ i ]->ScriptId() );
       
   302         // handle error
       
   303         if ( err != KErrNone )
       
   304             {
       
   305             // return what we have created so far
       
   306             return iScriptIds;
       
   307             }
       
   308         }
       
   309     return iScriptIds;
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CCFScriptHandler::GetEveryScriptIdByOwner
       
   314 //
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 const RArray< TInt >& CCFScriptHandler::GetEveryScriptIdByOwner( 
       
   318     const TUid& aScriptOwner )
       
   319     {
       
   320     FUNC_LOG;
       
   321 
       
   322     // local variable declarations
       
   323     TInt err( KErrNone );
       
   324 
       
   325     // reset array
       
   326     iScriptIds.Reset();
       
   327 
       
   328     // go thru all the scripts
       
   329     for ( TInt i = 0; i < iScripts.Count(); i++ )
       
   330         {
       
   331         // if the owner Uid matches
       
   332         if ( iScripts[ i ]->OwnerUid() == aScriptOwner )
       
   333             {
       
   334             // add Id to result array
       
   335             err = iScriptIds.Append( iScripts[ i ]->ScriptId() );
       
   336             // handle error
       
   337             if ( err != KErrNone )
       
   338                 {
       
   339                 // return what we have created so far
       
   340                 return iScriptIds;
       
   341                 }
       
   342             }
       
   343         }
       
   344     return iScriptIds;
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CCFScriptHandler::UpdateScript
       
   349 //
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 TInt CCFScriptHandler::UpdateScript( TInt aScriptID, 
       
   353 	const RThread& aOwnerThread,
       
   354     const TDesC8& aUpdatedScript,
       
   355     MCFScriptOwner* aScriptOwner )
       
   356     {
       
   357     FUNC_LOG;
       
   358 
       
   359     // local variable declaration & initialization
       
   360     TInt err( KErrNotFound );
       
   361     TInt index = iScripts.Count() - 1;
       
   362     TBool found( EFalse );
       
   363 
       
   364     // search for the script
       
   365     while ( !found &&    // script not found yet
       
   366             index >= 0 ) // still scripts left
       
   367         {
       
   368         if ( iScripts[ index ]->ScriptId() == aScriptID )
       
   369             {
       
   370             found = ETrue;
       
   371             err = KErrNone;
       
   372             break; // skip to not found
       
   373             }
       
   374         index--;
       
   375         }
       
   376     // not found
       
   377     if ( !found )
       
   378         {
       
   379         // return immediately with error code
       
   380         return err;
       
   381         }
       
   382     // script found
       
   383     else
       
   384         {
       
   385         // store session and owner Uid temporarily
       
   386         MCFActionHandler& scriptSession 
       
   387             = ( iScripts[ index ]->ActionHandler() );
       
   388 
       
   389         // handle Script Ids correctly
       
   390         TInt nextAvailableScriptId = iNextId;
       
   391         iNextId = iScripts[ index ]->ScriptId();
       
   392 
       
   393         TUid scriptOwner = iScripts[ index ]->OwnerUid();
       
   394 
       
   395         // remove found script from array and delete it
       
   396         CCFScript* outdatedScript = iScripts [ index ];
       
   397 
       
   398         // add the new script; note that the existing script
       
   399         // file in filestore is removed automatically
       
   400         iUpdatedScript = outdatedScript;
       
   401         err = AddScript( outdatedScript->Name(),
       
   402             aUpdatedScript,
       
   403         	scriptOwner,
       
   404         	aOwnerThread,
       
   405         	scriptSession,
       
   406             aScriptOwner );
       
   407         iUpdatedScript = NULL;
       
   408         
       
   409         // remove the outdated script only if the new one is succesully added.
       
   410         if( err > 0 )
       
   411             {
       
   412             iScripts.Remove( index );
       
   413             delete outdatedScript;
       
   414             }
       
   415         // restore Next available Script Id
       
   416         iNextId = nextAvailableScriptId;
       
   417         }
       
   418     // script added successfully
       
   419     if ( err > 0 )
       
   420         {
       
   421         err = KErrNone;
       
   422         }
       
   423 
       
   424     return err;
       
   425     }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 // CCFScriptHandler::SaveScript
       
   429 // -----------------------------------------------------------------------------
       
   430 //
       
   431 TInt CCFScriptHandler::SaveScript( const TDesC8& aScript,
       
   432     TInt aScriptId,
       
   433     const TUid& aOwnerUid )
       
   434     {
       
   435     FUNC_LOG;
       
   436     
       
   437     TInt err = KErrNotFound;
       
   438     for( TInt i = 0; i < iScripts.Count(); i++ )
       
   439         {
       
   440         CCFScript* script = iScripts[i];
       
   441         // Check script ID
       
   442         if( script->ScriptId() == aScriptId )
       
   443             {
       
   444             // Check script owner
       
   445             if( script->OwnerUid() == aOwnerUid ||
       
   446                 KCFServerSid == aOwnerUid )
       
   447                 {
       
   448                 // found matching uid - construct file path
       
   449                 HBufC* name = ScriptFilePath( script->Info() );
       
   450                 if( name )
       
   451                     {
       
   452                     err = DoSave( *name, aScript );
       
   453                     }
       
   454                 else
       
   455                     {
       
   456                     err = KErrNoMemory;
       
   457                     }
       
   458                 delete name;
       
   459                 name = NULL;
       
   460                 break;
       
   461                 }
       
   462             else
       
   463                 {
       
   464                 err = KErrAccessDenied;
       
   465                 }
       
   466             }
       
   467         }
       
   468     
       
   469     return err;
       
   470     }
       
   471 
       
   472 // -----------------------------------------------------------------------------
       
   473 // CCFScriptHandler::DeleteScriptByName
       
   474 // -----------------------------------------------------------------------------
       
   475 //
       
   476 TInt CCFScriptHandler::DeleteScriptByName( const TDesC& aScriptName,
       
   477     const TUid& aOwnerUid )
       
   478     {
       
   479     FUNC_LOG;
       
   480     
       
   481     TInt err = KErrNotFound;
       
   482 
       
   483     // Get script name
       
   484     TParsePtrC parse( aScriptName );
       
   485     TPtrC scriptName( parse.Name() );
       
   486     
       
   487     // Get scripts added by the owner
       
   488     RPointerArray<CCFScript> scripts;
       
   489     GetScriptsByUid( aOwnerUid, scripts );
       
   490     
       
   491     // If the script being deleted has already been deregistered, try to resolve
       
   492     // the script path from the script name and owner uid
       
   493     if( scripts.Count() )
       
   494         {
       
   495         for( TInt i = 0; i < scripts.Count(); i++ )
       
   496             {
       
   497             CCFScript* script = scripts[i];
       
   498             if( script->Name().CompareF( scriptName ) == KErrNone )
       
   499                 {
       
   500                 // correct owner uid - delete script
       
   501                 HBufC* name = ScriptFilePath( script->Info() );
       
   502                 if( name )
       
   503                     {
       
   504                     // Delete script file
       
   505                     TPtrC namePtrC( *name );
       
   506                     err = DeleteScriptFile( namePtrC, aOwnerUid );
       
   507                     }
       
   508                 else
       
   509                     {
       
   510                     err = KErrNoMemory;
       
   511                     }
       
   512                 
       
   513                 delete name;
       
   514                 name = NULL;
       
   515                 break;
       
   516                 }
       
   517             }
       
   518         }
       
   519     
       
   520     // Check manually from the system drive if the script was not found
       
   521     if( err == KErrNotFound )
       
   522         {
       
   523         // Resolve script name from owner uid and script name
       
   524         HBufC* name = ScriptFilePath( scriptName, aOwnerUid );
       
   525         if( name )
       
   526             {
       
   527             // Delete script file
       
   528             TPtrC namePtrC( *name );
       
   529             err = DeleteScriptFile( namePtrC, aOwnerUid );
       
   530             if( err == KErrNone )
       
   531                 {
       
   532                 // Transfer err as KErrDeregisterNotNeeded since the script
       
   533                 // has already been deregistered
       
   534                 err = KErrDeregisterNotNeeded;
       
   535                 }
       
   536             
       
   537             // Clean up
       
   538             delete name;
       
   539             name = NULL;
       
   540             }
       
   541         }
       
   542     
       
   543     // Clean up
       
   544     scripts.Close();
       
   545     
       
   546     return err;
       
   547     }
       
   548 
       
   549 // -----------------------------------------------------------------------------
       
   550 // CCFScriptHandler::DeleteScriptByUid
       
   551 // -----------------------------------------------------------------------------
       
   552 //
       
   553 TInt CCFScriptHandler::DeleteScriptByUid( const TUid& aUid )
       
   554     {
       
   555     FUNC_LOG;
       
   556     
       
   557     // Delete all scripts registered and stored by the client
       
   558     TInt err = KErrNotFound;
       
   559     HBufC* name = HBufC::New( KMaxFileName );
       
   560     if( name )
       
   561         {
       
   562         RPointerArray<CCFScript> scripts;
       
   563         GetScriptsByUid( aUid, scripts );
       
   564         if( scripts.Count() )
       
   565             {
       
   566             for( TInt i = 0; i < scripts.Count(); i++ )
       
   567                 {
       
   568                 CCFScript* script = scripts[i];
       
   569                 TPtr namePtr( name->Des() );
       
   570                 ScriptFilePath( script->Info(), namePtr );
       
   571                 err = BaflUtils::DeleteFile( iFs, namePtr );
       
   572                 INFO_3( "Deleted script file %S from client %x with code %d",
       
   573                     &namePtr, aUid.iUid, err );
       
   574                 }
       
   575             
       
   576             // Remove folder
       
   577             TParsePtrC parse( *name );
       
   578             TPtrC dir( parse.DriveAndPath() );
       
   579             
       
   580             // Check if not in rules base folder - delete this folder
       
   581             if( aUid != KCFServerSid )
       
   582                 {
       
   583                 err = iFs.RmDir( dir );
       
   584                 INFO_2( "Removed directory %S with code %d", &dir, err );
       
   585                 }
       
   586             }
       
   587         else
       
   588             {
       
   589             // Scripts not found (already deregistered), search the scripts
       
   590             // manually
       
   591             TPtr namePtr( name->Des() );
       
   592             namePtr.Format( KDefaultSystemRuleFileFormat, aUid.iUid, &KWildCardChar );
       
   593             namePtr[0] = iDefaultSystemDrive;
       
   594             
       
   595             // search all rule files from the particular client
       
   596             CDir* dir = NULL;
       
   597             err = iFs.GetDir( namePtr, KEntryAttNormal, ESortNone, dir );
       
   598             if( err == KErrNone && dir )
       
   599                 {
       
   600                 for( TInt i = 0; i < dir->Count(); i++ )
       
   601                     {
       
   602                     TParsePtrC entryNameParse( (*dir)[i].iName );
       
   603                     TPtrC entryName( entryNameParse.Name() );
       
   604                     namePtr.Format( KDefaultSystemRuleFileFormat,
       
   605                         aUid.iUid, &entryName );
       
   606                     namePtr[0] = iDefaultSystemDrive;
       
   607                     
       
   608                     // Possible error values can be ignored since deleting
       
   609                     // multiple files
       
   610                     DeleteScriptFile( namePtr, aUid );
       
   611                     }
       
   612                 err = KErrDeregisterNotNeeded;
       
   613                 }
       
   614             else
       
   615                 {
       
   616                 // Basically no scripts found, convert err to KErrNotFound
       
   617                 err = KErrNotFound;
       
   618                 }
       
   619             delete dir;
       
   620             dir = NULL;
       
   621             }
       
   622         
       
   623         // cleanup
       
   624         scripts.Close();
       
   625         delete name;
       
   626         name = NULL;
       
   627         }
       
   628     else
       
   629         {
       
   630         err = KErrNoMemory;
       
   631         }
       
   632     
       
   633     return err;
       
   634     }
       
   635 
       
   636 // -----------------------------------------------------------------------------
       
   637 // CCFScriptHandler::AlreadyExists
       
   638 // -----------------------------------------------------------------------------
       
   639 //
       
   640 TBool CCFScriptHandler::AlreadyExists( const TDesC& aScriptName,
       
   641     const TUid& aOwnerUid,
       
   642     TInt& aScriptId ) const
       
   643     {
       
   644     FUNC_LOG;
       
   645     
       
   646     TParsePtrC parsePtrC( aScriptName );
       
   647     TPtrC scriptName( parsePtrC.Name() );
       
   648     TBool exists = EFalse;
       
   649     RPointerArray<CCFScript> scripts;
       
   650     GetScriptsByUid( aOwnerUid, scripts );
       
   651     for( TInt i = 0; i < scripts.Count(); i++ )
       
   652         {
       
   653         CCFScript* script = scripts[i];
       
   654         if( script->Name().CompareF( scriptName ) == KErrNone )
       
   655             {
       
   656             // Name already in use - store script id
       
   657             aScriptId = script->ScriptId();
       
   658             exists = ETrue;
       
   659             break;
       
   660             }
       
   661         }
       
   662     scripts.Close();
       
   663     
       
   664     return exists;
       
   665     }
       
   666 
       
   667 // -----------------------------------------------------------------------------
       
   668 // CCFScriptHandler::CleanupPersistentDataByName
       
   669 // -----------------------------------------------------------------------------
       
   670 //
       
   671 void CCFScriptHandler::CleanupPersistentDataByName( const TDesC& aScriptName,
       
   672     const TUid& aOwnerUid )
       
   673     {
       
   674     FUNC_LOG;
       
   675     
       
   676     TParsePtrC parsePtrC( aScriptName );
       
   677     TPtrC scriptName( parsePtrC.Name() );
       
   678     RPointerArray<CCFScript> scripts;
       
   679     GetScriptsByUid( aOwnerUid, scripts );
       
   680     for( TInt i = 0; i < scripts.Count(); i++ )
       
   681         {
       
   682         CCFScript* script = scripts[i];
       
   683         if( script->Name().CompareF( scriptName ) == KErrNone )
       
   684             {
       
   685             script->CleanupPersistentData();
       
   686             }
       
   687         }
       
   688     scripts.Close();
       
   689     }
       
   690 
       
   691 // -----------------------------------------------------------------------------
       
   692 // CCFScriptHandler::CleanupPersistentDataByUid
       
   693 // -----------------------------------------------------------------------------
       
   694 //
       
   695 void CCFScriptHandler::CleanupPersistentDataByUid( const TUid& aOwnerUid )
       
   696     {
       
   697     FUNC_LOG;
       
   698     
       
   699     RPointerArray<CCFScript> scripts;
       
   700     GetScriptsByUid( aOwnerUid, scripts );
       
   701     for( TInt i = 0; i < scripts.Count(); i++ )
       
   702         {
       
   703         CCFScript* script = scripts[i];
       
   704         script->CleanupPersistentData();
       
   705         }
       
   706     scripts.Close();
       
   707     }
       
   708 
       
   709 // -----------------------------------------------------------------------------
       
   710 // CCFScriptHandler::RestoreRomScript
       
   711 // -----------------------------------------------------------------------------
       
   712 //
       
   713 TInt CCFScriptHandler::RestoreRomScript( TInt aScriptId,
       
   714     const TUid& /*aOwnerUid*/,
       
   715     const RThread& aClient )
       
   716     {
       
   717     FUNC_LOG;
       
   718     
       
   719     TInt err = KErrNotFound;
       
   720     
       
   721     // Check that the restored file actually exists
       
   722     RPointerArray<CCFScript> scripts;
       
   723     GetScriptsByUid( KCFServerSid, scripts );
       
   724     CCFScript* script = NULL;
       
   725     for( TInt i = 0; i < scripts.Count(); i++ )
       
   726         {
       
   727         script = scripts[i];
       
   728         if( script->ScriptId() == aScriptId )
       
   729             {
       
   730             // Script found - break from loop
       
   731             break;
       
   732             }
       
   733         script = NULL;
       
   734         }
       
   735     scripts.Close();
       
   736     
       
   737     // If the script was found
       
   738     if( script )
       
   739         {
       
   740         HBufC* name = ScriptFilePath( script->Info() );
       
   741         if( name )
       
   742             {
       
   743             // Resolve rom script name and replace system drive letter with rom
       
   744             TPtr namePtr( name->Des() );
       
   745             namePtr[0] = iDefaultRomDrive;
       
   746             
       
   747             TBool exists = BaflUtils::FileExists( iFs, namePtr );
       
   748             if( exists )
       
   749                 {
       
   750                 HBufC8* scriptBuf = LoadScriptFromFile( namePtr );
       
   751                 if( scriptBuf )
       
   752                     {
       
   753                     MCFActionHandler& scriptSession =
       
   754                         script->ActionHandler();
       
   755 
       
   756                     // Check thath upgrade is allowed
       
   757                     err = IsUpgradeAllowed( script->Name(), *scriptBuf,
       
   758                         script->OwnerUid(), aClient, scriptSession );
       
   759                     if( err == KErrNone )
       
   760                         {
       
   761                         // Script data succesfully loaded - update script
       
   762                         err = UpdateScript( aScriptId, aClient, *scriptBuf, NULL );
       
   763                         }
       
   764                     }
       
   765                 else
       
   766                     {
       
   767                     err = KErrCorrupt;
       
   768                     }
       
   769                 delete scriptBuf;
       
   770                 scriptBuf = NULL;
       
   771                 }
       
   772             }
       
   773 
       
   774         // Clean up
       
   775         delete name;
       
   776         name = NULL;
       
   777         }
       
   778     
       
   779     return err;
       
   780     }
       
   781 
       
   782 // -----------------------------------------------------------------------------
       
   783 // CCFScriptHandler::IsUpgradeAllowed
       
   784 // -----------------------------------------------------------------------------
       
   785 //
       
   786 TInt CCFScriptHandler::IsUpgradeAllowed( const TDesC& aName,
       
   787     const TDesC8& aScript,
       
   788     const TUid& aOwner,
       
   789     const RThread& aOwnerThread,
       
   790     MCFActionHandler& aActionHandler )
       
   791     {
       
   792     FUNC_LOG;
       
   793     
       
   794     TInt allowed( KErrNotSupported );
       
   795     
       
   796     // Since we are not creating a permanent script script id and owner are
       
   797     // temporary
       
   798     CCFScript* scriptUpgrade = NULL;
       
   799     TRAP( allowed, scriptUpgrade = CreateScriptL( aName, aScript, aOwner,
       
   800         aOwnerThread, aActionHandler, ETrue, KTempScriptId, NULL ) );
       
   801     if( allowed == KErrNone )
       
   802         {
       
   803         if( scriptUpgrade )
       
   804             {
       
   805             allowed = KErrNotFound;
       
   806             RPointerArray<CCFScript> scripts;
       
   807             
       
   808             // Get all rom scripts - this means all scripts with owner uid
       
   809             // of cfserver sid
       
   810             GetScriptsByUid( KCFServerSid, scripts );
       
   811             for( TInt i = 0; i < scripts.Count(); i++ )
       
   812                 {
       
   813                 CCFScript* script = scripts[i];
       
   814                 if( script->Name().CompareF(
       
   815                     scriptUpgrade->Name() ) == KErrNone )
       
   816                     {
       
   817                     // Matching script found - check that script upgrade has at
       
   818                     // least the same capability level than the rom script
       
   819                     if( scriptUpgrade->UpgradeSecurity().HasCapabilities(
       
   820                         script->UpgradeSecurity() ) )
       
   821                         {
       
   822                         // Check that client has the needed upgrade capabilities
       
   823                         TSecurityInfo client( aOwnerThread );
       
   824                         if( client.iCaps.HasCapabilities(
       
   825                             scriptUpgrade->UpgradeSecurity() ) )
       
   826                             {
       
   827                             allowed = KErrNone;
       
   828                             }
       
   829                         else
       
   830                             {
       
   831                             allowed = KErrAccessDenied;
       
   832                             }
       
   833                         }
       
   834                     else
       
   835                         {
       
   836                         allowed = KErrAccessDenied;
       
   837                         }
       
   838                     }
       
   839                 }
       
   840             scripts.Close();
       
   841             }
       
   842         else
       
   843             {
       
   844             allowed = KErrNotSupported;
       
   845             }
       
   846         }
       
   847         
       
   848     // clean up
       
   849     delete scriptUpgrade;
       
   850     scriptUpgrade = NULL;
       
   851     
       
   852     return allowed;
       
   853     }
       
   854 
       
   855 //------------------------------------------------------------------------------
       
   856 // CCFScriptHandler::RemoveScriptByProviderUid
       
   857 //------------------------------------------------------------------------------
       
   858 //
       
   859 TInt CCFScriptHandler::RemoveScriptByProviderUid( const TUid& aProviderUid,
       
   860     TBool aRollback )
       
   861     {
       
   862     FUNC_LOG;
       
   863     
       
   864     TInt err = KErrNotFound;
       
   865 
       
   866     // Check all scripts which have dependency to the provider in question
       
   867     for( TInt i = iScripts.Count() - 1; i >= 0; i-- )
       
   868         {
       
   869         CCFScript* script = iScripts[i];
       
   870         if( script->HasDependency( aProviderUid ) )
       
   871             {
       
   872             // Script has dependency to the provider, remove script
       
   873             err = KErrNone;
       
   874             if( aRollback )
       
   875                 {
       
   876                 // Rollback needed, create a copy from info
       
   877                 TRAPD( rollbackErr, AddRollbackInfoL( *script ) );
       
   878                 if( rollbackErr != KErrNone )
       
   879                     {
       
   880                     TPtrC name( script->Name() );
       
   881                     ERROR_2( rollbackErr, "Failed to rollback script [%S] from client [%x]",
       
   882                         &name, script->OwnerUid().iUid );
       
   883                     }
       
   884                 }
       
   885             iScripts.Remove( i );
       
   886             delete script;
       
   887             script = NULL;
       
   888             }
       
   889         }
       
   890     
       
   891     return err;
       
   892     }
       
   893 
       
   894 //------------------------------------------------------------------------------
       
   895 // CCFScriptHandler::RollbackScripts
       
   896 //------------------------------------------------------------------------------
       
   897 //
       
   898 void CCFScriptHandler::RollbackScripts()
       
   899     {
       
   900     FUNC_LOG;
       
   901     
       
   902     HBufC* path = HBufC::New( KMaxFileName );
       
   903     if( path )
       
   904         {
       
   905         TPtr pathPtr( path->Des() );
       
   906         TBool rollback = EFalse;
       
   907         
       
   908         // Go through roll back list and automatically search for scripts in rom
       
   909         // or ram. If a script is not found, owner of the script will be notified
       
   910         // that an automatic script dereigstration has occured.
       
   911         for( TInt i = iRollbackList.Count() - 1; i >= 0; i-- )
       
   912             {
       
   913             // Format file path from script info
       
   914             CCFScriptInfo* info = iRollbackList[i];
       
   915             ScriptFilePath( *info, pathPtr );
       
   916             if( BaflUtils::FileExists( iFs, pathPtr ) )
       
   917                 {
       
   918                 // Script found from default system drive
       
   919                 rollback = ETrue;
       
   920                 }
       
   921             else
       
   922                 {
       
   923                 // Script not found from default system drive, check rom if the
       
   924                 // owner if cfserver
       
   925                 if( info->OwnerUid() == KCFServerSid )
       
   926                     {
       
   927                     // Check from rom
       
   928                     pathPtr[0] = iDefaultRomDrive;
       
   929                     if( BaflUtils::FileExists( iFs, pathPtr ) )
       
   930                         {
       
   931                         // Script found from default rom drive
       
   932                         rollback = ETrue;
       
   933                         }
       
   934                     }
       
   935                 }
       
   936             
       
   937             // Rollback if possible
       
   938             if( rollback )
       
   939                 {
       
   940                 // Rollback
       
   941                 TRAPD( err, RollbackScriptL( *info, pathPtr ) );
       
   942                 TPtrC name( info->Name() );
       
   943                 if( err == KErrNone )
       
   944                     {
       
   945                     INFO_2( "Script [%S] from owner [%x] succesfull restored",
       
   946                         &name, info->OwnerUid().iUid );
       
   947                     }
       
   948                 else
       
   949                     {
       
   950                     INFO_2( "Script [%S] from owner [%x] failed to restore",
       
   951                         &name, info->OwnerUid().iUid );
       
   952                     }
       
   953                 
       
   954                 // Clean up rollback info
       
   955                 iRollbackList.Remove( i );
       
   956                 delete info;
       
   957                 info = NULL;
       
   958                 }
       
   959             }
       
   960         
       
   961         // Check scripts which could not be automatically activated
       
   962         while( iRollbackList.Count() )
       
   963             {
       
   964             // Get the first script info
       
   965             CCFScriptInfo* info = iRollbackList[0];
       
   966 
       
   967             // Check if the owner is set and check all the other scripts by the
       
   968             // same owner
       
   969             MCFScriptOwner* owner = info->OwnerSession();
       
   970             if( owner )
       
   971                 {
       
   972                 // Owner found, notify owner
       
   973                 NotifyScriptIds( owner );
       
   974                 }
       
   975             else
       
   976                 {
       
   977                 // Owner not set, delete info and update rollback list
       
   978                 iRollbackList.Remove( 0 );
       
   979                 delete info;
       
   980                 info = NULL;
       
   981                 }
       
   982             }
       
   983         
       
   984         // Clean up
       
   985         delete path;
       
   986         path = NULL;
       
   987         }
       
   988     else
       
   989         {
       
   990         ERROR( KErrNoMemory, "Failed to rollback scripts due OOM" );
       
   991         }
       
   992     iRollbackList.ResetAndDestroy();
       
   993     }
       
   994 
       
   995 //------------------------------------------------------------------------------
       
   996 // CCFScriptHandler::DeregisterScriptOwner
       
   997 //------------------------------------------------------------------------------
       
   998 //
       
   999 void CCFScriptHandler::DeregisterScriptOwner( MCFScriptOwner* aScriptOwner )
       
  1000     {
       
  1001     FUNC_LOG;
       
  1002     
       
  1003     for( TInt i = 0; i < iScripts.Count(); i++ )
       
  1004         {
       
  1005         CCFScriptInfo& info = iScripts[i]->Info();
       
  1006         if( info.OwnerSession() == aScriptOwner )
       
  1007             {
       
  1008             info.SetOwnerSession( NULL );
       
  1009             }
       
  1010         }
       
  1011     }
       
  1012 
       
  1013 // -----------------------------------------------------------------------------
       
  1014 // CCFScriptHandler::InitializePhaseL
       
  1015 // -----------------------------------------------------------------------------
       
  1016 //
       
  1017 void CCFScriptHandler::InitializePhaseL( CCFPhaseBase::TCFPhaseId aPhaseId )
       
  1018     {
       
  1019     FUNC_LOG;
       
  1020 
       
  1021     MCFStarterObserver* observer 
       
  1022     	= (MCFStarterObserver*) iOperationPluginManager;
       
  1023     observer->InitializePhaseL( aPhaseId );
       
  1024     
       
  1025     switch( aPhaseId )
       
  1026         {
       
  1027         case CCFPhaseBase::ECFDeviceStarting:
       
  1028             {
       
  1029             InitDeviceStartingPhaseL();
       
  1030             break;
       
  1031             }
       
  1032         case CCFPhaseBase::ECFLoadingRules:
       
  1033             {
       
  1034             InitDeviceStartedPhaseL();
       
  1035             break;
       
  1036             }
       
  1037         default:
       
  1038             {
       
  1039             break;
       
  1040             }
       
  1041         }
       
  1042     }
       
  1043 
       
  1044 //----------------------------------------------------------------------------
       
  1045 // CCFScriptHandler::SetEventHandler
       
  1046 //----------------------------------------------------------------------------
       
  1047 //
       
  1048 void CCFScriptHandler::SetEventHandler( MCFStarterEventHandler& /*aEventHandler*/ )
       
  1049     {
       
  1050 	  FUNC_LOG;
       
  1051     }
       
  1052 
       
  1053 // -----------------------------------------------------------------------------
       
  1054 // CCFScriptHandler::UpdatePlugInsL
       
  1055 // -----------------------------------------------------------------------------
       
  1056 //
       
  1057 void CCFScriptHandler::UpdatePlugInsL()
       
  1058     {
       
  1059     FUNC_LOG;
       
  1060 
       
  1061     iOperationPluginManager->UpdatePlugInsL(); // Let the "wait starter" to proceed.
       
  1062     }
       
  1063 
       
  1064 
       
  1065 // -----------------------------------------------------------------------------
       
  1066 // CCFScriptHandler::ParseFileCompleteL
       
  1067 // -----------------------------------------------------------------------------
       
  1068 //
       
  1069 void CCFScriptHandler::ParseFileCompleteL()
       
  1070     {
       
  1071     FUNC_LOG;
       
  1072 
       
  1073     iWaitParsing->AsyncStop(); // Let the "wait starter" to proceed.
       
  1074     }
       
  1075 
       
  1076 // -----------------------------------------------------------------------------
       
  1077 // CCFScriptHandler::GetData
       
  1078 // -----------------------------------------------------------------------------
       
  1079 //
       
  1080 void CCFScriptHandler::GetData( TPtrC8& aPtr, TRequestStatus& aStatus )
       
  1081     {
       
  1082     FUNC_LOG;
       
  1083 
       
  1084     TRequestStatus *requestStatus = &aStatus;
       
  1085     switch( iParserDataProvidingState )
       
  1086         {
       
  1087         case KInit:
       
  1088             aPtr.Set( iParserData );
       
  1089             iParserDataProvidingState = KDataSent;
       
  1090             User::RequestComplete( requestStatus, KMoreData );
       
  1091             break;
       
  1092         case KDataSent:
       
  1093             iParserDataProvidingState = KDone;
       
  1094             User::RequestComplete( requestStatus, KDataStreamEnd );
       
  1095             break;
       
  1096         case KDone:
       
  1097             User::RequestComplete( requestStatus, KDataStreamEnd );
       
  1098             break;
       
  1099         default:
       
  1100             User::RequestComplete( requestStatus, KDataStreamError );
       
  1101             break;
       
  1102         };
       
  1103     }
       
  1104 
       
  1105 // -----------------------------------------------------------------------------
       
  1106 // CCFScriptHandler::Disconnect
       
  1107 // -----------------------------------------------------------------------------
       
  1108 //
       
  1109 void CCFScriptHandler::Disconnect()
       
  1110     {
       
  1111     FUNC_LOG;
       
  1112 
       
  1113     iParserData.Set( 0, 0 ); // Parser data can be invalidated.
       
  1114     iParserDataProvidingState = KDone;
       
  1115     }
       
  1116 
       
  1117 // -----------------------------------------------------------------------------
       
  1118 // CCFScriptHandler::AddScriptL
       
  1119 // Method that implements the adding operation of a new script.
       
  1120 // -----------------------------------------------------------------------------
       
  1121 //
       
  1122 TInt CCFScriptHandler::AddScriptL( const TDesC& aName,
       
  1123     const TDesC8& aScript,
       
  1124     const TUid& aOwner,
       
  1125     const RThread& aOwnerThread,
       
  1126     MCFActionHandler& aActionHandler,
       
  1127     TBool aDoSecurityCheck,
       
  1128     MCFScriptOwner* aScriptOwner,
       
  1129     TInt aScriptId )
       
  1130     {
       
  1131     FUNC_LOG;
       
  1132     
       
  1133     CCFScript* script = CreateScriptL( aName, aScript, aOwner, aOwnerThread,
       
  1134         aActionHandler, aDoSecurityCheck, aScriptOwner, aScriptId );
       
  1135     
       
  1136     // Leave if script is not created
       
  1137     User::LeaveIfNull( script );
       
  1138 
       
  1139     CleanupStack::PushL( script );
       
  1140     if( iUpdatedScript )
       
  1141         {
       
  1142         iUpdatedScript->CleanupPersistentData();
       
  1143         }
       
  1144     script->ActivateL();
       
  1145     User::LeaveIfError( iScripts.Append( script ) );
       
  1146     CleanupStack::Pop( script );
       
  1147 
       
  1148     return aScriptId != KErrNotFound ? aScriptId : iNextId++;
       
  1149     }
       
  1150 
       
  1151 
       
  1152 // -----------------------------------------------------------------------------
       
  1153 // CCFScriptHandler::RemoveScriptsBySession
       
  1154 // Removes all scripts that are associated to the given aSession.
       
  1155 // -----------------------------------------------------------------------------
       
  1156 //
       
  1157 TInt CCFScriptHandler::RemoveScriptsBySession(
       
  1158     const MCFActionHandler& aSession )
       
  1159     {
       
  1160     FUNC_LOG;
       
  1161 
       
  1162     // local variable declarations and initialization
       
  1163     TInt count( KErrNone );             // init to zero
       
  1164     TInt index = iScripts.Count() - 1;  // index of last script
       
  1165 
       
  1166     // iterate through the scripts array
       
  1167     // and remove scripts if necessary
       
  1168     while ( index >= 0 )
       
  1169         {
       
  1170         // get the currently last script in the array
       
  1171         CCFScript* script = iScripts[ index ];
       
  1172 
       
  1173         // remove the script if the session matches
       
  1174         if ( &( script->ActionHandler() ) == &aSession )
       
  1175             {
       
  1176             // remove from internal array
       
  1177             iScripts.Remove( index );
       
  1178             delete script;
       
  1179             script = NULL;
       
  1180             // increment removed scripts count
       
  1181             count++;
       
  1182             }
       
  1183         // decrement index
       
  1184         index--;
       
  1185         }
       
  1186     // compress the array
       
  1187     iScripts.GranularCompress();
       
  1188     // return # of scripts removed
       
  1189     return count;
       
  1190     }
       
  1191 
       
  1192 // -----------------------------------------------------------------------------
       
  1193 // CCFScriptHandler::RemoveScriptById
       
  1194 // Removes a script according to its unique Id, if it exists.
       
  1195 // -----------------------------------------------------------------------------
       
  1196 //
       
  1197 TInt CCFScriptHandler::RemoveScriptById( TInt aScriptId, const RThread& aOwner )
       
  1198     {
       
  1199     FUNC_LOG;
       
  1200 
       
  1201     // make sure got valid Id
       
  1202     if ( aScriptId <= 0 )
       
  1203         {
       
  1204         ERROR_GEN( "Got negative script Id parameter" );
       
  1205         return KErrArgument;
       
  1206         }
       
  1207 
       
  1208     // Iterate through the entire scripts array
       
  1209     TInt err = KErrNotFound;
       
  1210     for ( TInt i = iScripts.Count() - 1; i >= 0; i-- )
       
  1211         {
       
  1212         CCFScript* script = iScripts[ i ];
       
  1213         if ( script->ScriptId() == aScriptId )
       
  1214             {
       
  1215             // script found
       
  1216 			if ( script->OwnerUid() == aOwner.SecureId() )
       
  1217             	{
       
  1218             	// remove from internal array
       
  1219             	iScripts.Remove( i );
       
  1220             	delete script;
       
  1221             	script = NULL;
       
  1222 	            err = KErrNone;
       
  1223             	}
       
  1224     		else
       
  1225     			{
       
  1226     			err = KErrAccessDenied;
       
  1227     			}        	
       
  1228             break;
       
  1229             }
       
  1230         }
       
  1231     // return whether or not we removed the script
       
  1232     return err;
       
  1233     }
       
  1234 
       
  1235 // -----------------------------------------------------------------------------
       
  1236 // CCFScriptHandler::RemoveScriptByName
       
  1237 // -----------------------------------------------------------------------------
       
  1238 //
       
  1239 TInt CCFScriptHandler::RemoveScriptByName( const TDesC& aScriptName,
       
  1240     const TUid& aOwnerUid )
       
  1241     {
       
  1242     FUNC_LOG;
       
  1243     
       
  1244     TParsePtrC parsePtrC( aScriptName );
       
  1245     TPtrC scriptName( parsePtrC.Name() );
       
  1246     
       
  1247     TInt err = KErrNotFound;
       
  1248     for( TInt i = 0; i < iScripts.Count(); ++i )
       
  1249         {
       
  1250         CCFScript* script = iScripts[i];
       
  1251         
       
  1252         // Check owner
       
  1253         if( script->OwnerUid() == aOwnerUid )
       
  1254             {
       
  1255             // Check name
       
  1256             if( script->Name().CompareF( scriptName ) == KErrNone )
       
  1257                 {
       
  1258                 // Owner uid and name matches - remove script
       
  1259                 iScripts.Remove( i );
       
  1260                 delete script;
       
  1261                 script = NULL;
       
  1262                 err = KErrNone;
       
  1263                 break;
       
  1264                 }
       
  1265             }
       
  1266         }
       
  1267     iScripts.GranularCompress();
       
  1268     
       
  1269     return err;
       
  1270     }
       
  1271 
       
  1272 // -----------------------------------------------------------------------------
       
  1273 // CCFScriptHandler::RemoveScriptByUid
       
  1274 // -----------------------------------------------------------------------------
       
  1275 //
       
  1276 TInt CCFScriptHandler::RemoveScriptByUid( const TUid& aUid )
       
  1277     {
       
  1278     FUNC_LOG;
       
  1279     
       
  1280     TInt err = KErrNotFound;
       
  1281     for( TInt i = iScripts.Count() - 1; i >= 0; i-- )
       
  1282         {
       
  1283         CCFScript* script = iScripts[i];
       
  1284         if( script->OwnerUid() == aUid )
       
  1285             {
       
  1286             // remove from internal array
       
  1287             iScripts.Remove( i );
       
  1288             delete script;
       
  1289             script = NULL;
       
  1290             err = KErrNone;
       
  1291             }
       
  1292         }
       
  1293     iScripts.GranularCompress();
       
  1294 
       
  1295     return err;
       
  1296     }
       
  1297 
       
  1298 // -----------------------------------------------------------------------------
       
  1299 // CCFScriptHandler::InitDeviceStartingPhaseL
       
  1300 // -----------------------------------------------------------------------------
       
  1301 //
       
  1302 void CCFScriptHandler::InitDeviceStartingPhaseL()
       
  1303     {
       
  1304     FUNC_LOG;
       
  1305 
       
  1306     // Load context source manager configuration cenrep
       
  1307     CRepository* cenRep = CRepository::NewLC( KCRUidCFRuleScriptConf );
       
  1308     TInt count = 0;
       
  1309     TInt err = cenRep->Get( KCRuleScriptNumberOfMandatoryRules, count );
       
  1310     if( err == KErrNone && count )
       
  1311         {
       
  1312         INFO_1( "Found %d scripts from cenrep", count );
       
  1313         
       
  1314         TUint32 key = KCRuleScriptNumberOfMandatoryRules + 1;
       
  1315         HBufC* fileName = HBufC::NewLC( KMaxFileName );
       
  1316         TPtr fileNamePtr = fileName->Des();
       
  1317         for( TInt i = 0; i < count; i++ )
       
  1318             {
       
  1319             // Ignore first key (count)
       
  1320             err = cenRep->Get( key + i, fileNamePtr );
       
  1321             if( err == KErrNone && fileNamePtr != KNullDesC )
       
  1322                 {
       
  1323                 // Check if the script has an upgrade
       
  1324                 CompleteFilePath( fileNamePtr );
       
  1325                 fileNamePtr[0] = iDefaultSystemDrive;
       
  1326                 if( !BaflUtils::FileExists( iFs, fileNamePtr ) )
       
  1327                     {
       
  1328                     // Use default rom drive
       
  1329                     fileNamePtr[0] = iDefaultRomDrive;
       
  1330                     }
       
  1331                 err = DoLoad( fileNamePtr );
       
  1332                 if( err == KErrNone )
       
  1333                     {
       
  1334                     CCFScript* script = iScripts[iScripts.Count() - 1];
       
  1335                     if( script )
       
  1336                         {
       
  1337                         TInt err = iLoadedScripts.Append( script );
       
  1338                         ERROR_1( err, "Unable to append script %S in loaded scripts array",
       
  1339                             &fileNamePtr );
       
  1340                         }
       
  1341                     }
       
  1342                 }
       
  1343             else
       
  1344                 {
       
  1345                 INFO_2( "Script in file '%S' skipped, error code %d", &fileNamePtr, err );
       
  1346                 }
       
  1347 
       
  1348             fileNamePtr.Zero();
       
  1349             }
       
  1350         CleanupStack::PopAndDestroy( fileName ); // fileName
       
  1351         }
       
  1352     CleanupStack::PopAndDestroy( cenRep ); // cenRep
       
  1353     }
       
  1354 
       
  1355 // -----------------------------------------------------------------------------
       
  1356 // CCFScriptHandler::InitDeviceStartedPhaseL
       
  1357 // -----------------------------------------------------------------------------
       
  1358 //
       
  1359 void CCFScriptHandler::InitDeviceStartedPhaseL()
       
  1360     {
       
  1361     FUNC_LOG;
       
  1362 
       
  1363     // Create search pattern and dir scanner
       
  1364     HBufC* filePath = HBufC::NewLC( KMaxFileName );
       
  1365     TPtr filePathPtr = filePath->Des();
       
  1366     filePathPtr.Append( KDefaultRuleFilePath );
       
  1367     filePathPtr.Append( KDefaultRuleFileSearchPattern );
       
  1368     filePathPtr[0] = iDefaultRomDrive;
       
  1369 
       
  1370     CDirScan* dirScan = CDirScan::NewLC( iFs );
       
  1371     dirScan->SetScanDataL( filePathPtr, KEntryAttNormal, ESortNone );
       
  1372     CDir* dir = NULL;
       
  1373     
       
  1374     // First scan rom
       
  1375     dirScan->NextL( dir );
       
  1376     while( dir )
       
  1377         {
       
  1378         for( TInt i = 0; i < dir->Count(); i++ )
       
  1379             {
       
  1380             filePathPtr.Copy( dirScan->FullPath() );
       
  1381             const TEntry& entry = ( *dir )[i];
       
  1382             filePathPtr.Append( entry.iName );
       
  1383             
       
  1384             // Check for upgrade
       
  1385             filePathPtr[0] = iDefaultSystemDrive;
       
  1386             if( !BaflUtils::FileExists( iFs, filePathPtr ) )
       
  1387                 {
       
  1388                 // Upgrade not found - use rom file
       
  1389                 filePathPtr[0] = iDefaultRomDrive;
       
  1390                 }
       
  1391             DoLoad( filePathPtr );
       
  1392             }
       
  1393         delete dir;
       
  1394         dir = NULL;
       
  1395         dirScan->NextL( dir );
       
  1396         }
       
  1397     
       
  1398     // Ensure that cfserver default rule folder exists
       
  1399     filePathPtr.Copy( KDefaultRuleFilePath );
       
  1400     filePathPtr[0] = iDefaultSystemDrive;
       
  1401     BaflUtils::EnsurePathExistsL( iFs, filePathPtr );
       
  1402 
       
  1403     // Scan ram for *.rul files but exclude all upgrade files
       
  1404     filePathPtr.Append( KDefaultRuleFileSearchPattern );
       
  1405     dirScan->SetScanDataL( filePathPtr, KEntryAttNormal, ESortNone );
       
  1406     dirScan->NextL( dir );
       
  1407     while( dir )
       
  1408         {
       
  1409         // Exlude all rom upgrade files
       
  1410         TPtrC fullPath( dirScan->FullPath() );
       
  1411         if( fullPath.Length() > KDefaultRuleFilePath().Length() )
       
  1412             {
       
  1413             for( TInt i = 0; i < dir->Count(); i++ )
       
  1414                 {
       
  1415                 filePathPtr.Copy( dirScan->FullPath() );
       
  1416                 const TEntry& entry = ( *dir )[i];
       
  1417                 filePathPtr.Append( entry.iName );
       
  1418                 DoLoad( filePathPtr );
       
  1419                 }
       
  1420             }
       
  1421         delete dir;
       
  1422         dir = NULL;
       
  1423         dirScan->NextL( dir );
       
  1424         }
       
  1425     
       
  1426     // Cleanup
       
  1427     CleanupStack::PopAndDestroy( dirScan );  // dirScan
       
  1428     CleanupStack::PopAndDestroy( filePath ); // filePath
       
  1429     
       
  1430     // Close loaded scripts array since it is not needed anymore
       
  1431     iLoadedScripts.Close();
       
  1432     }
       
  1433 
       
  1434 // -----------------------------------------------------------------------------
       
  1435 // CCFScriptHandler::CompleteFilePath
       
  1436 // -----------------------------------------------------------------------------
       
  1437 //
       
  1438 void CCFScriptHandler::CompleteFilePath( TDes& aFileName )
       
  1439     {
       
  1440     FUNC_LOG;
       
  1441 
       
  1442     if( aFileName.MaxLength() >
       
  1443         aFileName.Length() + KDefaultRuleFilePath().Length() )
       
  1444         {
       
  1445         aFileName.Insert( 0, KDefaultRuleFilePath );
       
  1446         }
       
  1447     }
       
  1448 
       
  1449 // -----------------------------------------------------------------------------
       
  1450 // CCFScriptHandler::LoadScriptFromFile
       
  1451 // -----------------------------------------------------------------------------
       
  1452 //
       
  1453 HBufC8* CCFScriptHandler::LoadScriptFromFile( const TDesC& aFilePath )
       
  1454     {
       
  1455     FUNC_LOG;
       
  1456     
       
  1457     INFO_1( "Loading script: %S", &aFilePath );
       
  1458 
       
  1459     HBufC8* script = NULL;
       
  1460     RFile file;
       
  1461     TInt err = file.Open( iFs, aFilePath, EFileRead );
       
  1462     if( err == KErrNone )
       
  1463         {
       
  1464         TInt size = 0;
       
  1465         err = file.Size( size );
       
  1466         if( err == KErrNone )
       
  1467             {
       
  1468             script = HBufC8::New( size );
       
  1469             if( script )
       
  1470                 {
       
  1471                 TPtr8 scriptPtr = script->Des();
       
  1472                 err = file.Read( scriptPtr );
       
  1473                 if( err == KErrNone )
       
  1474                     {
       
  1475                     // Strip all unnecassary data from script
       
  1476                     TInt pos = scriptPtr.FindF( KScriptStartTag );
       
  1477                     if( pos != KErrNotFound )
       
  1478                         {
       
  1479                         scriptPtr.Copy( scriptPtr.MidTPtr( pos ) );
       
  1480                         }
       
  1481                     else
       
  1482                         {
       
  1483                         // Incorrect script
       
  1484                         delete script;
       
  1485                         script = NULL;
       
  1486                         }
       
  1487                     }
       
  1488                 else
       
  1489                     {
       
  1490                     delete script;
       
  1491                     script = NULL;
       
  1492                     }
       
  1493                 }
       
  1494             }
       
  1495         }
       
  1496     
       
  1497     // Cleanup
       
  1498     file.Close();
       
  1499     
       
  1500     ERROR_1( err, "Failed loading script: %S", &aFilePath );
       
  1501     
       
  1502     return script;
       
  1503     }
       
  1504 
       
  1505 // CCFScriptHandler::GetScriptsByUid
       
  1506 // -----------------------------------------------------------------------------
       
  1507 //
       
  1508 void CCFScriptHandler::GetScriptsByUid( const TUid& aUid,
       
  1509     RPointerArray<CCFScript>& aArray ) const
       
  1510     {
       
  1511     FUNC_LOG;
       
  1512 
       
  1513     TInt err = KErrNone;
       
  1514     TInt count = iScripts.Count();
       
  1515     for( TInt i = 0; i < count; i++ )
       
  1516         {
       
  1517         CCFScript* script = iScripts[i];
       
  1518         if( script->OwnerUid() == aUid )
       
  1519             {
       
  1520             err = aArray.Append( script );
       
  1521             if( err != KErrNone )
       
  1522                 {
       
  1523                 // Something went wrong
       
  1524                 break;
       
  1525                 }
       
  1526             }
       
  1527         }
       
  1528     }
       
  1529 
       
  1530 // -----------------------------------------------------------------------------
       
  1531 // CCFScriptHandler::ScriptFilePath
       
  1532 // -----------------------------------------------------------------------------
       
  1533 //
       
  1534 HBufC* CCFScriptHandler::ScriptFilePath( CCFScriptInfo& aInfo ) const
       
  1535     {
       
  1536     FUNC_LOG;
       
  1537     
       
  1538     HBufC* filepath = HBufC::New( KMaxFileName );
       
  1539     if( filepath )
       
  1540         {
       
  1541         TPtr filepathPtr( filepath->Des() );
       
  1542         ScriptFilePath( aInfo, filepathPtr );
       
  1543         }
       
  1544 
       
  1545     return filepath;
       
  1546     }
       
  1547 
       
  1548 // -----------------------------------------------------------------------------
       
  1549 // CCFScriptHandler::ScriptFilePath
       
  1550 // -----------------------------------------------------------------------------
       
  1551 //
       
  1552 HBufC* CCFScriptHandler::ScriptFilePath( const TDesC& aName,
       
  1553     const TUid& aOwnerUid ) const
       
  1554     {
       
  1555     FUNC_LOG;
       
  1556     
       
  1557     HBufC* filepath = HBufC::New( KMaxFileName );
       
  1558     if( filepath )
       
  1559         {
       
  1560         TPtr filepathPtr( filepath->Des() );
       
  1561         if( aOwnerUid == KCFServerSid )
       
  1562             {
       
  1563             filepathPtr.Format( KDefaultRomRuleFileUpgradeFormat, &aName );
       
  1564             }
       
  1565         else
       
  1566             {
       
  1567             filepathPtr.Format( KDefaultSystemRuleFileFormat,
       
  1568                 aOwnerUid.iUid, &aName );
       
  1569             }
       
  1570         filepathPtr[0] = iDefaultSystemDrive;
       
  1571         }
       
  1572 
       
  1573     return filepath;
       
  1574     }
       
  1575 
       
  1576 // -----------------------------------------------------------------------------
       
  1577 // CCFScriptHandler::ScriptFilePath
       
  1578 // -----------------------------------------------------------------------------
       
  1579 //
       
  1580 void CCFScriptHandler::ScriptFilePath( CCFScriptInfo& aInfo, TDes& aFile ) const
       
  1581     {
       
  1582     FUNC_LOG;
       
  1583 
       
  1584     aFile.Zero();
       
  1585     TPtrC name( aInfo.Name() );
       
  1586     if( aInfo.OwnerUid() == KCFServerSid )
       
  1587         {
       
  1588         aFile.Format( KDefaultRomRuleFileUpgradeFormat, &name );
       
  1589         }
       
  1590     else
       
  1591         {
       
  1592         aFile.Format( KDefaultSystemRuleFileFormat,
       
  1593             aInfo.OwnerUid().iUid, &name );
       
  1594         }
       
  1595     aFile[0] = iDefaultSystemDrive;
       
  1596     }
       
  1597 
       
  1598 // -----------------------------------------------------------------------------
       
  1599 // CCFScriptHandler::DoSave
       
  1600 // -----------------------------------------------------------------------------
       
  1601 //
       
  1602 TInt CCFScriptHandler::DoSave( const TDesC& aFileName, const TDesC8& aScript )
       
  1603     {
       
  1604     FUNC_LOG;
       
  1605 
       
  1606     TInt err = KErrNone;
       
  1607     
       
  1608     // Create the target folder if one needed
       
  1609     TParsePtrC parse( aFileName );
       
  1610     TPtrC path( parse.DriveAndPath() );
       
  1611     if( !BaflUtils::FolderExists( iFs, path ) )
       
  1612         {
       
  1613         // Create the target folder
       
  1614         TInt err = iFs.MkDirAll( path );
       
  1615         INFO_2( "Created rule path %S with code %d", &path, err );
       
  1616         }
       
  1617 
       
  1618     // Create a new file and write script - replace
       
  1619     // existing file if one exists
       
  1620     RFile file;
       
  1621     err = file.Replace( iFs, aFileName, EFileWrite );
       
  1622     if ( err == KErrNone )
       
  1623         {
       
  1624         err = file.Write ( aScript, aScript.Length( ) );
       
  1625         }
       
  1626     INFO_2( "Saved script file %S with code %d",
       
  1627         &aFileName, err );
       
  1628     file.Close();
       
  1629     
       
  1630     return err;
       
  1631     }
       
  1632 
       
  1633 // -----------------------------------------------------------------------------
       
  1634 // CCFScriptHandler::DoLoad
       
  1635 // -----------------------------------------------------------------------------
       
  1636 //
       
  1637 TInt CCFScriptHandler::DoLoad( const TDesC& aFilePath )
       
  1638     {
       
  1639     FUNC_LOG;
       
  1640     
       
  1641     // Check if the script has already been loaded
       
  1642     TBool loaded = EFalse;
       
  1643     TUid uid;
       
  1644     TPtrC name( KNullDesC );
       
  1645     ParseUidAndName( aFilePath, uid, name );
       
  1646     TInt err = KErrNone;
       
  1647     if ( uid.iUid != 0 ) // If Uid == 0, subfolder is not valid
       
  1648         {
       
  1649         for( TInt i = 0; i < iLoadedScripts.Count(); i++ )
       
  1650             {
       
  1651             CCFScript* script = iLoadedScripts[i];
       
  1652             if( script->OwnerUid() == uid &&
       
  1653                 script->Name().CompareF( name ) == KErrNone )
       
  1654                 {
       
  1655                 // Script already loaded
       
  1656                 INFO_1( "Script %S already loaded",
       
  1657                     &aFilePath );
       
  1658                 loaded = ETrue;
       
  1659                 iLoadedScripts.Remove( i );
       
  1660                 break;
       
  1661                 }
       
  1662             }
       
  1663     
       
  1664         // Load script if it has not already been loaded
       
  1665         if( !loaded )
       
  1666             {
       
  1667             // Load script from file and configure script
       
  1668             HBufC8* script = LoadScriptFromFile( aFilePath );
       
  1669             if( script )
       
  1670                 {
       
  1671                 INFO_1( "Script %S loaded", &aFilePath );
       
  1672                
       
  1673                 // If script is faulty just ignore it
       
  1674                 RThread thread;
       
  1675                 TRAP( err, AddScriptL( name,
       
  1676                     *script,
       
  1677                     uid,
       
  1678                     thread,
       
  1679                     iScriptEventListener,
       
  1680                     EFalse,
       
  1681                     NULL ) );
       
  1682  
       
  1683                 // Clean up
       
  1684                 thread.Close(); // just in case
       
  1685                 delete script;
       
  1686                 script = NULL;
       
  1687             
       
  1688                 ERROR_1( err, "Script %S not created", &aFilePath );
       
  1689                 if( err >= KErrNone )
       
  1690                     {
       
  1691                     INFO_1( "Script %S succesfully created and initialized",
       
  1692                         &aFilePath );                        
       
  1693                     }
       
  1694                 }
       
  1695             else
       
  1696                 {
       
  1697                 err = KErrNotFound;
       
  1698                 }
       
  1699             }
       
  1700         }   
       
  1701         
       
  1702     return err >= 0 ? KErrNone : err;
       
  1703     }
       
  1704  
       
  1705 // -----------------------------------------------------------------------------
       
  1706 // CCFScriptHandler::ParseUidAndName
       
  1707 // -----------------------------------------------------------------------------
       
  1708 //
       
  1709 void CCFScriptHandler::ParseUidAndName( const TDesC& aFilePath,
       
  1710     TUid& aUid, TPtrC& aName ) const
       
  1711     {
       
  1712     FUNC_LOG;
       
  1713     
       
  1714     // Resolve script name
       
  1715     TParsePtrC parse( aFilePath );
       
  1716     aName.Set( parse.Name() );
       
  1717     
       
  1718     // Resolve uid
       
  1719     TPtrC path( parse.DriveAndPath() );
       
  1720     if( KDefaultRuleFilePath().Length() == path.Length() )
       
  1721         {
       
  1722         // Cfserver sid since no additional sid folder found
       
  1723         aUid.iUid = KCFServerSid.iUid;
       
  1724         }
       
  1725     else
       
  1726         {
       
  1727         if( path.Length() == ( KDefaultRuleFilePath().Length() + KUidLength + 1 ) )
       
  1728             {
       
  1729             // Parse client sid from the folder
       
  1730             TInt start = KDefaultRuleFilePath().Length();
       
  1731             TPtrC uidPtrC( path.Mid( start, KUidLength ) );
       
  1732             TLex lex( uidPtrC );
       
  1733        
       
  1734             // Convert descriptor to unsigned int
       
  1735             TUint32 value = 0;
       
  1736             TInt err = lex.BoundedVal( value, EHex, KLimit );
       
  1737             if( err == KErrNone )
       
  1738                 {
       
  1739                 aUid.iUid = value;
       
  1740                 }
       
  1741             ERROR_1( err, "Unable to parse uid from path %S", &aFilePath );
       
  1742             }
       
  1743         else
       
  1744             {
       
  1745             INFO_1( "The additional folder in path %S is not sid folder", &aFilePath );
       
  1746             // Set Uid = 0 and none script will be loaded from unsuitable subfolder
       
  1747             aUid.iUid = 0;
       
  1748             }
       
  1749         }
       
  1750     }
       
  1751 
       
  1752 // -----------------------------------------------------------------------------
       
  1753 // CCFScriptHandler::CreateScriptL
       
  1754 // -----------------------------------------------------------------------------
       
  1755 //
       
  1756 CCFScript* CCFScriptHandler::CreateScriptL( const TDesC& aName,
       
  1757     const TDesC8& aScript,
       
  1758     const TUid& aOwner,
       
  1759     const RThread& aOwnerThread,
       
  1760     MCFActionHandler& aActionHandler,
       
  1761     TBool aDoSecurityCheck,
       
  1762     MCFScriptOwner* aScriptOwner,
       
  1763     TInt aScriptId )
       
  1764     {
       
  1765     FUNC_LOG;
       
  1766     
       
  1767     // check that we have a descriptor with content
       
  1768     TInt len = aScript.Length();
       
  1769     if ( len <= 0 )
       
  1770         {
       
  1771         ERROR_GEN( "Script length invalid" );
       
  1772         User::Leave( KErrBadDescriptor );
       
  1773         }
       
  1774 
       
  1775     // Init parser usage.
       
  1776     iParserData.Set( aScript );
       
  1777     iParserDataProvidingState = KInit;
       
  1778 
       
  1779     iParser->SetSourceCharacterWidth( CMDXMLParser::EAscii );
       
  1780     iParser->ParseSource( this );
       
  1781     iWaitParsing->Start();
       
  1782 
       
  1783     // Check errors.
       
  1784     CCFScript* script = NULL;
       
  1785     TXMLErrorCodeSeverity severity = iParser->ErrorSeverity();
       
  1786     if ( severity == EXMLWorkable || severity == EXMLNone )
       
  1787         {
       
  1788         CMDXMLDocument* document = iParser->DetachXMLDoc();
       
  1789         if ( document )
       
  1790             {
       
  1791             CleanupStack::PushL( document ); // CLEANUP<< document
       
  1792             CMDXMLElement* documentElement = document->DocumentElement();
       
  1793             if ( documentElement )
       
  1794                 {
       
  1795                 // Get script root
       
  1796                 CMDXMLNode* child = documentElement->FirstChild();
       
  1797                 while ( child )
       
  1798                     {
       
  1799                     if ( child->NodeType() != CMDXMLNode::EElementNode )
       
  1800                         {
       
  1801                         child = child->NextSibling();
       
  1802                         continue;
       
  1803                         }
       
  1804                     else if ( script )
       
  1805                         {
       
  1806                         // One aScript is allowed to have one root (one tree).
       
  1807                         User::Leave( KErrNotSupported );
       
  1808                         }
       
  1809                     
       
  1810                     // Resolve script id
       
  1811                     TInt scriptId = 0;
       
  1812                     if( aScriptId != KErrNotFound )
       
  1813                         {
       
  1814                         scriptId = aScriptId;
       
  1815                         }
       
  1816                     else
       
  1817                         {
       
  1818                         scriptId = iNextId;
       
  1819                         }
       
  1820                     
       
  1821                     // Create new script
       
  1822                     TParsePtrC parse( aName );
       
  1823                     script = CCFScript::NewLC( *child,
       
  1824                         parse.Name(),
       
  1825                         scriptId,
       
  1826                         aOwner,
       
  1827                         aActionHandler,
       
  1828                         len,
       
  1829                         iCF,
       
  1830                         iSecurityChecker,
       
  1831                         *iOperationPluginManager,
       
  1832                         iFs,
       
  1833                         aScriptOwner ); // CLEANUP<< script
       
  1834 
       
  1835                     // Do security check for the script.
       
  1836                     if ( aDoSecurityCheck )
       
  1837                         {    
       
  1838                         User::LeaveIfError(
       
  1839                             script->CheckSecurity( aOwnerThread ) );
       
  1840                         }
       
  1841 
       
  1842                     child = child->NextSibling();
       
  1843                     }
       
  1844                 if( script )
       
  1845                     {
       
  1846                     CleanupStack::Pop( script );                // CLEANUP>> script
       
  1847                     }
       
  1848                 }
       
  1849             CleanupStack::PopAndDestroy( document );    // CLEANUP>> document
       
  1850             }
       
  1851         }
       
  1852     else
       
  1853         {
       
  1854         ERROR_GEN_1( "CCFScriptHandler::CreateScriptL - Parser error [%d]",
       
  1855                 iParser->Error() );
       
  1856         User::LeaveIfError( iParser->Error() );
       
  1857         }
       
  1858 
       
  1859     // Reset parser usage.
       
  1860     iParserData.Set( 0, 0 ); // Parser data will not be valid anymore.
       
  1861     iParserDataProvidingState = KError;
       
  1862     
       
  1863     return script;
       
  1864     }
       
  1865 
       
  1866 //------------------------------------------------------------------------------
       
  1867 // CCFScriptHandler::RollbackScriptL
       
  1868 //------------------------------------------------------------------------------
       
  1869 //
       
  1870 void CCFScriptHandler::RollbackScriptL( const CCFScriptInfo& aInfo,
       
  1871     const TDesC& aFilePath )
       
  1872     {
       
  1873     FUNC_LOG;
       
  1874     
       
  1875     HBufC8* script = LoadScriptFromFile( aFilePath );
       
  1876     User::LeaveIfNull( script );
       
  1877     
       
  1878     CleanupStack::PushL( script );
       
  1879     RThread mainThread;
       
  1880     CleanupClosePushL( mainThread );
       
  1881     
       
  1882     // Security check is not needed since the client has already
       
  1883     // succesfully registered script, using cfsrver main thread as a temp
       
  1884     // thread handle
       
  1885     AddScriptL( aInfo.Name(), *script, aInfo.OwnerUid(), mainThread,
       
  1886         iScriptEventListener, EFalse, aInfo.OwnerSession(), aInfo.Id() );
       
  1887 
       
  1888     // Clean up
       
  1889     CleanupStack::PopAndDestroy( &mainThread );
       
  1890     CleanupStack::PopAndDestroy( script );
       
  1891     }
       
  1892 
       
  1893 //------------------------------------------------------------------------------
       
  1894 // CCFScriptHandler::NotifyScriptIds
       
  1895 //------------------------------------------------------------------------------
       
  1896 //
       
  1897 void CCFScriptHandler::NotifyScriptIds( MCFScriptOwner* aScriptOwner )
       
  1898     {
       
  1899     FUNC_LOG;
       
  1900     
       
  1901     RArray<TInt> scriptIds;
       
  1902     for( TInt i = iRollbackList.Count() - 1; i >= 0; i-- )
       
  1903         {
       
  1904         CCFScriptInfo* info = iRollbackList[i];
       
  1905         if( aScriptOwner == info->OwnerSession() )
       
  1906             {
       
  1907             // Error is ignored. If there is no memory left then the client
       
  1908             // will miss the deregister notification of some scripts
       
  1909             scriptIds.Append( info->Id() );
       
  1910             
       
  1911             // Remove the info from rollback list
       
  1912             iRollbackList.Remove( i );
       
  1913             delete info;
       
  1914             info = NULL;
       
  1915             }
       
  1916         }
       
  1917     aScriptOwner->HandleScriptsRemoved( scriptIds );
       
  1918     scriptIds.Close();
       
  1919     }
       
  1920 
       
  1921 //------------------------------------------------------------------------------
       
  1922 // CCFScriptHandler::AddRollbackInfoL
       
  1923 //------------------------------------------------------------------------------
       
  1924 //
       
  1925 void CCFScriptHandler::AddRollbackInfoL( CCFScript& aScript )
       
  1926     {
       
  1927     FUNC_LOG;
       
  1928 
       
  1929     CCFScriptInfo* info = aScript.CopyInfoLC();
       
  1930     iRollbackList.AppendL( info );
       
  1931     CleanupStack::Pop( info );
       
  1932     }
       
  1933 
       
  1934 //------------------------------------------------------------------------------
       
  1935 // CCFScriptHandler::DeleteScriptFile
       
  1936 //------------------------------------------------------------------------------
       
  1937 //
       
  1938 TInt CCFScriptHandler::DeleteScriptFile( const TDesC& aFilePath,
       
  1939     const TUid& aOwnerUid )
       
  1940     {
       
  1941     FUNC_LOG;
       
  1942     
       
  1943     TInt err = KErrNotFound;
       
  1944     if( BaflUtils::FileExists( iFs, aFilePath ) )
       
  1945         {
       
  1946         err = BaflUtils::DeleteFile( iFs, aFilePath );
       
  1947         INFO_3( "Deleted script file %S from client %x with code %d",
       
  1948             &aFilePath, aOwnerUid.iUid, err );
       
  1949         if( err == KErrNone )
       
  1950             {
       
  1951             // All persistent data clean ups are not treated as errors
       
  1952             CDir* dir = NULL;
       
  1953             TParsePtrC parse( aFilePath );
       
  1954             TPtrC driveAndPath( parse.DriveAndPath() );
       
  1955             TPtrC name( parse.Name() );
       
  1956             
       
  1957             // Cleanup all persistent data files
       
  1958             HBufC* persistentData = HBufC::New( KMaxFileName );
       
  1959             if( persistentData )
       
  1960                 {
       
  1961                 TPtr persistentDataPtr( persistentData->Des() );
       
  1962                 persistentDataPtr.Format( KPersistenDataFormat,
       
  1963                     aOwnerUid.iUid, &name, &KWildCardChar );
       
  1964                 TInt persistentErr = iFs.GetDir( persistentDataPtr, KEntryAttNormal,
       
  1965                     ESortNone, dir );
       
  1966                 if( persistentErr == KErrNone && dir )
       
  1967                     {
       
  1968                     for( TInt i = 0; i < dir->Count(); i++ )
       
  1969                         {
       
  1970                         TParsePtrC entryNameParse( (*dir)[i].iName );
       
  1971                         TPtrC entryName( entryNameParse.Name() );
       
  1972                         persistentDataPtr.Format( KPersistenDataFormat2,
       
  1973                             aOwnerUid.iUid, &entryName );
       
  1974                         persistentDataPtr[0] = iDefaultSystemDrive;
       
  1975 
       
  1976                         persistentErr = BaflUtils::DeleteFile(
       
  1977                             iFs, persistentDataPtr );
       
  1978                         INFO_2( "Deleted persistent data file %S with code %d",
       
  1979                             &persistentDataPtr, persistentErr );
       
  1980                         }
       
  1981                     }
       
  1982                 
       
  1983                 // Clean up
       
  1984                 delete dir;
       
  1985                 dir = NULL;
       
  1986 
       
  1987                 delete persistentData;
       
  1988                 persistentData = NULL;
       
  1989                 }
       
  1990             
       
  1991             // Check if not in rules base folder - delete this folder
       
  1992             // if the folder is empty
       
  1993             if( aOwnerUid != KCFServerSid )
       
  1994                 {
       
  1995                 TInt rmDirErr = iFs.GetDir( driveAndPath, KEntryAttNormal,
       
  1996                     ESortNone, dir );
       
  1997                 if( rmDirErr == KErrNone && dir )
       
  1998                     {
       
  1999                     if( dir->Count() == 0 )
       
  2000                         {
       
  2001                         rmDirErr = iFs.RmDir( driveAndPath );
       
  2002                         INFO_2( "Removed directory %S with code %d",
       
  2003                             &driveAndPath, rmDirErr );
       
  2004                         }
       
  2005                     }
       
  2006                 delete dir;
       
  2007                 dir = NULL;
       
  2008                 }
       
  2009             }
       
  2010         }
       
  2011     return err;
       
  2012     }
       
  2013 
       
  2014 // End of file