Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef EXTENSIONS_BROWSER_MANIFEST_HIGHLIGHTER_H_ | 5 #ifndef EXTENSIONS_BROWSER_FILE_HIGHLIGHTER_H_ |
| 6 #define EXTENSIONS_BROWSER_MANIFEST_HIGHLIGHTER_H_ | 6 #define EXTENSIONS_BROWSER_FILE_HIGHLIGHTER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 | 11 |
| 12 namespace base { | |
| 13 class DictionaryValue; | |
| 14 } | |
| 15 | |
| 12 namespace extensions { | 16 namespace extensions { |
| 13 | 17 |
| 18 // The FileHighlighter class is used in order to isolate and highlight a portion | |
| 19 // of a given file (in string form). The Highlighter will split the source into | |
| 20 // three portions: the portion before the highlighted feature, the highlighted | |
| 21 // feature, and the portion following the highlighted feature. | |
| 22 // The file will be parsed for highlighting upon construction of the Highlighter | |
| 23 // object. | |
| 24 class FileHighlighter { | |
| 25 public: | |
| 26 virtual ~FileHighlighter(); | |
| 27 | |
| 28 // Get the portion of the manifest which should not be highlighted and is | |
| 29 // before the feature. | |
| 30 std::string GetBeforeFeature() const; | |
| 31 | |
| 32 // Get the feature portion of the manifest, which should be highlighted. | |
| 33 std::string GetFeature() const; | |
| 34 | |
| 35 // Get the portion of the manifest which should not be highlighted and is | |
| 36 // after the feature. | |
| 37 std::string GetAfterFeature() const; | |
| 38 | |
| 39 // Populate a DictionaryValue with the highlighted portions (in UTF16) of the | |
| 40 // source file. | |
| 41 void HighlightDictionary(base::DictionaryValue* dict) const; | |
|
Yoyo Zhou
2013/09/04 23:43:39
This name could be better, like SetHighlightedRegi
Devlin
2013/09/05 17:53:55
Done.
| |
| 42 | |
| 43 // The keys used in highlighting a dictionary. | |
|
Yoyo Zhou
2013/09/04 23:43:39
These needn't be declared in the header file if th
Devlin
2013/09/05 17:53:55
Done.
| |
| 44 static const char kBeforeHighlightKey[]; | |
| 45 static const char kHighlightKey[]; | |
| 46 static const char kAfterHighlightKey[]; | |
| 47 | |
| 48 protected: | |
| 49 FileHighlighter(const std::string& contents); | |
| 50 | |
| 51 // The contents of the file we are parsing. | |
| 52 std::string contents_; | |
| 53 | |
| 54 // The start of the feature. | |
| 55 size_t start_; | |
| 56 | |
| 57 // The end of the feature. | |
| 58 size_t end_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(FileHighlighter); | |
| 61 }; | |
| 62 | |
| 14 // Use the ManifestHighlighter class to find the bounds of a feature in the | 63 // Use the ManifestHighlighter class to find the bounds of a feature in the |
| 15 // manifest. The manifest is parsed for the feature upon construction of the | 64 // manifest. |
| 16 // object. | |
| 17 // A feature can be at any level in the hierarchy. The "start" of a feature is | 65 // A feature can be at any level in the hierarchy. The "start" of a feature is |
| 18 // the first character of the feature name, or the beginning quote of the name, | 66 // the first character of the feature name, or the beginning quote of the name, |
| 19 // if present. The "end" of a feature is wherever the next item at the same | 67 // if present. The "end" of a feature is wherever the next item at the same |
| 20 // level starts. | 68 // level starts. |
| 21 // For instance, the bounds for the 'permissions' feature at the top level could | 69 // For instance, the bounds for the 'permissions' feature at the top level could |
| 22 // be '"permissions": { "tabs", "history", "downloads" }', but the feature for | 70 // be '"permissions": { "tabs", "history", "downloads" }', but the feature for |
| 23 // 'tabs' within 'permissions' would just be '"tabs"'. | 71 // 'tabs' within 'permissions' would just be '"tabs"'. |
| 24 // We can't use the JSONParser to do this, because we want to display the actual | 72 // We can't use the JSONParser to do this, because we want to display the actual |
| 25 // manifest, and once we parse it into Values, we lose any formatting the user | 73 // manifest, and once we parse it into Values, we lose any formatting the user |
| 26 // may have had. | 74 // may have had. |
| 27 // If a feature cannot be found, the feature will have zero-length. | 75 // If a feature cannot be found, the feature will have zero-length. |
| 28 class ManifestHighlighter { | 76 class ManifestHighlighter : public FileHighlighter { |
| 29 public: | 77 public: |
| 30 ManifestHighlighter(const std::string& manifest, | 78 ManifestHighlighter(const std::string& manifest, |
| 31 const std::string& key, | 79 const std::string& key, |
| 32 const std::string& specific /* optional */); | 80 const std::string& specific /* optional */); |
| 33 ~ManifestHighlighter(); | 81 virtual ~ManifestHighlighter(); |
| 34 | |
| 35 // Get the portion of the manifest which should not be highlighted and is | |
| 36 // before the feature. | |
| 37 std::string GetBeforeFeature() const; | |
| 38 | |
| 39 // Get the feature portion of the manifest, which should be highlighted. | |
| 40 std::string GetFeature() const; | |
| 41 | |
| 42 // Get the portion of the manifest which should not be highlighted and is | |
| 43 // after the feature. | |
| 44 std::string GetAfterFeature() const; | |
| 45 | 82 |
| 46 private: | 83 private: |
| 47 // Called from the constructor; determine the start and end bounds of a | 84 // Called from the constructor; determine the start and end bounds of a |
| 48 // feature, using both the key and specific information. | 85 // feature, using both the key and specific information. |
| 49 void Parse(const std::string& key, const std::string& specific); | 86 void Parse(const std::string& key, const std::string& specific); |
| 50 | 87 |
| 51 // Find the bounds of any feature, either a full key or a specific item within | 88 // Find the bounds of any feature, either a full key or a specific item within |
| 52 // the key. |enforce_at_top_level| means that the feature we find must be at | 89 // the key. |enforce_at_top_level| means that the feature we find must be at |
| 53 // the same level as |start_| (i.e., ignore nested elements). | 90 // the same level as |start_| (i.e., ignore nested elements). |
| 54 // Returns true on success. | 91 // Returns true on success. |
| 55 bool FindBounds(const std::string& feature, bool enforce_at_top_level); | 92 bool FindBounds(const std::string& feature, bool enforce_at_top_level); |
| 56 | 93 |
| 57 // Finds the end of the feature. | 94 // Finds the end of the feature. |
| 58 void FindBoundsEnd(const std::string& feature, size_t local_start); | 95 void FindBoundsEnd(const std::string& feature, size_t local_start); |
| 59 | 96 |
| 60 // The manifest we are parsing. | |
| 61 std::string manifest_; | |
| 62 | |
| 63 // The start of the feature. | |
| 64 size_t start_; | |
| 65 | |
| 66 // The end of the feature. | |
| 67 size_t end_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(ManifestHighlighter); | 97 DISALLOW_COPY_AND_ASSIGN(ManifestHighlighter); |
| 70 }; | 98 }; |
| 71 | 99 |
| 100 // Use the SourceHighlighter to highlight a particular line in a given source | |
| 101 // file. | |
| 102 class SourceHighlighter : public FileHighlighter { | |
| 103 public: | |
| 104 SourceHighlighter(const std::string& source, size_t line_number); | |
| 105 virtual ~SourceHighlighter(); | |
| 106 | |
| 107 private: | |
| 108 // Called from the constructor; determine the bounds of the line in the source | |
| 109 // file. | |
| 110 void Parse(size_t line_number); | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(SourceHighlighter); | |
| 113 }; | |
| 114 | |
| 72 } // namespace extensions | 115 } // namespace extensions |
| 73 | 116 |
| 74 #endif // EXTENSIONS_BROWSER_MANIFEST_HIGHLIGHTER_H_ | 117 #endif // EXTENSIONS_BROWSER_FILE_HIGHLIGHTER_H_ |
| OLD | NEW |