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

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: Updated histograms.xml 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 {
37
38 // Keys for objects passed to and from extension error UI.
39 const char kPathSuffixKey[] = "pathSuffix";
40 const char kTitleKey[] = "title";
41
42 std::string ReadFileToString(const base::FilePath& path) {
43 std::string data;
44 base::ReadFileToString(path, &data);
45 return data;
46 }
47
48 } // namespace
49
50 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile) 21 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile)
51 : profile_(profile) { 22 : profile_(profile),
23 weak_ptr_factory_(this) {
52 } 24 }
53 25
54 ExtensionErrorHandler::~ExtensionErrorHandler() { 26 ExtensionErrorHandler::~ExtensionErrorHandler() {
55 } 27 }
56 28
57 void ExtensionErrorHandler::GetLocalizedValues( 29 void ExtensionErrorHandler::GetLocalizedValues(
58 content::WebUIDataSource* source) { 30 content::WebUIDataSource* source) {
59 source->AddString( 31 source->AddString(
60 "extensionErrorsShowMore", 32 "extensionErrorsShowMore",
61 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE)); 33 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE));
(...skipping 23 matching lines...) Expand all
85 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT_UNKNOWN)); 57 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT_UNKNOWN));
86 source->AddString( 58 source->AddString(
87 "extensionErrorOverlayNoCodeToDisplay", 59 "extensionErrorOverlayNoCodeToDisplay",
88 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_NO_CODE_TO_DISPLAY)); 60 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_NO_CODE_TO_DISPLAY));
89 } 61 }
90 62
91 void ExtensionErrorHandler::RegisterMessages() { 63 void ExtensionErrorHandler::RegisterMessages() {
92 web_ui()->RegisterMessageCallback( 64 web_ui()->RegisterMessageCallback(
93 "extensionErrorRequestFileSource", 65 "extensionErrorRequestFileSource",
94 base::Bind(&ExtensionErrorHandler::HandleRequestFileSource, 66 base::Bind(&ExtensionErrorHandler::HandleRequestFileSource,
95 base::Unretained(this))); 67 weak_ptr_factory_.GetWeakPtr()));
96 web_ui()->RegisterMessageCallback( 68 web_ui()->RegisterMessageCallback(
97 "extensionErrorOpenDevTools", 69 "extensionErrorOpenDevTools",
98 base::Bind(&ExtensionErrorHandler::HandleOpenDevTools, 70 base::Bind(&ExtensionErrorHandler::HandleOpenDevTools,
99 base::Unretained(this))); 71 weak_ptr_factory_.GetWeakPtr()));
100 } 72 }
101 73
102 void ExtensionErrorHandler::HandleRequestFileSource( 74 void ExtensionErrorHandler::HandleRequestFileSource(
103 const base::ListValue* args) { 75 const base::ListValue* args) {
104 // There should only be one argument, a dictionary. Use this instead of a list 76 // 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 77 // because it's more descriptive, harder to accidentally break with minor
106 // modifications, and supports optional arguments more easily. 78 // modifications, and supports optional arguments more easily.
107 CHECK_EQ(1u, args->GetSize()); 79 CHECK_EQ(1u, args->GetSize());
108 80
109 const base::DictionaryValue* dict = NULL; 81 const base::DictionaryValue* dict = NULL;
110 82
111 // Three required arguments: extension_id, path_suffix, and error_message. 83 // Three required arguments: extension_id, path_suffix, and error_message.
112 std::string extension_id; 84 std::string extension_id;
113 base::FilePath::StringType path_suffix_string; 85 base::FilePath::StringType path_suffix_string;
114 base::string16 error_message; 86 base::string16 error_message;
115 87
116 if (!args->GetDictionary(0, &dict) || 88 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(); 89 NOTREACHED();
121 return; 90 return;
122 } 91 }
123 92
124 const Extension* extension = 93 error_ui_util::HandleRequestFileSource(
125 ExtensionSystem::Get(Profile::FromWebUI(web_ui()))-> 94 dict,
126 extension_service()->GetExtensionById(extension_id, 95 Profile::FromWebUI(web_ui()),
127 true /* include disabled */ ); 96 base::Bind(&ExtensionErrorHandler::OnFileSourceHandled,
97 weak_ptr_factory_.GetWeakPtr()));
98 }
128 99
129 // Under no circumstances should we ever need to reference a file outside of 100 void ExtensionErrorHandler::OnFileSourceHandled(
130 // the extension's directory. If it tries to, abort. 101 const base::DictionaryValue& source) {
131 base::FilePath path_suffix(path_suffix_string); 102 web_ui()->CallJavascriptFunction(
132 if (path_suffix.ReferencesParent()) 103 "extensions.ExtensionErrorOverlay.requestFileSourceResponse", source);
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 } 104 }
178 105
179 void ExtensionErrorHandler::HandleOpenDevTools(const base::ListValue* args) { 106 void ExtensionErrorHandler::HandleOpenDevTools(const base::ListValue* args) {
180 CHECK_EQ(1U, args->GetSize()); 107 CHECK_EQ(1U, args->GetSize());
108 const base::DictionaryValue* dict = NULL;
181 109
182 const base::DictionaryValue* dict = NULL; 110 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(); 111 NOTREACHED();
192 return; 112 return;
193 } 113 }
194 114
195 content::RenderViewHost* rvh = 115 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 } 116 }
255 117
256 } // namespace extensions 118 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698