OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/net_internals/net_internals_ui.h" | 5 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/base64.h" | 13 #include "base/base64.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
18 #include "base/file_util.h" | 18 #include "base/file_util.h" |
19 #include "base/memory/singleton.h" | 19 #include "base/memory/singleton.h" |
20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
21 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
22 #include "base/message_loop_helpers.h" | 22 #include "base/message_loop_helpers.h" |
23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
24 #include "base/platform_file.h" | 24 #include "base/platform_file.h" |
25 #include "base/string_number_conversions.h" | 25 #include "base/string_number_conversions.h" |
26 #include "base/string_piece.h" | 26 #include "base/string_piece.h" |
27 #include "base/string_split.h" | 27 #include "base/string_split.h" |
28 #include "base/string_util.h" | 28 #include "base/string_util.h" |
| 29 #include "base/threading/worker_pool.h" |
29 #include "base/utf_string_conversions.h" | 30 #include "base/utf_string_conversions.h" |
30 #include "base/values.h" | 31 #include "base/values.h" |
31 #include "chrome/browser/browser_process.h" | 32 #include "chrome/browser/browser_process.h" |
32 #include "chrome/browser/browsing_data_remover.h" | 33 #include "chrome/browser/browsing_data_remover.h" |
| 34 #include "chrome/browser/download/download_util.h" |
33 #include "chrome/browser/io_thread.h" | 35 #include "chrome/browser/io_thread.h" |
34 #include "chrome/browser/net/chrome_net_log.h" | 36 #include "chrome/browser/net/chrome_net_log.h" |
35 #include "chrome/browser/net/connection_tester.h" | 37 #include "chrome/browser/net/connection_tester.h" |
36 #include "chrome/browser/net/url_fixer_upper.h" | 38 #include "chrome/browser/net/url_fixer_upper.h" |
37 #include "chrome/browser/prefs/pref_member.h" | 39 #include "chrome/browser/prefs/pref_member.h" |
38 #include "chrome/browser/prerender/prerender_manager.h" | 40 #include "chrome/browser/prerender/prerender_manager.h" |
39 #include "chrome/browser/prerender/prerender_manager_factory.h" | 41 #include "chrome/browser/prerender/prerender_manager_factory.h" |
40 #include "chrome/browser/profiles/profile.h" | 42 #include "chrome/browser/profiles/profile.h" |
41 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 43 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
42 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 44 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 #include "chrome/browser/chromeos/cros/cros_library.h" | 78 #include "chrome/browser/chromeos/cros/cros_library.h" |
77 #include "chrome/browser/chromeos/cros/network_library.h" | 79 #include "chrome/browser/chromeos/cros/network_library.h" |
78 #include "chrome/browser/chromeos/system/syslogs_provider.h" | 80 #include "chrome/browser/chromeos/system/syslogs_provider.h" |
79 #include "chromeos/dbus/dbus_thread_manager.h" | 81 #include "chromeos/dbus/dbus_thread_manager.h" |
80 #include "chromeos/dbus/debug_daemon_client.h" | 82 #include "chromeos/dbus/debug_daemon_client.h" |
81 #endif | 83 #endif |
82 #ifdef OS_WIN | 84 #ifdef OS_WIN |
83 #include "chrome/browser/net/service_providers_win.h" | 85 #include "chrome/browser/net/service_providers_win.h" |
84 #endif | 86 #endif |
85 | 87 |
| 88 using base::PassPlatformFile; |
86 using base::PlatformFile; | 89 using base::PlatformFile; |
87 using base::PlatformFileError; | 90 using base::PlatformFileError; |
88 using content::BrowserThread; | 91 using content::BrowserThread; |
89 using content::WebContents; | 92 using content::WebContents; |
90 using content::WebUIMessageHandler; | 93 using content::WebUIMessageHandler; |
91 | 94 |
92 namespace { | 95 namespace { |
93 | 96 |
94 // Delay between when an event occurs and when it is passed to the Javascript | 97 // Delay between when an event occurs and when it is passed to the Javascript |
95 // page. All events that occur during this period are grouped together and | 98 // page. All events that occur during this period are grouped together and |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 153 |
151 source->set_default_resource(IDR_NET_INTERNALS_INDEX_HTML); | 154 source->set_default_resource(IDR_NET_INTERNALS_INDEX_HTML); |
152 source->add_resource_path("help.html", IDR_NET_INTERNALS_HELP_HTML); | 155 source->add_resource_path("help.html", IDR_NET_INTERNALS_HELP_HTML); |
153 source->add_resource_path("help.js", IDR_NET_INTERNALS_HELP_JS); | 156 source->add_resource_path("help.js", IDR_NET_INTERNALS_HELP_JS); |
154 source->add_resource_path("index.js", IDR_NET_INTERNALS_INDEX_JS); | 157 source->add_resource_path("index.js", IDR_NET_INTERNALS_INDEX_JS); |
155 source->set_json_path("strings.js"); | 158 source->set_json_path("strings.js"); |
156 return source; | 159 return source; |
157 } | 160 } |
158 | 161 |
159 #ifdef OS_CHROMEOS | 162 #ifdef OS_CHROMEOS |
| 163 // Small helper class used to create temporary log file and pass its |
| 164 // handle and error status to callback. |
| 165 // Use case: |
| 166 // DebugLogFileHelper* helper = new DebugLogFileHelper(); |
| 167 // base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 168 // base::Bind(&DebugLogFileHelper::DoWork, base::Unretained(helper), ...), |
| 169 // base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper), ...), |
| 170 // false); |
| 171 class DebugLogFileHelper { |
| 172 public: |
| 173 typedef base::Callback<void(PassPlatformFile pass_platform_file, |
| 174 bool created, |
| 175 PlatformFileError error, |
| 176 const FilePath& file_path)> DebugLogFileCallback; |
| 177 |
| 178 DebugLogFileHelper() |
| 179 : file_handle_(base::kInvalidPlatformFileValue), |
| 180 created_(false), |
| 181 error_(base::PLATFORM_FILE_OK) { |
| 182 } |
| 183 |
| 184 ~DebugLogFileHelper() { |
| 185 } |
| 186 |
| 187 void DoWork(const FilePath& fileshelf) { |
| 188 const FilePath::CharType kLogFileName[] = |
| 189 FILE_PATH_LITERAL("debug-log.tgz"); |
| 190 |
| 191 file_path_ = fileshelf.Append(kLogFileName); |
| 192 file_path_ = logging::GenerateTimestampedName(file_path_, |
| 193 base::Time::Now()); |
| 194 |
| 195 int flags = |
| 196 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 197 base::PLATFORM_FILE_WRITE; |
| 198 file_handle_ = base::CreatePlatformFile(file_path_, flags, |
| 199 &created_, &error_); |
| 200 } |
| 201 |
| 202 void Reply(const DebugLogFileCallback& callback) { |
| 203 DCHECK(!callback.is_null()); |
| 204 callback.Run(PassPlatformFile(&file_handle_), created_, error_, file_path_); |
| 205 } |
| 206 |
| 207 private: |
| 208 PlatformFile file_handle_; |
| 209 bool created_; |
| 210 PlatformFileError error_; |
| 211 FilePath file_path_; |
| 212 |
| 213 DISALLOW_COPY_AND_ASSIGN(DebugLogFileHelper); |
| 214 }; |
| 215 |
160 // Following functions are used for getting debug logs. Logs are | 216 // Following functions are used for getting debug logs. Logs are |
161 // fetched from /var/log/* and put on the fileshelf. | 217 // fetched from /var/log/* and put on the fileshelf. |
162 | 218 |
163 // Called once StoreDebugLogs is complete. Takes two parameters: | 219 // Called once StoreDebugLogs is complete. Takes two parameters: |
164 // - log_path: where the log file was saved in the case of success; | 220 // - log_path: where the log file was saved in the case of success; |
165 // - succeeded: was the log file saved successfully. | 221 // - succeeded: was the log file saved successfully. |
166 typedef base::Callback<void(const FilePath& log_path, | 222 typedef base::Callback<void(const FilePath& log_path, |
167 bool succeded)> StoreDebugLogsCallback; | 223 bool succeded)> StoreDebugLogsCallback; |
168 | 224 |
169 // Called once creation of the debug log file is completed. If | 225 // Closes file handle, so, should be called on the WorkerPool thread. |
170 // creation failed, deletes the file by |log_path|. So, this function | 226 void CloseDebugLogFile(PassPlatformFile pass_platform_file) { |
171 // should be called on the FILE thread. After all, calls |callback| on | 227 base::ClosePlatformFile(pass_platform_file.ReleaseValue()); |
172 // the UI thread. | |
173 void CreateDebugLogFileCompleted(const StoreDebugLogsCallback& callback, | |
174 const FilePath& log_path, | |
175 bool succeeded) { | |
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
177 | |
178 if (!succeeded) | |
179 file_util::Delete(log_path, false); | |
180 | |
181 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
182 base::Bind(callback, log_path, succeeded)); | |
183 } | 228 } |
184 | 229 |
185 // Retrieves debug logs from DebugDaemon and puts them on the | 230 // Closes file handle and deletes debug log file, so, should be called |
186 // fileshelf directory into .tgz archive. So, this function should be | 231 // on the WorkerPool thread. |
187 // called on the FILE thread. Calls CreateDebugLogFileCompleted on the | 232 void CloseAndDeleteDebugLogFile(PassPlatformFile pass_platform_file, |
188 // FILE thread when creation of archive is completed. | 233 const FilePath& file_path) { |
189 void CreateDebugLogFile(const StoreDebugLogsCallback& callback) { | 234 CloseDebugLogFile(pass_platform_file); |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 235 file_util::Delete(file_path, false); |
| 236 } |
191 | 237 |
192 const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("debug-log.tgz"); | 238 // Called upon completion of |WriteDebugLogToFile|. Closes file |
193 | 239 // descriptor, deletes log file in the case of failure and calls |
194 FilePath fileshelf_dir; | 240 // |callback|. |
195 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_dir)) { | 241 void WriteDebugLogToFileCompleted(const StoreDebugLogsCallback& callback, |
196 LOG(ERROR) << "Can't get fileshelf dir"; | 242 PassPlatformFile pass_platform_file, |
197 CreateDebugLogFileCompleted(callback, FilePath(), false); | 243 const FilePath& file_path, |
| 244 bool succeeded) { |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 246 if (!succeeded) { |
| 247 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 248 base::Bind(&CloseAndDeleteDebugLogFile, pass_platform_file, file_path), |
| 249 base::Bind(callback, file_path, false), false); |
| 250 DCHECK(posted); |
198 return; | 251 return; |
199 } | 252 } |
| 253 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 254 base::Bind(&CloseDebugLogFile, pass_platform_file), |
| 255 base::Bind(callback, file_path, true), false); |
| 256 DCHECK(posted); |
| 257 } |
200 | 258 |
201 FilePath log_path = fileshelf_dir.Append(kLogFileName); | 259 // Stores into |file_path| debug logs in the .tgz format. Calls |
202 log_path = logging::GenerateTimestampedName(log_path, base::Time::Now()); | 260 // |callback| upon completion. |
203 | 261 void WriteDebugLogToFile(const StoreDebugLogsCallback& callback, |
204 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | | 262 PassPlatformFile pass_platform_file, |
205 base::PLATFORM_FILE_WRITE; | 263 bool created, |
206 bool created; | 264 PlatformFileError error, |
207 PlatformFileError error; | 265 const FilePath& file_path) { |
208 PlatformFile log_file = base::CreatePlatformFile( | 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
209 log_path, flags, &created, &error); | |
210 | |
211 if (!created) { | 267 if (!created) { |
212 LOG(ERROR) << | 268 LOG(ERROR) << |
213 "Can't create log file: " << log_path.AsUTF8Unsafe() << ", " << | 269 "Can't create debug log file: " << file_path.AsUTF8Unsafe() << ", " << |
214 "error: " << error; | 270 "error: " << error; |
215 CreateDebugLogFileCompleted(callback, log_path, false); | 271 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 272 base::Bind(&CloseDebugLogFile, pass_platform_file), |
| 273 base::Bind(callback, file_path, false), false); |
| 274 DCHECK(posted); |
216 return; | 275 return; |
217 } | 276 } |
218 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> | 277 PlatformFile platform_file = pass_platform_file.ReleaseValue(); |
219 GetDebugLogs(log_file, | 278 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->GetDebugLogs( |
220 base::Bind(&CreateDebugLogFileCompleted, | 279 platform_file, |
221 callback, log_path)); | 280 base::Bind(&WriteDebugLogToFileCompleted, |
| 281 callback, PassPlatformFile(&platform_file), file_path)); |
222 } | 282 } |
223 | 283 |
224 // Delegates the job of saving debug logs on the fileshelf to | 284 // Stores debug logs in the .tgz archive on the fileshelf. The file is |
225 // CreateDebugLogsFile on the FILE thread. Calls |callback| on the UI | 285 // created on the worker pool, then writing to it is triggered from |
226 // thread when saving is completed. This function should be called on | 286 // the UI thread, and finally it is closed (on success) or deleted (on |
227 // the UI thread. | 287 // failure) on the worker pool, prior to calling |callback|. |
228 void StoreDebugLogs(const StoreDebugLogsCallback& callback) { | 288 void StoreDebugLogs(const StoreDebugLogsCallback& callback) { |
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
230 | 290 DCHECK(!callback.is_null()); |
231 BrowserThread::PostTask( | 291 const FilePath fileshelf = download_util::GetDefaultDownloadDirectory(); |
232 BrowserThread::FILE, FROM_HERE, | 292 DebugLogFileHelper* helper = new DebugLogFileHelper(); |
233 base::Bind(&CreateDebugLogFile, callback)); | 293 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 294 base::Bind(&DebugLogFileHelper::DoWork, |
| 295 base::Unretained(helper), fileshelf), |
| 296 base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper), |
| 297 base::Bind(&WriteDebugLogToFile, callback)), false); |
| 298 DCHECK(posted); |
234 } | 299 } |
235 #endif // OS_CHROMEOS | 300 #endif // OS_CHROMEOS |
236 | 301 |
237 // This class receives javascript messages from the renderer. | 302 // This class receives javascript messages from the renderer. |
238 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 303 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
239 // this class's methods are expected to run on the UI thread. | 304 // this class's methods are expected to run on the UI thread. |
240 // | 305 // |
241 // Since the network code we want to run lives on the IO thread, we proxy | 306 // Since the network code we want to run lives on the IO thread, we proxy |
242 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which | 307 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which |
243 // runs on the IO thread. | 308 // runs on the IO thread. |
(...skipping 1426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1670 } | 1735 } |
1671 | 1736 |
1672 NetInternalsUI::NetInternalsUI(content::WebUI* web_ui) | 1737 NetInternalsUI::NetInternalsUI(content::WebUI* web_ui) |
1673 : WebUIController(web_ui) { | 1738 : WebUIController(web_ui) { |
1674 web_ui->AddMessageHandler(new NetInternalsMessageHandler()); | 1739 web_ui->AddMessageHandler(new NetInternalsMessageHandler()); |
1675 | 1740 |
1676 // Set up the chrome://net-internals/ source. | 1741 // Set up the chrome://net-internals/ source. |
1677 Profile* profile = Profile::FromWebUI(web_ui); | 1742 Profile* profile = Profile::FromWebUI(web_ui); |
1678 ChromeURLDataManager::AddDataSource(profile, CreateNetInternalsHTMLSource()); | 1743 ChromeURLDataManager::AddDataSource(profile, CreateNetInternalsHTMLSource()); |
1679 } | 1744 } |
OLD | NEW |