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"
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"
31 using content::BrowserThread
;
32 using content::WebContents
;
33 using content::WebUIMessageHandler
;
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
);
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
> {
55 NetExportMessageHandler();
56 ~NetExportMessageHandler() override
;
58 // WebUIMessageHandler implementation.
59 void RegisterMessages() override
;
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
);
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(
120 base::Bind(&NetExportMessageHandler::OnStartNetLog
,
121 base::Unretained(this)));
122 web_ui()->RegisterMessageCallback(
124 base::Bind(&NetExportMessageHandler::OnStopNetLog
,
125 base::Unretained(this)));
126 web_ui()->RegisterMessageCallback(
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
,
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
);
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
;
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_
,
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
,
170 base::Bind(&NetExportMessageHandler::GetNetLogFileName
,
171 base::Unretained(net_log_temp_file_
)),
172 base::Bind(&NetExportMessageHandler::SendEmail
));
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
,
184 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand
,
185 net_export_message_handler
,
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
);
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
;
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
,
216 // Failed posting the task, avoid leaking.
222 void NetExportMessageHandler::SendEmail(const base::FilePath
& file_to_send
) {
223 if (file_to_send
.empty())
225 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
227 #if defined(OS_ANDROID)
229 std::string subject
= "net_internals_log";
230 std::string title
= "Issue number: ";
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
));
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
);
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());