| 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,166 @@
 | 
| +// 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"
 | 
| +
 | 
| +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 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 OnGetMobileNetLogInfo(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 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(
 | 
| +      "getMobileNetLogInfo",
 | 
| +      base::Bind(&NetExportMessageHandler::OnGetMobileNetLogInfo,
 | 
| +                 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::OnGetMobileNetLogInfo(const ListValue* list) {
 | 
| +  NetLogTempFile* net_log_temp_file =
 | 
| +      g_browser_process->net_log()->net_log_temp_file();
 | 
| +  SendJavascriptCommand(net_log_temp_file->NetLogTempFileToValue());
 | 
| +}
 | 
| +
 | 
| +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(
 | 
| +    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);
 | 
| +
 | 
| +  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());
 | 
| +}
 | 
| 
 |