| OLD | NEW |
| 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // is ignored. | 122 // is ignored. |
| 123 virtual void Stop(std::unique_ptr<base::Value> polled_data) = 0; | 123 virtual void Stop(std::unique_ptr<base::Value> polled_data) = 0; |
| 124 | 124 |
| 125 // Drains |queue_| from WriteQueue into a local file queue and writes the | 125 // Drains |queue_| from WriteQueue into a local file queue and writes the |
| 126 // events in the queue to disk. | 126 // events in the queue to disk. |
| 127 virtual void Flush(scoped_refptr<WriteQueue> write_queue) = 0; | 127 virtual void Flush(scoped_refptr<WriteQueue> write_queue) = 0; |
| 128 | 128 |
| 129 // Deletes all netlog files. It is not valid to call any method of | 129 // Deletes all netlog files. It is not valid to call any method of |
| 130 // FileNetLogObserver after DeleteAllFiles(). | 130 // FileNetLogObserver after DeleteAllFiles(). |
| 131 virtual void DeleteAllFiles() = 0; | 131 virtual void DeleteAllFiles() = 0; |
| 132 |
| 133 void FlushThenStop(scoped_refptr<WriteQueue> write_queue, |
| 134 std::unique_ptr<base::Value> polled_data); |
| 132 }; | 135 }; |
| 133 | 136 |
| 134 // This implementation of FileWriter is used when the observer is in bounded | 137 // This implementation of FileWriter is used when the observer is in bounded |
| 135 // mode. | 138 // mode. |
| 136 // BoundedFileWriter can be constructed on any thread, and afterwards is only | 139 // BoundedFileWriter can be constructed on any thread, and afterwards is only |
| 137 // accessed on the file thread. | 140 // accessed on the file thread. |
| 138 class FileNetLogObserver::BoundedFileWriter | 141 class FileNetLogObserver::BoundedFileWriter |
| 139 : public FileNetLogObserver::FileWriter { | 142 : public FileNetLogObserver::FileWriter { |
| 140 public: | 143 public: |
| 141 BoundedFileWriter(const base::FilePath& directory, | 144 BoundedFileWriter(const base::FilePath& directory, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 std::set<URLRequestContext*> contexts; | 295 std::set<URLRequestContext*> contexts; |
| 293 contexts.insert(url_request_context); | 296 contexts.insert(url_request_context); |
| 294 CreateNetLogEntriesForActiveObjects(contexts, this); | 297 CreateNetLogEntriesForActiveObjects(contexts, this); |
| 295 } | 298 } |
| 296 | 299 |
| 297 net_log->DeprecatedAddObserver(this, capture_mode); | 300 net_log->DeprecatedAddObserver(this, capture_mode); |
| 298 } | 301 } |
| 299 | 302 |
| 300 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, | 303 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, |
| 301 const base::Closure& callback) { | 304 const base::Closure& callback) { |
| 302 file_task_runner_->PostTask( | |
| 303 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, | |
| 304 base::Unretained(file_writer_), write_queue_)); | |
| 305 | |
| 306 file_task_runner_->PostTaskAndReply( | 305 file_task_runner_->PostTaskAndReply( |
| 307 FROM_HERE, | 306 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, |
| 308 base::Bind(&FileNetLogObserver::FileWriter::Stop, | 307 base::Unretained(file_writer_), write_queue_, |
| 309 base::Unretained(file_writer_), base::Passed(&polled_data)), | 308 base::Passed(&polled_data)), |
| 310 callback); | 309 callback); |
| 311 | 310 |
| 312 net_log()->DeprecatedRemoveObserver(this); | 311 net_log()->DeprecatedRemoveObserver(this); |
| 313 } | 312 } |
| 314 | 313 |
| 315 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { | 314 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { |
| 316 std::unique_ptr<std::string> json(new std::string); | 315 std::unique_ptr<std::string> json(new std::string); |
| 317 | 316 |
| 318 // If |entry| cannot be converted to proper JSON, ignore it. | 317 // If |entry| cannot be converted to proper JSON, ignore it. |
| 319 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) | 318 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 DCHECK(local_queue->empty()); | 355 DCHECK(local_queue->empty()); |
| 357 base::AutoLock lock(lock_); | 356 base::AutoLock lock(lock_); |
| 358 queue_.swap(*local_queue); | 357 queue_.swap(*local_queue); |
| 359 memory_ = 0; | 358 memory_ = 0; |
| 360 } | 359 } |
| 361 | 360 |
| 362 FileNetLogObserver::WriteQueue::~WriteQueue() {} | 361 FileNetLogObserver::WriteQueue::~WriteQueue() {} |
| 363 | 362 |
| 364 FileNetLogObserver::FileWriter::~FileWriter() {} | 363 FileNetLogObserver::FileWriter::~FileWriter() {} |
| 365 | 364 |
| 365 void FileNetLogObserver::FileWriter::FlushThenStop( |
| 366 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue, |
| 367 std::unique_ptr<base::Value> polled_data) { |
| 368 Flush(write_queue); |
| 369 Stop(std::move(polled_data)); |
| 370 } |
| 371 |
| 366 FileNetLogObserver::BoundedFileWriter::BoundedFileWriter( | 372 FileNetLogObserver::BoundedFileWriter::BoundedFileWriter( |
| 367 const base::FilePath& directory, | 373 const base::FilePath& directory, |
| 368 size_t max_file_size, | 374 size_t max_file_size, |
| 369 size_t total_num_files, | 375 size_t total_num_files, |
| 370 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 376 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 371 : directory_(directory), | 377 : directory_(directory), |
| 372 total_num_files_(total_num_files), | 378 total_num_files_(total_num_files), |
| 373 current_file_idx_(0), | 379 current_file_idx_(0), |
| 374 max_file_size_(max_file_size), | 380 max_file_size_(max_file_size), |
| 375 task_runner_(task_runner) { | 381 task_runner_(task_runner) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { | 536 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { |
| 531 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 537 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 532 | 538 |
| 533 // Reset |file_| to release the file handle so base::DeleteFile can | 539 // Reset |file_| to release the file handle so base::DeleteFile can |
| 534 // safely access it. | 540 // safely access it. |
| 535 file_.reset(); | 541 file_.reset(); |
| 536 base::DeleteFile(file_path_, false); | 542 base::DeleteFile(file_path_, false); |
| 537 } | 543 } |
| 538 | 544 |
| 539 } // namespace net | 545 } // namespace net |
| OLD | NEW |