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 virtual ~NetExportMessageHandler();
58 // WebUIMessageHandler implementation.
59 virtual 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(BrowserThread::CurrentlyOn(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 ProcessNetLogCommand(weak_ptr_factory_
.GetWeakPtr(),
146 NetLogTempFile::DO_START
);
149 void NetExportMessageHandler::OnStopNetLog(const base::ListValue
* list
) {
150 ProcessNetLogCommand(weak_ptr_factory_
.GetWeakPtr(),
152 NetLogTempFile::DO_STOP
);
155 void NetExportMessageHandler::OnSendNetLog(const base::ListValue
* list
) {
156 content::BrowserThread::PostTaskAndReplyWithResult(
157 content::BrowserThread::FILE_USER_BLOCKING
,
159 base::Bind(&NetExportMessageHandler::GetNetLogFileName
,
160 base::Unretained(net_log_temp_file_
)),
161 base::Bind(&NetExportMessageHandler::SendEmail
));
165 void NetExportMessageHandler::ProcessNetLogCommand(
166 base::WeakPtr
<NetExportMessageHandler
> net_export_message_handler
,
167 NetLogTempFile
* net_log_temp_file
,
168 NetLogTempFile::Command command
) {
169 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
)) {
170 BrowserThread::PostTask(
171 BrowserThread::FILE_USER_BLOCKING
,
173 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand
,
174 net_export_message_handler
,
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
181 net_log_temp_file
->ProcessCommand(command
);
182 SendExportNetLogInfo(net_export_message_handler
, net_log_temp_file
);
186 base::FilePath
NetExportMessageHandler::GetNetLogFileName(
187 NetLogTempFile
* net_log_temp_file
) {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
189 base::FilePath net_export_file_path
;
190 net_log_temp_file
->GetFilePath(&net_export_file_path
);
191 return net_export_file_path
;
195 void NetExportMessageHandler::SendExportNetLogInfo(
196 base::WeakPtr
<NetExportMessageHandler
> net_export_message_handler
,
197 NetLogTempFile
* net_log_temp_file
) {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
199 base::Value
* value
= net_log_temp_file
->GetState();
200 if (!BrowserThread::PostTask(
201 BrowserThread::UI
, FROM_HERE
,
202 base::Bind(&NetExportMessageHandler::OnExportNetLogInfoChanged
,
203 net_export_message_handler
,
205 // Failed posting the task, avoid leaking.
211 void NetExportMessageHandler::SendEmail(const base::FilePath
& file_to_send
) {
212 if (file_to_send
.empty())
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
216 #if defined(OS_ANDROID)
218 std::string subject
= "net_internals_log";
219 std::string title
= "Issue number: ";
221 "Please add some informative text about the network issues.";
222 base::FilePath::StringType
file_to_attach(file_to_send
.value());
223 chrome::android::SendEmail(
224 base::UTF8ToUTF16(email
), base::UTF8ToUTF16(subject
),
225 base::UTF8ToUTF16(body
), base::UTF8ToUTF16(title
),
226 base::UTF8ToUTF16(file_to_attach
));
230 void NetExportMessageHandler::OnExportNetLogInfoChanged(base::Value
* arg
) {
231 scoped_ptr
<base::Value
> value(arg
);
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
233 web_ui()->CallJavascriptFunction(
234 "NetExportView.getInstance().onExportNetLogInfoChanged", *arg
);
239 NetExportUI::NetExportUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
240 web_ui
->AddMessageHandler(new NetExportMessageHandler());
242 // Set up the chrome://net-export/ source.
243 Profile
* profile
= Profile::FromWebUI(web_ui
);
244 content::WebUIDataSource::Add(profile
, CreateNetExportHTMLSource());