Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/locale/OSPreferences.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef mozilla_intl_IntlOSPreferences_h__
7
#define mozilla_intl_IntlOSPreferences_h__
8
9
#include "mozilla/StaticPtr.h"
10
#include "nsString.h"
11
#include "nsTArray.h"
12
#include "unicode/uloc.h"
13
14
#include "mozIOSPreferences.h"
15
16
namespace mozilla {
17
namespace intl {
18
19
/**
20
 * OSPreferences API provides a set of methods for retrieving information from
21
 * the host environment on topics such as:
22
 *   - Internationalization
23
 *   - Localization
24
 *   - Regional preferences
25
 *
26
 * The API is meant to remain as simple as possible, relaying information from
27
 * the host environment to the user without too much logic.
28
 *
29
 * Saying that, there are two exceptions to that paradigm.
30
 *
31
 * First one is normalization. We do intend to translate host environment
32
 * concepts to unified Intl/L10n vocabulary used by Mozilla.
33
 * That means that we will format locale IDs, timezone names, currencies etc.
34
 * into a chosen format.
35
 *
36
 * Second is caching. This API does cache values and where possible will
37
 * hook into the environment for some event-driven cache invalidation.
38
 *
39
 * This means that on platforms that do not support a mechanism to
40
 * notify apps about changes, new OS-level settings may not be reflected
41
 * in the app until it is relaunched.
42
 */
43
class OSPreferences: public mozIOSPreferences
44
{
45
46
public:
47
  NS_DECL_ISUPPORTS
48
  NS_DECL_MOZIOSPREFERENCES
49
50
  enum class DateTimeFormatStyle {
51
    Invalid = -1,
52
    None,
53
    Short,   // e.g. time: HH:mm, date: Y/m/d
54
    Medium,  // likely same as Short
55
    Long,    // e.g. time: including seconds, date: including weekday
56
    Full     // e.g. time: with timezone, date: with long weekday, month
57
  };
58
59
  /**
60
   * Constructor, to do any necessary initialization such as registering for
61
   * notifications from the system when prefs are modified.
62
   */
63
  OSPreferences();
64
65
  /**
66
   * Create (if necessary) and return a raw pointer to the singleton instance.
67
   * Use this accessor in C++ code that just wants to call a method on the
68
   * instance, but does not need to hold a reference, as in
69
   *    nsAutoCString str;
70
   *    OSPreferences::GetInstance()->GetSystemLocale(str);
71
   */
72
  static OSPreferences* GetInstance();
73
74
  /**
75
   * Return an addRef'd pointer to the singleton instance. This is used by the
76
   * XPCOM constructor that exists to support usage from JS.
77
   */
78
  static already_AddRefed<OSPreferences> GetInstanceAddRefed()
79
0
  {
80
0
    return RefPtr<OSPreferences>(GetInstance()).forget();
81
0
  }
82
83
84
  static bool GetDateTimeConnectorPattern(const nsACString& aLocale,
85
                                          nsAString& aRetVal);
86
87
  /**
88
   * Triggers a refresh of retrieving data from host environment.
89
   *
90
   * If the result differs from the previous list, it will additionally
91
   * trigger global events for changed values:
92
   *
93
   *  * SystemLocales: "intl:system-locales-changed"
94
   *
95
   * This method should not be called from anywhere except of per-platform
96
   * hooks into OS events.
97
   */
98
  void Refresh();
99
100
protected:
101
  nsTArray<nsCString> mSystemLocales;
102
  nsTArray<nsCString> mRegionalPrefsLocales;
103
104
private:
105
  virtual ~OSPreferences();
106
107
  static StaticRefPtr<OSPreferences> sInstance;
108
109
  static bool CanonicalizeLanguageTag(nsCString& aLoc);
110
111
  /**
112
   * Helper methods to get formats from ICU; these will return false
113
   * in case of error, in which case the caller cannot rely on aRetVal.
114
   */
115
  bool GetDateTimePatternForStyle(DateTimeFormatStyle aDateStyle,
116
                                  DateTimeFormatStyle aTimeStyle,
117
                                  const nsACString& aLocale,
118
                                  nsAString& aRetVal);
119
120
  bool GetDateTimeSkeletonForStyle(DateTimeFormatStyle aDateStyle,
121
                                   DateTimeFormatStyle aTimeStyle,
122
                                   const nsACString& aLocale,
123
                                   nsAString& aRetVal);
124
125
  bool GetPatternForSkeleton(const nsAString& aSkeleton,
126
                             const nsACString& aLocale,
127
                             nsAString& aRetVal);
128
129
  /**
130
   * This is a host environment specific method that will be implemented
131
   * separately for each platform.
132
   *
133
   * It is only called when the cache is empty or invalidated.
134
   *
135
   * The return value indicates whether the function successfully
136
   * resolved at least one locale.
137
   */
138
  bool ReadSystemLocales(nsTArray<nsCString>& aRetVal);
139
140
  bool ReadRegionalPrefsLocales(nsTArray<nsCString>& aRetVal);
141
142
  /**
143
   * This is a host environment specific method that will be implemented
144
   * separately for each platform.
145
   *
146
   * It is `best-effort` kind of API that attempts to construct the best
147
   * possible date/time pattern for the given styles and locales.
148
   *
149
   * In case we fail to, or don't know how to retrieve the pattern in a
150
   * given environment this function will return false.
151
   * Callers should always be prepared to handle that scenario.
152
   *
153
   * The heuristic may depend on the OS API and HIG guidelines.
154
   */
155
  bool ReadDateTimePattern(DateTimeFormatStyle aDateFormatStyle,
156
                           DateTimeFormatStyle aTimeFormatStyle,
157
                           const nsACString& aLocale,
158
                           nsAString& aRetVal);
159
};
160
161
} // intl
162
} // namespace mozilla
163
164
#endif /* mozilla_intl_IntlOSPreferences_h__ */