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

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, 10 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) 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.
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/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"
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 content::WebUIDataSource* CreateNetExportHTMLSource() {
38 content::WebUIDataSource* source =
39 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost);
40
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;
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. 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 virtual ~NetExportMessageHandler();
57
58 // WebUIMessageHandler implementation.
59 virtual void RegisterMessages() OVERRIDE;
60
61 // Messages.
62 void OnGetExportNetLogInfo(const ListValue* list);
63 void OnStartNetLog(const ListValue* list);
64 void OnStopNetLog(const ListValue* list);
65 void OnSendNetLog(const ListValue* list);
66
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 NetLogTempFile* net_log_temp_file,
72 NetLogTempFile::Command command);
73
74 static FilePath GetNetLogFileName(NetLogTempFile* net_log_temp_file);
James Hawkins 2013/01/29 22:48:34 nit: Document method.
ramant (doing other things) 2013/01/30 02:35:42 Done.
75
76 // Helper function to send state/file information from NetLogTempFile.
James Hawkins 2013/01/29 22:48:34 Optional nit: No need to actually say 'Helper func
ramant (doing other things) 2013/01/30 02:35:42 Done.
77 static void SendExportNetLogInfo(
78 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
79 NetLogTempFile* net_log_temp_file);
80
81 // Helper function that runs on UI thread to send NetLog data via email.
82 static void SendEmail(const FilePath& file_to_send);
83
84 // Helper that calls g_exportBrowserBridge.receivedData in the renderer,
85 // passing in |arg|. Takes ownership of |arg|.
86 void SendJavascriptCommand(Value* arg);
87
88 // Cache of g_browser_process->net_log()->net_log_temp_file().
89 NetLogTempFile* net_log_temp_file_;
90
91 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_;
92
93 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler);
94 };
95
96 NetExportMessageHandler::NetExportMessageHandler()
97 : net_log_temp_file_(g_browser_process->net_log()->net_log_temp_file()),
98 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
99 }
100
101 NetExportMessageHandler::~NetExportMessageHandler() {
102 // Cancel any in-progress requests to collect net_log into temporary file.
103 BrowserThread::PostTask(
104 BrowserThread::FILE_USER_BLOCKING,
105 FROM_HERE,
106 base::Bind(&NetLogTempFile::ProcessCommand,
107 base::Unretained(net_log_temp_file_),
108 NetLogTempFile::DO_STOP));
109 }
110
111 void NetExportMessageHandler::RegisterMessages() {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113
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)));
130 }
131
132 void NetExportMessageHandler::OnGetExportNetLogInfo(const ListValue* list) {
133 BrowserThread::PostTask(
134 BrowserThread::FILE_USER_BLOCKING,
135 FROM_HERE,
136 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo,
137 weak_ptr_factory_.GetWeakPtr(),
138 net_log_temp_file_));
139 }
140
141 void NetExportMessageHandler::OnStartNetLog(const ListValue* list) {
142 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(),
143 net_log_temp_file_,
144 NetLogTempFile::DO_START);
145 }
146
147 void NetExportMessageHandler::OnStopNetLog(const ListValue* list) {
148 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(),
149 net_log_temp_file_,
150 NetLogTempFile::DO_STOP);
151 }
152
153 void NetExportMessageHandler::OnSendNetLog(const ListValue* list) {
154 content::BrowserThread::PostTaskAndReplyWithResult(
155 content::BrowserThread::FILE_USER_BLOCKING,
156 FROM_HERE,
157 base::Bind(&NetExportMessageHandler::GetNetLogFileName,
158 base::Unretained(net_log_temp_file_)),
159 base::Bind(&NetExportMessageHandler::SendEmail));
160 }
161
162 // static
163 void NetExportMessageHandler::ProcessNetLogCommand(
164 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
165 NetLogTempFile* net_log_temp_file,
166 NetLogTempFile::Command command) {
167 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
168 BrowserThread::PostTask(
169 BrowserThread::FILE_USER_BLOCKING,
170 FROM_HERE,
171 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand,
172 net_export_message_handler,
173 net_log_temp_file,
174 command));
175 return;
176 }
177
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
179 net_log_temp_file->ProcessCommand(command);
180 SendExportNetLogInfo(net_export_message_handler, net_log_temp_file);
181 }
182
183 // static
184 FilePath NetExportMessageHandler::GetNetLogFileName(
185 NetLogTempFile* net_log_temp_file) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
187 FilePath net_export_file_path;
188 net_log_temp_file->GetFilePath(&net_export_file_path);
189 return net_export_file_path;
190 }
191
192 // static
193 void NetExportMessageHandler::SendExportNetLogInfo(
194 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
195 NetLogTempFile* net_log_temp_file) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
197 Value* value = net_log_temp_file->GetState();
198 if (!BrowserThread::PostTask(
199 BrowserThread::UI, FROM_HERE,
200 base::Bind(&NetExportMessageHandler::SendJavascriptCommand,
201 net_export_message_handler,
202 value))) {
203 // Failed posting the task, avoid leaking.
204 delete value;
205 }
206 }
207
208 // static
209 void NetExportMessageHandler::SendEmail(const FilePath& file_to_send) {
210 if (file_to_send.empty())
211 return;
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213
214 #if defined(OS_ANDROID)
215 std::string email;
216 std::string subject = "net_internals_log";
217 std::string title = "Issue number: ";
218 std::string body =
219 "Please add some informative text about the network issues.";
220 FilePath::StringType file_to_attach(file_to_send.value());
221 chrome::android::SendEmail(
222 UTF8ToUTF16(email), UTF8ToUTF16(subject),
223 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach));
224 #endif
225 }
226
227 void NetExportMessageHandler::SendJavascriptCommand(Value* arg) {
228 scoped_ptr<Value> value(arg);
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
230 web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData",
231 *arg);
232 }
233
234 } // namespace
235
236 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
237 web_ui->AddMessageHandler(new NetExportMessageHandler());
238
239 // Set up the chrome://net-export/ source.
240 Profile* profile = Profile::FromWebUI(web_ui);
241 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource());
242 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698