commonservices/PlatformEnv/disknotifyhandler/src/starteddismountwatcher.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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:  Class for started dismount watcher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDES
       
    20 #include <e32std.h>
       
    21 #include <f32file.h>
       
    22 #include "starteddismountwatcher.h"
       
    23 #include "disknotifyhandler.h"
       
    24 #include "disknotifyhandlerdebug.h"
       
    25 
       
    26 
       
    27 // ======== LOCAL FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // SetEvent
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 inline static void SetEvent(
       
    34         MDiskNotifyHandlerCallback::TDismountFinishedEvent& aEvent,
       
    35         TInt aDrive,
       
    36         TBool aForcedDismount )
       
    37     {
       
    38     aEvent.iDrive = aDrive;
       
    39     aEvent.iForcedDismount = aForcedDismount;
       
    40     }
       
    41 
       
    42 // ======== MEMBER FUNCTIONS ========
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CStartedDismountWatcher::CStartedDismountWatcher
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CStartedDismountWatcher::CStartedDismountWatcher(
       
    49         MDiskNotifyHandlerCallback& aCallback,
       
    50         RFs& aFs,
       
    51         RPointerArray< CStartedDismountWatcher >& aWatcherList,
       
    52         TInt aDrive,
       
    53         TTimeIntervalMicroSeconds32 aForcedTimeout ) :
       
    54     CDiskWatcherBase( aCallback, aFs ),
       
    55     iWatcherList( aWatcherList ),
       
    56     iDrive( aDrive ),
       
    57     iForcedTimeout( aForcedTimeout )
       
    58     {
       
    59     FUNC_LOG
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CStartedDismountWatcher::ConstructL
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void CStartedDismountWatcher::ConstructL()
       
    67     {
       
    68     FUNC_LOG
       
    69 
       
    70     iWatcherList.InsertInOrderL( this, CompareFindObject );
       
    71     Activate( iForcedTimeout );
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CStartedDismountWatcher::NewL
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CStartedDismountWatcher* CStartedDismountWatcher::NewL(
       
    79         MDiskNotifyHandlerCallback& aCallback,
       
    80         RFs& aFs,
       
    81         RPointerArray< CStartedDismountWatcher >& aWatcherList,
       
    82         TInt aDrive,
       
    83         TTimeIntervalMicroSeconds32 aForcedTimeout )
       
    84     {
       
    85     FUNC_LOG
       
    86 
       
    87     CStartedDismountWatcher* self = new ( ELeave ) CStartedDismountWatcher(
       
    88         aCallback, aFs, aWatcherList, aDrive, aForcedTimeout );
       
    89     CleanupStack::PushL( self );
       
    90     self->ConstructL();
       
    91     CleanupStack::Pop( self );
       
    92     return self;
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CStartedDismountWatcher::~CStartedDismountWatcher
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 CStartedDismountWatcher::~CStartedDismountWatcher()
       
   100     {
       
   101     FUNC_LOG
       
   102 
       
   103     Cancel();
       
   104     delete iForcedDismountTimer;
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CStartedDismountWatcher::CompareFind
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 TInt CStartedDismountWatcher::CompareFind(
       
   112         const TInt* aDrive,
       
   113         const CStartedDismountWatcher& aWatcher )
       
   114     {
       
   115     return Compare( *aDrive, aWatcher.iDrive );
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CStartedDismountWatcher::CompareFindObject
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 TInt CStartedDismountWatcher::CompareFindObject(
       
   123         const CStartedDismountWatcher& aWatcher1,
       
   124         const CStartedDismountWatcher& aWatcher2 )
       
   125     {
       
   126     return Compare( aWatcher1.iDrive, aWatcher2.iDrive );
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CStartedDismountWatcher::WatcherError
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void CStartedDismountWatcher::WatcherError(
       
   134         TInt aError,
       
   135         const TBool& aKilled )
       
   136     {
       
   137     FUNC_LOG
       
   138 
       
   139     LOG_IF_ERROR2(
       
   140         aError,
       
   141         "CStartedDismountWatcher::WatcherError-iDrive=%d,aError=%d",
       
   142         iDrive, aError )
       
   143 
       
   144     NotifyDismount( aError, aKilled );
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // CStartedDismountWatcher::ReactivateWatcher
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CStartedDismountWatcher::ReactivateWatcher()
       
   152     {
       
   153     FUNC_LOG
       
   154 
       
   155     // Automatic reactivation is not required
       
   156     }
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // CStartedDismountWatcher::RunWatcher
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 void CStartedDismountWatcher::RunWatcher( const TBool& aKilled )
       
   163     {
       
   164     FUNC_LOG
       
   165 
       
   166     INFO_LOG1( "CStartedDismountWatcher::RunWatcher-iDrive=%d", iDrive )
       
   167 
       
   168     NotifyDismount( KErrNone, aKilled );
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CStartedDismountWatcher::CancelWatcher
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CStartedDismountWatcher::CancelWatcher()
       
   176     {
       
   177     FUNC_LOG
       
   178 
       
   179     INFO_LOG1( "CDismountWatcher::CStartedDismountWatcher-iDrive=%d", iDrive )
       
   180 
       
   181     delete iForcedDismountTimer;
       
   182     iForcedDismountTimer = NULL;
       
   183     Fs().NotifyDismountCancel( iStatus );
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CStartedDismountWatcher::Activate
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CStartedDismountWatcher::Activate( TTimeIntervalMicroSeconds32 aForcedTimeout )
       
   191     {
       
   192     FUNC_LOG
       
   193 
       
   194     if ( !IsActive() )
       
   195         {
       
   196         INFO_LOG1( "CStartedDismountWatcher::Activate-iDrive=%d", iDrive )
       
   197 
       
   198         iForcedTimeout = aForcedTimeout;
       
   199         delete iForcedDismountTimer;
       
   200         iForcedDismountTimer = NULL;
       
   201         iForcedDismount = EFalse;
       
   202 
       
   203         // Start forced timer if needed
       
   204         TInt err( KErrNone );
       
   205         if ( iForcedTimeout.Int() > 0 )
       
   206             {
       
   207             iForcedDismountTimer = CPeriodic::New( CActive::EPriorityHigh );
       
   208             if ( iForcedDismountTimer )
       
   209                 {
       
   210                 iForcedDismountTimer->Start( iForcedTimeout, iForcedTimeout,
       
   211                     TCallBack( ForcedDismountCB, this ) );
       
   212                 }
       
   213             else
       
   214                 {
       
   215                 err = KErrNoMemory;
       
   216                 }
       
   217             }
       
   218 
       
   219         if ( err == KErrNone )
       
   220             {
       
   221             // Do immediate forced dismount if timeout is zero
       
   222             if ( iForcedTimeout.Int() == 0 )
       
   223                 {
       
   224                 iForcedDismount = ETrue;
       
   225                 Fs().NotifyDismount( iDrive, iStatus, EFsDismountForceDismount );
       
   226                 }
       
   227             else
       
   228                 {
       
   229                 Fs().NotifyDismount( iDrive, iStatus, EFsDismountNotifyClients );
       
   230                 }
       
   231             }
       
   232         else
       
   233             {
       
   234             ERROR_LOG1( "CStartedDismountWatcher::Activate-err=%d", err )
       
   235             TRequestStatus* status = &iStatus;
       
   236             User::RequestComplete( status, err );
       
   237             }
       
   238         SetActive();
       
   239         }
       
   240     }
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // CStartedDismountWatcher::RemoveFromListAndDestroy
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CStartedDismountWatcher::RemoveFromListAndDestroy()
       
   247     {
       
   248     FUNC_LOG
       
   249 
       
   250     TInt i( iWatcherList.FindInOrder( this, CompareFindObject ) );
       
   251     if ( i >= 0 && i < iWatcherList.Count() )
       
   252         {
       
   253         INFO_LOG2(
       
   254             "CStartedDismountWatcher::RemoveFromListAndDestroy-iDrive=%d,i=%d",
       
   255             iDrive, i )
       
   256         iWatcherList.Remove( i );
       
   257         delete this;
       
   258         }
       
   259     }
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // CStartedDismountWatcher::ForcedDismountCB
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 TInt CStartedDismountWatcher::ForcedDismountCB( TAny* aPtr )
       
   266     {
       
   267     FUNC_LOG
       
   268 
       
   269     CStartedDismountWatcher* self = static_cast<CStartedDismountWatcher*>( aPtr );
       
   270     if ( self )
       
   271         {
       
   272         self->RequestForcedDismount();
       
   273         }
       
   274     else
       
   275         {
       
   276         ERROR_LOG( "CStartedDismountWatcher::ForcedDismountCB-NULL" )
       
   277         }
       
   278     return KErrNone;
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CStartedDismountWatcher::RequestForcedDismount
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CStartedDismountWatcher::RequestForcedDismount()
       
   286     {
       
   287     FUNC_LOG
       
   288 
       
   289     // Cancel forced dismount timer
       
   290     delete iForcedDismountTimer;
       
   291     iForcedDismountTimer = NULL;
       
   292 
       
   293     // Cancel soft dismount
       
   294     Cancel();
       
   295 
       
   296     iForcedDismount = ETrue;
       
   297     Fs().NotifyDismount( iDrive, iStatus, EFsDismountForceDismount );    
       
   298     SetActive();
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // CStartedDismountWatcher::NotifyDismount
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 void CStartedDismountWatcher::NotifyDismount( TInt aError, const TBool& aKilled )
       
   306     {
       
   307     FUNC_LOG
       
   308 
       
   309     delete iForcedDismountTimer;
       
   310     iForcedDismountTimer = NULL;
       
   311 
       
   312     MDiskNotifyHandlerCallback::TDismountFinishedEvent event;
       
   313     SetEvent( event, iDrive, iForcedDismount );
       
   314     Callback().HandleNotifyDismountFinished( aError, event );
       
   315     if ( aKilled )
       
   316         {
       
   317         return;
       
   318         }
       
   319     // Remove useless watcher if not manually activated from callback
       
   320     if ( !IsActive() )
       
   321         {
       
   322         RemoveFromListAndDestroy();
       
   323         }
       
   324     }
       
   325 
       
   326 // End of File