| 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) | |
| 8 #include <windows.h> | |
| 9 #endif | |
| 10 | |
| 11 #include <string> | 7 #include <string> |
| 12 | 8 |
| 13 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
| 14 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 15 #include "base/files/memory_mapped_file.h" | 11 #include "base/files/memory_mapped_file.h" |
| 16 #include "base/logging.h" | 12 #include "base/logging.h" |
| 17 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 18 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 19 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 20 #include "third_party/icu/source/common/unicode/putil.h" | 16 #include "third_party/icu/source/common/unicode/putil.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 34 namespace base { | 30 namespace base { |
| 35 namespace i18n { | 31 namespace i18n { |
| 36 | 32 |
| 37 // 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. |
| 38 // 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 |
| 39 // build pkg configurations, etc). 'l' stands for Little Endian. | 35 // build pkg configurations, etc). 'l' stands for Little Endian. |
| 40 // This variable is exported through the header file. | 36 // This variable is exported through the header file. |
| 41 const char kIcuDataFileName[] = "icudtl.dat"; | 37 const char kIcuDataFileName[] = "icudtl.dat"; |
| 42 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED | 38 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED |
| 43 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" | 39 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" |
| 44 #if defined(OS_WIN) | |
| 45 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" | |
| 46 #endif | |
| 47 #endif | 40 #endif |
| 48 | 41 |
| 49 namespace { | 42 namespace { |
| 50 | 43 |
| 51 #if !defined(NDEBUG) | 44 #if !defined(NDEBUG) |
| 52 // Assert that we are not called more than once. Even though calling this | 45 // 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 | 46 // function isn't harmful (ICU can handle it), being called twice probably |
| 54 // indicates a programming error. | 47 // indicates a programming error. |
| 55 #if !defined(OS_NACL) | 48 #if !defined(OS_NACL) |
| 56 bool g_called_once = false; | 49 bool g_called_once = false; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // it is needed. This can fail if the process is sandboxed at that time. | 116 // 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 | 117 // Instead, we map the file in and hand off the data so the sandbox won't |
| 125 // cause any problems. | 118 // cause any problems. |
| 126 | 119 |
| 127 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever | 120 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever |
| 128 // be released. | 121 // be released. |
| 129 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); | 122 CR_DEFINE_STATIC_LOCAL(MemoryMappedFile, mapped_file, ()); |
| 130 if (!mapped_file.IsValid()) { | 123 if (!mapped_file.IsValid()) { |
| 131 #if !defined(OS_MACOSX) | 124 #if !defined(OS_MACOSX) |
| 132 FilePath data_path; | 125 FilePath data_path; |
| 133 #if defined(OS_WIN) | 126 #if defined(OS_ANDROID) |
| 134 // The data file will be in the same directory as the current module. | |
| 135 bool path_ok = PathService::Get(DIR_MODULE, &data_path); | |
| 136 wchar_t tmp_buffer[_MAX_PATH] = {0}; | |
| 137 wcscpy_s(tmp_buffer, data_path.value().c_str()); | |
| 138 debug::Alias(tmp_buffer); | |
| 139 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); | 127 bool path_ok = PathService::Get(DIR_ANDROID_APP_DATA, &data_path); |
| 142 #else | 128 #else |
| 143 // For now, expect the data file to be alongside the executable. | 129 // For now, expect the data file to be alongside the executable. |
| 144 // This is sufficient while we work on unit tests, but will eventually | 130 // This is sufficient while we work on unit tests, but will eventually |
| 145 // likely live in a data directory. | 131 // likely live in a data directory. |
| 146 bool path_ok = PathService::Get(DIR_EXE, &data_path); | 132 bool path_ok = PathService::Get(DIR_EXE, &data_path); |
| 147 #endif | 133 #endif |
| 148 DCHECK(path_ok); | 134 DCHECK(path_ok); |
| 149 data_path = data_path.AppendASCII(kIcuDataFileName); | 135 data_path = data_path.AppendASCII(kIcuDataFileName); |
| 150 | 136 |
| 151 #if defined(OS_WIN) | |
| 152 // TODO(scottmg): http://crbug.com/445616 | |
| 153 wchar_t tmp_buffer2[_MAX_PATH] = {0}; | |
| 154 wcscpy_s(tmp_buffer2, data_path.value().c_str()); | |
| 155 debug::Alias(tmp_buffer2); | |
| 156 #endif | |
| 157 | |
| 158 #else | 137 #else |
| 159 // Assume it is in the framework bundle's Resources directory. | 138 // Assume it is in the framework bundle's Resources directory. |
| 160 ScopedCFTypeRef<CFStringRef> data_file_name( | 139 ScopedCFTypeRef<CFStringRef> data_file_name( |
| 161 SysUTF8ToCFStringRef(kIcuDataFileName)); | 140 SysUTF8ToCFStringRef(kIcuDataFileName)); |
| 162 FilePath data_path = | 141 FilePath data_path = |
| 163 mac::PathForFrameworkBundleResource(data_file_name); | 142 mac::PathForFrameworkBundleResource(data_file_name); |
| 164 if (data_path.empty()) { | 143 if (data_path.empty()) { |
| 165 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; | 144 LOG(ERROR) << kIcuDataFileName << " not found in bundle"; |
| 166 return false; | 145 return false; |
| 167 } | 146 } |
| 168 #endif // OS check | 147 #endif // OS check |
| 169 if (!mapped_file.Initialize(data_path)) { | 148 if (!mapped_file.Initialize(data_path)) { |
| 170 #if defined(OS_WIN) | |
| 171 CHECK(false); // TODO(scottmg): http://crbug.com/445616 | |
| 172 #endif | |
| 173 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); | 149 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); |
| 174 return false; | 150 return false; |
| 175 } | 151 } |
| 176 } | 152 } |
| 177 UErrorCode err = U_ZERO_ERROR; | 153 UErrorCode err = U_ZERO_ERROR; |
| 178 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); | 154 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); |
| 179 result = (err == U_ZERO_ERROR); | 155 result = (err == U_ZERO_ERROR); |
| 180 #if defined(OS_WIN) | |
| 181 CHECK(result); // TODO(scottmg): http://crbug.com/445616 | |
| 182 #endif | |
| 183 #endif | 156 #endif |
| 184 | 157 |
| 185 // To respond to the timezone change properly, the default timezone | 158 // To respond to the timezone change properly, the default timezone |
| 186 // cache in ICU has to be populated on starting up. | 159 // cache in ICU has to be populated on starting up. |
| 187 // TODO(jungshik): Some callers do not care about tz at all. If necessary, | 160 // 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 | 161 // add a boolean argument to this function to init'd the default tz only |
| 189 // when requested. | 162 // when requested. |
| 190 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 163 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 191 if (result) | 164 if (result) |
| 192 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); | 165 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 193 #endif | 166 #endif |
| 194 return result; | 167 return result; |
| 195 } | 168 } |
| 196 #endif | 169 #endif |
| 197 | 170 |
| 198 void AllowMultipleInitializeCallsForTesting() { | 171 void AllowMultipleInitializeCallsForTesting() { |
| 199 #if !defined(NDEBUG) | 172 #if !defined(NDEBUG) |
| 200 g_check_called_once = false; | 173 g_check_called_once = false; |
| 201 #endif | 174 #endif |
| 202 } | 175 } |
| 203 | 176 |
| 204 } // namespace i18n | 177 } // namespace i18n |
| 205 } // namespace base | 178 } // namespace base |
| OLD | NEW |