xref: /aosp_15_r20/external/pdfium/xfa/fxfa/parser/cxfa_localemgr.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef XFA_FXFA_PARSER_CXFA_LOCALEMGR_H_
8 #define XFA_FXFA_PARSER_CXFA_LOCALEMGR_H_
9 
10 #include <vector>
11 
12 #include "core/fxcrt/unowned_ptr.h"
13 #include "core/fxcrt/widestring.h"
14 #include "fxjs/gc/heap.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16 #include "v8/include/cppgc/garbage-collected.h"
17 #include "v8/include/cppgc/member.h"
18 #include "xfa/fgas/crt/locale_mgr_iface.h"
19 #include "xfa/fxfa/parser/gced_locale_iface.h"
20 
21 class CXFA_Node;
22 class CXFA_NodeLocale;
23 class CXFA_XMLLocale;
24 
25 class CXFA_LocaleMgr final : public cppgc::GarbageCollected<CXFA_LocaleMgr>,
26                              public LocaleMgrIface {
27  public:
28   enum class LangID : uint16_t {
29     k_zh_HK = 0x0c04,
30     k_zh_CN = 0x0804,
31     k_zh_TW = 0x0404,
32     k_nl_NL = 0x0413,
33     k_en_GB = 0x0809,
34     k_en_US = 0x0409,
35     k_fr_FR = 0x040c,
36     k_de_DE = 0x0407,
37     k_it_IT = 0x0410,
38     k_ja_JP = 0x0411,
39     k_ko_KR = 0x0412,
40     k_pt_BR = 0x0416,
41     k_ru_RU = 0x0419,
42     k_es_LA = 0x080a,
43     k_es_ES = 0x0c0a,
44   };
45 
46   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
47   ~CXFA_LocaleMgr() override;
48 
49   void Trace(cppgc::Visitor* visitor) const;
50 
51   GCedLocaleIface* GetDefLocale() override;
52   GCedLocaleIface* GetLocaleByName(const WideString& wsLocaleName) override;
53 
54   void SetDefLocale(GCedLocaleIface* pLocale);
55   absl::optional<WideString> GetConfigLocaleName(CXFA_Node* pConfig) const;
56 
57  private:
58   CXFA_LocaleMgr(cppgc::Heap* pHeap,
59                  CXFA_Node* pLocaleSet,
60                  WideString wsDeflcid);
61 
62   // May allocate a new object on the cppgc heap.
63   CXFA_XMLLocale* GetLocale(LangID lcid);
64 
65   UnownedPtr<cppgc::Heap> m_pHeap;
66   std::vector<cppgc::Member<CXFA_NodeLocale>> m_LocaleArray;
67   std::vector<cppgc::Member<CXFA_XMLLocale>> m_XMLLocaleArray;
68   cppgc::Member<GCedLocaleIface> m_pDefLocale;
69 
70   // Note: three possiblities
71   // 1. we might never have tried to determine |m_wsConfigLocale|.
72   // 2. we might have tried but gotten nothing and want to continue
73   //    to return nothing without ever trying again.
74   // 3. we might have tried and gotten something.
75   // So |m_bConfigLocaleCached| indicates whether we've already tried,
76   // and |m_wsConfigLocale| is the possibly nothing we got if we tried.
77   mutable absl::optional<WideString> m_wsConfigLocale;
78   mutable bool m_bConfigLocaleCached = false;
79 
80   LangID m_eDeflcid;
81 };
82 
83 #endif  // XFA_FXFA_PARSER_CXFA_LOCALEMGR_H_
84