| 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 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/memory_mapped_file.h" | 14 #include "base/files/memory_mapped_file.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
| 19 #include "third_party/icu/source/common/unicode/putil.h" | 19 #include "third_party/icu/source/common/unicode/putil.h" |
| 20 #include "third_party/icu/source/common/unicode/udata.h" | 20 #include "third_party/icu/source/common/unicode/udata.h" |
| 21 | 21 |
| 22 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
| 23 #include "base/mac/foundation_util.h" | 23 #include "base/mac/foundation_util.h" |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #define ICU_UTIL_DATA_FILE 0 | 26 #define ICU_UTIL_DATA_FILE 0 |
| 27 #define ICU_UTIL_DATA_SHARED 1 | 27 #define ICU_UTIL_DATA_SHARED 1 |
| 28 #define ICU_UTIL_DATA_STATIC 2 | 28 #define ICU_UTIL_DATA_STATIC 2 |
| 29 | 29 |
| 30 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE | 30 namespace base { |
| 31 namespace i18n { |
| 32 |
| 31 // Use an unversioned file name to simplify a icu version update down the road. | 33 // Use an unversioned file name to simplify a icu version update down the road. |
| 32 // No need to change the filename in multiple places (gyp files, windows | 34 // No need to change the filename in multiple places (gyp files, windows |
| 33 // build pkg configurations, etc). 'l' stands for Little Endian. | 35 // build pkg configurations, etc). 'l' stands for Little Endian. |
| 34 #define ICU_UTIL_DATA_FILE_NAME "icudtl.dat" | 36 // This variable is exported through the header file. |
| 35 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED | 37 const char kIcuDataFileName[] = "icudtl.dat"; |
| 38 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED |
| 36 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" | 39 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" |
| 37 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
| 38 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" | 41 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" |
| 39 #endif | 42 #endif |
| 40 #endif | 43 #endif |
| 41 | 44 |
| 42 namespace base { | |
| 43 namespace i18n { | |
| 44 | |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 #if !defined(NDEBUG) | 47 #if !defined(NDEBUG) |
| 48 // Assert that we are not called more than once. Even though calling this | 48 // Assert that we are not called more than once. Even though calling this |
| 49 // function isn't harmful (ICU can handle it), being called twice probably | 49 // function isn't harmful (ICU can handle it), being called twice probably |
| 50 // indicates a programming error. | 50 // indicates a programming error. |
| 51 bool g_called_once = false; | 51 bool g_called_once = false; |
| 52 bool g_check_called_once = true; | 52 bool g_check_called_once = true; |
| 53 #endif | 53 #endif |
| 54 } | 54 } |
| 55 | 55 |
| 56 | |
| 57 #if defined(OS_ANDROID) | 56 #if defined(OS_ANDROID) |
| 58 bool InitializeICUWithFileDescriptor(int data_fd) { | 57 bool InitializeICUWithFileDescriptor( |
| 58 int data_fd, |
| 59 base::MemoryMappedFile::Region data_region) { |
| 59 #if !defined(NDEBUG) | 60 #if !defined(NDEBUG) |
| 60 DCHECK(!g_check_called_once || !g_called_once); | 61 DCHECK(!g_check_called_once || !g_called_once); |
| 61 g_called_once = true; | 62 g_called_once = true; |
| 62 #endif | 63 #endif |
| 63 | 64 |
| 64 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) | 65 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) |
| 65 // The ICU data is statically linked. | 66 // The ICU data is statically linked. |
| 66 return true; | 67 return true; |
| 67 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) | 68 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
| 68 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); | 69 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); |
| 69 if (!mapped_file.IsValid()) { | 70 if (!mapped_file.IsValid()) { |
| 70 if (!mapped_file.Initialize(base::File(data_fd))) { | 71 if (!mapped_file.Initialize(base::File(data_fd), data_region)) { |
| 71 LOG(ERROR) << "Couldn't mmap icu data file"; | 72 LOG(ERROR) << "Couldn't mmap icu data file"; |
| 72 return false; | 73 return false; |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 UErrorCode err = U_ZERO_ERROR; | 76 UErrorCode err = U_ZERO_ERROR; |
| 76 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 77 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
| 77 return err == U_ZERO_ERROR; | 78 return err == U_ZERO_ERROR; |
| 78 #endif // ICU_UTIL_DATA_FILE | 79 #endif // ICU_UTIL_DATA_FILE |
| 79 } | 80 } |
| 80 #endif | 81 #endif |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 bool path_ok = PathService::Get(base::DIR_MODULE, &data_path); | 129 bool path_ok = PathService::Get(base::DIR_MODULE, &data_path); |
| 129 #elif defined(OS_ANDROID) | 130 #elif defined(OS_ANDROID) |
| 130 bool path_ok = PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path); | 131 bool path_ok = PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path); |
| 131 #else | 132 #else |
| 132 // For now, expect the data file to be alongside the executable. | 133 // For now, expect the data file to be alongside the executable. |
| 133 // This is sufficient while we work on unit tests, but will eventually | 134 // This is sufficient while we work on unit tests, but will eventually |
| 134 // likely live in a data directory. | 135 // likely live in a data directory. |
| 135 bool path_ok = PathService::Get(base::DIR_EXE, &data_path); | 136 bool path_ok = PathService::Get(base::DIR_EXE, &data_path); |
| 136 #endif | 137 #endif |
| 137 DCHECK(path_ok); | 138 DCHECK(path_ok); |
| 138 data_path = data_path.AppendASCII(ICU_UTIL_DATA_FILE_NAME); | 139 data_path = data_path.AppendASCII(kIcuDataFileName); |
| 139 #else | 140 #else |
| 140 // Assume it is in the framework bundle's Resources directory. | 141 // Assume it is in the framework bundle's Resources directory. |
| 142 base::ScopedCFTypeRef<CFStringRef> data_file_name( |
| 143 SysUTF8ToCFStringRef(kIcuDataFileName)); |
| 141 FilePath data_path = | 144 FilePath data_path = |
| 142 base::mac::PathForFrameworkBundleResource(CFSTR(ICU_UTIL_DATA_FILE_NAME)); | 145 base::mac::PathForFrameworkBundleResource(data_file_name); |
| 143 if (data_path.empty()) { | 146 if (data_path.empty()) { |
| 144 LOG(ERROR) << ICU_UTIL_DATA_FILE_NAME << " not found in bundle"; | 147 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; |
| 145 return false; | 148 return false; |
| 146 } | 149 } |
| 147 #endif // OS check | 150 #endif // OS check |
| 148 if (!mapped_file.Initialize(data_path)) { | 151 if (!mapped_file.Initialize(data_path)) { |
| 149 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); | 152 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); |
| 150 return false; | 153 return false; |
| 151 } | 154 } |
| 152 } | 155 } |
| 153 UErrorCode err = U_ZERO_ERROR; | 156 UErrorCode err = U_ZERO_ERROR; |
| 154 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 157 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
| 155 return err == U_ZERO_ERROR; | 158 return err == U_ZERO_ERROR; |
| 156 #endif | 159 #endif |
| 157 } | 160 } |
| 158 | 161 |
| 159 void AllowMultipleInitializeCallsForTesting() { | 162 void AllowMultipleInitializeCallsForTesting() { |
| 160 #if !defined(NDEBUG) | 163 #if !defined(NDEBUG) |
| 161 g_check_called_once = false; | 164 g_check_called_once = false; |
| 162 #endif | 165 #endif |
| 163 } | 166 } |
| 164 | 167 |
| 165 } // namespace i18n | 168 } // namespace i18n |
| 166 } // namespace base | 169 } // namespace base |
| OLD | NEW |