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

Side by Side Diff: chrome/browser/ui/webui/extensions/extension_error_handler.cc

Issue 150663013: Integrate ErrorConsole with Apps Dev Tool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove DeveloperPrivate.getStrings() Created 6 years, 10 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/location.h"
11 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/sequenced_worker_pool.h" 9 #include "base/threading/sequenced_worker_pool.h"
14 #include "base/values.h" 10 #include "base/values.h"
15 #include "chrome/browser/devtools/devtools_window.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/webui/extensions/extension_error_ui_util.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/browser/web_ui.h" 13 #include "content/public/browser/web_ui.h"
25 #include "content/public/browser/web_ui_data_source.h" 14 #include "content/public/browser/web_ui_data_source.h"
26 #include "extensions/browser/extension_error.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/file_highlighter.h"
29 #include "extensions/common/constants.h"
30 #include "extensions/common/extension.h" 15 #include "extensions/common/extension.h"
31 #include "grit/generated_resources.h" 16 #include "grit/generated_resources.h"
32 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
33 18
34 namespace extensions { 19 namespace extensions {
35 20
36 namespace { 21 namespace {
37 22
38 // Keys for objects passed to and from extension error UI. 23 void CallJavascriptFunction(content::WebUI* ui,
Dan Beam 2014/02/11 02:43:49 nit: s/ui/web_ui/
Devlin 2014/02/11 19:26:45 Done.
39 const char kPathSuffixKey[] = "pathSuffix"; 24 const std::string& function_name,
40 const char kTitleKey[] = "title"; 25 const base::DictionaryValue& args) {
41 26 ui->CallJavascriptFunction(function_name, args);
42 std::string ReadFileToString(const base::FilePath& path) {
43 std::string data;
44 base::ReadFileToString(path, &data);
45 return data;
46 } 27 }
47 28
48 } // namespace 29 } // namespace
49 30
50 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile) 31 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile)
51 : profile_(profile) { 32 : profile_(profile) {
52 } 33 }
53 34
54 ExtensionErrorHandler::~ExtensionErrorHandler() { 35 ExtensionErrorHandler::~ExtensionErrorHandler() {
55 } 36 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 // modifications, and supports optional arguments more easily. 87 // modifications, and supports optional arguments more easily.
107 CHECK_EQ(1u, args->GetSize()); 88 CHECK_EQ(1u, args->GetSize());
108 89
109 const base::DictionaryValue* dict = NULL; 90 const base::DictionaryValue* dict = NULL;
110 91
111 // Three required arguments: extension_id, path_suffix, and error_message. 92 // Three required arguments: extension_id, path_suffix, and error_message.
112 std::string extension_id; 93 std::string extension_id;
113 base::FilePath::StringType path_suffix_string; 94 base::FilePath::StringType path_suffix_string;
114 base::string16 error_message; 95 base::string16 error_message;
115 96
116 if (!args->GetDictionary(0, &dict) || 97 if (!args->GetDictionary(0, &dict)) {
117 !dict->GetString(kPathSuffixKey, &path_suffix_string) ||
118 !dict->GetString(ExtensionError::kExtensionIdKey, &extension_id) ||
119 !dict->GetString(ExtensionError::kMessageKey, &error_message)) {
120 NOTREACHED(); 98 NOTREACHED();
121 return; 99 return;
122 } 100 }
123 101
124 const Extension* extension = 102 error_ui_util::HandleRequestFileSource(
125 ExtensionSystem::Get(Profile::FromWebUI(web_ui()))-> 103 dict,
126 extension_service()->GetExtensionById(extension_id, 104 Profile::FromWebUI(web_ui()),
127 true /* include disabled */ ); 105 base::Bind(&CallJavascriptFunction,
128 106 base::Unretained(web_ui()),
129 // Under no circumstances should we ever need to reference a file outside of 107 "extensions.ExtensionErrorOverlay.requestFileSourceResponse"));
Dan Beam 2014/02/11 02:43:49 ^ i think this is clearer if not bound, i.e. in .
Dan Beam 2014/02/11 18:54:43 what about this?
Devlin 2014/02/11 19:26:45 Done.
130 // the extension's directory. If it tries to, abort.
131 base::FilePath path_suffix(path_suffix_string);
132 if (path_suffix.ReferencesParent())
133 return;
134
135 base::FilePath path = extension->path().Append(path_suffix);
136
137 // Setting the title and the error message is the same for all file types.
138 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue);
139 results->SetString(kTitleKey,
140 base::UTF8ToUTF16(extension->name()) +
141 base::ASCIIToUTF16(": ") +
142 path.BaseName().LossyDisplayName());
143 results->SetString(ExtensionError::kMessageKey, error_message);
144
145 base::Callback<void(const std::string&)> reply;
146 if (path_suffix_string == kManifestFilename) {
147 std::string manifest_key;
148 if (!dict->GetString(ManifestError::kManifestKeyKey, &manifest_key)) {
149 NOTREACHED();
150 return;
151 }
152
153 // A "specific" location is optional.
154 std::string specific;
155 dict->GetString(ManifestError::kManifestSpecificKey, &specific);
156
157 reply = base::Bind(&ExtensionErrorHandler::GetManifestFileCallback,
158 base::Unretained(this),
159 base::Owned(results.release()),
160 manifest_key,
161 specific);
162 } else {
163 int line_number = 0;
164 dict->GetInteger(RuntimeError::kLineNumberKey, &line_number);
165
166 reply = base::Bind(&ExtensionErrorHandler::GetSourceFileCallback,
167 base::Unretained(this),
168 base::Owned(results.release()),
169 line_number);
170 }
171
172 base::PostTaskAndReplyWithResult(
173 content::BrowserThread::GetBlockingPool(),
174 FROM_HERE,
175 base::Bind(&ReadFileToString, path),
176 reply);
177 } 108 }
178 109
179 void ExtensionErrorHandler::HandleOpenDevTools(const base::ListValue* args) { 110 void ExtensionErrorHandler::HandleOpenDevTools(const base::ListValue* args) {
180 CHECK_EQ(1U, args->GetSize()); 111 CHECK_EQ(1U, args->GetSize());
112 const base::DictionaryValue* dict = NULL;
181 113
182 const base::DictionaryValue* dict = NULL; 114 if (!args->GetDictionary(0, &dict)) {
183 int render_process_id = 0;
184 int render_view_id = 0;
185
186 // The render view and render process ids are required.
187 if (!args->GetDictionary(0, &dict) ||
188 !dict->GetInteger(RuntimeError::kRenderProcessIdKey,
189 &render_process_id) ||
190 !dict->GetInteger(RuntimeError::kRenderViewIdKey, &render_view_id)) {
191 NOTREACHED(); 115 NOTREACHED();
192 return; 116 return;
193 } 117 }
194 118
195 content::RenderViewHost* rvh = 119 error_ui_util::HandleOpenDevTools(dict);
196 content::RenderViewHost::FromID(render_process_id, render_view_id);
197
198 // It's possible that the render view was closed since we last updated the
199 // links. Handle this gracefully.
200 if (!rvh)
201 return;
202
203 // If we include a url, we should inspect it specifically (and not just the
204 // render view).
205 base::string16 url;
206 if (dict->GetString(RuntimeError::kUrlKey, &url)) {
207 // Line and column numbers are optional; default to the first line.
208 int line_number = 1;
209 int column_number = 1;
210 dict->GetInteger(RuntimeError::kLineNumberKey, &line_number);
211 dict->GetInteger(RuntimeError::kColumnNumberKey, &column_number);
212
213 // Line/column numbers are reported in display-friendly 1-based numbers,
214 // but are inspected in zero-based numbers.
215 DevToolsWindow::OpenDevToolsWindow(rvh,
216 DevToolsToggleAction::Reveal(url, line_number - 1, column_number - 1));
217 } else {
218 DevToolsWindow::OpenDevToolsWindow(rvh);
219 }
220
221 // Once we open the inspector, we focus on the appropriate tab...
222 content::WebContents* web_contents =
223 content::WebContents::FromRenderViewHost(rvh);
224 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
225 // ... but background pages have no associated browser (and the inspector
226 // opens in its own window), so our work is done.
227 if (!browser)
228 return;
229
230 TabStripModel* tab_strip = browser->tab_strip_model();
231 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents),
232 false); // Not through direct user gesture.
233 }
234
235 void ExtensionErrorHandler::GetManifestFileCallback(
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 } 120 }
255 121
256 } // namespace extensions 122 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698