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

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: Various refactors to make code cleaner Created 3 years, 11 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
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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 std::string json; 391 std::string json;
395 392
396 // It should always be possible to convert constants to JSON. 393 // It should always be possible to convert constants to JSON.
397 if (!base::JSONWriter::Write(*constants_value, &json)) 394 if (!base::JSONWriter::Write(*constants_value, &json))
398 DCHECK(false); 395 DCHECK(false);
399 fprintf(constants_file.get(), "{\"constants\":%s,\n\"events\": [\n", 396 fprintf(constants_file.get(), "{\"constants\":%s,\n\"events\": [\n",
400 json.c_str()); 397 json.c_str());
401 } 398 }
402 399
403 void FileNetLogObserver::BoundedFileWriter::Stop( 400 void FileNetLogObserver::BoundedFileWriter::Stop(
404 std::unique_ptr<base::Value> tab_info) { 401 std::unique_ptr<base::Value> polled_data) {
405 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 402 DCHECK(task_runner_->RunsTasksOnCurrentThread());
406 403
407 base::ScopedFILE closing_file( 404 base::ScopedFILE closing_file(
408 base::OpenFile(directory_.AppendASCII("end_netlog.json"), "w")); 405 base::OpenFile(directory_.AppendASCII("end_netlog.json"), "w"));
409 406
410 std::string json; 407 std::string json;
411 if (tab_info) 408 if (polled_data)
412 base::JSONWriter::Write(*tab_info, &json); 409 base::JSONWriter::Write(*polled_data, &json);
413 410
414 fprintf(closing_file.get(), "]%s}\n", 411 fprintf(closing_file.get(), "]%s}\n",
415 json.empty() ? "" : (",\"tabInfo\": " + json + "\n").c_str()); 412 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str());
416 413
417 // Flush all fprintfs to disk so that files can be safely accessed on 414 // Flush all fprintfs to disk so that files can be safely accessed on
418 // callback. 415 // callback.
419 event_files_.clear(); 416 event_files_.clear();
420 } 417 }
421 418
422 void FileNetLogObserver::BoundedFileWriter::IncrementCurrentFile() { 419 void FileNetLogObserver::BoundedFileWriter::IncrementCurrentFile() {
423 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 420 DCHECK(task_runner_->RunsTasksOnCurrentThread());
424 421
425 current_file_idx_++; 422 current_file_idx_++;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 // Print constants to file and open events array. 486 // Print constants to file and open events array.
490 std::string json; 487 std::string json;
491 488
492 // It should always be possible to convert constants to JSON. 489 // It should always be possible to convert constants to JSON.
493 if (!base::JSONWriter::Write(*constants_value, &json)) 490 if (!base::JSONWriter::Write(*constants_value, &json))
494 DCHECK(false); 491 DCHECK(false);
495 fprintf(file_.get(), "{\"constants\":%s,\n\"events\": [\n", json.c_str()); 492 fprintf(file_.get(), "{\"constants\":%s,\n\"events\": [\n", json.c_str());
496 } 493 }
497 494
498 void FileNetLogObserver::UnboundedFileWriter::Stop( 495 void FileNetLogObserver::UnboundedFileWriter::Stop(
499 std::unique_ptr<base::Value> tab_info) { 496 std::unique_ptr<base::Value> polled_data) {
500 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 497 DCHECK(task_runner_->RunsTasksOnCurrentThread());
501 498
502 std::string json; 499 std::string json;
503 if (tab_info) 500 if (polled_data)
504 base::JSONWriter::Write(*tab_info, &json); 501 base::JSONWriter::Write(*polled_data, &json);
505 502
506 fprintf(file_.get(), "]%s}\n", 503 fprintf(file_.get(), "]%s}\n",
507 json.empty() ? "" : (",\n\"tabInfo\": " + json + "\n").c_str()); 504 json.empty() ? "" : (",\n\"polledData\": " + json + "\n").c_str());
508 505
509 // Flush all fprintfs to disk so that the file can be safely accessed on 506 // Flush all fprintfs to disk so that the file can be safely accessed on
510 // callback. 507 // callback.
511 file_.reset(); 508 file_.reset();
512 } 509 }
513 510
514 void FileNetLogObserver::UnboundedFileWriter::Flush( 511 void FileNetLogObserver::UnboundedFileWriter::Flush(
515 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) { 512 scoped_refptr<FileNetLogObserver::WriteQueue> write_queue) {
516 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 513 DCHECK(task_runner_->RunsTasksOnCurrentThread());
517 514
(...skipping 14 matching lines...) Expand all
532 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { 529 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() {
533 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 530 DCHECK(task_runner_->RunsTasksOnCurrentThread());
534 531
535 // Reset |file_| to release the file handle so base::DeleteFile can 532 // Reset |file_| to release the file handle so base::DeleteFile can
536 // safely access it. 533 // safely access it.
537 file_.reset(); 534 file_.reset();
538 base::DeleteFile(file_path_, false); 535 base::DeleteFile(file_path_, false);
539 } 536 }
540 537
541 } // namespace net 538 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698