blob: d23a5b1da0986e82e09bd0cd52c68204084066a7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/resource/resource_bundle.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/string16.h"
#include "base/string_piece.h"
#include "base/synchronization/lock.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/data_pack.h"
#include "ui/base/ui_base_switches.h"
#include "ui/gfx/font.h"
namespace ui {
namespace {
DataPack* LoadResourcesDataPak(FilePath resources_pak_path) {
DataPack* resources_pak = new DataPack;
bool success = resources_pak->Load(resources_pak_path);
if (!success) {
delete resources_pak;
resources_pak = NULL;
}
return resources_pak;
}
} // namespace
ResourceBundle::~ResourceBundle() {
FreeImages();
UnloadLocaleResources();
STLDeleteContainerPointers(data_packs_.begin(),
data_packs_.end());
delete resources_data_;
resources_data_ = NULL;
}
void ResourceBundle::UnloadLocaleResources() {
delete locale_resources_data_;
locale_resources_data_ = NULL;
}
// static
RefCountedStaticMemory* ResourceBundle::LoadResourceBytes(
DataHandle module, int resource_id) {
DCHECK(module);
return module->GetStaticMemory(resource_id);
}
base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
DCHECK(resources_data_);
base::StringPiece data;
if (!resources_data_->GetStringPiece(resource_id, &data)) {
if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
for (size_t i = 0; i < data_packs_.size(); ++i) {
if (data_packs_[i]->GetStringPiece(resource_id, &data))
return data;
}
return base::StringPiece();
}
}
return data;
}
string16 ResourceBundle::GetLocalizedString(int message_id) {
// If for some reason we were unable to load a resource pak, return an empty
// string (better than crashing).
if (!locale_resources_data_) {
LOG(WARNING) << "locale resources are not loaded";
return string16();
}
base::StringPiece data;
if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
// Fall back on the main data pack (shouldn't be any strings here except in
// unittests).
data = GetRawDataResource(message_id);
if (data.empty()) {
NOTREACHED() << "unable to find resource: " << message_id;
return string16();
}
}
// Data pack encodes strings as UTF16.
DCHECK_EQ(data.length() % 2, 0U);
string16 msg(reinterpret_cast<const char16*>(data.data()),
data.length() / 2);
return msg;
}
void ResourceBundle::LoadCommonResources() {
DCHECK(!resources_data_) << "chrome.pak already loaded";
FilePath resources_file_path = GetResourcesFilePath();
CHECK(!resources_file_path.empty()) << "chrome.pak not found";
resources_data_ = LoadResourcesDataPak(resources_file_path);
CHECK(resources_data_) << "failed to load chrome.pak";
FilePath large_icon_resources_file_path = GetLargeIconResourcesFilePath();
if (!large_icon_resources_file_path.empty()) {
large_icon_resources_data_ =
LoadResourcesDataPak(large_icon_resources_file_path);
CHECK(large_icon_resources_data_) <<
"failed to load theme_resources_large.pak";
}
}
std::string ResourceBundle::LoadLocaleResources(
const std::string& pref_locale) {
DCHECK(!locale_resources_data_) << "locale.pak already loaded";
std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
FilePath locale_file_path;
CommandLine *command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kLocalePak)) {
locale_file_path =
command_line->GetSwitchValuePath(switches::kLocalePak);
} else {
locale_file_path = GetLocaleFilePath(app_locale);
}
if (locale_file_path.empty()) {
// It's possible that there is no locale.pak.
NOTREACHED();
return std::string();
}
locale_resources_data_ = LoadResourcesDataPak(locale_file_path);
CHECK(locale_resources_data_) << "failed to load locale.pak";
return app_locale;
}
void ResourceBundle::LoadTestResources(const FilePath& path) {
DCHECK(!resources_data_) << "resource already loaded";
// Use the given resource pak for both common and localized resources.
resources_data_ = LoadResourcesDataPak(path);
locale_resources_data_ = LoadResourcesDataPak(path);
}
} // namespace ui
|