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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 new WriteQueue(std::numeric_limits<size_t>::max())); | 258 new WriteQueue(std::numeric_limits<size_t>::max())); |
259 | 259 |
260 return std::unique_ptr<FileNetLogObserver>( | 260 return std::unique_ptr<FileNetLogObserver>( |
261 new FileNetLogObserver(file_task_runner, std::move(file_writer), | 261 new FileNetLogObserver(file_task_runner, std::move(file_writer), |
262 std::move(write_queue), std::move(constants))); | 262 std::move(write_queue), std::move(constants))); |
263 } | 263 } |
264 | 264 |
265 FileNetLogObserver::~FileNetLogObserver() { | 265 FileNetLogObserver::~FileNetLogObserver() { |
266 if (net_log()) { | 266 if (net_log()) { |
267 // StopObserving was not called. | 267 // StopObserving was not called. |
| 268 net_log()->DeprecatedRemoveObserver(this); |
268 file_task_runner_->PostTask( | 269 file_task_runner_->PostTask( |
269 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, | 270 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, |
270 base::Unretained(file_writer_))); | 271 base::Unretained(file_writer_))); |
271 net_log()->DeprecatedRemoveObserver(this); | |
272 } | 272 } |
273 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); | 273 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); |
274 } | 274 } |
275 | 275 |
276 void FileNetLogObserver::StartObserving(NetLog* net_log, | 276 void FileNetLogObserver::StartObserving(NetLog* net_log, |
277 NetLogCaptureMode capture_mode) { | 277 NetLogCaptureMode capture_mode) { |
278 net_log->DeprecatedAddObserver(this, capture_mode); | 278 net_log->DeprecatedAddObserver(this, capture_mode); |
279 } | 279 } |
280 | 280 |
281 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, | 281 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, |
282 const base::Closure& callback) { | 282 const base::Closure& callback) { |
| 283 net_log()->DeprecatedRemoveObserver(this); |
| 284 |
283 file_task_runner_->PostTaskAndReply( | 285 file_task_runner_->PostTaskAndReply( |
284 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, | 286 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, |
285 base::Unretained(file_writer_), write_queue_, | 287 base::Unretained(file_writer_), write_queue_, |
286 base::Passed(&polled_data)), | 288 base::Passed(&polled_data)), |
287 callback); | 289 callback); |
288 | |
289 net_log()->DeprecatedRemoveObserver(this); | |
290 } | 290 } |
291 | 291 |
292 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { | 292 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { |
293 std::unique_ptr<std::string> json(new std::string); | 293 std::unique_ptr<std::string> json(new std::string); |
294 | 294 |
295 // If |entry| cannot be converted to proper JSON, ignore it. | 295 // If |entry| cannot be converted to proper JSON, ignore it. |
296 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) | 296 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) |
297 return; | 297 return; |
298 | 298 |
299 size_t queue_size = write_queue_->AddEntryToQueue(std::move(json)); | 299 size_t queue_size = write_queue_->AddEntryToQueue(std::move(json)); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_->RunsTasksInCurrentSequence()); | 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 CHECK(!event_files_.empty()); |
440 size_t file_size = ftell(event_files_[current_file_idx_].get()); | 441 size_t file_size = ftell(event_files_[current_file_idx_].get()); |
441 size_t memory_freed = 0; | 442 size_t memory_freed = 0; |
442 | 443 |
443 while (!local_file_queue.empty()) { | 444 while (!local_file_queue.empty()) { |
444 if (file_size >= max_file_size_) { | 445 if (file_size >= max_file_size_) { |
445 // The current file is full. Start a new current file. | 446 // The current file is full. Start a new current file. |
446 IncrementCurrentFile(); | 447 IncrementCurrentFile(); |
447 file_size = 0; | 448 file_size = 0; |
448 } | 449 } |
449 fprintf(event_files_[current_file_idx_].get(), "%s,\n", | 450 fprintf(event_files_[current_file_idx_].get(), "%s,\n", |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { | 531 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { |
531 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 532 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
532 | 533 |
533 // Reset |file_| to release the file handle so base::DeleteFile can | 534 // Reset |file_| to release the file handle so base::DeleteFile can |
534 // safely access it. | 535 // safely access it. |
535 file_.reset(); | 536 file_.reset(); |
536 base::DeleteFile(file_path_, false); | 537 base::DeleteFile(file_path_, false); |
537 } | 538 } |
538 | 539 |
539 } // namespace net | 540 } // namespace net |
OLD | NEW |