src/hbservers/hbthemeserver/hbthemeserverutils.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbServers module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbthemeserverutils_p.h"
       
    27 
       
    28 #include <QHash>
       
    29 #include <QString>
       
    30 #include <QFile>
       
    31 #include <QDebug>
       
    32 
       
    33 #include "hbmemoryutils_p.h"
       
    34 #include "hbcssparser_p.h"
       
    35 #include "hbeffectfxmldata_p.h"
       
    36 #include "hbeffectxmlparser_p.h"
       
    37 #include "hbdeviceprofiledatabase_p.h"
       
    38 #include "hbthemeperf_p.h"
       
    39 #include "hbcache_p.h"
       
    40 #include "hbiconsource_p.h"
       
    41 #include "hbwidgetloadersyntax_p.h"
       
    42 #include "hbwidgetloaderactions_p.h"
       
    43 
       
    44 //Hash of fileName-offset
       
    45 typedef QHash<QString, int> HbServerCache;
       
    46 
       
    47 Q_GLOBAL_STATIC(HbServerCache, effCache)
       
    48 
       
    49 // cache at the server side to maintain the offsets for
       
    50 // the filename,layout and section combination.
       
    51 typedef QHash<QString, int> ServerHashForLayoutDefs;
       
    52 Q_GLOBAL_STATIC(ServerHashForLayoutDefs, layoutDefsCache)
       
    53 
       
    54 HbSharedCache *sharedCache = 0;
       
    55 int HbThemeServerUtils::serverSecondaryCacheOffset = -1;
       
    56 static const int NumberOfSharedCacheItems = 50;
       
    57 
       
    58 static const int ICON_SOURCES_MAX_SIZE = 8;
       
    59 static QList<HbIconSource *> iconSources; // cache of recently used icon sources
       
    60 
       
    61 HbIconSource *HbThemeServerUtils::getIconSource(const QString &filename)
       
    62 {
       
    63     foreach(HbIconSource *source, iconSources) {
       
    64         if (source->filename() == filename) {
       
    65             return source;
       
    66         }
       
    67     }
       
    68     // Add new icon source last in the list, delete the first entry if the max size is exceeded.
       
    69     QScopedPointer<HbIconSource> tempHbIconSource(new HbIconSource(filename));
       
    70     HbIconSource *newSource = tempHbIconSource.data();
       
    71 
       
    72     if (iconSources.count() >= ICON_SOURCES_MAX_SIZE) {
       
    73         iconSources.removeFirst();
       
    74     }
       
    75     iconSources.append(newSource);
       
    76     tempHbIconSource.take();
       
    77     return newSource;
       
    78 }
       
    79 
       
    80 /**
       
    81  * formatFromPath
       
    82  */
       
    83 QString HbThemeServerUtils::formatFromPath(const QString &iconPath)
       
    84 {
       
    85     QString suffix = QFileInfo(iconPath).suffix().toUpper();
       
    86 
       
    87     if (suffix == "SVGZ") {
       
    88         return "SVG";
       
    89     }
       
    90 
       
    91     if (suffix == "QPIC") {
       
    92         return "PIC";
       
    93     }
       
    94     if (suffix == "XML" || suffix == "AXML" || suffix == "FXML") {
       
    95         return "BLOB";
       
    96     }
       
    97     return suffix;
       
    98 }
       
    99 
       
   100 /**
       
   101  * HbThemeServerUtils::getSharedStylesheet() parses the requested stylesheet
       
   102  * in shared memory and returns its offset. The offset could be -1 in case of
       
   103  * OOM condition or any error while parsing the css file.
       
   104  *
       
   105  * \param fileName required stylesheet (complete file path)
       
   106  * \param priority layer priority
       
   107  * \return offset of the shared style sheet in the shared memory, -1 if error
       
   108  *
       
   109  */
       
   110 int HbThemeServerUtils::getSharedStylesheet(const QString & fileName, HbLayeredStyleLoader::LayerPriority priority)
       
   111 {
       
   112 #ifdef THEME_SERVER_TRACES
       
   113     qDebug() << "In " << Q_FUNC_INFO;
       
   114 #endif // THEME_SERVER_TRACES
       
   115     int cssOffset = -1;
       
   116     if (QFile::exists(fileName)) {
       
   117         HbCss::Parser parser;
       
   118         if (!parseCssFile(parser, fileName, cssOffset)) {
       
   119             if (parser.errorCode == HbCss::Parser::OutOfMemoryError) {
       
   120                 return OUT_OF_MEMORY_ERROR;
       
   121             }
       
   122         }
       
   123     }
       
   124     // add the filename and css offset to the secondary cache.
       
   125     if ((priority == HbLayeredStyleLoader::Priority_Core) && sharedCache && cssOffset != -1) {
       
   126         // no need to check if this item is already present in the
       
   127         // cache as the parsing of the file happens only once
       
   128         // in the server side.
       
   129         try {
       
   130             HbSharedCacheItem cacheItem(fileName, cssOffset);
       
   131             sharedCache->append(cacheItem);
       
   132         } catch (std::bad_alloc &badAlloc) {
       
   133             Q_UNUSED(badAlloc)
       
   134             // item is not appended .
       
   135         }
       
   136 
       
   137     }
       
   138     return cssOffset;
       
   139 }
       
   140 
       
   141 /**
       
   142  * HbThemeServerUtils::parseCssFile()
       
   143  *
       
   144  * Returns false in case Css file has some error or there is not enough memory
       
   145  */
       
   146 bool HbThemeServerUtils::parseCssFile(HbCss::Parser &parser, const QString &fileName, int &cssOffset)
       
   147 {
       
   148     bool retVal = false;
       
   149     // 1. Create a styleSheet in shared memory
       
   150     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory) ;
       
   151     HB_START_SHAREDMEMORY_PRINT("");
       
   152     HB_START_HEAPMEMORY_PRINT("");
       
   153     HbCss::StyleSheet *styleSheet = 0;
       
   154     try {
       
   155         cssOffset = manager->alloc(sizeof(HbCss::StyleSheet));
       
   156         styleSheet = new((char*)manager->base() + cssOffset) HbCss::StyleSheet(HbMemoryManager::SharedMemory);
       
   157     } catch (std::bad_alloc &badAlloc) {
       
   158         Q_UNUSED(badAlloc)
       
   159         // if manager->alloc in the previous try block suceeds but creation of
       
   160         // HbCss::StyleSheet on shared memory failed
       
   161         if (cssOffset != -1) {
       
   162             manager->free(cssOffset);
       
   163             cssOffset = -1;
       
   164         }
       
   165         // if manager->alloc itself failed, in that case cssOffset will still be -1,
       
   166         // just return offset as -1 to represent error
       
   167         return retVal;
       
   168     }
       
   169     // 2. Parse the required file into styleSheet.
       
   170     parser.init(fileName, true);
       
   171 
       
   172     if (parser.parse(styleSheet)) {
       
   173         retVal = true;
       
   174     } else {
       
   175         //parser::parse returns false in a number of scenarios
       
   176         // 1. css file has some error
       
   177         // 2. shared memory operations on HbVector/HbString/HbVariant threw an exception
       
   178         // in either case free the memory occupied by stylesheet
       
   179         HbMemoryUtils::release<HbCss::StyleSheet>(styleSheet);
       
   180         manager->free(cssOffset);
       
   181         cssOffset = -1;
       
   182     }
       
   183     HB_END_SHAREDMEMORY_PRINT("");
       
   184     HB_END_HEAPMEMORY_PRINT("");
       
   185     return retVal;
       
   186 }
       
   187 
       
   188 /*
       
   189   Returns of the offset for the given filename,layout and section name.
       
   190  */
       
   191 
       
   192 int HbThemeServerUtils::getSharedLayoutDefinition(const QString & fileName, const QString &layout, const QString &section)
       
   193 {
       
   194     int layoutDefOffset = -1;
       
   195     // check in the cache.
       
   196     QString key(fileName + layout + section);
       
   197     if (layoutDefsCache()->contains(key)) {
       
   198         layoutDefOffset = layoutDefsCache()->value(key);
       
   199         return layoutDefOffset;
       
   200     }
       
   201     HbWidgetLoaderActions loader(HbMemoryManager::SharedMemory);
       
   202     HbWidgetLoaderSyntax widgetMLSyntax(&loader);
       
   203 
       
   204     QFile file(fileName);
       
   205     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
   206         qWarning("Unable to open file");
       
   207         return -1;
       
   208     }
       
   209 #ifdef THEME_SERVER_TRACES
       
   210     qDebug() << "Trying to load: " << fileName << "::" << layout << "::" << section;
       
   211 #endif // THEME_SERVER_TRACES
       
   212 
       
   213     bool load = widgetMLSyntax.load(&file, layout, section);
       
   214     if (load) {
       
   215         layoutDefOffset = loader.getLayoutDefintionOffset();
       
   216     } else {
       
   217         // load() failed. free the memory
       
   218         LayoutDefinition *layoutDef =
       
   219             HbMemoryUtils::getAddress<LayoutDefinition>(HbMemoryManager::SharedMemory,
       
   220                     loader.getLayoutDefintionOffset());
       
   221         if (layoutDef) {
       
   222             GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   223             layoutDef->~LayoutDefinition();
       
   224             manager->free(loader.getLayoutDefintionOffset());
       
   225             loader.setLayoutDefintionOffset(-1);
       
   226         }
       
   227     }
       
   228 
       
   229     if (layoutDefOffset != -1) {
       
   230         layoutDefsCache()->insert(key, layoutDefOffset);
       
   231         // add the filename and css offset to the secondary cache.
       
   232         if (sharedCache) {
       
   233             // no need to check if this item is already present in the
       
   234             // cache as the parsing of the file happens only once
       
   235             // in the server side.
       
   236             try {
       
   237                 HbSharedCacheItem cacheItem(key, layoutDefOffset);
       
   238                 sharedCache->append(cacheItem);
       
   239             } catch (std::bad_alloc &badAlloc) {
       
   240                 // item is not appended.
       
   241                 Q_UNUSED(badAlloc)
       
   242             }
       
   243         }
       
   244     }
       
   245 
       
   246     return layoutDefOffset;
       
   247 }
       
   248 
       
   249 /**
       
   250  * Creates the singleton HbDeviceProfileDatabase instance in shared memory
       
   251  *
       
   252  */
       
   253 void HbThemeServerUtils::createDeviceProfileDatabase()
       
   254 {
       
   255     HbDeviceProfileDatabase::instance(HbMemoryManager::SharedMemory);
       
   256 }
       
   257 
       
   258 /**
       
   259  * Creates/Returns the Shared Cache.
       
   260  *
       
   261  */
       
   262 HbSharedCache *HbThemeServerUtils::createSharedCache()
       
   263 {
       
   264     if (!sharedCache) {
       
   265         // secondary cache is not created. Create it.
       
   266         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory)
       
   267         try {
       
   268             serverSecondaryCacheOffset = manager->alloc(sizeof(HbSharedCache));
       
   269             sharedCache = new((char*)manager->base() +  serverSecondaryCacheOffset)
       
   270             HbSharedCache(HbMemoryManager::SharedMemory);
       
   271             // reserving memory so that realloc calls will be minimized in future.
       
   272             sharedCache->reserve(NumberOfSharedCacheItems);
       
   273         } catch (std::bad_alloc &exception) {
       
   274             Q_UNUSED(exception)
       
   275             if (serverSecondaryCacheOffset != -1) {
       
   276                 manager->free(serverSecondaryCacheOffset);
       
   277             }
       
   278             serverSecondaryCacheOffset = -1;
       
   279             sharedCache = 0;
       
   280         }
       
   281     }
       
   282     return sharedCache;
       
   283 }
       
   284 
       
   285 /**
       
   286  * Returns the Secondary Cache Offset.
       
   287  *
       
   288  */
       
   289 int HbThemeServerUtils::sharedCacheOffset()
       
   290 {
       
   291     return serverSecondaryCacheOffset;
       
   292 }
       
   293 
       
   294 /**
       
   295  * getSharedEffect parses the requested fxml document into the shared
       
   296  * memory and returns the offset of the parsed data.
       
   297  *
       
   298  * \param fileName requested fxml file
       
   299  * \return offset of the parsed effect data in the shared memory, -1 if error
       
   300  *
       
   301  */
       
   302 int HbThemeServerUtils::getSharedEffect(const QString &fileName)
       
   303 {
       
   304 #ifdef THEME_SERVER_TRACES
       
   305     qDebug() << "In " << Q_FUNC_INFO << fileName;
       
   306 #endif // THEME_SERVER_TRACES
       
   307 
       
   308     int effOffset = -1;
       
   309     if (effCache()->contains(fileName)) {
       
   310         effOffset = effCache()->value(fileName);
       
   311 
       
   312 #ifdef THEME_SERVER_TRACES
       
   313         qDebug() << "Got effect in cache: " << fileName << "offset=" << effOffset;
       
   314 #endif //THEME_SERVER_TRACES
       
   315 
       
   316     } else {
       
   317 
       
   318 #ifdef THEME_SERVER_TRACES
       
   319         qDebug() << "Not in cache, parsing: " << fileName;
       
   320 #endif //THEME_SERVER_TRACES
       
   321 
       
   322         // 1. Create an effect in shared memory
       
   323         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   324         HbEffectFxmlData *data = 0;
       
   325         try {
       
   326             effOffset = manager->alloc(sizeof(HbEffectFxmlData));
       
   327             data = new((char*)manager->base() + effOffset) HbEffectFxmlData(HbMemoryManager::SharedMemory);
       
   328         } catch (std::bad_alloc &badAlloc) {
       
   329             Q_UNUSED(badAlloc)
       
   330             // if manager->alloc in the previous try block suceeds but creation of
       
   331             // HbEffectFxmlData on shared memory failed
       
   332             if (effOffset != -1) {
       
   333                 manager->free(effOffset);
       
   334                 effOffset = -1;
       
   335             }
       
   336             // if manager->alloc itself failed, in that case effOffset will still be -1,
       
   337             // just return offset as -1 to represent error
       
   338             return effOffset;
       
   339         }
       
   340         // 2. Parse the file.
       
   341         HbEffectXmlParser parser;
       
   342 
       
   343 
       
   344         QFile f(fileName);
       
   345         if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
       
   346             try {
       
   347                 parser.read(&f, data);
       
   348                 f.close();
       
   349 
       
   350                 // 3. Mark an entry for this styleSheet into the table
       
   351                 effCache()->insert(fileName, effOffset);
       
   352             } catch (std::bad_alloc &badAlloc) {
       
   353                 Q_UNUSED(badAlloc)
       
   354                 f.close();
       
   355                 //HbMemoryUtils::release<HbEffectFxmlData>(data);
       
   356                 manager->free(effOffset);
       
   357                 effOffset = -1;
       
   358             }
       
   359 
       
   360         } else {
       
   361 #ifdef THEME_SERVER_TRACES
       
   362             qWarning() << "Cannot open" << fileName;
       
   363 #endif // THEME_SERVER_TRACES
       
   364             //-1 represents invalid offset
       
   365             manager->free(effOffset);
       
   366             effOffset = -1;
       
   367         }
       
   368 
       
   369         // add the filename and css offset to the secondary cache.
       
   370         if (sharedCache && effOffset != -1) {
       
   371             // no need to check if this item is already present in the
       
   372             // cache as the parsing of the file happens only once
       
   373             // in the server side.
       
   374             try {
       
   375                 HbSharedCacheItem cacheItem(fileName, effOffset);
       
   376                 sharedCache->append(cacheItem);
       
   377             } catch (std::bad_alloc &badAlloc) {
       
   378                 // item is not appended.
       
   379                 Q_UNUSED(badAlloc)
       
   380             }
       
   381         }
       
   382     }
       
   383 
       
   384 #ifdef THEME_SERVER_TRACES
       
   385     qDebug() << "returning offset: " << effOffset;
       
   386 #endif // THEME_SERVER_TRACES
       
   387 
       
   388     return effOffset;
       
   389 }
       
   390 
       
   391 /**
       
   392  * cleanupUnusedCss  function removes css-resources (stylesheets), whose reference count
       
   393  * is zero, it also releases the shared memory occupied by those resources.
       
   394  * \param cache server css-cache
       
   395  *
       
   396  */
       
   397 void HbThemeServerUtils::cleanupUnusedCss(HbCache *cache)
       
   398 {
       
   399     QList<HbCacheItem*> list = cache->lruList();
       
   400     while (!list.isEmpty()) {
       
   401         HbCacheItem* itemToRemove = list.takeFirst();
       
   402         if (itemToRemove->offset != -1) {
       
   403             HbCss::StyleSheet *styleSheet =
       
   404                 HbMemoryUtils::getAddress<HbCss::StyleSheet>(HbMemoryManager::SharedMemory,
       
   405                         itemToRemove->offset);
       
   406             HbMemoryUtils::release<HbCss::StyleSheet>(styleSheet);
       
   407             itemToRemove->offset = -1;
       
   408         }
       
   409         //Since we are cleaning up css-resources whose ref-count is zero, these entries will be
       
   410         // removed from actual cache.
       
   411         cache->cacheHandle().remove(itemToRemove->fileName);
       
   412     }
       
   413 }
       
   414 
       
   415 /**
       
   416  * sharedCacheItemOffset  function returns the offset of the cache item
       
   417  * for the given key
       
   418  * \param key
       
   419  *
       
   420  */
       
   421 int HbThemeServerUtils::sharedCacheItemOffset(const QString & key)
       
   422 {
       
   423     if (sharedCache) {
       
   424         int count = sharedCache->count();
       
   425         for (int i = 0; i < count ; i++) {
       
   426             if (key == sharedCache->at(i).key) {
       
   427                 return sharedCache->at(i).offset;
       
   428             }
       
   429         }
       
   430     }
       
   431     return -1;
       
   432 }