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

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 // Returns the path to the file which has NetLog data.
75 static FilePath GetNetLogFileName(NetLogTempFile* net_log_temp_file);
76
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);
81
82 // Send NetLog data via email. This runs on UI thread.
83 static void SendEmail(const FilePath& file_to_send);
84
85 // Call g_exportBrowserBridge.receivedData in the renderer, passing in |arg|.
86 // Takes ownership of |arg|.
87 void SendJavascriptCommand(Value* arg);
88
89 // Cache of g_browser_process->net_log()->net_log_temp_file().
90 NetLogTempFile* net_log_temp_file_;
91
92 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_;
93
94 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler);
95 };
96
97 NetExportMessageHandler::NetExportMessageHandler()
98 : net_log_temp_file_(g_browser_process->net_log()->net_log_temp_file()),
99 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
100 }
101
102 NetExportMessageHandler::~NetExportMessageHandler() {
103 // Cancel any in-progress requests to collect net_log into temporary file.
104 BrowserThread::PostTask(
105 BrowserThread::FILE_USER_BLOCKING,
106 FROM_HERE,
107 base::Bind(&NetLogTempFile::ProcessCommand,
108 base::Unretained(net_log_temp_file_),
109 NetLogTempFile::DO_STOP));
110 }
111
112 void NetExportMessageHandler::RegisterMessages() {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
114
115 web_ui()->RegisterMessageCallback(
116 "getExportNetLogInfo",
117 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo,
118 base::Unretained(this)));
119 web_ui()->RegisterMessageCallback(
120 "startNetLog",
121 base::Bind(&NetExportMessageHandler::OnStartNetLog,
122 base::Unretained(this)));
123 web_ui()->RegisterMessageCallback(
124 "stopNetLog",
125 base::Bind(&NetExportMessageHandler::OnStopNetLog,
126 base::Unretained(this)));
127 web_ui()->RegisterMessageCallback(
128 "sendNetLog",
129 base::Bind(&NetExportMessageHandler::OnSendNetLog,
130 base::Unretained(this)));
131 }
132
133 void NetExportMessageHandler::OnGetExportNetLogInfo(const ListValue* list) {
134 BrowserThread::PostTask(
135 BrowserThread::FILE_USER_BLOCKING,
136 FROM_HERE,
137 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo,
138 weak_ptr_factory_.GetWeakPtr(),
139 net_log_temp_file_));
140 }
141
142 void NetExportMessageHandler::OnStartNetLog(const ListValue* list) {
143 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(),
144 net_log_temp_file_,
145 NetLogTempFile::DO_START);
146 }
147
148 void NetExportMessageHandler::OnStopNetLog(const ListValue* list) {
149 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(),
150 net_log_temp_file_,
151 NetLogTempFile::DO_STOP);
152 }
153
154 void NetExportMessageHandler::OnSendNetLog(const ListValue* list) {
155 content::BrowserThread::PostTaskAndReplyWithResult(
156 content::BrowserThread::FILE_USER_BLOCKING,
157 FROM_HERE,
158 base::Bind(&NetExportMessageHandler::GetNetLogFileName,
159 base::Unretained(net_log_temp_file_)),
160 base::Bind(&NetExportMessageHandler::SendEmail));
161 }
162
163 // static
164 void NetExportMessageHandler::ProcessNetLogCommand(
165 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
166 NetLogTempFile* net_log_temp_file,
167 NetLogTempFile::Command command) {
168 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
169 BrowserThread::PostTask(
170 BrowserThread::FILE_USER_BLOCKING,
171 FROM_HERE,
172 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand,
173 net_export_message_handler,
174 net_log_temp_file,
175 command));
176 return;
177 }
178
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
180 net_log_temp_file->ProcessCommand(command);
181 SendExportNetLogInfo(net_export_message_handler, net_log_temp_file);
182 }
183
184 // static
185 FilePath NetExportMessageHandler::GetNetLogFileName(
186 NetLogTempFile* net_log_temp_file) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
188 FilePath net_export_file_path;
189 net_log_temp_file->GetFilePath(&net_export_file_path);
190 return net_export_file_path;
191 }
192
193 // static
194 void NetExportMessageHandler::SendExportNetLogInfo(
195 base::WeakPtr<NetExportMessageHandler> net_export_message_handler,
196 NetLogTempFile* net_log_temp_file) {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
198 Value* value = net_log_temp_file->GetState();
199 if (!BrowserThread::PostTask(
200 BrowserThread::UI, FROM_HERE,
201 base::Bind(&NetExportMessageHandler::SendJavascriptCommand,
202 net_export_message_handler,
203 value))) {
204 // Failed posting the task, avoid leaking.
205 delete value;
206 }
207 }
208
209 // static
210 void NetExportMessageHandler::SendEmail(const FilePath& file_to_send) {
211 if (file_to_send.empty())
212 return;
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214
215 #if defined(OS_ANDROID)
216 std::string email;
217 std::string subject = "net_internals_log";
218 std::string title = "Issue number: ";
219 std::string body =
220 "Please add some informative text about the network issues.";
221 FilePath::StringType file_to_attach(file_to_send.value());
222 chrome::android::SendEmail(
223 UTF8ToUTF16(email), UTF8ToUTF16(subject),
224 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach));
225 #endif
226 }
227
228 void NetExportMessageHandler::SendJavascriptCommand(Value* arg) {
229 scoped_ptr<Value> value(arg);
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
231 web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData",
232 *arg);
233 }
234
235 } // namespace
236
237 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
238 web_ui->AddMessageHandler(new NetExportMessageHandler());
239
240 // Set up the chrome://net-export/ source.
241 Profile* profile = Profile::FromWebUI(web_ui);
242 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource());
243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698