Use FileNetLogObserver in the net-internals browser_tests.
Bug: 716570
Change-Id: I9a81dff8ed17ff9d61b89c9ef887a918770a80a9
Reviewed-on: https://chromium-review.googlesource.com/563862
Reviewed-by: Matt Menke <mmenke@chromium.org>
Commit-Queue: Eric Roman <eroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485386}
diff --git a/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc b/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc
index 7ab14c99..c03a049 100644
--- a/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc
+++ b/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc
@@ -42,11 +42,11 @@
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_network_session.h"
#include "net/http/http_transaction_factory.h"
+#include "net/log/file_net_log_observer.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source_type.h"
#include "net/log/net_log_with_source.h"
-#include "net/log/write_to_file_net_log_observer.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "net/url_request/url_request_context.h"
@@ -91,6 +91,11 @@
ttl);
}
+struct WriteNetLogState {
+ base::ScopedTempDir temp_directory;
+ base::FilePath log_path;
+};
+
} // namespace
////////////////////////////////////////////////////////////////////////////////
@@ -140,14 +145,17 @@
// Closes an incognito browser created with CreateIncognitoBrowser.
void CloseIncognitoBrowser(const base::ListValue* list_value);
- // Creates a simple log using WriteToFileNetLogObserver, and returns it to
- // the Javascript callback.
+ // Creates a simple NetLog and returns it to the Javascript callback.
void GetNetLogFileContents(const base::ListValue* list_value);
// Changes the data reduction proxy mode. A boolean is assumed to exist at
// index 0 which enables the proxy is set to true.
void EnableDataReductionProxy(const base::ListValue* list_value);
+ // Called after the NetLog started by GetNetLogFileContents() has been written
+ // to disk. Responds to the Javascript caller with the log contents.
+ void OnFinishedWritingNetLog(std::unique_ptr<WriteNetLogState> state);
+
Browser* browser() { return net_internals_test_->browser(); }
NetInternalsTest* net_internals_test_;
@@ -292,37 +300,37 @@
void NetInternalsTest::MessageHandler::GetNetLogFileContents(
const base::ListValue* list_value) {
base::ThreadRestrictions::ScopedAllowIO allow_io;
- base::ScopedTempDir temp_directory;
- ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
- base::FilePath temp_file;
- ASSERT_TRUE(
- base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
- base::ScopedFILE temp_file_handle(base::OpenFile(temp_file, "w"));
- ASSERT_TRUE(temp_file_handle);
+
+ std::unique_ptr<WriteNetLogState> state =
+ base::MakeUnique<WriteNetLogState>();
+
+ ASSERT_TRUE(state->temp_directory.CreateUniqueTempDir());
+ ASSERT_TRUE(base::CreateTemporaryFileInDir(state->temp_directory.GetPath(),
+ &state->log_path));
std::unique_ptr<base::Value> constants(net_log::ChromeNetLog::GetConstants(
base::CommandLine::ForCurrentProcess()->GetCommandLineString(),
chrome::GetChannelString()));
- std::unique_ptr<net::WriteToFileNetLogObserver> net_log_logger(
- new net::WriteToFileNetLogObserver());
+
+ std::unique_ptr<net::FileNetLogObserver> net_log_logger =
+ net::FileNetLogObserver::CreateUnbounded(state->log_path,
+ std::move(constants));
+
net_log_logger->StartObserving(g_browser_process->net_log(),
- std::move(temp_file_handle), constants.get(),
- nullptr);
+ net::NetLogCaptureMode::Default());
+
g_browser_process->net_log()->AddGlobalEntry(
net::NetLogEventType::NETWORK_IP_ADDRESSES_CHANGED);
net::NetLogWithSource net_log_with_source = net::NetLogWithSource::Make(
g_browser_process->net_log(), net::NetLogSourceType::URL_REQUEST);
net_log_with_source.BeginEvent(net::NetLogEventType::REQUEST_ALIVE);
- net_log_logger->StopObserving(nullptr);
- net_log_logger.reset();
- std::string log_contents;
- ASSERT_TRUE(base::ReadFileToString(temp_file, &log_contents));
- ASSERT_GT(log_contents.length(), 0u);
-
- std::unique_ptr<base::Value> log_contents_value(
- new base::Value(log_contents));
- RunJavascriptCallback(log_contents_value.get());
+ // Call OnFinishedWritingNetLog() once net_log_logger has completed writing it
+ // to disk.
+ net_log_logger->StopObserving(
+ nullptr,
+ base::Bind(&NetInternalsTest::MessageHandler::OnFinishedWritingNetLog,
+ base::Unretained(this), base::Passed(std::move(state))));
}
void NetInternalsTest::MessageHandler::EnableDataReductionProxy(
@@ -333,6 +341,21 @@
prefs::kDataSaverEnabled, enable);
}
+void NetInternalsTest::MessageHandler::OnFinishedWritingNetLog(
+ std::unique_ptr<WriteNetLogState> state) {
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+
+ std::string log_contents;
+ ASSERT_TRUE(base::ReadFileToString(state->log_path, &log_contents));
+ ASSERT_GT(log_contents.length(), 0u);
+
+ std::unique_ptr<base::Value> log_contents_value(
+ new base::Value(log_contents));
+ RunJavascriptCallback(log_contents_value.get());
+
+ state.reset();
+}
+
////////////////////////////////////////////////////////////////////////////////
// NetInternalsTest
////////////////////////////////////////////////////////////////////////////////
diff --git a/chrome/test/data/webui/net_internals/log_util.js b/chrome/test/data/webui/net_internals/log_util.js
index b43fadf..b8e72f3 100644
--- a/chrome/test/data/webui/net_internals/log_util.js
+++ b/chrome/test/data/webui/net_internals/log_util.js
@@ -93,12 +93,11 @@
};
/**
- * A Task that creates a log dump in the browser process via
- * WriteToFileNetLogObserver, waits to receive it via IPC, and and then loads
- * it as a string.
+ * A Task that creates a log dump in the browser process, waits to receive it
+ * via IPC, and and then loads it as a string.
* @param {integer} truncate The number of bytes to truncate from the end of
* the string, if any, to simulate a truncated log due to crash, or
- * quitting without properly shutting down a WriteToFileNetLogObserver.
+ * quitting without properly shutting down the log writer.
* @extends {NetInternalsTest.Task}
*/
function GetNetLogFileContentsAndLoadLogTask(truncate) {
@@ -178,7 +177,7 @@
/**
* Checks the visibility of each view after loading a log dump created by the
- * WriteToFileNetLogObserver. Also checks that the BrowserBridge is disabled.
+ * browser. Also checks that the BrowserBridge is disabled.
*/
function checkViewsAfterNetLogFileLoaded() {
expectTrue(g_browser.isDisabled());
@@ -235,9 +234,8 @@
});
/**
- * Exports a log dump by using a WriteToFileNetLogObserver and attempts to load
- * it from a string. The string is passed to Javascript via an IPC rather than
- * drag and drop.
+ * Attempts to load a NetLog created by the browser. The log contents are
+ * passed to Javascript via an IPC rather than drag and drop.
*/
TEST_F('NetInternalsTest',
'netInternalsLogUtilImportNetLogFile',
diff --git a/net/log/file_net_log_observer.h b/net/log/file_net_log_observer.h
index fc1fd8a..747b76a2 100644
--- a/net/log/file_net_log_observer.h
+++ b/net/log/file_net_log_observer.h
@@ -83,7 +83,9 @@
// Stops observing net_log() and closes the output file(s). Must be called
// after StartObserving. Should be called before destruction of the
// FileNetLogObserver and the NetLog, or the NetLog files will be deleted when
- // the observer is destroyed.
+ // the observer is destroyed. Note that it is OK to destroy |this| immediately
+ // after calling StopObserving() - the callback will still be called once the
+ // file writing has completed.
//
// |polled_data| is an optional argument used to add additional network stack
// state to the log.