OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/string_util.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/net/chrome_net_log.h" |
| 16 #include "chrome/browser/net/net_log_temp_file.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 19 #include "chrome/common/url_constants.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/url_data_source.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_ui.h" |
| 24 #include "content/public/browser/web_ui_message_handler.h" |
| 25 #include "grit/browser_resources.h" |
| 26 |
| 27 #if defined(OS_ANDROID) |
| 28 #include "chrome/browser/android/intent_helper.h" |
| 29 #endif |
| 30 |
| 31 using content::BrowserThread; |
| 32 using content::WebContents; |
| 33 using content::WebUIMessageHandler; |
| 34 |
| 35 namespace { |
| 36 |
| 37 ChromeWebUIDataSource* CreateNetExportHTMLSource() { |
| 38 ChromeWebUIDataSource* source = |
| 39 new ChromeWebUIDataSource(chrome::kChromeUINetExportHost); |
| 40 |
| 41 source->set_json_path("strings.js"); |
| 42 source->add_resource_path("net_export.js", IDR_NET_EXPORT_JS); |
| 43 source->set_default_resource(IDR_NET_EXPORT_HTML); |
| 44 return source; |
| 45 } |
| 46 |
| 47 // This class receives javascript messages from the renderer. |
| 48 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
| 49 // this class's public methods are expected to run on the UI thread. |
| 50 class NetExportMessageHandler |
| 51 : public WebUIMessageHandler, |
| 52 public base::SupportsWeakPtr<NetExportMessageHandler> { |
| 53 public: |
| 54 NetExportMessageHandler(); |
| 55 virtual ~NetExportMessageHandler(); |
| 56 |
| 57 // WebUIMessageHandler implementation. |
| 58 virtual void RegisterMessages() OVERRIDE; |
| 59 |
| 60 // Messages. |
| 61 void OnGetExportNetLogInfo(const ListValue* list); |
| 62 void OnStartNetLog(const ListValue* list); |
| 63 void OnStopNetLog(const ListValue* list); |
| 64 void OnSendNetLog(const ListValue* list); |
| 65 |
| 66 private: |
| 67 // Calls NetLogTempFile's ProcessCommand with DO_START and DO_STOP commands. |
| 68 static void ProcessNetLogCommand( |
| 69 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, |
| 70 NetLogTempFile* net_log_temp_file, |
| 71 NetLogTempFile::Command command); |
| 72 |
| 73 static void GetNetLogFileName(NetLogTempFile* net_log_temp_file, |
| 74 FilePath* file_to_send); |
| 75 |
| 76 // Helper function to send state/file information from NetLogTempFile. |
| 77 static void SendExportNetLogInfo( |
| 78 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, |
| 79 NetLogTempFile* net_log_temp_file); |
| 80 |
| 81 // Helper function that runs on UI thread to send NetLog data via email. Takes |
| 82 // ownership of |file_to_send_arg|. |
| 83 static void SendEmail(FilePath* file_to_send_arg); |
| 84 |
| 85 // Helper that calls g_exportBrowserBridge.receivedData in the renderer, |
| 86 // passing in |arg|. Takes ownership of |arg|. |
| 87 void SendJavascriptCommand(Value* arg); |
| 88 |
| 89 // Cache of g_browser_process->net_log()->net_log_temp_file(). |
| 90 NetLogTempFile* net_log_temp_file_; |
| 91 |
| 92 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); |
| 95 }; |
| 96 |
| 97 NetExportMessageHandler::NetExportMessageHandler() |
| 98 : net_log_temp_file_(g_browser_process->net_log()->net_log_temp_file()), |
| 99 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 100 } |
| 101 |
| 102 NetExportMessageHandler::~NetExportMessageHandler() { |
| 103 // Cancel any in-progress requests to collect net_log into temporary file. |
| 104 BrowserThread::PostTask( |
| 105 BrowserThread::FILE_USER_BLOCKING, |
| 106 FROM_HERE, |
| 107 base::Bind(&NetLogTempFile::ProcessCommand, |
| 108 base::Unretained(net_log_temp_file_), |
| 109 NetLogTempFile::DO_STOP)); |
| 110 } |
| 111 |
| 112 void NetExportMessageHandler::RegisterMessages() { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 114 |
| 115 web_ui()->RegisterMessageCallback( |
| 116 "getExportNetLogInfo", |
| 117 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo, |
| 118 base::Unretained(this))); |
| 119 web_ui()->RegisterMessageCallback( |
| 120 "startNetLog", |
| 121 base::Bind(&NetExportMessageHandler::OnStartNetLog, |
| 122 base::Unretained(this))); |
| 123 web_ui()->RegisterMessageCallback( |
| 124 "stopNetLog", |
| 125 base::Bind(&NetExportMessageHandler::OnStopNetLog, |
| 126 base::Unretained(this))); |
| 127 web_ui()->RegisterMessageCallback( |
| 128 "sendNetLog", |
| 129 base::Bind(&NetExportMessageHandler::OnSendNetLog, |
| 130 base::Unretained(this))); |
| 131 } |
| 132 |
| 133 void NetExportMessageHandler::OnGetExportNetLogInfo(const ListValue* list) { |
| 134 BrowserThread::PostTask( |
| 135 BrowserThread::FILE_USER_BLOCKING, |
| 136 FROM_HERE, |
| 137 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo, |
| 138 weak_ptr_factory_.GetWeakPtr(), |
| 139 net_log_temp_file_)); |
| 140 } |
| 141 |
| 142 void NetExportMessageHandler::OnStartNetLog(const ListValue* list) { |
| 143 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), |
| 144 net_log_temp_file_, |
| 145 NetLogTempFile::DO_START); |
| 146 } |
| 147 |
| 148 void NetExportMessageHandler::OnStopNetLog(const ListValue* list) { |
| 149 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), |
| 150 net_log_temp_file_, |
| 151 NetLogTempFile::DO_STOP); |
| 152 } |
| 153 |
| 154 void NetExportMessageHandler::OnSendNetLog(const ListValue* list) { |
| 155 FilePath* file_to_send = new FilePath(); |
| 156 content::BrowserThread::PostTaskAndReply( |
| 157 content::BrowserThread::FILE_USER_BLOCKING, |
| 158 FROM_HERE, |
| 159 base::Bind(&GetNetLogFileName, |
| 160 base::Unretained(net_log_temp_file_), |
| 161 base::Unretained(file_to_send)), |
| 162 base::Bind(&NetExportMessageHandler::SendEmail, |
| 163 base::Owned(file_to_send))); |
| 164 } |
| 165 |
| 166 // static |
| 167 void NetExportMessageHandler::ProcessNetLogCommand( |
| 168 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, |
| 169 NetLogTempFile* net_log_temp_file, |
| 170 NetLogTempFile::Command command) { |
| 171 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { |
| 172 BrowserThread::PostTask( |
| 173 BrowserThread::FILE_USER_BLOCKING, |
| 174 FROM_HERE, |
| 175 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand, |
| 176 net_export_message_handler, |
| 177 net_log_temp_file, |
| 178 command)); |
| 179 return; |
| 180 } |
| 181 |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 183 net_log_temp_file->ProcessCommand(command); |
| 184 SendExportNetLogInfo(net_export_message_handler, net_log_temp_file); |
| 185 } |
| 186 |
| 187 // static |
| 188 void NetExportMessageHandler::GetNetLogFileName( |
| 189 NetLogTempFile* net_log_temp_file, |
| 190 FilePath* file_to_send) { |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 192 FilePath net_export_file_path; |
| 193 if (net_log_temp_file->GetFilePath(&net_export_file_path)) |
| 194 *file_to_send = net_export_file_path; |
| 195 } |
| 196 |
| 197 // static |
| 198 void NetExportMessageHandler::SendExportNetLogInfo( |
| 199 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, |
| 200 NetLogTempFile* net_log_temp_file) { |
| 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 202 Value* value = net_log_temp_file->GetState(); |
| 203 if (!BrowserThread::PostTask( |
| 204 BrowserThread::UI, FROM_HERE, |
| 205 base::Bind(&NetExportMessageHandler::SendJavascriptCommand, |
| 206 net_export_message_handler, |
| 207 value))) { |
| 208 // Failed posting the task, avoid leaking. |
| 209 delete value; |
| 210 } |
| 211 } |
| 212 |
| 213 // static |
| 214 void NetExportMessageHandler::SendEmail(FilePath* file_to_send_arg) { |
| 215 FilePath file_to_send(*file_to_send_arg); |
| 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 217 |
| 218 #if defined(OS_ANDROID) |
| 219 std::string email; |
| 220 std::string subject = "net_internals_log"; |
| 221 std::string title = "Issue number: "; |
| 222 std::string body = |
| 223 "Please add some informative text about the network issues."; |
| 224 FilePath::StringType file_to_attach(file_to_send.value()); |
| 225 chrome::android::SendEmail( |
| 226 UTF8ToUTF16(email), UTF8ToUTF16(subject), |
| 227 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach)); |
| 228 #endif |
| 229 } |
| 230 |
| 231 void NetExportMessageHandler::SendJavascriptCommand(Value* arg) { |
| 232 scoped_ptr<Value> value(arg); |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 234 web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData", |
| 235 *arg); |
| 236 } |
| 237 |
| 238 } // namespace |
| 239 |
| 240 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 241 web_ui->AddMessageHandler(new NetExportMessageHandler()); |
| 242 |
| 243 // Set up the chrome://net-export/ source. |
| 244 Profile* profile = Profile::FromWebUI(web_ui); |
| 245 ChromeURLDataManager::AddDataSourceImpl(profile, CreateNetExportHTMLSource()); |
| 246 } |
OLD | NEW |