OLD | NEW |
---|---|
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 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "third_party/icu/source/common/unicode/udata.h" | 22 #include "third_party/icu/source/common/unicode/udata.h" |
23 | 23 |
24 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
25 #include "base/mac/foundation_util.h" | 25 #include "base/mac/foundation_util.h" |
26 #endif | 26 #endif |
27 | 27 |
28 #define ICU_UTIL_DATA_FILE 0 | 28 #define ICU_UTIL_DATA_FILE 0 |
29 #define ICU_UTIL_DATA_SHARED 1 | 29 #define ICU_UTIL_DATA_SHARED 1 |
30 #define ICU_UTIL_DATA_STATIC 2 | 30 #define ICU_UTIL_DATA_STATIC 2 |
31 | 31 |
32 #ifndef ICU_UTIL_DATA_IMPL | |
33 | |
34 #if defined(OS_WIN) | |
35 #define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_SHARED | |
36 #elif defined(OS_IOS) | |
37 #define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_FILE | |
38 #else | |
39 #define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_STATIC | |
40 #endif | |
41 | |
42 #endif // ICU_UTIL_DATA_IMPL | |
43 | |
44 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE | 32 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE |
45 #define ICU_UTIL_DATA_FILE_NAME "icudt" U_ICU_VERSION_SHORT "l.dat" | 33 #define ICU_UTIL_DATA_FILE_NAME "icudt" U_ICU_VERSION_SHORT "l.dat" |
46 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED | 34 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED |
47 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" | 35 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" |
48 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
49 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" | 37 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" |
50 #endif | 38 #endif |
51 #endif | 39 #endif |
52 | 40 |
53 namespace base { | 41 namespace base { |
(...skipping 25 matching lines...) Expand all Loading... | |
79 if (!addr) { | 67 if (!addr) { |
80 DLOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " | 68 DLOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " |
81 << ICU_UTIL_DATA_SHARED_MODULE_NAME; | 69 << ICU_UTIL_DATA_SHARED_MODULE_NAME; |
82 return false; | 70 return false; |
83 } | 71 } |
84 | 72 |
85 UErrorCode err = U_ZERO_ERROR; | 73 UErrorCode err = U_ZERO_ERROR; |
86 udata_setCommonData(reinterpret_cast<void*>(addr), &err); | 74 udata_setCommonData(reinterpret_cast<void*>(addr), &err); |
87 return err == U_ZERO_ERROR; | 75 return err == U_ZERO_ERROR; |
88 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 76 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
89 // Mac/Linux bundle the ICU data in. | 77 // The ICU data is statically linked. |
90 return true; | 78 return true; |
91 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 79 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
92 #if !defined(OS_MACOSX) | |
93 // For now, expect the data file to be alongside the executable. | |
94 // This is sufficient while we work on unit tests, but will eventually | |
95 // likely live in a data directory. | |
96 FilePath data_path; | |
97 bool path_ok = PathService::Get(base::DIR_EXE, &data_path); | |
98 DCHECK(path_ok); | |
99 u_setDataDirectory(data_path.value().c_str()); | |
100 // Only look for the packaged data file; | |
101 // the default behavior is to look for individual files. | |
102 UErrorCode err = U_ZERO_ERROR; | |
103 udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); | |
104 return err == U_ZERO_ERROR; | |
105 #else | |
106 // If the ICU data directory is set, ICU won't actually load the data until | 80 // If the ICU data directory is set, ICU won't actually load the data until |
107 // it is needed. This can fail if the process is sandboxed at that time. | 81 // it is needed. This can fail if the process is sandboxed at that time. |
108 // Instead, Mac maps the file in and hands off the data so the sandbox won't | 82 // Instead, we map the file in and hand off the data so the sandbox won't |
109 // cause any problems. | 83 // cause any problems. |
110 | 84 |
111 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever | 85 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever |
112 // be released. | 86 // be released. |
113 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); | 87 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); |
114 if (!mapped_file.IsValid()) { | 88 if (!mapped_file.IsValid()) { |
115 // Assume it is in the framework bundle's Resources directory. | 89 // Assume it is in the framework bundle's Resources directory. |
Jiang Jiang
2013/12/04 10:43:35
This comment should be moved down to close to that
jungshik at Google
2013/12/09 05:54:46
Good catch. thanks.
| |
90 #if !defined(OS_MACOSX) | |
91 // For now, expect the data file to be alongside the executable. | |
92 // This is sufficient while we work on unit tests, but will eventually | |
93 // likely live in a data directory. | |
94 FilePath data_path; | |
95 bool path_ok = PathService::Get(base::DIR_EXE, &data_path); | |
96 DCHECK(path_ok); | |
97 data_path = data_path.AppendASCII(ICU_UTIL_DATA_FILE_NAME); | |
98 #else | |
116 FilePath data_path = | 99 FilePath data_path = |
117 base::mac::PathForFrameworkBundleResource(CFSTR(ICU_UTIL_DATA_FILE_NAME)); | 100 base::mac::PathForFrameworkBundleResource(CFSTR(ICU_UTIL_DATA_FILE_NAME)); |
Jiang Jiang
2013/12/04 10:45:42
Wrong indent by the way.
I also wonder how it can
jungshik at Google
2013/12/09 05:54:46
Thank you for the comment.
I don't know the answe
| |
118 if (data_path.empty()) { | 101 if (data_path.empty()) { |
119 DLOG(ERROR) << ICU_UTIL_DATA_FILE_NAME << " not found in bundle"; | 102 DLOG(ERROR) << ICU_UTIL_DATA_FILE_NAME << " not found in bundle"; |
120 return false; | 103 return false; |
121 } | 104 } |
105 #endif // OS check | |
122 if (!mapped_file.Initialize(data_path)) { | 106 if (!mapped_file.Initialize(data_path)) { |
123 DLOG(ERROR) << "Couldn't mmap " << data_path.value(); | 107 DLOG(ERROR) << "Couldn't mmap " << data_path.value(); |
124 return false; | 108 return false; |
125 } | 109 } |
126 } | 110 } |
127 UErrorCode err = U_ZERO_ERROR; | 111 UErrorCode err = U_ZERO_ERROR; |
128 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 112 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
129 return err == U_ZERO_ERROR; | 113 return err == U_ZERO_ERROR; |
130 #endif // OS check | |
131 #endif | 114 #endif |
132 } | 115 } |
133 | 116 |
134 } // namespace i18n | 117 } // namespace i18n |
135 } // namespace base | 118 } // namespace base |
OLD | NEW |