VirtualBox

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

Last change on this file since 92102 was 92102, checked in by vboxsync, 4 years ago

Main: bugref:1909: Fixed the memory leak in the translation engine using another way

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
1/* $Id: VirtualBoxTranslator.cpp 92102 2021-10-27 13:06:01Z 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.215389.xyz. 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 RTTLS g_idxTlsTr = NIL_RTTLS;
40static RTTLS g_idxTlsSrc = NIL_RTTLS;
41
42/**
43 * @callback_method_impl{FNRTONCE}
44 */
45static DECLCALLBACK(int32_t) initLock(void *pvUser)
46{
47 RT_NOREF(pvUser);
48 return VirtualBoxTranslator::initCritSect();
49}
50
51
52VirtualBoxTranslator::VirtualBoxTranslator()
53 : util::RWLockHandle(util::LOCKCLASS_TRANSLATOR)
54 , m_cInstanceRefs(0)
55 , m_pDefaultComponent(NULL)
56 , m_strLanguage("C")
57 , m_hStrCache(NIL_RTSTRCACHE)
58{
59 RTTlsAllocEx(&g_idxTlsTr, NULL);
60 RTTlsAllocEx(&g_idxTlsSrc, NULL);
61 int rc = RTStrCacheCreate(&m_hStrCache, "API Translation");
62 m_rcCache = rc;
63 if (RT_FAILURE(rc))
64 m_hStrCache = NIL_RTSTRCACHE; /* (loadLanguage will fail) */
65}
66
67
68VirtualBoxTranslator::~VirtualBoxTranslator()
69{
70 if (g_idxTlsTr != NIL_RTTLS)
71 {
72 RTTlsFree(g_idxTlsTr);
73 g_idxTlsTr = NIL_RTTLS;
74 }
75 if (g_idxTlsSrc != NIL_RTTLS)
76 {
77 RTTlsFree(g_idxTlsSrc);
78 g_idxTlsSrc = NIL_RTTLS;
79 }
80
81 m_pDefaultComponent = NULL;
82
83 for (TranslatorList::iterator it = m_lTranslators.begin();
84 it != m_lTranslators.end();
85 ++it)
86 {
87 if (it->pTranslator != NULL)
88 delete it->pTranslator;
89 it->pTranslator = NULL;
90 }
91 if (m_hStrCache != NIL_RTSTRCACHE)
92 {
93 RTStrCacheDestroy(m_hStrCache);
94 m_hStrCache = NIL_RTSTRCACHE;
95 m_rcCache = VERR_WRONG_ORDER;
96 }
97}
98
99
100/**
101 * Get or create a translator instance (singelton), referenced.
102 *
103 * The main reference is held by the main VBox singelton objects (VirtualBox,
104 * VirtualBoxClient) tying it's lifetime to theirs.
105 */
106/* static */
107VirtualBoxTranslator *VirtualBoxTranslator::instance()
108{
109 int rc = RTOnce(&g_Once, initLock, NULL);
110 if (RT_SUCCESS(rc))
111 {
112 RTCritSectRwEnterShared(&s_instanceRwLock);
113 VirtualBoxTranslator *pInstance = s_pInstance;
114 if (RT_LIKELY(pInstance != NULL))
115 {
116 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
117 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
118 RTCritSectRwLeaveShared(&s_instanceRwLock);
119 return pInstance;
120 }
121
122 /* Maybe create the instance: */
123 RTCritSectRwLeaveShared(&s_instanceRwLock);
124 RTCritSectRwEnterExcl(&s_instanceRwLock);
125 pInstance = s_pInstance;
126 if (pInstance == NULL)
127 s_pInstance = pInstance = new VirtualBoxTranslator();
128 ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
129 RTCritSectRwLeaveExcl(&s_instanceRwLock);
130 return pInstance;
131 }
132 return NULL;
133}
134
135
136/* static */
137VirtualBoxTranslator *VirtualBoxTranslator::tryInstance() RT_NOEXCEPT
138{
139 int rc = RTOnce(&g_Once, initLock, NULL);
140 if (RT_SUCCESS(rc))
141 {
142 RTCritSectRwEnterShared(&s_instanceRwLock);
143 VirtualBoxTranslator *pInstance = s_pInstance;
144 if (RT_LIKELY(pInstance != NULL))
145 {
146 uint32_t cRefs = ASMAtomicIncU32(&pInstance->m_cInstanceRefs);
147 Assert(cRefs > 1); Assert(cRefs < _8K); RT_NOREF(cRefs);
148 }
149 RTCritSectRwLeaveShared(&s_instanceRwLock);
150 return pInstance;
151 }
152 return NULL;
153}
154
155
156/**
157 * Release translator reference previous obtained via instance() or
158 * tryinstance().
159 */
160void VirtualBoxTranslator::release()
161{
162 RTCritSectRwEnterShared(&s_instanceRwLock);
163 uint32_t cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
164 Assert(cRefs < _8K);
165 if (RT_LIKELY(cRefs > 0))
166 RTCritSectRwLeaveShared(&s_instanceRwLock);
167 else
168 {
169 /* Looks like we've got the last reference. Must switch to exclusive
170 mode for safe cleanup. */
171 ASMAtomicIncU32(&m_cInstanceRefs);
172 RTCritSectRwLeaveShared(&s_instanceRwLock);
173 RTCritSectRwEnterExcl(&s_instanceRwLock);
174 cRefs = ASMAtomicDecU32(&m_cInstanceRefs);
175 Assert(cRefs < _8K);
176 if (cRefs == 0)
177 {
178 s_pInstance = NULL;
179 delete this;
180 }
181 RTCritSectRwLeaveExcl(&s_instanceRwLock);
182 }
183}
184
185
186HRESULT VirtualBoxTranslator::loadLanguage(ComPtr<IVirtualBox> aVirtualBox)
187{
188 AssertReturn(aVirtualBox, E_INVALIDARG);
189
190 ComPtr<ISystemProperties> pSystemProperties;
191 HRESULT hrc = aVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
192 if (SUCCEEDED(hrc))
193 {
194 com::Bstr bstrLocale;
195 hrc = pSystemProperties->COMGETTER(LanguageId)(bstrLocale.asOutParam());
196 if (SUCCEEDED(hrc))
197 {
198 int vrc = i_loadLanguage(com::Utf8Str(bstrLocale).c_str());
199 if (RT_FAILURE(vrc))
200 hrc = Global::vboxStatusCodeToCOM(vrc);
201 }
202 }
203 return hrc;
204}
205
206
207int VirtualBoxTranslator::i_loadLanguage(const char *pszLang)
208{
209 int rc = VINF_SUCCESS;
210 char szLocale[256];
211 if (pszLang == NULL || *pszLang == '\0')
212 {
213 rc = RTLocaleQueryNormalizedBaseLocaleName(szLocale, sizeof(szLocale));
214 if (RT_SUCCESS(rc))
215 pszLang = szLocale;
216 }
217 else
218 {
219 /* check the pszLang looks like language code, i.e. {ll} or {ll}_{CC} */
220 size_t cbLang = strlen(pszLang);
221 if ( !(cbLang == 1 && pszLang[0] == 'C')
222 && !(cbLang == 2 && RT_C_IS_LOWER(pszLang[0]) && RT_C_IS_LOWER(pszLang[1]))
223 && !(cbLang == 5 && RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLang)))
224 rc = VERR_INVALID_PARAMETER;
225 }
226 if (RT_SUCCESS(rc))
227 {
228 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
229
230 m_strLanguage = pszLang;
231
232 for (TranslatorList::iterator it = m_lTranslators.begin();
233 it != m_lTranslators.end();
234 ++it)
235 {
236 /* ignore errors from particular translator allowing the use of others */
237 i_loadLanguageForComponent(&(*it), pszLang);
238 }
239 }
240 return rc;
241}
242
243
244int VirtualBoxTranslator::i_loadLanguageForComponent(TranslatorComponent *aComponent, const char *aLang)
245{
246 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
247 int rc;
248 if (strcmp(aLang, "C") != 0)
249 {
250 /* Construct the base filename for the translations: */
251 char szNlsPath[RTPATH_MAX];
252 /* Try load language file on form 'VirtualBoxAPI_ll_CC.qm' if it exists
253 where 'll_CC' could for example be 'en_US' or 'de_CH': */
254 ssize_t cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%s.qm",
255 aComponent->strPath.c_str(), aLang);
256 if (cchOkay > 0)
257 rc = i_setLanguageFile(aComponent, szNlsPath);
258 else
259 rc = VERR_FILENAME_TOO_LONG;
260 if (RT_FAILURE(rc))
261 {
262 /* No luck, drop the country part, i.e. 'VirtualBoxAPI_de.qm' or 'VirtualBoxAPI_en.qm': */
263 const char *pszDash = strchr(aLang, '_');
264 if (pszDash && pszDash != aLang)
265 {
266 cchOkay = RTStrPrintf2(szNlsPath, sizeof(szNlsPath), "%s_%.*s.qm",
267 aComponent->strPath.c_str(), pszDash - aLang, aLang);
268 if (cchOkay > 0)
269 rc = i_setLanguageFile(aComponent, szNlsPath);
270 }
271 }
272 }
273 else
274 {
275 /* No translator needed for 'C' */
276 delete aComponent->pTranslator;
277 aComponent->pTranslator = NULL;
278 rc = VINF_SUCCESS;
279 }
280 return rc;
281}
282
283
284int VirtualBoxTranslator::i_setLanguageFile(TranslatorComponent *aComponent, const char *aFileName)
285{
286 AssertReturn(aComponent, VERR_INVALID_PARAMETER);
287
288 int rc = m_rcCache;
289 if (m_hStrCache != NIL_RTSTRCACHE)
290 {
291 QMTranslator *pNewTranslator;
292 try { pNewTranslator = new QMTranslator(); }
293 catch (std::bad_alloc &) { pNewTranslator = NULL; }
294 if (pNewTranslator)
295 {
296 rc = pNewTranslator->load(aFileName, m_hStrCache);
297 if (RT_SUCCESS(rc))
298 {
299 if (aComponent->pTranslator)
300 delete aComponent->pTranslator;
301 aComponent->pTranslator = pNewTranslator;
302 }
303 else
304 delete pNewTranslator;
305 }
306 else
307 rc = VERR_NO_MEMORY;
308 }
309 else
310 Assert(RT_FAILURE_NP(rc));
311 return rc;
312}
313
314
315int VirtualBoxTranslator::registerTranslation(const char *aTranslationPath,
316 bool aDefault,
317 PTRCOMPONENT *aComponent)
318{
319 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
320 int rc = VERR_GENERAL_FAILURE;
321 if (pCurrInstance != NULL)
322 {
323 rc = pCurrInstance->i_registerTranslation(aTranslationPath, aDefault, aComponent);
324 pCurrInstance->release();
325 }
326 return rc;
327}
328
329
330int VirtualBoxTranslator::i_registerTranslation(const char *aTranslationPath,
331 bool aDefault,
332 PTRCOMPONENT *aComponent)
333{
334 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
335 TranslatorComponent *pComponent;
336 for (TranslatorList::iterator it = m_lTranslators.begin();
337 it != m_lTranslators.end();
338 ++it)
339 {
340 if (it->strPath == aTranslationPath)
341 {
342 pComponent = &(*it);
343 if (aDefault)
344 m_pDefaultComponent = pComponent;
345 *aComponent = (PTRCOMPONENT)pComponent;
346 return VINF_SUCCESS;
347 }
348 }
349
350 try
351 {
352 m_lTranslators.push_back(TranslatorComponent());
353 pComponent = &m_lTranslators.back();
354 }
355 catch(std::bad_alloc &)
356 {
357 return VERR_NO_MEMORY;
358 }
359
360 pComponent->strPath = aTranslationPath;
361 if (aDefault)
362 m_pDefaultComponent = pComponent;
363 *aComponent = (PTRCOMPONENT)pComponent;
364 /* ignore the error during loading because path
365 * could contain no translation for current language */
366 i_loadLanguageForComponent(pComponent, m_strLanguage.c_str());
367 return VINF_SUCCESS;
368}
369
370
371int VirtualBoxTranslator::unregisterTranslation(PTRCOMPONENT aComponent)
372{
373 int rc;
374 if (aComponent != NULL)
375 {
376 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
377 if (pCurrInstance != NULL)
378 {
379 rc = pCurrInstance->i_unregisterTranslation(aComponent);
380 pCurrInstance->release();
381 }
382 else
383 rc = VERR_GENERAL_FAILURE;
384 }
385 else
386 rc = VWRN_NOT_FOUND;
387 return rc;
388}
389
390
391int VirtualBoxTranslator::i_unregisterTranslation(PTRCOMPONENT aComponent)
392{
393 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
394
395 if (aComponent == m_pDefaultComponent)
396 m_pDefaultComponent = NULL;
397
398 for (TranslatorList::iterator it = m_lTranslators.begin();
399 it != m_lTranslators.end();
400 ++it)
401 {
402 if (&(*it) == aComponent)
403 {
404 delete aComponent->pTranslator;
405 m_lTranslators.erase(it);
406 return VINF_SUCCESS;
407 }
408 }
409
410 return VERR_NOT_FOUND;
411}
412
413
414const char *VirtualBoxTranslator::translate(PTRCOMPONENT aComponent,
415 const char *aContext,
416 const char *aSourceText,
417 const char *aComment,
418 const size_t aNum) RT_NOEXCEPT
419{
420 VirtualBoxTranslator *pCurrInstance = VirtualBoxTranslator::tryInstance();
421 const char *pszTranslation = aSourceText;
422 if (pCurrInstance != NULL)
423 {
424 pszTranslation = pCurrInstance->i_translate(aComponent, aContext, aSourceText, aComment, aNum);
425 pCurrInstance->release();
426 }
427 return pszTranslation;
428}
429
430
431const char *VirtualBoxTranslator::i_translate(PTRCOMPONENT aComponent,
432 const char *aContext,
433 const char *aSourceText,
434 const char *aComment,
435 const size_t aNum) RT_NOEXCEPT
436{
437 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
438
439 if (aComponent == NULL)
440 aComponent = m_pDefaultComponent;
441
442 if ( aComponent == NULL
443 || aComponent->pTranslator == NULL)
444 return aSourceText;
445
446 const char *pszSafeSource = NULL;
447 const char *pszTranslation = aComponent->pTranslator->translate(aContext, aSourceText, &pszSafeSource, aComment, aNum);
448 if (pszSafeSource)
449 {
450 if (g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
451 {
452 int vrc = RTTlsSet(g_idxTlsTr, (void*)pszTranslation);
453 if (RT_SUCCESS(vrc))
454 vrc = RTTlsSet(g_idxTlsSrc, (void*)pszSafeSource);
455 if (RT_FAILURE(vrc))
456 RTTlsSet(g_idxTlsTr, NULL);
457 }
458 }
459
460 return pszTranslation;
461}
462
463
464const char *VirtualBoxTranslator::trSource(const char *aTranslation) RT_NOEXCEPT
465{
466 const char *pszSource = aTranslation;
467 VirtualBoxTranslator *pCurInstance = VirtualBoxTranslator::tryInstance(); /* paranoia */
468 if (pCurInstance != NULL)
469 {
470 if (g_idxTlsSrc != NIL_RTTLS && g_idxTlsTr != NIL_RTTLS)
471 {
472 const char *pszTranslationTls = NULL;
473 const char *pszSourceTls = NULL;
474 int vrc =RTTlsGetEx(g_idxTlsTr, (void**)&pszTranslationTls);
475 if ( RT_SUCCESS(vrc)
476 && ( pszTranslationTls == aTranslation
477 || strcmp(pszTranslationTls, aTranslation) == 0))
478 {
479 vrc = RTTlsGetEx(g_idxTlsSrc, (void**)&pszSourceTls);
480 if (RT_SUCCESS(vrc))
481 pszSource = pszSourceTls;
482 }
483 }
484 pCurInstance->release();
485 }
486 return pszSource;
487}
488
489
490int32_t VirtualBoxTranslator::initCritSect()
491{
492 return RTCritSectRwInit(&s_instanceRwLock);
493}
494/* 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