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

Side by Side Diff: app/resource_bundle_win.cc

Issue 6263008: Move ResourceBundle, DataPack to ui/base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « app/resource_bundle_posix.cc ('k') | app/test_suite.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "app/resource_bundle.h"
6
7 #include <atlbase.h>
8
9 #include "app/app_paths.h"
10 #include "app/data_pack.h"
11 #include "app/l10n_util.h"
12 #include "base/debug/stack_trace.h"
13 #include "base/file_util.h"
14 #include "base/lock.h"
15 #include "base/logging.h"
16 #include "base/path_service.h"
17 #include "base/resource_util.h"
18 #include "base/stl_util-inl.h"
19 #include "base/string_piece.h"
20 #include "base/win/windows_version.h"
21
22 #include "gfx/font.h"
23
24 namespace {
25
26 // Returns the flags that should be passed to LoadLibraryEx.
27 DWORD GetDataDllLoadFlags() {
28 if (base::win::GetVersion() >= base::win::VERSION_VISTA)
29 return LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE;
30
31 return DONT_RESOLVE_DLL_REFERENCES;
32 }
33
34 } // end anonymous namespace
35
36 ResourceBundle::~ResourceBundle() {
37 FreeImages();
38 UnloadLocaleResources();
39 STLDeleteContainerPointers(data_packs_.begin(),
40 data_packs_.end());
41 resources_data_ = NULL;
42 }
43
44 void ResourceBundle::LoadCommonResources() {
45 // As a convenience, set resources_data_ to the current resource module.
46 DCHECK(NULL == resources_data_) << "common resources already loaded";
47 resources_data_ = _AtlBaseModule.GetResourceInstance();
48 }
49
50 std::string ResourceBundle::LoadLocaleResources(
51 const std::string& pref_locale) {
52 DCHECK(NULL == locale_resources_data_) << "locale dll already loaded";
53 const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
54 const FilePath& locale_path = GetLocaleFilePath(app_locale);
55 if (locale_path.value().empty()) {
56 // It's possible that there are no locale dlls found, in which case we just
57 // return.
58 NOTREACHED();
59 return std::string();
60 }
61
62 // The dll should only have resources, not executable code.
63 locale_resources_data_ = LoadLibraryEx(locale_path.value().c_str(), NULL,
64 GetDataDllLoadFlags());
65 DCHECK(locale_resources_data_ != NULL) <<
66 "unable to load generated resources";
67 return app_locale;
68 }
69
70 void ResourceBundle::UnloadLocaleResources() {
71 if (locale_resources_data_) {
72 BOOL rv = FreeLibrary(locale_resources_data_);
73 DCHECK(rv);
74 locale_resources_data_ = NULL;
75 }
76 }
77
78 // static
79 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) {
80 FilePath locale_path;
81 PathService::Get(app::DIR_LOCALES, &locale_path);
82
83 if (app_locale.empty())
84 return FilePath();
85
86 return locale_path.AppendASCII(app_locale + ".dll");
87 }
88
89 // static
90 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes(
91 DataHandle module, int resource_id) {
92 void* data_ptr;
93 size_t data_size;
94 if (base::GetDataResourceFromModule(module, resource_id, &data_ptr,
95 &data_size)) {
96 return new RefCountedStaticMemory(
97 reinterpret_cast<const unsigned char*>(data_ptr), data_size);
98 } else {
99 return NULL;
100 }
101 }
102
103 HICON ResourceBundle::LoadThemeIcon(int icon_id) {
104 return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id));
105 }
106
107 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
108 void* data_ptr;
109 size_t data_size;
110 if (base::GetDataResourceFromModule(_AtlBaseModule.GetModuleInstance(),
111 resource_id,
112 &data_ptr,
113 &data_size)) {
114 return base::StringPiece(static_cast<const char*>(data_ptr), data_size);
115 } else if (locale_resources_data_ &&
116 base::GetDataResourceFromModule(locale_resources_data_,
117 resource_id,
118 &data_ptr,
119 &data_size)) {
120 return base::StringPiece(static_cast<const char*>(data_ptr), data_size);
121 }
122
123 base::StringPiece data;
124 for (size_t i = 0; i < data_packs_.size(); ++i) {
125 if (data_packs_[i]->GetStringPiece(resource_id, &data))
126 return data;
127 }
128
129 return base::StringPiece();
130 }
131
132 // Loads and returns a cursor from the current module.
133 HCURSOR ResourceBundle::LoadCursor(int cursor_id) {
134 return ::LoadCursor(_AtlBaseModule.GetModuleInstance(),
135 MAKEINTRESOURCE(cursor_id));
136 }
137
138 string16 ResourceBundle::GetLocalizedString(int message_id) {
139 // If for some reason we were unable to load a resource dll, return an empty
140 // string (better than crashing).
141 if (!locale_resources_data_) {
142 base::debug::StackTrace().PrintBacktrace(); // See http://crbug.com/21925.
143 LOG(WARNING) << "locale resources are not loaded";
144 return string16();
145 }
146
147 DCHECK(IS_INTRESOURCE(message_id));
148
149 // Get a reference directly to the string resource.
150 HINSTANCE hinstance = locale_resources_data_;
151 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(hinstance,
152 message_id);
153 if (!image) {
154 // Fall back on the current module (shouldn't be any strings here except
155 // in unittests).
156 image = AtlGetStringResourceImage(_AtlBaseModule.GetModuleInstance(),
157 message_id);
158 if (!image) {
159 // See http://crbug.com/21925.
160 base::debug::StackTrace().PrintBacktrace();
161 NOTREACHED() << "unable to find resource: " << message_id;
162 return string16();
163 }
164 }
165 // Copy into a string16 and return.
166 return string16(image->achString, image->nLength);
167 }
OLDNEW
« no previous file with comments | « app/resource_bundle_posix.cc ('k') | app/test_suite.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698