OLD | NEW |
---|---|
(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 "chrome/browser/ui/webui/extensions/extension_error_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/file_util.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/extensions/extension_service.h" | |
13 #include "chrome/browser/extensions/extension_system.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/extensions/extension.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 #include "content/public/browser/web_ui.h" | |
18 #include "content/public/browser/web_ui_data_source.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "ui/base/l10n/l10n_util.h" | |
21 | |
22 namespace extensions { | |
23 | |
24 namespace { | |
25 | |
26 // Keys for objects passed to and from extension error UI. | |
27 const char kExtensionIdKey[] = "extensionId"; | |
28 const char kPathSuffixKey[] = "pathSuffix"; | |
29 const char kErrorMessageKey[] = "errorMessage"; | |
30 const char kFileTypeKey[] = "fileType"; | |
31 const char kFeatureKey[] = "feature"; | |
32 const char kSpecificKey[] = "specific"; | |
33 const char kManifestFileType[] = "manifest"; | |
34 const char kTitleKey[] = "title"; | |
35 const char kBeforeHighlightKey[] = "beforeHighlight"; | |
36 const char kHighlightKey[] = "highlight"; | |
37 const char kAfterHighlightKey[] = "afterHighlight"; | |
38 | |
39 // Populate a DictionaryValue with the highlighted portions for the callback to | |
40 // ExtensionErrorOverlay, given the components. | |
41 void AddHighlightedToDictionaryForOverlay(base::DictionaryValue* dict, | |
42 const std::string& before_highlight, | |
43 const std::string& highlight, | |
44 const std::string& after_highlight) { | |
45 if (!before_highlight.empty()) | |
46 dict->SetString(kBeforeHighlightKey, base::UTF8ToUTF16(before_highlight)); | |
47 if (!highlight.empty()) | |
48 dict->SetString(kHighlightKey, base::UTF8ToUTF16(highlight)); | |
49 if (!after_highlight.empty()) | |
50 dict->SetString(kAfterHighlightKey, base::UTF8ToUTF16(after_highlight)); | |
51 } | |
52 | |
53 // Find the end of a feature in the manifest string. See also | |
54 // FindFeatureBounds(). | |
55 // |i| points to the start location of the feature, which is defined as the | |
56 // first character after the name of the feature. | |
57 size_t FindFeatureEnd(const std::string& manifest, size_t i) { | |
58 size_t levels = 0; | |
59 bool quoted = false; | |
60 | |
61 // If the feature is a string, we may need to account for the ending quote. | |
62 if (manifest[i] == '"') | |
63 ++i; | |
64 | |
65 char c = manifest[i]; | |
66 | |
67 // We loop until we find the end of a feature. We have found the end of a | |
68 // feature when we are at the initial level, are not quoted, and have found | |
69 // a terminating character (a comma or ending bracket). Also be sure to not | |
70 // run off the end. | |
71 while (!(levels == 0 && !quoted && (c == ',' || c == '}' || c == ']')) && | |
72 i < manifest.size() - 1) { | |
73 if (!quoted && (c == '{' || c == '[')) | |
74 ++levels; | |
75 else if (!quoted && (c == '}' || c == ']')) | |
76 --levels; | |
77 else if (c == '"') | |
78 quoted = !quoted; | |
79 c = manifest[++i]; | |
80 } | |
81 return i; | |
82 } | |
83 | |
84 // Find the bounds of a feature in the manifest (for highlighting purposes), and | |
85 // populate |start| and |end| accordingly. Return true on success and false on | |
86 // failure. | |
87 // A feature can be at any level in the hierarchy. The "start" of a feature is | |
88 // the first character of the feature name, or the beginning quote of the name, | |
89 // if present. The "end" of a feature is wherever the next item at the same | |
90 // level starts. | |
91 // For instance, the bounds for the 'permissions' feature at the top level could | |
92 // be '"permissions": { "tabs", "history", "downloads" }', but the feature for | |
93 // 'tabs' within 'permissions' would just be '"tabs"'. | |
94 // We can't use JSON to do this, because we want to display the actual manifest, | |
95 // and once we parse it into JSON, we lose any formatting the user may have had. | |
96 // | |
97 // |manifest| is the string containing the manifest content. | |
98 // |feature| is the name of the feature we are searching for. | |
99 // |enforce_at_top_level| means that the feature is guaranteed to be at the top | |
100 // level of the manifest (relative to |start|), so we can (and should) skip | |
101 // any nested occurrences. This is useful in case, e.g., we are looking for | |
102 // the "permissions" feature, and the manifest also includes a "permissions" | |
103 // resource file. | |
104 // |start| can point to a location at which we start our search, and will be | |
105 // populated with the start of the feature. | |
106 // |end| can point to a location which we should not pass, and will be populated | |
107 // with the end of the feature. | |
108 bool FindFeatureBounds(const std::string& manifest, | |
109 const std::string& feature, | |
110 bool enforce_at_top_level, | |
111 size_t* start, | |
112 size_t* end) { | |
113 for (size_t i = *start; | |
114 i < *end && i < manifest.size() && i != std::string::npos; ++i) { | |
115 if (manifest.substr(i, feature.size()) == feature) { | |
116 if (manifest[i - 1] == '"') | |
117 *start = i - 1; // include the quote at the beginning of the feature. | |
118 | |
119 *end = FindFeatureEnd(manifest, i + feature.size()); | |
120 return true; | |
121 } else if (manifest[i] == '{' && enforce_at_top_level) { | |
122 i = manifest.find('}', i); | |
123 } else if (manifest[i] == '[' && enforce_at_top_level) { | |
124 i = manifest.find(']', i); | |
125 } | |
126 } | |
127 return false; | |
128 } | |
129 | |
130 } // namespace | |
131 | |
132 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile) | |
133 : profile_(profile) { | |
134 } | |
135 | |
136 ExtensionErrorHandler::~ExtensionErrorHandler() { | |
137 } | |
138 | |
139 void ExtensionErrorHandler::GetLocalizedValues( | |
140 content::WebUIDataSource* source) { | |
141 source->AddString( | |
142 "extensionErrorsManifestErrors", | |
143 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_MANIFEST_ERRORS)); | |
144 source->AddString( | |
145 "extensionErrorsShowMore", | |
146 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE)); | |
147 source->AddString( | |
148 "extensionErrorsShowFewer", | |
149 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_FEWER)); | |
150 source->AddString( | |
151 "extensionErrorViewSource", | |
152 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_SOURCE)); | |
153 } | |
154 | |
155 void ExtensionErrorHandler::RegisterMessages() { | |
156 web_ui()->RegisterMessageCallback( | |
157 "extensionErrorRequestFileSource", | |
158 base::Bind(&ExtensionErrorHandler::HandleRequestFileSource, | |
159 base::Unretained(this))); | |
160 } | |
161 | |
162 void ExtensionErrorHandler::HandleRequestFileSource( | |
163 const base::ListValue* args) { | |
164 // There should only be one argument, a dictionary. Use this instead of a list | |
165 // because it's more descriptive, harder to accidentally break with minor | |
166 // modifications, and supports optional arguments more easily. | |
167 CHECK(args->GetSize() == 1); | |
168 | |
169 const base::DictionaryValue* dict = NULL; | |
170 | |
171 // Four required arguments: extension_id, path_suffix, error_message, and | |
172 // file_type. | |
173 std::string extension_id; | |
174 base::FilePath::StringType path_suffix; | |
175 base::string16 error_message; | |
176 std::string file_type; | |
177 | |
178 if (!args->GetDictionary(0, &dict) || | |
179 !dict->GetString(kExtensionIdKey, &extension_id) || | |
180 !dict->GetString(kPathSuffixKey, &path_suffix) || | |
181 !dict->GetString(kErrorMessageKey, &error_message) || | |
182 !dict->GetString(kFileTypeKey, &file_type)) { | |
183 NOTREACHED(); | |
184 return; | |
185 } | |
186 | |
187 const Extension* extension = | |
188 ExtensionSystem::Get(Profile::FromWebUI(web_ui()))-> | |
189 extension_service()->GetExtensionById(extension_id, | |
190 true /* include disabled */ ); | |
191 base::FilePath path = extension->path().Append(path_suffix); | |
192 | |
193 // Setting the title and the error message is the same for all file types. | |
194 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue); | |
195 results->SetString(kTitleKey, | |
196 base::UTF8ToUTF16(extension->name()) + | |
197 base::ASCIIToUTF16(": ") + | |
198 path.BaseName().LossyDisplayName()); | |
199 results->SetString(kErrorMessageKey, error_message); | |
200 | |
201 base::Closure closure; | |
202 std::string* contents = NULL; | |
203 | |
204 if (file_type == kManifestFileType) { | |
205 std::string feature; | |
206 std::string specific; | |
207 if (!dict->GetString(kFeatureKey, &feature)) { | |
208 NOTREACHED(); | |
209 return; | |
210 } | |
211 // A "specific" location is optional. | |
212 dict->GetString(kSpecificKey, &specific); | |
213 | |
214 contents = new std::string; // Owned by GetManifestFileCallback() | |
215 closure = base::Bind(&ExtensionErrorHandler::GetManifestFileCallback, | |
216 base::Unretained(this), | |
217 base::Owned(results.release()), | |
218 feature, | |
219 specific, | |
220 base::Owned(contents)); | |
221 } else { // currently, only manifest file types supported. | |
222 NOTREACHED(); | |
223 return; | |
224 } | |
225 | |
226 content::BrowserThread::PostBlockingPoolTaskAndReply( | |
227 FROM_HERE, | |
228 base::Bind(base::IgnoreResult(&file_util::ReadFileToString), | |
229 path, | |
230 contents), | |
231 closure); | |
232 } | |
233 | |
234 void ExtensionErrorHandler::GetManifestFileCallback( | |
235 base::DictionaryValue* results, | |
236 const std::string& key, | |
237 const std::string& specific, | |
238 std::string* contents) { | |
239 // We initially start at the opening of the manifest dictionary, and end at | |
240 // it's termination. | |
241 size_t feature_start = contents->find('{'); | |
242 size_t feature_end = contents->rfind('}'); | |
243 | |
244 // Find the bounds of the feature in order to highlight them. | |
245 FindFeatureBounds(*contents, key, true, &feature_start, &feature_end); | |
246 | |
247 // If we were given a specific location in the feature, find it to highlight. | |
248 if (!specific.empty()) | |
249 FindFeatureBounds(*contents, specific, false, &feature_start, &feature_end); | |
250 | |
251 // We may have found trailing whitespace. Don't use base::TrimWhitespace, | |
252 // because we want to keep any whitespace we find - just not highlight it. | |
253 size_t trim = contents->find_last_not_of(" \t\n\r", feature_end - 1); | |
254 if (trim < feature_end && trim > feature_start) | |
255 feature_end = trim + 1; | |
256 | |
257 // Break contents into three sections - unhighlighted before, highlighted, and | |
258 // unhighlighted after. The highlighted portion should be the feature (or | |
259 // specific portion of the feature). | |
260 // If we never found bounds for the feature, then there will be no highlighted | |
Yoyo Zhou
2013/08/16 00:17:26
It looks like we might highlight everything.
Devlin
2013/08/16 18:07:49
D'oh. Fixed.
| |
261 // portion. | |
262 AddHighlightedToDictionaryForOverlay( | |
263 results, | |
264 contents->substr(0, feature_start), // before highlight | |
265 contents->substr(feature_start, | |
266 feature_end - feature_start), // highlight | |
267 contents->substr(feature_end) /* after highlight */ ); | |
268 | |
269 web_ui()->CallJavascriptFunction( | |
270 "ExtensionErrorOverlay.requestFileSourceResponse", *results); | |
271 } | |
272 | |
273 } // namespace extensions | |
OLD | NEW |