Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2064)

Side by Side Diff: base/i18n/icu_util.cc

Issue 1147213004: Store and load icudtl.dat directly from the apk rather than extracting on start-up (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@load-from-apk
Patch Set: formatting nit Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/i18n/icu_util.h" 5 #include "base/i18n/icu_util.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #endif 9 #endif
10 10
11 #include <string> 11 #include <string>
12 12
13 #include "base/debug/alias.h" 13 #include "base/debug/alias.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/memory_mapped_file.h" 15 #include "base/files/memory_mapped_file.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/strings/sys_string_conversions.h" 19 #include "base/strings/sys_string_conversions.h"
20 #include "third_party/icu/source/common/unicode/putil.h" 20 #include "third_party/icu/source/common/unicode/putil.h"
21 #include "third_party/icu/source/common/unicode/udata.h" 21 #include "third_party/icu/source/common/unicode/udata.h"
22 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 22 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
23 #include "third_party/icu/source/i18n/unicode/timezone.h" 23 #include "third_party/icu/source/i18n/unicode/timezone.h"
24 #endif 24 #endif
25 25
26 #if defined(OS_ANDROID)
27 #include "base/android/apk_assets.h"
28 #endif
29
26 #if defined(OS_MACOSX) 30 #if defined(OS_MACOSX)
27 #include "base/mac/foundation_util.h" 31 #include "base/mac/foundation_util.h"
28 #endif 32 #endif
29 33
30 #define ICU_UTIL_DATA_FILE 0 34 #define ICU_UTIL_DATA_FILE 0
31 #define ICU_UTIL_DATA_SHARED 1 35 #define ICU_UTIL_DATA_SHARED 1
32 #define ICU_UTIL_DATA_STATIC 2 36 #define ICU_UTIL_DATA_STATIC 2
33 37
34 namespace base { 38 namespace base {
35 namespace i18n { 39 namespace i18n {
36 40
37 // Use an unversioned file name to simplify a icu version update down the road. 41 // Use an unversioned file name to simplify a icu version update down the road.
38 // No need to change the filename in multiple places (gyp files, windows 42 // No need to change the filename in multiple places (gyp files, windows
39 // build pkg configurations, etc). 'l' stands for Little Endian. 43 // build pkg configurations, etc). 'l' stands for Little Endian.
40 // This variable is exported through the header file. 44 // This variable is exported through the header file.
45 #if defined(OS_ANDROID)
46 const char kIcuDataFileName[] = "assets/icudtl.dat";
47 #else
41 const char kIcuDataFileName[] = "icudtl.dat"; 48 const char kIcuDataFileName[] = "icudtl.dat";
49 #endif // defined(OS_ANDROID)
42 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED 50 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
43 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" 51 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
44 #if defined(OS_WIN) 52 #if defined(OS_WIN)
45 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" 53 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
46 #endif 54 #endif
47 #endif 55 #endif
48 56
49 namespace { 57 namespace {
Lei Zhang 2015/06/05 02:52:43 nit: add a blank line after, since you end the nam
agrieve 2015/06/05 14:52:14 Done.
agrieve 2015/06/05 14:52:14 Done.
50 58 #if !defined(OS_NACL)
51 #if !defined(NDEBUG) 59 #if !defined(NDEBUG)
52 // Assert that we are not called more than once. Even though calling this 60 // Assert that we are not called more than once. Even though calling this
53 // function isn't harmful (ICU can handle it), being called twice probably 61 // function isn't harmful (ICU can handle it), being called twice probably
54 // indicates a programming error. 62 // indicates a programming error.
55 #if !defined(OS_NACL) 63 bool g_check_called_once = true;
56 bool g_called_once = false; 64 bool g_called_once = false;
57 #endif 65 #endif // !defined(NDEBUG)
58 bool g_check_called_once = true;
59 #endif
60 }
61 66
62 #if !defined(OS_NACL) 67
63 bool InitializeICUWithFileDescriptor( 68 bool InitializeICUWithFileDescriptorInternal(
64 PlatformFile data_fd, 69 PlatformFile data_fd,
65 MemoryMappedFile::Region data_region) { 70 MemoryMappedFile::Region data_region) {
66 #if !defined(NDEBUG)
67 DCHECK(!g_check_called_once || !g_called_once);
68 g_called_once = true;
69 #endif
70
71 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) 71 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
72 // The ICU data is statically linked. 72 // The ICU data is statically linked.
73 return true; 73 return true;
74 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) 74 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
75 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); 75 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ());
76 if (!mapped_file.IsValid()) { 76 if (!mapped_file.IsValid()) {
77 if (!mapped_file.Initialize(File(data_fd), data_region)) { 77 if (!mapped_file.Initialize(File(data_fd), data_region)) {
78 LOG(ERROR) << "Couldn't mmap icu data file"; 78 LOG(ERROR) << "Couldn't mmap icu data file";
79 return false; 79 return false;
80 } 80 }
81 } 81 }
82 UErrorCode err = U_ZERO_ERROR; 82 UErrorCode err = U_ZERO_ERROR;
83 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); 83 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err);
84 return err == U_ZERO_ERROR; 84 return err == U_ZERO_ERROR;
85 #endif // ICU_UTIL_DATA_FILE 85 #endif // ICU_UTIL_DATA_FILE
86 } 86 }
87 #endif // !defined(OS_NACL)
87 88
89 } // namespace
90
91 #if !defined(OS_NACL)
92 bool InitializeICUWithFileDescriptor(
93 PlatformFile data_fd,
94 MemoryMappedFile::Region data_region) {
Lei Zhang 2015/06/05 02:52:43 BTW, why are regions passed by value in this file?
agrieve 2015/06/05 14:52:14 My thinking was just that they are small, so it di
95 #if !defined(NDEBUG)
96 DCHECK(!g_check_called_once || !g_called_once);
97 g_called_once = true;
98 #endif
99 return InitializeICUWithFileDescriptorInternal(data_fd, data_region);
100 }
88 101
89 bool InitializeICU() { 102 bool InitializeICU() {
90 #if !defined(NDEBUG) 103 #if !defined(NDEBUG)
91 DCHECK(!g_check_called_once || !g_called_once); 104 DCHECK(!g_check_called_once || !g_called_once);
92 g_called_once = true; 105 g_called_once = true;
93 #endif 106 #endif
94 107
95 bool result; 108 bool result;
96 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) 109 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
97 // We expect to find the ICU data module alongside the current module. 110 // We expect to find the ICU data module alongside the current module.
(...skipping 13 matching lines...) Expand all
111 << ICU_UTIL_DATA_SHARED_MODULE_NAME; 124 << ICU_UTIL_DATA_SHARED_MODULE_NAME;
112 return false; 125 return false;
113 } 126 }
114 127
115 UErrorCode err = U_ZERO_ERROR; 128 UErrorCode err = U_ZERO_ERROR;
116 udata_setCommonData(reinterpret_cast<void*>(addr), &err); 129 udata_setCommonData(reinterpret_cast<void*>(addr), &err);
117 result = (err == U_ZERO_ERROR); 130 result = (err == U_ZERO_ERROR);
118 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) 131 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
119 // The ICU data is statically linked. 132 // The ICU data is statically linked.
120 result = true; 133 result = true;
121 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) 134 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) && defined(OS_ANDROID)
135 base::MemoryMappedFile::Region region;
136 int fd = base::android::OpenApkAsset(kIcuDataFileName, &region);
137 CHECK_GE(fd, 0);
138 result = InitializeICUWithFileDescriptorInternal(fd, region);
139 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) && !defined(OS_ANDROID)
122 // If the ICU data directory is set, ICU won't actually load the data until 140 // If the ICU data directory is set, ICU won't actually load the data until
123 // it is needed. This can fail if the process is sandboxed at that time. 141 // it is needed. This can fail if the process is sandboxed at that time.
124 // Instead, we map the file in and hand off the data so the sandbox won't 142 // Instead, we map the file in and hand off the data so the sandbox won't
125 // cause any problems. 143 // cause any problems.
126 144
127 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever 145 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever
128 // be released. 146 // be released.
129 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); 147 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ());
130 if (!mapped_file.IsValid()) { 148 if (!mapped_file.IsValid()) {
131 #if !defined(OS_MACOSX) 149 #if !defined(OS_MACOSX)
132 FilePath data_path; 150 FilePath data_path;
133 #if defined(OS_WIN) 151 #if defined(OS_WIN)
134 // The data file will be in the same directory as the current module. 152 // The data file will be in the same directory as the current module.
135 bool path_ok = PathService::Get(DIR_MODULE, &data_path); 153 bool path_ok = PathService::Get(DIR_MODULE, &data_path);
136 wchar_t tmp_buffer[_MAX_PATH] = {0}; 154 wchar_t tmp_buffer[_MAX_PATH] = {0};
137 wcscpy_s(tmp_buffer, data_path.value().c_str()); 155 wcscpy_s(tmp_buffer, data_path.value().c_str());
138 debug::Alias(tmp_buffer); 156 debug::Alias(tmp_buffer);
139 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 157 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616
140 #elif defined(OS_ANDROID)
141 bool path_ok = PathService::Get(DIR_ANDROID_APP_DATA, &data_path);
142 #else 158 #else
143 // For now, expect the data file to be alongside the executable. 159 // For now, expect the data file to be alongside the executable.
144 // This is sufficient while we work on unit tests, but will eventually 160 // This is sufficient while we work on unit tests, but will eventually
145 // likely live in a data directory. 161 // likely live in a data directory.
146 bool path_ok = PathService::Get(DIR_EXE, &data_path); 162 bool path_ok = PathService::Get(DIR_EXE, &data_path);
147 #endif 163 #endif
148 DCHECK(path_ok); 164 DCHECK(path_ok);
149 data_path = data_path.AppendASCII(kIcuDataFileName); 165 data_path = data_path.AppendASCII(kIcuDataFileName);
150 166
151 #if defined(OS_WIN) 167 #if defined(OS_WIN)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 // cache in ICU has to be populated on starting up. 202 // cache in ICU has to be populated on starting up.
187 // TODO(jungshik): Some callers do not care about tz at all. If necessary, 203 // TODO(jungshik): Some callers do not care about tz at all. If necessary,
188 // add a boolean argument to this function to init'd the default tz only 204 // add a boolean argument to this function to init'd the default tz only
189 // when requested. 205 // when requested.
190 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 206 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
191 if (result) 207 if (result)
192 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); 208 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
193 #endif 209 #endif
194 return result; 210 return result;
195 } 211 }
196 #endif 212 #endif // !defined(OS_NACL)
197 213
198 void AllowMultipleInitializeCallsForTesting() { 214 void AllowMultipleInitializeCallsForTesting() {
199 #if !defined(NDEBUG) 215 #if !defined(NDEBUG) && !defined(OS_NACL)
200 g_check_called_once = false; 216 g_check_called_once = false;
201 #endif 217 #endif
202 } 218 }
203 219
204 } // namespace i18n 220 } // namespace i18n
205 } // namespace base 221 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698