Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / log / net_log_logger.cc
blob4a2b475d95aa65c7b790e4df8704897944c67ef8
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/net_log_logger.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 NetLogLogger::NetLogLogger()
21 : log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), added_events_(false) {
24 NetLogLogger::~NetLogLogger() {
27 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) {
28 DCHECK(!net_log());
29 log_level_ = log_level;
32 void NetLogLogger::StartObserving(net::NetLog* net_log,
33 base::ScopedFILE file,
34 base::Value* constants,
35 net::URLRequestContext* url_request_context) {
36 DCHECK(file.get());
37 file_ = file.Pass();
38 added_events_ = false;
40 // Write constants to the output file. This allows loading files that have
41 // different source and event types, as they may be added and removed
42 // between Chrome versions.
43 std::string json;
44 if (constants) {
45 base::JSONWriter::Write(constants, &json);
46 } else {
47 scoped_ptr<base::DictionaryValue> scoped_constants(GetNetConstants());
48 base::JSONWriter::Write(scoped_constants.get(), &json);
50 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
52 // Start events array. It's closed in StopObserving().
53 fprintf(file_.get(), "\"events\": [\n");
55 // Add events for in progress requests if a context is given.
56 if (url_request_context) {
57 DCHECK(url_request_context->CalledOnValidThread());
59 std::set<URLRequestContext*> contexts;
60 contexts.insert(url_request_context);
61 CreateNetLogEntriesForActiveObjects(contexts, this);
64 net_log->DeprecatedAddObserver(this, log_level_);
67 void NetLogLogger::StopObserving(net::URLRequestContext* url_request_context) {
68 net_log()->DeprecatedRemoveObserver(this);
70 // End events array.
71 fprintf(file_.get(), "]");
73 // Write state of the URLRequestContext when logging stopped.
74 if (url_request_context) {
75 DCHECK(url_request_context->CalledOnValidThread());
77 std::string json;
78 scoped_ptr<base::DictionaryValue> net_info =
79 GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES);
80 base::JSONWriter::Write(net_info.get(), &json);
81 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str());
83 fprintf(file_.get(), "}");
85 file_.reset();
88 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
89 // Add a comma and newline for every event but the first. Newlines are needed
90 // so can load partial log files by just ignoring the last line. For this to
91 // work, lines cannot be pretty printed.
92 scoped_ptr<base::Value> value(entry.ToValue());
93 std::string json;
94 base::JSONWriter::Write(value.get(), &json);
95 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str());
96 added_events_ = true;
99 } // namespace net