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

Side by Side Diff: app/resource_bundle_posix.cc

Issue 442002: Share code between Mac and Linux in ResourceBundle. (Closed)
Patch Set: sync Created 11 years 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
« 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) 2009 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/gfx/font.h"
8 #include "base/data_pack.h"
9 #include "base/logging.h"
10 #include "base/string16.h"
11 #include "base/string_piece.h"
12
13 namespace {
14
15 base::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) {
16 base::DataPack* resources_pak = new base::DataPack;
17 bool success = resources_pak->Load(resources_pak_path);
18 if (!success) {
19 delete resources_pak;
20 resources_pak = NULL;
21 }
22 return resources_pak;
23 }
24
25 } // namespace
26
27 ResourceBundle::~ResourceBundle() {
28 FreeImages();
29 #if defined(OS_LINUX)
30 FreeGdkPixBufs();
31 #endif
32 delete locale_resources_data_;
33 locale_resources_data_ = NULL;
34 delete resources_data_;
35 resources_data_ = NULL;
36 }
37
38 // static
39 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes(
40 DataHandle module, int resource_id) {
41 DCHECK(module);
42 return module->GetStaticMemory(resource_id);
43 }
44
45 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
46 DCHECK(resources_data_);
47 base::StringPiece data;
48 if (!resources_data_->GetStringPiece(resource_id, &data)) {
49 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
50 return base::StringPiece();
51 }
52 }
53 return data;
54 }
55
56 string16 ResourceBundle::GetLocalizedString(int message_id) {
57 // If for some reason we were unable to load a resource pak, return an empty
58 // string (better than crashing).
59 if (!locale_resources_data_) {
60 LOG(WARNING) << "locale resources are not loaded";
61 return string16();
62 }
63
64 base::StringPiece data;
65 if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
66 // Fall back on the main data pack (shouldn't be any strings here except in
67 // unittests).
68 data = GetRawDataResource(message_id);
69 if (data.empty()) {
70 NOTREACHED() << "unable to find resource: " << message_id;
71 return string16();
72 }
73 }
74
75 // Data pack encodes strings as UTF16.
76 DCHECK_EQ(data.length() % 2, 0U);
77 string16 msg(reinterpret_cast<const char16*>(data.data()),
78 data.length() / 2);
79 return msg;
80 }
81
82 void ResourceBundle::LoadResources(const std::wstring& pref_locale) {
83 DCHECK(!resources_data_) << "chrome.pak already loaded";
84 FilePath resources_file_path = GetResourcesFilePath();
85 DCHECK(!resources_file_path.empty()) << "chrome.pak not found";
86 resources_data_ = LoadResourcesDataPak(resources_file_path);
87 DCHECK(resources_data_) << "failed to load chrome.pak";
88
89 DCHECK(!locale_resources_data_) << "locale.pak already loaded";
90 FilePath locale_file_path = GetLocaleFilePath(pref_locale);
91 if (locale_file_path.empty()) {
92 // It's possible that there is no locale.pak.
93 NOTREACHED();
94 return;
95 }
96 locale_resources_data_ = LoadResourcesDataPak(locale_file_path);
97 DCHECK(locale_resources_data_) << "failed to load locale.pak";
98 }
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