blob: 6070b951aede19bb310c63ad6436256138a9050e [file] [log] [blame]
[email protected]fa5fed32013-09-05 21:56:221// 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]2fb9bd22013-09-07 00:08:085#ifndef EXTENSIONS_BROWSER_FILE_HIGHLIGHTER_H_
6#define EXTENSIONS_BROWSER_FILE_HIGHLIGHTER_H_
[email protected]fa5fed32013-09-05 21:56:227
avic9cec102015-12-23 00:39:268#include <stddef.h>
9
[email protected]fa5fed32013-09-05 21:56:2210#include <string>
11
avic9cec102015-12-23 00:39:2612#include "base/macros.h"
[email protected]fa5fed32013-09-05 21:56:2213
[email protected]2fb9bd22013-09-07 00:08:0814namespace base {
15class DictionaryValue;
16}
17
[email protected]fa5fed32013-09-05 21:56:2218namespace extensions {
19
[email protected]2fb9bd22013-09-07 00:08:0820// 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]fa5fed32013-09-05 21:56:2225// object.
[email protected]2fb9bd22013-09-07 00:08:0826class FileHighlighter {
[email protected]fa5fed32013-09-05 21:56:2227 public:
[email protected]2fb9bd22013-09-07 00:08:0828 virtual ~FileHighlighter();
[email protected]fa5fed32013-09-05 21:56:2229
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]2fb9bd22013-09-07 00:08:0841 // 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]883346042014-06-03 12:04:3346 explicit FileHighlighter(const std::string& contents);
[email protected]2fb9bd22013-09-07 00:08:0847
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.
73class ManifestHighlighter : public FileHighlighter {
74 public:
75 ManifestHighlighter(const std::string& manifest,
76 const std::string& key,
77 const std::string& specific /* optional */);
dcheng9168b2f2014-10-21 12:38:2478 ~ManifestHighlighter() override;
[email protected]2fb9bd22013-09-07 00:08:0879
[email protected]fa5fed32013-09-05 21:56:2280 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]fa5fed32013-09-05 21:56:2294 DISALLOW_COPY_AND_ASSIGN(ManifestHighlighter);
95};
96
[email protected]2fb9bd22013-09-07 00:08:0897// Use the SourceHighlighter to highlight a particular line in a given source
98// file.
99class SourceHighlighter : public FileHighlighter {
100 public:
101 SourceHighlighter(const std::string& source, size_t line_number);
dcheng9168b2f2014-10-21 12:38:24102 ~SourceHighlighter() override;
[email protected]2fb9bd22013-09-07 00:08:08103
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]fa5fed32013-09-05 21:56:22112} // namespace extensions
113
[email protected]2fb9bd22013-09-07 00:08:08114#endif // EXTENSIONS_BROWSER_FILE_HIGHLIGHTER_H_