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/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/common/url_constants.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 NetLogTempFile
* net_log_temp_file
,
72 NetLogTempFile::Command command
);
74 // Returns the path to the file which has NetLog data.
75 static base::FilePath
GetNetLogFileName(NetLogTempFile
* net_log_temp_file
);
77 // Send state/file information from NetLogTempFile.
78 static void SendExportNetLogInfo(
79 base::WeakPtr
<NetExportMessageHandler
> net_export_message_handler
,
80 NetLogTempFile
* net_log_temp_file
);
82 // Send NetLog data via email. This runs on UI thread.
83 static void SendEmail(const base::FilePath
& file_to_send
);
85 // Call NetExportView.onExportNetLogInfoChanged JavsScript function in the
86 // renderer, passing in |arg|. Takes ownership of |arg|.
87 void OnExportNetLogInfoChanged(base::Value
* arg
);
89 // Cache of g_browser_process->net_log()->net_log_temp_file().
90 NetLogTempFile
* net_log_temp_file_
;
92 base::WeakPtrFactory
<NetExportMessageHandler
> weak_ptr_factory_
;
94 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler
);
97 NetExportMessageHandler::NetExportMessageHandler()
98 : net_log_temp_file_(g_browser_process
->net_log()->net_log_temp_file()),
99 weak_ptr_factory_(this) {
102 NetExportMessageHandler::~NetExportMessageHandler() {
103 // Cancel any in-progress requests to collect net_log into temporary file.
104 BrowserThread::PostTask(
105 BrowserThread::FILE_USER_BLOCKING
,
107 base::Bind(&NetLogTempFile::ProcessCommand
,
108 base::Unretained(net_log_temp_file_
),
109 NetLogTempFile::DO_STOP
));
112 void NetExportMessageHandler::RegisterMessages() {
113 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
115 web_ui()->RegisterMessageCallback(
116 "getExportNetLogInfo",
117 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo
,
118 base::Unretained(this)));
119 web_ui()->RegisterMessageCallback(
121 base::Bind(&NetExportMessageHandler::OnStartNetLog
,
122 base::Unretained(this)));
123 web_ui()->RegisterMessageCallback(
125 base::Bind(&NetExportMessageHandler::OnStopNetLog
,
126 base::Unretained(this)));
127 web_ui()->RegisterMessageCallback(
129 base::Bind(&NetExportMessageHandler::OnSendNetLog
,
130 base::Unretained(this)));
133 void NetExportMessageHandler::OnGetExportNetLogInfo(
134 const base::ListValue
* list
) {
135 BrowserThread::PostTask(
136 BrowserThread::FILE_USER_BLOCKING
,
138 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo
,
139 weak_ptr_factory_
.GetWeakPtr(),
140 net_log_temp_file_
));
143 void NetExportMessageHandler::OnStartNetLog(const base::ListValue
* list
) {
144 std::string log_mode
;
145 bool result
= list
->GetString(0, &log_mode
);
148 NetLogTempFile::Command command
;
149 if (log_mode
== "LOG_BYTES") {
150 command
= NetLogTempFile::DO_START_LOG_BYTES
;
151 } else if (log_mode
== "NORMAL") {
152 command
= NetLogTempFile::DO_START
;
154 DCHECK_EQ("STRIP_PRIVATE_DATA", log_mode
);
155 command
= NetLogTempFile::DO_START_STRIP_PRIVATE_DATA
;
158 ProcessNetLogCommand(weak_ptr_factory_
.GetWeakPtr(), net_log_temp_file_
,
162 void NetExportMessageHandler::OnStopNetLog(const base::ListValue
* list
) {
163 ProcessNetLogCommand(weak_ptr_factory_
.GetWeakPtr(),
165 NetLogTempFile::DO_STOP
);
168 void NetExportMessageHandler::OnSendNetLog(const base::ListValue
* list
) {
169 content::BrowserThread::PostTaskAndReplyWithResult(
170 content::BrowserThread::FILE_USER_BLOCKING
,
172 base::Bind(&NetExportMessageHandler::GetNetLogFileName
,
173 base::Unretained(net_log_temp_file_
)),
174 base::Bind(&NetExportMessageHandler::SendEmail
));
178 void NetExportMessageHandler::ProcessNetLogCommand(
179 base::WeakPtr
<NetExportMessageHandler
> net_export_message_handler
,
180 NetLogTempFile
* net_log_temp_file
,
181 NetLogTempFile::Command command
) {
182 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
)) {
183 BrowserThread::PostTask(
184 BrowserThread::FILE_USER_BLOCKING
,
186 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand
,
187 net_export_message_handler
,
193 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
194 net_log_temp_file
->ProcessCommand(command
);
195 SendExportNetLogInfo(net_export_message_handler
, net_log_temp_file
);
199 base::FilePath
NetExportMessageHandler::GetNetLogFileName(
200 NetLogTempFile
* net_log_temp_file
) {
201 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
202 base::FilePath net_export_file_path
;
203 net_log_temp_file
->GetFilePath(&net_export_file_path
);
204 return net_export_file_path
;
208 void NetExportMessageHandler::SendExportNetLogInfo(
209 base::WeakPtr
<NetExportMessageHandler
> net_export_message_handler
,
210 NetLogTempFile
* net_log_temp_file
) {
211 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
212 base::Value
* value
= net_log_temp_file
->GetState();
213 if (!BrowserThread::PostTask(
214 BrowserThread::UI
, FROM_HERE
,
215 base::Bind(&NetExportMessageHandler::OnExportNetLogInfoChanged
,
216 net_export_message_handler
,
218 // Failed posting the task, avoid leaking.
224 void NetExportMessageHandler::SendEmail(const base::FilePath
& file_to_send
) {
225 if (file_to_send
.empty())
227 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
229 #if defined(OS_ANDROID)
231 std::string subject
= "net_internals_log";
232 std::string title
= "Issue number: ";
234 "Please add some informative text about the network issues.";
235 base::FilePath::StringType
file_to_attach(file_to_send
.value());
236 chrome::android::SendEmail(
237 base::UTF8ToUTF16(email
), base::UTF8ToUTF16(subject
),
238 base::UTF8ToUTF16(body
), base::UTF8ToUTF16(title
),
239 base::UTF8ToUTF16(file_to_attach
));
243 void NetExportMessageHandler::OnExportNetLogInfoChanged(base::Value
* arg
) {
244 scoped_ptr
<base::Value
> value(arg
);
245 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
246 web_ui()->CallJavascriptFunction(
247 "NetExportView.getInstance().onExportNetLogInfoChanged", *arg
);
252 NetExportUI::NetExportUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
253 web_ui
->AddMessageHandler(new NetExportMessageHandler());
255 // Set up the chrome://net-export/ source.
256 Profile
* profile
= Profile::FromWebUI(web_ui
);
257 content::WebUIDataSource::Add(profile
, CreateNetExportHTMLSource());