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

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

Issue 2603523002: Move net-export thread-hopping code into NetLogFileWriter and add IO polled data. (Closed)
Patch Set: Fixed tommycli's nits from patchset 13 Created 3 years, 10 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/log/file_net_log_observer.h ('k') | net/log/file_net_log_observer_unittest.cc » ('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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 // FileWriter is an interface describing an object that drains events from a 110 // FileWriter is an interface describing an object that drains events from a
111 // WriteQueue and writes them to disk. 111 // WriteQueue and writes them to disk.
112 class FileNetLogObserver::FileWriter { 112 class FileNetLogObserver::FileWriter {
113 public: 113 public:
114 virtual ~FileWriter(); 114 virtual ~FileWriter();
115 115
116 // Writes |constants_value| to disk and opens the events array (closed in 116 // Writes |constants_value| to disk and opens the events array (closed in
117 // Stop()). 117 // Stop()).
118 virtual void Initialize(std::unique_ptr<base::Value> constants_value) = 0; 118 virtual void Initialize(std::unique_ptr<base::Value> constants_value) = 0;
119 119
120 // Closes the events array opened in Initialize() and writes |tab_info| to 120 // Closes the events array opened in Initialize() and writes |polled_data| to
121 // disk. If |tab_info| cannot be converted to proper JSON, then it 121 // disk. If |polled_data| cannot be converted to proper JSON, then it
122 // is ignored. 122 // is ignored.
123 virtual void Stop(std::unique_ptr<base::Value> tab_info) = 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 }; 132 };
133 133
134 // This implementation of FileWriter is used when the observer is in bounded 134 // This implementation of FileWriter is used when the observer is in bounded
135 // mode. 135 // mode.
136 // BoundedFileWriter can be constructed on any thread, and afterwards is only 136 // BoundedFileWriter can be constructed on any thread, and afterwards is only
137 // accessed on the file thread. 137 // accessed on the file thread.
138 class FileNetLogObserver::BoundedFileWriter 138 class FileNetLogObserver::BoundedFileWriter
139 : public FileNetLogObserver::FileWriter { 139 : public FileNetLogObserver::FileWriter {
140 public: 140 public:
141 BoundedFileWriter(const base::FilePath& directory, 141 BoundedFileWriter(const base::FilePath& directory,
142 size_t max_file_size, 142 size_t max_file_size,
143 size_t total_num_files, 143 size_t total_num_files,
144 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 144 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
145 145
146 ~BoundedFileWriter() override; 146 ~BoundedFileWriter() override;
147 147
148 // FileNetLogObserver::FileWriter implementation 148 // FileNetLogObserver::FileWriter implementation
149 void Initialize(std::unique_ptr<base::Value> constants_value) override; 149 void Initialize(std::unique_ptr<base::Value> constants_value) override;
150 void Stop(std::unique_ptr<base::Value> tab_info) override; 150 void Stop(std::unique_ptr<base::Value> polled_data) override;
151 void Flush(scoped_refptr<WriteQueue> write_queue) override; 151 void Flush(scoped_refptr<WriteQueue> write_queue) override;
152 void DeleteAllFiles() override; 152 void DeleteAllFiles() override;
153 153
154 private: 154 private:
155 // Increments |current_file_idx_|, and handles the clearing and openining of 155 // Increments |current_file_idx_|, and handles the clearing and openining of
156 // the new current file. Also sets |event_files_[current_file_idx_]| to point 156 // the new current file. Also sets |event_files_[current_file_idx_]| to point
157 // to the new current file. 157 // to the new current file.
158 void IncrementCurrentFile(); 158 void IncrementCurrentFile();
159 159
160 // Each ScopedFILE points to a netlog event file with the file name 160 // Each ScopedFILE points to a netlog event file with the file name
(...skipping 28 matching lines...) Expand all
189 class FileNetLogObserver::UnboundedFileWriter 189 class FileNetLogObserver::UnboundedFileWriter
190 : public FileNetLogObserver::FileWriter { 190 : public FileNetLogObserver::FileWriter {
191 public: 191 public:
192 UnboundedFileWriter(const base::FilePath& path, 192 UnboundedFileWriter(const base::FilePath& path,
193 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 193 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
194 194
195 ~UnboundedFileWriter() override; 195 ~UnboundedFileWriter() override;
196 196
197 // FileNetLogObserver::FileWriter implementation 197 // FileNetLogObserver::FileWriter implementation
198 void Initialize(std::unique_ptr<base::Value> constants_value) override; 198 void Initialize(std::unique_ptr<base::Value> constants_value) override;
199 void Stop(std::unique_ptr<base::Value> tab_info) override; 199 void Stop(std::unique_ptr<base::Value> polled_data) override;
200 void Flush(scoped_refptr<WriteQueue> write_queue) override; 200 void Flush(scoped_refptr<WriteQueue> write_queue) override;
201 void DeleteAllFiles() override; 201 void DeleteAllFiles() override;
202 202
203 private: 203 private:
204 base::FilePath file_path_; 204 base::FilePath file_path_;
205 base::ScopedFILE file_; 205 base::ScopedFILE file_;
206 206
207 // Is set to true after the first event is written to the log file. 207 // Is set to true after the first event is written to the log file.
208 bool first_event_written_; 208 bool first_event_written_;
209 209
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 if (url_request_context) { 290 if (url_request_context) {
291 DCHECK(url_request_context->CalledOnValidThread()); 291 DCHECK(url_request_context->CalledOnValidThread());
292 std::set<URLRequestContext*> contexts; 292 std::set<URLRequestContext*> contexts;
293 contexts.insert(url_request_context); 293 contexts.insert(url_request_context);
294 CreateNetLogEntriesForActiveObjects(contexts, this); 294 CreateNetLogEntriesForActiveObjects(contexts, this);
295 } 295 }
296 296
297 net_log->DeprecatedAddObserver(this, capture_mode); 297 net_log->DeprecatedAddObserver(this, capture_mode);
298 } 298 }
299 299
300 void FileNetLogObserver::StopObserving(URLRequestContext* url_request_context, 300 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data,
301 const base::Closure& callback) { 301 const base::Closure& callback) {
302 file_task_runner_->PostTask( 302 file_task_runner_->PostTask(
303 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, 303 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush,
304 base::Unretained(file_writer_), write_queue_)); 304 base::Unretained(file_writer_), write_queue_));
305 305
306 file_task_runner_->PostTaskAndReply( 306 file_task_runner_->PostTaskAndReply(
307 FROM_HERE, 307 FROM_HERE,
308 base::Bind( 308 base::Bind(&FileNetLogObserver::FileWriter::Stop,
309 &FileNetLogObserver::FileWriter::Stop, base::Unretained(file_writer_), 309 base::Unretained(file_writer_), base::Passed(&polled_data)),
310 base::Passed(url_request_context ? GetNetInfo(url_request_context,
311 NET_INFO_ALL_SOURCES)
312 : nullptr)),
313 callback); 310 callback);
314 311
315 net_log()->DeprecatedRemoveObserver(this); 312 net_log()->DeprecatedRemoveObserver(this);
316 } 313 }
317 314
318 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { 315 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) {
319 std::unique_ptr<std::string> json(new std::string); 316 std::unique_ptr<std::string> json(new std::string);
320 317
321 // If |entry| cannot be converted to proper JSON, ignore it. 318 // If |entry| cannot be converted to proper JSON, ignore it.
322 if (!base::JSONWriter::Write(*entry.ToValue(), json.get())) 319 if (!base::JSONWriter::Write(*entry.ToValue(), json.get()))
(...skipping 24 matching lines...) Expand all
347 344
348 while (memory_ > memory_max_ && !queue_.empty()) { 345 while (memory_ > memory_max_ && !queue_.empty()) {
349 // Delete oldest events in the queue. 346 // Delete oldest events in the queue.
350 DCHECK(queue_.front()); 347 DCHECK(queue_.front());
351 memory_ -= queue_.front()->size(); 348 memory_ -= queue_.front()->size();
352 queue_.pop(); 349 queue_.pop();
353 } 350 }
354 351
355 return queue_.size(); 352 return queue_.size();
356 } 353 }
354
357 void FileNetLogObserver::WriteQueue::SwapQueue(EventQueue* local_queue) { 355 void FileNetLogObserver::WriteQueue::SwapQueue(EventQueue* local_queue) {
358 DCHECK(local_queue->empty()); 356 DCHECK(local_queue->empty());
359 base::AutoLock lock(lock_); 357 base::AutoLock lock(lock_);
360 queue_.swap(*local_queue); 358 queue_.swap(*local_queue);
361 memory_ = 0; 359 memory_ = 0;
362 } 360 }
363 361
364 FileNetLogObserver::WriteQueue::~WriteQueue() {} 362 FileNetLogObserver::WriteQueue::~WriteQueue() {}
365 363
366 FileNetLogObserver::FileWriter::~FileWriter() {} 364 FileNetLogObserver::FileWriter::~FileWriter() {}
(...skipping 27 matching lines...) Expand all
394 std::string json; 392 std::string json;
395 393
396 // It should always be possible to convert constants to JSON. 394 // It should always be possible to convert constants to JSON.
397 if (!base::JSONWriter::Write(*constants_value, &json)) 395 if (!base::JSONWriter::Write(*constants_value, &json))
398 DCHECK(false); 396 DCHECK(false);
399 fprintf(constants_file.get(), "{\"constants\":%s,\n\"events\": [\n", 397 fprintf(constants_file.get(), "{\"constants\":%s,\n\"events\": [\n",
400 json.c_str()); 398 json.c_str());
401 } 399 }
402 400
403 void FileNetLogObserver::BoundedFileWriter::Stop( 401 void FileNetLogObserver::BoundedFileWriter::Stop(
404 std::unique_ptr<base::Value> tab_info) { 402 std::unique_ptr<base::Value> polled_data) {
405 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 403 DCHECK(task_runner_->RunsTasksOnCurrentThread());
406 404
407 base::ScopedFILE closing_file( 405 base::ScopedFILE closing_file(
408 base::OpenFile(directory_.AppendASCII("end_netlog.json"), "w")); 406 base::OpenFile(directory_.AppendASCII("end_netlog.json"), "w"));
409 407
410 std::string json; 408 std::string json;
411 if (tab_info) 409 if (polled_data)
412 base::JSONWriter::Write(*tab_info, &json); 410 base::JSONWriter::Write(*polled_data, &json);
413 411
414 fprintf(closing_file.get(), "]%s}\n", 412 fprintf(closing_file.get(), "]%s}\n",
415 json.empty() ? "" : (",\"tabInfo\": " + json + "\n").c_str()); 413 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str());
416 414
417 // 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
418 // callback. 416 // callback.
419 event_files_.clear(); 417 event_files_.clear();
420 } 418 }
421 419
422 void FileNetLogObserver::BoundedFileWriter::IncrementCurrentFile() { 420 void FileNetLogObserver::BoundedFileWriter::IncrementCurrentFile() {
423 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 421 DCHECK(task_runner_->RunsTasksOnCurrentThread());
424 422
425 current_file_idx_++; 423 current_file_idx_++;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 // Print constants to file and open events array. 487 // Print constants to file and open events array.
490 std::string json; 488 std::string json;
491 489
492 // It should always be possible to convert constants to JSON. 490 // It should always be possible to convert constants to JSON.
493 if (!base::JSONWriter::Write(*constants_value, &json)) 491 if (!base::JSONWriter::Write(*constants_value, &json))
494 DCHECK(false); 492 DCHECK(false);
495 fprintf(file_.get(), "{\"constants\":%s,\n\"events\": [\n", json.c_str()); 493 fprintf(file_.get(), "{\"constants\":%s,\n\"events\": [\n", json.c_str());
496 } 494 }
497 495
498 void FileNetLogObserver::UnboundedFileWriter::Stop( 496 void FileNetLogObserver::UnboundedFileWriter::Stop(
499 std::unique_ptr<base::Value> tab_info) { 497 std::unique_ptr<base::Value> polled_data) {
500 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 498 DCHECK(task_runner_->RunsTasksOnCurrentThread());
501 499
502 std::string json; 500 std::string json;
503 if (tab_info) 501 if (polled_data)
504 base::JSONWriter::Write(*tab_info, &json); 502 base::JSONWriter::Write(*polled_data, &json);
505 503
506 fprintf(file_.get(), "]%s}\n", 504 fprintf(file_.get(), "]%s}\n",
507 json.empty() ? "" : (",\n\"tabInfo\": " + json + "\n").c_str()); 505 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str());
508 506
509 // 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
510 // callback. 508 // callback.
511 file_.reset(); 509 file_.reset();
512 } 510 }
513 511
514 void FileNetLogObserver::UnboundedFileWriter::Flush( 512 void FileNetLogObserver::UnboundedFileWriter::Flush(
515 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) { 513 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) {
516 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 514 DCHECK(task_runner_->RunsTasksOnCurrentThread());
517 515
(...skipping 14 matching lines...) Expand all
532 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { 530 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() {
533 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 531 DCHECK(task_runner_->RunsTasksOnCurrentThread());
534 532
535 // Reset |file_| to release the file handle so base::DeleteFile can 533 // Reset |file_| to release the file handle so base::DeleteFile can
536 // safely access it. 534 // safely access it.
537 file_.reset(); 535 file_.reset();
538 base::DeleteFile(file_path_, false); 536 base::DeleteFile(file_path_, false);
539 } 537 }
540 538
541 } // namespace net 539 } // namespace net
OLDNEW
« no previous file with comments | « net/log/file_net_log_observer.h ('k') | net/log/file_net_log_observer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698