This commit is contained in:
Cheng Zhao 2015-08-11 18:29:55 +08:00
Родитель edda9d0488
Коммит 8a92ef34a7
8 изменённых файлов: 46 добавлений и 79 удалений

Просмотреть файл

@ -63,7 +63,9 @@ net::URLRequestContextGetter* BrowserClient::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors) {
auto context = static_cast<BrowserContext*>(browser_context);
return context->CreateRequestContext(protocol_handlers, protocol_interceptors.Pass());
return context->CreateRequestContext(static_cast<NetLog*>(GetNetLog()),
protocol_handlers,
protocol_interceptors.Pass());
}
content::MediaObserver* BrowserClient::GetMediaObserver() {
@ -81,7 +83,7 @@ void BrowserClient::GetAdditionalAllowedSchemesForFileSystem(
}
net::NetLog* BrowserClient::GetNetLog() {
return browser_context()->GetNetLog();
return &net_log_;
}
base::FilePath BrowserClient::GetDefaultDownloadDirectory() {

Просмотреть файл

@ -5,12 +5,14 @@
#ifndef BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_
#define BRIGHTRAY_BROWSER_BROWSER_CLIENT_H_
#include "browser/net_log.h"
#include "content/public/browser/content_browser_client.h"
namespace brightray {
class BrowserContext;
class BrowserMainParts;
class NetLog;
class BrowserClient : public content::ContentBrowserClient {
public:
@ -35,8 +37,6 @@ class BrowserClient : public content::ContentBrowserClient {
content::BrowserContext* browser_context,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors) override;
private:
content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams&) override;
content::MediaObserver* GetMediaObserver() override;
@ -46,7 +46,6 @@ class BrowserClient : public content::ContentBrowserClient {
net::NetLog* GetNetLog() override;
base::FilePath GetDefaultDownloadDirectory() override;
content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
void GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
@ -61,6 +60,8 @@ class BrowserClient : public content::ContentBrowserClient {
base::ScopedFD v8_snapshot_fd_;
#endif // OS_POSIX && !OS_MACOSX
NetLog net_log_;
DISALLOW_COPY_AND_ASSIGN(BrowserClient);
};

Просмотреть файл

@ -100,11 +100,13 @@ void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
}
net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
NetLog* net_log,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors) {
DCHECK(!url_request_getter_.get());
url_request_getter_ = new URLRequestContextGetter(
this,
net_log,
GetPath(),
BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
@ -114,10 +116,6 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
return url_request_getter_.get();
}
net::NetLog* BrowserContext::GetNetLog() {
return url_request_getter_->net_log();
}
net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() {
return new NetworkDelegate;
}

Просмотреть файл

@ -46,9 +46,9 @@ class BrowserContext : public content::BrowserContext,
content::PermissionManager* GetPermissionManager() override;
net::URLRequestContextGetter* CreateRequestContext(
NetLog* net_log,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors);
net::NetLog* GetNetLog();
net::URLRequestContextGetter* url_request_context_getter() const {
return url_request_getter_.get();

Просмотреть файл

@ -4,15 +4,13 @@
#include "browser/net_log.h"
#include "browser/browser_context.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/files/file_path.h"
#include "base/values.h"
#include "content/public/common/content_switches.h"
#include "net/log/net_log_util.h"
#include "net/log/net_log_capture_mode.h"
#include "net/url_request/url_request_context.h"
namespace brightray {
namespace {
@ -31,62 +29,34 @@ scoped_ptr<base::DictionaryValue> GetConstants() {
} // namespace
namespace brightray {
NetLog::NetLog(net::URLRequestContext* context)
: added_events_(false),
context_(context) {
NetLog::NetLog() {
auto command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kLogNetLog)) {
base::FilePath log_path =
command_line->GetSwitchValuePath(switches::kLogNetLog);
if (!command_line->HasSwitch(switches::kLogNetLog))
return;
#if defined(OS_WIN)
log_file_.reset(_wfopen(log_path.value().c_str(), L"w"));
#elif defined(OS_POSIX)
log_file_.reset(fopen(log_path.value().c_str(), "w"));
#endif
base::FilePath log_path = command_line->GetSwitchValuePath(switches::kLogNetLog);
#if defined(OS_WIN)
log_file_.reset(_wfopen(log_path.value().c_str(), L"w"));
#elif defined(OS_POSIX)
log_file_.reset(fopen(log_path.value().c_str(), "w"));
#endif
if (!log_file_) {
LOG(ERROR) << "Could not open file: " << log_path.value()
<< "for net logging";
} else {
std::string json;
scoped_ptr<base::Value> constants(GetConstants().Pass());
base::JSONWriter::Write(constants.get(), &json);
fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str());
fprintf(log_file_.get(), "\"events\": [\n");
if (context_) {
DCHECK(context_->CalledOnValidThread());
std::set<net::URLRequestContext*> contexts;
contexts.insert(context_);
net::CreateNetLogEntriesForActiveObjects(contexts, this);
}
DeprecatedAddObserver(this, net::NetLogCaptureMode::Default());
}
if (!log_file_) {
LOG(ERROR) << "Could not open file: " << log_path.value()
<< "for net logging";
return;
}
}
NetLog::~NetLog() {
if (log_file_) {
DeprecatedRemoveObserver(this);
// Ending events array.
fprintf(log_file_.get(), "]}");
log_file_.reset();
}
}
void NetLog::OnAddEntry(const net::NetLog::Entry& entry) {
std::string json;
base::JSONWriter::Write(entry.ToValue(), &json);
void NetLog::StartLogging(net::URLRequestContext* url_request_context) {
if (!log_file_)
return;
fprintf(log_file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str());
added_events_ = true;
scoped_ptr<base::Value> constants(GetConstants());
write_to_file_observer_.StartObserving(this, log_file_.Pass(), constants.get(), url_request_context);
}
} // namespace brightray

Просмотреть файл

@ -5,29 +5,22 @@
#ifndef BROWSER_NET_LOG_H_
#define BROWSER_NET_LOG_H_
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "net/log/net_log.h"
namespace net {
class URLRequestContext;
}
#include "net/log/write_to_file_net_log_observer.h"
namespace brightray {
class NetLog : public net::NetLog,
public net::NetLog::ThreadSafeObserver {
class NetLog : public net::NetLog {
public:
explicit NetLog(net::URLRequestContext* context);
NetLog();
~NetLog() override;
void OnAddEntry(const net::NetLog::Entry& entry) override;
void StartLogging(net::URLRequestContext* url_request_context);
private:
bool added_events_;
// We use raw pointer to prevent reference cycle.
net::URLRequestContext* const context_;
base::ScopedFILE log_file_;
net::WriteToFileNetLogObserver write_to_file_observer_;
DISALLOW_COPY_AND_ASSIGN(NetLog);
};

Просмотреть файл

@ -119,12 +119,14 @@ URLRequestContextGetter::Delegate::CreateHttpCacheBackendFactory(const base::Fil
URLRequestContextGetter::URLRequestContextGetter(
Delegate* delegate,
NetLog* net_log,
const base::FilePath& base_path,
base::MessageLoop* io_loop,
base::MessageLoop* file_loop,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors)
: delegate_(delegate),
net_log_(net_log),
base_path_(base_path),
io_loop_(io_loop),
file_loop_(file_loop),
@ -157,8 +159,8 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
url_request_context_.reset(new net::URLRequestContext);
// --log-net-log
net_log_.reset(new NetLog(url_request_context_.get()));
url_request_context_->set_net_log(net_log_.get());
net_log_->StartLogging(url_request_context_.get());
url_request_context_->set_net_log(net_log_);
network_delegate_.reset(delegate_->CreateNetworkDelegate());
url_request_context_->set_network_delegate(network_delegate_.get());

Просмотреть файл

@ -17,7 +17,6 @@ class MessageLoop;
}
namespace net {
class NetLog;
class HostMappingRules;
class HostResolver;
class NetworkDelegate;
@ -28,6 +27,8 @@ class URLRequestJobFactory;
namespace brightray {
class NetLog;
class URLRequestContextGetter : public net::URLRequestContextGetter {
public:
class Delegate {
@ -46,6 +47,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
URLRequestContextGetter(
Delegate* delegate,
NetLog* net_log,
const base::FilePath& base_path,
base::MessageLoop* io_loop,
base::MessageLoop* file_loop,
@ -58,17 +60,16 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const override;
net::HostResolver* host_resolver();
net::NetLog* net_log() { return net_log_.get(); }
private:
Delegate* delegate_;
NetLog* net_log_;
base::FilePath base_path_;
base::MessageLoop* io_loop_;
base::MessageLoop* file_loop_;
scoped_ptr<net::ProxyConfigService> proxy_config_service_;
scoped_ptr<net::NetLog> net_log_;
scoped_ptr<net::NetworkDelegate> network_delegate_;
scoped_ptr<net::URLRequestContextStorage> storage_;
scoped_ptr<net::URLRequestContext> url_request_context_;