Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_HANDLER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_HANDLER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/hash_tables.h" | |
| 11 #include "base/values.h" | |
| 12 | |
| 13 // Parses both default and application dictionaries (passed in Init method), | |
| 14 // and creates unified dictionary for lookup. | |
| 15 // It simplifies message handling by substituting placeholders into messages. | |
| 16 // It provides ReplaceVariablesInString method that can be used for both message | |
| 17 // substitution in HTML files and for placeholder replacement within messages. | |
| 18 class ExtensionMessageHandler { | |
|
Aaron Boodman
2009/08/31 21:42:55
This class seems mis-named. It isn't really "handl
| |
| 19 public: | |
| 20 typedef base::hash_map<std::string, std::string> SSMap; | |
|
Aaron Boodman
2009/08/31 21:42:55
Avoid abbreviations. Can you come up with a descri
| |
| 21 | |
| 22 // JSON keys of interest for messages file. | |
| 23 static const wchar_t* kContentKey; | |
| 24 static const wchar_t* kMessageKey; | |
| 25 static const wchar_t* kPlaceholdersKey; | |
| 26 | |
| 27 // Begin/end markers for placeholders and messages | |
| 28 static const char* kPlaceholderBegin; | |
| 29 static const char* kPlaceholderEnd; | |
| 30 static const char* kMessageBegin; | |
| 31 static const char* kMessageEnd; | |
| 32 | |
| 33 ExtensionMessageHandler(); | |
| 34 ~ExtensionMessageHandler(); | |
| 35 | |
| 36 // Merges application and default dictionaries into one hash_map for later | |
| 37 // lookup. | |
| 38 // It takes key-values from |application_dictionary| and stores them in | |
| 39 // |dictionary_|. | |
| 40 // It then stores all unique messages in |default_dictionary| into | |
| 41 // |dictionary_|. | |
| 42 bool Init(const DictionaryValue* default_dictionary, | |
|
Aaron Boodman
2009/08/31 21:42:55
In cases where initialization can fail, we tend to
| |
| 43 const DictionaryValue* application_dictionary, | |
| 44 std::string* error); | |
| 45 | |
| 46 // Get message with key |name|. | |
| 47 // Returns false if message is not in the dictionary. | |
| 48 bool GetMessage(const std::string& name, std::string* message) const; | |
|
Aaron Boodman
2009/08/31 21:42:55
Nit: Can we just return std::string? It could be e
| |
| 49 | |
| 50 // Dictionary size. | |
| 51 size_t GetDictionarySize() const { | |
|
Aaron Boodman
2009/08/31 21:42:55
Is this just the number of messages? If so, maybe
| |
| 52 return dictionary_.size(); | |
| 53 } | |
| 54 | |
| 55 // For a given |text| replaces all __MSG_message__ with values from | |
| 56 // dictionary_. | |
| 57 bool ReplaceMessagesInString(std::string* text) const; | |
|
Aaron Boodman
2009/08/31 21:42:55
Nit: "InString" is unnecessary as that information
| |
| 58 | |
| 59 // Given list of variable names/values |variables|, and markers for their | |
| 60 // begin/end |var_begin|, |var_end|, replaces each occurance of variable | |
| 61 // placeholder with its value. | |
| 62 // I.e. replaces __MSG_name__ with value from |variables| that has name as a | |
| 63 // key. | |
| 64 // Returns false if for a valid message/placeholder name there is no matching | |
| 65 // replacement in |variables|. Sets |message| to that variable name. | |
| 66 // Public for easier unittesting. | |
| 67 bool ReplaceVariablesInString(const SSMap& variables, | |
|
Aaron Boodman
2009/08/31 21:42:55
Looks like this should be static as it refers to n
| |
| 68 const std::string& var_begin, | |
|
Aaron Boodman
2009/08/31 21:42:55
Nit: maybe var_begin -> var_begin_delimiter? I tho
| |
| 69 const std::string& var_end, | |
| 70 std::string* message) const; | |
| 71 | |
| 72 // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name. | |
| 73 // Returns false if |name| is empty or if it has illegal characters. | |
| 74 // Public for easier unittesting. | |
| 75 bool IsValidName(const std::string& name) const; | |
|
Aaron Boodman
2009/08/31 21:42:55
Looks like this should be static.
| |
| 76 | |
| 77 private: | |
| 78 // Helper methods that navigate JSON tree and return simplified message. | |
| 79 // They replace all $PLACEHOLDERS$ with their value, and return just key/value | |
| 80 // of the message. | |
| 81 bool GetMessageValue(const std::wstring& wkey, | |
|
Aaron Boodman
2009/08/31 21:42:55
It is really odd to have a method take two keys in
| |
| 82 const std::string& key, | |
| 83 const DictionaryValue* main_catalog, | |
|
Aaron Boodman
2009/08/31 21:42:55
Chromium mainly uses const references for input pa
| |
| 84 std::string* value, | |
| 85 std::string* error) const; | |
| 86 | |
| 87 // Get all placeholders for a given message from JSON subtree. | |
| 88 bool GetPlaceholders(const DictionaryValue* name_tree, | |
| 89 const std::string name_key, | |
| 90 SSMap* placeholders, | |
| 91 std::string* error) const; | |
| 92 | |
| 93 // For a given message, replaces all placeholders with their actual value. | |
| 94 // Returns false if replacement failed (see ReplaceVariablesInString). | |
| 95 bool ReplacePlaceholdersInMessage(const SSMap& placeholders, | |
| 96 std::string* message) const; | |
| 97 | |
| 98 // Holds all messages for application locale. | |
| 99 SSMap dictionary_; | |
| 100 }; | |
| 101 | |
| 102 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_HANDLER_H_ | |
| OLD | NEW |