| 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 <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 base::Value* constants, | 36 base::Value* constants, |
| 37 URLRequestContext* url_request_context) { | 37 URLRequestContext* url_request_context) { |
| 38 DCHECK(file.get()); | 38 DCHECK(file.get()); |
| 39 file_ = file.Pass(); | 39 file_ = file.Pass(); |
| 40 added_events_ = false; | 40 added_events_ = false; |
| 41 | 41 |
| 42 // Write constants to the output file. This allows loading files that have | 42 // Write constants to the output file. This allows loading files that have |
| 43 // different source and event types, as they may be added and removed | 43 // different source and event types, as they may be added and removed |
| 44 // between Chrome versions. | 44 // between Chrome versions. |
| 45 std::string json; | 45 std::string json; |
| 46 if (constants) { | 46 if (constants) |
| 47 base::JSONWriter::Write(constants, &json); | 47 base::JSONWriter::Write(*constants, &json); |
| 48 } else { | 48 else |
| 49 scoped_ptr<base::DictionaryValue> scoped_constants(GetNetConstants()); | 49 base::JSONWriter::Write(*GetNetConstants(), &json); |
| 50 base::JSONWriter::Write(scoped_constants.get(), &json); | 50 |
| 51 } | |
| 52 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); | 51 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); |
| 53 | 52 |
| 54 // Start events array. It's closed in StopObserving(). | 53 // Start events array. It's closed in StopObserving(). |
| 55 fprintf(file_.get(), "\"events\": [\n"); | 54 fprintf(file_.get(), "\"events\": [\n"); |
| 56 | 55 |
| 57 // Add events for in progress requests if a context is given. | 56 // Add events for in progress requests if a context is given. |
| 58 if (url_request_context) { | 57 if (url_request_context) { |
| 59 DCHECK(url_request_context->CalledOnValidThread()); | 58 DCHECK(url_request_context->CalledOnValidThread()); |
| 60 | 59 |
| 61 std::set<URLRequestContext*> contexts; | 60 std::set<URLRequestContext*> contexts; |
| 62 contexts.insert(url_request_context); | 61 contexts.insert(url_request_context); |
| 63 CreateNetLogEntriesForActiveObjects(contexts, this); | 62 CreateNetLogEntriesForActiveObjects(contexts, this); |
| 64 } | 63 } |
| 65 | 64 |
| 66 net_log->DeprecatedAddObserver(this, capture_mode_); | 65 net_log->DeprecatedAddObserver(this, capture_mode_); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void WriteToFileNetLogObserver::StopObserving( | 68 void WriteToFileNetLogObserver::StopObserving( |
| 70 URLRequestContext* url_request_context) { | 69 URLRequestContext* url_request_context) { |
| 71 net_log()->DeprecatedRemoveObserver(this); | 70 net_log()->DeprecatedRemoveObserver(this); |
| 72 | 71 |
| 73 // End events array. | 72 // End events array. |
| 74 fprintf(file_.get(), "]"); | 73 fprintf(file_.get(), "]"); |
| 75 | 74 |
| 76 // Write state of the URLRequestContext when logging stopped. | 75 // Write state of the URLRequestContext when logging stopped. |
| 77 if (url_request_context) { | 76 if (url_request_context) { |
| 78 DCHECK(url_request_context->CalledOnValidThread()); | 77 DCHECK(url_request_context->CalledOnValidThread()); |
| 79 | 78 |
| 80 std::string json; | 79 std::string json; |
| 81 scoped_ptr<base::DictionaryValue> net_info = | 80 base::JSONWriter::Write( |
| 82 GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES); | 81 *GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES), &json); |
| 83 base::JSONWriter::Write(net_info.get(), &json); | |
| 84 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); | 82 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); |
| 85 } | 83 } |
| 86 fprintf(file_.get(), "}"); | 84 fprintf(file_.get(), "}"); |
| 87 | 85 |
| 88 file_.reset(); | 86 file_.reset(); |
| 89 } | 87 } |
| 90 | 88 |
| 91 void WriteToFileNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { | 89 void WriteToFileNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { |
| 92 // Add a comma and newline for every event but the first. Newlines are needed | 90 // 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 | 91 // so can load partial log files by just ignoring the last line. For this to |
| 94 // work, lines cannot be pretty printed. | 92 // work, lines cannot be pretty printed. |
| 95 scoped_ptr<base::Value> value(entry.ToValue()); | 93 scoped_ptr<base::Value> value(entry.ToValue()); |
| 96 std::string json; | 94 std::string json; |
| 97 base::JSONWriter::Write(value.get(), &json); | 95 base::JSONWriter::Write(*value, &json); |
| 98 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); | 96 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); |
| 99 added_events_ = true; | 97 added_events_ = true; |
| 100 } | 98 } |
| 101 | 99 |
| 102 } // namespace net | 100 } // namespace net |
| OLD | NEW |