Chromium Code Reviews| Index: chrome/browser/ui/webui/net_export_ui.cc |
| =================================================================== |
| --- chrome/browser/ui/webui/net_export_ui.cc (revision 0) |
| +++ chrome/browser/ui/webui/net_export_ui.cc (revision 0) |
| @@ -0,0 +1,180 @@ |
| +// Copyright (c) 2012 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 "chrome/browser/ui/webui/net_export_ui.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/net/chrome_net_log.h" |
| +#include "chrome/browser/net/net_log_temp_file.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| +#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "grit/browser_resources.h" |
| +#include "grit/generated_resources.h" |
|
mmenke
2013/01/11 18:31:12
Do we really need generated_resources?
ramant (doing other things)
2013/01/12 04:34:04
Done.
|
| + |
| +using content::BrowserThread; |
| +using content::WebContents; |
| +using content::WebUIMessageHandler; |
| + |
| +namespace { |
| + |
| +ChromeWebUIDataSource* CreateNetExportHTMLSource() { |
| + ChromeWebUIDataSource* source = |
| + new ChromeWebUIDataSource(chrome::kChromeUINetExportHost); |
| + |
| + source->set_json_path("strings.js"); |
| + source->add_resource_path("net_export.js", IDR_NET_EXPORT_JS); |
| + source->set_default_resource(IDR_NET_EXPORT_HTML); |
| + return source; |
| +} |
| + |
| +// This class receives javascript messages from the renderer. |
| +// Note that the WebUI infrastructure runs on the UI thread, therefore all of |
| +// this class's public methods are expected to run on the UI thread. |
| +class NetExportMessageHandler |
| + : public WebUIMessageHandler, |
| + public base::SupportsWeakPtr<NetExportMessageHandler> { |
| + public: |
| + NetExportMessageHandler() {} |
| + virtual ~NetExportMessageHandler(); |
| + |
| + // WebUIMessageHandler implementation. |
| + virtual void RegisterMessages() OVERRIDE; |
| + |
| + // Messages. |
| + void OnGetExportNetLogInfo(const ListValue* list); |
| + void OnStartNetLog(const ListValue* list); |
| + void OnStopNetLog(const ListValue* list); |
| + void OnSendNetLog(const ListValue* list); |
| + |
| + private: |
| + // Calls NetLogTempFile's ProcessCommand. |
| + void ProcessNetLogCommand(NetLogTempFile::Command command); |
| + |
| + // Helper function to send state/file information from NetLogTempFile. |
| + void SendExportNetLogInfo(); |
| + |
| + // Helper that calls g_exportBrowserBridge.receivedData in the renderer, |
| + // passing in |arg|. Takes ownership of |arg|. |
| + void SendJavascriptCommand(Value* arg); |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); |
| +}; |
| + |
| +NetExportMessageHandler::~NetExportMessageHandler() { |
| + // Cancel any in-progress requests to collect net_log into temporary file. |
| + NetLogTempFile* net_log_temp_file = |
| + g_browser_process->net_log()->net_log_temp_file(); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE_USER_BLOCKING, |
| + FROM_HERE, |
| + base::Bind(&NetLogTempFile::ProcessCommand, |
| + base::Unretained(net_log_temp_file), |
| + NetLogTempFile::DO_STOP)); |
| +} |
| + |
| +void NetExportMessageHandler::RegisterMessages() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + web_ui()->RegisterMessageCallback( |
| + "getExportNetLogInfo", |
| + base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "startNetLog", |
| + base::Bind(&NetExportMessageHandler::OnStartNetLog, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "stopNetLog", |
| + base::Bind(&NetExportMessageHandler::OnStopNetLog, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "sendNetLog", |
| + base::Bind(&NetExportMessageHandler::OnSendNetLog, |
| + base::Unretained(this))); |
| +} |
| + |
| +void NetExportMessageHandler::OnGetExportNetLogInfo(const ListValue* list) { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE_USER_BLOCKING, |
| + FROM_HERE, |
| + base::Bind(&NetExportMessageHandler::SendExportNetLogInfo, |
| + base::Unretained(this))); |
| +} |
| + |
| +void NetExportMessageHandler::OnStartNetLog(const ListValue* list) { |
| + ProcessNetLogCommand(NetLogTempFile::DO_START); |
| +} |
| + |
| +void NetExportMessageHandler::OnStopNetLog(const ListValue* list) { |
| + ProcessNetLogCommand(NetLogTempFile::DO_STOP); |
| +} |
| + |
| +void NetExportMessageHandler::OnSendNetLog(const ListValue* list) { |
| + ProcessNetLogCommand(NetLogTempFile::DO_SEND); |
| +} |
| + |
| +void NetExportMessageHandler::ProcessNetLogCommand( |
|
mmenke
2013/01/11 18:31:12
This should be static. Currently, |this| can be d
ramant (doing other things)
2013/01/12 04:34:04
Done.
|
| + NetLogTempFile::Command command) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE_USER_BLOCKING, |
| + FROM_HERE, |
| + base::Bind(&NetExportMessageHandler::ProcessNetLogCommand, |
| + base::Unretained(this), |
| + command)); |
| + return; |
| + } |
| + |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| + NetLogTempFile* net_log_temp_file = |
| + g_browser_process->net_log()->net_log_temp_file(); |
| + net_log_temp_file->ProcessCommand(command); |
| + |
| + SendExportNetLogInfo(); |
|
mmenke
2013/01/11 18:31:12
This is redundant - the Javascript code already se
ramant (doing other things)
2013/01/12 04:34:04
Deleted from the JS code.
Done.
|
| +} |
| + |
| +void NetExportMessageHandler::SendExportNetLogInfo() { |
|
mmenke
2013/01/11 18:31:12
This should also be static. Suggest you use PostT
ramant (doing other things)
2013/01/12 04:34:04
Done.
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| + |
| + NetLogTempFile* net_log_temp_file = |
| + g_browser_process->net_log()->net_log_temp_file(); |
| + |
| + Value* value = net_log_temp_file->NetLogTempFileToValue(); |
| + if (!BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&NetExportMessageHandler::SendJavascriptCommand, |
| + base::Unretained(this), |
| + value))) { |
| + // Failed posting the task, avoid leaking. |
| + delete value; |
| + } |
| +} |
| + |
| +void NetExportMessageHandler::SendJavascriptCommand(Value* arg) { |
| + scoped_ptr<Value> value(arg); |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData", |
| + *value.get()); |
| +} |
| + |
| +} // namespace |
| + |
| +NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| + web_ui->AddMessageHandler(new NetExportMessageHandler()); |
| + |
| + // Set up the chrome://net-export/ source. |
| + Profile* profile = Profile::FromWebUI(web_ui); |
| + ChromeURLDataManager::AddDataSource(profile, CreateNetExportHTMLSource()); |
| +} |