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" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/path_service.h" | 11 #include "base/path_service.h" |
13 #include "base/resource_util.h" | 12 #include "base/resource_util.h" |
14 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
15 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
16 #include "base/synchronization/lock.h" | |
17 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
18 #include "ui/base/l10n/l10n_util.h" | |
19 #include "ui/base/resource/data_pack.h" | 16 #include "ui/base/resource/data_pack.h" |
20 #include "ui/base/ui_base_paths.h" | |
21 #include "ui/gfx/font.h" | 17 #include "ui/gfx/font.h" |
22 | 18 |
23 namespace ui { | 19 namespace ui { |
24 | 20 |
25 namespace { | 21 namespace { |
26 | 22 |
27 HINSTANCE resources_data_dll; | 23 HINSTANCE resources_data_dll; |
28 | 24 |
29 // Returns the flags that should be passed to LoadLibraryEx. | 25 // Returns the flags that should be passed to LoadLibraryEx. |
30 DWORD GetDataDllLoadFlags() { | 26 DWORD GetDataDllLoadFlags() { |
(...skipping 17 matching lines...) Expand all Loading... |
48 // As a convenience, set resources_data_ to the current resource module. | 44 // As a convenience, set resources_data_ to the current resource module. |
49 DCHECK(NULL == resources_data_) << "common resources already loaded"; | 45 DCHECK(NULL == resources_data_) << "common resources already loaded"; |
50 | 46 |
51 if (resources_data_dll) { | 47 if (resources_data_dll) { |
52 resources_data_ = resources_data_dll; | 48 resources_data_ = resources_data_dll; |
53 } else { | 49 } else { |
54 resources_data_ = GetModuleHandle(NULL); | 50 resources_data_ = GetModuleHandle(NULL); |
55 } | 51 } |
56 } | 52 } |
57 | 53 |
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 | |
78 void ResourceBundle::LoadTestResources(const FilePath& path) { | 54 void ResourceBundle::LoadTestResources(const FilePath& path) { |
79 // On Windows, the test resources are normally compiled into the binary | 55 // On Windows, the test resources are normally compiled into the binary |
80 // itself. | 56 // itself. |
81 } | 57 } |
82 | 58 |
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 | |
102 // static | 59 // static |
103 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( | 60 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( |
104 DataHandle module, int resource_id) { | 61 DataHandle module, int resource_id) { |
105 void* data_ptr; | 62 void* data_ptr; |
106 size_t data_size; | 63 size_t data_size; |
107 if (base::GetDataResourceFromModule(module, resource_id, &data_ptr, | 64 if (base::GetDataResourceFromModule(module, resource_id, &data_ptr, |
108 &data_size)) { | 65 &data_size)) { |
109 return new RefCountedStaticMemory( | 66 return new RefCountedStaticMemory( |
110 reinterpret_cast<const unsigned char*>(data_ptr), data_size); | 67 reinterpret_cast<const unsigned char*>(data_ptr), data_size); |
111 } else { | 68 } else { |
112 return NULL; | 69 return NULL; |
113 } | 70 } |
114 } | 71 } |
115 | 72 |
116 // static | 73 // static |
117 void ResourceBundle::SetResourcesDataDLL(HINSTANCE handle) { | 74 void ResourceBundle::SetResourcesDataDLL(HINSTANCE handle) { |
118 resources_data_dll = handle; | 75 resources_data_dll = handle; |
119 } | 76 } |
120 | 77 |
121 HICON ResourceBundle::LoadThemeIcon(int icon_id) { | 78 HICON ResourceBundle::LoadThemeIcon(int icon_id) { |
122 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); | 79 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); |
123 } | 80 } |
124 | 81 |
125 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 82 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
126 void* data_ptr; | 83 void* data_ptr; |
127 size_t data_size; | 84 size_t data_size; |
| 85 base::StringPiece data; |
128 if (base::GetDataResourceFromModule(resources_data_, | 86 if (base::GetDataResourceFromModule(resources_data_, |
129 resource_id, | 87 resource_id, |
130 &data_ptr, | 88 &data_ptr, |
131 &data_size)) { | 89 &data_size)) { |
132 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); | 90 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); |
133 } else if (locale_resources_data_ && | 91 } else if (locale_resources_data_.get() && |
134 base::GetDataResourceFromModule(locale_resources_data_, | 92 locale_resources_data_->GetStringPiece(resource_id, &data)) { |
135 resource_id, | 93 return data; |
136 &data_ptr, | |
137 &data_size)) { | |
138 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); | |
139 } | 94 } |
140 | 95 |
141 base::StringPiece data; | 96 // TODO(tony): Remove this ATL code once we remove the strings in |
| 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 |
142 for (size_t i = 0; i < data_packs_.size(); ++i) { | 105 for (size_t i = 0; i < data_packs_.size(); ++i) { |
143 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 106 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
144 return data; | 107 return data; |
145 } | 108 } |
146 | 109 |
147 return base::StringPiece(); | 110 return base::StringPiece(); |
148 } | 111 } |
149 | 112 |
150 // Loads and returns a cursor from the current module. | 113 // Loads and returns a cursor from the current module. |
151 HCURSOR ResourceBundle::LoadCursor(int cursor_id) { | 114 HCURSOR ResourceBundle::LoadCursor(int cursor_id) { |
152 return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); | 115 return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); |
153 } | 116 } |
154 | 117 |
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 | |
185 // Windows only uses SkBitmap for gfx::Image, so this is the same as | 118 // Windows only uses SkBitmap for gfx::Image, so this is the same as |
186 // GetImageNamed. | 119 // GetImageNamed. |
187 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 120 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
188 return GetImageNamed(resource_id); | 121 return GetImageNamed(resource_id); |
189 } | 122 } |
190 | 123 |
191 } // namespace ui; | 124 } // namespace ui; |
OLD | NEW |