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