WindowsGlobalizationAdapter.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #if defined(ENABLE_INTL_OBJECT) || defined(ENABLE_ES6_CHAR_CLASSIFIER)
  7. #ifdef INTL_WINGLOB
  8. #include "Windows.Globalization.h"
  9. #ifndef NTBUILD
  10. #include "windows.globalization.numberformatting.h"
  11. #include "windows.globalization.datetimeformatting.h"
  12. #include "Windows.Data.Text.h"
  13. #include "activation.h"
  14. using namespace ABI;
  15. #endif // NTBUILD
  16. #endif // INTL_WINGLOB
  17. class ThreadContext;
  18. namespace Js
  19. {
  20. #ifdef INTL_WINGLOB
  21. class WindowsGlobalizationAdapter
  22. {
  23. private:
  24. bool initializedCommonGlobObjects;
  25. HRESULT hrForCommonGlobObjectsInit;
  26. #ifdef ENABLE_INTL_OBJECT
  27. bool initializedDateTimeFormatObjects;
  28. HRESULT hrForDateTimeFormatObjectsInit;
  29. bool initializedNumberFormatObjects;
  30. HRESULT hrForNumberFormatObjectsInit;
  31. #endif
  32. #if ENABLE_UNICODE_API
  33. bool initializedCharClassifierObjects;
  34. HRESULT hrForCharClassifierObjectsInit;
  35. #endif
  36. #ifdef ENABLE_INTL_OBJECT
  37. // Common
  38. AutoCOMPtr<Windows::Globalization::ILanguageFactory> languageFactory;
  39. AutoCOMPtr<Windows::Globalization::ILanguageStatics> languageStatics;
  40. AutoCOMPtr<Windows::Globalization::DateTimeFormatting::IDateTimeFormatterFactory> dateTimeFormatterFactory;
  41. // DateTimeFormat
  42. AutoCOMPtr<Windows::Globalization::ICalendarFactory> calendarFactory;
  43. AutoCOMPtr<Windows::Globalization::ITimeZoneOnCalendar> timeZoneCalendar; // use to validate timeZone
  44. AutoCOMPtr<Windows::Globalization::ITimeZoneOnCalendar> defaultTimeZoneCalendar; // default current time zone
  45. // NumberFormat
  46. AutoCOMPtr<Windows::Globalization::NumberFormatting::ICurrencyFormatterFactory> currencyFormatterFactory;
  47. AutoCOMPtr<Windows::Globalization::NumberFormatting::IDecimalFormatterFactory> decimalFormatterFactory;
  48. AutoCOMPtr<Windows::Globalization::NumberFormatting::IPercentFormatterFactory> percentFormatterFactory;
  49. AutoCOMPtr<IActivationFactory> incrementNumberRounderActivationFactory;
  50. AutoCOMPtr<IActivationFactory> significantDigitsRounderActivationFactory;
  51. #endif
  52. // CharClassifier
  53. AutoCOMPtr<Windows::Data::Text::IUnicodeCharactersStatics> unicodeStatics;
  54. DelayLoadWindowsGlobalization* GetWindowsGlobalizationLibrary(_In_ ScriptContext* scriptContext);
  55. DelayLoadWindowsGlobalization* GetWindowsGlobalizationLibrary(_In_ ThreadContext* threadContext);
  56. template <typename T>
  57. HRESULT GetActivationFactory(DelayLoadWindowsGlobalization *library, LPCWSTR factoryName, T** instance);
  58. public:
  59. WindowsGlobalizationAdapter()
  60. : initializedCommonGlobObjects(false),
  61. hrForCommonGlobObjectsInit(S_OK),
  62. #ifdef ENABLE_INTL_OBJECT
  63. initializedDateTimeFormatObjects(false),
  64. hrForDateTimeFormatObjectsInit(S_OK),
  65. initializedNumberFormatObjects(false),
  66. hrForNumberFormatObjectsInit(S_OK),
  67. #endif
  68. #if ENABLE_UNICODE_API
  69. initializedCharClassifierObjects(false),
  70. hrForCharClassifierObjectsInit(S_OK),
  71. #endif
  72. #ifdef ENABLE_INTL_OBJECT
  73. languageFactory(nullptr),
  74. languageStatics(nullptr),
  75. dateTimeFormatterFactory(nullptr),
  76. calendarFactory(nullptr),
  77. timeZoneCalendar(nullptr),
  78. defaultTimeZoneCalendar(nullptr),
  79. currencyFormatterFactory(nullptr),
  80. decimalFormatterFactory(nullptr),
  81. percentFormatterFactory(nullptr),
  82. incrementNumberRounderActivationFactory(nullptr),
  83. significantDigitsRounderActivationFactory(nullptr),
  84. #endif // ENABLE_INTL_OBJECT
  85. unicodeStatics(nullptr)
  86. { }
  87. #ifdef ENABLE_INTL_OBJECT
  88. HRESULT EnsureCommonObjectsInitialized(DelayLoadWindowsGlobalization *library);
  89. HRESULT EnsureDateTimeFormatObjectsInitialized(DelayLoadWindowsGlobalization *library);
  90. HRESULT EnsureNumberFormatObjectsInitialized(DelayLoadWindowsGlobalization *library);
  91. #endif
  92. #if ENABLE_UNICODE_API
  93. HRESULT EnsureDataTextObjectsInitialized(DelayLoadWindowsGlobalization *library);
  94. #endif
  95. #ifdef ENABLE_INTL_OBJECT
  96. HRESULT CreateLanguage(_In_ ScriptContext* scriptContext, _In_z_ PCWSTR languageTag, Windows::Globalization::ILanguage** language);
  97. boolean IsWellFormedLanguageTag(_In_ ScriptContext* scriptContext, _In_z_ PCWSTR languageTag);
  98. HRESULT NormalizeLanguageTag(_In_ ScriptContext* scriptContext, _In_z_ PCWSTR languageTag, HSTRING *result);
  99. HRESULT CreateCurrencyFormatterCode(_In_ ScriptContext* scriptContext, _In_z_ PCWSTR currencyCode, Windows::Globalization::NumberFormatting::ICurrencyFormatter** currencyFormatter);
  100. HRESULT CreateCurrencyFormatter(_In_ ScriptContext* scriptContext, PCWSTR* localeStrings, uint32 numLocaleStrings, _In_z_ PCWSTR currencyCode, Windows::Globalization::NumberFormatting::ICurrencyFormatter** currencyFormatter);
  101. HRESULT CreateNumberFormatter(_In_ ScriptContext* scriptContext, PCWSTR* localeStrings, uint32 numLocaleStrings, Windows::Globalization::NumberFormatting::INumberFormatter** numberFormatter);
  102. HRESULT CreatePercentFormatter(_In_ ScriptContext* scriptContext, PCWSTR* localeStrings, uint32 numLocaleStrings, Windows::Globalization::NumberFormatting::INumberFormatter** numberFormatter);
  103. HRESULT CreateDateTimeFormatter(_In_ ScriptContext* scriptContext, _In_z_ PCWSTR formatString, _In_z_ PCWSTR* localeStrings, uint32 numLocaleStrings,
  104. _In_opt_z_ PCWSTR calendar, _In_opt_z_ PCWSTR clock, _Out_ Windows::Globalization::DateTimeFormatting::IDateTimeFormatter** formatter);
  105. HRESULT CreateIncrementNumberRounder(_In_ ScriptContext* scriptContext, Windows::Globalization::NumberFormatting::INumberRounder** numberRounder);
  106. HRESULT CreateSignificantDigitsRounder(_In_ ScriptContext* scriptContext, Windows::Globalization::NumberFormatting::INumberRounder** numberRounder);
  107. boolean ValidateAndCanonicalizeTimeZone(_In_ ScriptContext* scriptContext, _In_z_ PCWSTR timeZoneId, HSTRING* result);
  108. HRESULT GetDefaultTimeZoneId(_In_ ScriptContext* scriptContext, HSTRING* result);
  109. HRESULT GetResolvedLanguage(_In_ Windows::Globalization::DateTimeFormatting::IDateTimeFormatter* formatter, HSTRING * locale);
  110. HRESULT GetResolvedLanguage(_In_ Windows::Globalization::NumberFormatting::INumberFormatterOptions* formatter, HSTRING * locale);
  111. HRESULT GetNumeralSystem(_In_ Windows::Globalization::DateTimeFormatting::IDateTimeFormatter* formatter, HSTRING * hNumeralSystem);
  112. HRESULT GetNumeralSystem(_In_ Windows::Globalization::NumberFormatting::INumberFormatterOptions* formatter, HSTRING * hNumeralSystem);
  113. HRESULT GetCalendar(_In_ Windows::Globalization::DateTimeFormatting::IDateTimeFormatter* formatter, HSTRING * hCalendar);
  114. HRESULT GetClock(_In_ Windows::Globalization::DateTimeFormatting::IDateTimeFormatter* formatter, HSTRING * hClock);
  115. HRESULT GetItemAt(_In_ Windows::Foundation::Collections::IVectorView<HSTRING>* vector, _In_ uint32 index, HSTRING * item);
  116. void ResetCommonFactoryObjects();
  117. void ResetDateTimeFormatFactoryObjects();
  118. void ResetNumberFormatFactoryObjects();
  119. #endif // ENABLE_INTL_OBJECT
  120. #if ENABLE_UNICODE_API
  121. // This is currently used only be the Platform Agnostic Layer
  122. // TODO: Move the management of this interface to the Platform Agnostic Interface Layer
  123. // TODO: Subsume the Windows Globalization Adapter into the Platform Agnostic Interface Layer
  124. Windows::Data::Text::IUnicodeCharactersStatics* GetUnicodeStatics()
  125. {
  126. return unicodeStatics;
  127. }
  128. #endif
  129. #ifdef ENABLE_INTL_OBJECT
  130. private:
  131. HRESULT CreateTimeZoneOnCalendar(_In_ DelayLoadWindowsGlobalization *library, __out Windows::Globalization::ITimeZoneOnCalendar** result);
  132. static HRESULT VerifyResult(HSTRING * result, HRESULT errCode);
  133. #endif
  134. };
  135. #endif // INTL_WINGLOB
  136. }
  137. #endif // defined(ENABLE_INTL_OBJECT) || defined(ENABLE_ES6_CHAR_CLASSIFIER)