upnpavcontroller/upnpavcontrollerhelper/src/upnplocalitemresolver.cpp
changeset 0 7f85d04be362
child 38 5360b7ddc251
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2007 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:      Resolver for local items
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 // upnp stack api's
       
    25 #include <upnpitem.h>
       
    26 
       
    27 // upnpframework / avcontroller api
       
    28 #include "upnpavcontroller.h" // avcontroller service
       
    29 #include "upnpavbrowsingsession.h" // browsing session
       
    30 #include "upnpavdevice.h" // device (for creating a session)
       
    31 #include <upnpfilesharing.h>
       
    32 
       
    33 // upnpframework / avcontroller helper api
       
    34 #include "upnpitemresolverfactory.h" // optimisation flags
       
    35 #include "upnpitemresolverobserver.h" // MUPnPItemResolverObserver
       
    36 #include "upnpfileutility.h" // IsFileProtected
       
    37 #include "upnpresourceselector.h" // MUPnPResourceSelector
       
    38 
       
    39 // upnpframework / internal api's
       
    40 #include "upnpmetadatafetcher.h" // CreateItemFromFileLC
       
    41 #include "upnpcommonutils.h" // ReplacePlaceHolderInURIL
       
    42 
       
    43 // avcontrollerhelper internal
       
    44 #include "upnplocalitemresolver.h"
       
    45 
       
    46 #include "upnpconstantdefs.h" // for upnp-specific stuff
       
    47 #include "upnpitemutility.h" // for GetResElements
       
    48 #include "upnpsecaccesscontroller.h" // CUpnpSecAccessController
       
    49 #include "upnpperiodic.h"
       
    50 
       
    51 _LIT( KComponentLogfile, "upnpavcontrollerhelper.txt");
       
    52 #include "upnplog.h"
       
    53 
       
    54 // CONSTANTS
       
    55 const TInt KCancelWaitMaximum =   4000000;
       
    56 const TInt KCancelWaitResolution = 500000;
       
    57 const TInt KUnshareWait =  1000000;
       
    58 
       
    59 // METHODS
       
    60 
       
    61 // --------------------------------------------------------------------------
       
    62 // CUPnPLocalItemResolver:: NewL
       
    63 // See upnplocalitemresolver.h
       
    64 //---------------------------------------------------------------------------
       
    65 CUPnPLocalItemResolver* CUPnPLocalItemResolver::NewL(
       
    66     const TDesC& aFilePath,
       
    67     MUPnPAVController& aAvController,
       
    68     MUPnPResourceSelector& aSelector,
       
    69     TInt aOptimisationFlags    )
       
    70     {
       
    71     CUPnPLocalItemResolver* self =
       
    72         new (ELeave )CUPnPLocalItemResolver(
       
    73         aAvController, aSelector, aOptimisationFlags );
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL( aFilePath );
       
    76     CleanupStack::Pop( self );
       
    77     return self;
       
    78     }
       
    79 
       
    80 // --------------------------------------------------------------------------
       
    81 // CUPnPLocalItemResolver::CUPnPLocalItemResolver
       
    82 // See upnplocalitemresolver.h
       
    83 //---------------------------------------------------------------------------
       
    84 CUPnPLocalItemResolver::CUPnPLocalItemResolver(
       
    85     MUPnPAVController& aAvController,
       
    86     MUPnPResourceSelector& aSelector,
       
    87     TInt aOptimisationFlags )
       
    88     : CActive( EPriorityStandard )
       
    89     , iAvController( aAvController )
       
    90     , iSelector( aSelector )
       
    91     {
       
    92     CActiveScheduler::Add( this );
       
    93     iOptimisationFlags = aOptimisationFlags;
       
    94     iState = EStateIdle;
       
    95     }
       
    96 
       
    97 
       
    98 // --------------------------------------------------------------------------
       
    99 // CUPnPLocalItemResolver::ConstructL
       
   100 // See upnplocalitemresolver.h
       
   101 //---------------------------------------------------------------------------
       
   102 void CUPnPLocalItemResolver::ConstructL(
       
   103     const TDesC& aFilePath )
       
   104     {
       
   105     __LOG1( "LocalItemResolver:ConstructL() 0x%d", TInt(this) );
       
   106     iFilePath = aFilePath.AllocL();
       
   107     iFileSharing = CUpnpFileSharing::NewL();    
       
   108     iAccessController = CUpnpSecAccessController::NewL();
       
   109     iWait = new (ELeave) CActiveSchedulerWait();
       
   110     iTimer = CUPnPPeriodic::NewL( CActive::EPriorityHigh );
       
   111     }
       
   112 
       
   113 
       
   114 // --------------------------------------------------------------------------
       
   115 // CUPnPLocalItemResolver::~CUPnPLocalItemResolver
       
   116 // See upnplocalitemresolver.h
       
   117 //---------------------------------------------------------------------------
       
   118 CUPnPLocalItemResolver::~CUPnPLocalItemResolver()
       
   119     {
       
   120     __LOG1( "LocalItemResolver destructor 0x%d", TInt(this) );
       
   121     
       
   122     Cleanup();
       
   123     
       
   124     if ( iTempSession )
       
   125         {
       
   126         iTempSession->RemoveObserver();
       
   127         iAvController.StopBrowsingSession( *iTempSession );
       
   128         }
       
   129 
       
   130     delete iFileSharing;
       
   131     iFileSharing = NULL;
       
   132     
       
   133     // cancel any async calls
       
   134     if ( IsActive() )
       
   135         {
       
   136          __LOG( "LocalItemResolver destructor RequestComplete" );
       
   137         TRequestStatus* stat = &iStatus;
       
   138         User::RequestComplete( stat, KErrNone );
       
   139         }
       
   140     
       
   141     Cancel();
       
   142       
       
   143     delete iFilePath;
       
   144     iFilePath = NULL;
       
   145     
       
   146     delete iAccessController;
       
   147     iAccessController = NULL;
       
   148     
       
   149     delete iSharedItem;
       
   150     iSharedItem = NULL;
       
   151         
       
   152     if( iTimer )
       
   153         {
       
   154         iTimer->Cancel();
       
   155         delete iTimer;
       
   156         iTimer = NULL;
       
   157         }
       
   158     
       
   159     if( iWait->IsStarted() )
       
   160         {
       
   161         iWait->AsyncStop();
       
   162         }
       
   163     delete iWait;
       
   164     iWait = NULL;
       
   165         
       
   166     __LOG( "LocalItemResolver destructor end" );
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------
       
   170 // CUPnPLocalItemResolver::ResolveL
       
   171 // See upnplocalitemresolver.h
       
   172 //---------------------------------------------------------------------------
       
   173 void CUPnPLocalItemResolver::ResolveL(
       
   174     MUPnPItemResolverObserver& aObserver )
       
   175     {
       
   176     __LOG1( "LocalItemResolver:Resolve() 0x%d", TInt(this) );
       
   177     __ASSERTD( iState == EStateIdle, __FILE__, __LINE__ );
       
   178     iObserver = &aObserver;
       
   179 
       
   180     if ( iOptimisationFlags & UPnPItemResolverFactory::EOmitDrmCheck )
       
   181         {
       
   182         // no need to check DRM ! this branch is empty.
       
   183         }
       
   184     else
       
   185         {
       
   186         // check DRM
       
   187         if ( UPnPFileUtility::IsFileProtectedL( iFilePath->Des() ) )
       
   188             {
       
   189             User::Leave( KErrNotSupported );
       
   190             }
       
   191         }
       
   192 
       
   193     // create item metadata
       
   194     iState = EStateCreatingItem;
       
   195     CUpnpItem* item = UPnPMetadataFetcher::CreateItemFromFileLC(
       
   196         iFilePath->Des() );
       
   197     CleanupStack::Pop( item );
       
   198     __LOG1( "LocalItemResolver:Resolve CreateItemFromFileLC done 0x%d", TInt(this) );
       
   199     if( iSharedItem )
       
   200         {
       
   201         delete iSharedItem;
       
   202         iSharedItem = 0;
       
   203         }
       
   204     iSharedItem = item;
       
   205 
       
   206     // state check
       
   207     if ( iState == EStateCreatingItem )
       
   208         {
       
   209         // start local mediaserver
       
   210         if ( iOptimisationFlags & UPnPItemResolverFactory::EOmitLocalMSStart )
       
   211             {
       
   212             // omit mediaserver start - go directly to sharing.
       
   213             DoShareL();
       
   214             }
       
   215         else
       
   216             {
       
   217             // start a session for local MS keepalive
       
   218             CUpnpAVDevice* dummyDevice = CUpnpAVDevice::NewLC();
       
   219             dummyDevice->SetUuidL( KNullDesC8 );
       
   220             dummyDevice->SetDeviceType(CUpnpAVDevice::EMediaServer);    
       
   221             iTempSession = &iAvController.StartBrowsingSessionL( *dummyDevice );
       
   222             CleanupStack::PopAndDestroy( dummyDevice );
       
   223 
       
   224             iTempSession->SetObserver( *this );
       
   225             // request for start local MS
       
   226             iState = EStateStartingMS;
       
   227             iTempSession->ReserveLocalMSServicesL();
       
   228             }
       
   229         
       
   230         }
       
   231     else
       
   232         {
       
   233         __LOG( "LocalItemResolver: create item interrupted" );
       
   234 		iState = EStateIdle;
       
   235         }
       
   236         
       
   237     __LOG( "LocalItemResolver:Resolve() END" );
       
   238     }
       
   239 
       
   240 
       
   241 // --------------------------------------------------------------------------
       
   242 // CUPnPLocalItemResolver::DoCancel
       
   243 // See upnplocalitemresolver.h
       
   244 // --------------------------------------------------------------------------
       
   245 void CUPnPLocalItemResolver::DoCancel()
       
   246     {
       
   247     }
       
   248 
       
   249 // --------------------------------------------------------------------------
       
   250 // CUPnPLocalItemResolver::RunError
       
   251 // See upnplocalitemresolver.h
       
   252 // --------------------------------------------------------------------------
       
   253 TInt CUPnPLocalItemResolver::RunError( TInt aError )
       
   254     {
       
   255     __LOG1( "CUPnPLocalItemResolver::RunError %d", aError );
       
   256     // should never be here.
       
   257     __PANICD( __FILE__, __LINE__ );
       
   258     return KErrNone;
       
   259     }
       
   260 
       
   261 // --------------------------------------------------------------------------
       
   262 // CUPnPLocalItemResolver::RunL
       
   263 // See upnplocalitemresolver.h
       
   264 // --------------------------------------------------------------------------
       
   265 void CUPnPLocalItemResolver::RunL()
       
   266     {
       
   267     __LOG2( "CUPnPLocalItemResolver::RunL iStatus=%d, iState=%d", 
       
   268         iStatus.Int(), iState );
       
   269 
       
   270 
       
   271 	if ( iState == EStateSharing )
       
   272 		{
       
   273 		// If the sharing failed
       
   274         if( iStatus.Int() != KErrNone )
       
   275             {
       
   276             // Deny access to the files listed in res-elements
       
   277             SetAccesstoItemResources( *iSharedItem, EFalse );
       
   278             }
       
   279         
       
   280         if( iStatus.Int() == KErrNone )
       
   281     		{
       
   282     	    // replace the IP address in the URI
       
   283     	    __LOG( "LocalItemResolver:replacing IP in URI" );
       
   284     	    TInetAddr address;
       
   285     	    User::LeaveIfError( iMediaServer.Connect() );
       
   286     	    iMediaServer.GetAddress( address );
       
   287     	    UPnPCommonUtils::ReplacePlaceHolderInURIL(
       
   288     	        *iSharedItem, address );
       
   289     	    iMediaServer.Close();
       
   290 
       
   291     	    // select the resource
       
   292     	    iResource = &iSelector.SelectResourceL( *iSharedItem );
       
   293             }
       
   294                
       
   295         Complete( iStatus.Int() );
       
   296 		}
       
   297 		
       
   298     else if ( iState == EStateUnsharing )
       
   299 		{	
       
   300 		iTimer->Cancel();
       
   301 		
       
   302         // Deny access to the files listed in res-elements
       
   303         SetAccesstoItemResources( *iSharedItem, EFalse );
       
   304         
       
   305         iResource = 0; // NOTE: no deletion !
       
   306         iState = EStateIdle;
       
   307         
       
   308         iWait->AsyncStop();
       
   309         }
       
   310         
       
   311     __LOG( "CUPnPLocalItemResolver::RunL end" );
       
   312     }
       
   313     
       
   314     
       
   315 // --------------------------------------------------------------------------
       
   316 // CUPnPLocalItemResolver::ReserveLocalMSServicesCompleted
       
   317 // See upnplocalitemresolver.h
       
   318 //---------------------------------------------------------------------------
       
   319 void CUPnPLocalItemResolver::ReserveLocalMSServicesCompleted(
       
   320     TInt aError )
       
   321     {
       
   322     __LOG( "LocalItemResolver:MSServicesComplete" );
       
   323     __ASSERTD( iState == EStateStartingMS, __FILE__, __LINE__ );
       
   324 
       
   325     if ( aError == KErrNone )
       
   326         {
       
   327         TRAPD( err, DoShareL() );
       
   328         if( err != KErrNone )
       
   329             {
       
   330             __LOG1( "LocalItemResolver:MSServicesComplete\
       
   331  DoShareL failed %d", err );
       
   332             }
       
   333         }
       
   334     else
       
   335         {
       
   336         Complete( aError );
       
   337         }
       
   338     }
       
   339 
       
   340 // --------------------------------------------------------------------------
       
   341 // CUPnPLocalItemResolver::DoShareL
       
   342 // See upnplocalitemresolver.h
       
   343 //---------------------------------------------------------------------------
       
   344 void CUPnPLocalItemResolver::DoShareL()
       
   345     {
       
   346     __LOG( "CUPnPLocalItemResolver::DoShareL start" );
       
   347     // share the item
       
   348     iState = EStateSharing;
       
   349     
       
   350     // Allow access to the files listed in res-elements
       
   351     SetAccesstoItemResources( *iSharedItem, ETrue );
       
   352     
       
   353     // Share the item
       
   354     iFileSharing->ShareItemL( KContainerIdRoot, *iSharedItem, iStatus );
       
   355         
       
   356     SetActive();
       
   357     __LOG( "CUPnPLocalItemResolver::DoShareL end" );
       
   358     }
       
   359 
       
   360 // --------------------------------------------------------------------------
       
   361 // CUPnPLocalItemResolver::DoUnshareL
       
   362 // See upnplocalitemresolver.h
       
   363 // --------------------------------------------------------------------------
       
   364 void CUPnPLocalItemResolver::DoUnshareL()
       
   365     {
       
   366     __LOG( "CUPnPLocalItemResolver::DoUnshareL" );
       
   367 
       
   368     iState = EStateUnsharing;
       
   369     
       
   370     TInt id;
       
   371     TLex8 idParser( iSharedItem->Id() );
       
   372     TInt status = idParser.Val( id );
       
   373     if ( status == KErrNone )
       
   374         {
       
   375         iFileSharing->UnshareItemL( id, iStatus );
       
   376 
       
   377         SetActive();
       
   378         }
       
   379     else
       
   380         {
       
   381         __LOG1( "CUPnPLocalItemResolver::DoUnshareL leave %d", status );
       
   382         User::Leave( status );
       
   383         }
       
   384         
       
   385     __LOG( "CUPnPLocalItemResolver::DoUnshareL - end" );
       
   386     }
       
   387 
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // CUPnPLocalItemResolver::Item
       
   391 // See upnplocalitemresolver.h
       
   392 //---------------------------------------------------------------------------
       
   393 const CUpnpItem& CUPnPLocalItemResolver::Item() const
       
   394     {
       
   395     __ASSERTD( iState == EStateReady, __FILE__, __LINE__ );
       
   396     __ASSERTD( iSharedItem, __FILE__, __LINE__ );
       
   397 
       
   398     return *iSharedItem;
       
   399     }
       
   400 
       
   401 
       
   402 // --------------------------------------------------------------------------
       
   403 // CUPnPLocalItemResolver::Resource
       
   404 // See upnplocalitemresolver.h
       
   405 //---------------------------------------------------------------------------
       
   406 const CUpnpElement& CUPnPLocalItemResolver::Resource() const
       
   407     {
       
   408     __ASSERTD( iState == EStateReady, __FILE__, __LINE__ );
       
   409     __ASSERTD( iResource, __FILE__, __LINE__ );
       
   410 
       
   411     return *iResource;
       
   412     }
       
   413 
       
   414 // --------------------------------------------------------------------------
       
   415 // CUPnPLocalItemResolver::Complete
       
   416 // See upnplocalitemresolver.h
       
   417 //---------------------------------------------------------------------------
       
   418 void CUPnPLocalItemResolver::Complete( TInt aError )
       
   419     {
       
   420     __LOG1( "LocalItemResolver:Complete() %d", aError );
       
   421     __ASSERTD( iState == EStateStartingMS || 
       
   422         iState == EStateSharing, __FILE__, __LINE__ );
       
   423         
       
   424     MUPnPItemResolverObserver& observer = *iObserver;
       
   425     iObserver = 0;
       
   426     if ( aError == KErrNone )
       
   427         {
       
   428         iState = EStateReady;
       
   429         }
       
   430     else
       
   431         {
       
   432         iState = EStateIdle;
       
   433         Cleanup();
       
   434         }
       
   435 
       
   436     observer.ResolveComplete( *this, aError );
       
   437     __LOG( "LocalItemResolver:Complete() END" );
       
   438     }
       
   439 
       
   440 // --------------------------------------------------------------------------
       
   441 // CUPnPLocalItemResolver::Cleanup
       
   442 // See upnplocalitemresolver.h
       
   443 //---------------------------------------------------------------------------
       
   444 void CUPnPLocalItemResolver::Cleanup()
       
   445     {
       
   446     __LOG1( "CUPnPLocalItemResolver:Cleanup() iState %d", iState );
       
   447     
       
   448     iObserver = 0;
       
   449 
       
   450     if ( iState == EStateCreatingItem )
       
   451         {
       
   452         // signal cancel, wait until create item exits
       
   453         iState = EStateCancel;
       
   454         for ( TInt t = KCancelWaitMaximum;
       
   455             t > 0 && iState != EStateIdle;
       
   456             t -= KCancelWaitResolution )
       
   457             {
       
   458             User::After( TTimeIntervalMicroSeconds32(
       
   459                 KCancelWaitResolution ) );
       
   460             }
       
   461         }
       
   462     else if ( iState == EStateStartingMS )
       
   463         {
       
   464         if( iTempSession )
       
   465             {
       
   466             TRAP_IGNORE( iTempSession->CancelReserveLocalMSServicesL() );
       
   467             }
       
   468         }
       
   469     else if ( iState == EStateSharing )
       
   470 		{
       
   471         // do nothing, cannot unshare item if sharing hasn't finished yet.
       
   472         __LOG( "CUPnPLocalItemResolver:Cleanup() sharing in progress, do nothing" );
       
   473 		} 
       
   474     else if ( iState == EStateUnsharing )
       
   475 		{
       
   476         // do nothing, unsharing is still in progress. This happens,
       
   477         // if we start shutting down while previous cleanup hasn't finished yet.
       
   478         __LOG( "CUPnPLocalItemResolver:Cleanup() unsharing in progress, do nothing" );
       
   479 		}
       
   480     else if ( iState == EStateCancel )
       
   481 		{
       
   482         // do nothing, we shouldn't be here
       
   483         __LOG( "CUPnPLocalItemResolver:Cleanup() cancelling, shouldn't be here.." );
       
   484 		}
       
   485     else if ( iSharedItem )
       
   486         {
       
   487         TRAPD( error, DoUnshareL() );
       
   488         
       
   489         if( error == KErrNone )
       
   490             {
       
   491             // wait some time.. 
       
   492             // if unshare doesn't finish in time, just cancel andcontinue
       
   493             __LOG( "CUPnPLocalItemResolver:Cleanup() start timer" );
       
   494             iTimer->Start( KUnshareWait, KUnshareWait, TCallBack( TimerExpired, this ) );
       
   495             iWait->Start();
       
   496             
       
   497             iTimer->Cancel();
       
   498             }
       
   499         else
       
   500             {
       
   501             __LOG1( "CUPnPLocalItemResolver:Cleanup() unshare failed %d", error );
       
   502             }
       
   503         }
       
   504 
       
   505     __LOG( "CUPnPLocalItemResolver:Cleanup() end" );
       
   506     }
       
   507 
       
   508 // --------------------------------------------------------------------------
       
   509 // CUPnPLocalItemResolver::SetAccesstoItemResources
       
   510 // See upnplocalitemresolver.h
       
   511 // --------------------------------------------------------------------------
       
   512 void CUPnPLocalItemResolver::SetAccesstoItemResources(
       
   513                                                 CUpnpItem& aItem,
       
   514                                                 TBool aAccessAllowed )
       
   515     {
       
   516     __LOG( "CUPnPLocalItemResolver::SetAccesstoItemResources" );
       
   517 
       
   518     // Get all filenames and set the access for those.
       
   519     RUPnPElementsArray resElements;
       
   520     UPnPItemUtility::GetResElements( aItem, resElements );
       
   521     for( TInt i=0; i<resElements.Count(); i++ )
       
   522         {
       
   523         if( aAccessAllowed )
       
   524             {
       
   525             iAccessController->AddAllowedFile( resElements[i]->FilePath() );
       
   526             }
       
   527         else
       
   528             {
       
   529             iAccessController->RemoveAllowedFile(
       
   530                                         resElements[i]->FilePath() );
       
   531             }
       
   532         }
       
   533     resElements.Close();
       
   534     }
       
   535 
       
   536 // --------------------------------------------------------------------------
       
   537 // CUPnPLocalItemResolver::TimerExpired
       
   538 // See upnplocalitemresolver.h
       
   539 // -------------------------------------------------------------------------- 
       
   540 TInt CUPnPLocalItemResolver::TimerExpired( TAny* aArg )
       
   541     {
       
   542     __LOG( "CUPnPLocalItemResolver::TimerExpired" );
       
   543     
       
   544     CUPnPLocalItemResolver* resolver =
       
   545         (static_cast<CUPnPLocalItemResolver*>( aArg ));
       
   546     
       
   547     resolver->iTimer->Cancel();
       
   548     resolver->iWait->AsyncStop();
       
   549     
       
   550     __LOG( "CUPnPLocalItemResolver::TimerExpired end" );
       
   551     return KErrNone;
       
   552     }
       
   553