omadrm/drmhelper/src/DRMHelperClient.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Implementation of the Helper Client session functionality 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <e32math.h>
       
    22 #include <drmcommon.h>
       
    23 #include "DRMHelperCommon.h"
       
    24 #include "DRMHelperClient.h"
       
    25 
       
    26 
       
    27 // LOCAL CONSTANTS AND MACROS
       
    28 // Number of message slots to reserve for this client server session.
       
    29 // Since we only communicate synchronously here, we never have any
       
    30 // outstanding asynchronous requests.
       
    31 LOCAL_C const TUint KDefaultMessageSlots = 0;
       
    32 LOCAL_C const TUid KServerUid3 = {0x101F6DC5};
       
    33 
       
    34 #ifdef __WINS__
       
    35 LOCAL_C const TUint KServerMinHeapSize =  0x1000;  //  4K
       
    36 LOCAL_C const TUint KServerMaxHeapSize = 0x10000;  // 64K
       
    37 #endif
       
    38 
       
    39 // ============================ LOCAL FUNCTIONS ===============================
       
    40 #ifdef _DRM_TESTING
       
    41 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs );
       
    42 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName );
       
    43 LOCAL_C void CreateLogL();
       
    44 LOCAL_C void WriteL( const TDesC& aText );
       
    45 LOCAL_C void WriteL( const TDesC8& aText );
       
    46 LOCAL_C void WriteCurrentTimeL();
       
    47 #endif
       
    48 
       
    49 
       
    50 LOCAL_C TInt FromFileNameToUri(const TDesC16& aFileName , HBufC8*& aContentUri );
       
    51 LOCAL_C TInt CheckExpiration( const TDesC8& aUri , TTime& aEndTime , TInt& aCountLeft );
       
    52 LOCAL_C TInt StartServer();
       
    53 LOCAL_C TInt CreateServerProcess();
       
    54 
       
    55 
       
    56 
       
    57 // ============================ MEMBER FUNCTIONS ===============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // RDRMHelperClient::RDRMHelperClient
       
    61 // C++ default constructor can NOT contain any code, that
       
    62 // might leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C RDRMHelperClient::RDRMHelperClient()
       
    66 :   RSessionBase()
       
    67     {
       
    68     // No implementation required
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // RDRMHelperClient::Connect
       
    73 // 
       
    74 // Connect to the server session
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C TInt RDRMHelperClient::Connect()
       
    78     {
       
    79 #ifdef _DRM_TESTING
       
    80     TRAPD( err , CreateLogL() );
       
    81     if (err)
       
    82         {
       
    83         return err;
       
    84         }
       
    85     TRAP( err , WriteL(_L("Connect")) );
       
    86     if (err)
       
    87         {
       
    88         return err;
       
    89         }
       
    90     TRAP( err , WriteCurrentTimeL() );
       
    91     if (err)
       
    92         {
       
    93         return err;
       
    94         }
       
    95 #endif
       
    96     TInt error = StartServer();
       
    97 
       
    98     if (KErrNone == error)
       
    99         {
       
   100 
       
   101         error = CreateSession(KDRMHelperServerName,
       
   102                               Version(),
       
   103                               KDefaultMessageSlots);
       
   104         }
       
   105     return error;
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // RDRMHelperClient::Version
       
   110 // 
       
   111 // return server version
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C TVersion RDRMHelperClient::Version() const
       
   115     {
       
   116     return(TVersion(KDRMHSMajorVersionNumber,
       
   117                     KDRMHSMinorVersionNumber,
       
   118                     KDRMHSBuildVersionNumber));
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // RDRMHelperClient::SetAutomated
       
   123 // 
       
   124 // Register one content uri to the helper server
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C TInt RDRMHelperClient::SetAutomated( const TDesC8& aUri , const TInt& aType ) const
       
   128     {
       
   129     TInt temp = aType;
       
   130     TPtrC8 descriptor(aUri);
       
   131 
       
   132     // This call waits for the server to complete the request before 
       
   133     // proceeding. 
       
   134     return SendReceive(ERegister, TIpcArgs( &descriptor, temp ) );
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // RDRMHelperClient::SetAutomated
       
   139 // 
       
   140 // Register one file to the helper server
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 EXPORT_C TInt RDRMHelperClient::SetAutomated( const TDesC16& aFileName , const TInt& aType ) const
       
   144     {
       
   145     TInt temp = aType;
       
   146     TPtrC8 descriptor( NULL , 0 );
       
   147    
       
   148     descriptor.Set( reinterpret_cast<const TUint8*>( aFileName.Ptr() ), aFileName.Length()*2);
       
   149 
       
   150     // This call waits for the server to complete the request before 
       
   151     // proceeding. 
       
   152     return SendReceive(ERegisterFile, TIpcArgs( &descriptor, temp ) );
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // RDRMHelperClient::RemoveAutomated
       
   157 // 
       
   158 // Register one content uri to the helper server
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 EXPORT_C TInt RDRMHelperClient::RemoveAutomated( const TDesC8& aUri , const TInt& aType ) const
       
   162     {
       
   163     TInt temp = aType;
       
   164     TPtrC8 descriptor(aUri);
       
   165 
       
   166     // This call waits for the server to complete the request before 
       
   167     // proceeding.
       
   168     return SendReceive(ERemove, TIpcArgs( &descriptor, temp ) );
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // RDRMHelperClient::RemoveAutomated
       
   173 // 
       
   174 // Register one file to the helper server
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 EXPORT_C TInt RDRMHelperClient::RemoveAutomated( const TDesC16& aFileName , const TInt& aType ) const
       
   178     {
       
   179     TInt temp = aType;
       
   180     TPtrC8 descriptor( NULL , 0 );
       
   181 
       
   182     descriptor.Set( reinterpret_cast<const TUint8*>( aFileName.Ptr() ), aFileName.Length()*2);
       
   183 
       
   184     // This call waits for the server to complete the request before 
       
   185     // proceeding. 
       
   186     return SendReceive(ERemoveFile, TIpcArgs( &descriptor ) );
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // RDRMHelperClient::IndicateIdle
       
   191 // 
       
   192 // Register one file to the helper server
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 EXPORT_C TInt RDRMHelperClient::IndicateIdle() const
       
   196     {
       
   197     return SendReceive(EIndicateIdle, TIpcArgs());
       
   198     }
       
   199 
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // RDRMHelperClient::CanSetAutomated
       
   203 // 
       
   204 // Register one content uri to the helper server
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 EXPORT_C TInt RDRMHelperClient::CanSetAutomated( const TDesC8& aUri , TBool& aValue ) const
       
   208     {
       
   209     TTime endTime;
       
   210     TTime temp;
       
   211     TInt countsLeft = 0;
       
   212     TInt err = 0;
       
   213     temp.Set( KNullDate );
       
   214     endTime.Set( KNullDate );
       
   215 
       
   216     err = CheckExpiration( aUri , endTime , countsLeft );
       
   217     if ( endTime != temp )
       
   218         {
       
   219         aValue = ETrue;
       
   220         }
       
   221     else
       
   222         {
       
   223         aValue = EFalse;
       
   224         }
       
   225     return err;
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // RDRMHelperClient::CanSetAutomated
       
   230 // 
       
   231 // Register one file to the helper server
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 EXPORT_C TInt RDRMHelperClient::CanSetAutomated( const TDesC16& aFileName , TBool& aValue ) const
       
   235     {
       
   236     HBufC8* contentUri = NULL;
       
   237     TInt err = 0;
       
   238     err = FromFileNameToUri( aFileName , contentUri );
       
   239     if (err)
       
   240         {
       
   241         return err;
       
   242         }
       
   243     err = CanSetAutomated( contentUri->Des() , aValue );
       
   244     delete contentUri;
       
   245     return err;
       
   246     }
       
   247 
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // RDRMHelperClient::IsAutomated
       
   251 // 
       
   252 // Register one content uri to the helper server
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 EXPORT_C TInt RDRMHelperClient::IsAutomated( const TDesC8& aUri , TInt& aType , TBool& aIs ) 
       
   256     {
       
   257     TPtr8 type( reinterpret_cast< TUint8* >( &aType ),
       
   258                0, 
       
   259                sizeof( TInt ) );
       
   260     TPtr8 flag( reinterpret_cast< TUint8* >( &aIs ),
       
   261                0, 
       
   262                sizeof( TInt ) );
       
   263 
       
   264     TPtrC8 descriptor(aUri);
       
   265 
       
   266     // This call waits for the server to complete the request before 
       
   267     // proceeding. 
       
   268     return SendReceive(EIsRegistered, TIpcArgs( &type, &descriptor, &flag ) );
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // RDRMHelperClient::IsAutomated
       
   273 // 
       
   274 // Register one file to the helper server
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 EXPORT_C TInt RDRMHelperClient::IsAutomated( const TDesC16& aFileName , TInt& aType , TBool& aIs ) 
       
   278     {
       
   279     TPtr8 type( reinterpret_cast< TUint8* >( &aType ),
       
   280                0, 
       
   281                sizeof( TInt ) );
       
   282     TPtr8 flag( reinterpret_cast< TUint8* >( &aIs ),
       
   283                0, 
       
   284                sizeof( TInt ) );
       
   285 
       
   286     TPtrC8 descriptor( NULL , 0 );
       
   287 
       
   288     
       
   289     descriptor.Set( reinterpret_cast<const TUint8*>( aFileName.Ptr() ), aFileName.Length()*2);
       
   290 
       
   291     // This call waits for the server to complete the request before 
       
   292     // proceeding. 
       
   293     return SendReceive(EIsRegisteredFile, TIpcArgs( &type, &descriptor, &flag ) );
       
   294 
       
   295     }
       
   296 
       
   297 
       
   298 
       
   299 // ============================= LOCAL FUNCTIONS ===============================
       
   300 // -----------------------------------------------------------------------------
       
   301 // StartServer
       
   302 // 
       
   303 // Start the helper server
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 LOCAL_C TInt StartServer()
       
   307     {
       
   308 
       
   309 #ifdef _DRM_TESTING
       
   310     TRAPD( err , WriteL(_L("StartServer")) );
       
   311     if (err)
       
   312         {
       
   313         return err;
       
   314         }
       
   315     TRAP( err , WriteCurrentTimeL() );
       
   316     if (err)
       
   317         {
       
   318         return err;
       
   319         }
       
   320 #endif
       
   321     TInt result = 0;
       
   322 
       
   323     TFindServer findHelperServer(KDRMHelperServerName);
       
   324     TFullName name;
       
   325 
       
   326     result = findHelperServer.Next(name);
       
   327     if (result == KErrNone)
       
   328         {
       
   329         // Server already running
       
   330         return KErrNone;
       
   331         }
       
   332 
       
   333     RSemaphore semaphore;       
       
   334     result = semaphore.CreateGlobal(KDRMHelperServerSemaphoreName, 0);
       
   335     if (result != KErrNone)
       
   336         {
       
   337         return  result;
       
   338         }
       
   339 
       
   340     result = CreateServerProcess();
       
   341     if (result != KErrNone)
       
   342         {
       
   343         semaphore.Close(); 
       
   344         return  result;
       
   345         }
       
   346 
       
   347     semaphore.Wait();
       
   348     semaphore.Close();       
       
   349 
       
   350     return  KErrNone;
       
   351     }
       
   352 
       
   353 LOCAL_C TInt CreateServerProcess()
       
   354     {
       
   355 #ifdef _DRM_TESTING
       
   356     TRAPD( err , WriteL(_L("CreateServerProcess")) );
       
   357     if (err)
       
   358         {
       
   359         return err;
       
   360         }
       
   361     TRAP( err , WriteCurrentTimeL() );
       
   362     if (err)
       
   363         {
       
   364         return err;
       
   365         }
       
   366 #endif
       
   367     TInt result;
       
   368 
       
   369     const TUidType serverUid(KNullUid, KNullUid, KServerUid3);
       
   370 
       
   371 #ifdef __WINS__
       
   372 
       
   373     RLibrary lib;
       
   374     result = lib.Load( KDRMHSServerFileName , serverUid );
       
   375     if (result != KErrNone)
       
   376         {
       
   377         return  result;
       
   378         }
       
   379 
       
   380 #ifdef _DRM_TESTING
       
   381     TRAP( err , WriteL(_L("library is loaded")) );
       
   382     if (err)
       
   383         {
       
   384         return err;
       
   385         }
       
   386     TRAP( err , WriteCurrentTimeL() );
       
   387     if (err)
       
   388         {
       
   389         return err;
       
   390         }
       
   391 #endif
       
   392 
       
   393     //  Get the WinsMain function
       
   394     TLibraryFunction functionWinsMain = lib.Lookup(1);
       
   395 
       
   396     //  Call it and cast the result to a thread function
       
   397     TThreadFunction serverThreadFunction = reinterpret_cast<TThreadFunction>(functionWinsMain());
       
   398 
       
   399     TName threadName(KDRMHelperServerName);
       
   400 
       
   401     // Append a random number to make it unique
       
   402     threadName.AppendNum(Math::Random(), EHex);
       
   403 
       
   404     RThread server;
       
   405 
       
   406     result = server.Create(threadName,   // create new server thread
       
   407                              serverThreadFunction, // thread's main function
       
   408                              KDefaultStackSize,
       
   409                              NULL,
       
   410                              &lib,
       
   411                              NULL,
       
   412                              KServerMinHeapSize,
       
   413                              KServerMaxHeapSize,
       
   414                              EOwnerProcess);
       
   415 
       
   416 
       
   417 
       
   418     lib.Close();    // if successful, server thread has handle to library now
       
   419 
       
   420     if (result != KErrNone)
       
   421         {
       
   422         return  result;
       
   423         }
       
   424 #ifdef _DRM_TESTING
       
   425     TRAP( err , WriteL(_L("server thread is created")) );
       
   426     if (err)
       
   427         {
       
   428         return err;
       
   429         }
       
   430     TRAP( err , WriteCurrentTimeL() );
       
   431     if (err)
       
   432         {
       
   433         return err;
       
   434         }
       
   435 #endif
       
   436     server.SetPriority(EPriorityMore);
       
   437 
       
   438 
       
   439 #else
       
   440 
       
   441     RProcess server;
       
   442     result = server.Create( KDRMHSServerFileName, KNullDesC, serverUid);
       
   443     if (result != KErrNone)
       
   444         {
       
   445         return  result;
       
   446         }
       
   447 #ifdef _DRM_TESTING
       
   448     TRAP( err , WriteL(_L("server thread is created")) );
       
   449     if (err)
       
   450         {
       
   451         return err;
       
   452         }
       
   453     TRAP( err , WriteCurrentTimeL() );
       
   454     if (err)
       
   455         {
       
   456         return err;
       
   457         }
       
   458 #endif
       
   459 
       
   460 #endif
       
   461 
       
   462     server.Resume();
       
   463     server.Close();
       
   464 
       
   465     return  KErrNone;
       
   466     }
       
   467 
       
   468 LOCAL_C TInt FromFileNameToUri(const TDesC16& aFileName , HBufC8*& aContentUri )
       
   469     {
       
   470     DRMAuthenticated* c = NULL;
       
   471     DRMCommon::TContentProtection protection;
       
   472     HBufC8* mimeType = NULL;
       
   473     TUint dataLen = 0;
       
   474     TRAPD(err , c = DRMAuthenticated::NewL());
       
   475     if (err)
       
   476         {
       
   477         return err;
       
   478         }
       
   479     err = c->GetFileInfo(
       
   480         aFileName,
       
   481         protection,
       
   482         mimeType,
       
   483         aContentUri,
       
   484         dataLen);
       
   485     delete mimeType;
       
   486     if (err)
       
   487         {
       
   488         delete aContentUri;
       
   489         aContentUri = NULL;
       
   490         }
       
   491     return err;
       
   492     }
       
   493 
       
   494 
       
   495 LOCAL_C TInt CheckExpiration( const TDesC8& aUri , TTime& aEndTime , TInt& aCountLeft )
       
   496     {
       
   497     DRMAuthenticated* c = NULL;
       
   498     CDRMRights* right = NULL;
       
   499     TRAPD( err  , c = DRMAuthenticated::NewL() );
       
   500     if (err)
       
   501         {
       
   502         return err;
       
   503         }
       
   504     err = c->GetActiveRights( aUri , 
       
   505         DRMCommon::EPlay | DRMCommon::EExecute | DRMCommon::EPrint | DRMCommon::EDisplay , 
       
   506         right );
       
   507     if (!err)
       
   508         {
       
   509         err = right->GetExpirationDetails(
       
   510             DRMCommon::EPlay | DRMCommon::EExecute | DRMCommon::EPrint | DRMCommon::EDisplay,
       
   511             aEndTime,
       
   512             aCountLeft);
       
   513         }
       
   514     delete right;
       
   515     delete c;
       
   516     return err;
       
   517     }
       
   518 
       
   519 #ifdef _DRM_TESTING
       
   520 
       
   521 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
   522     {
       
   523     _LIT( KLogFile , "c:\\HSClientLog.txt" );
       
   524     WriteFileL( text , aFs , KLogFile );
       
   525     }
       
   526 
       
   527 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
   528     {
       
   529     RFile file;
       
   530     TInt size;
       
   531     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
   532     CleanupClosePushL( file );
       
   533     User::LeaveIfError( file.Size( size ) );
       
   534     User::LeaveIfError( file.Write( size, text ) );
       
   535     CleanupStack::PopAndDestroy(); //file
       
   536     }
       
   537 
       
   538 LOCAL_C void CreateLogL()
       
   539     {
       
   540     RFs fs;
       
   541     User::LeaveIfError(fs.Connect());
       
   542     CleanupClosePushL(fs);
       
   543     RFile file;
       
   544     User::LeaveIfError( file.Replace( fs , _L("c:\\HSClientLog.txt") , EFileWrite ) );
       
   545     file.Close();
       
   546     CleanupStack::PopAndDestroy(); //fs
       
   547     }
       
   548 
       
   549 LOCAL_C void WriteL( const TDesC& aText )
       
   550     {
       
   551     RFs fs;
       
   552     User::LeaveIfError( fs.Connect() );
       
   553     CleanupClosePushL(fs);
       
   554     HBufC8* text = HBufC8::NewLC(1000);
       
   555     TPtr8 textptr(text->Des() );
       
   556     textptr.Append( aText );
       
   557     textptr.Append( _L("\r\n") );
       
   558     WriteLogL(textptr , fs);
       
   559     CleanupStack::PopAndDestroy(text);
       
   560     CleanupStack::PopAndDestroy(); //fs
       
   561     }
       
   562 
       
   563 LOCAL_C void WriteL( const TDesC8& aText )
       
   564     {
       
   565     RFs fs;
       
   566     User::LeaveIfError( fs.Connect() );
       
   567     CleanupClosePushL(fs);
       
   568     HBufC8* text = HBufC8::NewLC(1000);
       
   569     TPtr8 textptr(text->Des() );
       
   570     textptr.Append( aText );
       
   571     textptr.Append( _L8("\r\n") );
       
   572     WriteLogL(textptr , fs);
       
   573     CleanupStack::PopAndDestroy(text);
       
   574     CleanupStack::PopAndDestroy(); //fs
       
   575     }
       
   576 
       
   577 
       
   578 LOCAL_C void WriteCurrentTimeL()
       
   579     {
       
   580     RFs fs;
       
   581     User::LeaveIfError( fs.Connect() );
       
   582     CleanupClosePushL(fs);
       
   583     HBufC8* text = HBufC8::NewLC(100);
       
   584     TPtr8 textptr(text->Des() );
       
   585 // Date and Time display
       
   586     TTime time;
       
   587     time.HomeTime();
       
   588     TBuf<256> dateString;
       
   589     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   590     time.FormatL(dateString,KDate);
       
   591     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
   592     textptr.Append( dateString );
       
   593     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   594     time.FormatL(dateString,KTime);
       
   595     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   596     textptr.Append( dateString );
       
   597     textptr.Append(_L( "\r\n" ) );
       
   598     textptr.Append(_L( "\r\n" ) );
       
   599     WriteLogL(textptr , fs);
       
   600     CleanupStack::PopAndDestroy(text);
       
   601     CleanupStack::PopAndDestroy(); //fs
       
   602     }
       
   603 #endif
       
   604 
       
   605 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   606 
       
   607 // Epoc DLL entry point, return that everything is ok
       
   608 GLDEF_C TInt E32Dll(TDllReason)
       
   609     {
       
   610     return KErrNone;
       
   611     }
       
   612 
       
   613 //  End of File