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