| 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/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/resource_util.h" | 13 #include "base/resource_util.h" |
| 14 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
| 15 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/win/windows_version.h" | 17 #include "base/win/windows_version.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/data_pack.h" | 19 #include "ui/base/resource/data_pack.h" |
| 20 #include "ui/base/ui_base_paths.h" | 20 #include "ui/base/ui_base_paths.h" |
| 21 #include "ui/gfx/font.h" | 21 #include "ui/gfx/font.h" |
| 22 | 22 |
| 23 namespace ui { | 23 namespace ui { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 HINSTANCE resources_data_dll; |
| 28 |
| 27 // Returns the flags that should be passed to LoadLibraryEx. | 29 // Returns the flags that should be passed to LoadLibraryEx. |
| 28 DWORD GetDataDllLoadFlags() { | 30 DWORD GetDataDllLoadFlags() { |
| 29 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | 31 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
| 30 return LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE; | 32 return LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE; |
| 31 | 33 |
| 32 return DONT_RESOLVE_DLL_REFERENCES; | 34 return DONT_RESOLVE_DLL_REFERENCES; |
| 33 } | 35 } |
| 34 | 36 |
| 35 } // end anonymous namespace | 37 } // end anonymous namespace |
| 36 | 38 |
| 37 ResourceBundle::~ResourceBundle() { | 39 ResourceBundle::~ResourceBundle() { |
| 38 FreeImages(); | 40 FreeImages(); |
| 39 UnloadLocaleResources(); | 41 UnloadLocaleResources(); |
| 40 STLDeleteContainerPointers(data_packs_.begin(), | 42 STLDeleteContainerPointers(data_packs_.begin(), |
| 41 data_packs_.end()); | 43 data_packs_.end()); |
| 42 resources_data_ = NULL; | 44 resources_data_ = NULL; |
| 43 } | 45 } |
| 44 | 46 |
| 45 void ResourceBundle::LoadCommonResources() { | 47 void ResourceBundle::LoadCommonResources() { |
| 46 // As a convenience, set resources_data_ to the current resource module. | 48 // As a convenience, set resources_data_ to the current resource module. |
| 47 DCHECK(NULL == resources_data_) << "common resources already loaded"; | 49 DCHECK(NULL == resources_data_) << "common resources already loaded"; |
| 48 resources_data_ = _AtlBaseModule.GetResourceInstance(); | 50 |
| 51 if (resources_data_dll) { |
| 52 resources_data_ = resources_data_dll; |
| 53 } else { |
| 54 resources_data_ = GetModuleHandle(NULL); |
| 55 } |
| 49 } | 56 } |
| 50 | 57 |
| 51 std::string ResourceBundle::LoadLocaleResources( | 58 std::string ResourceBundle::LoadLocaleResources( |
| 52 const std::string& pref_locale) { | 59 const std::string& pref_locale) { |
| 53 DCHECK(NULL == locale_resources_data_) << "locale dll already loaded"; | 60 DCHECK(NULL == locale_resources_data_) << "locale dll already loaded"; |
| 54 const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | 61 const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
| 55 const FilePath& locale_path = GetLocaleFilePath(app_locale); | 62 const FilePath& locale_path = GetLocaleFilePath(app_locale); |
| 56 if (locale_path.value().empty()) { | 63 if (locale_path.value().empty()) { |
| 57 // It's possible that there are no locale dlls found, in which case we just | 64 // It's possible that there are no locale dlls found, in which case we just |
| 58 // return. | 65 // return. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 size_t data_size; | 106 size_t data_size; |
| 100 if (base::GetDataResourceFromModule(module, resource_id, &data_ptr, | 107 if (base::GetDataResourceFromModule(module, resource_id, &data_ptr, |
| 101 &data_size)) { | 108 &data_size)) { |
| 102 return new RefCountedStaticMemory( | 109 return new RefCountedStaticMemory( |
| 103 reinterpret_cast<const unsigned char*>(data_ptr), data_size); | 110 reinterpret_cast<const unsigned char*>(data_ptr), data_size); |
| 104 } else { | 111 } else { |
| 105 return NULL; | 112 return NULL; |
| 106 } | 113 } |
| 107 } | 114 } |
| 108 | 115 |
| 116 // static |
| 117 void ResourceBundle::SetResourcesDataDLL(HINSTANCE handle) { |
| 118 resources_data_dll = handle; |
| 119 } |
| 120 |
| 109 HICON ResourceBundle::LoadThemeIcon(int icon_id) { | 121 HICON ResourceBundle::LoadThemeIcon(int icon_id) { |
| 110 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); | 122 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); |
| 111 } | 123 } |
| 112 | 124 |
| 113 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 125 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
| 114 void* data_ptr; | 126 void* data_ptr; |
| 115 size_t data_size; | 127 size_t data_size; |
| 116 if (base::GetDataResourceFromModule(_AtlBaseModule.GetModuleInstance(), | 128 if (base::GetDataResourceFromModule(resources_data_, |
| 117 resource_id, | 129 resource_id, |
| 118 &data_ptr, | 130 &data_ptr, |
| 119 &data_size)) { | 131 &data_size)) { |
| 120 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); | 132 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); |
| 121 } else if (locale_resources_data_ && | 133 } else if (locale_resources_data_ && |
| 122 base::GetDataResourceFromModule(locale_resources_data_, | 134 base::GetDataResourceFromModule(locale_resources_data_, |
| 123 resource_id, | 135 resource_id, |
| 124 &data_ptr, | 136 &data_ptr, |
| 125 &data_size)) { | 137 &data_size)) { |
| 126 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); | 138 return base::StringPiece(static_cast<const char*>(data_ptr), data_size); |
| 127 } | 139 } |
| 128 | 140 |
| 129 base::StringPiece data; | 141 base::StringPiece data; |
| 130 for (size_t i = 0; i < data_packs_.size(); ++i) { | 142 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 131 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 143 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 132 return data; | 144 return data; |
| 133 } | 145 } |
| 134 | 146 |
| 135 return base::StringPiece(); | 147 return base::StringPiece(); |
| 136 } | 148 } |
| 137 | 149 |
| 138 // Loads and returns a cursor from the current module. | 150 // Loads and returns a cursor from the current module. |
| 139 HCURSOR ResourceBundle::LoadCursor(int cursor_id) { | 151 HCURSOR ResourceBundle::LoadCursor(int cursor_id) { |
| 140 return ::LoadCursor(_AtlBaseModule.GetModuleInstance(), | 152 return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); |
| 141 MAKEINTRESOURCE(cursor_id)); | |
| 142 } | 153 } |
| 143 | 154 |
| 144 string16 ResourceBundle::GetLocalizedString(int message_id) { | 155 string16 ResourceBundle::GetLocalizedString(int message_id) { |
| 145 // If for some reason we were unable to load a resource dll, return an empty | 156 // If for some reason we were unable to load a resource dll, return an empty |
| 146 // string (better than crashing). | 157 // string (better than crashing). |
| 147 if (!locale_resources_data_) { | 158 if (!locale_resources_data_) { |
| 148 base::debug::StackTrace().PrintBacktrace(); // See http://crbug.com/21925. | 159 base::debug::StackTrace().PrintBacktrace(); // See http://crbug.com/21925. |
| 149 LOG(WARNING) << "locale resources are not loaded"; | 160 LOG(WARNING) << "locale resources are not loaded"; |
| 150 return string16(); | 161 return string16(); |
| 151 } | 162 } |
| 152 | 163 |
| 153 DCHECK(IS_INTRESOURCE(message_id)); | 164 DCHECK(IS_INTRESOURCE(message_id)); |
| 154 | 165 |
| 155 // Get a reference directly to the string resource. | 166 // Get a reference directly to the string resource. |
| 156 HINSTANCE hinstance = locale_resources_data_; | 167 HINSTANCE hinstance = locale_resources_data_; |
| 157 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(hinstance, | 168 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(hinstance, |
| 158 message_id); | 169 message_id); |
| 159 if (!image) { | 170 if (!image) { |
| 160 // Fall back on the current module (shouldn't be any strings here except | 171 // Fall back on the current module (shouldn't be any strings here except |
| 161 // in unittests). | 172 // in unittests). |
| 162 image = AtlGetStringResourceImage(_AtlBaseModule.GetModuleInstance(), | 173 image = AtlGetStringResourceImage(resources_data_, message_id); |
| 163 message_id); | |
| 164 if (!image) { | 174 if (!image) { |
| 165 // See http://crbug.com/21925. | 175 // See http://crbug.com/21925. |
| 166 base::debug::StackTrace().PrintBacktrace(); | 176 base::debug::StackTrace().PrintBacktrace(); |
| 167 NOTREACHED() << "unable to find resource: " << message_id; | 177 NOTREACHED() << "unable to find resource: " << message_id; |
| 168 return string16(); | 178 return string16(); |
| 169 } | 179 } |
| 170 } | 180 } |
| 171 // Copy into a string16 and return. | 181 // Copy into a string16 and return. |
| 172 return string16(image->achString, image->nLength); | 182 return string16(image->achString, image->nLength); |
| 173 } | 183 } |
| 174 | 184 |
| 175 // 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 |
| 176 // GetImageNamed. | 186 // GetImageNamed. |
| 177 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 187 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
| 178 return GetImageNamed(resource_id); | 188 return GetImageNamed(resource_id); |
| 179 } | 189 } |
| 180 | 190 |
| 181 } // namespace ui; | 191 } // namespace ui; |
| OLD | NEW |