omadrm/drmengine/notifier/src/DRMNotifierSession.cpp
changeset 0 95b198f216e5
child 2 76350b5be3d8
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004-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:  This class handles all client requests.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <s32file.h>
       
    21 #include <f32file.h>
       
    22 #include "drmcommon.h"
       
    23 #include "DRMNotifierSession.h"
       
    24 #include "DRMNotifierServer.h"
       
    25 #include "drmnotifierclientserver.h"
       
    26 #include <e32test.h>
       
    27 
       
    28 // NAMESPACES
       
    29 using namespace DRMNotifier;
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 // CONSTANTS
       
    34 
       
    35 
       
    36 // MACROS
       
    37 #ifdef _DRM_TESTING
       
    38 #define LOG( a ) ( ( CDRMNotifierServer* )( Server() ) )->Log( a )
       
    39 #else
       
    40 #define LOG( a )
       
    41 #endif
       
    42 
       
    43 // LOCAL CONSTANTS AND MACROS
       
    44 
       
    45 const TInt KSanityDataLengthLow = 0;
       
    46 const TInt KSanityDataLengthHigh = 32768;
       
    47 
       
    48 // MODULE DATA STRUCTURES
       
    49 // DATA TYPES
       
    50 // LOCAL FUNCTIONS
       
    51 
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // SanitizeL
       
    55 // Performs a sanity check on length parameters
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 LOCAL_C void SanitizeL( TInt aParam )
       
    59     {
       
    60     if( aParam <= KSanityDataLengthLow || aParam > KSanityDataLengthHigh )
       
    61         {
       
    62         User::Leave(KErrArgument);
       
    63         }
       
    64     }
       
    65 
       
    66 // FORWARD DECLARATIONS
       
    67 // ============================ MEMBER FUNCTIONS ===============================
       
    68 // -----------------------------------------------------------------------------
       
    69 // CDRMNotifierSession::NewLC
       
    70 // Two-phased constructor.
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CDRMNotifierSession* CDRMNotifierSession::NewL( CDRMMessageStorage* aStorage) 
       
    74     {
       
    75     CDRMNotifierSession* self = new( ELeave ) CDRMNotifierSession( aStorage );
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop(); // self
       
    79     return self;
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CDRMNotifierSession::~CDRMNotifierSession
       
    84 // Destructor.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CDRMNotifierSession::~CDRMNotifierSession()
       
    88     {
       
    89     TInt error = KErrNone;
       
    90     TInt i = 0;
       
    91 
       
    92     if( iStorage )
       
    93         {
       
    94         TRAP(error, iStorage->CancelL( this ) );
       
    95         }
       
    96 
       
    97     if(error)
       
    98         {
       
    99         LOG( _L8( " Something went wrong with Cancelling the notifications " ) );
       
   100         }
       
   101 
       
   102     for( i = 0; i < iMessageQueue.Count(); i++ )
       
   103         {
       
   104         iStorage->UpdateMessage(iMessageQueue[i]);
       
   105         }
       
   106 
       
   107     iMessageQueue.Reset();
       
   108 
       
   109     for( i = 0; i < iContentIDList.Count(); i++ )
       
   110         {
       
   111         if( iContentIDList[i]->iContentID )
       
   112             {
       
   113             delete iContentIDList[i]->iContentID;
       
   114             iContentIDList[i]->iContentID = NULL;
       
   115             }
       
   116         delete iContentIDList[i];
       
   117         iContentIDList[i] = NULL;
       
   118         }
       
   119     iContentIDList.Reset();
       
   120 
       
   121     LOG( _L8( "Notifier session closed." ) );
       
   122     }
       
   123 
       
   124 
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CDRMNotifierSession::SendNotificationL
       
   128 // This method sends a notification to the client or if it's not active adds it
       
   129 // to the queue and the next time a registration happens the client will get
       
   130 // notified.
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 TBool CDRMNotifierSession::SendNotificationL( CDRMMessageStorage::TMessageData* aMessage )
       
   134     {
       
   135     if( !CanNotify( aMessage ) )
       
   136         {
       
   137         User::Leave(KErrNotFound);
       
   138         }
       
   139 
       
   140     if( !iIsListening || iMessageQueue.Count() )
       
   141         {
       
   142         iMessageQueue.Append( aMessage );
       
   143         return ETrue;
       
   144         }
       
   145     NotifyL( aMessage, EFalse );
       
   146     return EFalse;
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CDRMNotifierSession::ServiceL
       
   151 // This method runs DispatchL() under TRAP harness, since every error case
       
   152 // can be handled ==> no need to let this function leave.
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CDRMNotifierSession::ServiceL( const RMessage2& aMessage )
       
   156     {
       
   157     LOG( _L8( "ServiceL called" ) );
       
   158     // Trap possible errors...
       
   159     
       
   160     TRAPD( error, DispatchL( aMessage ) );
       
   161     
       
   162     if ( error )
       
   163         {
       
   164         LOG( _L8( "DispatcL threw an exception" ) );
       
   165         // ...and complete the request in case of an error.
       
   166         aMessage.Complete( error );
       
   167         return;
       
   168         }
       
   169 
       
   170     // The message has already completed successfully.
       
   171     LOG( _L8( "DispatchL completed successfully" ) );
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CDRMNotifierSession::CDRMNotifierSession
       
   176 // Default constructor.
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 CDRMNotifierSession::CDRMNotifierSession( CDRMMessageStorage* aStorage ) : 
       
   180     // Base class' constructor is called first.
       
   181     iStorage( aStorage )
       
   182     {
       
   183     // Nothing.
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CDRMNotifierSession::ConstructL
       
   188 // Second phase constructor. Initializes the log tool in DRM internal testing.
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CDRMNotifierSession::ConstructL()
       
   192     {
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CDRMNotifierSession::DispatchL
       
   197 // Checks which command the user requested, and forwards the request to 
       
   198 // appropriate private method. This helps to keep the code more readable.
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CDRMNotifierSession::DispatchL( const RMessage2& aMessage ) 
       
   202     {
       
   203 		RDebug::Printf(">>> [%s] %d", _S8(__PRETTY_FUNCTION__), aMessage.Function());
       
   204     switch ( aMessage.Function() )
       
   205         {
       
   206         case ENotifyClients:
       
   207             NotifyClientsL( aMessage );
       
   208             break;
       
   209         case ERecieveNotification:
       
   210             RecieveNotificationL( aMessage );
       
   211             break;
       
   212         case ECancelNotification:
       
   213             CancelNotificationL( aMessage );
       
   214             break;
       
   215         case ERegister:
       
   216             RegisterL( aMessage );
       
   217             break;
       
   218         case EUnRegister:
       
   219             UnRegisterL( aMessage );
       
   220             break;
       
   221         case ERegisterURI:
       
   222             RegisterURIL( aMessage );
       
   223             break;
       
   224         case EUnRegisterURI:
       
   225             UnRegisterURIL( aMessage );
       
   226             break;
       
   227         default:
       
   228             LOG( _L8( "DispatchL: Invalid command" ) );
       
   229             User::Leave( KErrNotSupported );
       
   230         }
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CDRMNotifierSession::NotifyClientsL
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CDRMNotifierSession::NotifyClientsL( const RMessage2& aMessage )
       
   238     {
       
   239     LOG( _L8( "NotifyClientsL" ) );
       
   240     TDRMEventType eventType;
       
   241 
       
   242     eventType = aMessage.Int1();
       
   243 
       
   244     iStorage->NotifyL(eventType,aMessage);
       
   245 
       
   246     aMessage.Complete( KErrNone );
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CDRMNotifierSession::RecieveNotificationL
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void CDRMNotifierSession::RecieveNotificationL( const RMessage2& aMessage )
       
   254     {
       
   255     LOG( _L8( "RecieveNotificationL" ) );
       
   256     TInt error = KErrNone;
       
   257     TPckg<TInt> package(0);
       
   258     TDRMEventType eventType;
       
   259 
       
   260     package.Set(reinterpret_cast<TUint8*>(&eventType),sizeof(TDRMEventType),sizeof(TDRMEventType));
       
   261     TRAP(error, aMessage.ReadL(1, package));
       
   262 
       
   263     // Set the status of listening
       
   264     iIsListening = ETrue;
       
   265 
       
   266     // Set this to message as listener
       
   267     iListener = aMessage;
       
   268 
       
   269     if( !iIsInStorage )
       
   270         {
       
   271         iIsInStorage = ETrue;
       
   272         iStorage->AddSession( this );
       
   273         }
       
   274 
       
   275     // Check if there are any notifications in queue if so process first one
       
   276     if( iMessageQueue.Count() )
       
   277         {
       
   278         NotifyL( iMessageQueue[0], ETrue );
       
   279         }
       
   280  
       
   281     // Message complete will be set elsewhere.
       
   282     // All done.
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CDRMNotifierSession::CancelNotificationL
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CDRMNotifierSession::CancelNotificationL( const RMessage2& aMessage )
       
   290     {
       
   291     LOG( _L8( "CancelNotificationL" ) );
       
   292     TInt error = KErrNone;
       
   293     TInt i = 0;
       
   294 
       
   295     TRAP(error, iStorage->CancelL( this ) );
       
   296     if( error && error != KErrNotFound )
       
   297         {
       
   298         LOG(  _L8( "CDRMNotifierSession:: Something went wrong with Cancelling the notifications " ) );
       
   299         }
       
   300 
       
   301     for( i = 0; i < iMessageQueue.Count(); i++ )
       
   302         {
       
   303         iStorage->UpdateMessage(iMessageQueue[i]);
       
   304         }
       
   305     iMessageQueue.Reset();
       
   306 
       
   307     for( i = 0; i < iContentIDList.Count(); i++ )
       
   308         {
       
   309         if( iContentIDList[i]->iContentID )
       
   310             {
       
   311             delete iContentIDList[i]->iContentID;
       
   312             iContentIDList[i]->iContentID = NULL;
       
   313             }
       
   314         delete iContentIDList[i];
       
   315         iContentIDList[i] = NULL;
       
   316         }
       
   317     iContentIDList.Reset();
       
   318 
       
   319     iIsInStorage = EFalse;
       
   320 
       
   321 	iIsListening = EFalse;
       
   322 	
       
   323 	if ( !iListener.IsNull() )
       
   324 		{
       
   325 		iListener.Complete( KErrCancel );
       
   326 		}
       
   327 	
       
   328     aMessage.Complete( KErrNone );
       
   329     }
       
   330 // -----------------------------------------------------------------------------
       
   331 // CDRMNotifierSession::RegisterURIL
       
   332 // Get the information from the client, construct a rights object, and add 
       
   333 // it to the database.
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CDRMNotifierSession::RegisterL( const RMessage2& aMessage )
       
   337     {
       
   338     LOG( _L8( "RegisterL" ) );
       
   339     TPckg<TInt> package(0);
       
   340     TDRMEventType eventType = 0;
       
   341     TContentData* content = 0;
       
   342 
       
   343     package.Set(reinterpret_cast<TUint8*>(&eventType),sizeof(TDRMEventType),sizeof(TDRMEventType));
       
   344     aMessage.ReadL(0, package);
       
   345 
       
   346     content = new (ELeave) TContentData;
       
   347 
       
   348     content->iContentID = NULL;
       
   349     content->iEventType = eventType;
       
   350 
       
   351     iContentIDList.Append( content );
       
   352 
       
   353     aMessage.Complete( KErrNone );
       
   354     }
       
   355 
       
   356 // -----------------------------------------------------------------------------
       
   357 // CDRMNotifierSession::UnRegisterURIL
       
   358 // Get the information from the client, construct a rights object, and add 
       
   359 // it to the database.
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CDRMNotifierSession::UnRegisterL( const RMessage2& aMessage )
       
   363     {
       
   364     LOG( _L8( "UnRegisterL" ) );
       
   365     TPckg<TInt> package(0);
       
   366     TDRMEventType eventType = 0;
       
   367 
       
   368     package.Set(reinterpret_cast<TUint8*>(&eventType),sizeof(TDRMEventType),sizeof(TDRMEventType));
       
   369     aMessage.ReadL(0, package);
       
   370 
       
   371     for( TInt i = 0; i < iContentIDList.Count(); i++ )
       
   372         {
       
   373         if( !iContentIDList[i]->iContentID &&
       
   374             iContentIDList[i]->iEventType == eventType )
       
   375             {
       
   376             delete iContentIDList[i];
       
   377             iContentIDList[i] = NULL;
       
   378 
       
   379             iContentIDList.Remove(i);
       
   380             break;
       
   381             }
       
   382         }
       
   383 
       
   384     aMessage.Complete( KErrNone );
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CDRMNotifierSession::RegisterURIL
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CDRMNotifierSession::RegisterURIL( const RMessage2& aMessage )
       
   392     {
       
   393     LOG( _L8( "RegisterURIL" ) );
       
   394     TPckg<TInt> package(0);
       
   395     TDRMEventType eventType = 0;
       
   396     TContentData* content = 0;
       
   397     TInt length = 0;
       
   398 
       
   399     HBufC8* contentID = 0;
       
   400     TPtr8 packageData(NULL,0);
       
   401 
       
   402     length = aMessage.GetDesLength(0);
       
   403 
       
   404     SanitizeL( length );
       
   405 
       
   406     contentID = HBufC8::NewLC(length);
       
   407     packageData.Set(contentID->Des());
       
   408 
       
   409     aMessage.ReadL(0, packageData);
       
   410 
       
   411     package.Set(reinterpret_cast<TUint8*>(&eventType),sizeof(TDRMEventType),sizeof(TDRMEventType));
       
   412     aMessage.ReadL(1, package);
       
   413 
       
   414     content = new (ELeave) TContentData;
       
   415 
       
   416     content->iContentID = contentID;
       
   417     content->iEventType = eventType;
       
   418 
       
   419     iContentIDList.Append( content );
       
   420 
       
   421     CleanupStack::Pop();
       
   422     aMessage.Complete( KErrNone );
       
   423     }
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CDRMNotifierSession::UnRegisterURIL
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 void CDRMNotifierSession::UnRegisterURIL( const RMessage2& aMessage )
       
   430     {
       
   431     LOG( _L8( "UnRegisterURIL" ) );
       
   432     TPckg<TInt> package(0);
       
   433     TDRMEventType eventType = 0;
       
   434     TInt length = 0;
       
   435 
       
   436     HBufC8* contentID = 0;
       
   437     TPtr8 packageData(NULL,0);
       
   438 
       
   439     length = aMessage.GetDesLength(0);
       
   440 
       
   441     SanitizeL( length );
       
   442 
       
   443     contentID = HBufC8::NewLC(length);
       
   444     packageData.Set(contentID->Des());
       
   445 
       
   446     aMessage.ReadL(0, packageData);
       
   447 
       
   448     package.Set(reinterpret_cast<TUint8*>(&eventType),sizeof(TDRMEventType),sizeof(TDRMEventType));
       
   449     aMessage.ReadL(1, package);
       
   450 
       
   451     for( TInt i = 0; i < iContentIDList.Count(); i++ )
       
   452         {
       
   453         if( iContentIDList[i]->iContentID &&
       
   454             iContentIDList[i]->iEventType == eventType &&
       
   455             !iContentIDList[i]->iContentID->Compare( contentID->Des() ) )
       
   456             {
       
   457             if( iContentIDList[i]->iContentID )
       
   458                 {
       
   459                 delete iContentIDList[i]->iContentID;
       
   460                 iContentIDList[i]->iContentID = NULL;
       
   461                 }
       
   462             delete iContentIDList[i];
       
   463             iContentIDList[i] = NULL;
       
   464 
       
   465             iContentIDList.Remove(i);
       
   466             break;
       
   467             }
       
   468         }
       
   469     CleanupStack::PopAndDestroy();
       
   470     aMessage.Complete( KErrNone );
       
   471     }
       
   472 
       
   473 
       
   474 // -----------------------------------------------------------------------------
       
   475 // CDRMNotifierSession::NotifyL
       
   476 // -----------------------------------------------------------------------------
       
   477 //
       
   478 void CDRMNotifierSession::NotifyL( CDRMMessageStorage::TMessageData *aMessage, TBool aFromQueue )
       
   479     {
       
   480     LOG( _L8( "NotifyL" ) );
       
   481     TPtr8 data( reinterpret_cast<TUint8*>(&aMessage->iEventType),
       
   482                 sizeof(TDRMEventType),
       
   483                 sizeof(TDRMEventType));
       
   484     TPtr8 event( aMessage->iMessageData, DRMNotifier::KDRMSizeOfMessage, 
       
   485                  DRMNotifier::KDRMSizeOfMessage );
       
   486 
       
   487     iListener.WriteL(0, event);
       
   488     iListener.WriteL(1, data);
       
   489 
       
   490     iListener.Complete( KErrNone );
       
   491 
       
   492     // Remove the message from the queue
       
   493     if( aFromQueue )
       
   494         {
       
   495         // Reduce the counter and release the message if needed
       
   496         iStorage->UpdateMessage( aMessage );
       
   497 
       
   498         iMessageQueue.Remove(0);
       
   499         }
       
   500     iIsListening = EFalse;
       
   501     }
       
   502 // -----------------------------------------------------------------------------
       
   503 // CDRMNotifierSession::CanNotifyL
       
   504 // -----------------------------------------------------------------------------
       
   505 //
       
   506 TBool CDRMNotifierSession::CanNotify( CDRMMessageStorage::TMessageData *aMessage )
       
   507     {
       
   508     for( TInt i = 0; i < iContentIDList.Count(); i++ )
       
   509         {
       
   510         if( iContentIDList[i]->iEventType == aMessage->iEventType &&
       
   511             (!iContentIDList[i]->iContentID ||
       
   512             !iContentIDList[i]->iContentID->Compare( aMessage->iData->Des() ) ) )
       
   513             {
       
   514             return ETrue;
       
   515             }
       
   516         }
       
   517     return EFalse;
       
   518     }
       
   519 
       
   520 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   521 //  End of File