Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/browser/ui/webui/net_export_ui.cc

Issue 11828036: First cut at UI for saving net_logs data into a temporary file on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
mmenke 2013/01/17 17:49:43 nit: 2013
ramant (doing other things) 2013/01/18 04:59:52 Done.
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_url_data_manager.h"
19 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
20 #include "chrome/common/url_constants.h"
21 #include "content/public/browser/browser_thread.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::Command command);
71
72 // Sends net_log data via email if there is a net_log_temp_file.
mmenke 2013/01/17 17:49:43 There's always a NetLogTempFile class. May want t
ramant (doing other things) 2013/01/18 04:59:52 Done.
73 static void SendNetLog(
74 base::WeakPtr<NetExportMessageHandler> net_export_message_handler);
75
76 // Helper function to send state/file information from NetLogTempFile.
77 static void SendExportNetLogInfo(
78 base::WeakPtr<NetExportMessageHandler> net_export_message_handler);
79
80 // Helper function that runs on UI thread to send NetLog data via email.
81 static void SendEmail(const FilePath& file_to_send);
82
83 // Helper that calls g_exportBrowserBridge.receivedData in the renderer,
84 // passing in |arg|. Takes ownership of |arg|.
85 void SendJavascriptCommand(Value* arg);
86
87 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_;
88
89 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler);
90 };
91
92 NetExportMessageHandler::NetExportMessageHandler()
93 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
94 }
95
96 NetExportMessageHandler::~NetExportMessageHandler() {
97 // Cancel any in-progress requests to collect net_log into temporary file.
98 NetLogTempFile* net_log_temp_file =
99 g_browser_process->net_log()->net_log_temp_file();
mmenke 2013/01/17 17:49:43 optional: I'm not a big fan of using g_browser_pr
ramant (doing other things) 2013/01/18 04:59:52 Done.
100 BrowserThread::PostTask(
101 BrowserThread::FILE_USER_BLOCKING,
102 FROM_HERE,
103 base::Bind(&NetLogTempFile::ProcessCommand,
104 base::Unretained(net_log_temp_file),
105 NetLogTempFile::DO_STOP));
106 }
107
108 void NetExportMessageHandler::RegisterMessages() {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110
111 web_ui()->RegisterMessageCallback(
112 "getExportNetLogInfo",
113 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo,
114 base::Unretained(this)));
115 web_ui()->RegisterMessageCallback(
116 "startNetLog",
117 base::Bind(&NetExportMessageHandler::OnStartNetLog,
118 base::Unretained(this)));
119 web_ui()->RegisterMessageCallback(
120 "stopNetLog",
121 base::Bind(&NetExportMessageHandler::OnStopNetLog,
122 base::Unretained(this)));
123 web_ui()->RegisterMessageCallback(
124 "sendNetLog",
125 base::Bind(&NetExportMessageHandler::OnSendNetLog,
126 base::Unretained(this)));
127 }
128
129 void NetExportMessageHandler::OnGetExportNetLogInfo(const ListValue* list) {
130 BrowserThread::PostTask(
131 BrowserThread::FILE_USER_BLOCKING,
132 FROM_HERE,
133 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo,
134 weak_ptr_factory_.GetWeakPtr()));
135 }
136
137 void NetExportMessageHandler::OnStartNetLog(const ListValue* list) {
138 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(),
139 NetLogTempFile::DO_START);
140 }
141
142 void NetExportMessageHandler::OnStopNetLog(const ListValue* list) {
143 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), NetLogTempFile::DO_STOP);
144 }
145
146 void NetExportMessageHandler::OnSendNetLog(const ListValue* list) {
147 SendNetLog(weak_ptr_factory_.GetWeakPtr());
mmenke 2013/01/17 17:49:43 optional: I think this would be a little cleaner
ramant (doing other things) 2013/01/18 04:59:52 Done.
148 }
149
150 // static
151 void NetExportMessageHandler::ProcessNetLogCommand(
152 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
153 NetLogTempFile::Command command) {
154 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
155 BrowserThread::PostTask(
156 BrowserThread::FILE_USER_BLOCKING,
157 FROM_HERE,
158 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand,
159 net_export_message_handler,
160 command));
161 return;
162 }
163
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
165 NetLogTempFile* net_log_temp_file =
166 g_browser_process->net_log()->net_log_temp_file();
167 net_log_temp_file->ProcessCommand(command);
168
169 SendExportNetLogInfo(net_export_message_handler);
170 }
171
172 // static
173 void NetExportMessageHandler::SendNetLog(
174 base::WeakPtr<NetExportMessageHandler> net_export_message_handler) {
175 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
176 BrowserThread::PostTask(
177 BrowserThread::FILE_USER_BLOCKING,
178 FROM_HERE,
179 base::Bind(&NetExportMessageHandler::SendNetLog,
180 net_export_message_handler));
181 return;
182 }
183
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
185 NetLogTempFile* net_log_temp_file =
186 g_browser_process->net_log()->net_log_temp_file();
187
188 FilePath net_export_file_path;
189 if (net_log_temp_file->GetNetLogToSend(&net_export_file_path)) {
190 BrowserThread::PostTask(
191 BrowserThread::UI,
192 FROM_HERE,
193 base::Bind(&NetExportMessageHandler::SendEmail, net_export_file_path));
194 }
195
196 SendExportNetLogInfo(net_export_message_handler);
mmenke 2013/01/17 17:49:43 Has anything changed by calling GetNetLogToSend()?
ramant (doing other things) 2013/01/18 04:59:52 Done.
197 }
198
199 // static
200 void NetExportMessageHandler::SendExportNetLogInfo(
201 base::WeakPtr<NetExportMessageHandler> net_export_message_handler) {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
203
204 NetLogTempFile* net_log_temp_file =
205 g_browser_process->net_log()->net_log_temp_file();
206
207 Value* value = net_log_temp_file->GetState();
208 if (!BrowserThread::PostTask(
209 BrowserThread::UI, FROM_HERE,
210 base::Bind(&NetExportMessageHandler::SendJavascriptCommand,
211 net_export_message_handler,
212 value))) {
213 // Failed posting the task, avoid leaking.
214 delete value;
215 }
216 }
217
218 // static
219 void NetExportMessageHandler::SendEmail(const FilePath& file_to_send) {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
221
222 #if defined(OS_ANDROID)
223 std::string email;
224 std::string subject = "net_internals_log";
225 std::string title = "test_title";
226 std::string body = "Net Internals log data";
mmenke 2013/01/17 17:49:43 Suggest default values that encourage users to add
ramant (doing other things) 2013/01/18 04:59:52 Would appreciate if you could suggest some text.
227 FilePath::StringType file_to_attach(file_to_send.value());
228 chrome::android::SendEmail(
229 UTF8ToUTF16(email), UTF8ToUTF16(subject),
230 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach));
231 #endif
232 }
233
234 void NetExportMessageHandler::SendJavascriptCommand(Value* arg) {
235 scoped_ptr<Value> value(arg);
236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
237 web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData",
238 *value.get());
mmenke 2013/01/17 17:49:43 Doesn't just *value work here?
ramant (doing other things) 2013/01/18 04:59:52 NetInternalsMessageHandler::SendJavascriptCommand
239 }
240
241 } // namespace
242
243 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
244 web_ui->AddMessageHandler(new NetExportMessageHandler());
245
246 // Set up the chrome://net-export/ source.
247 Profile* profile = Profile::FromWebUI(web_ui);
248 ChromeURLDataManager::AddDataSource(profile, CreateNetExportHTMLSource());
249 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698