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