src/hbservers/hbthemeserver/hbthemeserverutils.cpp
changeset 2 06ff229162e9
parent 1 f7ac710697a9
child 3 11d3954df52a
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
    49 // cache at the server side to maintain the offsets for
    49 // cache at the server side to maintain the offsets for
    50 // the filename,layout and section combination.
    50 // the filename,layout and section combination.
    51 typedef QHash<QString, int> ServerHashForLayoutDefs;
    51 typedef QHash<QString, int> ServerHashForLayoutDefs;
    52 Q_GLOBAL_STATIC(ServerHashForLayoutDefs, layoutDefsCache)
    52 Q_GLOBAL_STATIC(ServerHashForLayoutDefs, layoutDefsCache)
    53 
    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;
    54 static const int ICON_SOURCES_MAX_SIZE = 8;
    59 static QList<HbIconSource *> iconSources; // cache of recently used icon sources
    55 static QList<HbIconSource *> iconSources; // cache of recently used icon sources
    60 
    56 
    61 HbIconSource *HbThemeServerUtils::getIconSource(const QString &filename)
    57 HbIconSource *HbThemeServerUtils::getIconSource(const QString &filename)
    62 {
    58 {
   105  * \param fileName required stylesheet (complete file path)
   101  * \param fileName required stylesheet (complete file path)
   106  * \param priority layer priority
   102  * \param priority layer priority
   107  * \return offset of the shared style sheet in the shared memory, -1 if error
   103  * \return offset of the shared style sheet in the shared memory, -1 if error
   108  *
   104  *
   109  */
   105  */
   110 int HbThemeServerUtils::getSharedStylesheet(const QString & fileName, HbLayeredStyleLoader::LayerPriority priority)
   106 int HbThemeServerUtils::getSharedStylesheet(const QString &fileName,
   111 {
   107                                             HbLayeredStyleLoader::LayerPriority priority,
       
   108                                             bool *inSharedCache)
       
   109 {
       
   110     int cssOffset = -1;
       
   111     HbSharedCache *sharedCache = 0;
       
   112     if (priority == HbLayeredStyleLoader::Priority_Core) {
       
   113         sharedCache = HbSharedCache::instance();
       
   114         cssOffset = sharedCache->offset(HbSharedCache::Stylesheet, fileName);
       
   115         if (inSharedCache) {
       
   116             *inSharedCache = (cssOffset >= 0) ? true : false;
       
   117         }
       
   118     } else if (inSharedCache) {
       
   119         *inSharedCache = false;
       
   120     }
       
   121 
   112 #ifdef THEME_SERVER_TRACES
   122 #ifdef THEME_SERVER_TRACES
   113     qDebug() << "In " << Q_FUNC_INFO;
   123     qDebug() << "In " << Q_FUNC_INFO;
   114 #endif // THEME_SERVER_TRACES
   124 #endif // THEME_SERVER_TRACES
   115     int cssOffset = -1;
   125     if (cssOffset < 0) {
   116     if (QFile::exists(fileName)) {
   126         if (QFile::exists(fileName)) {
   117         HbCss::Parser parser;
   127             HbCss::Parser parser;
   118         if (!parseCssFile(parser, fileName, cssOffset)) {
   128             if (!parseCssFile(parser, fileName, cssOffset)) {
   119             if (parser.errorCode == HbCss::Parser::OutOfMemoryError) {
   129                 if (parser.errorCode == HbCss::Parser::OutOfMemoryError) {
   120                 return OUT_OF_MEMORY_ERROR;
   130                     return OUT_OF_MEMORY_ERROR;
       
   131                 }
   121             }
   132             }
   122         }
   133         }
   123     }
   134         if (sharedCache) { //sharedCache valid only when priority is Priority_Core
   124     // add the filename and css offset to the secondary cache.
   135             sharedCache->add(HbSharedCache::Stylesheet, fileName, cssOffset);
   125     if ((priority == HbLayeredStyleLoader::Priority_Core) && sharedCache && cssOffset != -1) {
   136         }
   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::exception &) {
       
   133         }
       
   134 
       
   135     }
   137     }
   136     return cssOffset;
   138     return cssOffset;
   137 }
   139 }
   138 
   140 
   139 /**
   141 /**
   218         return layoutDefOffset;
   220         return layoutDefOffset;
   219     }
   221     }
   220 
   222 
   221     if (loader.loadLayoutDefinition(layoutDef, &file, layout, section)) {
   223     if (loader.loadLayoutDefinition(layoutDef, &file, layout, section)) {
   222         layoutDefsCache()->insert(key, layoutDefOffset);
   224         layoutDefsCache()->insert(key, layoutDefOffset);
   223         // add the filename and css offset to the secondary cache.
   225         // add the filename and css offset to the shared cache.
   224         if (sharedCache) {
   226         // no need to check if this item is already present in the
   225             // no need to check if this item is already present in the
   227         // cache as the parsing of the file happens only once
   226             // cache as the parsing of the file happens only once
   228         // in the server side.
   227             // in the server side.
   229         HbSharedCache::instance()->add(HbSharedCache::LayoutDefinition, key, layoutDefOffset);
   228             try {
       
   229                 HbSharedCacheItem cacheItem(key, layoutDefOffset);
       
   230                 sharedCache->append(cacheItem);
       
   231             } catch (std::bad_alloc &badAlloc) {
       
   232                 // item is not appended.
       
   233                 Q_UNUSED(badAlloc)
       
   234             }
       
   235         }
       
   236     } else {
   230     } else {
   237         // load failed
   231         // load failed
   238         layoutDef->~LayoutDefinition();
   232         layoutDef->~LayoutDefinition();
   239         manager->free(layoutDefOffset);
   233         manager->free(layoutDefOffset);
   240         layoutDefOffset = -1;
   234         layoutDefOffset = -1;
   250 {
   244 {
   251     HbDeviceProfileDatabase::instance(HbMemoryManager::SharedMemory);
   245     HbDeviceProfileDatabase::instance(HbMemoryManager::SharedMemory);
   252 }
   246 }
   253 
   247 
   254 /**
   248 /**
   255  * Creates/Returns the Shared Cache.
       
   256  *
       
   257  */
       
   258 HbSharedCache *HbThemeServerUtils::createSharedCache()
       
   259 {
       
   260     if (!sharedCache) {
       
   261         // secondary cache is not created. Create it.
       
   262         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory)
       
   263         try {
       
   264             serverSecondaryCacheOffset = manager->alloc(sizeof(HbSharedCache));
       
   265             sharedCache = new((char*)manager->base() +  serverSecondaryCacheOffset)
       
   266             HbSharedCache(HbMemoryManager::SharedMemory);
       
   267             // reserving memory so that realloc calls will be minimized in future.
       
   268             sharedCache->reserve(NumberOfSharedCacheItems);
       
   269         } catch (std::exception &) {
       
   270             if (serverSecondaryCacheOffset != -1) {
       
   271                 if (sharedCache) {
       
   272                     sharedCache->~HbSharedCache();
       
   273                     sharedCache = 0;
       
   274                 }
       
   275                 manager->free(serverSecondaryCacheOffset);
       
   276                 serverSecondaryCacheOffset = -1;
       
   277             }
       
   278         }
       
   279     }
       
   280     return sharedCache;
       
   281 }
       
   282 
       
   283 /**
       
   284  * Returns the Secondary Cache Offset.
       
   285  *
       
   286  */
       
   287 int HbThemeServerUtils::sharedCacheOffset()
       
   288 {
       
   289     return serverSecondaryCacheOffset;
       
   290 }
       
   291 
       
   292 /**
       
   293  * Removes fxml document from the shared memory and effects cache
   249  * Removes fxml document from the shared memory and effects cache
   294  *
   250  *
   295  * \param fileName of the removed fxml file
   251  * \param fileName of the removed fxml file
   296  * \return true if effect was found and removed, false otherwise
   252  * \return true if effect was found and removed, false otherwise
   297  */
   253  */
   298 bool HbThemeServerUtils::removeSharedEffect(const QString &fileName)
   254 bool HbThemeServerUtils::removeSharedEffect(const QString &fileName)
   299 {
   255 {
   300     if (effCache()->contains(fileName)) {
   256     if (effCache()->contains(fileName)) {
   301         effCache()->remove(fileName);
   257         effCache()->remove(fileName);
   302 
   258         HbSharedCache::instance()->remove(HbSharedCache::Effect, fileName);
   303         if (sharedCache) {
       
   304             int count = sharedCache->count();
       
   305             for (int i = 0; i < count ; i++) {
       
   306                 QString cacheKey = sharedCache->at(i).key;
       
   307                 if (fileName == cacheKey) {
       
   308                     sharedCache->remove(i,1);
       
   309                     break;
       
   310                 }
       
   311             }
       
   312         }
       
   313         return true;
   259         return true;
   314     }
   260     }
   315     return false;
   261     return false;
   316 }
   262 }
   317 
   263 
   319  * Clears fxml documents from the shared memory and effects cache
   265  * Clears fxml documents from the shared memory and effects cache
   320  *
   266  *
   321  */
   267  */
   322 void HbThemeServerUtils::clearSharedEffects()
   268 void HbThemeServerUtils::clearSharedEffects()
   323 {
   269 {
       
   270     HbSharedCache *cache = HbSharedCache::instance();
   324     HbServerCache::const_iterator iterEnd(effCache()->constEnd());
   271     HbServerCache::const_iterator iterEnd(effCache()->constEnd());
   325     for (HbServerCache::const_iterator iter = effCache()->constBegin();
   272     for (HbServerCache::const_iterator iter = effCache()->constBegin();iter != iterEnd;++iter) {
   326             iter != iterEnd;
   273         cache->remove(HbSharedCache::Effect, iter.key());
   327             ++iter) {
       
   328         if (sharedCache) {
       
   329             int count = sharedCache->count();
       
   330             for (int i = 0; i < count ; i++) {
       
   331                 QString cacheKey = sharedCache->at(i).key;
       
   332                 if (cacheKey == iter.key()) {
       
   333                     sharedCache->remove(i,1);
       
   334                     break;
       
   335                 }
       
   336             }
       
   337         }
       
   338     }
   274     }
   339     effCache()->clear();
   275     effCache()->clear();
   340 }
   276 }
   341 
   277 
   342 /**
   278 /**
   409             manager->free(effOffset);
   345             manager->free(effOffset);
   410             return -1;
   346             return -1;
   411         }
   347         }
   412 
   348 
   413         // add the filename and css offset to the secondary cache.
   349         // add the filename and css offset to the secondary cache.
   414         if (sharedCache) {
   350         // no need to check if this item is already present in the
   415             // no need to check if this item is already present in the
   351         // cache as the parsing of the file happens only once
   416             // cache as the parsing of the file happens only once
   352         // in the server side.
   417             // in the server side.
   353         HbSharedCache::instance()->add(HbSharedCache::Effect, fileName, effOffset);
   418             try {
       
   419                 HbSharedCacheItem cacheItem(fileName, effOffset);
       
   420                 sharedCache->append(cacheItem);
       
   421             } catch (std::exception &) {
       
   422             }
       
   423         }
       
   424     }
   354     }
   425 
   355 
   426 #ifdef THEME_SERVER_TRACES
   356 #ifdef THEME_SERVER_TRACES
   427     qDebug() << "returning offset: " << effOffset;
   357     qDebug() << "returning offset: " << effOffset;
   428 #endif // THEME_SERVER_TRACES
   358 #endif // THEME_SERVER_TRACES
   452         // removed from actual cache.
   382         // removed from actual cache.
   453         delete cache->cacheHandle().take(itemToRemove->fileName);
   383         delete cache->cacheHandle().take(itemToRemove->fileName);
   454     }
   384     }
   455 }
   385 }
   456 
   386 
   457 /**
       
   458  * sharedCacheItemOffset  function returns the offset of the cache item
       
   459  * for the given key
       
   460  * \param key
       
   461  *
       
   462  */
       
   463 int HbThemeServerUtils::sharedCacheItemOffset(const QString & key)
       
   464 {
       
   465     if (sharedCache) {
       
   466         int count = sharedCache->count();
       
   467         for (int i = 0; i < count ; i++) {
       
   468             if (key == sharedCache->at(i).key) {
       
   469                 return sharedCache->at(i).offset;
       
   470             }
       
   471         }
       
   472     }
       
   473     return -1;
       
   474 }