Chromium Code Reviews| 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 = base::PLATFORM_FILE_CREATE_ALWAYS | | |
| 196 base::PLATFORM_FILE_WRITE; | |
|
mmenke
2012/05/15 17:25:28
nit: For legibility, may want to align the two fl
ygorshenin1
2012/05/17 07:48:46
Done.
| |
| 197 file_handle_ = base::CreatePlatformFile(file_path_, flags, | |
| 198 &created_, &error_); | |
| 199 } | |
| 200 | |
| 201 void Reply(const DebugLogFileCallback& callback) { | |
| 202 DCHECK(!callback.is_null()); | |
| 203 callback.Run(PassPlatformFile(&file_handle_), created_, error_, file_path_); | |
| 204 } | |
| 205 | |
| 206 private: | |
| 207 PlatformFile file_handle_; | |
| 208 bool created_; | |
| 209 PlatformFileError error_; | |
| 210 FilePath file_path_; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(DebugLogFileHelper); | |
| 213 }; | |
| 214 | |
| 160 // Following functions are used for getting debug logs. Logs are | 215 // Following functions are used for getting debug logs. Logs are |
| 161 // fetched from /var/log/* and put on the fileshelf. | 216 // fetched from /var/log/* and put on the fileshelf. |
| 162 | 217 |
| 163 // Called once StoreDebugLogs is complete. Takes two parameters: | 218 // Called once StoreDebugLogs is complete. Takes two parameters: |
| 164 // - log_path: where the log file was saved in the case of success; | 219 // - log_path: where the log file was saved in the case of success; |
| 165 // - succeeded: was the log file saved successfully. | 220 // - succeeded: was the log file saved successfully. |
| 166 typedef base::Callback<void(const FilePath& log_path, | 221 typedef base::Callback<void(const FilePath& log_path, |
| 167 bool succeded)> StoreDebugLogsCallback; | 222 bool succeded)> StoreDebugLogsCallback; |
| 168 | 223 |
| 169 // Called once creation of the debug log file is completed. If | 224 // Closes file handle, so, should be called on the FILE thread. |
|
Sam Leffler
2012/05/15 16:42:28
nit: called on a workerpool thread so comment need
ygorshenin1
2012/05/15 17:13:39
Done.
| |
| 170 // creation failed, deletes the file by |log_path|. So, this function | 225 void CloseDebugLogFile(PassPlatformFile pass_platform_file) { |
| 171 // should be called on the FILE thread. After all, calls |callback| on | 226 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 } | 227 } |
| 184 | 228 |
| 185 // Retrieves debug logs from DebugDaemon and puts them on the | 229 // Closes file handle and deletes debug log file, so, should be called |
| 186 // fileshelf directory into .tgz archive. So, this function should be | 230 // on the FILE thread. |
|
Sam Leffler
2012/05/15 16:42:28
workerpool thread
ygorshenin1
2012/05/15 17:13:39
Done.
| |
| 187 // called on the FILE thread. Calls CreateDebugLogFileCompleted on the | 231 void CloseAndDeleteDebugLogFile(PassPlatformFile pass_platform_file, |
| 188 // FILE thread when creation of archive is completed. | 232 const FilePath& file_path) { |
| 189 void CreateDebugLogFile(const StoreDebugLogsCallback& callback) { | 233 CloseDebugLogFile(pass_platform_file); |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 234 file_util::Delete(file_path, false); |
| 235 } | |
| 191 | 236 |
| 192 const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("debug-log.tgz"); | 237 // Called after completion of |FillDebugLogFile|. Closes file |
| 193 | 238 // descriptor, deletes log file in the case of failure and calls |
| 194 FilePath fileshelf_dir; | 239 // |callback|. |
| 195 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_dir)) { | 240 void FillDebugLogFileCompleted(const StoreDebugLogsCallback& callback, |
| 196 LOG(ERROR) << "Can't get fileshelf dir"; | 241 PassPlatformFile pass_platform_file, |
| 197 CreateDebugLogFileCompleted(callback, FilePath(), false); | 242 const FilePath& file_path, |
| 243 bool succeeded) { | |
| 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 245 if (!succeeded) { | |
| 246 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, | |
| 247 base::Bind(&CloseAndDeleteDebugLogFile, pass_platform_file, file_path), | |
| 248 base::Bind(callback, file_path, false), false); | |
| 249 DCHECK(posted); | |
| 198 return; | 250 return; |
| 199 } | 251 } |
| 252 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, | |
| 253 base::Bind(&CloseDebugLogFile, pass_platform_file), | |
| 254 base::Bind(callback, file_path, true), false); | |
| 255 DCHECK(posted); | |
| 256 } | |
| 200 | 257 |
| 201 FilePath log_path = fileshelf_dir.Append(kLogFileName); | 258 // Stores into |file_path| user logs in the .tgz format. Calls |
| 202 log_path = logging::GenerateTimestampedName(log_path, base::Time::Now()); | 259 // |callback| upon completion. |
| 203 | 260 void FillDebugLogFile(const StoreDebugLogsCallback& callback, |
| 204 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | | 261 PassPlatformFile pass_platform_file, |
| 205 base::PLATFORM_FILE_WRITE; | 262 bool created, |
| 206 bool created; | 263 PlatformFileError error, |
| 207 PlatformFileError error; | 264 const FilePath& file_path) { |
| 208 PlatformFile log_file = base::CreatePlatformFile( | 265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 209 log_path, flags, &created, &error); | |
| 210 | |
| 211 if (!created) { | 266 if (!created) { |
| 212 LOG(ERROR) << | 267 LOG(ERROR) << |
| 213 "Can't create log file: " << log_path.AsUTF8Unsafe() << ", " << | 268 "Can't create debug log file: " << file_path.AsUTF8Unsafe() << ", " << |
| 214 "error: " << error; | 269 "error: " << error; |
| 215 CreateDebugLogFileCompleted(callback, log_path, false); | 270 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 271 base::Bind(&CloseDebugLogFile, pass_platform_file), | |
| 272 base::Bind(callback, file_path, false), false); | |
| 273 DCHECK(posted); | |
| 216 return; | 274 return; |
| 217 } | 275 } |
| 218 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> | 276 PlatformFile platform_file = pass_platform_file.ReleaseValue(); |
| 219 GetDebugLogs(log_file, | 277 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->GetDebugLogs( |
| 220 base::Bind(&CreateDebugLogFileCompleted, | 278 platform_file, |
| 221 callback, log_path)); | 279 base::Bind(&FillDebugLogFileCompleted, |
| 280 callback, PassPlatformFile(&platform_file), file_path)); | |
| 222 } | 281 } |
| 223 | 282 |
| 224 // Delegates the job of saving debug logs on the fileshelf to | 283 // Creates .tgz archive with user logs on the fileshelf. Calls |
| 225 // CreateDebugLogsFile on the FILE thread. Calls |callback| on the UI | 284 // |callback| upon completion. |
| 226 // thread when saving is completed. This function should be called on | |
| 227 // the UI thread. | |
| 228 void StoreDebugLogs(const StoreDebugLogsCallback& callback) { | 285 void StoreDebugLogs(const StoreDebugLogsCallback& callback) { |
| 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 230 | 287 DCHECK(!callback.is_null()); |
| 231 BrowserThread::PostTask( | 288 const FilePath fileshelf = download_util::GetDefaultDownloadDirectory(); |
| 232 BrowserThread::FILE, FROM_HERE, | 289 DebugLogFileHelper* helper = new DebugLogFileHelper(); |
| 233 base::Bind(&CreateDebugLogFile, callback)); | 290 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, |
| 291 base::Bind(&DebugLogFileHelper::DoWork, | |
| 292 base::Unretained(helper), fileshelf), | |
| 293 base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper), | |
| 294 base::Bind(&FillDebugLogFile, callback)), false); | |
| 295 DCHECK(posted); | |
| 234 } | 296 } |
| 235 #endif // OS_CHROMEOS | 297 #endif // OS_CHROMEOS |
| 236 | 298 |
| 237 // This class receives javascript messages from the renderer. | 299 // This class receives javascript messages from the renderer. |
| 238 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 300 // 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. | 301 // this class's methods are expected to run on the UI thread. |
| 240 // | 302 // |
| 241 // Since the network code we want to run lives on the IO thread, we proxy | 303 // Since the network code we want to run lives on the IO thread, we proxy |
| 242 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which | 304 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which |
| 243 // runs on the IO thread. | 305 // runs on the IO thread. |
| (...skipping 1426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1670 } | 1732 } |
| 1671 | 1733 |
| 1672 NetInternalsUI::NetInternalsUI(content::WebUI* web_ui) | 1734 NetInternalsUI::NetInternalsUI(content::WebUI* web_ui) |
| 1673 : WebUIController(web_ui) { | 1735 : WebUIController(web_ui) { |
| 1674 web_ui->AddMessageHandler(new NetInternalsMessageHandler()); | 1736 web_ui->AddMessageHandler(new NetInternalsMessageHandler()); |
| 1675 | 1737 |
| 1676 // Set up the chrome://net-internals/ source. | 1738 // Set up the chrome://net-internals/ source. |
| 1677 Profile* profile = Profile::FromWebUI(web_ui); | 1739 Profile* profile = Profile::FromWebUI(web_ui); |
| 1678 ChromeURLDataManager::AddDataSource(profile, CreateNetInternalsHTMLSource()); | 1740 ChromeURLDataManager::AddDataSource(profile, CreateNetInternalsHTMLSource()); |
| 1679 } | 1741 } |
| OLD | NEW |