OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/values.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/net/chrome_net_log.h" | |
14 #include "chrome/browser/net/net_log_temp_file.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | |
18 #include "chrome/common/url_constants.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "content/public/browser/web_ui.h" | |
22 #include "content/public/browser/web_ui_message_handler.h" | |
23 #include "grit/browser_resources.h" | |
24 #include "grit/generated_resources.h" | |
mmenke
2013/01/11 18:31:12
Do we really need generated_resources?
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
25 | |
26 using content::BrowserThread; | |
27 using content::WebContents; | |
28 using content::WebUIMessageHandler; | |
29 | |
30 namespace { | |
31 | |
32 ChromeWebUIDataSource* CreateNetExportHTMLSource() { | |
33 ChromeWebUIDataSource* source = | |
34 new ChromeWebUIDataSource(chrome::kChromeUINetExportHost); | |
35 | |
36 source->set_json_path("strings.js"); | |
37 source->add_resource_path("net_export.js", IDR_NET_EXPORT_JS); | |
38 source->set_default_resource(IDR_NET_EXPORT_HTML); | |
39 return source; | |
40 } | |
41 | |
42 // This class receives javascript messages from the renderer. | |
43 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | |
44 // this class's public methods are expected to run on the UI thread. | |
45 class NetExportMessageHandler | |
46 : public WebUIMessageHandler, | |
47 public base::SupportsWeakPtr<NetExportMessageHandler> { | |
48 public: | |
49 NetExportMessageHandler() {} | |
50 virtual ~NetExportMessageHandler(); | |
51 | |
52 // WebUIMessageHandler implementation. | |
53 virtual void RegisterMessages() OVERRIDE; | |
54 | |
55 // Messages. | |
56 void OnGetExportNetLogInfo(const ListValue* list); | |
57 void OnStartNetLog(const ListValue* list); | |
58 void OnStopNetLog(const ListValue* list); | |
59 void OnSendNetLog(const ListValue* list); | |
60 | |
61 private: | |
62 // Calls NetLogTempFile's ProcessCommand. | |
63 void ProcessNetLogCommand(NetLogTempFile::Command command); | |
64 | |
65 // Helper function to send state/file information from NetLogTempFile. | |
66 void SendExportNetLogInfo(); | |
67 | |
68 // Helper that calls g_exportBrowserBridge.receivedData in the renderer, | |
69 // passing in |arg|. Takes ownership of |arg|. | |
70 void SendJavascriptCommand(Value* arg); | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); | |
73 }; | |
74 | |
75 NetExportMessageHandler::~NetExportMessageHandler() { | |
76 // Cancel any in-progress requests to collect net_log into temporary file. | |
77 NetLogTempFile* net_log_temp_file = | |
78 g_browser_process->net_log()->net_log_temp_file(); | |
79 BrowserThread::PostTask( | |
80 BrowserThread::FILE_USER_BLOCKING, | |
81 FROM_HERE, | |
82 base::Bind(&NetLogTempFile::ProcessCommand, | |
83 base::Unretained(net_log_temp_file), | |
84 NetLogTempFile::DO_STOP)); | |
85 } | |
86 | |
87 void NetExportMessageHandler::RegisterMessages() { | |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
89 | |
90 web_ui()->RegisterMessageCallback( | |
91 "getExportNetLogInfo", | |
92 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo, | |
93 base::Unretained(this))); | |
94 web_ui()->RegisterMessageCallback( | |
95 "startNetLog", | |
96 base::Bind(&NetExportMessageHandler::OnStartNetLog, | |
97 base::Unretained(this))); | |
98 web_ui()->RegisterMessageCallback( | |
99 "stopNetLog", | |
100 base::Bind(&NetExportMessageHandler::OnStopNetLog, | |
101 base::Unretained(this))); | |
102 web_ui()->RegisterMessageCallback( | |
103 "sendNetLog", | |
104 base::Bind(&NetExportMessageHandler::OnSendNetLog, | |
105 base::Unretained(this))); | |
106 } | |
107 | |
108 void NetExportMessageHandler::OnGetExportNetLogInfo(const ListValue* list) { | |
109 BrowserThread::PostTask( | |
110 BrowserThread::FILE_USER_BLOCKING, | |
111 FROM_HERE, | |
112 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo, | |
113 base::Unretained(this))); | |
114 } | |
115 | |
116 void NetExportMessageHandler::OnStartNetLog(const ListValue* list) { | |
117 ProcessNetLogCommand(NetLogTempFile::DO_START); | |
118 } | |
119 | |
120 void NetExportMessageHandler::OnStopNetLog(const ListValue* list) { | |
121 ProcessNetLogCommand(NetLogTempFile::DO_STOP); | |
122 } | |
123 | |
124 void NetExportMessageHandler::OnSendNetLog(const ListValue* list) { | |
125 ProcessNetLogCommand(NetLogTempFile::DO_SEND); | |
126 } | |
127 | |
128 void NetExportMessageHandler::ProcessNetLogCommand( | |
mmenke
2013/01/11 18:31:12
This should be static. Currently, |this| can be d
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
129 NetLogTempFile::Command command) { | |
130 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { | |
131 BrowserThread::PostTask( | |
132 BrowserThread::FILE_USER_BLOCKING, | |
133 FROM_HERE, | |
134 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand, | |
135 base::Unretained(this), | |
136 command)); | |
137 return; | |
138 } | |
139 | |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
141 NetLogTempFile* net_log_temp_file = | |
142 g_browser_process->net_log()->net_log_temp_file(); | |
143 net_log_temp_file->ProcessCommand(command); | |
144 | |
145 SendExportNetLogInfo(); | |
mmenke
2013/01/11 18:31:12
This is redundant - the Javascript code already se
ramant (doing other things)
2013/01/12 04:34:04
Deleted from the JS code.
Done.
| |
146 } | |
147 | |
148 void NetExportMessageHandler::SendExportNetLogInfo() { | |
mmenke
2013/01/11 18:31:12
This should also be static. Suggest you use PostT
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
150 | |
151 NetLogTempFile* net_log_temp_file = | |
152 g_browser_process->net_log()->net_log_temp_file(); | |
153 | |
154 Value* value = net_log_temp_file->NetLogTempFileToValue(); | |
155 if (!BrowserThread::PostTask( | |
156 BrowserThread::UI, FROM_HERE, | |
157 base::Bind(&NetExportMessageHandler::SendJavascriptCommand, | |
158 base::Unretained(this), | |
159 value))) { | |
160 // Failed posting the task, avoid leaking. | |
161 delete value; | |
162 } | |
163 } | |
164 | |
165 void NetExportMessageHandler::SendJavascriptCommand(Value* arg) { | |
166 scoped_ptr<Value> value(arg); | |
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
168 web_ui()->CallJavascriptFunction("g_exportBrowserBridge.receivedData", | |
169 *value.get()); | |
170 } | |
171 | |
172 } // namespace | |
173 | |
174 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
175 web_ui->AddMessageHandler(new NetExportMessageHandler()); | |
176 | |
177 // Set up the chrome://net-export/ source. | |
178 Profile* profile = Profile::FromWebUI(web_ui); | |
179 ChromeURLDataManager::AddDataSource(profile, CreateNetExportHTMLSource()); | |
180 } | |
OLD | NEW |