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); | |
Dan Beam
2014/02/11 02:43:49
i have a feeling this method uses |data| as an out
Devlin
2014/02/11 18:41:16
base::ReadFileToString() actually returns a bool i
| |
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 const Extension* extension = | |
81 ExtensionSystem::Get(profile)->extension_service()->GetExtensionById( | |
Dan Beam
2014/02/11 02:43:49
^ what if |extension_service()| is NULL?
Devlin
2014/02/11 18:41:16
Hmm... I wonder if this can happen (this is only c
| |
82 extension_id, true /* include disabled */); | |
83 | |
84 // Under no circumstances should we ever need to reference a file outside of | |
85 // the extension's directory. If it tries to, abort. | |
86 base::FilePath path_suffix(path_suffix_string); | |
87 if (path_suffix.ReferencesParent()) | |
88 return; | |
89 | |
90 base::FilePath path = extension->path().Append(path_suffix); | |
91 | |
92 // Setting the title and the error message is the same for all file types. | |
93 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue); | |
94 results->SetString(kTitleKey, | |
95 base::UTF8ToUTF16(extension->name()) + | |
96 base::ASCIIToUTF16(": ") + | |
97 path.BaseName().LossyDisplayName()); | |
98 results->SetString(ExtensionError::kMessageKey, error_message); | |
99 | |
100 base::Callback<void(const std::string&)> reply; | |
101 if (path_suffix_string == kManifestFilename) { | |
102 std::string manifest_key; | |
103 if (!args->GetString(ManifestError::kManifestKeyKey, &manifest_key)) { | |
104 NOTREACHED(); | |
105 return; | |
106 } | |
107 | |
108 // A "specific" location is optional. | |
109 std::string specific; | |
110 args->GetString(ManifestError::kManifestSpecificKey, &specific); | |
111 | |
112 reply = base::Bind(&GetManifestFileCallback, | |
113 base::Owned(results.release()), | |
114 manifest_key, | |
115 specific, | |
116 response); | |
117 } else { | |
118 int line_number = 0; | |
119 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); | |
120 | |
121 reply = base::Bind(&GetSourceFileCallback, | |
122 base::Owned(results.release()), | |
123 line_number, | |
124 response); | |
125 } | |
126 | |
127 base::PostTaskAndReplyWithResult(content::BrowserThread::GetBlockingPool(), | |
128 FROM_HERE, | |
129 base::Bind(&ReadFileToString, path), | |
130 reply); | |
131 } | |
132 | |
133 void HandleOpenDevTools(const base::DictionaryValue* args) { | |
134 int render_process_id = 0; | |
135 int render_view_id = 0; | |
136 | |
137 // The render view and render process ids are required. | |
138 if (!args->GetInteger(RuntimeError::kRenderProcessIdKey, | |
139 &render_process_id) || | |
140 !args->GetInteger(RuntimeError::kRenderViewIdKey, &render_view_id)) { | |
141 NOTREACHED(); | |
142 return; | |
143 } | |
144 | |
145 content::RenderViewHost* rvh = | |
146 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
147 | |
148 // It's possible that the render view was closed since we last updated the | |
149 // links. Handle this gracefully. | |
150 if (!rvh) | |
151 return; | |
152 | |
153 // If we include a url, we should inspect it specifically (and not just the | |
154 // render view). | |
155 base::string16 url; | |
156 if (args->GetString(RuntimeError::kUrlKey, &url)) { | |
157 // Line and column numbers are optional; default to the first line. | |
158 int line_number = 1; | |
159 int column_number = 1; | |
160 args->GetInteger(RuntimeError::kLineNumberKey, &line_number); | |
161 args->GetInteger(RuntimeError::kColumnNumberKey, &column_number); | |
162 | |
163 // Line/column numbers are reported in display-friendly 1-based numbers, | |
164 // but are inspected in zero-based numbers. | |
165 DevToolsWindow::OpenDevToolsWindow( | |
166 rvh, | |
167 DevToolsToggleAction::Reveal(url, line_number - 1, column_number - 1)); | |
168 } else { | |
169 DevToolsWindow::OpenDevToolsWindow(rvh); | |
170 } | |
171 | |
172 // Once we open the inspector, we focus on the appropriate tab... | |
173 content::WebContents* web_contents = | |
174 content::WebContents::FromRenderViewHost(rvh); | |
175 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | |
176 // ... but background pages have no associated browser (and the inspector | |
177 // opens in its own window), so our work is done. | |
178 if (!browser) | |
179 return; | |
180 | |
181 TabStripModel* tab_strip = browser->tab_strip_model(); | |
182 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(web_contents), | |
Dan Beam
2014/02/11 02:43:49
^ what if !browser->is_type_tabbed() (i.e. it's a
Devlin
2014/02/11 18:41:16
It actually works fine now - for Browsers::TYPE_PO
| |
183 false); // Not through direct user gesture. | |
Dan Beam
2014/02/11 02:43:49
^ didn't the user click "Open dev tools"?
Devlin
2014/02/11 18:41:16
Yeah, kinda anti-intuitive.
"|user_gesture| is tr
| |
184 } | |
185 | |
186 } // namespace error_ui_util | |
187 } // namespace extensions | |
OLD | NEW |