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

Side by Side Diff: chrome/common/extensions/extension_message_bundle.h

Issue 10787002: Moved ExtensionMessage* into extensions namespace (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Latest master for cq Created 8 years, 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/linked_ptr.h"
13
14 namespace base {
15 class DictionaryValue;
16 }
17
18 // Contains localized extension messages for one locale. Any messages that the
19 // locale does not provide are pulled from the default locale.
20 class ExtensionMessageBundle {
21 public:
22 typedef std::map<std::string, std::string> SubstitutionMap;
23 typedef std::vector<linked_ptr<base::DictionaryValue> > CatalogVector;
24
25 // JSON keys of interest for messages file.
26 static const char* kContentKey;
27 static const char* kMessageKey;
28 static const char* kPlaceholdersKey;
29
30 // Begin/end markers for placeholders and messages
31 static const char* kPlaceholderBegin;
32 static const char* kPlaceholderEnd;
33 static const char* kMessageBegin;
34 static const char* kMessageEnd;
35
36 // Reserved message names in the dictionary.
37 // Update i18n documentation when adding new reserved value.
38 static const char* kUILocaleKey;
39 // See http://code.google.com/apis/gadgets/docs/i18n.html#BIDI for
40 // description.
41 // TODO(cira): point to chrome docs once they are out.
42 static const char* kBidiDirectionKey;
43 static const char* kBidiReversedDirectionKey;
44 static const char* kBidiStartEdgeKey;
45 static const char* kBidiEndEdgeKey;
46 // Extension id gets added in the
47 // browser/renderer_host/resource_message_filter.cc to enable message
48 // replacement for non-localized extensions.
49 static const char* kExtensionIdKey;
50
51 // Values for some of the reserved messages.
52 static const char* kBidiLeftEdgeValue;
53 static const char* kBidiRightEdgeValue;
54
55 // Creates ExtensionMessageBundle or returns NULL if there was an error.
56 // Expects locale_catalogs to be sorted from more specific to less specific,
57 // with default catalog at the end.
58 static ExtensionMessageBundle* Create(const CatalogVector& locale_catalogs,
59 std::string* error);
60
61 // Get message from the catalog with given key.
62 // Returned message has all of the internal placeholders resolved to their
63 // value (content).
64 // Returns empty string if it can't find a message.
65 // We don't use simple GetMessage name, since there is a global
66 // #define GetMessage GetMessageW override in Chrome code.
67 std::string GetL10nMessage(const std::string& name) const;
68
69 // Get message from the given catalog with given key.
70 static std::string GetL10nMessage(const std::string& name,
71 const SubstitutionMap& dictionary);
72
73 // Number of messages in the catalog.
74 // Used for unittesting only.
75 size_t size() const { return dictionary_.size(); }
76
77 // Replaces all __MSG_message__ with values from the catalog.
78 // Returns false if there is a message in text that's not defined in the
79 // dictionary.
80 bool ReplaceMessages(std::string* text, std::string* error) const;
81 // Static version that accepts dictionary.
82 static bool ReplaceMessagesWithExternalDictionary(
83 const SubstitutionMap& dictionary, std::string* text, std::string* error);
84
85 // Replaces each occurance of variable placeholder with its value.
86 // I.e. replaces __MSG_name__ with value from the catalog with the key "name".
87 // Returns false if for a valid message/placeholder name there is no matching
88 // replacement.
89 // Public for easier unittesting.
90 static bool ReplaceVariables(const SubstitutionMap& variables,
91 const std::string& var_begin,
92 const std::string& var_end,
93 std::string* message,
94 std::string* error);
95
96 // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name.
97 // Returns false if the input is empty or if it has illegal characters.
98 static bool IsValidName(const std::string& name);
99
100 // Getter for dictionary_.
101 const SubstitutionMap* dictionary() const { return &dictionary_; }
102
103 ~ExtensionMessageBundle();
104
105 private:
106 // Testing friend.
107 friend class ExtensionMessageBundleTest;
108
109 // Use Create to create ExtensionMessageBundle instance.
110 ExtensionMessageBundle();
111
112 // Initializes the instance from the contents of vector of catalogs.
113 // If the key is not present in more specific catalog we fall back to next one
114 // (less specific).
115 // Returns false on error.
116 bool Init(const CatalogVector& locale_catalogs, std::string* error);
117
118 // Appends locale specific reserved messages to the dictionary.
119 // Returns false if there was a conflict with user defined messages.
120 bool AppendReservedMessagesForLocale(const std::string& application_locale,
121 std::string* error);
122
123 // Helper methods that navigate JSON tree and return simplified message.
124 // They replace all $PLACEHOLDERS$ with their value, and return just key/value
125 // of the message.
126 bool GetMessageValue(const std::string& key,
127 const base::DictionaryValue& catalog,
128 std::string* value,
129 std::string* error) const;
130
131 // Get all placeholders for a given message from JSON subtree.
132 bool GetPlaceholders(const base::DictionaryValue& name_tree,
133 const std::string& name_key,
134 SubstitutionMap* placeholders,
135 std::string* error) const;
136
137 // For a given message, replaces all placeholders with their actual value.
138 // Returns false if replacement failed (see ReplaceVariables).
139 bool ReplacePlaceholders(const SubstitutionMap& placeholders,
140 std::string* message,
141 std::string* error) const;
142
143 // Holds all messages for application locale.
144 SubstitutionMap dictionary_;
145 };
146
147 ///////////////////////////////////////////////////////////////////////////////
148 //
149 // Renderer helper typedefs and functions.
150 //
151 ///////////////////////////////////////////////////////////////////////////////
152
153 // A map of message name to message.
154 typedef std::map<std::string, std::string> L10nMessagesMap;
155
156 // A map of extension ID to l10n message map.
157 typedef std::map<std::string, L10nMessagesMap > ExtensionToL10nMessagesMap;
158
159 // Returns the extension_id to messages map.
160 ExtensionToL10nMessagesMap* GetExtensionToL10nMessagesMap();
161
162 // Returns message map that matches given extension_id, or NULL.
163 L10nMessagesMap* GetL10nMessagesMap(const std::string& extension_id);
164
165 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_localization_peer_unittest.cc ('k') | chrome/common/extensions/extension_message_bundle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698