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

Side by Side Diff: extensions/browser/manifest_highlighter.cc

Issue 22938005: Add ErrorConsole UI for Extension Install Warnings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dc_ec_install_warnings
Patch Set: Yoz's, Round II Created 7 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 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
5 #include <stack>
6
7 #include "extensions/browser/manifest_highlighter.h"
8
9 namespace extensions {
10
11 namespace {
12
13 size_t FindNextQuote(const std::string& string, size_t index) {
14 bool found = false;
15 while (!found && index < string.size()) {
16 if (string[index] == '\\')
17 index += 2; // if we find an escaped character, skip it.
18 else if (string[index] == '"')
19 found = true;
20 else
21 ++index;
22 }
23 return index < string.size() ? index : std::string::npos;
Yoyo Zhou 2013/08/19 21:48:51 Should this test use found?
Devlin 2013/08/20 20:38:22 Done.
24 }
25
26 } // namespace
27
28 ManifestHighlighter::ManifestHighlighter(const std::string& manifest,
29 const std::string& key,
30 const std::string& specific)
31 : manifest_(manifest),
32 start_(manifest_.find('{') + 1),
33 end_(manifest_.rfind('}')) {
34 Parse(key, specific);
35 }
36
37 ManifestHighlighter::~ManifestHighlighter() {
38 }
39
40 std::string ManifestHighlighter::GetBeforeFeature() const {
41 return manifest_.substr(0, start_);
42 }
43
44 std::string ManifestHighlighter::GetFeature() const {
45 return manifest_.substr(start_, end_ - start_);
46 }
47
48 std::string ManifestHighlighter::GetAfterFeature() const {
49 return manifest_.substr(end_);
50 }
51
52 void ManifestHighlighter::Parse(const std::string& key,
53 const std::string& specific) {
54 // First, try to find the bounds of the full key.
55 if (FindBounds(key, true) /* enforce at top level */ ) {
56 // If we succeed, and we have a specific location, find the bounds of the
57 // specific.
58 if (!specific.empty())
59 FindBounds(specific, false /* don't enforce at top level */ );
60
61 // We may have found trailing whitespace. Don't use base::TrimWhitespace,
62 // because we want to keep any whitespace we find - just not highlight it.
63 size_t trim = manifest_.find_last_not_of(" \t\n\r", end_ - 1);
64 if (trim < end_ && trim > start_)
65 end_ = trim + 1;
66 } else {
67 // If we fail, then we set start to end so that the highlighted portion is
68 // empty.
69 start_ = end_;
70 }
71 }
72
73 bool ManifestHighlighter::FindBounds(const std::string& feature,
74 bool enforce_at_top_level) {
75 char c = '\0';
76 while (start_ < end_) {
77 c = manifest_[start_];
78 if (c == '"') {
79 // The feature may be quoted.
80 size_t quote_end = FindNextQuote(manifest_, start_ + 1);
81 if (manifest_.substr(start_ + 1, quote_end - 1 - start_) == feature) {
82 FindBoundsEnd(feature, quote_end + 1);
83 return true;
84 } else {
85 // If it's not the feature, then we can skip the quoted section.
86 start_ = quote_end + 1;
87 }
88 } else if (manifest_.substr(start_, feature.size()) == feature) {
89 FindBoundsEnd(feature, start_ + feature.size() + 1);
90 return true;
91 } else if (enforce_at_top_level && (c == '{' || c == '[')) {
92 // If we don't have to be at the top level, then we can skip any chunks
93 // we find.
94 ChunkIncrement(&start_);
95 } else {
96 CommentSafeIncrement(&start_);
97 }
98 }
99 return false;
100 }
101
102 void ManifestHighlighter::FindBoundsEnd(const std::string& feature,
103 size_t local_start) {
104 char c = '\0';
105 while (local_start < end_) {
106 c = manifest_[local_start];
107 // We're done when we find a terminating character (i.e., either a comma or
108 // an ending bracket.
109 if (c == ',' || c == '}' || c == ']') {
110 end_ = local_start;
111 return;
112 }
113 // We can skip any chunks we find, since we are looking for the end of the
114 // current feature, and don't want to go any deeper.
115 if (c == '"' || c == '{' || c == '[')
116 ChunkIncrement(&local_start);
117 else
118 CommentSafeIncrement(&local_start);
119 }
120 }
121
122 void ManifestHighlighter::ChunkIncrement(size_t* index) {
123 char c = manifest_[*index];
124 std::stack<char> stack;
125 do {
126 if (c == '"')
127 *index = FindNextQuote(manifest_, *index + 1);
128 else if (c == '[')
129 stack.push(']');
130 else if (c == '{')
131 stack.push('}');
132 else if (!stack.empty() && c == stack.top())
133 stack.pop();
134 CommentSafeIncrement(index);
135 c = manifest_[*index];
136 } while (!stack.empty() && *index < end_);
137 }
138
139 void ManifestHighlighter::CommentSafeIncrement(size_t* index) {
Yoyo Zhou 2013/08/19 21:48:51 nit: seems like this and FindNextQuote are in the
Devlin 2013/08/20 20:38:22 Done.
140 size_t i = *index;
141 if (manifest_[i] == '/' && i + 1 < manifest_.size()) {
142 // Eat a single-line comment.
143 if (manifest_[i + 1] == '/') {
144 i += 2; // Eat the '//'.
145 while (i < manifest_.size() &&
146 manifest_[i] != '\n' && manifest_[i] != '\r') {
147 ++i;
148 }
149 } else if (manifest_[i + 1] == '*') { // Eat a multi-line comment.
150 i += 3; // Advance to the first possible comment end.
151 while (i < manifest_.size() &&
152 !(manifest_[i - 1] == '*' && manifest_[i] == '/')) {
153 ++i;
154 }
155 }
156 }
157 *index = i + 1;
158 }
159
160 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/manifest_highlighter.h ('k') | extensions/browser/manifest_highlighter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698