VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/VirtualBoxTranslator.cpp@ 92039

Last change on this file since 92039 was 92039, checked in by vboxsync, 3 years ago

Main: bugref:1909: Fixed memory leak in the translation engine

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.7 KB
Line 
1/* $Id: VirtualBoxTranslator.cpp 92039 2021-10-25 14:27:59Z vboxsync $ */
2/** @file
3 * VirtualBox Translator class.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <iprt/asm.h>
19#include <iprt/ctype.h>
20#include <iprt/err.h>
21#include <iprt/locale.h>
22#include <iprt/once.h>
23#include <iprt/path.h>
24#include <iprt/string.h>
25#include <iprt/thread.h>
26#include <iprt/strcache.h>
27
28#include "Global.h"
29#include "VirtualBoxBase.h"
30#include "QMTranslator.h"
31#include "VirtualBoxTranslator.h"
32
33#define TRANSLATOR_CACHE_SIZE 32
34
35/** Init once for the critical section. */
36static RTONCE g_Once = RTONCE_INITIALIZER;
37RTCRITSECTRW VirtualBoxTranslator::s_instanceRwLock;
38VirtualBoxTranslator *VirtualBoxTranslator::s_pInstance = NULL;
39static RTONCE g_tlsOnce = RTONCE_INITIALIZER;
40static RTTLS g_idxTls = NIL_RTTLS;
41
42typedef std::pair<const char *, const char *> LastTranslation;
43
44/**
45 * @callback_method_impl{FNRTONCE}
46 */
47static DECLCALLBACK(int32_t) initLock(void *pvUser)
48{
49 RT_NOREF(pvUser);
50 return VirtualBoxTranslator::initCritSect();
51}
52
53
54/**
55 * @callback_method_impl{FNRTTLSDTOR, Destroys the cache during thread termination.}
56 */
57static DECLCALLBACK(void) freeThreadCache(void *pvValue) RT_NOTHROW_DEF
58{
59 if (pvValue != NULL)
60 {
61 LastTranslation *pCache = (LastTranslation *)pvValue;
62 delete pCache;
63 }
64}
65
66
67/**
68 * @callback_method_impl{FNRTONCECLEANUP, Destroys TLS index during the process termination.}
69 */
70DECLCALLBACK(void) uninitTls(void *pvUser, bool fLazyCleanUpOk)
71{
72 RT_NOREF(pvUser);
73 if (fLazyCleanUpOk && g_idxTls != NIL_RTTLS)
74 {
75 RTTlsFree(g_idxTls);
76 g_idxTls = NIL_RTTLS;
77 }
78}
79
80
81/**
82 * @callback_method_impl{FNRTONCE}
83 */
84static DECLCALLBACK(int32_t) initTls(void *pvUser)
85{
86 RT_NOREF(pvUser);
87 RTTlsAllocEx(&g_idxTls, &freeThreadCache);
88 return VINF_SUCCESS;
89}
90
91
92VirtualBoxTranslator::VirtualBoxTranslator()
93 : util::RWLockHandle(util::LOCKCLASS_TRANSLATOR)
94 , m_cInstanceRefs(0)
95 , m_pDefaultComponent(NULL)
96 , m_strLanguage("C")
97 , m_hStrCache(NIL_RTSTRCACHE)
98{
99 int rc = RTStrCacheCreate(&m_hStrCache, "API Translation");
100 m_rcCache = rc;
101 if (RT_FAILURE(rc))
102 m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
103}
104
105
106VirtualBoxTranslator::~VirtualBoxTranslator()
107{
108 if (g_idxTls != NIL_RTTLS)
109 {
110 void *pvTlsValue = NULL;
111 int rc = RTTlsGetEx(g_idxTls, &pvTlsValue);
112 if (RT_SUCCESS(rc))
113 {
114 freeThreadCache(pvTlsValue);
115 RTTlsSet(g_idxTls, NULL);
116 }
117 }
118
119 m_pDefaultComponent = NULL;
120
121 for (TranslatorList::iterator it = m_lTranslators.begin();
122 it != m_lTranslators.end();
123 ++it)
124 {
125 if (it->pTranslator != NULL)
126 delete it->pTranslator;
127 it->pTranslator = NULL;
128 }
129 if (m_hStrCache != NIL_RTSTRCACHE)
130 {
131 RTStrCacheDestroy(m_hStrCache);
132 m_hStrCache = NIL_RTSTRCACHE;
133 m_rcCache = VERR_WRONG_ORDER;
134 }
135}
136
137
138/**
139 * Get or create a translator instance (singelton), referenced.
140 *
141 * The main reference is held by the main VBox singelton objects (VirtualBox,
142 * VirtualBoxClient) tying it's lifetime to theirs.
143 */
144/* static */
145VirtualBoxTranslator *VirtualBoxTranslator::instance()
146{
147 int rc = RTOnce(&g_Once, initLock, NULL);
148 if (RT_SUCCESS(rc))
149 {
150 RTCritSectRwEnterShared(&s_instanceRwLock);
151 VirtualBoxTranslator *pInstance = s_pInstance;
152 if (RT_LIKELY(pInstance != NULL))
153 {
154 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
155 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
156 RTCritSectRwLeaveShared(&s_instanceRwLock);
157 return pInstance;
158 }
159
160 /* Maybe create the instance: */
161 RTCritSectRwLeaveShared(&s_instanceRwLock);
162 RTCritSectRwEnterExcl(&s_instanceRwLock);
163 pInstance = s_pInstance;
164 if (pInstance == NULL)
165 s_pInstance = pInstance = new VirtualBoxTranslator();
166 ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
167 RTCritSectRwLeaveExcl(&s_instanceRwLock);
168 return pInstance;
169 }
170 return NULL;
171}
172
173
174/* static */
175VirtualBoxTranslator *VirtualBoxTranslator::tryInstance() RT_NOEXCEPT
176{
177 int rc = RTOnce(&g_Once, initLock, NULL);
178 if (RT_SUCCESS(rc))
179 {
180 RTCritSectRwEnterShared(&s_instanceRwLock);
181 VirtualBoxTranslator *pInstance = s_pInstance;
182 if (RT_LIKELY(pInstance != NULL))
183 {
184 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
185 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
186 }
187 RTCritSectRwLeaveShared(&s_instanceRwLock);
188 return pInstance;
189 }
190 return NULL;
191}
192
193
194/**
195 * Release translator reference previous obtained via instance() or
196 * tryinstance().
197 */
198void VirtualBoxTranslator::release()
199{
200 RTCritSectRwEnterShared(&s_instanceRwLock);
201 uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
202 Assert(cRefs < _8K);
203 if (RT_LIKELY(cRefs > 0))
204 RTCritSectRwLeaveShared(&s_instanceRwLock);
205 else
206 {
207 /* Looks like we've got the last reference. Must switch to exclusive
208 mode for safe cleanup. */
209 ASMAtomicIncU32(&m_cInstanceRefs);
210 RTCritSectRwLeaveShared(&s_instanceRwLock);
211 RTCritSectRwEnterExcl(&s_instanceRwLock);
212 cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
213 Assert(cRefs < _8K);
214 if (cRefs == 0)
215 {
216 s_pInstance = NULL;
217 delete this;
218 }
219 RTCritSectRwLeaveExcl(&s_instanceRwLock);
220 }
221}
222
223
224HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
225{
226 AssertReturn(aVirtualBox, E_INVALIDARG);
227
228 ComPtr<ISystemProperties> pSystemProperties;
229 HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
230 if (SUCCEEDED(hrc))
231 {
232 com::Bstr bstrLocale;
233 hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
234 if (SUCCEEDED(hrc))
235 {
236 int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
237 if (RT_FAILURE(vrc))
238 hrc = Global::vboxStatusCodeToCOM(vrc);
239 }
240 }
241 return hrc;
242}
243
244
245int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
246{
247 int rc = VINF_SUCCESS;
248 char szLocale[256];
249 if (pszLang == NULL || *pszLang == '\0')
250 {
251 rc = RTLocaleQueryNormalizedBaseLocaleName(szLocale, sizeof(szLocale));
252 if (RT_SUCCESS(rc))
253 pszLang = szLocale;
254 }
255 else
256 {
257 /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
258 size_t cbLang = strlen(pszLang);
259 if ( !(cbLang == 1 && pszLang[0] == 'C')
260 && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
261 && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
262 rc = VERR_INVALID_PARAMETER;
263 }
264 if (RT_SUCCESS(rc))
265 {
266 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
267
268 m_strLanguage = pszLang;
269
270 for (TranslatorList::iterator it = m_lTranslators.begin();
271 it != m_lTranslators.end();
272 ++it)
273 {
274 /* ignore errors from particular translator allowing the use of others */
275 i_loadLanguageForComponent(&(*it), pszLang);
276 }
277 }
278 return rc;
279}
280
281
282int VirtualBoxTranslator::i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang)
283{
284 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
285 int rc;
286 if (strcmp(aLang, "C") != 0)
287 {
288 /* Construct the base filename for the translations: */
289 char szNlsPath[RTPATH_MAX];
290 /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
291 where 'll_CC' could for example be 'en_US' or 'de_CH': */
292 ssize_t cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%s.qm",
293 aComponent->strPath.c_str(), aLang);
294 if (cchOkay > 0)
295 rc = i_setLanguageFile(aComponent, szNlsPath);
296 else
297 rc = VERR_FILENAME_TOO_LONG;
298 if (RT_FAILURE(rc))
299 {
300 /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
301 const char *pszDash = strchr(aLang, '_');
302 if (pszDash && pszDash != aLang)
303 {
304 cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%.*s.qm",
305 aComponent->strPath.c_str(), pszDash - aLang, aLang);
306 if (cchOkay > 0)
307 rc = i_setLanguageFile(aComponent, szNlsPath);
308 }
309 }
310 }
311 else
312 {
313 /* No translator needed for 'C' */
314 delete aComponent->pTranslator;
315 aComponent->pTranslator = NULL;
316 rc = VINF_SUCCESS;
317 }
318 return rc;
319}
320
321
322int VirtualBoxTranslator::i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName)
323{
324 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
325
326 int rc = m_rcCache;
327 if (m_hStrCache != NIL_RTSTRCACHE)
328 {
329 QMTranslator *pNewTranslator;
330 try { pNewTranslator = new QMTranslator(); }
331 catch (std::bad_alloc &) { pNewTranslator = NULL; }
332 if (pNewTranslator)
333 {
334 rc = pNewTranslator->load(aFileName, m_hStrCache);
335 if (RT_SUCCESS(rc))
336 {
337 if (aComponent->pTranslator)
338 delete aComponent->pTranslator;
339 aComponent->pTranslator = pNewTranslator;
340 }
341 else
342 delete pNewTranslator;
343 }
344 else
345 rc = VERR_NO_MEMORY;
346 }
347 else
348 Assert(RT_FAILURE_NP(rc));
349 return rc;
350}
351
352
353int VirtualBoxTranslator::registerTranslation(const char *aTranslationPath,
354 bool aDefault,
355 PTRCOMPONENT *aComponent)
356{
357 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
358 int rc = VERR_GENERAL_FAILURE;
359 if (pCurrInstance != NULL)
360 {
361 rc = pCurrInstance->i_registerTranslation(aTranslationPath, aDefault, aComponent);
362 pCurrInstance->release();
363 }
364 return rc;
365}
366
367
368int VirtualBoxTranslator::i_registerTranslation(const char *aTranslationPath,
369 bool aDefault,
370 PTRCOMPONENT *aComponent)
371{
372 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
373 TranslatorComponent *pComponent;
374 for (TranslatorList::iterator it = m_lTranslators.begin();
375 it != m_lTranslators.end();
376 ++it)
377 {
378 if (it->strPath == aTranslationPath)
379 {
380 pComponent = &(*it);
381 if (aDefault)
382 m_pDefaultComponent = pComponent;
383 *aComponent = (PTRCOMPONENT)pComponent;
384 return VINF_SUCCESS;
385 }
386 }
387
388 try
389 {
390 m_lTranslators.push_back(TranslatorComponent());
391 pComponent = &m_lTranslators.back();
392 }
393 catch(std::bad_alloc &)
394 {
395 return VERR_NO_MEMORY;
396 }
397
398 pComponent->strPath = aTranslationPath;
399 if (aDefault)
400 m_pDefaultComponent = pComponent;
401 *aComponent = (PTRCOMPONENT)pComponent;
402 /* ignore the error during loading because path
403 * could contain no translation for current language */
404 i_loadLanguageForComponent(pComponent, m_strLanguage.c_str());
405 return VINF_SUCCESS;
406}
407
408
409int VirtualBoxTranslator::unregisterTranslation(PTRCOMPONENT aComponent)
410{
411 int rc;
412 if (aComponent != NULL)
413 {
414 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
415 if (pCurrInstance != NULL)
416 {
417 rc = pCurrInstance->i_unregisterTranslation(aComponent);
418 pCurrInstance->release();
419 }
420 else
421 rc = VERR_GENERAL_FAILURE;
422 }
423 else
424 rc = VWRN_NOT_FOUND;
425 return rc;
426}
427
428
429int VirtualBoxTranslator::i_unregisterTranslation(PTRCOMPONENT aComponent)
430{
431 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
432
433 if (aComponent == m_pDefaultComponent)
434 m_pDefaultComponent = NULL;
435
436 for (TranslatorList::iterator it = m_lTranslators.begin();
437 it != m_lTranslators.end();
438 ++it)
439 {
440 if (&(*it) == aComponent)
441 {
442 delete aComponent->pTranslator;
443 m_lTranslators.erase(it);
444 return VINF_SUCCESS;
445 }
446 }
447
448 return VERR_NOT_FOUND;
449}
450
451
452const char *VirtualBoxTranslator::translate(PTRCOMPONENT aComponent,
453 const char *aContext,
454 const char *aSourceText,
455 const char *aComment,
456 const size_t aNum) RT_NOEXCEPT
457{
458 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
459 const char *pszTranslation = aSourceText;
460 if (pCurrInstance != NULL)
461 {
462 pszTranslation = pCurrInstance->i_translate(aComponent, aContext, aSourceText, aComment, aNum);
463 pCurrInstance->release();
464 }
465 return pszTranslation;
466}
467
468
469static LastTranslation *getTlsEntry() RT_NOEXCEPT
470{
471 int rc = RTOnceEx(&g_tlsOnce, initTls, uninitTls, NULL);
472 if (RT_SUCCESS(rc) && RT_LIKELY(g_idxTls != NIL_RTTLS))
473 {
474 LastTranslation *pEntry = (LastTranslation *)RTTlsGet(g_idxTls);
475 if (pEntry != NULL)
476 return pEntry; /* likely */
477
478 /* Create a new entry. */
479 try
480 {
481 pEntry = new LastTranslation();
482 pEntry->first = pEntry->second = "";
483 rc = RTTlsSet(g_idxTls, pEntry);
484 if (RT_SUCCESS(rc))
485 return pEntry;
486 delete pEntry;
487 }
488 catch (std::bad_alloc &)
489 {
490 }
491 }
492 return NULL;
493}
494
495
496const char *VirtualBoxTranslator::i_translate(PTRCOMPONENT aComponent,
497 const char *aContext,
498 const char *aSourceText,
499 const char *aComment,
500 const size_t aNum) RT_NOEXCEPT
501{
502 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
503
504 if (aComponent == NULL)
505 aComponent = m_pDefaultComponent;
506
507 if ( aComponent == NULL
508 || aComponent->pTranslator == NULL)
509 return aSourceText;
510
511 const char *pszSafeSource = NULL;
512 const char *pszTranslation = aComponent->pTranslator->translate(aContext, aSourceText, &pszSafeSource, aComment, aNum);
513 if (pszSafeSource)
514 {
515 LastTranslation *pEntry = getTlsEntry();
516 if (pEntry)
517 {
518 pEntry->first = pszTranslation;
519 pEntry->second = pszSafeSource;
520 }
521 }
522
523 return pszTranslation;
524}
525
526
527const char *VirtualBoxTranslator::trSource(const char *aTranslation) RT_NOEXCEPT
528{
529 const char *pszSource = aTranslation;
530 VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
531 if (pCurInstance != NULL)
532 {
533 LastTranslation *pEntry = getTlsEntry();
534 if ( pEntry != NULL
535 && ( pEntry->first == aTranslation
536 || strcmp(pEntry->first, aTranslation) == 0) )
537 pszSource = pEntry->second;
538 pCurInstance->release();
539 }
540 return pszSource;
541}
542
543
544int32_t VirtualBoxTranslator::initCritSect()
545{
546 return RTCritSectRwInit(&s_instanceRwLock);
547}
548/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette