Chromium Code Reviews| 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 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/lazy_instance.h" | |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/download/download_prefs.h" | |
| 17 #include "chrome/browser/platform_util.h" | 19 #include "chrome/browser/platform_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/chrome_select_file_policy.h" | 21 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 20 #include "chrome/common/features.h" | 22 #include "chrome/common/features.h" |
| 21 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 22 #include "components/grit/components_resources.h" | 24 #include "components/grit/components_resources.h" |
| 23 #include "components/net_log/chrome_net_log.h" | 25 #include "components/net_log/chrome_net_log.h" |
| 24 #include "components/net_log/net_export_ui_constants.h" | 26 #include "components/net_log/net_export_ui_constants.h" |
| 25 #include "components/net_log/net_log_file_writer.h" | 27 #include "components/net_log/net_log_file_writer.h" |
| 26 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/url_data_source.h" | 29 #include "content/public/browser/url_data_source.h" |
| 28 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/browser/web_ui.h" | 31 #include "content/public/browser/web_ui.h" |
| 30 #include "content/public/browser/web_ui_data_source.h" | 32 #include "content/public/browser/web_ui_data_source.h" |
| 31 #include "content/public/browser/web_ui_message_handler.h" | 33 #include "content/public/browser/web_ui_message_handler.h" |
| 32 #include "ui/shell_dialogs/select_file_dialog.h" | 34 #include "ui/shell_dialogs/select_file_dialog.h" |
| 33 | 35 |
| 34 #if BUILDFLAG(ANDROID_JAVA_UI) | 36 #if BUILDFLAG(ANDROID_JAVA_UI) |
| 35 #include "chrome/browser/android/intent_helper.h" | 37 #include "chrome/browser/android/intent_helper.h" |
| 36 #endif | 38 #endif |
| 37 | 39 |
| 38 using content::BrowserThread; | 40 using content::BrowserThread; |
| 39 using content::WebContents; | 41 using content::WebContents; |
| 40 using content::WebUIMessageHandler; | 42 using content::WebUIMessageHandler; |
| 41 | 43 |
| 42 namespace { | 44 namespace { |
| 43 | 45 |
| 46 base::LazyInstance<base::FilePath>::Leaky | |
| 47 last_save_dir = LAZY_INSTANCE_INITIALIZER; | |
|
mmenke
2016/11/17 18:50:50
Comment that this may only be accessed on the UI t
wangyix
2016/11/17 19:46:15
Done.
| |
| 48 | |
| 44 content::WebUIDataSource* CreateNetExportHTMLSource() { | 49 content::WebUIDataSource* CreateNetExportHTMLSource() { |
| 45 content::WebUIDataSource* source = | 50 content::WebUIDataSource* source = |
| 46 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); | 51 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); |
| 47 | 52 |
| 48 source->SetJsonPath("strings.js"); | 53 source->SetJsonPath("strings.js"); |
| 49 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); | 54 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); |
| 50 source->SetDefaultResource(IDR_NET_LOG_NET_EXPORT_HTML); | 55 source->SetDefaultResource(IDR_NET_LOG_NET_EXPORT_HTML); |
| 51 return source; | 56 return source; |
| 52 } | 57 } |
| 53 | 58 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_)); | 192 weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_)); |
| 188 } | 193 } |
| 189 | 194 |
| 190 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { | 195 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { |
| 191 bool result = list->GetString(0, &log_mode_); | 196 bool result = list->GetString(0, &log_mode_); |
| 192 DCHECK(result); | 197 DCHECK(result); |
| 193 | 198 |
| 194 if (UsingMobileUI()) { | 199 if (UsingMobileUI()) { |
| 195 StartNetLog(); | 200 StartNetLog(); |
| 196 } else { | 201 } else { |
| 197 base::FilePath home_dir = base::GetHomeDir(); | 202 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? |
| 198 base::FilePath default_path = | 203 DownloadPrefs::FromBrowserContext( |
| 199 home_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); | 204 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : |
| 200 ShowSelectFileDialog(default_path); | 205 *last_save_dir.Pointer(); |
| 206 base::FilePath initial_path = | |
| 207 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); | |
| 208 ShowSelectFileDialog(initial_path); | |
| 201 } | 209 } |
| 202 } | 210 } |
| 203 | 211 |
| 204 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) { | 212 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) { |
| 205 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_, | 213 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_, |
| 206 net_log::NetLogFileWriter::DO_STOP); | 214 net_log::NetLogFileWriter::DO_STOP); |
| 207 } | 215 } |
| 208 | 216 |
| 209 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) { | 217 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) { |
| 210 content::BrowserThread::PostTaskAndReplyWithResult( | 218 content::BrowserThread::PostTaskAndReplyWithResult( |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, | 336 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, |
| 329 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); | 337 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); |
| 330 } | 338 } |
| 331 | 339 |
| 332 void NetExportMessageHandler::FileSelected(const base::FilePath& path, | 340 void NetExportMessageHandler::FileSelected(const base::FilePath& path, |
| 333 int index, | 341 int index, |
| 334 void* params) { | 342 void* params) { |
| 335 DCHECK(select_file_dialog_); | 343 DCHECK(select_file_dialog_); |
| 336 select_file_dialog_ = nullptr; | 344 select_file_dialog_ = nullptr; |
| 337 | 345 |
| 346 *last_save_dir.Pointer() = path.DirName(); | |
| 338 BrowserThread::PostTaskAndReply( | 347 BrowserThread::PostTaskAndReply( |
| 339 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, | 348 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, |
| 340 base::Bind(&net_log::NetLogFileWriter::SetUpNetExportLogPath, | 349 base::Bind(&net_log::NetLogFileWriter::SetUpNetExportLogPath, |
| 341 base::Unretained(net_log_file_writer_), path), | 350 base::Unretained(net_log_file_writer_), path), |
| 342 // NetExportMessageHandler is tied to the lifetime of the tab | 351 // NetExportMessageHandler is tied to the lifetime of the tab |
| 343 // so it cannot be assured that it will be valid when this | 352 // so it cannot be assured that it will be valid when this |
| 344 // StartNetLog is called. Instead of using base::Unretained a | 353 // StartNetLog is called. Instead of using base::Unretained a |
| 345 // weak pointer is used to adjust for this. | 354 // weak pointer is used to adjust for this. |
| 346 base::Bind(&NetExportMessageHandler::StartNetLog, | 355 base::Bind(&NetExportMessageHandler::StartNetLog, |
| 347 weak_ptr_factory_.GetWeakPtr())); | 356 weak_ptr_factory_.GetWeakPtr())); |
| 348 } | 357 } |
| 349 | 358 |
| 350 void NetExportMessageHandler::FileSelectionCanceled(void* params) { | 359 void NetExportMessageHandler::FileSelectionCanceled(void* params) { |
| 351 DCHECK(select_file_dialog_); | 360 DCHECK(select_file_dialog_); |
| 352 select_file_dialog_ = nullptr; | 361 select_file_dialog_ = nullptr; |
| 353 } | 362 } |
| 354 | 363 |
| 355 } // namespace | 364 } // namespace |
| 356 | 365 |
| 357 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 366 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 358 web_ui->AddMessageHandler(new NetExportMessageHandler()); | 367 web_ui->AddMessageHandler(new NetExportMessageHandler()); |
| 359 | 368 |
| 360 // Set up the chrome://net-export/ source. | 369 // Set up the chrome://net-export/ source. |
| 361 Profile* profile = Profile::FromWebUI(web_ui); | 370 Profile* profile = Profile::FromWebUI(web_ui); |
| 362 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); | 371 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); |
| 363 } | 372 } |
| OLD | NEW |