webengine/device/src/Device.cpp
changeset 10 a359256acfc6
parent 0 dd21522fd290
child 15 60c5402cb945
equal deleted inserted replaced
5:10e98eab6f85 10:a359256acfc6
    27 #include <LiwServiceHandler.h>
    27 #include <LiwServiceHandler.h>
    28 
    28 
    29 using namespace KJS;
    29 using namespace KJS;
    30 
    30 
    31 const ClassInfo Device::info = { "Device", 0, 0, 0 };
    31 const ClassInfo Device::info = { "Device", 0, 0, 0 };
    32 const TInt INIT_SO_ARRAY_SIZE = 10;   // initial service object array
    32 const TInt INIT_ARRAY_SIZE = 10;   // initial service object array
    33 
    33 
    34 // ============================= LOCAL FUNCTIONS ===============================
    34 // ============================= LOCAL FUNCTIONS ===============================
    35 /*
    35 /*
    36 @begin DeviceTable 1
    36 @begin DeviceTable 1
    37     getServiceObject Device::getServiceObject DontDelete|Function 2
    37     getServiceObject Device::getServiceObject DontDelete|Function 2
    47 // ----------------------------------------------------------------------------
    47 // ----------------------------------------------------------------------------
    48 //
    48 //
    49 Device::Device( ExecState* exec )
    49 Device::Device( ExecState* exec )
    50     : JSObject()
    50     : JSObject()
    51     {
    51     {
    52     m_privateData = new DevicePrivate();
    52     m_privateData = new DevicePrivate(this);
    53     if (!m_privateData || !m_privateData->m_deviceBinding )
    53     if (!m_privateData || !m_privateData->m_deviceBinding )
    54         m_valid = false;
    54         m_valid = EFalse;
    55     else
    55     else
    56         m_valid = true;
    56         m_valid = ETrue;
    57     }
    57     }
    58 
    58 
    59 
    59 
    60 // ----------------------------------------------------------------------------
    60 // ----------------------------------------------------------------------------
    61 // Device::SetUid
    61 // Device::SetUid
    73 //
    73 //
    74 // ----------------------------------------------------------------------------
    74 // ----------------------------------------------------------------------------
    75 //
    75 //
    76 void Device::Close()
    76 void Device::Close()
    77     {
    77     {
       
    78     if ( !m_valid )
       
    79         return;
       
    80     
       
    81     m_valid = EFalse;
    78     delete m_privateData;
    82     delete m_privateData;
    79     m_privateData = NULL;
    83     m_privateData = NULL;
    80     }
    84     }
    81 
    85 
    82 
    86 
   105 //
   109 //
   106 //
   110 //
   107 // ----------------------------------------------------------------------------
   111 // ----------------------------------------------------------------------------
   108 bool Device::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
   112 bool Device::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
   109 {
   113 {
       
   114     if ( !m_valid )
       
   115         return false;
       
   116 
   110     m_privateData->m_exec = exec;
   117     m_privateData->m_exec = exec;
   111     m_privateData->m_propName = propertyName;
   118     m_privateData->m_propName = propertyName;
   112     const HashEntry* entry = Lookup::findEntry(&DeviceTable, propertyName);
   119     const HashEntry* entry = Lookup::findEntry(&DeviceTable, propertyName);
   113     if (entry)
   120     if (entry)
   114         {
   121         {
   125 //
   132 //
   126 //
   133 //
   127 // ----------------------------------------------------------------------------
   134 // ----------------------------------------------------------------------------
   128 JSValue* Device::getValueProperty(ExecState *exec, int token) const
   135 JSValue* Device::getValueProperty(ExecState *exec, int token) const
   129     {
   136     {
       
   137     if ( !m_valid )
       
   138         return jsUndefined();
       
   139         
   130     switch( token )
   140     switch( token )
   131         {
   141         {
   132         case getServiceObject:
   142         case getServiceObject:
   133         case listProviders:
   143         case listProviders:
   134                 return new DeviceFunc( exec, m_privateData->m_deviceBinding, token );
   144                 return new DeviceFunc( exec, m_privateData->m_deviceBinding, token );
   137             return throwError(exec, GeneralError);
   147             return throwError(exec, GeneralError);
   138         }
   148         }
   139     }
   149     }
   140 
   150 
   141 // ---------------------------------------------------------------------------
   151 // ---------------------------------------------------------------------------
       
   152 // DevicePrivateBase constructor
       
   153 //
       
   154 // ---------------------------------------------------------------------------
       
   155 DevicePrivateBase::DevicePrivateBase()
       
   156     {
       
   157     m_parent = NULL;
       
   158     m_isDeleting = EFalse;
       
   159     TRAP_IGNORE(
       
   160         m_children = new RPointerArray<DevicePrivateBase>( INIT_ARRAY_SIZE );)
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // DevicePrivateBase destructor
       
   165 //
       
   166 // ---------------------------------------------------------------------------
       
   167 DevicePrivateBase::~DevicePrivateBase()
       
   168     {
       
   169     m_isDeleting = ETrue;
       
   170     // 1. remove self from the parent
       
   171     if ( m_parent )  
       
   172         {
       
   173         m_parent->RemoveChild(this);
       
   174         }
       
   175 
       
   176     // 2. delete all the children
       
   177     for ( int i = 0; i < m_children->Count(); i++ )
       
   178         {
       
   179         delete (*m_children)[i];
       
   180         }
       
   181 
       
   182     // 3. delete array
       
   183     m_children->Close();
       
   184     delete m_children;
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // DevicePrivateBase setParent
       
   189 //
       
   190 // ---------------------------------------------------------------------------
       
   191 void DevicePrivateBase::SetParent( DevicePrivateBase* aValue )
       
   192     {
       
   193     m_parent = aValue;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // DevicePrivateBase add child into list
       
   198 //
       
   199 // ---------------------------------------------------------------------------
       
   200 void DevicePrivateBase::AddChild( DevicePrivateBase* aValue )
       
   201     {
       
   202     m_children->Append( aValue );
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // DevicePrivateBase add child into list
       
   207 //
       
   208 // ---------------------------------------------------------------------------
       
   209 void DevicePrivateBase::RemoveChild( DevicePrivateBase* aValue )
       
   210     {
       
   211     if ( m_isDeleting )
       
   212         return;
       
   213     
       
   214     TInt index = m_children->Find( aValue );
       
   215     if ( index != KErrNotFound )
       
   216         m_children->Remove( index );
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
   142 // DevicePrivate constructor
   220 // DevicePrivate constructor
   143 //
   221 //
   144 // ---------------------------------------------------------------------------
   222 // ---------------------------------------------------------------------------
   145 DevicePrivate::DevicePrivate()
   223 DevicePrivate::DevicePrivate( Device* jsobj )
   146     {
   224     {
   147 	m_deviceBinding = NULL;
   225     m_deviceBinding = NULL;
   148     TRAP_IGNORE(
   226     TRAP_IGNORE(
   149         m_serviceObjArray = new RPointerArray<ServiceObject>( INIT_SO_ARRAY_SIZE );
       
   150         m_deviceBinding = CDeviceLiwBinding::NewL();
   227         m_deviceBinding = CDeviceLiwBinding::NewL();
       
   228         m_jsobj = jsobj;
   151         m_exec = NULL;)
   229         m_exec = NULL;)
   152     }
   230     }
   153 
   231 
   154 // ---------------------------------------------------------------------------
   232 // ---------------------------------------------------------------------------
   155 // DevicePrivate Close
   233 // DevicePrivate destructor
   156 //
   234 //
   157 // ---------------------------------------------------------------------------
   235 // ---------------------------------------------------------------------------
   158 void DevicePrivate::Close()
   236 DevicePrivate::~DevicePrivate()
   159     {
   237     {
   160     if ( m_serviceObjArray )
   238     // invalid the Device
   161         {
   239     if (m_jsobj)
   162         // close all the service objects created for this device
   240         m_jsobj->m_valid = EFalse;
   163         for (int i = 0; i < m_serviceObjArray->Count(); i++)
   241         
   164             {
       
   165             (*m_serviceObjArray)[i]->Close( m_exec, true );
       
   166             }
       
   167         m_serviceObjArray->Close();
       
   168         delete m_serviceObjArray;
       
   169         m_serviceObjArray = NULL;
       
   170         }
       
   171     delete m_deviceBinding;
   242     delete m_deviceBinding;
   172     m_deviceBinding = NULL;
   243     m_deviceBinding = NULL;
   173     }
   244     }
   174 
   245 
   175 // ---------------------------------------------------------------------------
   246 // ---------------------------------------------------------------------------
   247             HBufC8* svcName = KJS::GetAsciiBufferL( args[0]->toString( exec ) );
   318             HBufC8* svcName = KJS::GetAsciiBufferL( args[0]->toString( exec ) );
   248 
   319 
   249             ServiceObject *so = new ServiceObject( exec, svcName, m_deviceBinding );
   320             ServiceObject *so = new ServiceObject( exec, svcName, m_deviceBinding );
   250             if ( so != NULL )
   321             if ( so != NULL )
   251                 {
   322                 {
   252                 (static_cast<Device*>(thisObj))->m_privateData->m_serviceObjArray->Append( so );
   323                 DevicePrivateBase* devData = (static_cast<Device*>(thisObj))->getDeviceData();
       
   324                 DevicePrivateBase* soData = so->getServiceData();
       
   325                 soData->SetParent( devData ); 
       
   326                 devData->AddChild( soData );
   253                 ret = so;
   327                 ret = so;
   254                 }
   328                 }
   255             }
   329             }
   256         }
   330         }
   257     else if ( m_func == Device::listProviders )
   331     else if ( m_func == Device::listProviders )
   260         }
   334         }
   261 
   335 
   262     return ret;
   336     return ret;
   263     }
   337     }
   264 
   338 
       
   339 MDeviceBinding* Device::GetDeviceBinding()
       
   340 {
       
   341     return m_privateData->m_deviceBinding;
       
   342 }
       
   343 
   265 //END OF FILE
   344 //END OF FILE
   266 
   345 
   267 
   346 
   268 
   347