webengine/device/src/DeviceLiwPeer.cpp
changeset 0 dd21522fd290
child 16 a359256acfc6
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 the License "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 LIW Device Peer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <config.h>
       
    20 #include "DeviceLiwPeer.h"
       
    21 #include "DeviceLiwBinding.h"
       
    22 #include "ServiceEventHandler.h"
       
    23 
       
    24 using namespace KJS;
       
    25 
       
    26 const TInt INIT_CALLBACK_ARRAY_SIZE = 10;
       
    27    
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 
       
    31 // ============================================================================
       
    32 // DeviceLiwPeer::DeviceLiwPeer
       
    33 // C++ constructor
       
    34 //
       
    35 // @since 5.0
       
    36 // ============================================================================
       
    37 //
       
    38 DeviceLiwPeer::DeviceLiwPeer(
       
    39     ExecState* exec,
       
    40     CDeviceLiwBinding* liwbinding,
       
    41     MLiwInterface* liwinterface )
       
    42     : m_globalExecState( exec ),
       
    43     m_binding( liwbinding ),
       
    44     m_interface( liwinterface ),
       
    45     m_serviceName( 0 ),
       
    46     m_isRunningCallBack(EFalse)
       
    47 {
       
    48     m_callbackArray = new RPointerArray<ServiceEventHandler>(INIT_CALLBACK_ARRAY_SIZE );
       
    49 }
       
    50 
       
    51 // ============================================================================
       
    52 // DeviceLiwPeer::~DeviceLiwPeer
       
    53 // destructor
       
    54 //
       
    55 // @since 5.0
       
    56 // ============================================================================
       
    57 //
       
    58 DeviceLiwPeer::~DeviceLiwPeer()
       
    59 {
       
    60     delete m_serviceName;
       
    61     m_callbackArray->ResetAndDestroy();
       
    62     m_callbackArray->Close();
       
    63     m_interface->Close();
       
    64     m_interface = NULL; // in majority cases, the interface close methods delete them selves.
       
    65 }
       
    66 
       
    67 
       
    68 // ============================================================================
       
    69 // DeviceLiwPeer::SetServiceNameL
       
    70 //
       
    71 //
       
    72 // @since 5.0
       
    73 // ============================================================================
       
    74 //
       
    75 void DeviceLiwPeer::SetServiceNameL( ExecState* exec, const List& args )
       
    76 {
       
    77     delete m_serviceName;
       
    78     m_serviceName = KJS::GetAsciiBufferL( args[0]->toString( exec ) );
       
    79 }
       
    80 
       
    81 
       
    82 // ============================================================================
       
    83 // DeviceLiwPeer::SetObserver
       
    84 //
       
    85 //
       
    86 // @since 5.0
       
    87 // ============================================================================
       
    88 //
       
    89 void DeviceLiwPeer::InstallCallback( ServiceEventHandler* eventHandler )
       
    90 {
       
    91     m_callbackArray->Append((const ServiceEventHandler*)eventHandler);
       
    92 }
       
    93 
       
    94 
       
    95 // ============================================================================
       
    96 // DeviceLiwPeer::HandleNotifyL
       
    97 // Implement MLiwNofityCallback
       
    98 //
       
    99 // @since 5.0
       
   100 // ============================================================================
       
   101 //
       
   102 TInt DeviceLiwPeer::HandleNotifyL(
       
   103     TInt cmdId,
       
   104     TInt eventId,
       
   105     CLiwGenericParamList& eventParamList,
       
   106     const CLiwGenericParamList& /*inParamList*/ )
       
   107 {
       
   108     // set running flag
       
   109     m_isRunningCallBack = ETrue;
       
   110 
       
   111     // look up the transaction id in the table to find the callback.
       
   112     int i;
       
   113     for (i = 0; i < m_callbackArray->Count(); i++)
       
   114     {
       
   115         ServiceEventHandler * event_handler = (*m_callbackArray)[i];
       
   116         if (event_handler && event_handler->TransId() == cmdId)
       
   117         {
       
   118             if (eventId == KLiwEventCanceled)
       
   119             {
       
   120                 m_callbackArray->Remove(i); // cmdId is actually transaction Id.
       
   121                 // clear running flag
       
   122                 m_isRunningCallBack = EFalse;
       
   123                 return KErrNone;
       
   124             }
       
   125             //Convert params to JS list and call m_onServiceEventCallback->InvokeCall()
       
   126             List params;
       
   127             JSLock::lock();
       
   128             JSValue* vEventParams = m_binding->LiwGenericParamList2JsArray(
       
   129                                                 m_globalExecState, &eventParamList );
       
   130             JSLock::unlock();
       
   131             params.append( jsNumber( cmdId ) );
       
   132             params.append( jsNumber( eventId ) );
       
   133             params.append( vEventParams );
       
   134 
       
   135             event_handler->InvokeCall( params );
       
   136             break;
       
   137         }
       
   138     }
       
   139 
       
   140     // clear running flag
       
   141     m_isRunningCallBack = EFalse;
       
   142 
       
   143     if ( i >= m_callbackArray->Count())
       
   144         return KErrNotFound;
       
   145 
       
   146     if (eventId == KLiwEventCompleted
       
   147         || eventId == KLiwEventError
       
   148         || eventId == KLiwEventStopped)
       
   149     {
       
   150         m_callbackArray->Remove(i); // cmdId is actually transaction Id.
       
   151     }
       
   152 
       
   153     return KErrNone;
       
   154 }
       
   155 
       
   156 
       
   157 // ============================================================================
       
   158 // DeviceLiwPeer::toString
       
   159 //
       
   160 //
       
   161 // @since 5.0
       
   162 // ============================================================================
       
   163 //
       
   164 UString DeviceLiwPeer::toString( ExecState* /*exec*/ )
       
   165 {
       
   166     if( m_interface )
       
   167     {
       
   168         return "[Interface Peer]";
       
   169     }
       
   170 
       
   171     return "[Service Peer]";
       
   172 }
       
   173 
       
   174 
       
   175 //END OF FILE