Flutter Engine
The Flutter Engine
icu_util.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/icu_util.h"
6
7#include <memory>
8#include <mutex>
9
10#include "flutter/fml/build_config.h"
11#include "flutter/fml/logging.h"
12#include "flutter/fml/mapping.h"
13#include "flutter/fml/native_library.h"
14#include "flutter/fml/paths.h"
15#include "third_party/icu/source/common/unicode/udata.h"
16
17namespace fml {
18namespace icu {
19
21 public:
22 explicit ICUContext(const std::string& icu_data_path) : valid_(false) {
23 valid_ = SetupMapping(icu_data_path) && SetupICU();
24 }
25
26 explicit ICUContext(std::unique_ptr<Mapping> mapping)
27 : mapping_(std::move(mapping)) {
28 valid_ = SetupICU();
29 }
30
31 ~ICUContext() = default;
32
33 bool SetupMapping(const std::string& icu_data_path) {
34 // Check if the path exists and it readable directly.
35 auto fd =
36 fml::OpenFile(icu_data_path.c_str(), false, fml::FilePermission::kRead);
37
38 // Check the path relative to the current executable.
39 if (!fd.is_valid()) {
41
42 if (!directory.first) {
43 return false;
44 }
45
46 std::string path_relative_to_executable =
47 paths::JoinPaths({directory.second, icu_data_path});
48
49 fd = fml::OpenFile(path_relative_to_executable.c_str(), false,
51 }
52
53 if (!fd.is_valid()) {
54 return false;
55 }
56
57 std::initializer_list<FileMapping::Protection> protection = {
59
60 auto file_mapping = std::make_unique<FileMapping>(fd, protection);
61
62 if (file_mapping->GetSize() != 0) {
63 mapping_ = std::move(file_mapping);
64 return true;
65 }
66
67 return false;
68 }
69
70 bool SetupICU() {
71 if (GetSize() == 0) {
72 return false;
73 }
74
75 UErrorCode err_code = U_ZERO_ERROR;
76 udata_setCommonData(GetMapping(), &err_code);
77 return (err_code == U_ZERO_ERROR);
78 }
79
80 const uint8_t* GetMapping() const {
81 return mapping_ ? mapping_->GetMapping() : nullptr;
82 }
83
84 size_t GetSize() const { return mapping_ ? mapping_->GetSize() : 0; }
85
86 bool IsValid() const { return valid_; }
87
88 private:
89 bool valid_;
90 std::unique_ptr<Mapping> mapping_;
91
92 FML_DISALLOW_COPY_AND_ASSIGN(ICUContext);
93};
94
95void InitializeICUOnce(const std::string& icu_data_path) {
96 static ICUContext* context = new ICUContext(icu_data_path);
97 FML_CHECK(context->IsValid())
98 << "Must be able to initialize the ICU context. Tried: " << icu_data_path;
99}
100
101std::once_flag g_icu_init_flag;
102void InitializeICU(const std::string& icu_data_path) {
103 std::call_once(g_icu_init_flag,
104 [&icu_data_path]() { InitializeICUOnce(icu_data_path); });
105}
106
107void InitializeICUFromMappingOnce(std::unique_ptr<Mapping> mapping) {
108 static ICUContext* context = new ICUContext(std::move(mapping));
109 FML_CHECK(context->IsValid())
110 << "Unable to initialize the ICU context from a mapping.";
111}
112
113void InitializeICUFromMapping(std::unique_ptr<Mapping> mapping) {
114 std::call_once(g_icu_init_flag, [mapping = std::move(mapping)]() mutable {
115 InitializeICUFromMappingOnce(std::move(mapping));
116 });
117}
118
119} // namespace icu
120} // namespace fml
ICUContext(std::unique_ptr< Mapping > mapping)
Definition: icu_util.cc:26
bool SetupMapping(const std::string &icu_data_path)
Definition: icu_util.cc:33
bool IsValid() const
Definition: icu_util.cc:86
ICUContext(const std::string &icu_data_path)
Definition: icu_util.cc:22
const uint8_t * GetMapping() const
Definition: icu_util.cc:80
size_t GetSize() const
Definition: icu_util.cc:84
#define FML_CHECK(condition)
Definition: logging.h:85
void InitializeICU(const std::string &icu_data_path)
Definition: icu_util.cc:102
void InitializeICUFromMapping(std::unique_ptr< Mapping > mapping)
Definition: icu_util.cc:113
void InitializeICUFromMappingOnce(std::unique_ptr< Mapping > mapping)
Definition: icu_util.cc:107
void InitializeICUOnce(const std::string &icu_data_path)
Definition: icu_util.cc:95
std::once_flag g_icu_init_flag
Definition: icu_util.cc:101
std::string JoinPaths(std::initializer_list< std::string > components)
Definition: paths.cc:14
std::pair< bool, std::string > GetExecutableDirectoryPath()
Definition: paths.cc:55
Definition: ascii_trie.cc:9
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:66
Definition: ref_ptr.h:256