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