resourcemgmt/hwresourcesmgr/server/src/HWRMPolicy.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <s32file.h>    // For RFileReadStream
       
    21 
       
    22 #include "HWRMPolicy.h"
       
    23 #include "HWRMtrace.h"
       
    24 
       
    25 // CONSTANTS
       
    26 // id used for client ALL
       
    27 _LIT_SECURE_ID(KAllPolicySid,0x00000000);
       
    28 
       
    29 // Maximum policy file line length
       
    30 const TInt KMaxLineLength(100);
       
    31 // New line character used to terminate each line in policy file
       
    32 const TInt KNewLine('\n');
       
    33 
       
    34 // format specifier used to check for feedback clients
       
    35 _LIT8( KFeedback, "FEEDBACK" );
       
    36 
       
    37 
       
    38 // format specifier used to match valid UIDs or SIDs
       
    39 _LIT8( KMatchUid, "0x????????" );
       
    40 // match for ALL client ID
       
    41 _LIT8( KMatchAll, "ALL" );
       
    42 
       
    43 
       
    44 // ============================= LOCAL FUNCTIONS ===============================
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ===============================
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CHWRMPolicy::CHWRMPolicy
       
    50 // C++ constructor
       
    51 // (other items were commented in a header).
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CHWRMPolicy::CHWRMPolicy()
       
    55     {
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CHWRMPolicy::~CHWRMPolicy
       
    60 // Destructor
       
    61 // (other items were commented in a header).
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CHWRMPolicy::~CHWRMPolicy()
       
    65     {
       
    66     // Delete all the array objects
       
    67     iClientArray.ResetAndDestroy();
       
    68     // Delete the default policy
       
    69     if( iAllPolicyClient )
       
    70         {
       
    71         delete iAllPolicyClient;
       
    72         }
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CHWRMPolicy::NewL
       
    77 // Two-phased constructor.
       
    78 // (other items were commented in a header).
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CHWRMPolicy* CHWRMPolicy::NewL( const TDesC& aFilename )
       
    82     {
       
    83     CHWRMPolicy* self = NewLC( aFilename );
       
    84     CleanupStack::Pop();
       
    85 
       
    86     return self;
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CHWRMPolicy::NewLC
       
    91 // Two-phased constructor. Leaves instance on the cleanup stack.
       
    92 // (other items were commented in a header).
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CHWRMPolicy* CHWRMPolicy::NewLC( const TDesC& aFilename )
       
    96     {
       
    97     CHWRMPolicy* self = new( ELeave ) CHWRMPolicy();
       
    98     
       
    99     CleanupStack::PushL( self );
       
   100     self->ConstructL( aFilename );
       
   101 
       
   102     return self;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CHWRMPolicy::ConstructL
       
   107 // By default Symbian 2nd phase constructor is private.
       
   108 // (other items were commented in a header).
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CHWRMPolicy::ConstructL( const TDesC& aFilename )
       
   112     {
       
   113     RFs fs;
       
   114     User::LeaveIfError( fs.Connect() );
       
   115     CleanupClosePushL( fs );
       
   116     
       
   117     // Buffer to store the full path and filename
       
   118     TFileName filename;
       
   119     TFileName internalFilename;
       
   120     
       
   121     // Get private file path
       
   122     User::LeaveIfError( fs.PrivatePath( filename ) );
       
   123     
       
   124     // add the filename to path
       
   125     internalFilename.Append( KDrivename );            // "Z:"
       
   126     internalFilename.Append( filename );              // Private-folder of HWRM
       
   127     internalFilename.Append( KCustomerPolicyPrefix ); // "Customer" -prefix
       
   128     internalFilename.Append( aFilename );             // Actual filename
       
   129     filename.Insert(0, KDrivename);
       
   130     filename.Append( KProductPolicyPrefix ); // "Product" -prefix
       
   131     filename.Append( aFilename );
       
   132     
       
   133     // Parse the files and construct the array of policy clients
       
   134     // Add product specific file first if they need to change priorities of
       
   135     // customer defined processes.
       
   136     ParsePriorityFileL( fs, filename );
       
   137     ParsePriorityFileL( fs, internalFilename );
       
   138    
       
   139     // completed parsing the files; now add ALL client if one does not 
       
   140     // already exist (check is made in AddPolicyClientL method)
       
   141     AddPolicyClientL( KDefaultNormalPriority, KAllPolicySid, EFalse, KNullDesC8 );
       
   142 
       
   143 
       
   144     CleanupStack::PopAndDestroy(); // fs
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CHWRMPolicy::GetPriority
       
   149 // Method to retrieve the priority of a client with the given ID and if
       
   150 // it is trusted or not.
       
   151 // (other items were commented in a header).
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 TInt CHWRMPolicy::GetPriority( const TSecureId aSid, TBool& aTrusted ) const
       
   155     {
       
   156     TInt ret( KErrNotFound );
       
   157     
       
   158     // find the client
       
   159     TInt pos = FindClient( aSid );
       
   160     
       
   161     if( pos != KErrNotFound )
       
   162         {
       
   163         // if client exists return the priority
       
   164         ret = iClientArray[pos]->Priority();
       
   165         aTrusted = ETrue;
       
   166         }
       
   167     else
       
   168         {
       
   169         // client does not exist so return priority for all other clients
       
   170         ret = iAllPolicyClient->Priority();
       
   171         aTrusted = EFalse;
       
   172         }
       
   173 
       
   174     return ret;
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CHWRMPolicy::CHWRMPolicy::FeedbackClient
       
   179 // Returns ETrue if requested sid is feedback client.
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 TBool CHWRMPolicy::FeedbackClient(TSecureId aSid) const
       
   183     {
       
   184     TBool isFeedback( EFalse );
       
   185     
       
   186     // find the client
       
   187     TInt pos = FindClient( aSid );
       
   188     
       
   189     if( pos != KErrNotFound )
       
   190         {
       
   191         isFeedback = iClientArray[pos]->FeedbackClient();
       
   192         }
       
   193 
       
   194     return isFeedback;
       
   195     }
       
   196 
       
   197 
       
   198 
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CHWRMPolicy::ParsePriorityFileL
       
   202 // Method constructs the array of policy clients from the 
       
   203 // given file 
       
   204 // A valid line in the file contains priority, id and app name
       
   205 // priority must be a valid integer from KLowestPriority to KHighestPriority.
       
   206 // id must be a valid SID or ALL indicating the priority for all clients 
       
   207 //      not specified in the file.
       
   208 // app name can be any string indicating the name of the client.
       
   209 // (other items were commented in a header).
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 void CHWRMPolicy::ParsePriorityFileL(  RFs& aFs, const TDesC& aFilename )
       
   213     {
       
   214     TLex8 lex;
       
   215     TInt priority;
       
   216     TPtrC8 id;
       
   217     TSecureId sid;
       
   218     TPtrC8 feedback;
       
   219     TBool feedbackClient;
       
   220     TPtrC8 appname( KNullDesC8 );
       
   221     TInt err( KErrNone );
       
   222     // Buffer to read each line of the file into
       
   223     TBuf8<KMaxLineLength> fileline;
       
   224     TChar newLine(KNewLine);  
       
   225     RFileReadStream stream;    
       
   226     
       
   227     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPolicy::ParsePriorityFileL - Opening policy file: %S" ), &aFilename);
       
   228     
       
   229     // Open the file and attach to stream 
       
   230     err = stream.Open(aFs, aFilename, EFileRead);
       
   231     
       
   232     // Return without error if file is not found
       
   233     if ( err != KErrNone )
       
   234         {
       
   235         COMPONENT_TRACE3( _L( "HWRM Server - CHWRMPolicy::ParsePriorityFileL - Policy file open failed: %S, error: %d" ), &aFilename, err);
       
   236         }
       
   237     else
       
   238         {
       
   239         CleanupClosePushL( stream );
       
   240        
       
   241         while( err != KErrEof )
       
   242             {
       
   243             // read from the file upto the newline character
       
   244             TRAP( err, stream.ReadL( fileline, newLine ) );
       
   245 
       
   246             lex.Assign( fileline );            
       
   247             //if( err != KErrEof && lex.Val( priority ) == KErrNone )
       
   248             if( lex.Val( priority ) == KErrNone )
       
   249                 {
       
   250                 // got the priority now validate it
       
   251                 if( priority >= KHighestPriority && priority <= KLowestPriority )
       
   252                     {
       
   253                     // get the id
       
   254                     id.Set( lex.NextToken() );
       
   255                     // convert it
       
   256                     if( ConvertId( id, sid ) != KErrCorrupt )
       
   257                         {
       
   258                         feedbackClient = EFalse;
       
   259                         // check whether feedback client (may be empty)
       
   260                         feedback.Set( lex.NextToken() );
       
   261                         if( feedback.MatchF( KFeedback ) == 0 )
       
   262                             {
       
   263                             feedbackClient = ETrue;
       
   264                             }
       
   265                         // now get the appname (may be empty)
       
   266                         appname.Set( lex.NextToken() );
       
   267                         // if last token was empty and previous not, but it was
       
   268                         // not feedback token, then it must be appname
       
   269                         if( appname.Size() == 0 && (feedback.Size() != 0 && !feedbackClient) )
       
   270                             {
       
   271                             appname.Set( feedback );
       
   272                             }
       
   273                         // Create the policy
       
   274                         AddPolicyClientL( priority, sid, feedbackClient, appname );
       
   275                         }
       
   276                     }
       
   277                 }
       
   278             }
       
   279 
       
   280         // Close stream
       
   281         CleanupStack::PopAndDestroy();
       
   282     	}
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CHWRMPolicy::ConvertId
       
   287 // Helper method to convert and validate SID from a descriptor
       
   288 // (other items were commented in a header).
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 TInt CHWRMPolicy::ConvertId( const TDesC8& aSidDes, TSecureId& aSid ) const
       
   292     {
       
   293     TInt ret(KErrNone);
       
   294     TUint32 id;
       
   295 
       
   296     if( aSidDes.Match( KMatchUid ) == 0 )
       
   297         {
       
   298         // this is a matching id
       
   299         TLex8 lex( aSidDes.Right(8) );
       
   300         
       
   301         if( lex.Val( id, EHex ) != KErrNone )
       
   302             {
       
   303             // Failed to convert to int
       
   304             ret = KErrCorrupt;
       
   305             }
       
   306         else
       
   307             {
       
   308             // conversion ok so set the sid
       
   309             aSid.iId = id;
       
   310             }
       
   311         }
       
   312     else if( aSidDes.Match( KMatchAll ) == 0 )
       
   313         {
       
   314         // matching id for all remaining clients so no appname
       
   315         aSid.iId = KAllPolicySid;
       
   316         }
       
   317     else
       
   318         {
       
   319         ret = KErrCorrupt;
       
   320         }
       
   321 
       
   322     return ret;
       
   323     }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CHWRMPolicy::AddPolicyClientL
       
   327 // Adds a policy client to the array, first checks that no other 
       
   328 // client has been registered with the identical sid.
       
   329 // If one already exists KErrAlreadyExists is returned.
       
   330 // (other items were commented in a header).
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 TInt CHWRMPolicy::AddPolicyClientL( const TInt aPriority, const TSecureId& aSid,
       
   334                                     const TBool aFeedbackClient, const TDesC8& aAppName )
       
   335     {
       
   336     TInt ret(KErrNone);
       
   337     
       
   338     // search for existing client
       
   339     if( aSid == KAllPolicySid && iAllPolicyClient == NULL )
       
   340         {
       
   341         // Default policy does not exist, store seperately for efficiency
       
   342         iAllPolicyClient = CPolicyClient::NewL( aPriority, aSid, aFeedbackClient, aAppName );
       
   343         }
       
   344     else if( FindClient( aSid ) == KErrNotFound )
       
   345         {
       
   346         // does not already exist so create and add to the array
       
   347         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPolicy::AddPolicyClientL - adding client with sid=0x%x" ), aSid.iId);
       
   348         // create new client to be added to the array and put on cleanup stack
       
   349         CPolicyClient* ptrClient = 
       
   350                             CPolicyClient::NewLC( aPriority, aSid, aFeedbackClient, aAppName );
       
   351         // append the new client to the array
       
   352         User::LeaveIfError( iClientArray.Append( ptrClient ) );
       
   353         // array now owns the new client so pop it off the cleanup stack
       
   354         CleanupStack::Pop( ptrClient );
       
   355         }
       
   356     else
       
   357         {
       
   358         // client already exists so return KErrAlreadyExists
       
   359         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPolicy::AddPolicyClientL - client already exists with sid=0x%x" ), aSid.iId);
       
   360         ret = KErrAlreadyExists;
       
   361         }
       
   362     
       
   363     return ret;
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CHWRMPolicy::FindClient
       
   368 // Searches for client with the given sid.
       
   369 // (other items were commented in a header).
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 TInt CHWRMPolicy::FindClient( const TSecureId& aSid ) const
       
   373     {
       
   374     for( TInt i = 0; i < iClientArray.Count(); i++ )
       
   375         {
       
   376         if( aSid == iClientArray[i]->Sid() )
       
   377             {
       
   378             COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPolicy::FindClient - client found with sid=0x%x" ), aSid.iId);
       
   379             return i;
       
   380             }
       
   381         }
       
   382     return KErrNotFound;
       
   383     }
       
   384 
       
   385 
       
   386 // ============================ MEMBER FUNCTIONS ===============================
       
   387 // ======================= Embedded CPolicyClient class ========================
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CHWRMPolicy::CPolicyClient::NewL
       
   391 // Two-phased constructor.
       
   392 // (other items were commented in a header).
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 CHWRMPolicy::CPolicyClient* 
       
   396 CHWRMPolicy::CPolicyClient::NewL( const TInt aPriority,const TSecureId& aSid,
       
   397                                   const TBool aFeedbackClient, const TDesC8& aAppName)
       
   398     {
       
   399     CHWRMPolicy::CPolicyClient* self = NewLC( aPriority, aSid, aFeedbackClient, aAppName );
       
   400     CleanupStack::Pop();
       
   401 
       
   402     return self;
       
   403     }
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CHWRMPolicy::CPolicyClient::NewLC
       
   407 // Two-phased constructor. Leaves instance on the cleanup stack.
       
   408 // (other items were commented in a header).
       
   409 // -----------------------------------------------------------------------------
       
   410 //
       
   411 CHWRMPolicy::CPolicyClient*
       
   412 CHWRMPolicy::CPolicyClient::NewLC( const TInt aPriority, const TSecureId& aSid,
       
   413                                    const TBool aFeedbackClient, const TDesC8& aAppName)
       
   414     {
       
   415     CHWRMPolicy::CPolicyClient* self =
       
   416         new( ELeave ) CHWRMPolicy::CPolicyClient( aPriority, aSid, aFeedbackClient );
       
   417     
       
   418     CleanupStack::PushL( self );
       
   419     self->ConstructL( aAppName );
       
   420 
       
   421     return self;
       
   422     }
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // CHWRMPolicy::CPolicyClient::~CPolicyClient
       
   426 // Destructor.
       
   427 // (other items were commented in a header).
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 CHWRMPolicy::CPolicyClient::~CPolicyClient()
       
   431     {
       
   432     delete iAppName;
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CHWRMPolicy::CPolicyClient::CPolicyClient
       
   437 // C++ constructor.
       
   438 // (other items were commented in a header).
       
   439 // -----------------------------------------------------------------------------
       
   440 //
       
   441 CHWRMPolicy::CPolicyClient::CPolicyClient( TInt aPriority, 
       
   442                                            const TSecureId& aSid,
       
   443                                            const TBool aFeedbackClient)
       
   444     : iPriority( aPriority ), iSid( aSid ), iFeedbackClient( aFeedbackClient )
       
   445     {
       
   446     }
       
   447 
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CHWRMPolicy::CPolicyClient::ConstructL
       
   451 // By default Symbian 2nd phase constructor is private.
       
   452 // (other items were commented in a header).
       
   453 // -----------------------------------------------------------------------------
       
   454 //
       
   455 void CHWRMPolicy::CPolicyClient::ConstructL( const TDesC8& aAppName )
       
   456     {
       
   457     // create the descriptor for the appname
       
   458     iAppName = HBufC::NewL( aAppName.Length() );
       
   459     // and copy the 8 bit descriptor into it
       
   460     iAppName->Des().Copy(aAppName);
       
   461     }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // CHWRMPolicy::CPolicyClient::Priority
       
   465 // Returns the clients priority.
       
   466 // (other items were commented in a header).
       
   467 // -----------------------------------------------------------------------------
       
   468 //
       
   469 TInt CHWRMPolicy::CPolicyClient::Priority() const
       
   470     {
       
   471     return iPriority;
       
   472     }
       
   473 
       
   474 // -----------------------------------------------------------------------------
       
   475 // CHWRMPolicy::CPolicyClient::Id
       
   476 // Returns the clients ID.
       
   477 // (other items were commented in a header).
       
   478 // -----------------------------------------------------------------------------
       
   479 //
       
   480 TSecureId CHWRMPolicy::CPolicyClient::Sid() const
       
   481     {
       
   482     return iSid;
       
   483     }
       
   484 
       
   485 // -----------------------------------------------------------------------------
       
   486 // CHWRMPolicy::CPolicyClient::FeedbackClient
       
   487 // Returns ETrue if feedback client.
       
   488 // -----------------------------------------------------------------------------
       
   489 //
       
   490 TBool CHWRMPolicy::CPolicyClient::FeedbackClient() const
       
   491     {
       
   492     return iFeedbackClient;
       
   493     }
       
   494 
       
   495 // -----------------------------------------------------------------------------
       
   496 // CHWRMPolicy::CPolicyClient::AppName
       
   497 // Returns the client application name.
       
   498 // (other items were commented in a header).
       
   499 // -----------------------------------------------------------------------------
       
   500 //
       
   501 const TDesC& CHWRMPolicy::CPolicyClient::AppName() const
       
   502     {
       
   503     return *iAppName;
       
   504     }
       
   505 
       
   506 
       
   507 // End of File