File: intl_profile_watcher.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (115 lines) | stat: -rw-r--r-- 3,522 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/fuchsia/intl_profile_watcher.h"

#include <fuchsia/intl/cpp/fidl.h>
#include <lib/sys/cpp/component_context.h>

#include <string>
#include <vector>

#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"

using ::fuchsia::intl::Profile;

namespace base {

FuchsiaIntlProfileWatcher::FuchsiaIntlProfileWatcher(
    ProfileChangeCallback on_profile_changed)
    : FuchsiaIntlProfileWatcher(
          ComponentContextForProcess()
              ->svc()
              ->Connect<::fuchsia::intl::PropertyProvider>(),
          on_profile_changed) {}

FuchsiaIntlProfileWatcher::FuchsiaIntlProfileWatcher(
    ::fuchsia::intl::PropertyProviderPtr property_provider,
    ProfileChangeCallback on_profile_changed)
    : property_provider_(std::move(property_provider)),
      on_profile_changed_(std::move(on_profile_changed)) {
  DCHECK(property_provider_.is_bound());
  DCHECK(on_profile_changed_);

  property_provider_.set_error_handler([](zx_status_t status) {
    ZX_LOG(ERROR, status) << "intl.PropertyProvider disconnected. "
                          << "Profile changes will not be monitored.";
  });

  property_provider_.events().OnChange = [this] {
    property_provider_->GetProfile(
        [this](Profile profile) { on_profile_changed_.Run(profile); });
  };
}

FuchsiaIntlProfileWatcher::~FuchsiaIntlProfileWatcher() = default;

// static
std::string FuchsiaIntlProfileWatcher::GetPrimaryTimeZoneIdFromProfile(
    const Profile& profile) {
  if (!profile.has_time_zones()) {
    DLOG(WARNING) << "Profile does not contain time zones.";
    return std::string();
  }

  const std::vector<::fuchsia::intl::TimeZoneId>& time_zones =
      profile.time_zones();
  if (time_zones.empty()) {
    DLOG(ERROR) << "Profile contains an empty time zones list.";
    return std::string();
  }

  return time_zones[0].id;
}

// static
std::string
FuchsiaIntlProfileWatcher::GetPrimaryTimeZoneIdForIcuInitialization() {
  return GetPrimaryTimeZoneIdFromProfile(GetCurrentProfileSync());
}

// static
std::string FuchsiaIntlProfileWatcher::GetPrimaryLocaleIdFromProfile(
    const ::fuchsia::intl::Profile& profile) {
  if (!profile.has_locales()) {
    DLOG(ERROR) << "Profile does not contain locale information.";
    return std::string();
  }

  const std::vector<::fuchsia::intl::LocaleId>& locale_preferences =
      profile.locales();
  if (locale_preferences.empty()) {
    DLOG(ERROR) << "Profile contains an empty locale list.";
    return std::string();
  }

  return locale_preferences[0].id;
}

// static
std::string FuchsiaIntlProfileWatcher::GetPrimaryLocaleIdForInitialization() {
  return GetPrimaryLocaleIdFromProfile(GetCurrentProfileSync());
}

// static
Profile FuchsiaIntlProfileWatcher::GetProfileFromPropertyProvider(
    ::fuchsia::intl::PropertyProviderSyncPtr property_provider) {
  DCHECK(property_provider.is_bound());
  Profile profile;
  zx_status_t status = property_provider->GetProfile(&profile);
  if (status != ZX_OK) {
    ZX_DLOG(ERROR, status) << "Failed to get intl Profile";
  }
  return profile;
}

// static
::fuchsia::intl::Profile FuchsiaIntlProfileWatcher::GetCurrentProfileSync() {
  ::fuchsia::intl::PropertyProviderSyncPtr provider;
  ComponentContextForProcess()->svc()->Connect(provider.NewRequest());
  return GetProfileFromPropertyProvider(std::move(provider));
}

}  // namespace base