Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "components/net_log/chrome_net_log.h" | 5 #include "components/net_log/chrome_net_log.h" |
| 6 | 6 |
| 7 #include <stdio.h> | |
| 8 #include <utility> | 7 #include <utility> |
| 9 | 8 |
| 10 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 11 #include "base/files/scoped_file.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 15 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 16 #include "base/values.h" | 13 #include "base/values.h" |
| 17 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event _store.h" | 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event _store.h" |
| 19 #include "components/net_log/net_export_file_writer.h" | 16 #include "components/net_log/net_export_file_writer.h" |
| 20 #include "components/version_info/version_info.h" | 17 #include "components/version_info/version_info.h" |
| 18 #include "net/log/file_net_log_observer.h" | |
| 21 #include "net/log/net_log_util.h" | 19 #include "net/log/net_log_util.h" |
| 22 #include "net/log/trace_net_log_observer.h" | 20 #include "net/log/trace_net_log_observer.h" |
| 23 #include "net/log/write_to_file_net_log_observer.h" | |
| 24 | 21 |
| 25 namespace net_log { | 22 namespace net_log { |
| 26 | 23 |
| 27 ChromeNetLog::ChromeNetLog() { | 24 ChromeNetLog::ChromeNetLog() { |
| 28 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); | 25 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); |
| 29 trace_net_log_observer_->WatchForTraceStart(this); | 26 trace_net_log_observer_->WatchForTraceStart(this); |
| 30 } | 27 } |
| 31 | 28 |
| 32 ChromeNetLog::~ChromeNetLog() { | 29 ChromeNetLog::~ChromeNetLog() { |
| 33 net_export_file_writer_.reset(); | 30 net_export_file_writer_.reset(); |
| 34 // Remove the observers we own before we're destroyed. | 31 ClearFileNetLogObserver(); |
| 35 if (write_to_file_observer_) | 32 trace_net_log_observer_->StopWatchForTraceStart(); |
| 36 write_to_file_observer_->StopObserving(nullptr); | |
| 37 if (trace_net_log_observer_) | |
| 38 trace_net_log_observer_->StopWatchForTraceStart(); | |
| 39 } | 33 } |
| 40 | 34 |
| 41 void ChromeNetLog::StartWritingToFile( | 35 void ChromeNetLog::StartWritingToFile( |
| 42 const base::FilePath& log_file, | 36 const base::FilePath& path, |
| 43 net::NetLogCaptureMode log_file_mode, | 37 net::NetLogCaptureMode capture_mode, |
| 44 const base::CommandLine::StringType& command_line_string, | 38 const base::CommandLine::StringType& command_line_string, |
| 45 const std::string& channel_string) { | 39 const std::string& channel_string) { |
| 46 DCHECK(!log_file.empty()); | 40 DCHECK(!path.empty()); |
| 47 | 41 |
| 48 // TODO(716570): Use common code to write NetLog to file. | 42 // TODO(739485): The log file does not contain about:flags data. |
| 43 file_net_log_observer_ = net::FileNetLogObserver::CreateUnbounded( | |
| 44 path, GetConstants(command_line_string, channel_string)); | |
| 49 | 45 |
| 50 // Much like logging.h, bypass threading restrictions by using fopen | 46 file_net_log_observer_->StartObserving(this, capture_mode); |
| 51 // directly. Have to write on a thread that's shutdown to handle events on | |
| 52 // shutdown properly, and posting events to another thread as they occur | |
| 53 // would result in an unbounded buffer size, so not much can be gained by | |
| 54 // doing this on another thread. It's only used when debugging Chrome, so | |
| 55 // performance is not a big concern. | |
| 56 base::ScopedFILE file; | |
| 57 #if defined(OS_WIN) | |
| 58 file.reset(_wfopen(log_file.value().c_str(), L"w")); | |
| 59 #elif defined(OS_POSIX) | |
| 60 file.reset(fopen(log_file.value().c_str(), "w")); | |
| 61 #endif | |
| 62 | |
| 63 if (!file) { | |
| 64 LOG(ERROR) << "Could not open file " << log_file.value() | |
| 65 << " for net logging"; | |
| 66 } else { | |
| 67 std::unique_ptr<base::Value> constants( | |
| 68 GetConstants(command_line_string, channel_string)); | |
| 69 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | |
| 70 | |
| 71 write_to_file_observer_->set_capture_mode(log_file_mode); | |
| 72 | |
| 73 write_to_file_observer_->StartObserving(this, std::move(file), | |
| 74 constants.get(), nullptr); | |
| 75 } | |
| 76 } | 47 } |
| 77 | 48 |
| 78 NetExportFileWriter* ChromeNetLog::net_export_file_writer() { | 49 NetExportFileWriter* ChromeNetLog::net_export_file_writer() { |
| 79 if (!net_export_file_writer_) | 50 if (!net_export_file_writer_) |
| 80 net_export_file_writer_ = base::WrapUnique(new NetExportFileWriter(this)); | 51 net_export_file_writer_ = base::WrapUnique(new NetExportFileWriter(this)); |
| 81 return net_export_file_writer_.get(); | 52 return net_export_file_writer_.get(); |
| 82 } | 53 } |
| 83 | 54 |
| 84 // static | 55 // static |
| 85 std::unique_ptr<base::Value> ChromeNetLog::GetConstants( | 56 std::unique_ptr<base::Value> ChromeNetLog::GetConstants( |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 108 dict->SetString("command_line", command_line_string); | 79 dict->SetString("command_line", command_line_string); |
| 109 | 80 |
| 110 constants_dict->Set("clientInfo", std::move(dict)); | 81 constants_dict->Set("clientInfo", std::move(dict)); |
| 111 | 82 |
| 112 data_reduction_proxy::DataReductionProxyEventStore::AddConstants( | 83 data_reduction_proxy::DataReductionProxyEventStore::AddConstants( |
| 113 constants_dict.get()); | 84 constants_dict.get()); |
| 114 | 85 |
| 115 return std::move(constants_dict); | 86 return std::move(constants_dict); |
| 116 } | 87 } |
| 117 | 88 |
| 89 void ChromeNetLog::ShutdownBeforeTaskScheduler() { | |
|
mmenke
2017/07/06 21:31:29
nit: ShutDown*
eroman
2017/07/06 21:57:54
Done.
| |
| 90 // TODO(eroman): Stop in-progress net_export_file_writer_ or delete its files? | |
| 91 | |
| 92 ClearFileNetLogObserver(); | |
| 93 } | |
| 94 | |
| 95 void ChromeNetLog::ClearFileNetLogObserver() { | |
| 96 if (file_net_log_observer_) { | |
|
mmenke
2017/07/06 21:31:29
nit: Suggest early exit here (Think it's unlikely
eroman
2017/07/06 21:57:54
Done.
| |
| 97 // TODO(739487): The log file does not contain any polled data. | |
| 98 // | |
| 99 // TODO(eroman): FileNetLogObserver::StopObserving() posts to the file task | |
| 100 // runner to finish writing the log file. Despite that sequenced task runner | |
| 101 // being marked BLOCK_SHUTDOWN, those tasks are not actually running. | |
| 102 // | |
| 103 // This isn't a big deal when using the unbounded logger since the log | |
| 104 // loading code can handle such truncated logs. But this will need fixing | |
| 105 // if switching to log formats that are not so versatile (also if adding | |
| 106 // polled data). | |
| 107 file_net_log_observer_->StopObserving(nullptr /*polled_data*/, | |
| 108 base::Closure()); | |
| 109 file_net_log_observer_.reset(); | |
| 110 } | |
| 111 } | |
| 112 | |
| 118 } // namespace net_log | 113 } // namespace net_log |
| OLD | NEW |