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

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: Review comments 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 {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 << ICU_UTIL_DATA_SHARED_MODULE_NAME; 115 << ICU_UTIL_DATA_SHARED_MODULE_NAME;
112 return false; 116 return false;
113 } 117 }
114 118
115 UErrorCode err = U_ZERO_ERROR; 119 UErrorCode err = U_ZERO_ERROR;
116 udata_setCommonData(reinterpret_cast<void*>(addr), &err); 120 udata_setCommonData(reinterpret_cast<void*>(addr), &err);
117 result = (err == U_ZERO_ERROR); 121 result = (err == U_ZERO_ERROR);
118 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) 122 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
119 // The ICU data is statically linked. 123 // The ICU data is statically linked.
120 result = true; 124 result = true;
121 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) 125 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) && defined(OS_ANDROID)
126 base::MemoryMappedFile::Region region;
127 int fd = base::android::OpenApkAsset(kIcuDataFileName, &region);
128 CHECK(fd != -1);
Lei Zhang 2015/06/02 20:31:15 CHECK_GE(fd, 0);
129
130 #if !defined(NDEBUG)
131 g_called_once = false;
Lei Zhang 2015/06/02 20:31:15 Can you explain what's going on here?
Yaron 2015/06/02 21:19:40 In comment or why this is happening? It was buggin
132 #endif
133 result = InitializeICUWithFileDescriptor(fd, region);
134 #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 135 // 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. 136 // 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 137 // Instead, we map the file in and hand off the data so the sandbox won't
125 // cause any problems. 138 // cause any problems.
126 139
127 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever 140 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever
128 // be released. 141 // be released.
129 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); 142 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ());
130 if (!mapped_file.IsValid()) { 143 if (!mapped_file.IsValid()) {
131 #if !defined(OS_MACOSX) 144 #if !defined(OS_MACOSX)
132 FilePath data_path; 145 FilePath data_path;
133 #if defined(OS_WIN) 146 #if defined(OS_WIN)
134 // The data file will be in the same directory as the current module. 147 // The data file will be in the same directory as the current module.
135 bool path_ok = PathService::Get(DIR_MODULE, &data_path); 148 bool path_ok = PathService::Get(DIR_MODULE, &data_path);
136 wchar_t tmp_buffer[_MAX_PATH] = {0}; 149 wchar_t tmp_buffer[_MAX_PATH] = {0};
137 wcscpy_s(tmp_buffer, data_path.value().c_str()); 150 wcscpy_s(tmp_buffer, data_path.value().c_str());
138 debug::Alias(tmp_buffer); 151 debug::Alias(tmp_buffer);
139 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 152 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 153 #else
143 // For now, expect the data file to be alongside the executable. 154 // For now, expect the data file to be alongside the executable.
144 // This is sufficient while we work on unit tests, but will eventually 155 // This is sufficient while we work on unit tests, but will eventually
145 // likely live in a data directory. 156 // likely live in a data directory.
146 bool path_ok = PathService::Get(DIR_EXE, &data_path); 157 bool path_ok = PathService::Get(DIR_EXE, &data_path);
147 #endif 158 #endif
148 DCHECK(path_ok); 159 DCHECK(path_ok);
149 data_path = data_path.AppendASCII(kIcuDataFileName); 160 data_path = data_path.AppendASCII(kIcuDataFileName);
150 161
151 #if defined(OS_WIN) 162 #if defined(OS_WIN)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 #endif 207 #endif
197 208
198 void AllowMultipleInitializeCallsForTesting() { 209 void AllowMultipleInitializeCallsForTesting() {
199 #if !defined(NDEBUG) 210 #if !defined(NDEBUG)
200 g_check_called_once = false; 211 g_check_called_once = false;
201 #endif 212 #endif
202 } 213 }
203 214
204 } // namespace i18n 215 } // namespace i18n
205 } // namespace base 216 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698