webengine/device/src/DeviceLiwResult.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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <config.h>
       
    21 #include <lookup.h>
       
    22 #include <liwvariant.h>
       
    23 #include <PropertyNameArray.h>
       
    24 #include "DeviceLiwResult.h"
       
    25 #include "DeviceLiwIterable.h"
       
    26 #include "DeviceLiwMap.h"
       
    27 #include "array_instance.h"
       
    28 
       
    29 const TInt KMaxKeySize = 255;
       
    30 
       
    31 using namespace KJS;
       
    32 const ClassInfo DeviceLiwResult::info = { "DeviceLiwResult", 0/*&ArrayInstance::info*/, 0, 0 };
       
    33 
       
    34 // ============================= LOCAL FUNCTIONS ===============================
       
    35 /*
       
    36 @begin DeviceLiwResultTable 1
       
    37     close DeviceLiwResult::close DontDelete|Function 0
       
    38 @end
       
    39 */
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // ----------------------------------------------------------------------------
       
    44 // DeviceLiwResult::DeviceLiwResult
       
    45 //
       
    46 // ----------------------------------------------------------------------------
       
    47 //
       
    48 DeviceLiwResult::DeviceLiwResult( JSValue* proto)
       
    49     : JSObject(proto)
       
    50     {
       
    51         m_privateData = new DeviceLiwResultPrivate();
       
    52         if (!m_privateData)
       
    53             m_valid = false;
       
    54         else 
       
    55             {
       
    56             m_valid = true;
       
    57             // protect this object
       
    58             KJS::Collector::protect(this);
       
    59             }            
       
    60     }
       
    61 
       
    62 // ----------------------------------------------------------------------------
       
    63 // DeviceLiwResult::~DeviceLiwResult
       
    64 //
       
    65 // ----------------------------------------------------------------------------
       
    66 //
       
    67 DeviceLiwResult::~DeviceLiwResult()
       
    68     {
       
    69         // only can be called by garbage collection after the 
       
    70         // DeviceLiwResult::Close() was called
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // DeviceLiwResult::Close
       
    75 //
       
    76 // ----------------------------------------------------------------------------
       
    77 void DeviceLiwResult::Close( ExecState* exec, bool unmark )
       
    78     {
       
    79     
       
    80     // avoid double close    
       
    81     if(!m_valid) 
       
    82         {   
       
    83         if(unmark) 
       
    84             {
       
    85             // unprotect this to allow the garbage collection to release this jsobject
       
    86             KJS::Collector::unprotect(this);
       
    87             }
       
    88         return;
       
    89         }
       
    90     
       
    91     // close thDeviceLiwIterable
       
    92     if ( exec )
       
    93         {
       
    94         PropertyNameArray propertyNames;
       
    95         this->getPropertyNames( exec, propertyNames );
       
    96         unsigned size = static_cast<unsigned>(propertyNames.size());
       
    97 
       
    98         for (unsigned i = 0; i < size; i++)
       
    99             {
       
   100             JSValue * jsvalue = this->get( exec, propertyNames[i] );
       
   101             if(jsvalue->isObject()) 
       
   102                 {            
       
   103                 JSObject * prop = jsvalue->toObject( exec );
       
   104                 if (prop && prop->inherits( &DeviceLiwIterable::info ))
       
   105                     {
       
   106                     (static_cast<DeviceLiwIterable*>(prop))->Close(exec, true);
       
   107                     }
       
   108                 }                
       
   109             }            
       
   110         }
       
   111     
       
   112     delete m_privateData;
       
   113     m_privateData = NULL;
       
   114     m_valid = false;
       
   115    
       
   116     if(unmark) 
       
   117         {
       
   118         // unprotect this to allow the garbage collection to release this jsobject
       
   119         KJS::Collector::unprotect(this);
       
   120         }
       
   121     }
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // DeviceLiwResult::QuickClose
       
   125 //
       
   126 // ----------------------------------------------------------------------------
       
   127 void DeviceLiwResult::quickClose()
       
   128     { 
       
   129     if(!m_valid) 
       
   130         return
       
   131  
       
   132     delete m_privateData;
       
   133     m_privateData = NULL;
       
   134     m_valid = false;
       
   135    
       
   136     // unprotect this to allow the garbage collection to release this jsobject
       
   137     KJS::Collector::unprotect(this);
       
   138     }
       
   139     
       
   140 // ----------------------------------------------------------------------------
       
   141 // DeviceLiwResult::toString
       
   142 //
       
   143 // 
       
   144 // ----------------------------------------------------------------------------
       
   145 UString DeviceLiwResult::toString( ExecState* /*exec*/ ) const
       
   146     {
       
   147     return "[object DeviceLiwResult]";
       
   148     }
       
   149 
       
   150 // ----------------------------------------------------------------------------
       
   151 // DeviceLiwResult::getOwnPropertySlot
       
   152 //
       
   153 //
       
   154 // ----------------------------------------------------------------------------
       
   155 bool DeviceLiwResult::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
       
   156     {
       
   157     // When the DeviceLiwResult is valid, the check order is
       
   158     //      DeviceLiwResult table => own property => prototype property
       
   159     // When the DeviceLiwResult is invalid (after close), the check order is
       
   160     //      close function in table => prototype property
       
   161 
       
   162     // 1. when it is valid
       
   163     if(m_valid) 
       
   164         {
       
   165         m_privateData->m_propName = propertyName;
       
   166     
       
   167         // 1.1 check DeviceLiwResult table
       
   168         const HashEntry* entry = Lookup::findEntry(&DeviceLiwResultTable, propertyName);
       
   169         if (entry)
       
   170             {
       
   171             slot.setStaticEntry(this, entry, staticValueGetter<DeviceLiwResult>);
       
   172             return true;
       
   173             }
       
   174 
       
   175         // 1.2 check own property
       
   176         if (JSObject::getOwnPropertySlot(exec, propertyName, slot))
       
   177             return true;
       
   178         
       
   179         // 1.3 check prototypes
       
   180         JSObject *proto = static_cast<JSObject *>(this->prototype());
       
   181         while (!proto->isNull() && proto->isObject())
       
   182             {
       
   183             if (proto->getOwnPropertySlot(exec, propertyName, slot))
       
   184                 return true;
       
   185 
       
   186             proto = static_cast<JSObject *>(proto->prototype());
       
   187             }
       
   188         }
       
   189     // 2. when it is invalid
       
   190     else
       
   191         {
       
   192         // 2.1 check close() function
       
   193         if (propertyName == "close")
       
   194             {
       
   195             const HashEntry* entry = Lookup::findEntry(&DeviceLiwResultTable, propertyName);
       
   196             if (entry)
       
   197                 {
       
   198                 slot.setStaticEntry(this, entry, staticValueGetter<DeviceLiwResult>);
       
   199                 return true;
       
   200                 }      
       
   201             }
       
   202 
       
   203         // 2.2 check prototypes
       
   204         JSObject *proto = static_cast<JSObject *>(this->prototype());
       
   205         while (!proto->isNull() && proto->isObject())
       
   206             {
       
   207             if (proto->getOwnPropertySlot(exec, propertyName, slot))
       
   208                 return true;
       
   209 
       
   210             proto = static_cast<JSObject *>(proto->prototype());
       
   211             }
       
   212         }
       
   213     return false;
       
   214     }
       
   215 
       
   216 
       
   217 // ----------------------------------------------------------------------------
       
   218 // DeviceLiwResult::getValueProperty
       
   219 //
       
   220 // ----------------------------------------------------------------------------
       
   221 JSValue* DeviceLiwResult::getValueProperty(ExecState *exec, int token) const
       
   222     {
       
   223     switch (token)
       
   224         {
       
   225         case close:
       
   226             {
       
   227             return new DeviceLiwResultFunc(exec, token);
       
   228             }
       
   229         default:
       
   230             if(m_valid)
       
   231                 return getDirect(m_privateData->m_propName);
       
   232             else
       
   233                 return jsUndefined();
       
   234         }
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // DeviceLiwIterablePrivate constructor
       
   239 //
       
   240 // ---------------------------------------------------------------------------
       
   241 DeviceLiwResultPrivate::DeviceLiwResultPrivate()
       
   242     {
       
   243         // currently, do nothing
       
   244     }
       
   245     
       
   246 // ---------------------------------------------------------------------------
       
   247 // DeviceLiwMapPrivate::Close
       
   248 //
       
   249 // ---------------------------------------------------------------------------
       
   250 void DeviceLiwResultPrivate::Close()
       
   251     {
       
   252         // currently, do nothing
       
   253     }
       
   254     
       
   255 // ----------------------------------------------------------------------------
       
   256 // DeviceLiwResultFunc::DeviceLiwResultFunc
       
   257 //
       
   258 // ----------------------------------------------------------------------------
       
   259 //
       
   260 DeviceLiwResultFunc::DeviceLiwResultFunc( ExecState* exec, int token )
       
   261     : JSObject( exec->lexicalInterpreter()->builtinObjectPrototype() ),
       
   262     m_func( token )
       
   263     {
       
   264     }
       
   265 
       
   266 // ----------------------------------------------------------------------------
       
   267 // DeviceLiwResultFunc::call
       
   268 //
       
   269 // ----------------------------------------------------------------------------
       
   270 //
       
   271 JSValue* DeviceLiwResultFunc::callAsFunction(ExecState* exec, JSObject *thisObj, const List& aArgs )
       
   272     {
       
   273     if (!thisObj->inherits(&DeviceLiwResult::info)) 
       
   274         {
       
   275         return throwError(exec, GeneralError);
       
   276         }
       
   277     DeviceLiwResult *result = static_cast<DeviceLiwResult *>(thisObj);
       
   278 
       
   279     if ( m_func == DeviceLiwResult::close )
       
   280         {
       
   281         result->Close(exec, false);
       
   282         }
       
   283     return jsUndefined();
       
   284     }
       
   285 
       
   286 //END OF FILE
       
   287 
       
   288 
       
   289 
       
   290