Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / net_export_ui.cc
bloba98bce5a0e579dfe41f91d7f52c4b74e5377e6ff
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.
5 #include "chrome/browser/ui/webui/net_export_ui.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/url_constants.h"
17 #include "components/net_log/chrome_net_log.h"
18 #include "components/net_log/net_log_temp_file.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/url_data_source.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h"
24 #include "content/public/browser/web_ui_message_handler.h"
25 #include "grit/browser_resources.h"
27 #if defined(OS_ANDROID)
28 #include "chrome/browser/android/intent_helper.h"
29 #endif
31 using content::BrowserThread;
32 using content::WebContents;
33 using content::WebUIMessageHandler;
35 namespace {
37 content::WebUIDataSource* CreateNetExportHTMLSource() {
38 content::WebUIDataSource* source =
39 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost);
41 source->SetJsonPath("strings.js");
42 source->AddResourcePath("net_export.js", IDR_NET_EXPORT_JS);
43 source->SetDefaultResource(IDR_NET_EXPORT_HTML);
44 return source;
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. All static
50 // functions except SendEmail run on FILE_USER_BLOCKING thread.
51 class NetExportMessageHandler
52 : public WebUIMessageHandler,
53 public base::SupportsWeakPtr<NetExportMessageHandler> {
54 public:
55 NetExportMessageHandler();
56 ~NetExportMessageHandler() override;
58 // WebUIMessageHandler implementation.
59 void RegisterMessages() override;
61 // Messages.
62 void OnGetExportNetLogInfo(const base::ListValue* list);
63 void OnStartNetLog(const base::ListValue* list);
64 void OnStopNetLog(const base::ListValue* list);
65 void OnSendNetLog(const base::ListValue* list);
67 private:
68 // Calls NetLogTempFile's ProcessCommand with DO_START and DO_STOP commands.
69 static void ProcessNetLogCommand(
70 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
71 net_log::NetLogTempFile* net_log_temp_file,
72 net_log::NetLogTempFile::Command command);
74 // Returns the path to the file which has NetLog data.
75 static base::FilePath GetNetLogFileName(
76 net_log::NetLogTempFile* net_log_temp_file);
78 // Send state/file information from NetLogTempFile.
79 static void SendExportNetLogInfo(
80 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
81 net_log::NetLogTempFile* net_log_temp_file);
83 // Send NetLog data via email. This runs on UI thread.
84 static void SendEmail(const base::FilePath& file_to_send);
86 // Call NetExportView.onExportNetLogInfoChanged JavsScript function in the
87 // renderer, passing in |arg|. Takes ownership of |arg|.
88 void OnExportNetLogInfoChanged(base::Value* arg);
90 // Cache of g_browser_process->net_log()->net_log_temp_file().
91 net_log::NetLogTempFile* net_log_temp_file_;
93 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_;
95 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler);
98 NetExportMessageHandler::NetExportMessageHandler()
99 : net_log_temp_file_(g_browser_process->net_log()->net_log_temp_file()),
100 weak_ptr_factory_(this) {
103 NetExportMessageHandler::~NetExportMessageHandler() {
104 // Cancel any in-progress requests to collect net_log into temporary file.
105 BrowserThread::PostTask(BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
106 base::Bind(&net_log::NetLogTempFile::ProcessCommand,
107 base::Unretained(net_log_temp_file_),
108 net_log::NetLogTempFile::DO_STOP));
111 void NetExportMessageHandler::RegisterMessages() {
112 DCHECK_CURRENTLY_ON(BrowserThread::UI);
114 web_ui()->RegisterMessageCallback(
115 "getExportNetLogInfo",
116 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo,
117 base::Unretained(this)));
118 web_ui()->RegisterMessageCallback(
119 "startNetLog",
120 base::Bind(&NetExportMessageHandler::OnStartNetLog,
121 base::Unretained(this)));
122 web_ui()->RegisterMessageCallback(
123 "stopNetLog",
124 base::Bind(&NetExportMessageHandler::OnStopNetLog,
125 base::Unretained(this)));
126 web_ui()->RegisterMessageCallback(
127 "sendNetLog",
128 base::Bind(&NetExportMessageHandler::OnSendNetLog,
129 base::Unretained(this)));
132 void NetExportMessageHandler::OnGetExportNetLogInfo(
133 const base::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_));
142 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) {
143 std::string log_mode;
144 bool result = list->GetString(0, &log_mode);
145 DCHECK(result);
147 net_log::NetLogTempFile::Command command;
148 if (log_mode == "LOG_BYTES") {
149 command = net_log::NetLogTempFile::DO_START_LOG_BYTES;
150 } else if (log_mode == "NORMAL") {
151 command = net_log::NetLogTempFile::DO_START;
152 } else {
153 DCHECK_EQ("STRIP_PRIVATE_DATA", log_mode);
154 command = net_log::NetLogTempFile::DO_START_STRIP_PRIVATE_DATA;
157 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_temp_file_,
158 command);
161 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) {
162 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_temp_file_,
163 net_log::NetLogTempFile::DO_STOP);
166 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) {
167 content::BrowserThread::PostTaskAndReplyWithResult(
168 content::BrowserThread::FILE_USER_BLOCKING,
169 FROM_HERE,
170 base::Bind(&NetExportMessageHandler::GetNetLogFileName,
171 base::Unretained(net_log_temp_file_)),
172 base::Bind(&NetExportMessageHandler::SendEmail));
175 // static
176 void NetExportMessageHandler::ProcessNetLogCommand(
177 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
178 net_log::NetLogTempFile* net_log_temp_file,
179 net_log::NetLogTempFile::Command command) {
180 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
181 BrowserThread::PostTask(
182 BrowserThread::FILE_USER_BLOCKING,
183 FROM_HERE,
184 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand,
185 net_export_message_handler,
186 net_log_temp_file,
187 command));
188 return;
191 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING);
192 net_log_temp_file->ProcessCommand(command);
193 SendExportNetLogInfo(net_export_message_handler, net_log_temp_file);
196 // static
197 base::FilePath NetExportMessageHandler::GetNetLogFileName(
198 net_log::NetLogTempFile* net_log_temp_file) {
199 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING);
200 base::FilePath net_export_file_path;
201 net_log_temp_file->GetFilePath(&net_export_file_path);
202 return net_export_file_path;
205 // static
206 void NetExportMessageHandler::SendExportNetLogInfo(
207 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
208 net_log::NetLogTempFile* net_log_temp_file) {
209 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING);
210 base::Value* value = net_log_temp_file->GetState();
211 if (!BrowserThread::PostTask(
212 BrowserThread::UI, FROM_HERE,
213 base::Bind(&NetExportMessageHandler::OnExportNetLogInfoChanged,
214 net_export_message_handler,
215 value))) {
216 // Failed posting the task, avoid leaking.
217 delete value;
221 // static
222 void NetExportMessageHandler::SendEmail(const base::FilePath& file_to_send) {
223 if (file_to_send.empty())
224 return;
225 DCHECK_CURRENTLY_ON(BrowserThread::UI);
227 #if defined(OS_ANDROID)
228 std::string email;
229 std::string subject = "net_internals_log";
230 std::string title = "Issue number: ";
231 std::string body =
232 "Please add some informative text about the network issues.";
233 base::FilePath::StringType file_to_attach(file_to_send.value());
234 chrome::android::SendEmail(
235 base::UTF8ToUTF16(email), base::UTF8ToUTF16(subject),
236 base::UTF8ToUTF16(body), base::UTF8ToUTF16(title),
237 base::UTF8ToUTF16(file_to_attach));
238 #endif
241 void NetExportMessageHandler::OnExportNetLogInfoChanged(base::Value* arg) {
242 scoped_ptr<base::Value> value(arg);
243 DCHECK_CURRENTLY_ON(BrowserThread::UI);
244 web_ui()->CallJavascriptFunction(
245 "NetExportView.getInstance().onExportNetLogInfoChanged", *arg);
248 } // namespace
250 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
251 web_ui->AddMessageHandler(new NetExportMessageHandler());
253 // Set up the chrome://net-export/ source.
254 Profile* profile = Profile::FromWebUI(web_ui);
255 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource());