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/files/file_path.h" | |
11 #include "base/files/file_util.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/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 "extensions/browser/extension_error.h" | |
26 #include "extensions/browser/extension_registry.h" | |
27 #include "extensions/browser/file_highlighter.h" | |
28 #include "extensions/common/constants.h" | |
29 #include "extensions/common/extension.h" | |
30 | |
31 namespace extensions { | |
32 namespace error_ui_util { | |
33 | |
34 namespace { | |
35 | |
36 // Keys for objects passed to and from extension error UI. | |
37 const char kPathSuffixKey[] = "pathSuffix"; | |
38 const char kTitleKey[] = "title"; | |
39 | |
40 std::string ReadFileToString(const base::FilePath& path) { | |
41 std::string data; | |
42 base::ReadFileToString(path, &data); | |
43 return data; | |
44 } | |
45 | |
46 void GetManifestFileCallback(base::DictionaryValue* results, | |
47 const std::string& key, | |
48 const std::string& specific, | |
49 const RequestFileSourceCallback& response, | |
50 const std::string& contents) { | |
51 ManifestHighlighter highlighter(contents, key, specific); | |
52 highlighter.SetHighlightedRegions(results); | |
53 response.Run(*results); | |
54 } | |
55 | |
56 void GetSourceFileCallback(base::DictionaryValue* results, | |
57 int line_number, | |
58 const RequestFileSourceCallback& response, | |
59 const std::string& contents) { | |
60 SourceHighlighter highlighter(contents, line_number); | |
61 highlighter.SetHighlightedRegions(results); | |
62 response.Run(*results); | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 void HandleRequestFileSource(const base::DictionaryValue* args, | |
68 Profile* profile, | |
69 const RequestFileSourceCallback& response) { | |
70 // Three required arguments: extension_id, path_suffix, and error_message. | |
71 std::string extension_id; | |
72 base::FilePath::StringType path_suffix_string; | |
73 base::string16 error_message; | |
74 | |
75 if (!args->GetString(kPathSuffixKey, &path_suffix_string) || | |
76 !args->GetString(ExtensionError::kExtensionIdKey, &extension_id) || | |
77 !args->GetString(ExtensionError::kMessageKey, &error_message)) { | |
78 NOTREACHED(); | |
79 return; | |
80 } | |
81 | |
82 const Extension* extension = | |
83 ExtensionRegistry::Get(profile)->GetExtensionById( | |
84 extension_id, ExtensionRegistry::EVERYTHING); | |
85 | |
86 if (!extension) { | |
87 NOTREACHED(); | |
88 return; | |
89 } | |
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 content::WebContents* web_contents = | |
161 content::WebContents::FromRenderViewHost(rvh); | |
162 if (!web_contents) | |
163 return; | |
164 | |
165 // If we include a url, we should inspect it specifically (and not just the | |
166 // render view). | |
167 base::string16 url; | |
168 if (args->GetString(RuntimeError::kUrlKey, &url)) { | |
169 // Line and column numbers are optional; default to the first line. | |
170 int line_number = 1; | |
171 int column_number = 1; | |
172 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); | |
173 args->GetInteger(RuntimeError::kColumnNumberKey, &column_number); | |
174 | |
175 // Line/column numbers are reported in display-friendly 1-based numbers, | |
176 // but are inspected in zero-based numbers. | |
177 DevToolsWindow::OpenDevToolsWindow( | |
178 web_contents, | |
179 DevToolsToggleAction::Reveal(url, line_number - 1, column_number - 1)); | |
180 } else { | |
181 DevToolsWindow::OpenDevToolsWindow(web_contents); | |
182 } | |
183 | |
184 // Once we open the inspector, we focus on the appropriate tab... | |
185 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | |
186 | |
187 // ... but some pages (popups and apps) don't have tabs, and some (background | |
188 // pages) don't have an associated browser. For these, the inspector opens in | |
189 // a new window, and our work is done. | |
190 if (!browser || !browser->is_type_tabbed()) | |
191 return; | |
192 | |
193 TabStripModel* tab_strip = browser->tab_strip_model(); | |
194 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents), | |
195 false); // Not through direct user gesture. | |
196 } | |
197 | |
198 } // namespace error_ui_util | |
199 } // namespace extensions | |
OLD | NEW |