webengine/device/src/DeviceLiwInterface.cpp
changeset 0 dd21522fd290
child 10 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 Service Interface Object
       
    15 *
       
    16 */
       
    17 
       
    18 #include <config.h>
       
    19 #include <e32std.h>
       
    20 #include <lookup.h>
       
    21 #include <value.h>
       
    22 #include <interpreter.h>
       
    23 #include "LiwServiceHandler.h"
       
    24 #include "DeviceLiwInterface.h"
       
    25 #include "ServiceEventHandler.h"
       
    26 #include "DeviceBinding.h"
       
    27 #include "DeviceLiwResult.h"
       
    28 #include "DeviceLiwPeer.h"
       
    29 
       
    30 using namespace KJS;
       
    31 
       
    32 const ClassInfo DeviceLiwInterface::info = { "DeviceLiwInterface", 0, 0, 0 };
       
    33 const TInt INIT_RST_ARRAY_SIZE = 10;   // initial result object array
       
    34 
       
    35 // ============================= LOCAL FUNCTIONS ===============================
       
    36 /*
       
    37 @begin DeviceLiwInterfaceTable 1
       
    38     close DeviceLiwInterface::close DontDelete|Function 0
       
    39 @end
       
    40 */
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // DeviceLiwInterface::DeviceLiwInterface
       
    46 //
       
    47 // ----------------------------------------------------------------------------
       
    48 //
       
    49 DeviceLiwInterface::DeviceLiwInterface(
       
    50     ExecState* exec,
       
    51     MDeviceBinding* deviceBinding,
       
    52     MDevicePeer* devicePeer )
       
    53     : JSObject( exec->lexicalInterpreter()->builtinObjectPrototype() )
       
    54     {
       
    55         m_privateData = new DeviceLiwInterfacePrivate(deviceBinding, devicePeer);
       
    56         if (!m_privateData || !m_privateData->m_devicePeer )
       
    57             m_valid = false;
       
    58         else 
       
    59             m_valid = true;           
       
    60     }
       
    61 
       
    62 // ----------------------------------------------------------------------------
       
    63 // DeviceLiwInterface::~DeviceLiwInterface
       
    64 //
       
    65 // ----------------------------------------------------------------------------
       
    66 //
       
    67 DeviceLiwInterface::~DeviceLiwInterface()
       
    68     {
       
    69     delete m_privateData;
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // DeviceLiwInterface::Close
       
    74 //
       
    75 // ----------------------------------------------------------------------------
       
    76 //
       
    77 void DeviceLiwInterface::Close(ExecState* exec)
       
    78     {
       
    79     if(!m_valid)
       
    80         return;
       
    81     
       
    82     // need exec to close other jsobject
       
    83     m_privateData->m_exec = exec;
       
    84     delete m_privateData;
       
    85     m_privateData = NULL;
       
    86     m_valid = false;   
       
    87     }
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // DeviceLiwInterface::getOwnPropertySlot
       
    91 //
       
    92 // ----------------------------------------------------------------------------
       
    93 //
       
    94 bool DeviceLiwInterface::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& aSlot)
       
    95 {    
       
    96     // When the DeviceLiwInterface is valid, the check order is
       
    97     //      own property => prototype property
       
    98     // When the DeviceLiwInterface is invalid (after close), the check order is
       
    99     //      close function in table => prototype property
       
   100 
       
   101     // 1 when it is valid
       
   102     if(m_valid)
       
   103         {
       
   104         m_privateData->m_interfaceName = propertyName;
       
   105         m_privateData->m_exec = exec;
       
   106             
       
   107         // 1.1 check own property
       
   108         if (!getDirect(propertyName))
       
   109             {
       
   110             // 1.2 check prototype
       
   111             JSObject *proto = static_cast<JSObject *>(this->prototype());
       
   112             while (!proto->isNull() && proto->isObject())
       
   113                 {
       
   114                 if (proto->getOwnPropertySlot(exec, propertyName, aSlot))
       
   115                     return true;
       
   116 
       
   117                 proto = static_cast<JSObject *>(proto->prototype());
       
   118                 }
       
   119 
       
   120             DeviceLiwInterfaceFunc *wf = new DeviceLiwInterfaceFunc( exec, propertyName );
       
   121             const_cast<DeviceLiwInterface*>(this)->putDirect(
       
   122                 propertyName, wf, DontDelete|ReadOnly );
       
   123             }
       
   124 
       
   125         return JSObject::getOwnPropertySlot(exec, propertyName, aSlot);
       
   126         }
       
   127     // 2. when it is invalid
       
   128     else
       
   129         {
       
   130         // 2.1 check close() function
       
   131         if (propertyName == "close")
       
   132             {
       
   133             if(JSObject::getOwnPropertySlot(exec, propertyName, aSlot))
       
   134                 return true;
       
   135             else
       
   136                 {
       
   137                 DeviceLiwInterfaceFunc *wf = new DeviceLiwInterfaceFunc( exec, propertyName );
       
   138                 const_cast<DeviceLiwInterface*>(this)->putDirect(
       
   139                     propertyName, wf, DontDelete|ReadOnly );
       
   140                 return JSObject::getOwnPropertySlot(exec, propertyName, aSlot);
       
   141                 }        
       
   142             }
       
   143 
       
   144         // 2.2 check prototypes
       
   145         JSObject *proto = static_cast<JSObject *>(this->prototype());
       
   146         while (!proto->isNull() && proto->isObject())
       
   147             {
       
   148             if (proto->getOwnPropertySlot(exec, propertyName, aSlot))
       
   149                 return true;
       
   150 
       
   151             proto = static_cast<JSObject *>(proto->prototype());
       
   152             }
       
   153         }
       
   154     return false;
       
   155 }
       
   156 
       
   157 // ----------------------------------------------------------------------------
       
   158 // DeviceLiwInterface::SetObserver
       
   159 //
       
   160 // ----------------------------------------------------------------------------
       
   161 //
       
   162 void DeviceLiwInterface::SetObserver( ServiceEventHandler* aObserver )
       
   163     {
       
   164     if (m_valid && m_privateData->m_devicePeer )
       
   165         {
       
   166         m_privateData->m_devicePeer->InstallCallback( aObserver );
       
   167         }
       
   168     }
       
   169 
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // DeviceLiwInterface::InvokeOp
       
   173 //
       
   174 // ----------------------------------------------------------------------------
       
   175 //
       
   176 JSValue* DeviceLiwInterface::InvokeOp(
       
   177     ExecState* exec,
       
   178     const Identifier& propertyName,
       
   179     const List& aArgs )
       
   180     {
       
   181     if (m_valid && m_privateData->m_deviceBinding && m_privateData->m_devicePeer )
       
   182         {
       
   183         return ( m_privateData->m_deviceBinding->InvokeOp(
       
   184             exec, propertyName, aArgs, m_privateData->m_devicePeer ) );
       
   185         }
       
   186     else
       
   187         {
       
   188         return jsUndefined();
       
   189         }
       
   190     }
       
   191 
       
   192 // ----------------------------------------------------------------------------
       
   193 // DeviceLiwInterface::IsRunningCallBack()
       
   194 //
       
   195 // ----------------------------------------------------------------------------
       
   196 //
       
   197 TBool DeviceLiwInterface::IsRunningCallBack() const
       
   198     {
       
   199     if(!isValid())
       
   200         return EFalse;
       
   201     
       
   202     DeviceLiwPeer* devicePeer = static_cast<DeviceLiwPeer*> (m_privateData->m_devicePeer);
       
   203     
       
   204     if(devicePeer && devicePeer->IsRunningCallBack())
       
   205         return ETrue;
       
   206     else
       
   207         return EFalse;
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // DeviceLiwInterfacePrivate constructor
       
   212 //
       
   213 // ---------------------------------------------------------------------------
       
   214 DeviceLiwInterfacePrivate::DeviceLiwInterfacePrivate(MDeviceBinding* deviceBinding, MDevicePeer* devicePeer)
       
   215     {
       
   216     TRAP_IGNORE(
       
   217         m_resultObjArray = new RPointerArray<DeviceLiwResult>( INIT_RST_ARRAY_SIZE );
       
   218         m_deviceBinding = deviceBinding;    
       
   219         m_devicePeer = devicePeer;
       
   220         m_exec = NULL;
       
   221         )
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // DevicePrivate Close
       
   226 //
       
   227 // ---------------------------------------------------------------------------
       
   228 void DeviceLiwInterfacePrivate::Close()
       
   229     {    
       
   230     if ( m_resultObjArray && m_exec)
       
   231         {
       
   232         // close all the result objects created for this device
       
   233         for (int i = 0; i < m_resultObjArray->Count(); i++)
       
   234             {
       
   235             (*m_resultObjArray)[i]->Close( m_exec, true );
       
   236             }
       
   237         m_resultObjArray->Close();
       
   238         delete m_resultObjArray;
       
   239         m_resultObjArray = NULL;
       
   240         }
       
   241         
       
   242     m_deviceBinding = NULL;
       
   243     m_exec = NULL;
       
   244     
       
   245     delete m_devicePeer;
       
   246     m_devicePeer = NULL;
       
   247     }
       
   248 
       
   249 // ----------------------------------------------------------------------------
       
   250 // DeviceLiwInterfaceFunc::DeviceLiwInterfaceFunc
       
   251 //
       
   252 // ----------------------------------------------------------------------------
       
   253 //
       
   254 DeviceLiwInterfaceFunc::DeviceLiwInterfaceFunc(
       
   255     ExecState* exec,
       
   256     const Identifier& propertyName )
       
   257     : JSObject( exec->lexicalInterpreter()->builtinObjectPrototype() ),
       
   258     m_funcName( propertyName )
       
   259     {
       
   260     }
       
   261 
       
   262 // ----------------------------------------------------------------------------
       
   263 // DeviceLiwInterfaceFunc::~DeviceLiwInterfaceFunc
       
   264 //
       
   265 // ----------------------------------------------------------------------------
       
   266 //
       
   267 DeviceLiwInterfaceFunc::~DeviceLiwInterfaceFunc()
       
   268     {
       
   269     }
       
   270 
       
   271 // ----------------------------------------------------------------------------
       
   272 // DeviceLiwInterfaceFunc::implementsCall
       
   273 //
       
   274 // ----------------------------------------------------------------------------
       
   275 //
       
   276 bool DeviceLiwInterfaceFunc::implementsCall() const
       
   277     {
       
   278     return true;
       
   279     }
       
   280 
       
   281 // ----------------------------------------------------------------------------
       
   282 // DeviceLiwInterfaceFunc::callAsFunction
       
   283 //
       
   284 // ----------------------------------------------------------------------------
       
   285 //
       
   286 JSValue* DeviceLiwInterfaceFunc::callAsFunction(ExecState *exec, JSObject *aThisObj, const List &aArgs)
       
   287     {
       
   288     JSValue* ret = jsUndefined();
       
   289 
       
   290     if ( !aThisObj->inherits( &KJS::DeviceLiwInterface::info ) )
       
   291         {
       
   292         return throwError(exec, GeneralError);
       
   293         }
       
   294 
       
   295     DeviceLiwInterface* sapiif = static_cast<DeviceLiwInterface*>(aThisObj);
       
   296 
       
   297     if(!sapiif->isValid())
       
   298         return ret;
       
   299 
       
   300     if (m_funcName == "close")
       
   301         {
       
   302         // the close function cann't be called in the callback function
       
   303         if(sapiif->IsRunningCallBack())
       
   304             {
       
   305             return throwError(exec, GeneralError, "Can not close interface object in callback function.");
       
   306             }
       
   307         sapiif->Close(exec);
       
   308         return ret;
       
   309         }
       
   310 
       
   311     ret = sapiif->InvokeOp( exec, m_funcName, aArgs );
       
   312     
       
   313     if(ret->isObject() && (static_cast<JSObject*> (ret))->inherits( &KJS::DeviceLiwResult::info ))
       
   314     {
       
   315         // insert into jsobject array
       
   316         sapiif->m_privateData->m_resultObjArray->Append(static_cast<DeviceLiwResult*> (ret));
       
   317         if ( aArgs.size() > 1 )
       
   318         {
       
   319             bool ok;
       
   320             TInt32 err; // check error code before extract transaction id
       
   321             err = ret->getObject()->get(exec, Identifier("ErrorCode"))->toInt32( exec, ok );
       
   322             if (!ok)
       
   323                 return throwError( exec, GeneralError, "error occurred during asynchronous call." );
       
   324             if ( err == KErrNone )
       
   325             {
       
   326                 TInt32 tranId;
       
   327                 tranId = ret->getObject()->get( exec, Identifier( "TransactionID" ))->toInt32( exec, ok );
       
   328                 if ( !ok )
       
   329                     return throwError( exec, GeneralError, "invalid transaction id" );
       
   330                 sapiif->SetObserver( new ServiceEventHandler(
       
   331                                         exec->lexicalInterpreter()->globalExec(),
       
   332                                         this->toObject( exec ), aArgs[1], tranId ) );
       
   333             }
       
   334         }
       
   335     }
       
   336 
       
   337     return ret;
       
   338     }
       
   339 
       
   340 
       
   341 //END OF FILE
       
   342