| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/write_to_file_net_log_observer.h" | 5 #include "net/log/write_to_file_net_log_observer.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 else | 50 else |
| 51 base::JSONWriter::Write(*GetNetConstants(), &json); | 51 base::JSONWriter::Write(*GetNetConstants(), &json); |
| 52 | 52 |
| 53 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); | 53 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); |
| 54 | 54 |
| 55 // Start events array. It's closed in StopObserving(). | 55 // Start events array. It's closed in StopObserving(). |
| 56 fprintf(file_.get(), "\"events\": [\n"); | 56 fprintf(file_.get(), "\"events\": [\n"); |
| 57 | 57 |
| 58 // Add events for in progress requests if a context is given. | 58 // Add events for in progress requests if a context is given. |
| 59 if (url_request_context) { | 59 if (url_request_context) { |
| 60 DCHECK(url_request_context->CalledOnValidThread()); | 60 url_request_context->AssertCalledOnValidSequence(); |
| 61 | 61 |
| 62 std::set<URLRequestContext*> contexts; | 62 std::set<URLRequestContext*> contexts; |
| 63 contexts.insert(url_request_context); | 63 contexts.insert(url_request_context); |
| 64 CreateNetLogEntriesForActiveObjects(contexts, this); | 64 CreateNetLogEntriesForActiveObjects(contexts, this); |
| 65 } | 65 } |
| 66 | 66 |
| 67 net_log->DeprecatedAddObserver(this, capture_mode_); | 67 net_log->DeprecatedAddObserver(this, capture_mode_); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void WriteToFileNetLogObserver::StopObserving( | 70 void WriteToFileNetLogObserver::StopObserving( |
| 71 URLRequestContext* url_request_context) { | 71 URLRequestContext* url_request_context) { |
| 72 net_log()->DeprecatedRemoveObserver(this); | 72 net_log()->DeprecatedRemoveObserver(this); |
| 73 | 73 |
| 74 // End events array. | 74 // End events array. |
| 75 fprintf(file_.get(), "]"); | 75 fprintf(file_.get(), "]"); |
| 76 | 76 |
| 77 // Write state of the URLRequestContext when logging stopped. | 77 // Write state of the URLRequestContext when logging stopped. |
| 78 if (url_request_context) { | 78 if (url_request_context) { |
| 79 DCHECK(url_request_context->CalledOnValidThread()); | 79 url_request_context->AssertCalledOnValidSequence(); |
| 80 | 80 |
| 81 std::string json; | 81 std::string json; |
| 82 base::JSONWriter::Write( | 82 base::JSONWriter::Write( |
| 83 *GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES), &json); | 83 *GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES), &json); |
| 84 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); | 84 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); |
| 85 } | 85 } |
| 86 fprintf(file_.get(), "}"); | 86 fprintf(file_.get(), "}"); |
| 87 | 87 |
| 88 file_.reset(); | 88 file_.reset(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void WriteToFileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { | 91 void WriteToFileNetLogObserver::OnAddEntry(const NetLogEntry& entry) { |
| 92 // Add a comma and newline for every event but the first. Newlines are needed | 92 // Add a comma and newline for every event but the first. Newlines are needed |
| 93 // so can load partial log files by just ignoring the last line. For this to | 93 // so can load partial log files by just ignoring the last line. For this to |
| 94 // work, lines cannot be pretty printed. | 94 // work, lines cannot be pretty printed. |
| 95 std::unique_ptr<base::Value> value(entry.ToValue()); | 95 std::unique_ptr<base::Value> value(entry.ToValue()); |
| 96 std::string json; | 96 std::string json; |
| 97 base::JSONWriter::Write(*value, &json); | 97 base::JSONWriter::Write(*value, &json); |
| 98 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); | 98 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); |
| 99 added_events_ = true; | 99 added_events_ = true; |
| 100 } | 100 } |
| 101 | 101 |
| 102 } // namespace net | 102 } // namespace net |
| OLD | NEW |