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

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

Issue 2698143004: Add ongoing events to net-export log when logging starts (Closed)
Patch Set: Added/improved some comments Created 3 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/net_export_ui.h" 5 #include "chrome/browser/ui/webui/net_export_ui.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/scoped_observer.h" 15 #include "base/scoped_observer.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h" 18 #include "base/values.h"
18 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/download/download_prefs.h" 20 #include "chrome/browser/download/download_prefs.h"
21 #include "chrome/browser/io_thread.h"
20 #include "chrome/browser/net/net_export_helper.h" 22 #include "chrome/browser/net/net_export_helper.h"
21 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/chrome_select_file_policy.h" 24 #include "chrome/browser/ui/chrome_select_file_policy.h"
23 #include "chrome/common/url_constants.h" 25 #include "chrome/common/url_constants.h"
24 #include "components/grit/components_resources.h" 26 #include "components/grit/components_resources.h"
25 #include "components/net_log/chrome_net_log.h" 27 #include "components/net_log/chrome_net_log.h"
26 #include "components/net_log/net_export_ui_constants.h" 28 #include "components/net_log/net_export_ui_constants.h"
27 #include "components/net_log/net_log_file_writer.h" 29 #include "components/net_log/net_log_file_writer.h"
28 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/storage_partition.h"
29 #include "content/public/browser/url_data_source.h" 32 #include "content/public/browser/url_data_source.h"
30 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
31 #include "content/public/browser/web_ui.h" 34 #include "content/public/browser/web_ui.h"
32 #include "content/public/browser/web_ui_data_source.h" 35 #include "content/public/browser/web_ui_data_source.h"
33 #include "content/public/browser/web_ui_message_handler.h" 36 #include "content/public/browser/web_ui_message_handler.h"
37 #include "extensions/features/features.h"
34 #include "net/log/net_log_capture_mode.h" 38 #include "net/log/net_log_capture_mode.h"
35 #include "net/url_request/url_request_context_getter.h" 39 #include "net/url_request/url_request_context_getter.h"
36 #include "ui/shell_dialogs/select_file_dialog.h" 40 #include "ui/shell_dialogs/select_file_dialog.h"
37 41
38 #if defined(OS_ANDROID) 42 #if defined(OS_ANDROID)
39 #include "chrome/browser/android/intent_helper.h" 43 #include "chrome/browser/android/intent_helper.h"
40 #endif 44 #endif
41 45
42 using content::BrowserThread; 46 using content::BrowserThread;
43 using content::WebContents; 47 using content::WebContents;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 // ui::SelectFileDialog::Listener implementation. 95 // ui::SelectFileDialog::Listener implementation.
92 void FileSelected(const base::FilePath& path, 96 void FileSelected(const base::FilePath& path,
93 int index, 97 int index,
94 void* params) override; 98 void* params) override;
95 void FileSelectionCanceled(void* params) override; 99 void FileSelectionCanceled(void* params) override;
96 100
97 // net_log::NetLogFileWriter::StateObserver implementation. 101 // net_log::NetLogFileWriter::StateObserver implementation.
98 void OnNewState(const base::DictionaryValue& state) override; 102 void OnNewState(const base::DictionaryValue& state) override;
99 103
100 private: 104 private:
105 using URLRequestContextGetterList =
106 std::vector<scoped_refptr<net::URLRequestContextGetter>>;
107
101 // Send NetLog data via email. 108 // Send NetLog data via email.
102 static void SendEmail(const base::FilePath& file_to_send); 109 static void SendEmail(const base::FilePath& file_to_send);
103 110
104 // chrome://net-export can be used on both mobile and desktop platforms. 111 // chrome://net-export can be used on both mobile and desktop platforms.
105 // On mobile a user cannot pick where their NetLog file is saved to. 112 // On mobile a user cannot pick where their NetLog file is saved to.
106 // Instead, everything is saved on the user's temp directory. Thus the 113 // Instead, everything is saved on the user's temp directory. Thus the
107 // mobile user has the UI available to send their NetLog file as an 114 // mobile user has the UI available to send their NetLog file as an
108 // email while the desktop user, who gets to choose their NetLog file's 115 // email while the desktop user, who gets to choose their NetLog file's
109 // location, does not. Furthermore, since every time a user starts logging 116 // location, does not. Furthermore, since every time a user starts logging
110 // to a new NetLog file on mobile platforms it overwrites the previous 117 // to a new NetLog file on mobile platforms it overwrites the previous
(...skipping 18 matching lines...) Expand all
129 net_log::NetLogFileWriter::StateObserver> 136 net_log::NetLogFileWriter::StateObserver>
130 state_observer_manager_; 137 state_observer_manager_;
131 138
132 // The capture mode the user chose in the UI when logging started is cached 139 // The capture mode the user chose in the UI when logging started is cached
133 // here and is read after a file path is chosen in the save dialog. 140 // here and is read after a file path is chosen in the save dialog.
134 // Its value is only valid while the save dialog is open on the desktop UI. 141 // Its value is only valid while the save dialog is open on the desktop UI.
135 net::NetLogCaptureMode capture_mode_; 142 net::NetLogCaptureMode capture_mode_;
136 143
137 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; 144 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
138 145
146 // Context getters from which ongoing events at logging start are retrieved
147 // so they can be added to the net log.
148 URLRequestContextGetterList context_getters_;
149
139 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_; 150 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_;
140 151
141 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); 152 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler);
142 }; 153 };
143 154
144 NetExportMessageHandler::NetExportMessageHandler() 155 NetExportMessageHandler::NetExportMessageHandler()
145 : file_writer_(g_browser_process->net_log()->net_log_file_writer()), 156 : file_writer_(g_browser_process->net_log()->net_log_file_writer()),
146 state_observer_manager_(this), 157 state_observer_manager_(this),
147 weak_ptr_factory_(this) { 158 weak_ptr_factory_(this) {
148 file_writer_->Initialize( 159 file_writer_->Initialize(
149 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE_USER_BLOCKING), 160 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE_USER_BLOCKING),
150 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); 161 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
151 } 162 }
152 163
153 NetExportMessageHandler::~NetExportMessageHandler() { 164 NetExportMessageHandler::~NetExportMessageHandler() {
154 // There may be a pending file dialog, it needs to be told that the user 165 // There may be a pending file dialog, it needs to be told that the user
155 // has gone away so that it doesn't try to call back. 166 // has gone away so that it doesn't try to call back.
156 if (select_file_dialog_) 167 if (select_file_dialog_)
157 select_file_dialog_->ListenerDestroyed(); 168 select_file_dialog_->ListenerDestroyed();
158 169
159 file_writer_->StopNetLog(nullptr, nullptr); 170 file_writer_->StopNetLog(nullptr, nullptr);
160 } 171 }
161 172
162 void NetExportMessageHandler::RegisterMessages() { 173 void NetExportMessageHandler::RegisterMessages() {
163 DCHECK_CURRENTLY_ON(BrowserThread::UI); 174 DCHECK_CURRENTLY_ON(BrowserThread::UI);
164 175
176 Profile* profile = Profile::FromWebUI(web_ui());
177
178 context_getters_.push_back(profile->GetRequestContext());
179 context_getters_.push_back(
180 content::BrowserContext::GetDefaultStoragePartition(profile)
181 ->GetMediaURLRequestContext());
182 #if BUILDFLAG(ENABLE_EXTENSIONS)
183 context_getters_.push_back(profile->GetRequestContextForExtensions());
184 #endif
185 context_getters_.push_back(
186 g_browser_process->io_thread()->system_url_request_context_getter());
187 context_getters_.push_back(
eroman 2017/02/21 22:14:23 Rather than adding a member variable initialized i
wangyix1 2017/02/22 01:28:18 Done.
188 g_browser_process->io_thread()->proxy_script_fetcher_context_getter());
189
165 web_ui()->RegisterMessageCallback( 190 web_ui()->RegisterMessageCallback(
166 net_log::kEnableNotifyUIWithStateHandler, 191 net_log::kEnableNotifyUIWithStateHandler,
167 base::Bind(&NetExportMessageHandler::OnEnableNotifyUIWithState, 192 base::Bind(&NetExportMessageHandler::OnEnableNotifyUIWithState,
168 base::Unretained(this))); 193 base::Unretained(this)));
169 web_ui()->RegisterMessageCallback( 194 web_ui()->RegisterMessageCallback(
170 net_log::kStartNetLogHandler, 195 net_log::kStartNetLogHandler,
171 base::Bind(&NetExportMessageHandler::OnStartNetLog, 196 base::Bind(&NetExportMessageHandler::OnStartNetLog,
172 base::Unretained(this))); 197 base::Unretained(this)));
173 web_ui()->RegisterMessageCallback( 198 web_ui()->RegisterMessageCallback(
174 net_log::kStopNetLogHandler, 199 net_log::kStopNetLogHandler,
(...skipping 20 matching lines...) Expand all
195 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { 220 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) {
196 DCHECK_CURRENTLY_ON(BrowserThread::UI); 221 DCHECK_CURRENTLY_ON(BrowserThread::UI);
197 std::string capture_mode_string; 222 std::string capture_mode_string;
198 bool result = list->GetString(0, &capture_mode_string); 223 bool result = list->GetString(0, &capture_mode_string);
199 DCHECK(result); 224 DCHECK(result);
200 225
201 capture_mode_ = 226 capture_mode_ =
202 net_log::NetLogFileWriter::CaptureModeFromString(capture_mode_string); 227 net_log::NetLogFileWriter::CaptureModeFromString(capture_mode_string);
203 228
204 if (UsingMobileUI()) { 229 if (UsingMobileUI()) {
205 file_writer_->StartNetLog(base::FilePath(), capture_mode_); 230 file_writer_->StartNetLog(base::FilePath(), capture_mode_,
231 context_getters_);
206 } else { 232 } else {
207 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? 233 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ?
208 DownloadPrefs::FromBrowserContext( 234 DownloadPrefs::FromBrowserContext(
209 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : 235 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() :
210 *last_save_dir.Pointer(); 236 *last_save_dir.Pointer();
211 base::FilePath initial_path = 237 base::FilePath initial_path =
212 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); 238 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json"));
213 ShowSelectFileDialog(initial_path); 239 ShowSelectFileDialog(initial_path);
214 } 240 }
215 } 241 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 } 273 }
248 274
249 void NetExportMessageHandler::FileSelected(const base::FilePath& path, 275 void NetExportMessageHandler::FileSelected(const base::FilePath& path,
250 int index, 276 int index,
251 void* params) { 277 void* params) {
252 DCHECK_CURRENTLY_ON(BrowserThread::UI); 278 DCHECK_CURRENTLY_ON(BrowserThread::UI);
253 DCHECK(select_file_dialog_); 279 DCHECK(select_file_dialog_);
254 select_file_dialog_ = nullptr; 280 select_file_dialog_ = nullptr;
255 *last_save_dir.Pointer() = path.DirName(); 281 *last_save_dir.Pointer() = path.DirName();
256 282
257 file_writer_->StartNetLog(path, capture_mode_); 283 file_writer_->StartNetLog(path, capture_mode_, context_getters_);
258 } 284 }
259 285
260 void NetExportMessageHandler::FileSelectionCanceled(void* params) { 286 void NetExportMessageHandler::FileSelectionCanceled(void* params) {
261 DCHECK(select_file_dialog_); 287 DCHECK(select_file_dialog_);
262 select_file_dialog_ = nullptr; 288 select_file_dialog_ = nullptr;
263 } 289 }
264 290
265 void NetExportMessageHandler::OnNewState(const base::DictionaryValue& state) { 291 void NetExportMessageHandler::OnNewState(const base::DictionaryValue& state) {
266 NotifyUIWithState(state.CreateDeepCopy()); 292 NotifyUIWithState(state.CreateDeepCopy());
267 } 293 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 350
325 } // namespace 351 } // namespace
326 352
327 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { 353 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
328 web_ui->AddMessageHandler(base::MakeUnique<NetExportMessageHandler>()); 354 web_ui->AddMessageHandler(base::MakeUnique<NetExportMessageHandler>());
329 355
330 // Set up the chrome://net-export/ source. 356 // Set up the chrome://net-export/ source.
331 Profile* profile = Profile::FromWebUI(web_ui); 357 Profile* profile = Profile::FromWebUI(web_ui);
332 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); 358 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource());
333 } 359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698