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

Side by Side Diff: base/i18n/icu_util.cc

Issue 697203006: Update the ICU's default timezone in render (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: init tz only when icu data load succ. Created 5 years, 8 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/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 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
22 #include "third_party/icu/source/i18n/unicode/timezone.h"
23 #endif
21 24
22 #if defined(OS_MACOSX) 25 #if defined(OS_MACOSX)
23 #include "base/mac/foundation_util.h" 26 #include "base/mac/foundation_util.h"
24 #endif 27 #endif
25 28
26 #define ICU_UTIL_DATA_FILE 0 29 #define ICU_UTIL_DATA_FILE 0
27 #define ICU_UTIL_DATA_SHARED 1 30 #define ICU_UTIL_DATA_SHARED 1
28 #define ICU_UTIL_DATA_STATIC 2 31 #define ICU_UTIL_DATA_STATIC 2
29 32
30 namespace base { 33 namespace base {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 #endif 86 #endif
84 87
85 88
86 #if !defined(OS_NACL) 89 #if !defined(OS_NACL)
87 bool InitializeICU() { 90 bool InitializeICU() {
88 #if !defined(NDEBUG) 91 #if !defined(NDEBUG)
89 DCHECK(!g_check_called_once || !g_called_once); 92 DCHECK(!g_check_called_once || !g_called_once);
90 g_called_once = true; 93 g_called_once = true;
91 #endif 94 #endif
92 95
96 bool result;
93 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) 97 #if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
94 // We expect to find the ICU data module alongside the current module. 98 // We expect to find the ICU data module alongside the current module.
95 FilePath data_path; 99 FilePath data_path;
96 PathService::Get(base::DIR_MODULE, &data_path); 100 PathService::Get(base::DIR_MODULE, &data_path);
97 data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME); 101 data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME);
98 102
99 HMODULE module = LoadLibrary(data_path.value().c_str()); 103 HMODULE module = LoadLibrary(data_path.value().c_str());
100 if (!module) { 104 if (!module) {
101 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME; 105 LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME;
102 return false; 106 return false;
103 } 107 }
104 108
105 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); 109 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
106 if (!addr) { 110 if (!addr) {
107 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " 111 LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in "
108 << ICU_UTIL_DATA_SHARED_MODULE_NAME; 112 << ICU_UTIL_DATA_SHARED_MODULE_NAME;
109 return false; 113 return false;
110 } 114 }
111 115
112 UErrorCode err = U_ZERO_ERROR; 116 UErrorCode err = U_ZERO_ERROR;
113 udata_setCommonData(reinterpret_cast<void*>(addr), &err); 117 udata_setCommonData(reinterpret_cast<void*>(addr), &err);
114 return err == U_ZERO_ERROR; 118 result = (err == U_ZERO_ERROR);
115 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) 119 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
116 // The ICU data is statically linked. 120 // The ICU data is statically linked.
117 return true; 121 result = true;
118 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) 122 #elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
119 // If the ICU data directory is set, ICU won't actually load the data until 123 // If the ICU data directory is set, ICU won't actually load the data until
120 // it is needed. This can fail if the process is sandboxed at that time. 124 // it is needed. This can fail if the process is sandboxed at that time.
121 // Instead, we map the file in and hand off the data so the sandbox won't 125 // Instead, we map the file in and hand off the data so the sandbox won't
122 // cause any problems. 126 // cause any problems.
123 127
124 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever 128 // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever
125 // be released. 129 // be released.
126 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ()); 130 CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ());
127 if (!mapped_file.IsValid()) { 131 if (!mapped_file.IsValid()) {
(...skipping 23 matching lines...) Expand all
151 return false; 155 return false;
152 } 156 }
153 #endif // OS check 157 #endif // OS check
154 if (!mapped_file.Initialize(data_path)) { 158 if (!mapped_file.Initialize(data_path)) {
155 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe(); 159 LOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe();
156 return false; 160 return false;
157 } 161 }
158 } 162 }
159 UErrorCode err = U_ZERO_ERROR; 163 UErrorCode err = U_ZERO_ERROR;
160 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err); 164 udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err);
161 return err == U_ZERO_ERROR; 165 result = (err == U_ZERO_ERROR);
162 #endif 166 #endif
167
168 // To respond to the timezone change properly, the default timezone
169 // cache in ICU has to be populated on starting up.
170 // TODO(jungshik): Some callers do not care about tz at all. If necessary,
171 // add a boolean argument to this function to init'd the default tz only
172 // when requested.
173 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
174 if (result)
175 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
176 #endif
177 return result;
163 } 178 }
164 #endif 179 #endif
165 180
166 void AllowMultipleInitializeCallsForTesting() { 181 void AllowMultipleInitializeCallsForTesting() {
167 #if !defined(NDEBUG) 182 #if !defined(NDEBUG)
168 g_check_called_once = false; 183 g_check_called_once = false;
169 #endif 184 #endif
170 } 185 }
171 186
172 } // namespace i18n 187 } // namespace i18n
173 } // namespace base 188 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | content/browser/renderer_host/render_process_host_impl.h » ('j') | content/browser/time_zone_monitor.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698