OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/extensions/extension_message_bundle.h" | 5 #include "chrome/common/extensions/extension_message_bundle.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
13 #include "base/linked_ptr.h" | 13 #include "base/linked_ptr.h" |
14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
15 #include "base/singleton.h" | 15 #include "base/singleton.h" |
16 #include "base/stl_util-inl.h" | 16 #include "base/stl_util-inl.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
20 #include "chrome/common/extensions/extension_error_utils.h" | 20 #include "chrome/common/extensions/extension_error_utils.h" |
21 #include "chrome/common/extensions/extension_l10n_util.h" | 21 #include "chrome/common/extensions/extension_l10n_util.h" |
22 | 22 |
23 namespace errors = extension_manifest_errors; | 23 namespace errors = extension_manifest_errors; |
24 | 24 |
25 const wchar_t* ExtensionMessageBundle::kContentKey = L"content"; | 25 const char* ExtensionMessageBundle::kContentKey = "content"; |
26 const wchar_t* ExtensionMessageBundle::kMessageKey = L"message"; | 26 const char* ExtensionMessageBundle::kMessageKey = "message"; |
27 const wchar_t* ExtensionMessageBundle::kPlaceholdersKey = L"placeholders"; | 27 const char* ExtensionMessageBundle::kPlaceholdersKey = "placeholders"; |
28 | 28 |
29 const char* ExtensionMessageBundle::kPlaceholderBegin = "$"; | 29 const char* ExtensionMessageBundle::kPlaceholderBegin = "$"; |
30 const char* ExtensionMessageBundle::kPlaceholderEnd = "$"; | 30 const char* ExtensionMessageBundle::kPlaceholderEnd = "$"; |
31 const char* ExtensionMessageBundle::kMessageBegin = "__MSG_"; | 31 const char* ExtensionMessageBundle::kMessageBegin = "__MSG_"; |
32 const char* ExtensionMessageBundle::kMessageEnd = "__"; | 32 const char* ExtensionMessageBundle::kMessageEnd = "__"; |
33 | 33 |
34 // Reserved messages names. | 34 // Reserved messages names. |
35 const char* ExtensionMessageBundle::kUILocaleKey = "@@ui_locale"; | 35 const char* ExtensionMessageBundle::kUILocaleKey = "@@ui_locale"; |
36 const char* ExtensionMessageBundle::kBidiDirectionKey = "@@bidi_dir"; | 36 const char* ExtensionMessageBundle::kBidiDirectionKey = "@@bidi_dir"; |
37 const char* ExtensionMessageBundle::kBidiReversedDirectionKey = | 37 const char* ExtensionMessageBundle::kBidiReversedDirectionKey = |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 std::string* value, | 131 std::string* value, |
132 std::string* error) const { | 132 std::string* error) const { |
133 // Get the top level tree for given key (name part). | 133 // Get the top level tree for given key (name part). |
134 DictionaryValue* name_tree; | 134 DictionaryValue* name_tree; |
135 if (!catalog.GetDictionaryWithoutPathExpansion(key, &name_tree)) { | 135 if (!catalog.GetDictionaryWithoutPathExpansion(key, &name_tree)) { |
136 *error = StringPrintf("Not a valid tree for key %s.", key.c_str()); | 136 *error = StringPrintf("Not a valid tree for key %s.", key.c_str()); |
137 return false; | 137 return false; |
138 } | 138 } |
139 // Extract message from it. | 139 // Extract message from it. |
140 if (!name_tree->GetString(kMessageKey, value)) { | 140 if (!name_tree->GetString(kMessageKey, value)) { |
141 *error = StringPrintf("There is no \"%s\" element for key %s.", | 141 *error = StringPrintf("There is no \"%s\" element for key %s.", kMessageKey, |
142 WideToUTF8(kMessageKey).c_str(), | |
143 key.c_str()); | 142 key.c_str()); |
144 return false; | 143 return false; |
145 } | 144 } |
146 | 145 |
147 SubstitutionMap placeholders; | 146 SubstitutionMap placeholders; |
148 if (!GetPlaceholders(*name_tree, key, &placeholders, error)) | 147 if (!GetPlaceholders(*name_tree, key, &placeholders, error)) |
149 return false; | 148 return false; |
150 | 149 |
151 if (!ReplacePlaceholders(placeholders, value, error)) | 150 if (!ReplacePlaceholders(placeholders, value, error)) |
152 return false; | 151 return false; |
153 | 152 |
154 return true; | 153 return true; |
155 } | 154 } |
156 | 155 |
157 ExtensionMessageBundle::ExtensionMessageBundle() { | 156 ExtensionMessageBundle::ExtensionMessageBundle() { |
158 } | 157 } |
159 | 158 |
160 bool ExtensionMessageBundle::GetPlaceholders(const DictionaryValue& name_tree, | 159 bool ExtensionMessageBundle::GetPlaceholders(const DictionaryValue& name_tree, |
161 const std::string& name_key, | 160 const std::string& name_key, |
162 SubstitutionMap* placeholders, | 161 SubstitutionMap* placeholders, |
163 std::string* error) const { | 162 std::string* error) const { |
164 if (!name_tree.HasKey(kPlaceholdersKey)) | 163 if (!name_tree.HasKey(kPlaceholdersKey)) |
165 return true; | 164 return true; |
166 | 165 |
167 DictionaryValue* placeholders_tree; | 166 DictionaryValue* placeholders_tree; |
168 if (!name_tree.GetDictionary(kPlaceholdersKey, &placeholders_tree)) { | 167 if (!name_tree.GetDictionary(kPlaceholdersKey, &placeholders_tree)) { |
169 *error = StringPrintf("Not a valid \"%s\" element for key %s.", | 168 *error = StringPrintf("Not a valid \"%s\" element for key %s.", |
170 WideToUTF8(kPlaceholdersKey).c_str(), | 169 kPlaceholdersKey, name_key.c_str()); |
171 name_key.c_str()); | |
172 return false; | 170 return false; |
173 } | 171 } |
174 | 172 |
175 for (DictionaryValue::key_iterator key_it = placeholders_tree->begin_keys(); | 173 for (DictionaryValue::key_iterator key_it = placeholders_tree->begin_keys(); |
176 key_it != placeholders_tree->end_keys(); ++key_it) { | 174 key_it != placeholders_tree->end_keys(); ++key_it) { |
177 DictionaryValue* placeholder; | 175 DictionaryValue* placeholder; |
178 const std::string& content_key(*key_it); | 176 const std::string& content_key(*key_it); |
179 if (!IsValidName(content_key)) | 177 if (!IsValidName(content_key)) |
180 return BadKeyMessage(content_key, error); | 178 return BadKeyMessage(content_key, error); |
181 if (!placeholders_tree->GetDictionaryWithoutPathExpansion(content_key, | 179 if (!placeholders_tree->GetDictionaryWithoutPathExpansion(content_key, |
182 &placeholder)) { | 180 &placeholder)) { |
183 *error = StringPrintf("Invalid placeholder %s for key %s", | 181 *error = StringPrintf("Invalid placeholder %s for key %s", |
184 content_key.c_str(), | 182 content_key.c_str(), |
185 name_key.c_str()); | 183 name_key.c_str()); |
186 return false; | 184 return false; |
187 } | 185 } |
188 std::string content; | 186 std::string content; |
189 if (!placeholder->GetString(kContentKey, &content)) { | 187 if (!placeholder->GetString(kContentKey, &content)) { |
190 *error = StringPrintf("Invalid \"%s\" element for key %s.", | 188 *error = StringPrintf("Invalid \"%s\" element for key %s.", |
191 WideToUTF8(kContentKey).c_str(), | 189 kContentKey, name_key.c_str()); |
192 name_key.c_str()); | |
193 return false; | 190 return false; |
194 } | 191 } |
195 (*placeholders)[StringToLowerASCII(content_key)] = content; | 192 (*placeholders)[StringToLowerASCII(content_key)] = content; |
196 } | 193 } |
197 | 194 |
198 return true; | 195 return true; |
199 } | 196 } |
200 | 197 |
201 bool ExtensionMessageBundle::ReplacePlaceholders( | 198 bool ExtensionMessageBundle::ReplacePlaceholders( |
202 const SubstitutionMap& placeholders, | 199 const SubstitutionMap& placeholders, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 } | 300 } |
304 | 301 |
305 L10nMessagesMap* GetL10nMessagesMap(const std::string extension_id) { | 302 L10nMessagesMap* GetL10nMessagesMap(const std::string extension_id) { |
306 ExtensionToL10nMessagesMap::iterator it = | 303 ExtensionToL10nMessagesMap::iterator it = |
307 Singleton<ExtensionToMessagesMap>()->messages_map.find(extension_id); | 304 Singleton<ExtensionToMessagesMap>()->messages_map.find(extension_id); |
308 if (it != Singleton<ExtensionToMessagesMap>()->messages_map.end()) | 305 if (it != Singleton<ExtensionToMessagesMap>()->messages_map.end()) |
309 return &(it->second); | 306 return &(it->second); |
310 | 307 |
311 return NULL; | 308 return NULL; |
312 } | 309 } |
OLD | NEW |