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

Side by Side Diff: app/resource_bundle_posix.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_mac.mm ('k') | app/resource_bundle_win.cc » ('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 "app/data_pack.h"
8 #include "app/l10n_util.h"
9 #include "base/lock.h"
10 #include "base/logging.h"
11 #include "base/stl_util-inl.h"
12 #include "base/string16.h"
13 #include "base/string_piece.h"
14 #include "gfx/font.h"
15
16 namespace {
17
18 app::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) {
19 app::DataPack* resources_pak = new app::DataPack;
20 bool success = resources_pak->Load(resources_pak_path);
21 if (!success) {
22 delete resources_pak;
23 resources_pak = NULL;
24 }
25 return resources_pak;
26 }
27
28 } // namespace
29
30 ResourceBundle::~ResourceBundle() {
31 FreeImages();
32 #if defined(OS_POSIX) && !defined(OS_MACOSX)
33 FreeGdkPixBufs();
34 #endif
35 UnloadLocaleResources();
36 STLDeleteContainerPointers(data_packs_.begin(),
37 data_packs_.end());
38 delete resources_data_;
39 resources_data_ = NULL;
40 }
41
42 void ResourceBundle::UnloadLocaleResources() {
43 delete locale_resources_data_;
44 locale_resources_data_ = NULL;
45 }
46
47 // static
48 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes(
49 DataHandle module, int resource_id) {
50 DCHECK(module);
51 return module->GetStaticMemory(resource_id);
52 }
53
54 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
55 DCHECK(resources_data_);
56 base::StringPiece data;
57 if (!resources_data_->GetStringPiece(resource_id, &data)) {
58 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
59 for (size_t i = 0; i < data_packs_.size(); ++i) {
60 if (data_packs_[i]->GetStringPiece(resource_id, &data))
61 return data;
62 }
63
64 return base::StringPiece();
65 }
66 }
67 return data;
68 }
69
70 string16 ResourceBundle::GetLocalizedString(int message_id) {
71 // If for some reason we were unable to load a resource pak, return an empty
72 // string (better than crashing).
73 if (!locale_resources_data_) {
74 LOG(WARNING) << "locale resources are not loaded";
75 return string16();
76 }
77
78 base::StringPiece data;
79 if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
80 // Fall back on the main data pack (shouldn't be any strings here except in
81 // unittests).
82 data = GetRawDataResource(message_id);
83 if (data.empty()) {
84 NOTREACHED() << "unable to find resource: " << message_id;
85 return string16();
86 }
87 }
88
89 // Data pack encodes strings as UTF16.
90 DCHECK_EQ(data.length() % 2, 0U);
91 string16 msg(reinterpret_cast<const char16*>(data.data()),
92 data.length() / 2);
93 return msg;
94 }
95
96 void ResourceBundle::LoadCommonResources() {
97 DCHECK(!resources_data_) << "chrome.pak already loaded";
98 FilePath resources_file_path = GetResourcesFilePath();
99 CHECK(!resources_file_path.empty()) << "chrome.pak not found";
100 resources_data_ = LoadResourcesDataPak(resources_file_path);
101 CHECK(resources_data_) << "failed to load chrome.pak";
102 }
103
104 std::string ResourceBundle::LoadLocaleResources(
105 const std::string& pref_locale) {
106 DCHECK(!locale_resources_data_) << "locale.pak already loaded";
107 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
108 FilePath locale_file_path = GetLocaleFilePath(app_locale);
109 if (locale_file_path.empty()) {
110 // It's possible that there is no locale.pak.
111 NOTREACHED();
112 return std::string();
113 }
114 locale_resources_data_ = LoadResourcesDataPak(locale_file_path);
115 CHECK(locale_resources_data_) << "failed to load locale.pak";
116 return app_locale;
117 }
OLDNEW
« no previous file with comments | « app/resource_bundle_mac.mm ('k') | app/resource_bundle_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698