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