xref: /aosp_15_r20/external/cronet/base/fuchsia/intl_profile_watcher.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium 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 #ifndef BASE_FUCHSIA_INTL_PROFILE_WATCHER_H_
6 #define BASE_FUCHSIA_INTL_PROFILE_WATCHER_H_
7 
8 #include <fuchsia/intl/cpp/fidl.h>
9 #include <string>
10 
11 #include "base/base_export.h"
12 #include "base/functional/callback.h"
13 #include "base/strings/string_piece.h"
14 
15 namespace base {
16 
17 // Watches fuchsia.intl.PropertyProvider for change notifications and notifies
18 // the provided callback. If necessary, the caller is responsible for
19 // determining whether an actual change of interest has occurred.
20 class BASE_EXPORT FuchsiaIntlProfileWatcher {
21  public:
22   using ProfileChangeCallback =
23       base::RepeatingCallback<void(const ::fuchsia::intl::Profile&)>;
24 
25   // |on_profile_changed| will be called each time the profile may have changed.
26   explicit FuchsiaIntlProfileWatcher(ProfileChangeCallback on_profile_changed);
27 
28   FuchsiaIntlProfileWatcher(const FuchsiaIntlProfileWatcher&) = delete;
29   FuchsiaIntlProfileWatcher& operator=(const FuchsiaIntlProfileWatcher&) =
30       delete;
31   ~FuchsiaIntlProfileWatcher();
32 
33   // Returns the ID of the primary time zone in |profile|.
34   // Returns an empty string if the ID cannot be obtained.
35   static std::string GetPrimaryTimeZoneIdFromProfile(
36       const ::fuchsia::intl::Profile& profile);
37 
38   // Returns the ID of the primary time zone for the system.
39   // Returns the empty string if the ID cannot be obtained.
40   // This is a synchronous blocking call to the system service and should only
41   // be used for ICU initialization.
42   static std::string GetPrimaryTimeZoneIdForIcuInitialization();
43 
44   // Returns the ID of the primary locale preference in |profile|.
45   // Returns an empty string if the ID cannot be obtained.
46   static std::string GetPrimaryLocaleIdFromProfile(
47       const ::fuchsia::intl::Profile& profile);
48 
49   // Returns the ID of the primary locale preference for the system.
50   // Returns an empty string if the ID cannot be obtained.
51   // This is a synchronous blocking call to the system service, and should only
52   // be used for first value initialization.
53   static std::string GetPrimaryLocaleIdForInitialization();
54 
55  private:
56   friend class GetValuesFromIntlPropertyProviderTest;
57   friend class IntlProfileWatcherTest;
58 
59   FuchsiaIntlProfileWatcher(
60       ::fuchsia::intl::PropertyProviderPtr property_provider,
61       ProfileChangeCallback on_profile_changed);
62 
63   static ::fuchsia::intl::Profile GetProfileFromPropertyProvider(
64       ::fuchsia::intl::PropertyProviderSyncPtr property_provider);
65 
66   static ::fuchsia::intl::Profile GetCurrentProfileSync();
67 
68   ::fuchsia::intl::PropertyProviderPtr property_provider_;
69   const ProfileChangeCallback on_profile_changed_;
70 };
71 
72 }  // namespace base
73 
74 #endif  // BASE_FUCHSIA_INTL_PROFILE_WATCHER_H_
75