Commit 5abcbb90 authored by mef's avatar mef Committed by Commit bot

[CrNet] Properly setup net log.

BUG=493250

Review URL: https://codereview.chromium.org/1158033010

Cr-Commit-Position: refs/heads/master@{#333115}
parent d811358b
......@@ -31,8 +31,6 @@
'CrNet.mm',
'crnet_environment.h',
'crnet_environment.mm',
'crnet_net_log.h',
'crnet_net_log.cc',
],
'defines': [
# TODO(stuartmorgan): Revisit the way this is set, and the above is
......
......@@ -10,16 +10,17 @@
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#import "ios/crnet/CrNet.h"
#include "ios/crnet/crnet_net_log.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
namespace net {
class HttpCache;
class NetworkChangeNotifier;
class NetLog;
class ProxyConfigService;
class SdchManager;
class URLRequestContextGetter;
class WriteToFileNetLogObserver;
}
class CrNetHttpProtocolHandlerDelegate;
......@@ -143,9 +144,8 @@ class CrNetEnvironment {
scoped_ptr<net::URLRequestContext> main_context_;
scoped_ptr<CrNetHttpProtocolHandlerDelegate> http_protocol_handler_delegate_;
std::string user_agent_product_name_;
base::Lock net_log_lock_;
bool net_log_started_;
scoped_ptr<CrNetNetLog> net_log_;
scoped_ptr<net::NetLog> net_log_;
scoped_ptr<net::WriteToFileNetLogObserver> net_log_observer_;
DISALLOW_COPY_AND_ASSIGN(CrNetEnvironment);
};
......
......@@ -8,6 +8,9 @@
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/i18n/icu_util.h"
#include "base/json/json_writer.h"
#include "base/mac/bind_objc_block.h"
......@@ -32,6 +35,8 @@
#include "net/http/http_server_properties_impl.h"
#include "net/http/http_stream_factory.h"
#include "net/http/http_util.h"
#include "net/log/net_log.h"
#include "net/log/write_to_file_net_log_observer.h"
#include "net/proxy/proxy_service.h"
#include "net/socket/next_proto.h"
#include "net/ssl/channel_id_service.h"
......@@ -154,11 +159,6 @@ void CrNetEnvironment::Initialize() {
void CrNetEnvironment::StartNetLog(base::FilePath::StringType file_name,
bool log_bytes) {
DCHECK(file_name.length());
base::AutoLock lock(net_log_lock_);
if (net_log_started_) {
return;
}
net_log_started_ = true;
PostToFileUserBlockingThread(FROM_HERE,
base::Bind(&CrNetEnvironment::StartNetLogInternal,
base::Unretained(this), file_name, log_bytes));
......@@ -169,21 +169,31 @@ void CrNetEnvironment::StartNetLogInternal(
DCHECK(base::MessageLoop::current() ==
file_user_blocking_thread_->message_loop());
DCHECK(file_name.length());
if (!net_log_.get()) {
net_log_.reset(new CrNetNetLog());
main_context_.get()->set_net_log(net_log_.get());
}
CrNetNetLog::Mode mode = log_bytes ? CrNetNetLog::LOG_ALL_BYTES :
CrNetNetLog::LOG_STRIP_PRIVATE_DATA;
net_log_->Start(base::FilePath(file_name), mode);
DCHECK(net_log_);
if (net_log_observer_)
return;
base::FilePath temp_dir;
if (!base::GetTempDir(&temp_dir))
return;
base::FilePath full_path = temp_dir.Append(file_name);
base::ScopedFILE file(base::OpenFile(full_path, "w"));
if (!file)
return;
net::NetLogCaptureMode capture_mode = log_bytes ?
net::NetLogCaptureMode::IncludeSocketBytes() :
net::NetLogCaptureMode::Default();
net_log_observer_.reset(new net::WriteToFileNetLogObserver());
net_log_observer_->set_capture_mode(capture_mode);
net_log_observer_->StartObserving(net_log_.get(), file.Pass(), nullptr,
nullptr);
}
void CrNetEnvironment::StopNetLog() {
base::AutoLock lock(net_log_lock_);
if (!net_log_started_) {
return;
}
net_log_started_ = false;
PostToFileUserBlockingThread(FROM_HERE,
base::Bind(&CrNetEnvironment::StopNetLogInternal,
base::Unretained(this)));
......@@ -192,8 +202,9 @@ void CrNetEnvironment::StopNetLog() {
void CrNetEnvironment::StopNetLogInternal() {
DCHECK(base::MessageLoop::current() ==
file_user_blocking_thread_->message_loop());
if (net_log_.get()) {
net_log_->Stop();
if (net_log_observer_) {
net_log_observer_->StopObserving(nullptr);
net_log_observer_.reset();
}
}
......@@ -270,7 +281,6 @@ void CrNetEnvironment::Install() {
main_context_getter_ = new CrNetURLRequestContextGetter(
main_context_.get(), network_io_thread_->task_runner());
SetRequestFilterBlock(nil);
net_log_started_ = false;
}
void CrNetEnvironment::InstallIntoSessionConfiguration(
......@@ -421,6 +431,9 @@ void CrNetEnvironment::InitializeOnNetworkThread() {
job_factory->SetProtocolHandler(
"file", new net::FileProtocolHandler(file_thread_->task_runner()));
main_context_->set_job_factory(job_factory);
net_log_.reset(new net::NetLog());
main_context_->set_net_log(net_log_.get());
}
std::string CrNetEnvironment::user_agent() {
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/crnet/crnet_net_log.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/values.h"
#include "net/log/net_log.h"
#include "net/log/net_log_util.h"
#include "net/log/write_to_file_net_log_observer.h"
CrNetNetLog::CrNetNetLog() { }
CrNetNetLog::~CrNetNetLog() { }
bool CrNetNetLog::Start(const base::FilePath& log_file,
CrNetNetLog::Mode mode) {
DCHECK(thread_checker_.CalledOnValidThread());
base::FilePath temp_dir;
if (!base::GetTempDir(&temp_dir))
return false;
base::FilePath full_path = temp_dir.Append(log_file);
base::ScopedFILE file(base::OpenFile(full_path, "w"));
if (!file)
return false;
scoped_ptr<base::Value> constants(net::GetNetConstants().Pass());
net::NetLogCaptureMode capture_mode = mode == LOG_ALL_BYTES ?
net::NetLogCaptureMode::IncludeSocketBytes() :
net::NetLogCaptureMode::Default();
write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
write_to_file_observer_->set_capture_mode(capture_mode);
write_to_file_observer_->StartObserving(this, file.Pass(), constants.get(),
nullptr);
return true;
}
void CrNetNetLog::Stop() {
DCHECK(thread_checker_.CalledOnValidThread());
write_to_file_observer_->StopObserving(nullptr);
write_to_file_observer_.reset();
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CRNET_CRNET_NET_LOG_H_
#define IOS_CRNET_CRNET_NET_LOG_H_
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "net/log/net_log.h"
namespace base {
class FilePath;
class DictionaryValue;
}
namespace net {
class WriteToFileNetLogObserver;
}
// CrNet-specific NetLog subclass.
// This class can be used as a NetLog where needed; it logs all log entries to
// the file specified in Start().
class CrNetNetLog : public net::NetLog {
public:
enum Mode {
LOG_ALL_BYTES,
LOG_STRIP_PRIVATE_DATA,
};
CrNetNetLog();
~CrNetNetLog() override;
// Starts logging to the file named |log_file|. If |mode| is LOG_ALL_BYTES,
// logs all network traffic, including raw bytes. If |mode| is
// LOG_STRIP_PRIVATE_DATA, strips cookies and other private data, and does
// not log raw bytes.
bool Start(const base::FilePath& log_file, Mode mode);
// Stops logging.
void Stop();
private:
// Underlying file writer. This observer observes NetLog events and writes
// them back to the file this class is logging to.
scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
base::ThreadChecker thread_checker_;
};
#endif // IOS_CRNET_CRNET_NET_LOG_H
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