OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/ui/webui/extensions/extension_error_handler.h" | 5 #include "chrome/browser/ui/webui/extensions/extension_error_ui_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
14 #include "base/values.h" | 13 #include "base/values.h" |
15 #include "chrome/browser/devtools/devtools_window.h" | 14 #include "chrome/browser/devtools/devtools_window.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
24 #include "content/public/browser/web_ui.h" | |
25 #include "content/public/browser/web_ui_data_source.h" | |
26 #include "extensions/browser/extension_error.h" | 23 #include "extensions/browser/extension_error.h" |
27 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
28 #include "extensions/browser/file_highlighter.h" | 25 #include "extensions/browser/file_highlighter.h" |
29 #include "extensions/common/constants.h" | 26 #include "extensions/common/constants.h" |
30 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
31 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
32 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
33 | 30 |
34 namespace extensions { | 31 namespace extensions { |
| 32 namespace error_ui_util { |
35 | 33 |
36 namespace { | 34 namespace { |
37 | 35 |
38 // Keys for objects passed to and from extension error UI. | 36 // Keys for objects passed to and from extension error UI. |
39 const char kPathSuffixKey[] = "pathSuffix"; | 37 const char kPathSuffixKey[] = "pathSuffix"; |
40 const char kTitleKey[] = "title"; | 38 const char kTitleKey[] = "title"; |
41 | 39 |
42 std::string ReadFileToString(const base::FilePath& path) { | 40 std::string ReadFileToString(const base::FilePath& path) { |
43 std::string data; | 41 std::string data; |
44 base::ReadFileToString(path, &data); | 42 base::ReadFileToString(path, &data); |
45 return data; | 43 return data; |
46 } | 44 } |
47 | 45 |
| 46 void GetManifestFileCallback(base::DictionaryValue* results, |
| 47 const std::string& key, |
| 48 const std::string& specific, |
| 49 const JavascriptResponseCallback& response, |
| 50 const std::string& contents) { |
| 51 ManifestHighlighter highlighter(contents, key, specific); |
| 52 highlighter.SetHighlightedRegions(results); |
| 53 response.Run(*results); |
| 54 } |
| 55 |
| 56 void GetSourceFileCallback(base::DictionaryValue* results, |
| 57 int line_number, |
| 58 const JavascriptResponseCallback& response, |
| 59 const std::string& contents) { |
| 60 SourceHighlighter highlighter(contents, line_number); |
| 61 highlighter.SetHighlightedRegions(results); |
| 62 response.Run(*results); |
| 63 } |
| 64 |
48 } // namespace | 65 } // namespace |
49 | 66 |
50 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile) | 67 void GetLocalizedValues(const AddLocalizedStringCallback& add_string) { |
51 : profile_(profile) { | 68 add_string.Run("extensionErrorsShowMore", |
52 } | 69 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE)); |
53 | 70 add_string.Run("extensionErrorsShowFewer", |
54 ExtensionErrorHandler::~ExtensionErrorHandler() { | 71 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_FEWER)); |
55 } | 72 add_string.Run("extensionErrorViewDetails", |
56 | 73 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_DETAILS)); |
57 void ExtensionErrorHandler::GetLocalizedValues( | 74 add_string.Run("extensionErrorViewManifest", |
58 content::WebUIDataSource* source) { | 75 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_MANIFEST)); |
59 source->AddString( | 76 add_string.Run("extensionErrorOverlayContextUrl", |
60 "extensionErrorsShowMore", | 77 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT)); |
61 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE)); | 78 add_string.Run("extensionErrorOverlayStackTrace", |
62 source->AddString( | 79 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_STACK_TRACE)); |
63 "extensionErrorsShowFewer", | 80 add_string.Run( |
64 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_FEWER)); | |
65 source->AddString( | |
66 "extensionErrorViewDetails", | |
67 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_DETAILS)); | |
68 source->AddString( | |
69 "extensionErrorViewManifest", | |
70 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_MANIFEST)); | |
71 source->AddString( | |
72 "extensionErrorOverlayContextUrl", | |
73 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT)); | |
74 source->AddString( | |
75 "extensionErrorOverlayStackTrace", | |
76 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_STACK_TRACE)); | |
77 source->AddString( | |
78 "extensionErrorOverlayAnonymousFunction", | 81 "extensionErrorOverlayAnonymousFunction", |
79 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_ANONYMOUS_FUNCTION)); | 82 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_ANONYMOUS_FUNCTION)); |
80 source->AddString( | 83 add_string.Run( |
81 "extensionErrorOverlayLaunchDevtools", | 84 "extensionErrorOverlayLaunchDevtools", |
82 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_LAUNCH_DEVTOOLS)); | 85 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_LAUNCH_DEVTOOLS)); |
83 source->AddString( | 86 add_string.Run( |
84 "extensionErrorOverlayContextUnknown", | 87 "extensionErrorOverlayContextUnknown", |
85 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT_UNKNOWN)); | 88 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT_UNKNOWN)); |
86 source->AddString( | 89 add_string.Run( |
87 "extensionErrorOverlayNoCodeToDisplay", | 90 "extensionErrorOverlayNoCodeToDisplay", |
88 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_NO_CODE_TO_DISPLAY)); | 91 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_NO_CODE_TO_DISPLAY)); |
89 } | 92 } |
90 | 93 |
91 void ExtensionErrorHandler::RegisterMessages() { | 94 void HandleRequestFileSource(const base::DictionaryValue* args, |
92 web_ui()->RegisterMessageCallback( | 95 Profile* profile, |
93 "extensionErrorRequestFileSource", | 96 const JavascriptResponseCallback& response) { |
94 base::Bind(&ExtensionErrorHandler::HandleRequestFileSource, | |
95 base::Unretained(this))); | |
96 web_ui()->RegisterMessageCallback( | |
97 "extensionErrorOpenDevTools", | |
98 base::Bind(&ExtensionErrorHandler::HandleOpenDevTools, | |
99 base::Unretained(this))); | |
100 } | |
101 | |
102 void ExtensionErrorHandler::HandleRequestFileSource( | |
103 const base::ListValue* args) { | |
104 // There should only be one argument, a dictionary. Use this instead of a list | |
105 // because it's more descriptive, harder to accidentally break with minor | |
106 // modifications, and supports optional arguments more easily. | |
107 CHECK_EQ(1u, args->GetSize()); | |
108 | |
109 const base::DictionaryValue* dict = NULL; | |
110 | |
111 // Three required arguments: extension_id, path_suffix, and error_message. | 97 // Three required arguments: extension_id, path_suffix, and error_message. |
112 std::string extension_id; | 98 std::string extension_id; |
113 base::FilePath::StringType path_suffix_string; | 99 base::FilePath::StringType path_suffix_string; |
114 base::string16 error_message; | 100 base::string16 error_message; |
115 | 101 |
116 if (!args->GetDictionary(0, &dict) || | 102 if (!args->GetString(kPathSuffixKey, &path_suffix_string) || |
117 !dict->GetString(kPathSuffixKey, &path_suffix_string) || | 103 !args->GetString(ExtensionError::kExtensionIdKey, &extension_id) || |
118 !dict->GetString(ExtensionError::kExtensionIdKey, &extension_id) || | 104 !args->GetString(ExtensionError::kMessageKey, &error_message)) { |
119 !dict->GetString(ExtensionError::kMessageKey, &error_message)) { | |
120 NOTREACHED(); | 105 NOTREACHED(); |
121 return; | 106 return; |
122 } | 107 } |
123 | 108 |
124 const Extension* extension = | 109 const Extension* extension = |
125 ExtensionSystem::Get(Profile::FromWebUI(web_ui()))-> | 110 ExtensionSystem::Get(profile)->extension_service()-> |
126 extension_service()->GetExtensionById(extension_id, | 111 GetExtensionById(extension_id, true /* include disabled */ ); |
127 true /* include disabled */ ); | |
128 | 112 |
129 // Under no circumstances should we ever need to reference a file outside of | 113 // Under no circumstances should we ever need to reference a file outside of |
130 // the extension's directory. If it tries to, abort. | 114 // the extension's directory. If it tries to, abort. |
131 base::FilePath path_suffix(path_suffix_string); | 115 base::FilePath path_suffix(path_suffix_string); |
132 if (path_suffix.ReferencesParent()) | 116 if (path_suffix.ReferencesParent()) |
133 return; | 117 return; |
134 | 118 |
135 base::FilePath path = extension->path().Append(path_suffix); | 119 base::FilePath path = extension->path().Append(path_suffix); |
136 | 120 |
137 // Setting the title and the error message is the same for all file types. | 121 // Setting the title and the error message is the same for all file types. |
138 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue); | 122 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue); |
139 results->SetString(kTitleKey, | 123 results->SetString(kTitleKey, |
140 base::UTF8ToUTF16(extension->name()) + | 124 base::UTF8ToUTF16(extension->name()) + |
141 base::ASCIIToUTF16(": ") + | 125 base::ASCIIToUTF16(": ") + |
142 path.BaseName().LossyDisplayName()); | 126 path.BaseName().LossyDisplayName()); |
143 results->SetString(ExtensionError::kMessageKey, error_message); | 127 results->SetString(ExtensionError::kMessageKey, error_message); |
144 | 128 |
145 base::Callback<void(const std::string&)> reply; | 129 base::Callback<void(const std::string&)> reply; |
146 if (path_suffix_string == kManifestFilename) { | 130 if (path_suffix_string == kManifestFilename) { |
147 std::string manifest_key; | 131 std::string manifest_key; |
148 if (!dict->GetString(ManifestError::kManifestKeyKey, &manifest_key)) { | 132 if (!args->GetString(ManifestError::kManifestKeyKey, &manifest_key)) { |
149 NOTREACHED(); | 133 NOTREACHED(); |
150 return; | 134 return; |
151 } | 135 } |
152 | 136 |
153 // A "specific" location is optional. | 137 // A "specific" location is optional. |
154 std::string specific; | 138 std::string specific; |
155 dict->GetString(ManifestError::kManifestSpecificKey, &specific); | 139 args->GetString(ManifestError::kManifestSpecificKey, &specific); |
156 | 140 |
157 reply = base::Bind(&ExtensionErrorHandler::GetManifestFileCallback, | 141 reply = base::Bind(&GetManifestFileCallback, |
158 base::Unretained(this), | |
159 base::Owned(results.release()), | 142 base::Owned(results.release()), |
160 manifest_key, | 143 manifest_key, |
161 specific); | 144 specific, |
| 145 response); |
162 } else { | 146 } else { |
163 int line_number = 0; | 147 int line_number = 0; |
164 dict->GetInteger(RuntimeError::kLineNumberKey, &line_number); | 148 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); |
165 | 149 |
166 reply = base::Bind(&ExtensionErrorHandler::GetSourceFileCallback, | 150 reply = base::Bind(&GetSourceFileCallback, |
167 base::Unretained(this), | |
168 base::Owned(results.release()), | 151 base::Owned(results.release()), |
169 line_number); | 152 line_number, |
| 153 response); |
170 } | 154 } |
171 | 155 |
172 base::PostTaskAndReplyWithResult( | 156 base::PostTaskAndReplyWithResult( |
173 content::BrowserThread::GetBlockingPool(), | 157 content::BrowserThread::GetBlockingPool(), |
174 FROM_HERE, | 158 FROM_HERE, |
175 base::Bind(&ReadFileToString, path), | 159 base::Bind(&ReadFileToString, path), |
176 reply); | 160 reply); |
177 } | 161 } |
178 | 162 |
179 void ExtensionErrorHandler::HandleOpenDevTools(const base::ListValue* args) { | 163 void HandleOpenDevTools(const base::DictionaryValue* args) { |
180 CHECK_EQ(1U, args->GetSize()); | |
181 | |
182 const base::DictionaryValue* dict = NULL; | |
183 int render_process_id = 0; | 164 int render_process_id = 0; |
184 int render_view_id = 0; | 165 int render_view_id = 0; |
185 | 166 |
186 // The render view and render process ids are required. | 167 // The render view and render process ids are required. |
187 if (!args->GetDictionary(0, &dict) || | 168 if (!args->GetInteger(RuntimeError::kRenderProcessIdKey, |
188 !dict->GetInteger(RuntimeError::kRenderProcessIdKey, | |
189 &render_process_id) || | 169 &render_process_id) || |
190 !dict->GetInteger(RuntimeError::kRenderViewIdKey, &render_view_id)) { | 170 !args->GetInteger(RuntimeError::kRenderViewIdKey, &render_view_id)) { |
191 NOTREACHED(); | 171 NOTREACHED(); |
192 return; | 172 return; |
193 } | 173 } |
194 | 174 |
195 content::RenderViewHost* rvh = | 175 content::RenderViewHost* rvh = |
196 content::RenderViewHost::FromID(render_process_id, render_view_id); | 176 content::RenderViewHost::FromID(render_process_id, render_view_id); |
197 | 177 |
198 // It's possible that the render view was closed since we last updated the | 178 // It's possible that the render view was closed since we last updated the |
199 // links. Handle this gracefully. | 179 // links. Handle this gracefully. |
200 if (!rvh) | 180 if (!rvh) |
201 return; | 181 return; |
202 | 182 |
203 // If we include a url, we should inspect it specifically (and not just the | 183 // If we include a url, we should inspect it specifically (and not just the |
204 // render view). | 184 // render view). |
205 base::string16 url; | 185 base::string16 url; |
206 if (dict->GetString(RuntimeError::kUrlKey, &url)) { | 186 if (args->GetString(RuntimeError::kUrlKey, &url)) { |
207 // Line and column numbers are optional; default to the first line. | 187 // Line and column numbers are optional; default to the first line. |
208 int line_number = 1; | 188 int line_number = 1; |
209 int column_number = 1; | 189 int column_number = 1; |
210 dict->GetInteger(RuntimeError::kLineNumberKey, &line_number); | 190 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); |
211 dict->GetInteger(RuntimeError::kColumnNumberKey, &column_number); | 191 args->GetInteger(RuntimeError::kColumnNumberKey, &column_number); |
212 | 192 |
213 // Line/column numbers are reported in display-friendly 1-based numbers, | 193 // Line/column numbers are reported in display-friendly 1-based numbers, |
214 // but are inspected in zero-based numbers. | 194 // but are inspected in zero-based numbers. |
215 DevToolsWindow::OpenDevToolsWindow(rvh, | 195 DevToolsWindow::OpenDevToolsWindow( |
216 DevToolsToggleAction::Reveal(url, line_number - 1, column_number - 1)); | 196 rvh, |
| 197 DevToolsToggleAction::Reveal(url, line_number - 1, column_number -1)); |
217 } else { | 198 } else { |
218 DevToolsWindow::OpenDevToolsWindow(rvh); | 199 DevToolsWindow::OpenDevToolsWindow(rvh); |
219 } | 200 } |
220 | 201 |
221 // Once we open the inspector, we focus on the appropriate tab... | 202 // Once we open the inspector, we focus on the appropriate tab... |
222 content::WebContents* web_contents = | 203 content::WebContents* web_contents = |
223 content::WebContents::FromRenderViewHost(rvh); | 204 content::WebContents::FromRenderViewHost(rvh); |
224 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 205 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
225 // ... but background pages have no associated browser (and the inspector | 206 // ... but background pages have no associated browser (and the inspector |
226 // opens in its own window), so our work is done. | 207 // opens in its own window), so our work is done. |
227 if (!browser) | 208 if (!browser) |
228 return; | 209 return; |
229 | 210 |
230 TabStripModel* tab_strip = browser->tab_strip_model(); | 211 TabStripModel* tab_strip = browser->tab_strip_model(); |
231 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents), | 212 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents), |
232 false); // Not through direct user gesture. | 213 false); // Not through direct user gesture. |
233 } | 214 } |
234 | 215 |
235 void ExtensionErrorHandler::GetManifestFileCallback( | 216 } // namespace error_ui_util |
236 base::DictionaryValue* results, | |
237 const std::string& key, | |
238 const std::string& specific, | |
239 const std::string& contents) { | |
240 ManifestHighlighter highlighter(contents, key, specific); | |
241 highlighter.SetHighlightedRegions(results); | |
242 web_ui()->CallJavascriptFunction( | |
243 "extensions.ExtensionErrorOverlay.requestFileSourceResponse", *results); | |
244 } | |
245 | |
246 void ExtensionErrorHandler::GetSourceFileCallback( | |
247 base::DictionaryValue* results, | |
248 int line_number, | |
249 const std::string& contents) { | |
250 SourceHighlighter highlighter(contents, line_number); | |
251 highlighter.SetHighlightedRegions(results); | |
252 web_ui()->CallJavascriptFunction( | |
253 "extensions.ExtensionErrorOverlay.requestFileSourceResponse", *results); | |
254 } | |
255 | |
256 } // namespace extensions | 217 } // namespace extensions |
OLD | NEW |