| 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 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 -1; | 75 -1; |
| 76 #endif | 76 #endif |
| 77 PlatformFile g_icudtl_pf = kInvalidPlatformFile; | 77 PlatformFile g_icudtl_pf = kInvalidPlatformFile; |
| 78 MemoryMappedFile* g_icudtl_mapped_file = nullptr; | 78 MemoryMappedFile* g_icudtl_mapped_file = nullptr; |
| 79 MemoryMappedFile::Region g_icudtl_region; | 79 MemoryMappedFile::Region g_icudtl_region; |
| 80 | 80 |
| 81 void LazyInitIcuDataFile() { | 81 void LazyInitIcuDataFile() { |
| 82 if (g_icudtl_pf != kInvalidPlatformFile) { | 82 if (g_icudtl_pf != kInvalidPlatformFile) { |
| 83 return; | 83 return; |
| 84 } | 84 } |
| 85 #if defined(OS_ANDROID) | |
| 86 int fd = base::android::OpenApkAsset(kAndroidAssetsIcuDataFileName, | |
| 87 &g_icudtl_region); | |
| 88 g_icudtl_pf = fd; | |
| 89 if (fd != -1) { | |
| 90 return; | |
| 91 } | |
| 92 // For unit tests, data file is located on disk, so try there as a fallback. | |
| 93 #endif // defined(OS_ANDROID) | |
| 94 #if !defined(OS_MACOSX) | |
| 95 FilePath data_path; | |
| 96 #if defined(OS_WIN) | |
| 97 // The data file will be in the same directory as the current module. | |
| 98 bool path_ok = PathService::Get(DIR_MODULE, &data_path); | |
| 99 wchar_t tmp_buffer[_MAX_PATH] = {0}; | |
| 100 wcscpy_s(tmp_buffer, data_path.value().c_str()); | |
| 101 debug::Alias(tmp_buffer); | |
| 102 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 | |
| 103 #elif defined(OS_ANDROID) | |
| 104 bool path_ok = PathService::Get(DIR_ANDROID_APP_DATA, &data_path); | |
| 105 #else | |
| 106 // For now, expect the data file to be alongside the executable. | |
| 107 // This is sufficient while we work on unit tests, but will eventually | |
| 108 // likely live in a data directory. | |
| 109 bool path_ok = PathService::Get(DIR_EXE, &data_path); | |
| 110 #endif | |
| 111 DCHECK(path_ok); | |
| 112 data_path = data_path.AppendASCII(kIcuDataFileName); | |
| 113 | 85 |
| 114 #if defined(OS_WIN) | 86 g_icudtl_pf = OpenIcuDataFile(); |
| 115 // TODO(scottmg): http://crbug.com/445616 | 87 if (g_icudtl_pf != kInvalidPlatformFile) |
| 116 wchar_t tmp_buffer2[_MAX_PATH] = {0}; | |
| 117 wcscpy_s(tmp_buffer2, data_path.value().c_str()); | |
| 118 debug::Alias(tmp_buffer2); | |
| 119 #endif | |
| 120 | |
| 121 #else | |
| 122 // Assume it is in the framework bundle's Resources directory. | |
| 123 ScopedCFTypeRef<CFStringRef> data_file_name( | |
| 124 SysUTF8ToCFStringRef(kIcuDataFileName)); | |
| 125 FilePath data_path = mac::PathForFrameworkBundleResource(data_file_name); | |
| 126 if (data_path.empty()) { | |
| 127 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; | |
| 128 return; | |
| 129 } | |
| 130 #endif // !defined(OS_MACOSX) | |
| 131 File file(data_path, File::FLAG_OPEN | File::FLAG_READ); | |
| 132 if (file.IsValid()) { | |
| 133 g_icudtl_pf = file.TakePlatformFile(); | |
| 134 g_icudtl_region = MemoryMappedFile::Region::kWholeFile; | 88 g_icudtl_region = MemoryMappedFile::Region::kWholeFile; |
| 135 } | |
| 136 } | 89 } |
| 137 | 90 |
| 138 bool InitializeICUWithFileDescriptorInternal( | 91 bool InitializeICUWithFileDescriptorInternal( |
| 139 PlatformFile data_fd, | 92 PlatformFile data_fd, |
| 140 const MemoryMappedFile::Region& data_region) { | 93 const MemoryMappedFile::Region& data_region) { |
| 141 // This can be called multiple times in tests. | 94 // This can be called multiple times in tests. |
| 142 if (g_icudtl_mapped_file) { | 95 if (g_icudtl_mapped_file) { |
| 143 return true; | 96 return true; |
| 144 } | 97 } |
| 145 if (data_fd == kInvalidPlatformFile) { | 98 if (data_fd == kInvalidPlatformFile) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 174 return InitializeICUWithFileDescriptorInternal(data_fd, data_region); | 127 return InitializeICUWithFileDescriptorInternal(data_fd, data_region); |
| 175 } | 128 } |
| 176 | 129 |
| 177 PlatformFile GetIcuDataFileHandle(MemoryMappedFile::Region* out_region) { | 130 PlatformFile GetIcuDataFileHandle(MemoryMappedFile::Region* out_region) { |
| 178 CHECK_NE(g_icudtl_pf, kInvalidPlatformFile); | 131 CHECK_NE(g_icudtl_pf, kInvalidPlatformFile); |
| 179 *out_region = g_icudtl_region; | 132 *out_region = g_icudtl_region; |
| 180 return g_icudtl_pf; | 133 return g_icudtl_pf; |
| 181 } | 134 } |
| 182 #endif // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE | 135 #endif // ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE |
| 183 | 136 |
| 137 PlatformFile OpenIcuDataFile() { |
| 138 #if defined(OS_ANDROID) |
| 139 int fd = base::android::OpenApkAsset(kAndroidAssetsIcuDataFileName, |
| 140 &g_icudtl_region); |
| 141 if (fd != -1) |
| 142 return fd; |
| 143 // For unit tests, data file is located on disk, so try there as a fallback. |
| 144 #endif // defined(OS_ANDROID) |
| 145 #if !defined(OS_MACOSX) |
| 146 FilePath data_path; |
| 147 #if defined(OS_WIN) |
| 148 // The data file will be in the same directory as the current module. |
| 149 bool path_ok = PathService::Get(DIR_MODULE, &data_path); |
| 150 wchar_t tmp_buffer[_MAX_PATH] = {0}; |
| 151 wcscpy_s(tmp_buffer, data_path.value().c_str()); |
| 152 debug::Alias(tmp_buffer); |
| 153 CHECK(path_ok); // TODO(scottmg): http://crbug.com/445616 |
| 154 #elif defined(OS_ANDROID) |
| 155 bool path_ok = PathService::Get(DIR_ANDROID_APP_DATA, &data_path); |
| 156 #else |
| 157 // For now, expect the data file to be alongside the executable. |
| 158 // This is sufficient while we work on unit tests, but will eventually |
| 159 // likely live in a data directory. |
| 160 bool path_ok = PathService::Get(DIR_EXE, &data_path); |
| 161 #endif |
| 162 DCHECK(path_ok); |
| 163 data_path = data_path.AppendASCII(kIcuDataFileName); |
| 164 |
| 165 #if defined(OS_WIN) |
| 166 // TODO(scottmg): http://crbug.com/445616 |
| 167 wchar_t tmp_buffer2[_MAX_PATH] = {0}; |
| 168 wcscpy_s(tmp_buffer2, data_path.value().c_str()); |
| 169 debug::Alias(tmp_buffer2); |
| 170 #endif |
| 171 |
| 172 #else |
| 173 // Assume it is in the framework bundle's Resources directory. |
| 174 ScopedCFTypeRef<CFStringRef> data_file_name( |
| 175 SysUTF8ToCFStringRef(kIcuDataFileName)); |
| 176 FilePath data_path = mac::PathForFrameworkBundleResource(data_file_name); |
| 177 if (data_path.empty()) { |
| 178 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; |
| 179 return kInvalidPlatformFile; |
| 180 } |
| 181 #endif // !defined(OS_MACOSX) |
| 182 File file(data_path, File::FLAG_OPEN | File::FLAG_READ); |
| 183 if (file.IsValid()) |
| 184 return file.TakePlatformFile(); |
| 185 |
| 186 return kInvalidPlatformFile; |
| 187 } |
| 188 |
| 184 bool InitializeICU() { | 189 bool InitializeICU() { |
| 185 #if !defined(NDEBUG) | 190 #if !defined(NDEBUG) |
| 186 DCHECK(!g_check_called_once || !g_called_once); | 191 DCHECK(!g_check_called_once || !g_called_once); |
| 187 g_called_once = true; | 192 g_called_once = true; |
| 188 #endif | 193 #endif |
| 189 | 194 |
| 190 bool result; | 195 bool result; |
| 191 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) | 196 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) |
| 192 // We expect to find the ICU data module alongside the current module. | 197 // We expect to find the ICU data module alongside the current module. |
| 193 FilePath data_path; | 198 FilePath data_path; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 #endif // !defined(OS_NACL) | 245 #endif // !defined(OS_NACL) |
| 241 | 246 |
| 242 void AllowMultipleInitializeCallsForTesting() { | 247 void AllowMultipleInitializeCallsForTesting() { |
| 243 #if !defined(NDEBUG) && !defined(OS_NACL) | 248 #if !defined(NDEBUG) && !defined(OS_NACL) |
| 244 g_check_called_once = false; | 249 g_check_called_once = false; |
| 245 #endif | 250 #endif |
| 246 } | 251 } |
| 247 | 252 |
| 248 } // namespace i18n | 253 } // namespace i18n |
| 249 } // namespace base | 254 } // namespace base |
| OLD | NEW |