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

Side by Side Diff: net/log/file_net_log_observer.cc

Issue 2894863002: Rename TaskRunner::RunsTasksOnCurrentThread() in //net (Closed)
Patch Set: fixed build error Created 3 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
« no previous file with comments | « net/http/transport_security_persister.cc ('k') | net/nqe/network_qualities_prefs_manager.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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "net/log/file_net_log_observer.h" 5 #include "net/log/file_net_log_observer.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <memory> 8 #include <memory>
9 #include <queue> 9 #include <queue>
10 #include <set> 10 #include <set>
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 current_file_idx_(0), 373 current_file_idx_(0),
374 max_file_size_(max_file_size), 374 max_file_size_(max_file_size),
375 task_runner_(task_runner) { 375 task_runner_(task_runner) {
376 event_files_.resize(total_num_files_); 376 event_files_.resize(total_num_files_);
377 } 377 }
378 378
379 FileNetLogObserver::BoundedFileWriter::~BoundedFileWriter() {} 379 FileNetLogObserver::BoundedFileWriter::~BoundedFileWriter() {}
380 380
381 void FileNetLogObserver::BoundedFileWriter::Initialize( 381 void FileNetLogObserver::BoundedFileWriter::Initialize(
382 std::unique_ptr<base::Value> constants_value) { 382 std::unique_ptr<base::Value> constants_value) {
383 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 383 DCHECK(task_runner_->RunsTasksInCurrentSequence());
384 384
385 event_files_[current_file_idx_] = base::ScopedFILE( 385 event_files_[current_file_idx_] = base::ScopedFILE(
386 base::OpenFile(directory_.AppendASCII("event_file_0.json"), "w")); 386 base::OpenFile(directory_.AppendASCII("event_file_0.json"), "w"));
387 387
388 base::ScopedFILE constants_file( 388 base::ScopedFILE constants_file(
389 base::OpenFile(directory_.AppendASCII("constants.json"), "w")); 389 base::OpenFile(directory_.AppendASCII("constants.json"), "w"));
390 390
391 // Print constants to file and open events array. 391 // Print constants to file and open events array.
392 std::string json; 392 std::string json;
393 393
394 // It should always be possible to convert constants to JSON. 394 // It should always be possible to convert constants to JSON.
395 if (!base::JSONWriter::Write(*constants_value, &json)) 395 if (!base::JSONWriter::Write(*constants_value, &json))
396 DCHECK(false); 396 DCHECK(false);
397 fprintf(constants_file.get(), "{\"constants\":%s,\n\"events\": [\n", 397 fprintf(constants_file.get(), "{\"constants\":%s,\n\"events\": [\n",
398 json.c_str()); 398 json.c_str());
399 } 399 }
400 400
401 void FileNetLogObserver::BoundedFileWriter::Stop( 401 void FileNetLogObserver::BoundedFileWriter::Stop(
402 std::unique_ptr<base::Value> polled_data) { 402 std::unique_ptr<base::Value> polled_data) {
403 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 403 DCHECK(task_runner_->RunsTasksInCurrentSequence());
404 404
405 base::ScopedFILE closing_file( 405 base::ScopedFILE closing_file(
406 base::OpenFile(directory_.AppendASCII("end_netlog.json"), "w")); 406 base::OpenFile(directory_.AppendASCII("end_netlog.json"), "w"));
407 407
408 std::string json; 408 std::string json;
409 if (polled_data) 409 if (polled_data)
410 base::JSONWriter::Write(*polled_data, &json); 410 base::JSONWriter::Write(*polled_data, &json);
411 411
412 fprintf(closing_file.get(), "]%s}\n", 412 fprintf(closing_file.get(), "]%s}\n",
413 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str()); 413 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str());
414 414
415 // Flush all fprintfs to disk so that files can be safely accessed on 415 // Flush all fprintfs to disk so that files can be safely accessed on
416 // callback. 416 // callback.
417 event_files_.clear(); 417 event_files_.clear();
418 } 418 }
419 419
420 void FileNetLogObserver::BoundedFileWriter::IncrementCurrentFile() { 420 void FileNetLogObserver::BoundedFileWriter::IncrementCurrentFile() {
421 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 421 DCHECK(task_runner_->RunsTasksInCurrentSequence());
422 422
423 current_file_idx_++; 423 current_file_idx_++;
424 current_file_idx_ %= total_num_files_; 424 current_file_idx_ %= total_num_files_;
425 event_files_[current_file_idx_].reset(); 425 event_files_[current_file_idx_].reset();
426 event_files_[current_file_idx_] = base::ScopedFILE(base::OpenFile( 426 event_files_[current_file_idx_] = base::ScopedFILE(base::OpenFile(
427 directory_.AppendASCII("event_file_" + 427 directory_.AppendASCII("event_file_" +
428 base::SizeTToString(current_file_idx_) + ".json"), 428 base::SizeTToString(current_file_idx_) + ".json"),
429 "w")); 429 "w"));
430 } 430 }
431 431
432 void FileNetLogObserver::BoundedFileWriter::Flush( 432 void FileNetLogObserver::BoundedFileWriter::Flush(
433 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) { 433 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) {
434 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 434 DCHECK(task_runner_->RunsTasksInCurrentSequence());
435 435
436 EventQueue local_file_queue; 436 EventQueue local_file_queue;
437 write_queue->SwapQueue(&local_file_queue); 437 write_queue->SwapQueue(&local_file_queue);
438 438
439 std::string to_print; 439 std::string to_print;
440 size_t file_size = ftell(event_files_[current_file_idx_].get()); 440 size_t file_size = ftell(event_files_[current_file_idx_].get());
441 size_t memory_freed = 0; 441 size_t memory_freed = 0;
442 442
443 while (!local_file_queue.empty()) { 443 while (!local_file_queue.empty()) {
444 if (file_size >= max_file_size_) { 444 if (file_size >= max_file_size_) {
445 // The current file is full. Start a new current file. 445 // The current file is full. Start a new current file.
446 IncrementCurrentFile(); 446 IncrementCurrentFile();
447 file_size = 0; 447 file_size = 0;
448 } 448 }
449 fprintf(event_files_[current_file_idx_].get(), "%s,\n", 449 fprintf(event_files_[current_file_idx_].get(), "%s,\n",
450 local_file_queue.front().get()->c_str()); 450 local_file_queue.front().get()->c_str());
451 file_size += local_file_queue.front()->size(); 451 file_size += local_file_queue.front()->size();
452 memory_freed += local_file_queue.front()->size(); 452 memory_freed += local_file_queue.front()->size();
453 local_file_queue.pop(); 453 local_file_queue.pop();
454 } 454 }
455 } 455 }
456 456
457 void FileNetLogObserver::BoundedFileWriter::DeleteAllFiles() { 457 void FileNetLogObserver::BoundedFileWriter::DeleteAllFiles() {
458 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 458 DCHECK(task_runner_->RunsTasksInCurrentSequence());
459 459
460 // Reset |event_files_| to release all file handles so base::DeleteFile can 460 // Reset |event_files_| to release all file handles so base::DeleteFile can
461 // safely access files. 461 // safely access files.
462 event_files_.clear(); 462 event_files_.clear();
463 463
464 base::DeleteFile(directory_.AppendASCII("constants.json"), false); 464 base::DeleteFile(directory_.AppendASCII("constants.json"), false);
465 base::DeleteFile(directory_.AppendASCII("end_netlog.json"), false); 465 base::DeleteFile(directory_.AppendASCII("end_netlog.json"), false);
466 for (size_t i = 0; i < total_num_files_; i++) { 466 for (size_t i = 0; i < total_num_files_; i++) {
467 base::DeleteFile(directory_.AppendASCII("event_file_" + 467 base::DeleteFile(directory_.AppendASCII("event_file_" +
468 base::SizeTToString(i) + ".json"), 468 base::SizeTToString(i) + ".json"),
469 false); 469 false);
470 } 470 }
471 } 471 }
472 472
473 FileNetLogObserver::UnboundedFileWriter::UnboundedFileWriter( 473 FileNetLogObserver::UnboundedFileWriter::UnboundedFileWriter(
474 const base::FilePath& path, 474 const base::FilePath& path,
475 scoped_refptr<base::SingleThreadTaskRunner> task_runner) 475 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
476 : file_path_(path), task_runner_(task_runner) {} 476 : file_path_(path), task_runner_(task_runner) {}
477 477
478 FileNetLogObserver::UnboundedFileWriter::~UnboundedFileWriter() {} 478 FileNetLogObserver::UnboundedFileWriter::~UnboundedFileWriter() {}
479 479
480 void FileNetLogObserver::UnboundedFileWriter::Initialize( 480 void FileNetLogObserver::UnboundedFileWriter::Initialize(
481 std::unique_ptr<base::Value> constants_value) { 481 std::unique_ptr<base::Value> constants_value) {
482 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 482 DCHECK(task_runner_->RunsTasksInCurrentSequence());
483 483
484 file_.reset(base::OpenFile(file_path_, "w")); 484 file_.reset(base::OpenFile(file_path_, "w"));
485 first_event_written_ = false; 485 first_event_written_ = false;
486 486
487 // Print constants to file and open events array. 487 // Print constants to file and open events array.
488 std::string json; 488 std::string json;
489 489
490 // It should always be possible to convert constants to JSON. 490 // It should always be possible to convert constants to JSON.
491 if (!base::JSONWriter::Write(*constants_value, &json)) 491 if (!base::JSONWriter::Write(*constants_value, &json))
492 DCHECK(false); 492 DCHECK(false);
493 fprintf(file_.get(), "{\"constants\":%s,\n\"events\": [\n", json.c_str()); 493 fprintf(file_.get(), "{\"constants\":%s,\n\"events\": [\n", json.c_str());
494 } 494 }
495 495
496 void FileNetLogObserver::UnboundedFileWriter::Stop( 496 void FileNetLogObserver::UnboundedFileWriter::Stop(
497 std::unique_ptr<base::Value> polled_data) { 497 std::unique_ptr<base::Value> polled_data) {
498 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 498 DCHECK(task_runner_->RunsTasksInCurrentSequence());
499 499
500 std::string json; 500 std::string json;
501 if (polled_data) 501 if (polled_data)
502 base::JSONWriter::Write(*polled_data, &json); 502 base::JSONWriter::Write(*polled_data, &json);
503 503
504 fprintf(file_.get(), "]%s}\n", 504 fprintf(file_.get(), "]%s}\n",
505 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str()); 505 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str());
506 506
507 // Flush all fprintfs to disk so that the file can be safely accessed on 507 // Flush all fprintfs to disk so that the file can be safely accessed on
508 // callback. 508 // callback.
509 file_.reset(); 509 file_.reset();
510 } 510 }
511 511
512 void FileNetLogObserver::UnboundedFileWriter::Flush( 512 void FileNetLogObserver::UnboundedFileWriter::Flush(
513 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) { 513 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) {
514 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 514 DCHECK(task_runner_->RunsTasksInCurrentSequence());
515 515
516 EventQueue local_file_queue; 516 EventQueue local_file_queue;
517 write_queue->SwapQueue(&local_file_queue); 517 write_queue->SwapQueue(&local_file_queue);
518 518
519 while (!local_file_queue.empty()) { 519 while (!local_file_queue.empty()) {
520 if (first_event_written_) { 520 if (first_event_written_) {
521 fputs(",\n", file_.get()); 521 fputs(",\n", file_.get());
522 } else { 522 } else {
523 first_event_written_ = true; 523 first_event_written_ = true;
524 } 524 }
525 fputs(local_file_queue.front()->c_str(), file_.get()); 525 fputs(local_file_queue.front()->c_str(), file_.get());
526 local_file_queue.pop(); 526 local_file_queue.pop();
527 } 527 }
528 } 528 }
529 529
530 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { 530 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() {
531 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 531 DCHECK(task_runner_->RunsTasksInCurrentSequence());
532 532
533 // Reset |file_| to release the file handle so base::DeleteFile can 533 // Reset |file_| to release the file handle so base::DeleteFile can
534 // safely access it. 534 // safely access it.
535 file_.reset(); 535 file_.reset();
536 base::DeleteFile(file_path_, false); 536 base::DeleteFile(file_path_, false);
537 } 537 }
538 538
539 } // namespace net 539 } // namespace net
OLDNEW
« no previous file with comments | « net/http/transport_security_persister.cc ('k') | net/nqe/network_qualities_prefs_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698