Commit a96ce4b0 authored by mmenke@chromium.org's avatar mmenke@chromium.org

Update about:net-internals to use Javascript for loading logs.

BUG=none
TEST=manual

Review URL: http://codereview.chromium.org/6995086

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88460 0039d316-1c4b-4281-b951-d872f2087c98
parent 5785c113
...@@ -53,8 +53,9 @@ function DataView(mainBoxId, ...@@ -53,8 +53,9 @@ function DataView(mainBoxId,
this.capturingTextSpan_ = document.getElementById(capturingTextSpanId); this.capturingTextSpan_ = document.getElementById(capturingTextSpanId);
this.loggingTextSpan_ = document.getElementById(loggingTextSpanId); this.loggingTextSpan_ = document.getElementById(loggingTextSpanId);
document.getElementById(loadLogFileId).onclick = var loadLogFileElement = document.getElementById(loadLogFileId);
g_browser.loadLogFile.bind(g_browser); loadLogFileElement.onchange =
this.logFileChanged.bind(this, loadLogFileElement);
this.updateEventCounts_(); this.updateEventCounts_();
this.waitingForUpdate_ = false; this.waitingForUpdate_ = false;
...@@ -136,6 +137,37 @@ DataView.prototype.onSecurityStrippingChanged = function() { ...@@ -136,6 +137,37 @@ DataView.prototype.onSecurityStrippingChanged = function() {
this.setText_(''); this.setText_('');
} }
/**
* Called when a log file is selected.
*
* Gets the log file from the input element and tries to read from it.
*/
DataView.prototype.logFileChanged = function(loadLogFileElement) {
var logFile = loadLogFileElement.files[0];
if (logFile) {
var fileReader = new FileReader();
fileReader.onload = this.onLoadLogFile.bind(this);
fileReader.onerror = this.onLoadLogFileError.bind(this);
fileReader.readAsText(logFile);
}
}
/**
* Displays an error message when unable to read the selected log file.
*/
DataView.prototype.onLoadLogFileError = function(event) {
alert('Error ' + event.target.error.code + '. Unable to load file.');
}
/**
* Tries to load the contents of the log file.
*/
DataView.prototype.onLoadLogFile = function(event) {
g_browser.loadedLogFile(event.target.result);
}
/** /**
* If not already waiting for results from all updates, triggers all * If not already waiting for results from all updates, triggers all
* updates and starts waiting for them to complete. * updates and starts waiting for them to complete.
......
...@@ -265,7 +265,7 @@ found in the LICENSE file. ...@@ -265,7 +265,7 @@ found in the LICENSE file.
<div id=dataViewLoadDataDiv> <div id=dataViewLoadDataDiv>
<h2>Load data</h2> <h2>Load data</h2>
<div style="margin: 8px"> <div style="margin: 8px">
<p><input type=button value="Load log from file" id=dataViewLoadLogFile /></p> <p><input type=file value="Load log from file" id=dataViewLoadLogFile /></p>
<p>Only works with log files created with "--log-net-log=file_name". <p>Only works with log files created with "--log-net-log=file_name".
"--net-log-level=#" will set the default log level used. "--net-log-level=#" will set the default log level used.
</p> </p>
......
...@@ -396,10 +396,6 @@ BrowserBridge.prototype.enableHttpThrottling = function(enable) { ...@@ -396,10 +396,6 @@ BrowserBridge.prototype.enableHttpThrottling = function(enable) {
chrome.send('enableHttpThrottling', [enable]); chrome.send('enableHttpThrottling', [enable]);
}; };
BrowserBridge.prototype.loadLogFile = function() {
chrome.send('loadLogFile');
};
BrowserBridge.prototype.refreshSystemLogs = function() { BrowserBridge.prototype.refreshSystemLogs = function() {
chrome.send('refreshSystemLogs'); chrome.send('refreshSystemLogs');
}; };
...@@ -613,7 +609,7 @@ BrowserBridge.prototype.loadedLogFile = function(logFileContents) { ...@@ -613,7 +609,7 @@ BrowserBridge.prototype.loadedLogFile = function(logFileContents) {
// removed or added between versions. Could also happen // removed or added between versions. Could also happen
// with source types, but less likely. // with source types, but less likely.
console.log( console.log(
'Unrecognized values in log entry: ' + JSON.stringify(entry)); 'Unrecognized values in log entry: ' + JSON.stringify(entries[i]));
} }
} }
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/base64.h" #include "base/base64.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/file_util.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
...@@ -31,7 +30,6 @@ ...@@ -31,7 +30,6 @@
#include "chrome/browser/platform_util.h" #include "chrome/browser/platform_util.h"
#include "chrome/browser/prefs/pref_member.h" #include "chrome/browser/prefs/pref_member.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/shell_dialogs.h"
#include "chrome/browser/ui/webui/chrome_url_data_manager.h" #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_version_info.h" #include "chrome/common/chrome_version_info.h"
...@@ -39,9 +37,8 @@ ...@@ -39,9 +37,8 @@
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "content/browser/browser_thread.h" #include "content/browser/browser_thread.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/tab_contents/tab_contents_view.h"
#include "content/common/notification_details.h" #include "content/common/notification_details.h"
#include "content/common/notification_type.h"
#include "grit/generated_resources.h" #include "grit/generated_resources.h"
#include "grit/net_internals_resources.h" #include "grit/net_internals_resources.h"
#include "net/base/escape.h" #include "net/base/escape.h"
...@@ -153,7 +150,6 @@ class NetInternalsHTMLSource : public ChromeURLDataManager::DataSource { ...@@ -153,7 +150,6 @@ class NetInternalsHTMLSource : public ChromeURLDataManager::DataSource {
// TODO(eroman): Can we start on the IO thread to begin with? // TODO(eroman): Can we start on the IO thread to begin with?
class NetInternalsMessageHandler class NetInternalsMessageHandler
: public WebUIMessageHandler, : public WebUIMessageHandler,
public SelectFileDialog::Listener,
public base::SupportsWeakPtr<NetInternalsMessageHandler>, public base::SupportsWeakPtr<NetInternalsMessageHandler>,
public NotificationObserver { public NotificationObserver {
public: public:
...@@ -182,37 +178,9 @@ class NetInternalsMessageHandler ...@@ -182,37 +178,9 @@ class NetInternalsMessageHandler
void OnGetSystemLog(const ListValue* list); void OnGetSystemLog(const ListValue* list);
#endif #endif
// SelectFileDialog::Listener implementation
virtual void FileSelected(const FilePath& path, int index, void* params);
virtual void FileSelectionCanceled(void* params);
// The only callback handled on the UI thread. As it needs to access fields
// from |web_ui_|, it can't be called on the IO thread.
void OnLoadLogFile(const ListValue* list);
private: private:
class IOThreadImpl; class IOThreadImpl;
// Task run on the FILE thread to read the contents of a log file. The result
// is then passed to IOThreadImpl's CallJavascriptFunction, which sends it
// back to the web page. IOThreadImpl is used instead of the
// NetInternalsMessageHandler directly because it checks if the message
// handler has been destroyed in the meantime.
class ReadLogFileTask : public Task {
public:
ReadLogFileTask(IOThreadImpl* proxy, const FilePath& path);
virtual void Run();
private:
// IOThreadImpl implements existence checks already. Simpler to reused them
// then to reimplement them.
scoped_refptr<IOThreadImpl> proxy_;
// Path of the file to open.
const FilePath path_;
};
#ifdef OS_CHROMEOS #ifdef OS_CHROMEOS
// Class that is used for getting network related ChromeOS logs. // Class that is used for getting network related ChromeOS logs.
// Logs are fetched from ChromeOS libcros on user request, and only when we // Logs are fetched from ChromeOS libcros on user request, and only when we
...@@ -284,9 +252,6 @@ class NetInternalsMessageHandler ...@@ -284,9 +252,6 @@ class NetInternalsMessageHandler
scoped_ptr<SystemLogsGetter> syslogs_getter_; scoped_ptr<SystemLogsGetter> syslogs_getter_;
#endif #endif
// Used for loading log files.
scoped_refptr<SelectFileDialog> select_log_file_dialog_;
DISALLOW_COPY_AND_ASSIGN(NetInternalsMessageHandler); DISALLOW_COPY_AND_ASSIGN(NetInternalsMessageHandler);
}; };
...@@ -537,8 +502,6 @@ NetInternalsMessageHandler::~NetInternalsMessageHandler() { ...@@ -537,8 +502,6 @@ NetInternalsMessageHandler::~NetInternalsMessageHandler() {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
NewRunnableMethod(proxy_.get(), &IOThreadImpl::Detach)); NewRunnableMethod(proxy_.get(), &IOThreadImpl::Detach));
} }
if (select_log_file_dialog_)
select_log_file_dialog_->ListenerDestroyed();
} }
WebUIMessageHandler* NetInternalsMessageHandler::Attach(WebUI* web_ui) { WebUIMessageHandler* NetInternalsMessageHandler::Attach(WebUI* web_ui) {
...@@ -561,36 +524,8 @@ WebUIMessageHandler* NetInternalsMessageHandler::Attach(WebUI* web_ui) { ...@@ -561,36 +524,8 @@ WebUIMessageHandler* NetInternalsMessageHandler::Attach(WebUI* web_ui) {
return result; return result;
} }
void NetInternalsMessageHandler::FileSelected(
const FilePath& path, int index, void* params) {
select_log_file_dialog_.release();
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
new ReadLogFileTask(proxy_.get(), path));
}
void NetInternalsMessageHandler::FileSelectionCanceled(void* params) {
select_log_file_dialog_.release();
}
void NetInternalsMessageHandler::OnLoadLogFile(const ListValue* list) {
// Only allow a single dialog at a time.
if (select_log_file_dialog_.get())
return;
select_log_file_dialog_ = SelectFileDialog::Create(this);
select_log_file_dialog_->SelectFile(
SelectFileDialog::SELECT_OPEN_FILE, string16(), FilePath(), NULL, 0,
FILE_PATH_LITERAL(""), web_ui_->tab_contents(),
web_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL);
}
void NetInternalsMessageHandler::RegisterMessages() { void NetInternalsMessageHandler::RegisterMessages() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Only callback handled on UI thread.
web_ui_->RegisterMessageCallback(
"loadLogFile",
NewCallback(this, &NetInternalsMessageHandler::OnLoadLogFile));
web_ui_->RegisterMessageCallback( web_ui_->RegisterMessageCallback(
"notifyReady", "notifyReady",
NewCallback(this, &NetInternalsMessageHandler::OnRendererReady)); NewCallback(this, &NetInternalsMessageHandler::OnRendererReady));
...@@ -720,25 +655,6 @@ void NetInternalsMessageHandler::OnEnableHttpThrottling(const ListValue* list) { ...@@ -720,25 +655,6 @@ void NetInternalsMessageHandler::OnEnableHttpThrottling(const ListValue* list) {
http_throttling_enabled_.SetValue(enable); http_throttling_enabled_.SetValue(enable);
} }
////////////////////////////////////////////////////////////////////////////////
//
// NetInternalsMessageHandler::ReadLogFileTask
//
////////////////////////////////////////////////////////////////////////////////
NetInternalsMessageHandler::ReadLogFileTask::ReadLogFileTask(
IOThreadImpl* proxy, const FilePath& path)
: proxy_(proxy), path_(path) {
}
void NetInternalsMessageHandler::ReadLogFileTask::Run() {
std::string file_contents;
if (!file_util::ReadFileToString(path_, &file_contents))
return;
proxy_->CallJavascriptFunction(L"g_browser.loadedLogFile",
new StringValue(file_contents));
}
#ifdef OS_CHROMEOS #ifdef OS_CHROMEOS
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// //
...@@ -1637,5 +1553,5 @@ NetInternalsUI::NetInternalsUI(TabContents* contents) : ChromeWebUI(contents) { ...@@ -1637,5 +1553,5 @@ NetInternalsUI::NetInternalsUI(TabContents* contents) : ChromeWebUI(contents) {
NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource();
// Set up the chrome://net-internals/ source. // Set up the chrome://net-internals/ source.
contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source);
} }
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