DevTools: kill codeschool extension from whitelist
[chromium-blink-merge.git] / net / log / write_to_file_net_log_observer.cc
blob9df765eb595b0840793a0d8b5bd1c69d3953f31e
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
3 // found in the LICENSE file.
5 #include "net/log/write_to_file_net_log_observer.h"
7 #include <stdio.h>
9 #include <set>
11 #include "base/json/json_writer.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "net/log/net_log_util.h"
16 #include "net/url_request/url_request_context.h"
18 namespace net {
20 WriteToFileNetLogObserver::WriteToFileNetLogObserver()
21 : capture_mode_(NetLogCaptureMode::Default()), added_events_(false) {
24 WriteToFileNetLogObserver::~WriteToFileNetLogObserver() {
27 void WriteToFileNetLogObserver::set_capture_mode(
28 net::NetLogCaptureMode capture_mode) {
29 DCHECK(!net_log());
30 capture_mode_ = capture_mode;
33 void WriteToFileNetLogObserver::StartObserving(
34 net::NetLog* net_log,
35 base::ScopedFILE file,
36 base::Value* constants,
37 net::URLRequestContext* url_request_context) {
38 DCHECK(file.get());
39 file_ = file.Pass();
40 added_events_ = false;
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
44 // between Chrome versions.
45 std::string json;
46 if (constants) {
47 base::JSONWriter::Write(constants, &json);
48 } else {
49 scoped_ptr<base::DictionaryValue> scoped_constants(GetNetConstants());
50 base::JSONWriter::Write(scoped_constants.get(), &json);
52 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
54 // Start events array. It's closed in StopObserving().
55 fprintf(file_.get(), "\"events\": [\n");
57 // Add events for in progress requests if a context is given.
58 if (url_request_context) {
59 DCHECK(url_request_context->CalledOnValidThread());
61 std::set<URLRequestContext*> contexts;
62 contexts.insert(url_request_context);
63 CreateNetLogEntriesForActiveObjects(contexts, this);
66 net_log->DeprecatedAddObserver(this, capture_mode_);
69 void WriteToFileNetLogObserver::StopObserving(
70 net::URLRequestContext* url_request_context) {
71 net_log()->DeprecatedRemoveObserver(this);
73 // End events array.
74 fprintf(file_.get(), "]");
76 // Write state of the URLRequestContext when logging stopped.
77 if (url_request_context) {
78 DCHECK(url_request_context->CalledOnValidThread());
80 std::string json;
81 scoped_ptr<base::DictionaryValue> net_info =
82 GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES);
83 base::JSONWriter::Write(net_info.get(), &json);
84 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str());
86 fprintf(file_.get(), "}");
88 file_.reset();
91 void WriteToFileNetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) {
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
94 // work, lines cannot be pretty printed.
95 scoped_ptr<base::Value> value(entry.ToValue());
96 std::string json;
97 base::JSONWriter::Write(value.get(), &json);
98 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str());
99 added_events_ = true;
102 } // namespace net