Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: chrome/browser/ui/webui/net_internals/net_internals_ui.cc

Issue 270853002: Use base::File for DebugDaemonClient::GetDebugLogs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chromeos/dbus/debug_daemon_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_util.h" 17 #include "base/file_util.h"
18 #include "base/files/file.h" 18 #include "base/files/file.h"
19 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
20 #include "base/memory/weak_ptr.h" 20 #include "base/memory/weak_ptr.h"
21 #include "base/message_loop/message_loop.h" 21 #include "base/message_loop/message_loop.h"
22 #include "base/prefs/pref_member.h" 22 #include "base/prefs/pref_member.h"
23 #include "base/sequenced_task_runner_helpers.h" 23 #include "base/sequenced_task_runner_helpers.h"
24 #include "base/strings/string_number_conversions.h" 24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_piece.h" 25 #include "base/strings/string_piece.h"
26 #include "base/strings/string_split.h" 26 #include "base/strings/string_split.h"
27 #include "base/strings/string_util.h" 27 #include "base/strings/string_util.h"
28 #include "base/strings/utf_string_conversions.h" 28 #include "base/strings/utf_string_conversions.h"
29 #include "base/task/cancelable_task_tracker.h" 29 #include "base/task/cancelable_task_tracker.h"
30 #include "base/threading/worker_pool.h"
31 #include "base/values.h" 30 #include "base/values.h"
32 #include "chrome/browser/browser_process.h" 31 #include "chrome/browser/browser_process.h"
33 #include "chrome/browser/browsing_data/browsing_data_helper.h" 32 #include "chrome/browser/browsing_data/browsing_data_helper.h"
34 #include "chrome/browser/browsing_data/browsing_data_remover.h" 33 #include "chrome/browser/browsing_data/browsing_data_remover.h"
35 #include "chrome/browser/chrome_notification_types.h" 34 #include "chrome/browser/chrome_notification_types.h"
36 #include "chrome/browser/download/download_prefs.h" 35 #include "chrome/browser/download/download_prefs.h"
37 #include "chrome/browser/extensions/extension_service.h" 36 #include "chrome/browser/extensions/extension_service.h"
38 #include "chrome/browser/io_thread.h" 37 #include "chrome/browser/io_thread.h"
39 #include "chrome/browser/net/chrome_net_log.h" 38 #include "chrome/browser/net/chrome_net_log.h"
40 #include "chrome/browser/net/chrome_network_delegate.h" 39 #include "chrome/browser/net/chrome_network_delegate.h"
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 content::WebUIDataSource::Create(chrome::kChromeUINetInternalsHost); 201 content::WebUIDataSource::Create(chrome::kChromeUINetInternalsHost);
203 202
204 source->SetUseJsonJSFormatV2(); 203 source->SetUseJsonJSFormatV2();
205 source->SetDefaultResource(IDR_NET_INTERNALS_INDEX_HTML); 204 source->SetDefaultResource(IDR_NET_INTERNALS_INDEX_HTML);
206 source->AddResourcePath("index.js", IDR_NET_INTERNALS_INDEX_JS); 205 source->AddResourcePath("index.js", IDR_NET_INTERNALS_INDEX_JS);
207 source->SetJsonPath("strings.js"); 206 source->SetJsonPath("strings.js");
208 return source; 207 return source;
209 } 208 }
210 209
211 #if defined(OS_CHROMEOS) 210 #if defined(OS_CHROMEOS)
212 // Small helper class used to create temporary log file and pass its
213 // handle and error status to callback.
214 // Use case:
215 // DebugLogFileHelper* helper = new DebugLogFileHelper();
216 // base::WorkerPool::PostTaskAndReply(FROM_HERE,
217 // base::Bind(&DebugLogFileHelper::DoWork, base::Unretained(helper), ...),
218 // base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper), ...),
219 // false);
220 class DebugLogFileHelper {
221 public:
222 typedef base::Callback<void(base::File file,
223 const base::FilePath& file_path)>
224 DebugLogFileCallback;
225
226 DebugLogFileHelper() {}
227
228 ~DebugLogFileHelper() {}
229
230 void DoWork(const base::FilePath& fileshelf) {
231 const base::FilePath::CharType kLogFileName[] =
232 FILE_PATH_LITERAL("debug-log.tgz");
233
234 file_path_ = fileshelf.Append(kLogFileName);
235 file_path_ = logging::GenerateTimestampedName(file_path_,
236 base::Time::Now());
237
238 int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE;
239 file_.Initialize(file_path_, flags);
240 }
241
242 void Reply(const DebugLogFileCallback& callback) {
243 DCHECK(!callback.is_null());
244 callback.Run(file_.Pass(), file_path_);
245 }
246
247 private:
248 base::File file_;
249 base::FilePath file_path_;
250
251 DISALLOW_COPY_AND_ASSIGN(DebugLogFileHelper);
252 };
253
254 // Following functions are used for getting debug logs. Logs are 211 // Following functions are used for getting debug logs. Logs are
255 // fetched from /var/log/* and put on the fileshelf. 212 // fetched from /var/log/* and put on the fileshelf.
256 213
257 // Called once StoreDebugLogs is complete. Takes two parameters: 214 // Called once StoreDebugLogs is complete. Takes two parameters:
258 // - log_path: where the log file was saved in the case of success; 215 // - log_path: where the log file was saved in the case of success;
259 // - succeeded: was the log file saved successfully. 216 // - succeeded: was the log file saved successfully.
260 typedef base::Callback<void(const base::FilePath& log_path, 217 typedef base::Callback<void(const base::FilePath& log_path,
261 bool succeded)> StoreDebugLogsCallback; 218 bool succeded)> StoreDebugLogsCallback;
262 219
263 // Closes file handle, so, should be called on the WorkerPool thread.
264 void CloseDebugLogFile(base::File file) {
265 file.Close();
266 }
267
268 // Closes file handle and deletes debug log file, so, should be called
269 // on the WorkerPool thread.
270 void CloseAndDeleteDebugLogFile(base::File file,
271 const base::FilePath& file_path) {
272 file.Close();
273 base::DeleteFile(file_path, false);
274 }
275
276 // Called upon completion of |WriteDebugLogToFile|. Closes file 220 // Called upon completion of |WriteDebugLogToFile|. Closes file
277 // descriptor, deletes log file in the case of failure and calls 221 // descriptor, deletes log file in the case of failure and calls
278 // |callback|. 222 // |callback|.
279 void WriteDebugLogToFileCompleted(const StoreDebugLogsCallback& callback, 223 void WriteDebugLogToFileCompleted(const StoreDebugLogsCallback& callback,
280 base::File file,
281 const base::FilePath& file_path, 224 const base::FilePath& file_path,
282 bool succeeded) { 225 bool succeeded) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI); 226 DCHECK_CURRENTLY_ON(BrowserThread::UI);
284 if (!succeeded) { 227 if (!succeeded) {
285 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, 228 bool posted = BrowserThread::PostBlockingPoolTaskAndReply(
286 base::Bind(&CloseAndDeleteDebugLogFile, Passed(&file), file_path), 229 FROM_HERE,
287 base::Bind(callback, file_path, false), false); 230 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path, false),
231 base::Bind(callback, file_path, false));
288 DCHECK(posted); 232 DCHECK(posted);
289 return; 233 return;
290 } 234 }
291 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, 235 callback.Run(file_path, true);
292 base::Bind(&CloseDebugLogFile, Passed(&file)),
293 base::Bind(callback, file_path, true), false);
294 DCHECK(posted);
295 } 236 }
296 237
297 // Stores into |file_path| debug logs in the .tgz format. Calls 238 // Stores into |file_path| debug logs in the .tgz format. Calls
298 // |callback| upon completion. 239 // |callback| upon completion.
299 void WriteDebugLogToFile(const StoreDebugLogsCallback& callback, 240 void WriteDebugLogToFile(const StoreDebugLogsCallback& callback,
300 base::File file, 241 base::File* file,
301 const base::FilePath& file_path) { 242 const base::FilePath& file_path) {
302 DCHECK_CURRENTLY_ON(BrowserThread::UI); 243 DCHECK_CURRENTLY_ON(BrowserThread::UI);
303 if (!file.IsValid()) { 244 if (!file->IsValid()) {
304 LOG(ERROR) << 245 LOG(ERROR) <<
305 "Can't create debug log file: " << file_path.AsUTF8Unsafe() << ", " << 246 "Can't create debug log file: " << file_path.AsUTF8Unsafe() << ", " <<
306 "error: " << file.error_details(); 247 "error: " << file->error_details();
307 return; 248 return;
308 } 249 }
309 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->GetDebugLogs( 250 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->GetDebugLogs(
310 file.GetPlatformFile(), 251 file->Pass(),
mmenke 2014/05/08 14:29:34 Erm...I don't see Pass() defined anywhere for a ba
hashimoto 2014/05/08 15:13:12 Like scoped_ptr, base::File contains MOVE_ONLY_TYP
mmenke 2014/05/08 15:43:50 I'm having trouble understanding their explanation
hashimoto 2014/05/08 15:59:51 StoreDebugLogs() below runs base::File::Initialize
311 base::Bind(&WriteDebugLogToFileCompleted, 252 base::Bind(&WriteDebugLogToFileCompleted, callback, file_path));
312 callback, Passed(&file), file_path));
313 } 253 }
314 254
315 // Stores debug logs in the .tgz archive on the |fileshelf|. The file 255 // Stores debug logs in the .tgz archive on the |fileshelf|. The file
316 // is created on the worker pool, then writing to it is triggered from 256 // is created on the worker pool, then writing to it is triggered from
317 // the UI thread, and finally it is closed (on success) or deleted (on 257 // the UI thread, and finally it is closed (on success) or deleted (on
318 // failure) on the worker pool, prior to calling |callback|. 258 // failure) on the worker pool, prior to calling |callback|.
319 void StoreDebugLogs(const base::FilePath& fileshelf, 259 void StoreDebugLogs(const base::FilePath& fileshelf,
320 const StoreDebugLogsCallback& callback) { 260 const StoreDebugLogsCallback& callback) {
321 DCHECK_CURRENTLY_ON(BrowserThread::UI); 261 DCHECK_CURRENTLY_ON(BrowserThread::UI);
322 DCHECK(!callback.is_null()); 262 DCHECK(!callback.is_null());
323 DebugLogFileHelper* helper = new DebugLogFileHelper(); 263
324 bool posted = base::WorkerPool::PostTaskAndReply(FROM_HERE, 264 const base::FilePath::CharType kLogFileName[] =
325 base::Bind(&DebugLogFileHelper::DoWork, 265 FILE_PATH_LITERAL("debug-log.tgz");
326 base::Unretained(helper), fileshelf), 266
327 base::Bind(&DebugLogFileHelper::Reply, base::Owned(helper), 267 base::FilePath file_path = fileshelf.Append(kLogFileName);
328 base::Bind(&WriteDebugLogToFile, callback)), false); 268 file_path = logging::GenerateTimestampedName(file_path, base::Time::Now());
269
270 int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE;
271 base::File* file = new base::File;
272 bool posted = BrowserThread::PostBlockingPoolTaskAndReply(
273 FROM_HERE,
274 base::Bind(&base::File::Initialize,
275 base::Unretained(file), file_path, flags),
276 base::Bind(&WriteDebugLogToFile, callback, base::Owned(file), file_path));
329 DCHECK(posted); 277 DCHECK(posted);
330 } 278 }
331 #endif // defined(OS_CHROMEOS) 279 #endif // defined(OS_CHROMEOS)
332 280
333 // This class receives javascript messages from the renderer. 281 // This class receives javascript messages from the renderer.
334 // Note that the WebUI infrastructure runs on the UI thread, therefore all of 282 // Note that the WebUI infrastructure runs on the UI thread, therefore all of
335 // this class's methods are expected to run on the UI thread. 283 // this class's methods are expected to run on the UI thread.
336 // 284 //
337 // Since the network code we want to run lives on the IO thread, we proxy 285 // Since the network code we want to run lives on the IO thread, we proxy
338 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which 286 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which
(...skipping 1547 matching lines...) Expand 10 before | Expand all | Expand 10 after
1886 } 1834 }
1887 1835
1888 NetInternalsUI::NetInternalsUI(content::WebUI* web_ui) 1836 NetInternalsUI::NetInternalsUI(content::WebUI* web_ui)
1889 : WebUIController(web_ui) { 1837 : WebUIController(web_ui) {
1890 web_ui->AddMessageHandler(new NetInternalsMessageHandler()); 1838 web_ui->AddMessageHandler(new NetInternalsMessageHandler());
1891 1839
1892 // Set up the chrome://net-internals/ source. 1840 // Set up the chrome://net-internals/ source.
1893 Profile* profile = Profile::FromWebUI(web_ui); 1841 Profile* profile = Profile::FromWebUI(web_ui);
1894 content::WebUIDataSource::Add(profile, CreateNetInternalsHTMLSource()); 1842 content::WebUIDataSource::Add(profile, CreateNetInternalsHTMLSource());
1895 } 1843 }
OLDNEW
« no previous file with comments | « no previous file | chromeos/dbus/debug_daemon_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698