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

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: Updated ios NetExportMessageHandler Created 3 years, 9 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;
44 using content::WebUIMessageHandler; 48 using content::WebUIMessageHandler;
45 49
46 namespace { 50 namespace {
47 51
52 class ProxyScriptFetcherContextGetter : public net::URLRequestContextGetter {
53 public:
54 explicit ProxyScriptFetcherContextGetter(IOThread* io_thread)
55 : io_thread_(io_thread) {}
56
57 // net::URLRequestContextGetter implementation.
eroman 2017/02/24 00:17:38 [optional] I suggest removing this comment and the
wangyix1 2017/02/24 00:58:07 Done.
58 net::URLRequestContext* GetURLRequestContext() override {
59 DCHECK_CURRENTLY_ON(BrowserThread::IO);
60 DCHECK(io_thread_->globals()->proxy_script_fetcher_context.get());
61 return io_thread_->globals()->proxy_script_fetcher_context.get();
62 }
63
64 // net::URLRequestContextGetter implementation.
65 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
66 const override {
67 return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
68 }
69
70 protected:
71 ~ProxyScriptFetcherContextGetter() override {}
72
73 private:
74 IOThread* const io_thread_; // Owned by BrowserProcess.
75 };
76
48 // May only be accessed on the UI thread 77 // May only be accessed on the UI thread
49 base::LazyInstance<base::FilePath>::Leaky 78 base::LazyInstance<base::FilePath>::Leaky
50 last_save_dir = LAZY_INSTANCE_INITIALIZER; 79 last_save_dir = LAZY_INSTANCE_INITIALIZER;
51 80
52 content::WebUIDataSource* CreateNetExportHTMLSource() { 81 content::WebUIDataSource* CreateNetExportHTMLSource() {
53 content::WebUIDataSource* source = 82 content::WebUIDataSource* source =
54 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); 83 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost);
55 84
56 source->SetJsonPath("strings.js"); 85 source->SetJsonPath("strings.js");
57 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); 86 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 // ui::SelectFileDialog::Listener implementation. 120 // ui::SelectFileDialog::Listener implementation.
92 void FileSelected(const base::FilePath& path, 121 void FileSelected(const base::FilePath& path,
93 int index, 122 int index,
94 void* params) override; 123 void* params) override;
95 void FileSelectionCanceled(void* params) override; 124 void FileSelectionCanceled(void* params) override;
96 125
97 // net_log::NetLogFileWriter::StateObserver implementation. 126 // net_log::NetLogFileWriter::StateObserver implementation.
98 void OnNewState(const base::DictionaryValue& state) override; 127 void OnNewState(const base::DictionaryValue& state) override;
99 128
100 private: 129 private:
130 using URLRequestContextGetterList =
131 std::vector<scoped_refptr<net::URLRequestContextGetter>>;
132
101 // Send NetLog data via email. 133 // Send NetLog data via email.
102 static void SendEmail(const base::FilePath& file_to_send); 134 static void SendEmail(const base::FilePath& file_to_send);
103 135
104 // chrome://net-export can be used on both mobile and desktop platforms. 136 // 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. 137 // 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 138 // 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 139 // 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 140 // 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 141 // location, does not. Furthermore, since every time a user starts logging
110 // to a new NetLog file on mobile platforms it overwrites the previous 142 // to a new NetLog file on mobile platforms it overwrites the previous
111 // NetLog file, a warning message appears on the Start Logging button 143 // NetLog file, a warning message appears on the Start Logging button
112 // that informs the user of this. This does not exist on the desktop 144 // that informs the user of this. This does not exist on the desktop
113 // UI. 145 // UI.
114 static bool UsingMobileUI(); 146 static bool UsingMobileUI();
115 147
116 // Calls NetExportView.onExportNetLogInfoChanged JavaScript function in the 148 // Calls NetExportView.onExportNetLogInfoChanged JavaScript function in the
117 // renderer, passing in |file_writer_state|. 149 // renderer, passing in |file_writer_state|.
118 void NotifyUIWithState(std::unique_ptr<base::DictionaryValue> state); 150 void NotifyUIWithState(std::unique_ptr<base::DictionaryValue> state);
119 151
120 // Opens the SelectFileDialog UI with the default path to save a 152 // Opens the SelectFileDialog UI with the default path to save a
121 // NetLog file. 153 // NetLog file.
122 void ShowSelectFileDialog(const base::FilePath& default_path); 154 void ShowSelectFileDialog(const base::FilePath& default_path);
123 155
156 // Returns a list of context getters used to retrieve ongoing events when
157 // logging starts so that net log entries can be added for those events.
158 URLRequestContextGetterList GetURLRequestContexts() const;
159
124 // Cache of g_browser_process->net_log()->net_log_file_writer(). This 160 // Cache of g_browser_process->net_log()->net_log_file_writer(). This
125 // is owned by ChromeNetLog which is owned by BrowserProcessImpl. 161 // is owned by ChromeNetLog which is owned by BrowserProcessImpl.
126 net_log::NetLogFileWriter* file_writer_; 162 net_log::NetLogFileWriter* file_writer_;
127 163
128 ScopedObserver<net_log::NetLogFileWriter, 164 ScopedObserver<net_log::NetLogFileWriter,
129 net_log::NetLogFileWriter::StateObserver> 165 net_log::NetLogFileWriter::StateObserver>
130 state_observer_manager_; 166 state_observer_manager_;
131 167
132 // The capture mode the user chose in the UI when logging started is cached 168 // 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. 169 // here and is read after a file path is chosen in the save dialog.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { 231 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) {
196 DCHECK_CURRENTLY_ON(BrowserThread::UI); 232 DCHECK_CURRENTLY_ON(BrowserThread::UI);
197 std::string capture_mode_string; 233 std::string capture_mode_string;
198 bool result = list->GetString(0, &capture_mode_string); 234 bool result = list->GetString(0, &capture_mode_string);
199 DCHECK(result); 235 DCHECK(result);
200 236
201 capture_mode_ = 237 capture_mode_ =
202 net_log::NetLogFileWriter::CaptureModeFromString(capture_mode_string); 238 net_log::NetLogFileWriter::CaptureModeFromString(capture_mode_string);
203 239
204 if (UsingMobileUI()) { 240 if (UsingMobileUI()) {
205 file_writer_->StartNetLog(base::FilePath(), capture_mode_); 241 file_writer_->StartNetLog(base::FilePath(), capture_mode_,
242 GetURLRequestContexts());
206 } else { 243 } else {
207 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? 244 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ?
208 DownloadPrefs::FromBrowserContext( 245 DownloadPrefs::FromBrowserContext(
209 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : 246 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() :
210 *last_save_dir.Pointer(); 247 *last_save_dir.Pointer();
211 base::FilePath initial_path = 248 base::FilePath initial_path =
212 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); 249 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json"));
213 ShowSelectFileDialog(initial_path); 250 ShowSelectFileDialog(initial_path);
214 } 251 }
215 } 252 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 } 284 }
248 285
249 void NetExportMessageHandler::FileSelected(const base::FilePath& path, 286 void NetExportMessageHandler::FileSelected(const base::FilePath& path,
250 int index, 287 int index,
251 void* params) { 288 void* params) {
252 DCHECK_CURRENTLY_ON(BrowserThread::UI); 289 DCHECK_CURRENTLY_ON(BrowserThread::UI);
253 DCHECK(select_file_dialog_); 290 DCHECK(select_file_dialog_);
254 select_file_dialog_ = nullptr; 291 select_file_dialog_ = nullptr;
255 *last_save_dir.Pointer() = path.DirName(); 292 *last_save_dir.Pointer() = path.DirName();
256 293
257 file_writer_->StartNetLog(path, capture_mode_); 294 file_writer_->StartNetLog(path, capture_mode_, GetURLRequestContexts());
258 } 295 }
259 296
260 void NetExportMessageHandler::FileSelectionCanceled(void* params) { 297 void NetExportMessageHandler::FileSelectionCanceled(void* params) {
261 DCHECK(select_file_dialog_); 298 DCHECK(select_file_dialog_);
262 select_file_dialog_ = nullptr; 299 select_file_dialog_ = nullptr;
263 } 300 }
264 301
265 void NetExportMessageHandler::OnNewState(const base::DictionaryValue& state) { 302 void NetExportMessageHandler::OnNewState(const base::DictionaryValue& state) {
266 NotifyUIWithState(state.CreateDeepCopy()); 303 NotifyUIWithState(state.CreateDeepCopy());
267 } 304 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 select_file_dialog_ = ui::SelectFileDialog::Create( 352 select_file_dialog_ = ui::SelectFileDialog::Create(
316 this, new ChromeSelectFilePolicy(webcontents)); 353 this, new ChromeSelectFilePolicy(webcontents));
317 ui::SelectFileDialog::FileTypeInfo file_type_info; 354 ui::SelectFileDialog::FileTypeInfo file_type_info;
318 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}}; 355 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}};
319 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow(); 356 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow();
320 select_file_dialog_->SelectFile( 357 select_file_dialog_->SelectFile(
321 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, 358 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path,
322 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); 359 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr);
323 } 360 }
324 361
362 NetExportMessageHandler::URLRequestContextGetterList
363 NetExportMessageHandler::GetURLRequestContexts() const {
364 DCHECK_CURRENTLY_ON(BrowserThread::UI);
365
366 URLRequestContextGetterList context_getters;
367
368 Profile* profile = Profile::FromWebUI(web_ui());
369
370 context_getters.push_back(profile->GetRequestContext());
371 context_getters.push_back(
372 content::BrowserContext::GetDefaultStoragePartition(profile)
373 ->GetMediaURLRequestContext());
374 #if BUILDFLAG(ENABLE_EXTENSIONS)
375 context_getters.push_back(profile->GetRequestContextForExtensions());
376 #endif
377 context_getters.push_back(
378 g_browser_process->io_thread()->system_url_request_context_getter());
379 context_getters.push_back(
380 new ProxyScriptFetcherContextGetter(g_browser_process->io_thread()));
381
382 return context_getters;
383 }
384
325 } // namespace 385 } // namespace
326 386
327 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { 387 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
328 web_ui->AddMessageHandler(base::MakeUnique<NetExportMessageHandler>()); 388 web_ui->AddMessageHandler(base::MakeUnique<NetExportMessageHandler>());
329 389
330 // Set up the chrome://net-export/ source. 390 // Set up the chrome://net-export/ source.
331 Profile* profile = Profile::FromWebUI(web_ui); 391 Profile* profile = Profile::FromWebUI(web_ui);
332 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); 392 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource());
333 } 393 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698