Commit dec4e769 authored by Eric Roman's avatar Eric Roman Committed by Commit Bot

Use FileNetLogObserver in Cast.

This replaces use of the deprecated WriteToFileNetLogObserver.
It has the advantage of moving file I/O off the calling thread.

Bug: 716570
Change-Id: Idac0cec3d1eb59fedc1793e2f60f9244d0324789
Reviewed-on: https://chromium-review.googlesource.com/564074Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Commit-Queue: Eric Roman <eroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485339}
parent 51456a96
...@@ -8,14 +8,15 @@ ...@@ -8,14 +8,15 @@
#include <utility> #include <utility>
#include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/scoped_file.h" #include "base/files/scoped_file.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "net/log/file_net_log_observer.h"
#include "net/log/net_log_util.h" #include "net/log/net_log_util.h"
#include "net/log/write_to_file_net_log_observer.h"
namespace chromecast { namespace chromecast {
...@@ -41,39 +42,25 @@ std::unique_ptr<base::DictionaryValue> GetShellConstants() { ...@@ -41,39 +42,25 @@ std::unique_ptr<base::DictionaryValue> GetShellConstants() {
} // namespace } // namespace
CastNetLog::CastNetLog() { CastNetLog::CastNetLog() {
// TODO(derekjchow): This code is virtually identical to ShellNetLog which is
// nearly identical to code in ChromeNetLog. Consider merging the code.
const base::CommandLine* command_line = const base::CommandLine* command_line =
base::CommandLine::ForCurrentProcess(); base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kLogNetLog)) { if (command_line->HasSwitch(switches::kLogNetLog)) {
base::FilePath log_path = base::FilePath log_path =
command_line->GetSwitchValuePath(switches::kLogNetLog); command_line->GetSwitchValuePath(switches::kLogNetLog);
// Much like logging.h, bypass threading restrictions by using fopen net::NetLogCaptureMode capture_mode = net::NetLogCaptureMode::Default();
// directly. Have to write on a thread that's shutdown to handle events on
// shutdown properly, and posting events to another thread as they occur file_net_log_observer_ =
// would result in an unbounded buffer size, so not much can be gained by net::FileNetLogObserver::CreateUnbounded(log_path, GetShellConstants());
// doing this on another thread. It's only used when debugging, so
// performance is not a big concern. file_net_log_observer_->StartObserving(this, capture_mode);
base::ScopedFILE file;
file.reset(fopen(log_path.value().c_str(), "w"));
if (!file) {
LOG(ERROR) << "Could not open file " << log_path.value()
<< " for net logging";
} else {
std::unique_ptr<base::Value> constants(GetShellConstants());
write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
write_to_file_observer_->StartObserving(this, std::move(file),
constants.get(), nullptr);
}
} }
} }
CastNetLog::~CastNetLog() { CastNetLog::~CastNetLog() {
// Remove the observer we own before we're destroyed. // Remove the observer we own before we're destroyed.
if (write_to_file_observer_) if (file_net_log_observer_)
write_to_file_observer_->StopObserving(nullptr); file_net_log_observer_->StopObserving(nullptr, base::OnceClosure());
} }
} // namespace chromecast } // namespace chromecast
...@@ -9,7 +9,11 @@ ...@@ -9,7 +9,11 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "net/log/write_to_file_net_log_observer.h" #include "net/log/net_log.h"
namespace net {
class FileNetLogObserver;
}
namespace chromecast { namespace chromecast {
...@@ -19,7 +23,7 @@ class CastNetLog : public net::NetLog { ...@@ -19,7 +23,7 @@ class CastNetLog : public net::NetLog {
~CastNetLog() override; ~CastNetLog() override;
private: private:
std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; std::unique_ptr<net::FileNetLogObserver> file_net_log_observer_;
DISALLOW_COPY_AND_ASSIGN(CastNetLog); DISALLOW_COPY_AND_ASSIGN(CastNetLog);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment