OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/extensions/extension_error_ui_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/location.h" |
| 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "base/values.h" |
| 17 #include "chrome/browser/devtools/devtools_window.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_finder.h" |
| 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/render_view_host.h" |
| 25 #include "content/public/browser/web_contents.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" |
| 31 |
| 32 namespace extensions { |
| 33 namespace error_ui_util { |
| 34 |
| 35 namespace { |
| 36 |
| 37 // Keys for objects passed to and from extension error UI. |
| 38 const char kPathSuffixKey[] = "pathSuffix"; |
| 39 const char kTitleKey[] = "title"; |
| 40 |
| 41 std::string ReadFileToString(const base::FilePath& path) { |
| 42 std::string data; |
| 43 base::ReadFileToString(path, &data); |
| 44 return data; |
| 45 } |
| 46 |
| 47 void GetManifestFileCallback(base::DictionaryValue* results, |
| 48 const std::string& key, |
| 49 const std::string& specific, |
| 50 const RequestFileSourceCallback& response, |
| 51 const std::string& contents) { |
| 52 ManifestHighlighter highlighter(contents, key, specific); |
| 53 highlighter.SetHighlightedRegions(results); |
| 54 response.Run(*results); |
| 55 } |
| 56 |
| 57 void GetSourceFileCallback(base::DictionaryValue* results, |
| 58 int line_number, |
| 59 const RequestFileSourceCallback& response, |
| 60 const std::string& contents) { |
| 61 SourceHighlighter highlighter(contents, line_number); |
| 62 highlighter.SetHighlightedRegions(results); |
| 63 response.Run(*results); |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 |
| 68 void HandleRequestFileSource(const base::DictionaryValue* args, |
| 69 Profile* profile, |
| 70 const RequestFileSourceCallback& response) { |
| 71 // Three required arguments: extension_id, path_suffix, and error_message. |
| 72 std::string extension_id; |
| 73 base::FilePath::StringType path_suffix_string; |
| 74 base::string16 error_message; |
| 75 |
| 76 if (!args->GetString(kPathSuffixKey, &path_suffix_string) || |
| 77 !args->GetString(ExtensionError::kExtensionIdKey, &extension_id) || |
| 78 !args->GetString(ExtensionError::kMessageKey, &error_message)) { |
| 79 NOTREACHED(); |
| 80 return; |
| 81 } |
| 82 |
| 83 ExtensionService* extension_service = |
| 84 ExtensionSystem::Get(profile)->extension_service(); |
| 85 if (!extension_service) |
| 86 return; |
| 87 |
| 88 const Extension* extension = extension_service->GetExtensionById( |
| 89 extension_id, true /* include disabled */); |
| 90 |
| 91 // Under no circumstances should we ever need to reference a file outside of |
| 92 // the extension's directory. If it tries to, abort. |
| 93 base::FilePath path_suffix(path_suffix_string); |
| 94 if (path_suffix.ReferencesParent()) |
| 95 return; |
| 96 |
| 97 base::FilePath path = extension->path().Append(path_suffix); |
| 98 |
| 99 // Setting the title and the error message is the same for all file types. |
| 100 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue); |
| 101 results->SetString(kTitleKey, |
| 102 base::UTF8ToUTF16(extension->name()) + |
| 103 base::ASCIIToUTF16(": ") + |
| 104 path.BaseName().LossyDisplayName()); |
| 105 results->SetString(ExtensionError::kMessageKey, error_message); |
| 106 |
| 107 base::Callback<void(const std::string&)> reply; |
| 108 if (path_suffix_string == kManifestFilename) { |
| 109 std::string manifest_key; |
| 110 if (!args->GetString(ManifestError::kManifestKeyKey, &manifest_key)) { |
| 111 NOTREACHED(); |
| 112 return; |
| 113 } |
| 114 |
| 115 // A "specific" location is optional. |
| 116 std::string specific; |
| 117 args->GetString(ManifestError::kManifestSpecificKey, &specific); |
| 118 |
| 119 reply = base::Bind(&GetManifestFileCallback, |
| 120 base::Owned(results.release()), |
| 121 manifest_key, |
| 122 specific, |
| 123 response); |
| 124 } else { |
| 125 int line_number = 0; |
| 126 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); |
| 127 |
| 128 reply = base::Bind(&GetSourceFileCallback, |
| 129 base::Owned(results.release()), |
| 130 line_number, |
| 131 response); |
| 132 } |
| 133 |
| 134 base::PostTaskAndReplyWithResult(content::BrowserThread::GetBlockingPool(), |
| 135 FROM_HERE, |
| 136 base::Bind(&ReadFileToString, path), |
| 137 reply); |
| 138 } |
| 139 |
| 140 void HandleOpenDevTools(const base::DictionaryValue* args) { |
| 141 int render_process_id = 0; |
| 142 int render_view_id = 0; |
| 143 |
| 144 // The render view and render process ids are required. |
| 145 if (!args->GetInteger(RuntimeError::kRenderProcessIdKey, |
| 146 &render_process_id) || |
| 147 !args->GetInteger(RuntimeError::kRenderViewIdKey, &render_view_id)) { |
| 148 NOTREACHED(); |
| 149 return; |
| 150 } |
| 151 |
| 152 content::RenderViewHost* rvh = |
| 153 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 154 |
| 155 // It's possible that the render view was closed since we last updated the |
| 156 // links. Handle this gracefully. |
| 157 if (!rvh) |
| 158 return; |
| 159 |
| 160 // If we include a url, we should inspect it specifically (and not just the |
| 161 // render view). |
| 162 base::string16 url; |
| 163 if (args->GetString(RuntimeError::kUrlKey, &url)) { |
| 164 // Line and column numbers are optional; default to the first line. |
| 165 int line_number = 1; |
| 166 int column_number = 1; |
| 167 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); |
| 168 args->GetInteger(RuntimeError::kColumnNumberKey, &column_number); |
| 169 |
| 170 // Line/column numbers are reported in display-friendly 1-based numbers, |
| 171 // but are inspected in zero-based numbers. |
| 172 DevToolsWindow::OpenDevToolsWindow( |
| 173 rvh, |
| 174 DevToolsToggleAction::Reveal(url, line_number - 1, column_number - 1)); |
| 175 } else { |
| 176 DevToolsWindow::OpenDevToolsWindow(rvh); |
| 177 } |
| 178 |
| 179 // Once we open the inspector, we focus on the appropriate tab... |
| 180 content::WebContents* web_contents = |
| 181 content::WebContents::FromRenderViewHost(rvh); |
| 182 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 183 |
| 184 // ... but some pages (popups and apps) don't have tabs, and some (background |
| 185 // pages) don't have an associated browser. For these, the inspector opens in |
| 186 // a new window, and our work is done. |
| 187 if (!browser || !browser->is_type_tabbed()) |
| 188 return; |
| 189 |
| 190 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 191 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents), |
| 192 false); // Not through direct user gesture. |
| 193 } |
| 194 |
| 195 } // namespace error_ui_util |
| 196 } // namespace extensions |
OLD | NEW |