crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianDebugLib/PluginAPI/DbgPluginPrimer.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.Text;
       
    20 using SymbianUtils;
       
    21 using SymbianUtils.PluginManager;
       
    22 using SymbianStructuresLib.Arm;
       
    23 using SymbianStructuresLib.Arm.Instructions;
       
    24 using SymbianDebugLib.Engine;
       
    25 using SymbianDebugLib.Entity;
       
    26 
       
    27 namespace SymbianDebugLib.PluginAPI
       
    28 {
       
    29     public abstract class DbgPluginPrimer
       
    30     {
       
    31         #region Enumerations
       
    32         public enum TPrimeEvent
       
    33         {
       
    34             EEventPrimingStarted = 0,
       
    35             EEventPrimingProgress,
       
    36             EEventPrimingComplete
       
    37         }
       
    38         #endregion
       
    39 
       
    40         #region Delegates & events
       
    41         public delegate void PrimeEventHandler( TPrimeEvent aEvent, object aData );
       
    42         public event PrimeEventHandler EventHandler;
       
    43         #endregion
       
    44 
       
    45         #region Constructors
       
    46         protected DbgPluginPrimer( DbgPluginEngine aEngine )
       
    47 		{
       
    48             iEngine = aEngine;
       
    49 		}
       
    50 		#endregion
       
    51 
       
    52 		#region Framework API
       
    53         public abstract void Add( DbgEntity aEntity );
       
    54 
       
    55         public abstract void Prime( TSynchronicity aSynchronicity );
       
    56 
       
    57         protected abstract int Count
       
    58         {
       
    59             get;
       
    60         }
       
    61         #endregion
       
    62 
       
    63         #region Properties
       
    64         public bool ResetEngineBeforePriming
       
    65         {
       
    66             get { return iResetEngineBeforePriming; }
       
    67             set { iResetEngineBeforePriming = value; }
       
    68         }
       
    69 
       
    70         protected bool IsComplete
       
    71         {
       
    72             get
       
    73             {
       
    74                 int completedCount = 0;
       
    75                 lock ( iCompleted )
       
    76                 {
       
    77                     completedCount = iCompleted.Count;
       
    78                 }
       
    79                 int totalCount = Count;
       
    80                 bool primeCompleted = ( completedCount == totalCount );
       
    81                 return primeCompleted;
       
    82             }
       
    83         }
       
    84         #endregion
       
    85 
       
    86         #region Framework API
       
    87         protected DbgPluginEngine Engine
       
    88         {
       
    89             get { return iEngine; }
       
    90         }
       
    91         #endregion
       
    92 
       
    93         #region Internal framework methods
       
    94         protected virtual void OnPrepareToPrime()
       
    95         {
       
    96             lock ( iCompleted )
       
    97             {
       
    98                 iCompleted.Clear();
       
    99             }
       
   100             lock ( iSourceProgressValues )
       
   101             {
       
   102                 iSourceProgressValues.Clear();
       
   103             }
       
   104             iLastReportedProgress = -1;
       
   105         }
       
   106 
       
   107         protected virtual void OnPrimeComplete()
       
   108         {
       
   109             lock ( iSourceProgressValues )
       
   110             {
       
   111                 iSourceProgressValues.Clear();
       
   112             }
       
   113             lock ( iCompleted )
       
   114             {
       
   115                 iCompleted.Clear();
       
   116             }
       
   117             //
       
   118             ReportEvent( TPrimeEvent.EEventPrimingComplete, null );
       
   119         }
       
   120 
       
   121         protected void ReportProgressIfNeeded( bool aEntireOperationComplete )
       
   122         {
       
   123             int newProgress = ( aEntireOperationComplete ? 100 : TotalProgress );
       
   124             if ( newProgress > iLastReportedProgress )
       
   125             {
       
   126                 // Set to max completion value if all sources are finished.
       
   127                 if ( aEntireOperationComplete )
       
   128                 {
       
   129                     newProgress = 100;
       
   130                 }
       
   131 
       
   132                 iLastReportedProgress = newProgress;
       
   133                 ReportEvent( TPrimeEvent.EEventPrimingProgress, newProgress );
       
   134             }
       
   135         }
       
   136 
       
   137         protected bool AddToCompleted( object aEntity )
       
   138         {
       
   139             lock ( iCompleted )
       
   140             {
       
   141                 iCompleted.Add( aEntity );
       
   142             }
       
   143             //
       
   144             bool primeCompleted = primeCompleted = this.IsComplete;
       
   145             SaveLatestProgress( aEntity, 100 );
       
   146             return primeCompleted;
       
   147         }
       
   148 
       
   149         protected void RemoveFromCompleted( object aEntity )
       
   150         {
       
   151             lock ( iCompleted )
       
   152             {
       
   153                 iCompleted.Remove( aEntity );
       
   154             }
       
   155             lock ( iSourceProgressValues )
       
   156             {
       
   157                 if ( iSourceProgressValues.ContainsKey( aEntity ) )
       
   158                 {
       
   159                     iSourceProgressValues.Remove( aEntity );
       
   160                 }
       
   161             }
       
   162         }
       
   163 
       
   164         protected void SaveLatestProgress( object aEntity, int aProgress )
       
   165         {
       
   166             lock ( iSourceProgressValues )
       
   167             {
       
   168                 if ( !iSourceProgressValues.ContainsKey( aEntity ) )
       
   169                 {
       
   170                     iSourceProgressValues.Add( aEntity, aProgress );
       
   171                 }
       
   172                 else
       
   173                 {
       
   174                     iSourceProgressValues[ aEntity ] = aProgress;
       
   175                 }
       
   176             }
       
   177         }
       
   178 
       
   179         protected void ReportEvent( TPrimeEvent aEvent, object aData )
       
   180         {
       
   181             if ( EventHandler != null )
       
   182             {
       
   183                 EventHandler( aEvent, aData );
       
   184             }
       
   185         }
       
   186 
       
   187         protected int TotalProgress
       
   188         {
       
   189             get
       
   190             {
       
   191                 long combined = 0;
       
   192                 //
       
   193                 lock ( iSourceProgressValues )
       
   194                 {
       
   195                     foreach ( KeyValuePair<object, int> kvp in iSourceProgressValues )
       
   196                     {
       
   197                         combined += kvp.Value;
       
   198                     }
       
   199                 }
       
   200                 
       
   201                 // Scale by number of objects that are reporting progress
       
   202                 int count = Count;
       
   203                 long maxCompletionValue = ( count * 100 );
       
   204                 //
       
   205                 float scaled = ((float) combined ) / (float) maxCompletionValue;
       
   206                 return (int) ( scaled * 100.0f );
       
   207             }
       
   208         }
       
   209         #endregion
       
   210 
       
   211         #region Data members
       
   212         private readonly DbgPluginEngine iEngine;
       
   213         private List<object> iCompleted = new List<object>();
       
   214         private Dictionary<object, int> iSourceProgressValues = new Dictionary<object, int>();
       
   215         private int iLastReportedProgress = -1;
       
   216         private bool iResetEngineBeforePriming = false;
       
   217         #endregion
       
   218     }
       
   219 }