sysresmonitoring/oommonitor/src/oomconfigparser.cpp
branchRCL_3
changeset 1 0fdb7f6b0309
child 2 7645e9ce10dc
equal deleted inserted replaced
0:2e3d3ce01487 1:0fdb7f6b0309
       
     1 /*
       
     2 * Copyright (c) 2006 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 parser for the OOM configuration file.
       
    15 *
       
    16 */
       
    17 #include <e32base.h>
       
    18 #include <xml/parser.h>
       
    19 
       
    20 #include "oomconfigparser.h"
       
    21 #include "oompanic.h"
       
    22 #include "oomtraces.h"
       
    23 #include "oomidletimerule.h"
       
    24 #include "oomforegroundrule.h"
       
    25 #include "oomconstants.hrh"
       
    26 #include "oomapplicationconfig.h"
       
    27 #include "oomcloseappconfig.h"
       
    28 #include "oomconfig.h"
       
    29 #include "oomrunpluginconfig.h"
       
    30 
       
    31 enum TOomConfigParserPanic
       
    32 {
       
    33 KOomErrMoreThanOneOomConfig = 0,
       
    34 KOomErrGlobalSettingsMustComeAfterRoot,
       
    35 KOomErrAppSettingsMustComeAfterGlobalSettings,
       
    36 KOomErrCloseAppSettingsMustComeAfterAppSettings,
       
    37 KOomErrAppIdleSettingsMustComeAfterAppCloseSettings,
       
    38 KOomErrLowRamErrorInGlobalSettings,
       
    39 KOomErrGoodRamErrorInGlobalSettings,
       
    40 KOomErrMaxCloseErrorInGlobalSettings,
       
    41 KOomErrDefaultPriorityErrorInGlobalSettings,
       
    42 KOomErrMissingUidFromAppCloseConfig,
       
    43 KOomErrMissingPriorityFromAppCloseConfig,
       
    44 KOomErrMissingSyncModeFromAppCloseConfig,
       
    45 KOomErrMissingEstimateFromAppCloseConfig,
       
    46 KOomErrInvalidSyncMode,
       
    47 KOomErrMissingSyncModeInAppCloseConfig,
       
    48 KOomErrBadOrMissingPriorityInAppIdleRule,
       
    49 KOomErrBadOrMissingIdleTimeInAppIdleRule,
       
    50 KOomErrBadOrMissingUidInAppIdleRule,
       
    51 KOomErrBadNeverCloseValue,
       
    52 KOomErrBadOrMissingUidInAppConfig,
       
    53 KOomErrBadOrMissingPriorityInAppCloseConfig,
       
    54 KOomErrBadLowThresholdValueForAppConfig,
       
    55 KOomErrBadGoodThresholdValueForAppConfig,
       
    56 KOomErrSystemPluginSettingsMustComeAfterAppCloseSettings,
       
    57 KOomErrAppPluginSettingsMustComeAfterSystemPluginSettings,
       
    58 KOomErrAppPluginIdleTimeRulesMustComeAfterAppPluginSettings,
       
    59 KOomErrBadOrMissingUidInAppCloseConfig,
       
    60 KOomErrBadOrMissingUidInSystemPluginConfig,
       
    61 KOomErrBadOrMissingPriorityInSystemPluginConfig,
       
    62 KOomErrBadOrMissingTargetAppIdInAppPluginConfig,
       
    63 KOomErrBadOrMissingUidInAppPluginConfig,
       
    64 KOomErrBadOrMissingPriorityInAppPluginConfig,
       
    65 KOomErrBadOrMissingPriorityInPluginIdleRule,
       
    66 KOomErrBadOrMissingIdleTimeInPluginIdleRule,
       
    67 KOomErrBadOrMissingUidInPluginIdleRule,
       
    68 KOomErrBadOrMissingUidInForegroundAppRule,
       
    69 KOomErrBadOrMissingPriorityInForegroundAppRule,
       
    70 KOomErrBadOrMissingTargetAppIdInForegroundAppRule,
       
    71 KOomErrDefaultWaitAfterPluginInGlobalSettings,
       
    72 KOomErrBadOrMissingPriorityInForceCheck,
       
    73 KOomErrOomRulesMustComeLast,
       
    74 KOomErrBadPluginWaitTime,
       
    75 KOomErrBadXml,
       
    76 KOomErrAppCloseIdleRuleOutsideAppCloseElement,
       
    77 KOomErrForegroundAppRuleOutsideAppCloseElement,
       
    78 KOomErrPluginIdleRuleOutsideAppPluginElement,
       
    79 KOomErrPluginForegroundRuleOutsidePluginElement,
       
    80 KOomErrBadCallIfTargetAppNotRunning
       
    81 };
       
    82 
       
    83 const TInt KOomXmlFileBufferSize = 1024;
       
    84 const TInt KOomMaxAppExitTime = 2000;
       
    85 const TInt KBytesInMegabyte = 1024;
       
    86 #ifdef __WINS__
       
    87 const TInt KEmulatorTickDivisor = 5; // The tick is 5 times slower on the emulator than on the phone
       
    88 #endif
       
    89 using namespace Xml;
       
    90 
       
    91 // Mime type of the parsed document
       
    92 _LIT8(KXmlMimeType, "text/xml");
       
    93 
       
    94 _LIT(KOomConfigFilePath, ":\\private\\10207218\\oomconfig.xml");
       
    95 _LIT(KRomDrive, "z");
       
    96 
       
    97 // Element strings
       
    98 // Root
       
    99 _LIT8(KOomConfigOomConfig, "oom_config");
       
   100 
       
   101 // Global settings
       
   102 _LIT8(KOomConfigGlobalSettings, "global_settings");
       
   103 _LIT8(KOomConfigForceCheckAtPriority, "force_check");
       
   104 
       
   105 // App settings
       
   106 _LIT8(KOomConfigAppSettings, "app_specific_thresholds");
       
   107 _LIT8(KOomConfigApp, "app");
       
   108 
       
   109 // App close settings
       
   110 _LIT8(KOomConfigAppCloseSettings, "app_close_settings");
       
   111 _LIT8(KOomConfigCloseApp, "close_app");
       
   112 
       
   113 // App close idle time
       
   114 _LIT8(KOomConfigAppCloseIdlePriority, "app_close_idle_priority");
       
   115 
       
   116 _LIT8(KOomConfigForegroundAppPriority, "foreground_app_priority");
       
   117 
       
   118 // Global settings attribute names
       
   119 _LIT8(KOomAttributeLowRamThreshold, "low_ram_threshold");
       
   120 _LIT8(KOomAttributeGoodRamThreshold, "good_ram_threshold");
       
   121 _LIT8(KOomAttributeMaxAppCloseBatch, "max_app_close_batch");
       
   122 _LIT8(KOomAttributeDefaultWaitAfterPlugin, "default_wait_after_plugin");
       
   123 _LIT8(KOomAttributeMaxAppExitTime , "max_app_exit_time");
       
   124 
       
   125 // System plugins 
       
   126 
       
   127 _LIT8(KOomAttributeSystemPluginSettings, "system_plugin_settings");
       
   128 _LIT8(KOomAttributeSystemPlugin, "system_plugin");
       
   129 
       
   130 // Application plugins
       
   131 
       
   132 _LIT8(KOomAttributeAppPluginSettings, "app_plugin_settings");
       
   133 _LIT8(KOomAttributeAppPlugin, "app_plugin");
       
   134 
       
   135 // Plugin idle time rules
       
   136 
       
   137 _LIT8(KOomAttributePluginIdlePriority, "plugin_idle_priority");
       
   138 
       
   139 // Plugin foreground app rules
       
   140 _LIT8(KOomAttributePluginForegroundAppPriority, "plugin_foreground_app_priority");
       
   141 
       
   142 // Atribute names
       
   143 _LIT8(KOomAttibuteUid, "uid");
       
   144 _LIT8(KOomAttibuteSyncMode, "sync_mode");
       
   145 _LIT8(KOomAttibutePriority, "priority");
       
   146 _LIT8(KOomAttibuteRamEstimate, "ram_estimate");
       
   147 
       
   148 _LIT8(KOomConfigSyncModeContinue, "continue");
       
   149 _LIT8(KOomConfigSyncModeCheck, "check");
       
   150 _LIT8(KOomConfigSyncModeEstimate, "estimate");
       
   151 
       
   152 _LIT8(KOomAttibuteIdleTime, "idle_time");
       
   153 _LIT8(KOomAttibuteIdlePriority, "priority");
       
   154 
       
   155 _LIT8(KOomAttibuteNeverClose, "NEVER_CLOSE");
       
   156 
       
   157 _LIT8(KOomAttributeTargetAppId, "target_app_id");
       
   158 
       
   159 _LIT8(KOomAttributeWait, "wait");
       
   160 
       
   161 _LIT8(KOomAttributeIfForegroundAppId, "if_foreground_app_id");
       
   162 
       
   163 _LIT8(KOomAttributeCallIfTargetAppNotRunning, "call_if_target_app_not_running");
       
   164 _LIT8(KOomAttributeTrue, "true");
       
   165 _LIT8(KOomAttributeFalse, "false");
       
   166 _LIT8(KOomAttribute0, "0");
       
   167 _LIT8(KOomAttribute1, "1");
       
   168 
       
   169 
       
   170 _LIT8(KOomConfigDefaultAppUid, "DEFAULT_APP");
       
   171 _LIT8(KOomConfigDefaultPluginUid, "DEFAULT_PLUGIN");
       
   172 _LIT8(KOomConfigTargetAppValue, "TARGET_APP");
       
   173 
       
   174 _LIT8(KOomConfigBusyAppUid, "BUSY_APP");
       
   175 _LIT8(KOomConfigHighPriorityAppUid, "HIGH_PRIORITY_APP");
       
   176 
       
   177 COomConfigParser::COomConfigParser(COomConfig& aConfig, RFs& aFs) : iConfig(aConfig), iFs(aFs), iState(EOomParsingStateNone)
       
   178     {
       
   179     }
       
   180 
       
   181 void COomConfigParser::ParseL()
       
   182     {
       
   183     FUNC_LOG;
       
   184 
       
   185     TRACES("COomConfigParser::ParseL: Parsing Config File");
       
   186     
       
   187     CParser* parser = CParser::NewLC(KXmlMimeType, *this);
       
   188     
       
   189     RFile configFile;
       
   190     TFileName configFileName;
       
   191     TChar driveChar = iFs.GetSystemDriveChar();
       
   192     configFileName.Append(driveChar);
       
   193     configFileName.Append(KOomConfigFilePath);
       
   194     if (configFile.Open(iFs, configFileName, EFileShareExclusive) != KErrNone)
       
   195         {
       
   196         configFileName.Replace(0,1,KRomDrive); //replace 'c' with 'z'
       
   197         User::LeaveIfError(configFile.Open(iFs, configFileName, EFileShareExclusive));
       
   198         }
       
   199     CleanupClosePushL(configFile);
       
   200     
       
   201     TBuf8<KOomXmlFileBufferSize> fileBuffer;
       
   202     TInt bytesRead;
       
   203     do
       
   204         {
       
   205         User::LeaveIfError(configFile.Read(fileBuffer));
       
   206         bytesRead = fileBuffer.Size();
       
   207         
       
   208         parser->ParseL(fileBuffer);
       
   209         
       
   210         } while (bytesRead != 0);
       
   211     
       
   212     CleanupStack::PopAndDestroy(2, parser); // config file - automatically closes it
       
   213                                             // parser
       
   214             
       
   215     TRACES("COomConfigParser::ParseL: Finished Parsing Config File");    
       
   216     }
       
   217 
       
   218 void COomConfigParser::OnStartDocumentL(const RDocumentParameters&, TInt)
       
   219     {
       
   220     FUNC_LOG;
       
   221     }
       
   222 
       
   223 void COomConfigParser::OnEndDocumentL(TInt)
       
   224     {
       
   225     FUNC_LOG;
       
   226     }
       
   227 
       
   228 
       
   229 void COomConfigParser::OnEndElementL(const RTagInfo&, TInt)
       
   230     {
       
   231     }
       
   232 
       
   233 void COomConfigParser::OnContentL(const TDesC8&, TInt)
       
   234     {
       
   235     }
       
   236 
       
   237 void COomConfigParser::OnStartPrefixMappingL(const RString&, const RString&, 
       
   238                                    TInt)
       
   239     {
       
   240     }
       
   241 
       
   242 void COomConfigParser::OnEndPrefixMappingL(const RString&, TInt)
       
   243     {
       
   244     }
       
   245 
       
   246 void COomConfigParser::OnIgnorableWhiteSpaceL(const TDesC8&, TInt)
       
   247     {
       
   248     }
       
   249 
       
   250 void COomConfigParser::OnSkippedEntityL(const RString&, TInt)
       
   251     {
       
   252     }
       
   253 
       
   254 void COomConfigParser::OnProcessingInstructionL(const TDesC8&, const TDesC8&, 
       
   255                                       TInt)
       
   256     {
       
   257     }
       
   258 
       
   259 void COomConfigParser::OnError(TInt)
       
   260     {
       
   261     }
       
   262 
       
   263 TAny* COomConfigParser::GetExtendedInterface(const TInt32)
       
   264     {
       
   265     return 0;
       
   266     }
       
   267 
       
   268 void COomConfigParser::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, 
       
   269                              TInt aErrorCode)
       
   270     {
       
   271     if (aErrorCode != KErrNone)
       
   272         ConfigError(KOomErrBadXml);
       
   273     
       
   274     StartElementL(aElement.LocalName().DesC(), aAttributes);
       
   275     }
       
   276 
       
   277 void COomConfigParser::StartElementL(const TDesC8& aLocalName,
       
   278                                         const RAttributeArray& aAttributes)
       
   279     {
       
   280     // Root
       
   281     if (aLocalName == KOomConfigOomConfig)
       
   282         {
       
   283         if (iState != EOomParsingStateNone)
       
   284             ConfigError(KOomErrMoreThanOneOomConfig);
       
   285         
       
   286         ChangeState(EOomParsingStateRoot);
       
   287         }
       
   288     // Parse main elements
       
   289     else if (aLocalName == KOomConfigGlobalSettings)
       
   290         {
       
   291         if (iState != EOomParsingStateRoot)
       
   292             ConfigError(KOomErrGlobalSettingsMustComeAfterRoot);
       
   293         
       
   294         SetGlobalSettings(aAttributes);
       
   295         
       
   296         ChangeState(EOomParsingStateGlobalSettings);
       
   297         }
       
   298     else if (aLocalName == KOomConfigAppSettings)
       
   299         {
       
   300         ChangeState(EOomParsingStateAppSettings);
       
   301         }
       
   302     else if (aLocalName == KOomConfigAppCloseSettings)
       
   303         {
       
   304         ChangeState(EOomParsingStateAppCloseSettings);
       
   305         }
       
   306     else if (aLocalName == KOomAttributeSystemPluginSettings)
       
   307         {
       
   308         ChangeState(EOomParsingStateSystemPluginSettings);
       
   309         }
       
   310     else if (aLocalName == KOomAttributeAppPluginSettings)
       
   311         {
       
   312         ChangeState(EOomParsingStateAppPluginSettings);
       
   313         }
       
   314     // Parse actual configuration elements
       
   315     else if (aLocalName == KOomConfigForceCheckAtPriority)
       
   316         {
       
   317         SetForceCheckConfigL(aAttributes);
       
   318         }
       
   319     else if (aLocalName == KOomConfigApp)
       
   320         {
       
   321         SetAppConfigL(aAttributes);
       
   322         }
       
   323     else if (aLocalName == KOomConfigCloseApp)
       
   324         {
       
   325         SetCloseAppConfigL(aAttributes);
       
   326         }
       
   327     else if (aLocalName == KOomConfigAppCloseIdlePriority)
       
   328         {
       
   329         CheckState(EOomParsingStateAppCloseSettings, KOomErrAppCloseIdleRuleOutsideAppCloseElement);
       
   330         SetAppCloseIdlePriorityConfigL(aAttributes);
       
   331         }
       
   332     else if (aLocalName == KOomConfigForegroundAppPriority)
       
   333         {
       
   334         CheckState(EOomParsingStateAppCloseSettings, KOomErrForegroundAppRuleOutsideAppCloseElement);
       
   335         SetForegroundAppPriorityL(aAttributes);
       
   336         }
       
   337     else if (aLocalName == KOomAttributeSystemPlugin)
       
   338         {
       
   339         SetSystemPluginConfigL(aAttributes);
       
   340         }
       
   341     else if (aLocalName == KOomAttributeAppPlugin)
       
   342         {
       
   343         SetAppPluginConfigL(aAttributes);
       
   344         }
       
   345     else if (aLocalName == KOomAttributePluginIdlePriority)
       
   346         {
       
   347         CheckState(EOomParsingStateAppPluginSettings, KOomErrPluginIdleRuleOutsideAppPluginElement);
       
   348         SetPluginIdlePriorityL(aAttributes);
       
   349         }
       
   350     else if (aLocalName == KOomAttributePluginForegroundAppPriority)
       
   351         {
       
   352         CheckState(EOomParsingStateAppPluginSettings, EOomParsingStateSystemPluginSettings, KOomErrPluginForegroundRuleOutsidePluginElement);
       
   353         SetPluginForegroundAppPriorityL(aAttributes);
       
   354         }
       
   355     
       
   356     }
       
   357 
       
   358 void COomConfigParser::ConfigError(TInt aError)
       
   359     {
       
   360     OomConfigParserPanic(aError);
       
   361     }
       
   362 
       
   363 void COomConfigParser::SetGlobalSettings(const RAttributeArray& aAttributes)
       
   364     {
       
   365     TInt defaultLowMemoryThreshold;    
       
   366     TInt err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeLowRamThreshold, defaultLowMemoryThreshold);
       
   367 
       
   368     if (err == KErrNone)
       
   369         iConfig.SetDefaultLowRamThreshold(defaultLowMemoryThreshold * KBytesInMegabyte);
       
   370     else
       
   371         ConfigError(KOomErrLowRamErrorInGlobalSettings);
       
   372     
       
   373     if (err == KErrNone)
       
   374         {
       
   375         TInt defaultGoodMemoryThreshold;    
       
   376         TInt err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeGoodRamThreshold, defaultGoodMemoryThreshold);
       
   377 
       
   378         if (err == KErrNone)
       
   379             iConfig.SetDefaultGoodRamThreshold(defaultGoodMemoryThreshold * KBytesInMegabyte);
       
   380         else
       
   381             ConfigError(KOomErrGoodRamErrorInGlobalSettings);
       
   382         }
       
   383 
       
   384     if (err == KErrNone)
       
   385         {
       
   386         TInt defaultMaxCloseAppBatch;    
       
   387         TInt err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeMaxAppCloseBatch, defaultMaxCloseAppBatch);
       
   388 
       
   389         if (err == KErrNone)
       
   390             iConfig.SetMaxCloseAppBatch(defaultMaxCloseAppBatch);
       
   391         else
       
   392             ConfigError(KOomErrMaxCloseErrorInGlobalSettings);
       
   393         }
       
   394     
       
   395     if (err == KErrNone)
       
   396         {
       
   397         TInt defaultWaitAfterPlugin;    
       
   398         TInt err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeDefaultWaitAfterPlugin, defaultWaitAfterPlugin);
       
   399 
       
   400         if (err == KErrNone)
       
   401             iConfig.SetDefaultWaitAfterPlugin(defaultWaitAfterPlugin);
       
   402         else
       
   403             ConfigError(KOomErrDefaultWaitAfterPluginInGlobalSettings);
       
   404         }
       
   405     
       
   406     if (err == KErrNone)
       
   407         {
       
   408         TInt maxAppExitTime;    
       
   409         TInt err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeMaxAppExitTime, maxAppExitTime);
       
   410 
       
   411         if (err == KErrNone)
       
   412             iConfig.SetMaxAppExitTime(maxAppExitTime);
       
   413         else
       
   414             iConfig.SetMaxAppExitTime(KOomMaxAppExitTime);
       
   415         }
       
   416     }
       
   417 
       
   418 void COomConfigParser::SetForceCheckConfigL(const RAttributeArray& aAttributes)
       
   419     {
       
   420     TUint priority;
       
   421     TInt err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibutePriority, priority);
       
   422     if (err == KErrNone)
       
   423         {
       
   424         iConfig.GlobalConfig().AddForceCheckPriorityL(priority);
       
   425         }
       
   426     else
       
   427         {
       
   428         ConfigError(KOomErrBadOrMissingPriorityInForceCheck);    
       
   429         }
       
   430     }
       
   431 
       
   432 void COomConfigParser::SetAppConfigL(const RAttributeArray& aAttributes)
       
   433     {
       
   434     TUint uid;
       
   435     COomApplicationConfig* appConfig = NULL;
       
   436     
       
   437     TInt err = GetValueFromHexAttributeList(aAttributes, KOomAttibuteUid, uid);
       
   438     
       
   439     if (err != KErrNone)
       
   440         {
       
   441         ConfigError(KOomErrBadOrMissingUidInAppConfig);
       
   442         }
       
   443     else
       
   444         iParentUid = uid;
       
   445 
       
   446     appConfig = COomApplicationConfig::NewL(uid);
       
   447     CleanupStack::PushL(appConfig);    
       
   448         
       
   449     // Set the app specific memory thresholds (if they exist)
       
   450     // Get the app specific low threshold
       
   451     if (err == KErrNone)
       
   452         {
       
   453         TUint lowThreshold;
       
   454         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeLowRamThreshold, lowThreshold);
       
   455         if (err == KErrNone)
       
   456             {
       
   457             appConfig->iLowRamThreshold = lowThreshold * KBytesInMegabyte;
       
   458             }
       
   459         else if (err == KErrNotFound)
       
   460             err = KErrNone;
       
   461         
       
   462         if (err != KErrNone)
       
   463             ConfigError(KOomErrBadLowThresholdValueForAppConfig);
       
   464         }
       
   465 
       
   466     // Get the app specific good threshold
       
   467     if (err == KErrNone)
       
   468         {
       
   469         TUint goodThreshold;
       
   470         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeGoodRamThreshold, goodThreshold);
       
   471         if (err == KErrNone)
       
   472             {
       
   473             appConfig->iGoodRamThreshold = goodThreshold * KBytesInMegabyte;
       
   474             }
       
   475         else if (err == KErrNotFound)
       
   476             err = KErrNone;
       
   477         
       
   478         if (err != KErrNone)
       
   479             ConfigError(KOomErrBadGoodThresholdValueForAppConfig);
       
   480         }
       
   481     
       
   482     // Add the applciation config to the main config
       
   483     if ((err == KErrNone) && (appConfig))
       
   484         {
       
   485         iConfig.AddApplicationConfigL(appConfig);
       
   486         }
       
   487     
       
   488     if (appConfig)
       
   489         CleanupStack::Pop(appConfig);
       
   490     }
       
   491 
       
   492 void COomConfigParser::SetCloseAppConfigL(const RAttributeArray& aAttributes)
       
   493     {
       
   494     // Get and convert uid attribute to TInt
       
   495     TInt err = KErrNone;
       
   496     
       
   497     TUint uid;
       
   498     err = GetValueFromHexAttributeList(aAttributes, KOomAttibuteUid, uid);
       
   499 
       
   500     if (err != KErrNone)
       
   501         {
       
   502         ConfigError(KOomErrBadOrMissingUidInAppCloseConfig);    
       
   503         return;
       
   504         }
       
   505     else
       
   506         iParentUid = uid;
       
   507     
       
   508     COomCloseAppConfig* closeAppConfig = COomCloseAppConfig::NewL(uid); // Radio UID
       
   509     CleanupStack::PushL(closeAppConfig);
       
   510 
       
   511      if (err == KErrNone)
       
   512         {
       
   513         // Check that we have a priority for the added app_close event
       
   514         // Specifying a priority is mandatory
       
   515         TUint priority;
       
   516         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibutePriority, priority);
       
   517         if (err == KErrNone)
       
   518             {
       
   519             closeAppConfig->SetDefaultPriority(priority);
       
   520             }
       
   521         else
       
   522             {
       
   523             ConfigError(KOomErrBadOrMissingPriorityInAppCloseConfig);    
       
   524             }
       
   525         }
       
   526     
       
   527     if (err == KErrNone)
       
   528         {
       
   529         TPtrC8 syncModeString;
       
   530         err = GetValueFromAttributeList(aAttributes, KOomAttibuteSyncMode, syncModeString);
       
   531         
       
   532         if (err == KErrNone)
       
   533             {
       
   534             TOomSyncMode syncMode = EContinue;
       
   535             
       
   536             if (syncModeString == KOomConfigSyncModeContinue)
       
   537                 syncMode = EContinue;
       
   538             else if (syncModeString == KOomConfigSyncModeCheck)
       
   539                 syncMode = ECheckRam;
       
   540             else if (syncModeString == KOomConfigSyncModeEstimate)
       
   541                 syncMode = EEstimate;
       
   542             else
       
   543                 ConfigError(KOomErrInvalidSyncMode);
       
   544             
       
   545             if (err == KErrNone)
       
   546                 {
       
   547                 closeAppConfig->iSyncMode = syncMode;
       
   548                 }
       
   549             }
       
   550         else
       
   551             {
       
   552             ConfigError(KOomErrMissingSyncModeInAppCloseConfig);
       
   553             }
       
   554         }
       
   555     
       
   556     
       
   557     if (err == KErrNone)
       
   558         {
       
   559         // If we have a default priority attribute then add it, otherwise use the global default priority
       
   560         TInt ramEstimate;
       
   561         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteRamEstimate, ramEstimate);
       
   562         if ((err == KErrNotFound) && (closeAppConfig->iSyncMode != EEstimate))
       
   563             {
       
   564             err = KErrNone;
       
   565             }
       
   566         
       
   567         if (err != KErrNone)
       
   568             ConfigError(KOomErrMissingEstimateFromAppCloseConfig);
       
   569         else
       
   570             closeAppConfig->iRamEstimate = ramEstimate * KBytesInMegabyte;
       
   571            }
       
   572 
       
   573     if (err == KErrNone)
       
   574         iConfig.SetAppCloseConfigL(closeAppConfig);
       
   575     
       
   576     CleanupStack::Pop(closeAppConfig);
       
   577     }
       
   578 
       
   579 void COomConfigParser::SetAppCloseIdlePriorityConfigL(const RAttributeArray& aAttributes)
       
   580     {
       
   581     TUint uid;
       
   582     TInt idleTime;
       
   583     TUint priority;
       
   584 
       
   585     // Use the UID from the parent scope
       
   586     uid = iParentUid;
       
   587 
       
   588     TInt err = KErrNone;
       
   589     
       
   590     err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteIdleTime, idleTime);
       
   591     
       
   592 #ifdef __WINS__
       
   593     // The tick is 5 times slower on the emulator than on the phone
       
   594     idleTime = idleTime / KEmulatorTickDivisor;
       
   595 #endif
       
   596     
       
   597     if (err != KErrNone)
       
   598         {
       
   599         ConfigError(KOomErrBadOrMissingIdleTimeInAppIdleRule);
       
   600         }
       
   601     
       
   602     if (err == KErrNone)
       
   603         {
       
   604         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteIdlePriority, priority);
       
   605         
       
   606         if (err != KErrNone)
       
   607             {
       
   608             ConfigError(KOomErrBadOrMissingPriorityInAppIdleRule);
       
   609             }
       
   610         }
       
   611     
       
   612     if (err == KErrNone)
       
   613         {
       
   614         COomIdleTimeRule* idleRule = COomIdleTimeRule::NewL(idleTime, priority);
       
   615         CleanupStack::PushL(idleRule);
       
   616         iConfig.AddApplicationRuleL(uid, idleRule);
       
   617         CleanupStack::Pop(idleRule);
       
   618         }
       
   619     }
       
   620 
       
   621 void COomConfigParser::SetForegroundAppPriorityL(const RAttributeArray& aAttributes)
       
   622     {
       
   623     TUint appUid;
       
   624     TUint targetAppId;
       
   625     TUint priority;
       
   626 
       
   627     TInt err = KErrNone;
       
   628     
       
   629     // Use the UID from the parent scope
       
   630     appUid = iParentUid;
       
   631 
       
   632     // Check that we have a priority for the added system plugin action
       
   633     // Specifying a priority is mandatory
       
   634     err = GetValueFromHexAttributeList(aAttributes, KOomAttributeIfForegroundAppId, targetAppId);
       
   635     if (err != KErrNone)
       
   636         {
       
   637         ConfigError(KOomErrBadOrMissingTargetAppIdInForegroundAppRule);    
       
   638         }
       
   639     
       
   640     if (err == KErrNone)
       
   641         {
       
   642         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteIdlePriority, priority);
       
   643         
       
   644         if (err != KErrNone)
       
   645             {
       
   646             ConfigError(KOomErrBadOrMissingPriorityInForegroundAppRule);
       
   647             }
       
   648         }
       
   649     
       
   650     if (err == KErrNone)
       
   651         {
       
   652         COomForegroundRule* foregroundRule = new (ELeave) COomForegroundRule(targetAppId, priority);
       
   653         CleanupStack::PushL(foregroundRule);
       
   654         iConfig.AddApplicationRuleL(appUid, foregroundRule);
       
   655         CleanupStack::Pop(foregroundRule);
       
   656         }
       
   657 
       
   658     }
       
   659 
       
   660 void COomConfigParser::SetSystemPluginConfigL(const RAttributeArray& aAttributes)
       
   661     {
       
   662     // Get and convert uid attribute to TInt
       
   663     TInt err = KErrNone;
       
   664     
       
   665     TUint uid;
       
   666     err = GetValueFromHexAttributeList(aAttributes, KOomAttibuteUid, uid);
       
   667 
       
   668     if (err != KErrNone)
       
   669         {
       
   670         ConfigError(KOomErrBadOrMissingUidInSystemPluginConfig);    
       
   671         return;
       
   672         }
       
   673     else
       
   674         iParentUid = uid;
       
   675     
       
   676     COomRunPluginConfig* pluginConfig = COomRunPluginConfig::NewL(uid, EOomSystemPlugin);
       
   677     CleanupStack::PushL(pluginConfig);
       
   678 
       
   679      if (err == KErrNone)
       
   680         {
       
   681         // Check that we have a priority for the added system plugin action
       
   682         // Specifying a priority is mandatory
       
   683         TUint priority;
       
   684         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibutePriority, priority);
       
   685         if (err == KErrNone)
       
   686             {
       
   687             pluginConfig->SetDefaultPriority(priority);
       
   688             }
       
   689         else
       
   690             {
       
   691             ConfigError(KOomErrBadOrMissingPriorityInSystemPluginConfig);    
       
   692             }
       
   693         }
       
   694      
       
   695      if (err == KErrNone)
       
   696         {
       
   697         TInt wait;
       
   698         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeWait, wait);
       
   699         if (err == KErrNone)
       
   700             {
       
   701             pluginConfig->SetWaitAfterPlugin(wait);
       
   702             }
       
   703         else if (err == KErrNotFound)
       
   704             {
       
   705             // If this attribute isn't present then just don't set it, and clear the error
       
   706             err = KErrNone;
       
   707             }
       
   708         else
       
   709             ConfigError(KOomErrBadPluginWaitTime);
       
   710         }
       
   711      
       
   712      if (err == KErrNone)
       
   713          {
       
   714          // Get the config for the sync mode for this plugin (if one is specified) and set it
       
   715          SetPluginSyncMode(aAttributes, *pluginConfig);
       
   716          }
       
   717 
       
   718      iConfig.AddPluginConfigL(pluginConfig);
       
   719      
       
   720      CleanupStack::Pop(pluginConfig);
       
   721     }
       
   722 
       
   723 void COomConfigParser::SetAppPluginConfigL(const RAttributeArray& aAttributes)
       
   724     {
       
   725     // Get and convert uid attribute to TInt
       
   726     TInt err = KErrNone;
       
   727     
       
   728     TUint uid;
       
   729     err = GetValueFromHexAttributeList(aAttributes, KOomAttibuteUid, uid);
       
   730 
       
   731     if (err != KErrNone)
       
   732         {
       
   733         ConfigError(KOomErrBadOrMissingUidInAppPluginConfig);    
       
   734         return;
       
   735         }
       
   736     else
       
   737         iParentUid = uid;
       
   738     
       
   739     COomRunPluginConfig* pluginConfig = COomRunPluginConfig::NewL(uid, EOomAppPlugin);
       
   740     CleanupStack::PushL(pluginConfig);
       
   741 
       
   742     if (err == KErrNone)
       
   743        {
       
   744        // Check that we have a priority for the added system plugin action
       
   745        // Specifying a priority is mandatory
       
   746         TUint priority;
       
   747         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibutePriority, priority);
       
   748         if (err == KErrNone)
       
   749             {
       
   750             pluginConfig->SetDefaultPriority(priority);
       
   751             }
       
   752         else
       
   753             {
       
   754             ConfigError(KOomErrBadOrMissingPriorityInAppPluginConfig);    
       
   755             }
       
   756        }
       
   757 
       
   758     if (err == KErrNone)
       
   759        {
       
   760        // Check that we have a priority for the added system plugin action
       
   761        // Specifying a priority is mandatory
       
   762         TUint targetAppId;
       
   763         err = GetValueFromHexAttributeList(aAttributes, KOomAttributeTargetAppId, targetAppId);
       
   764         if (err == KErrNone)
       
   765             {
       
   766             pluginConfig->SetTargetApp(targetAppId);
       
   767             iParentTargetApp = targetAppId;
       
   768             }
       
   769         else
       
   770             {
       
   771             ConfigError(KOomErrBadOrMissingTargetAppIdInAppPluginConfig);    
       
   772             }
       
   773        }
       
   774     
       
   775     if (err == KErrNone)
       
   776        {
       
   777        TInt wait;
       
   778         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttributeWait, wait);
       
   779         if (err == KErrNone)
       
   780             {
       
   781             pluginConfig->SetWaitAfterPlugin(wait);
       
   782             }
       
   783         else if (err == KErrNotFound)
       
   784             {
       
   785             // If this attribute isn't present then just don't set it, and clear the error
       
   786             err = KErrNone;
       
   787             }
       
   788         else
       
   789             ConfigError(KOomErrBadPluginWaitTime);
       
   790        }    
       
   791     
       
   792     if (err == KErrNone)
       
   793         {
       
   794         // Get the config for the sync mode for this plugin (if one is specified) and set it
       
   795         SetPluginSyncMode(aAttributes, *pluginConfig);
       
   796         
       
   797         TBool targetAppNotRunning;
       
   798         err = GetValueFromBooleanAttributeList(aAttributes, KOomAttributeCallIfTargetAppNotRunning, targetAppNotRunning);
       
   799         if (err == KErrNone)
       
   800             {
       
   801             pluginConfig->SetCallIfTargetAppNotRunning(targetAppNotRunning);
       
   802             }
       
   803         else if (err == KErrNotFound)
       
   804             {
       
   805             // If this attribute isn't present then just don't set it, and clear the error
       
   806             err = KErrNone;
       
   807             }
       
   808         else
       
   809             {
       
   810             ConfigError(KOomErrBadCallIfTargetAppNotRunning);
       
   811             }
       
   812         }
       
   813 
       
   814      iConfig.AddPluginConfigL(pluginConfig);
       
   815      
       
   816      CleanupStack::Pop(pluginConfig);
       
   817 
       
   818     }
       
   819 
       
   820 void COomConfigParser::SetPluginIdlePriorityL(const RAttributeArray& aAttributes)
       
   821     {
       
   822     TUint uid;
       
   823     TInt idleTime;
       
   824     TUint priority;
       
   825 
       
   826     TInt err = KErrNone;
       
   827     
       
   828     // Use the UID from the parent scope
       
   829     uid = iParentUid;
       
   830 
       
   831     err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteIdleTime, idleTime);
       
   832     
       
   833 #ifdef __WINS__
       
   834     // The tick is 5 times slower on the emulator than on the phone
       
   835     idleTime = idleTime / KEmulatorTickDivisor;
       
   836 #endif
       
   837     
       
   838     if (err != KErrNone)
       
   839         {
       
   840         ConfigError(KOomErrBadOrMissingIdleTimeInPluginIdleRule);
       
   841         }
       
   842     
       
   843     if (err == KErrNone)
       
   844         {
       
   845         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteIdlePriority, priority);
       
   846         
       
   847         if (err != KErrNone)
       
   848             {
       
   849             ConfigError(KOomErrBadOrMissingPriorityInPluginIdleRule);
       
   850             }
       
   851         }
       
   852     
       
   853     if (err == KErrNone)
       
   854         {
       
   855         COomIdleTimeRule* idleRule = COomIdleTimeRule::NewL(idleTime, priority);
       
   856         CleanupStack::PushL(idleRule);
       
   857         iConfig.AddPluginRuleL(uid, idleRule);
       
   858         CleanupStack::Pop(idleRule);
       
   859         }
       
   860     }
       
   861 
       
   862 void COomConfigParser::SetPluginForegroundAppPriorityL(const RAttributeArray& aAttributes)
       
   863     {
       
   864     TUint uid;
       
   865     TUint targetAppId;
       
   866     TUint priority;
       
   867 
       
   868     TInt err = KErrNone;
       
   869     
       
   870     // Use the UID from the parent scope
       
   871     uid = iParentUid;
       
   872 
       
   873         // Check that we have a priority for the added system plugin action
       
   874         // Specifying a priority is mandatory
       
   875         
       
   876     TPtrC8 targetAppString;
       
   877     err = GetValueFromAttributeList(aAttributes, KOomAttributeTargetAppId, targetAppString);
       
   878     if ((err == KErrNone)
       
   879             && (targetAppString == KOomConfigTargetAppValue)
       
   880             && (iState == EOomParsingStateAppPluginSettings))
       
   881         // If the target app is specified as "TARGET_APP" then we use the target app from the parent entry
       
   882         {
       
   883         targetAppId = iParentTargetApp;
       
   884         }
       
   885     else
       
   886         {
       
   887         err = GetValueFromHexAttributeList(aAttributes, KOomAttributeTargetAppId, targetAppId);
       
   888         if (err != KErrNone)
       
   889             {
       
   890             ConfigError(KOomErrBadOrMissingTargetAppIdInForegroundAppRule);    
       
   891             }
       
   892         }
       
   893      
       
   894     if (err == KErrNone)
       
   895         {
       
   896         err = GetValueFromDecimalAttributeList(aAttributes, KOomAttibuteIdlePriority, priority);
       
   897         
       
   898         if (err != KErrNone)
       
   899             {
       
   900             ConfigError(KOomErrBadOrMissingPriorityInForegroundAppRule);
       
   901             }
       
   902         }
       
   903     
       
   904     if (err == KErrNone)
       
   905         {
       
   906         COomForegroundRule* foregroundRule = new (ELeave) COomForegroundRule(targetAppId, priority);
       
   907         CleanupStack::PushL(foregroundRule);
       
   908         iConfig.AddPluginRuleL(uid, foregroundRule);
       
   909         CleanupStack::Pop(foregroundRule);
       
   910         }
       
   911     }
       
   912 
       
   913 // Finds an attribute of the given name and gets its value
       
   914 // A value is only valid as long as AAtrributes is valid (and unmodified)
       
   915 // Returns KErrNone if the attribute is present in the list, KErrNotFound otherwise
       
   916 TInt COomConfigParser::GetValueFromAttributeList(const RAttributeArray& aAttributes, const TDesC8& aName, TPtrC8& aValue)
       
   917     {
       
   918     TInt index = aAttributes.Count();
       
   919     TBool attributeFound = EFalse;
       
   920     while ((index--) && (!attributeFound))
       
   921         {
       
   922         if (aAttributes[index].Attribute().LocalName().DesC() == aName)
       
   923             {
       
   924             attributeFound = ETrue;
       
   925             aValue.Set(aAttributes[index].Value().DesC());
       
   926             }
       
   927         }
       
   928     
       
   929     TInt err = KErrNone;
       
   930     
       
   931     if (!attributeFound)
       
   932         err = KErrNotFound;
       
   933     
       
   934     return err;
       
   935     }
       
   936 
       
   937 // Finds an attribute of the given name and gets its value (coverting the string hex value to a UInt)
       
   938 // Returns KErrNone if the attribute is present in the list, KErrNotFound otherwise
       
   939 // Returns KErrCorrupt if the string is not a valid hex number
       
   940 TInt COomConfigParser::GetValueFromHexAttributeList(const RAttributeArray& aAttributes, const TDesC8& aName, TUint& aValue)
       
   941     {
       
   942     TPtrC8 hexString;
       
   943     TInt err = GetValueFromAttributeList(aAttributes, aName, hexString);
       
   944 
       
   945     if (hexString == KOomConfigDefaultAppUid)
       
   946         {
       
   947         // This is a special case
       
   948         // When we hit this value in a hex field then we return the default app UID
       
   949         aValue = KOomDefaultAppId;
       
   950         }
       
   951     else if (hexString == KOomConfigDefaultPluginUid)
       
   952         {
       
   953         // This is a special case
       
   954         // When we hit this value in a hex field then we return the default app UID
       
   955         aValue = KOomDefaultPluginId;
       
   956         }
       
   957     else if (hexString == KOomConfigBusyAppUid)
       
   958         {
       
   959         aValue = KOomBusyAppId;
       
   960         }
       
   961     else if (hexString == KOomConfigHighPriorityAppUid)
       
   962         {
       
   963         aValue = KOomHighPriorityAppId;
       
   964         }
       
   965     else if (err == KErrNone)
       
   966         {
       
   967         TLex8 hexLex(hexString);
       
   968         err = hexLex.Val(aValue, EHex);
       
   969         if (err != KErrNone)
       
   970             err = KErrCorrupt;
       
   971         }
       
   972     
       
   973     return err;
       
   974     }
       
   975 
       
   976 // Finds an attribute of the given name and gets its value (coverting the string decimal value to a UInt)
       
   977 // Returns KErrNone if the attribute is present in the list, KErrNotFound otherwise
       
   978 // Returns KErrCorrupt if the string is not a valid decimal number
       
   979 TInt COomConfigParser::GetValueFromDecimalAttributeList(const RAttributeArray& aAttributes, const TDesC8& aName, TUint& aValue)
       
   980     {
       
   981     TPtrC8 decimalString;
       
   982     TInt err = GetValueFromAttributeList(aAttributes, aName, decimalString);
       
   983 
       
   984     if (err == KErrNone)
       
   985         {
       
   986         if (decimalString == KOomAttibuteNeverClose)
       
   987             aValue = KOomPriorityInfinate;
       
   988         else
       
   989             {
       
   990             TLex8 decimalLex(decimalString);
       
   991             err = decimalLex.Val(aValue, EDecimal);
       
   992             if (err != KErrNone)
       
   993                 err = KErrCorrupt;
       
   994             }
       
   995         }
       
   996     
       
   997     return err;
       
   998     }
       
   999 
       
  1000 TInt COomConfigParser::GetValueFromDecimalAttributeList(const RAttributeArray& aAttributes, const TDesC8& aName, TInt& aValue)
       
  1001     {
       
  1002     TUint uintValue;
       
  1003     TInt err = GetValueFromDecimalAttributeList(aAttributes, aName, uintValue);
       
  1004     aValue = uintValue;
       
  1005     return err;
       
  1006     }
       
  1007 
       
  1008 TInt COomConfigParser::GetValueFromBooleanAttributeList(const RAttributeArray& aAttributes, const TDesC8& aName, TBool& aValue)
       
  1009     {
       
  1010     TPtrC8 ptrValue;
       
  1011     TInt err = GetValueFromAttributeList(aAttributes, aName, ptrValue);
       
  1012     if (err == KErrNone)
       
  1013         {
       
  1014         if (ptrValue == KOomAttributeTrue || ptrValue == KOomAttribute1)
       
  1015             {
       
  1016             aValue = ETrue;
       
  1017             }
       
  1018         else if (ptrValue == KOomAttributeFalse || ptrValue == KOomAttribute0)
       
  1019             {
       
  1020             aValue = EFalse;
       
  1021             }
       
  1022         else
       
  1023             {
       
  1024             err = KErrCorrupt;
       
  1025             }
       
  1026         }
       
  1027     return err;
       
  1028     }
       
  1029 
       
  1030 void COomConfigParser::SetPluginSyncMode(const RAttributeArray& aAttributes, COomRunPluginConfig& aRunPluginConfig)
       
  1031     {
       
  1032     TPtrC8 syncModeString;
       
  1033     TInt err = GetValueFromAttributeList(aAttributes, KOomAttibuteSyncMode, syncModeString);
       
  1034     
       
  1035     if (err == KErrNone)
       
  1036         // If there is no specified sync mode then leave it as the default
       
  1037         {
       
  1038         TOomSyncMode syncMode = EContinue;
       
  1039         
       
  1040         if (syncModeString == KOomConfigSyncModeContinue)
       
  1041             syncMode = EContinueIgnoreMaxBatchSize;
       
  1042         else if (syncModeString == KOomConfigSyncModeCheck)
       
  1043             syncMode = ECheckRam;
       
  1044         else if (syncModeString == KOomConfigSyncModeEstimate)
       
  1045             syncMode = EEstimate;
       
  1046         else
       
  1047             ConfigError(KOomErrInvalidSyncMode);
       
  1048         
       
  1049         if (err == KErrNone)
       
  1050             {
       
  1051             aRunPluginConfig.iSyncMode = syncMode;
       
  1052             }
       
  1053         }
       
  1054     }
       
  1055 
       
  1056 // Check that the current state is as expected
       
  1057 // If not then the specified config error is generated
       
  1058 void COomConfigParser::CheckState(TOomParsingState aExpectedState, TInt aError)
       
  1059     {
       
  1060     if (iState != aExpectedState)
       
  1061         ConfigError(aError);
       
  1062     }
       
  1063 
       
  1064 // Check that the current state is as expected
       
  1065 // If not then the specified config error is generated
       
  1066 // This version checks to ensure that the current state matches either of the passed in states
       
  1067 void COomConfigParser::CheckState(TOomParsingState aExpectedState1, TOomParsingState aExpectedState2, TInt aError)
       
  1068     {
       
  1069     if ((iState != aExpectedState1)
       
  1070             && (iState != aExpectedState2))
       
  1071         ConfigError(aError);
       
  1072     }
       
  1073 
       
  1074 void COomConfigParser::ChangeState(TOomParsingState aState)
       
  1075     {
       
  1076     iState = aState;
       
  1077     }