omadrm/drmhelper/src/RDRMHelper.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004 - 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:  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 "drmhelperserver.h"
       
    25 #include "RDRMHelper.h"
       
    26 
       
    27 // LOCAL CONSTANTS AND MACROS
       
    28 
       
    29 // Number of message slots to reserve for this client server session.
       
    30 // Since we only communicate synchronously here, we never have any
       
    31 // outstanding asynchronous requests.
       
    32 _LIT( KDRMHelperServerSemaphoreStartingName, "DRMHelperServerSemaphoreStarting" );
       
    33 
       
    34 const TUint KDefaultMessageSlots = 0;
       
    35 const TUid KServerUid3 = { 0x101F6DC5 };
       
    36 _LIT_SECURE_ID( KServerSecureId, 0x101F6DC5 );
       
    37 
       
    38 #ifdef _DEBUG
       
    39     #include <e32svr.h> // RDebug
       
    40     #define TRACE( x ) RDebug::Print( _L( x ) )
       
    41     #define TRACE2( x, y ) RDebug::Print( _L( x ), y )
       
    42     #define TRACE3( x, y, z ) RDebug::Print( _L( x ), y, z )
       
    43 #else
       
    44     #define TRACE( x )
       
    45     #define TRACE2( x, y )
       
    46     #define TRACE3( x, y, z )
       
    47 #endif
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // RDRMHelper::RDRMHelper
       
    53 // C++ default constructor can NOT contain any code, that might leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 RDRMHelper::RDRMHelper() : RSessionBase()
       
    57     {
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // RDRMHelper::Connect
       
    62 //
       
    63 // Connect to the server session
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 TInt RDRMHelper::Connect()
       
    67     {
       
    68     TInt error = StartServer();
       
    69     TRACE2( "RDRMHelper::Connect() StartServer(): error: %d", error );
       
    70     if ( !error )
       
    71         {
       
    72         error = CreateSession( KDRMHelperServerName, Version(),
       
    73             KDefaultMessageSlots );
       
    74         TRACE2( "RDRMHelper::Connect() CreateSession(): error: %d", error );
       
    75         }
       
    76     return error;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // RDRMHelper::Version
       
    81 //
       
    82 // return server version
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 TVersion RDRMHelper::Version() const
       
    86     {
       
    87     return( TVersion( KDRMHSMajorVersionNumber, KDRMHSMinorVersionNumber,
       
    88         KDRMHSBuildVersionNumber ) );
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // RDRMHelper::SetAutomated
       
    93 //
       
    94 // Register one content uri to the helper server
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 TInt RDRMHelper::SetAutomated(
       
    98     const TDesC8& aUri,
       
    99     TBool aActive,
       
   100     TInt aAutomatedType,
       
   101     TInt aIntent ) const
       
   102     {
       
   103     TInt mode =
       
   104         aActive ? CDRMHelperServer::EActive : CDRMHelperServer::EPassive;
       
   105 
       
   106     // Create descriptor to enable copying data between
       
   107     // client and server. Note: This can be local since
       
   108     // this is a synchronous call.
       
   109     // Note : Using TPtr8 since this is binary information
       
   110     TPtrC8 descriptor( aUri );
       
   111 
       
   112     // This call waits for the server to complete the request before
       
   113     // proceeding.
       
   114     return SendReceive(
       
   115         ERegister, TIpcArgs( aIntent, mode, aAutomatedType, &descriptor ) );
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // RDRMHelper::RemoveAutomated
       
   120 //
       
   121 // Unregister one content uri
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 TInt RDRMHelper::RemoveAutomated(
       
   125     const TDesC8& aUri,
       
   126     TBool aActive,
       
   127     TInt aAutomatedType,
       
   128     TInt aIntent ) const
       
   129     {
       
   130     TInt ret( 0 );
       
   131     TInt mode(
       
   132         aActive ? CDRMHelperServer::EActive : CDRMHelperServer::EPassive );
       
   133 
       
   134     // Create descriptor to enable copying data between
       
   135     // client and server. Note: This can be local since
       
   136     // this is a synchronous call.
       
   137     // Note : Using TPtr8 since this is binary information
       
   138     TPtrC8 descriptor( aUri );
       
   139 
       
   140     // This call waits for the server to complete the request before
       
   141     // proceeding.
       
   142     ret = SendReceive(
       
   143         ERemove, TIpcArgs( aIntent, mode, aAutomatedType, &descriptor ) );
       
   144 
       
   145     if ( ret == KErrNotFound )
       
   146         {
       
   147         // content was never actually registered
       
   148         ret = KErrNone;
       
   149         }
       
   150     return ret;
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // RDRMHelper::RemoveAutomatedAll
       
   155 //
       
   156 // Unregister one content uri
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 TInt RDRMHelper::RemoveAutomatedAll(
       
   160     const TDesC8& aUri,
       
   161     TBool aActive,
       
   162     TInt aAutomatedType,
       
   163     TInt aIntent ) const
       
   164     {
       
   165     TPtrC8 descriptor( aUri );
       
   166     TInt ret;
       
   167     TInt mode(
       
   168         aActive ? CDRMHelperServer::EActive : CDRMHelperServer::EPassive );
       
   169     TBool automated = EFalse;
       
   170     TInt tempMode( 0 );
       
   171 
       
   172     // This call waits for the server to complete the request before
       
   173     // proceeding.
       
   174     ret = SendReceive(
       
   175         ERemove, TIpcArgs( aIntent, mode, aAutomatedType, &descriptor ) );
       
   176     IsAutomated( aUri, aAutomatedType, aIntent, automated, tempMode );
       
   177     while ( automated && tempMode == mode )
       
   178         {
       
   179         // unregister all
       
   180         ret = SendReceive(
       
   181             ERemove, TIpcArgs( aIntent, mode, aAutomatedType, &descriptor ) );
       
   182         IsAutomated( aUri, aAutomatedType, aIntent, automated, tempMode );
       
   183         }
       
   184 
       
   185     if ( ret == KErrNotFound )
       
   186         {
       
   187         ret = KErrNone;
       
   188         }
       
   189     return ret;
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // RDRMHelper::IndicateIdle
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 TInt RDRMHelper::IndicateIdle() const
       
   197     {
       
   198     return SendReceive( EIndicateIdle, TIpcArgs() );
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // RDRMHelper::IsAutomated
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 TInt RDRMHelper::IsAutomated(
       
   206     const TDesC8& aUri,
       
   207     TInt aAutomatedType,
       
   208     TInt aIntent,
       
   209     TBool& aAutomated,
       
   210     TInt& aType ) const
       
   211     {
       
   212     TPtr8 typeptr( reinterpret_cast< TUint8* >( &aType ), 0, sizeof( TInt ) );
       
   213     TPtr8 flag( reinterpret_cast< TUint8* >( &aAutomated ), 0, sizeof( TInt ) );
       
   214     TInt ret( 0 );
       
   215     TInt type = CDRMHelperServer::EActive;
       
   216 
       
   217     // Create descriptor to enable copying data between
       
   218     // client and server. Note: This can be local since
       
   219     // this is a synchronous call.
       
   220     // Note : Using TPtr8 since this is binary information
       
   221     TPtrC8 descriptor( aUri );
       
   222 
       
   223     // This call waits for the server to complete the request before
       
   224     // proceeding.
       
   225     ret = SendReceive(
       
   226         EIsRegistered,
       
   227         TIpcArgs( aIntent, type, aAutomatedType, &descriptor ) );
       
   228     if ( !ret )
       
   229         {
       
   230         type = CDRMHelperServer::EPassive;
       
   231         ret = SendReceive(
       
   232             EIsRegistered,
       
   233             TIpcArgs( aIntent, type, aAutomatedType, &descriptor ) );
       
   234         }
       
   235     aAutomated = ret > 0 ? ETrue : EFalse;
       
   236     aType = type;
       
   237     return KErrNone;
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // RDRMHelper::StartServer
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 TInt RDRMHelper::StartServer()
       
   245     {
       
   246     RThread currentThread;
       
   247     const TUint32 secureIdAsTuint32( currentThread.SecureId() );
       
   248     TRACE2( "RDRMHelper::StartServer(): currentThread: 0x%08x",
       
   249             secureIdAsTuint32 );
       
   250     if ( currentThread.SecureId() == KServerSecureId )
       
   251         {
       
   252         // HelperServer cannot connect to itself.
       
   253         return KErrCouldNotConnect;
       
   254         }
       
   255 
       
   256     TInt result( 0 );
       
   257 
       
   258     TFindServer findHelperServer( KDRMHelperServerName );
       
   259     TFullName name;
       
   260 
       
   261     result = findHelperServer.Next( name );
       
   262     TRACE2( "RDRMHelper::StartServer(): result: %d", result );
       
   263     if ( result == KErrNone )
       
   264         {
       
   265         // Server already running
       
   266         return KErrNone;
       
   267         }
       
   268 
       
   269     RSemaphore semaphoreStarting;
       
   270     TInt semaphoreExist( semaphoreStarting.CreateGlobal(
       
   271             KDRMHelperServerSemaphoreStartingName, 0 ) );
       
   272     TRACE2( "RDRMHelper::StartServer(): semaphoreExist: %d", semaphoreExist );
       
   273 
       
   274     if( semaphoreExist != KErrAlreadyExists && semaphoreExist != KErrNone )
       
   275         {
       
   276         return semaphoreExist;
       
   277         }
       
   278 
       
   279     // Semaphore exists, wait until server is finished with it's tasks
       
   280     if ( semaphoreExist == KErrAlreadyExists )
       
   281         {
       
   282         TInt openErr( semaphoreStarting.OpenGlobal(
       
   283                 KDRMHelperServerSemaphoreStartingName) );
       
   284         TRACE2( "RDRMHelper::StartServer(): openErr: %d", openErr );
       
   285         if ( openErr != KErrNone )
       
   286             {
       
   287             return openErr;
       
   288             }
       
   289 
       
   290         TRACE( "RDRMHelper::StartServer(): semaphoreStarting.Wait()" );
       
   291         semaphoreStarting.Wait();
       
   292         TRACE( "RDRMHelper::StartServer(): semaphoreStarting.Wait() - returning" );
       
   293         }
       
   294     else
       
   295         {
       
   296         RSemaphore semaphoreStartServer;
       
   297         TInt result( semaphoreStartServer.CreateGlobal(
       
   298                 KDRMHelperServerSemaphoreName, 0 ) );
       
   299         TRACE2( "RDRMHelper::StartServer(): 2 result: %d", result );
       
   300         if ( result != KErrNone )
       
   301             {
       
   302             semaphoreStarting.Close();
       
   303             return result;
       
   304             }
       
   305 
       
   306         result = CreateServerProcess();
       
   307         TRACE2( "RDRMHelper::StartServer(): 3 result: %d", result );
       
   308         if ( result != KErrNone )
       
   309             {
       
   310             return  result;
       
   311             }
       
   312 
       
   313         semaphoreStartServer.Wait();
       
   314         semaphoreStartServer.Close();
       
   315 
       
   316         TRACE( "RDRMHelper::StartServer(): semaphoreStarting.Signal()" );
       
   317 
       
   318         semaphoreStarting.Signal();
       
   319         semaphoreStarting.Close();
       
   320         }
       
   321 
       
   322     TRACE( "RDRMHelper::StartServer(): return KErrNone" );
       
   323     return  KErrNone;
       
   324     }
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // RDRMHelper::CreateServerProcess
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 TInt RDRMHelper::CreateServerProcess()
       
   331     {
       
   332     TInt result( 0 );
       
   333 
       
   334     const TUidType serverUid( KNullUid, KNullUid, KServerUid3 );
       
   335 
       
   336 
       
   337     RProcess server;
       
   338     result = server.Create( KDRMHSServerFileName, _L( "" ), serverUid );
       
   339     if ( result != KErrNone )
       
   340         {
       
   341         return  result;
       
   342         }
       
   343 
       
   344     server.Resume();
       
   345     server.Close();
       
   346 
       
   347     return  KErrNone;
       
   348     }
       
   349 
       
   350 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   351 
       
   352 
       
   353 //  End of File