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
20class ICUContext {
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(path: icu_data_path.c_str(), create_if_necessary: false, permission: fml::FilePermission::kRead);
37
38 // Check the path relative to the current executable.
39 if (!fd.is_valid()) {
40 auto directory = fml::paths::GetExecutableDirectoryPath();
41
42 if (!directory.first) {
43 return false;
44 }
45
46 std::string path_relative_to_executable =
47 paths::JoinPaths(components: {directory.second, icu_data_path});
48
49 fd = fml::OpenFile(path: path_relative_to_executable.c_str(), create_if_necessary: false,
50 permission: fml::FilePermission::kRead);
51 }
52
53 if (!fd.is_valid()) {
54 return false;
55 }
56
57 std::initializer_list<FileMapping::Protection> protection = {
58 fml::FileMapping::Protection::kRead};
59
60 auto file_mapping = std::make_unique<FileMapping>(args&: fd, args&: 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(data: GetMapping(), err: &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(flag&: g_icu_init_flag,
104 func: [&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(flag&: g_icu_init_flag, func: [mapping = std::move(mapping)]() mutable {
115 InitializeICUFromMappingOnce(mapping: std::move(mapping));
116 });
117}
118
119} // namespace icu
120} // namespace fml
121

source code of flutter_engine/flutter/fml/icu_util.cc