1 // Copyright (c) 2012 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 "components/net_log/chrome_net_log.h"
9 #include "base/command_line.h"
10 #include "base/files/scoped_file.h"
11 #include "base/logging.h"
12 #include "base/values.h"
13 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h"
14 #include "components/net_log/net_log_temp_file.h"
15 #include "components/net_log/net_log_temp_file.h"
16 #include "components/version_info/version_info.h"
17 #include "net/log/net_log_util.h"
18 #include "net/log/trace_net_log_observer.h"
19 #include "net/log/write_to_file_net_log_observer.h"
23 ChromeNetLog::ChromeNetLog(
24 const base::FilePath
& log_file
,
25 net::NetLogCaptureMode log_file_mode
,
26 const base::CommandLine::StringType
& command_line_string
,
27 const std::string
& channel_string
)
29 new NetLogTempFile(this, command_line_string
, channel_string
)) {
30 if (!log_file
.empty()) {
31 // Much like logging.h, bypass threading restrictions by using fopen
32 // directly. Have to write on a thread that's shutdown to handle events on
33 // shutdown properly, and posting events to another thread as they occur
34 // would result in an unbounded buffer size, so not much can be gained by
35 // doing this on another thread. It's only used when debugging Chrome, so
36 // performance is not a big concern.
37 base::ScopedFILE file
;
39 file
.reset(_wfopen(log_file
.value().c_str(), L
"w"));
40 #elif defined(OS_POSIX)
41 file
.reset(fopen(log_file
.value().c_str(), "w"));
45 LOG(ERROR
) << "Could not open file " << log_file
.value()
46 << " for net logging";
48 scoped_ptr
<base::Value
> constants(
49 GetConstants(command_line_string
, channel_string
));
50 write_to_file_observer_
.reset(new net::WriteToFileNetLogObserver());
52 write_to_file_observer_
->set_capture_mode(log_file_mode
);
54 write_to_file_observer_
->StartObserving(this, file
.Pass(),
55 constants
.get(), nullptr);
59 trace_net_log_observer_
.reset(new net::TraceNetLogObserver());
60 trace_net_log_observer_
->WatchForTraceStart(this);
63 ChromeNetLog::~ChromeNetLog() {
64 net_log_temp_file_
.reset();
65 // Remove the observers we own before we're destroyed.
66 if (write_to_file_observer_
)
67 write_to_file_observer_
->StopObserving(nullptr);
68 if (trace_net_log_observer_
)
69 trace_net_log_observer_
->StopWatchForTraceStart();
73 base::Value
* ChromeNetLog::GetConstants(
74 const base::CommandLine::StringType
& command_line_string
,
75 const std::string
& channel_string
) {
76 scoped_ptr
<base::DictionaryValue
> constants_dict
= net::GetNetConstants();
77 DCHECK(constants_dict
);
79 // Add a dictionary with the version of the client and its command line
82 base::DictionaryValue
* dict
= new base::DictionaryValue();
84 // We have everything we need to send the right values.
85 dict
->SetString("name", version_info::GetProductName());
86 dict
->SetString("version", version_info::GetVersionNumber());
87 dict
->SetString("cl", version_info::GetLastChange());
88 dict
->SetString("version_mod", channel_string
);
89 dict
->SetString("official", version_info::IsOfficialBuild() ? "official"
91 dict
->SetString("os_type", version_info::GetOSType());
92 dict
->SetString("command_line", command_line_string
);
94 constants_dict
->Set("clientInfo", dict
);
96 data_reduction_proxy::DataReductionProxyEventStore::AddConstants(
97 constants_dict
.get());
100 return constants_dict
.release();
103 } // namespace net_log