diff options
author | derekjchow <derekjchow@chromium.org> | 2015-06-26 11:50:22 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-26 18:51:16 +0000 |
commit | 8ac56a2a2a14877b49d56a19a2e3396bca48a207 (patch) | |
tree | 1c72c852e7f069aa18e55731603838d0c763d05d | |
parent | 382159069f616f40c6eacbf7699d0843b288176e (diff) | |
download | chromium_src-8ac56a2a2a14877b49d56a19a2e3396bca48a207.zip chromium_src-8ac56a2a2a14877b49d56a19a2e3396bca48a207.tar.gz chromium_src-8ac56a2a2a14877b49d56a19a2e3396bca48a207.tar.bz2 |
[Chromecast] Add net logging.
Adds ShellNetLog to URLRequestContextGetters.
BUG=internal b/21900278
Review URL: https://codereview.chromium.org/1216443002
Cr-Commit-Position: refs/heads/master@{#336426}
-rw-r--r-- | chromecast/browser/cast_browser_main_parts.cc | 6 | ||||
-rw-r--r-- | chromecast/browser/cast_browser_main_parts.h | 5 | ||||
-rw-r--r-- | chromecast/browser/cast_net_log.cc | 75 | ||||
-rw-r--r-- | chromecast/browser/cast_net_log.h | 28 | ||||
-rw-r--r-- | chromecast/browser/url_request_context_factory.cc | 7 | ||||
-rw-r--r-- | chromecast/browser/url_request_context_factory.h | 5 | ||||
-rw-r--r-- | chromecast/chromecast.gyp | 2 |
7 files changed, 124 insertions, 4 deletions
diff --git a/chromecast/browser/cast_browser_main_parts.cc b/chromecast/browser/cast_browser_main_parts.cc index a841a11..32489fb 100644 --- a/chromecast/browser/cast_browser_main_parts.cc +++ b/chromecast/browser/cast_browser_main_parts.cc @@ -25,6 +25,7 @@ #include "chromecast/base/metrics/grouped_histogram.h" #include "chromecast/browser/cast_browser_context.h" #include "chromecast/browser/cast_browser_process.h" +#include "chromecast/browser/cast_net_log.h" #include "chromecast/browser/devtools/remote_debugging_server.h" #include "chromecast/browser/metrics/cast_metrics_prefs.h" #include "chromecast/browser/metrics/cast_metrics_service_client.h" @@ -209,7 +210,8 @@ CastBrowserMainParts::CastBrowserMainParts( cast_browser_process_(new CastBrowserProcess()), parameters_(parameters), url_request_context_factory_(url_request_context_factory), - audio_manager_factory_(audio_manager_factory.Pass()) { + audio_manager_factory_(audio_manager_factory.Pass()), + net_log_(new CastNetLog()) { base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); AddDefaultCommandLineSwitches(command_line); } @@ -305,7 +307,7 @@ void CastBrowserMainParts::PreMainMessageLoopRun() { content::BrowserThread::GetMessageLoopProxyForThread( content::BrowserThread::IO))); - url_request_context_factory_->InitializeOnUIThread(); + url_request_context_factory_->InitializeOnUIThread(net_log_.get()); cast_browser_process_->SetBrowserContext( make_scoped_ptr(new CastBrowserContext(url_request_context_factory_))); diff --git a/chromecast/browser/cast_browser_main_parts.h b/chromecast/browser/cast_browser_main_parts.h index 49954ce..4b5b5d8 100644 --- a/chromecast/browser/cast_browser_main_parts.h +++ b/chromecast/browser/cast_browser_main_parts.h @@ -15,6 +15,10 @@ namespace media { class AudioManagerFactory; } +namespace net { +class NetLog; +} + namespace chromecast { namespace shell { class CastBrowserProcess; @@ -42,6 +46,7 @@ class CastBrowserMainParts : public content::BrowserMainParts { const content::MainFunctionParams parameters_; // For running browser tests. URLRequestContextFactory* const url_request_context_factory_; scoped_ptr<::media::AudioManagerFactory> audio_manager_factory_; + scoped_ptr<net::NetLog> net_log_; DISALLOW_COPY_AND_ASSIGN(CastBrowserMainParts); }; diff --git a/chromecast/browser/cast_net_log.cc b/chromecast/browser/cast_net_log.cc new file mode 100644 index 0000000..d1c62d4 --- /dev/null +++ b/chromecast/browser/cast_net_log.cc @@ -0,0 +1,75 @@ +// 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 "chromecast/browser/cast_net_log.h" + +#include <stdio.h> + +#include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/files/scoped_file.h" +#include "base/values.h" +#include "content/public/common/content_switches.h" +#include "net/log/net_log_util.h" +#include "net/log/write_to_file_net_log_observer.h" + +namespace chromecast { + +namespace { + +base::DictionaryValue* GetShellConstants() { + scoped_ptr<base::DictionaryValue> constants_dict = net::GetNetConstants(); + + // Add a dictionary with client information + base::DictionaryValue* dict = new base::DictionaryValue(); + + dict->SetString("name", "cast_shell"); + dict->SetString( + "command_line", + base::CommandLine::ForCurrentProcess()->GetCommandLineString()); + + constants_dict->Set("clientInfo", dict); + + return constants_dict.release(); +} + +} // namespace + +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 = + base::CommandLine::ForCurrentProcess(); + + if (command_line->HasSwitch(switches::kLogNetLog)) { + base::FilePath log_path = + command_line->GetSwitchValuePath(switches::kLogNetLog); + // Much like logging.h, bypass threading restrictions by using fopen + // 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 + // would result in an unbounded buffer size, so not much can be gained by + // doing this on another thread. It's only used when debugging, so + // performance is not a big concern. + 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 { + scoped_ptr<base::Value> constants(GetShellConstants()); + write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); + write_to_file_observer_->StartObserving(this, file.Pass(), + constants.get(), nullptr); + } + } +} + +CastNetLog::~CastNetLog() { + // Remove the observer we own before we're destroyed. + if (write_to_file_observer_) + write_to_file_observer_->StopObserving(nullptr); +} + +} // namespace chromecast diff --git a/chromecast/browser/cast_net_log.h b/chromecast/browser/cast_net_log.h new file mode 100644 index 0000000..6be2b5b3 --- /dev/null +++ b/chromecast/browser/cast_net_log.h @@ -0,0 +1,28 @@ +// 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 CHROMECAST_BROWSER_CAST_NET_LOG_H_ +#define CHROMECAST_BROWSER_CAST_NET_LOG_H_ + +#include <string> + +#include "base/memory/scoped_ptr.h" +#include "net/log/write_to_file_net_log_observer.h" + +namespace chromecast { + +class CastNetLog : public net::NetLog { + public: + CastNetLog(); + ~CastNetLog() override; + + private: + scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; + + DISALLOW_COPY_AND_ASSIGN(CastNetLog); +}; + +} // namespace chromecast + +#endif // CHROMECAST_BROWSER_CAST_NET_LOG_H_ diff --git a/chromecast/browser/url_request_context_factory.cc b/chromecast/browser/url_request_context_factory.cc index 2c389a9..693a220 100644 --- a/chromecast/browser/url_request_context_factory.cc +++ b/chromecast/browser/url_request_context_factory.cc @@ -143,7 +143,7 @@ URLRequestContextFactory::URLRequestContextFactory() URLRequestContextFactory::~URLRequestContextFactory() { } -void URLRequestContextFactory::InitializeOnUIThread() { +void URLRequestContextFactory::InitializeOnUIThread(net::NetLog* net_log) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); // Cast http user agent settings must be initialized in UI thread // because it registers itself to pref notification observer which is not @@ -157,6 +157,8 @@ void URLRequestContextFactory::InitializeOnUIThread() { content::BrowserThread::IO), content::BrowserThread::GetMessageLoopProxyForThread( content::BrowserThread::FILE))); + + net_log_ = net_log; } net::URLRequestContextGetter* URLRequestContextFactory::CreateMainGetter( @@ -328,6 +330,7 @@ net::URLRequestContext* URLRequestContextFactory::CreateSystemRequestContext() { system_context->set_cookie_store( content::CreateCookieStore(content::CookieStoreConfig())); system_context->set_network_delegate(system_network_delegate_.get()); + system_context->set_net_log(net_log_); return system_context; } @@ -347,6 +350,7 @@ net::URLRequestContext* URLRequestContextFactory::CreateMediaRequestContext() { media_context->CopyFrom(main_context); media_context->set_http_transaction_factory( media_transaction_factory_.get()); + media_context->set_net_log(net_log_); return media_context; } @@ -399,6 +403,7 @@ net::URLRequestContext* URLRequestContextFactory::CreateMainRequestContext( main_transaction_factory_.get()); main_context->set_job_factory(main_job_factory_.get()); main_context->set_network_delegate(app_network_delegate_.get()); + main_context->set_net_log(net_log_); return main_context; } diff --git a/chromecast/browser/url_request_context_factory.h b/chromecast/browser/url_request_context_factory.h index 5ae43cb..c0d881d 100644 --- a/chromecast/browser/url_request_context_factory.h +++ b/chromecast/browser/url_request_context_factory.h @@ -11,6 +11,7 @@ namespace net { class HttpTransactionFactory; class HttpUserAgentSettings; +class NetLog; class ProxyConfigService; class URLRequestJobFactory; } // namespace net @@ -25,7 +26,7 @@ class URLRequestContextFactory { ~URLRequestContextFactory(); // Some members must be initialized on UI thread. - void InitializeOnUIThread(); + void InitializeOnUIThread(net::NetLog* net_log); // Since main context requires a bunch of input params, if these get called // multiple times, either multiple main contexts should be supported/managed @@ -113,6 +114,8 @@ class URLRequestContextFactory { bool media_dependencies_initialized_; scoped_ptr<net::HttpTransactionFactory> media_transaction_factory_; + + net::NetLog* net_log_; }; } // namespace shell diff --git a/chromecast/chromecast.gyp b/chromecast/chromecast.gyp index 61b5336..181634c 100644 --- a/chromecast/chromecast.gyp +++ b/chromecast/chromecast.gyp @@ -273,6 +273,8 @@ 'browser/cast_download_manager_delegate.h', 'browser/cast_http_user_agent_settings.cc', 'browser/cast_http_user_agent_settings.h', + 'browser/cast_net_log.cc', + 'browser/cast_net_log.h', 'browser/cast_network_delegate.cc', 'browser/cast_network_delegate.h', 'browser/cast_permission_manager.cc', |