icu_util.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
3// found in the LICENSE file.
4
5#include "base/i18n/icu_util.h"
6
7#include "build/build_config.h"
8
9#if defined(OS_WIN)
10#include <windows.h>
11#endif
12
13#include <string>
14
15#include "base/files/file_path.h"
16#include "base/files/memory_mapped_file.h"
17#include "base/logging.h"
18#include "base/path_service.h"
19#include "base/strings/string_util.h"
20#include "base/strings/sys_string_conversions.h"
21#include "third_party/icu/source/common/unicode/putil.h"
22#include "third_party/icu/source/common/unicode/udata.h"
23
24#if defined(OS_MACOSX)
25#include "base/mac/foundation_util.h"
26#endif
27
28#define ICU_UTIL_DATA_FILE   0
29#define ICU_UTIL_DATA_SHARED 1
30#define ICU_UTIL_DATA_STATIC 2
31
32#if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
33// Use an unversioned file name to simplify a icu version update down the road.
34// No need to change the filename in multiple places (gyp files, windows
35// build pkg configurations, etc). 'l' stands for Little Endian.
36#define ICU_UTIL_DATA_FILE_NAME "icudtl.dat"
37#elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED
38#define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
39#if defined(OS_WIN)
40#define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll"
41#endif
42#endif
43
44namespace base {
45namespace i18n {
46
47bool InitializeICU() {
48#ifndef NDEBUG
49  // Assert that we are not called more than once.  Even though calling this
50  // function isn't harmful (ICU can handle it), being called twice probably
51  // indicates a programming error.
52  static bool called_once = false;
53  DCHECK(!called_once);
54  called_once = true;
55#endif
56
57#if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
58  // We expect to find the ICU data module alongside the current module.
59  FilePath data_path;
60  PathService::Get(base::DIR_MODULE, &data_path);
61  data_path = data_path.AppendASCII(ICU_UTIL_DATA_SHARED_MODULE_NAME);
62
63  HMODULE module = LoadLibrary(data_path.value().c_str());
64  if (!module) {
65    DLOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME;
66    return false;
67  }
68
69  FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL);
70  if (!addr) {
71    DLOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in "
72               << ICU_UTIL_DATA_SHARED_MODULE_NAME;
73    return false;
74  }
75
76  UErrorCode err = U_ZERO_ERROR;
77  udata_setCommonData(reinterpret_cast<void*>(addr), &err);
78  return err == U_ZERO_ERROR;
79#elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC)
80  // The ICU data is statically linked.
81  return true;
82#elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE)
83  // If the ICU data directory is set, ICU won't actually load the data until
84  // it is needed.  This can fail if the process is sandboxed at that time.
85  // Instead, we map the file in and hand off the data so the sandbox won't
86  // cause any problems.
87
88  // Chrome doesn't normally shut down ICU, so the mapped data shouldn't ever
89  // be released.
90  CR_DEFINE_STATIC_LOCAL(base::MemoryMappedFile, mapped_file, ());
91  if (!mapped_file.IsValid()) {
92#if !defined(OS_MACOSX)
93    FilePath data_path;
94#if defined(OS_WIN)
95    // The data file will be in the same directory as the current module.
96    bool path_ok = PathService::Get(base::DIR_MODULE, &data_path);
97#else
98    // For now, expect the data file to be alongside the executable.
99    // This is sufficient while we work on unit tests, but will eventually
100    // likely live in a data directory.
101    bool path_ok = PathService::Get(base::DIR_EXE, &data_path);
102#endif
103    DCHECK(path_ok);
104    data_path = data_path.AppendASCII(ICU_UTIL_DATA_FILE_NAME);
105#else
106    // Assume it is in the framework bundle's Resources directory.
107    FilePath data_path =
108      base::mac::PathForFrameworkBundleResource(CFSTR(ICU_UTIL_DATA_FILE_NAME));
109    if (data_path.empty()) {
110      DLOG(ERROR) << ICU_UTIL_DATA_FILE_NAME << " not found in bundle";
111      return false;
112    }
113#endif  // OS check
114    if (!mapped_file.Initialize(data_path)) {
115      DLOG(ERROR) << "Couldn't mmap " << data_path.AsUTF8Unsafe();
116      return false;
117    }
118  }
119  UErrorCode err = U_ZERO_ERROR;
120  udata_setCommonData(const_cast<uint8*>(mapped_file.data()), &err);
121  return err == U_ZERO_ERROR;
122#endif
123}
124
125}  // namespace i18n
126}  // namespace base
127