OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/net_export_ui.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/net/chrome_net_log.h" |
| 14 #include "chrome/browser/net/net_log_temp_file.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 18 #include "chrome/common/url_constants.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/browser/web_ui.h" |
| 22 #include "content/public/browser/web_ui_message_handler.h" |
| 23 #include "grit/browser_resources.h" |
| 24 #include "grit/generated_resources.h" |
| 25 |
| 26 using content::BrowserThread; |
| 27 using content::WebContents; |
| 28 using content::WebUIMessageHandler; |
| 29 |
| 30 namespace { |
| 31 |
| 32 ChromeWebUIDataSource* CreateNetExportHTMLSource() { |
| 33 ChromeWebUIDataSource* source = |
| 34 new ChromeWebUIDataSource(chrome::kChromeUINetExportHost); |
| 35 |
| 36 source->set_json_path("strings.js"); |
| 37 source->add_resource_path("net_export.js", IDR_NET_EXPORT_JS); |
| 38 source->set_default_resource(IDR_NET_EXPORT_HTML); |
| 39 return source; |
| 40 } |
| 41 |
| 42 // This class receives javascript messages from the renderer. |
| 43 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
| 44 // this class's methods are expected to run on the UI thread. |
| 45 class NetExportMessageHandler |
| 46 : public WebUIMessageHandler, |
| 47 public base::SupportsWeakPtr<NetExportMessageHandler> { |
| 48 public: |
| 49 NetExportMessageHandler() {} |
| 50 virtual ~NetExportMessageHandler(); |
| 51 |
| 52 // WebUIMessageHandler implementation. |
| 53 virtual void RegisterMessages() OVERRIDE; |
| 54 |
| 55 // Messages. |
| 56 void OnGetMobileNetLogInfo(const ListValue* list); |
| 57 void OnStartNetLog(const ListValue* list); |
| 58 void OnStopNetLog(const ListValue* list); |
| 59 void OnSendNetLog(const ListValue* list); |
| 60 |
| 61 private: |
| 62 // Calls NetLogTempFile's ProcessCommand. |
| 63 void ProcessNetLogCommand(NetLogTempFile::Command command); |
| 64 |
| 65 // Helper that calls g_exportBrowserBridge.receivedData in the renderer, |
| 66 // passing in |arg|. Takes ownership of |arg|. |
| 67 void SendJavascriptCommand(Value* arg); |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); |
| 70 }; |
| 71 |
| 72 NetExportMessageHandler::~NetExportMessageHandler() { |
| 73 // Cancel any in-progress requests to collect net_log into temporary file. |
| 74 NetLogTempFile* net_log_temp_file = |
| 75 g_browser_process->net_log()->net_log_temp_file(); |
| 76 BrowserThread::PostTask( |
| 77 BrowserThread::FILE_USER_BLOCKING, |
| 78 FROM_HERE, |
| 79 base::Bind(&NetLogTempFile::ProcessCommand, |
| 80 base::Unretained(net_log_temp_file), |
| 81 NetLogTempFile::DO_STOP)); |
| 82 } |
| 83 |
| 84 void NetExportMessageHandler::RegisterMessages() { |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 86 |
| 87 web_ui()->RegisterMessageCallback( |
| 88 "getMobileNetLogInfo", |
| 89 base::Bind(&NetExportMessageHandler::OnGetMobileNetLogInfo, |
| 90 base::Unretained(this))); |
| 91 web_ui()->RegisterMessageCallback( |
| 92 "startNetLog", |
| 93 base::Bind(&NetExportMessageHandler::OnStartNetLog, |
| 94 base::Unretained(this))); |
| 95 web_ui()->RegisterMessageCallback( |
| 96 "stopNetLog", |
| 97 base::Bind(&NetExportMessageHandler::OnStopNetLog, |
| 98 base::Unretained(this))); |
| 99 web_ui()->RegisterMessageCallback( |
| 100 "sendNetLog", |
| 101 base::Bind(&NetExportMessageHandler::OnSendNetLog, |
| 102 base::Unretained(this))); |
| 103 } |
| 104 |
| 105 void NetExportMessageHandler::OnGetMobileNetLogInfo(const ListValue* list) { |
| 106 NetLogTempFile* net_log_temp_file = |
| 107 g_browser_process->net_log()->net_log_temp_file(); |
| 108 SendJavascriptCommand(net_log_temp_file->NetLogTempFileToValue()); |
| 109 } |
| 110 |
| 111 void NetExportMessageHandler::OnStartNetLog(const ListValue* list) { |
| 112 ProcessNetLogCommand(NetLogTempFile::DO_START); |
| 113 } |
| 114 |
| 115 void NetExportMessageHandler::OnStopNetLog(const ListValue* list) { |
| 116 ProcessNetLogCommand(NetLogTempFile::DO_STOP); |
| 117 } |
| 118 |
| 119 void NetExportMessageHandler::OnSendNetLog(const ListValue* list) { |
| 120 ProcessNetLogCommand(NetLogTempFile::DO_SEND); |
| 121 } |
| 122 |
| 123 void NetExportMessageHandler::ProcessNetLogCommand( |
| 124 NetLogTempFile::Command command) { |
| 125 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { |
| 126 BrowserThread::PostTask( |
| 127 BrowserThread::FILE_USER_BLOCKING, |
| 128 FROM_HERE, |
| 129 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand, |
| 130 base::Unretained(this), |
| 131 command)); |
| 132 return; |
| 133 } |
| 134 |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 136 NetLogTempFile* net_log_temp_file = |
| 137 g_browser_process->net_log()->net_log_temp_file(); |
| 138 net_log_temp_file->ProcessCommand(command); |
| 139 |
| 140 Value* value = net_log_temp_file->NetLogTempFileToValue(); |
| 141 if (!BrowserThread::PostTask( |
| 142 BrowserThread::UI, FROM_HERE, |
| 143 base::Bind(&NetExportMessageHandler::SendJavascriptCommand, |
| 144 base::Unretained(this), |
| 145 value))) { |
| 146 // Failed posting the task, avoid leaking. |
| 147 delete value; |
| 148 } |
| 149 } |
| 150 |
| 151 void NetExportMessageHandler::SendJavascriptCommand(Value* arg) { |
| 152 scoped_ptr<Value> value(arg); |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 154 web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData", |
| 155 *value.get()); |
| 156 } |
| 157 |
| 158 } // namespace |
| 159 |
| 160 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 161 web_ui->AddMessageHandler(new NetExportMessageHandler()); |
| 162 |
| 163 // Set up the chrome://net-export/ source. |
| 164 Profile* profile = Profile::FromWebUI(web_ui); |
| 165 ChromeURLDataManager::AddDataSource(profile, CreateNetExportHTMLSource()); |
| 166 } |
OLD | NEW |