OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
7 #include <atlbase.h> | 7 #include <atlbase.h> |
8 | 8 |
9 #include "base/debug/stack_trace.h" | 9 #include "base/debug/stack_trace.h" |
| 10 #include "base/file_util.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
| 15 #include "base/synchronization/lock.h" |
14 #include "base/win/resource_util.h" | 16 #include "base/win/resource_util.h" |
15 #include "base/win/windows_version.h" | 17 #include "base/win/windows_version.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
16 #include "ui/base/resource/data_pack.h" | 19 #include "ui/base/resource/data_pack.h" |
| 20 #include "ui/base/ui_base_paths.h" |
17 #include "ui/gfx/font.h" | 21 #include "ui/gfx/font.h" |
18 | 22 |
19 namespace ui { | 23 namespace ui { |
20 | 24 |
21 namespace { | 25 namespace { |
22 | 26 |
23 HINSTANCE resources_data_dll; | 27 HINSTANCE resources_data_dll; |
24 | 28 |
25 // Returns the flags that should be passed to LoadLibraryEx. | 29 // Returns the flags that should be passed to LoadLibraryEx. |
26 DWORD GetDataDllLoadFlags() { | 30 DWORD GetDataDllLoadFlags() { |
(...skipping 17 matching lines...) Expand all Loading... |
44 // As a convenience, set resources_data_ to the current resource module. | 48 // As a convenience, set resources_data_ to the current resource module. |
45 DCHECK(NULL == resources_data_) << "common resources already loaded"; | 49 DCHECK(NULL == resources_data_) << "common resources already loaded"; |
46 | 50 |
47 if (resources_data_dll) { | 51 if (resources_data_dll) { |
48 resources_data_ = resources_data_dll; | 52 resources_data_ = resources_data_dll; |
49 } else { | 53 } else { |
50 resources_data_ = GetModuleHandle(NULL); | 54 resources_data_ = GetModuleHandle(NULL); |
51 } | 55 } |
52 } | 56 } |
53 | 57 |
| 58 std::string ResourceBundle::LoadLocaleResources( |
| 59 const std::string& pref_locale) { |
| 60 DCHECK(NULL == locale_resources_data_) << "locale dll already loaded"; |
| 61 const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
| 62 const FilePath& locale_path = GetLocaleFilePath(app_locale); |
| 63 if (locale_path.value().empty()) { |
| 64 // It's possible that there are no locale dlls found, in which case we just |
| 65 // return. |
| 66 NOTREACHED(); |
| 67 return std::string(); |
| 68 } |
| 69 |
| 70 // The dll should only have resources, not executable code. |
| 71 locale_resources_data_ = LoadLibraryEx(locale_path.value().c_str(), NULL, |
| 72 GetDataDllLoadFlags()); |
| 73 DCHECK(locale_resources_data_ != NULL) << |
| 74 "unable to load generated resources"; |
| 75 return app_locale; |
| 76 } |
| 77 |
54 void ResourceBundle::LoadTestResources(const FilePath& path) { | 78 void ResourceBundle::LoadTestResources(const FilePath& path) { |
55 // On Windows, the test resources are normally compiled into the binary | 79 // On Windows, the test resources are normally compiled into the binary |
56 // itself. | 80 // itself. |
57 } | 81 } |
58 | 82 |
| 83 void ResourceBundle::UnloadLocaleResources() { |
| 84 if (locale_resources_data_) { |
| 85 BOOL rv = FreeLibrary(locale_resources_data_); |
| 86 DCHECK(rv); |
| 87 locale_resources_data_ = NULL; |
| 88 } |
| 89 } |
| 90 |
| 91 // static |
| 92 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
| 93 FilePath locale_path; |
| 94 PathService::Get(ui::DIR_LOCALES, &locale_path); |
| 95 |
| 96 if (app_locale.empty()) |
| 97 return FilePath(); |
| 98 |
| 99 return locale_path.AppendASCII(app_locale + ".dll"); |
| 100 } |
| 101 |
59 // static | 102 // static |
60 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( | 103 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( |
61 DataHandle module, int resource_id) { | 104 DataHandle module, int resource_id) { |
62 void* data_ptr; | 105 void* data_ptr; |
63 size_t data_size; | 106 size_t data_size; |
64 if (base::win::GetDataResourceFromModule(module, resource_id, &data_ptr, | 107 if (base::win::GetDataResourceFromModule(module, resource_id, &data_ptr, |
65 &data_size)) { | 108 &data_size)) { |
66 return new RefCountedStaticMemory( | 109 return new RefCountedStaticMemory( |
67 reinterpret_cast<const unsigned char*>(data_ptr), data_size); | 110 reinterpret_cast<const unsigned char*>(data_ptr), data_size); |
68 } else { | 111 } else { |
69 return NULL; | 112 return NULL; |
70 } | 113 } |
71 } | 114 } |
72 | 115 |
73 // static | 116 // static |
74 void ResourceBundle::SetResourcesDataDLL(HINSTANCE handle) { | 117 void ResourceBundle::SetResourcesDataDLL(HINSTANCE handle) { |
75 resources_data_dll = handle; | 118 resources_data_dll = handle; |
76 } | 119 } |
77 | 120 |
78 HICON ResourceBundle::LoadThemeIcon(int icon_id) { | 121 HICON ResourceBundle::LoadThemeIcon(int icon_id) { |
79 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); | 122 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); |
80 } | 123 } |
81 | 124 |
82 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 125 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
83 void* data_ptr; | 126 void* data_ptr; |
84 size_t data_size; | 127 size_t data_size; |
85 base::StringPiece data; | |
86 if (base::win::GetDataResourceFromModule(resources_data_, | 128 if (base::win::GetDataResourceFromModule(resources_data_, |
87 resource_id, | 129 resource_id, |
88 &data_ptr, | 130 &data_ptr, |
89 &data_size)) { | 131 &data_size)) { |
90 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); | 132 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); |
91 } else if (locale_resources_data_.get() && | 133 } else if (locale_resources_data_ && |
92 locale_resources_data_->GetStringPiece(resource_id, &data)) { | 134 base::GetDataResourceFromModule(locale_resources_data_, |
93 return data; | 135 resource_id, |
| 136 &data_ptr, |
| 137 &data_size)) { |
| 138 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); |
94 } | 139 } |
95 | 140 |
96 // TODO(tony): Remove this ATL code once we remove the strings in | 141 base::StringPiece data; |
97 // chrome.dll. | |
98 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage( | |
99 resources_data_, resource_id); | |
100 if (image) { | |
101 return base::StringPiece(reinterpret_cast<const char*>(image->achString), | |
102 image->nLength * 2); | |
103 } | |
104 | |
105 for (size_t i = 0; i < data_packs_.size(); ++i) { | 142 for (size_t i = 0; i < data_packs_.size(); ++i) { |
106 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 143 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
107 return data; | 144 return data; |
108 } | 145 } |
109 | 146 |
110 return base::StringPiece(); | 147 return base::StringPiece(); |
111 } | 148 } |
112 | 149 |
113 // Loads and returns a cursor from the current module. | 150 // Loads and returns a cursor from the current module. |
114 HCURSOR ResourceBundle::LoadCursor(int cursor_id) { | 151 HCURSOR ResourceBundle::LoadCursor(int cursor_id) { |
115 return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); | 152 return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); |
116 } | 153 } |
117 | 154 |
| 155 string16 ResourceBundle::GetLocalizedString(int message_id) { |
| 156 // If for some reason we were unable to load a resource dll, return an empty |
| 157 // string (better than crashing). |
| 158 if (!locale_resources_data_) { |
| 159 base::debug::StackTrace().PrintBacktrace(); // See http://crbug.com/21925. |
| 160 LOG(WARNING) << "locale resources are not loaded"; |
| 161 return string16(); |
| 162 } |
| 163 |
| 164 DCHECK(IS_INTRESOURCE(message_id)); |
| 165 |
| 166 // Get a reference directly to the string resource. |
| 167 HINSTANCE hinstance = locale_resources_data_; |
| 168 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(hinstance, |
| 169 message_id); |
| 170 if (!image) { |
| 171 // Fall back on the current module (shouldn't be any strings here except |
| 172 // in unittests). |
| 173 image = AtlGetStringResourceImage(resources_data_, message_id); |
| 174 if (!image) { |
| 175 // See http://crbug.com/21925. |
| 176 base::debug::StackTrace().PrintBacktrace(); |
| 177 NOTREACHED() << "unable to find resource: " << message_id; |
| 178 return string16(); |
| 179 } |
| 180 } |
| 181 // Copy into a string16 and return. |
| 182 return string16(image->achString, image->nLength); |
| 183 } |
| 184 |
118 // Windows only uses SkBitmap for gfx::Image, so this is the same as | 185 // Windows only uses SkBitmap for gfx::Image, so this is the same as |
119 // GetImageNamed. | 186 // GetImageNamed. |
120 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 187 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
121 return GetImageNamed(resource_id); | 188 return GetImageNamed(resource_id); |
122 } | 189 } |
123 | 190 |
124 } // namespace ui; | 191 } // namespace ui; |
OLD | NEW |