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

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

Issue 2444523003: Have net-export's save dialog default to last used save directory (Closed)
Patch Set: Added DCHECK_CURRENTLY_ON for functions that access last_save_dir Created 4 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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 // May only be accessed on the UI thread
47 base::LazyInstance<base::FilePath>::Leaky
48 last_save_dir = LAZY_INSTANCE_INITIALIZER;
49
44 content::WebUIDataSource* CreateNetExportHTMLSource() { 50 content::WebUIDataSource* CreateNetExportHTMLSource() {
45 content::WebUIDataSource* source = 51 content::WebUIDataSource* source =
46 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); 52 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost);
47 53
48 source->SetJsonPath("strings.js"); 54 source->SetJsonPath("strings.js");
49 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); 55 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS);
50 source->SetDefaultResource(IDR_NET_LOG_NET_EXPORT_HTML); 56 source->SetDefaultResource(IDR_NET_LOG_NET_EXPORT_HTML);
51 return source; 57 return source;
52 } 58 }
53 59
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 187
182 void NetExportMessageHandler::OnGetExportNetLogInfo( 188 void NetExportMessageHandler::OnGetExportNetLogInfo(
183 const base::ListValue* list) { 189 const base::ListValue* list) {
184 BrowserThread::PostTask( 190 BrowserThread::PostTask(
185 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, 191 BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
186 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo, 192 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo,
187 weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_)); 193 weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_));
188 } 194 }
189 195
190 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { 196 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) {
197 DCHECK_CURRENTLY_ON(BrowserThread::UI);
191 bool result = list->GetString(0, &log_mode_); 198 bool result = list->GetString(0, &log_mode_);
192 DCHECK(result); 199 DCHECK(result);
193 200
194 if (UsingMobileUI()) { 201 if (UsingMobileUI()) {
195 StartNetLog(); 202 StartNetLog();
196 } else { 203 } else {
197 base::FilePath home_dir = base::GetHomeDir(); 204 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ?
198 base::FilePath default_path = 205 DownloadPrefs::FromBrowserContext(
199 home_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); 206 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() :
200 ShowSelectFileDialog(default_path); 207 *last_save_dir.Pointer();
208 base::FilePath initial_path =
209 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json"));
210 ShowSelectFileDialog(initial_path);
201 } 211 }
202 } 212 }
203 213
204 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) { 214 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) {
205 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_, 215 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_,
206 net_log::NetLogFileWriter::DO_STOP); 216 net_log::NetLogFileWriter::DO_STOP);
207 } 217 }
208 218
209 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) { 219 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) {
210 content::BrowserThread::PostTaskAndReplyWithResult( 220 content::BrowserThread::PostTaskAndReplyWithResult(
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}}; 335 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}};
326 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow(); 336 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow();
327 select_file_dialog_->SelectFile( 337 select_file_dialog_->SelectFile(
328 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, 338 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path,
329 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); 339 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr);
330 } 340 }
331 341
332 void NetExportMessageHandler::FileSelected(const base::FilePath& path, 342 void NetExportMessageHandler::FileSelected(const base::FilePath& path,
333 int index, 343 int index,
334 void* params) { 344 void* params) {
345 DCHECK_CURRENTLY_ON(BrowserThread::UI);
335 DCHECK(select_file_dialog_); 346 DCHECK(select_file_dialog_);
336 select_file_dialog_ = nullptr; 347 select_file_dialog_ = nullptr;
337 348
349 *last_save_dir.Pointer() = path.DirName();
338 BrowserThread::PostTaskAndReply( 350 BrowserThread::PostTaskAndReply(
339 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, 351 BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
340 base::Bind(&net_log::NetLogFileWriter::SetUpNetExportLogPath, 352 base::Bind(&net_log::NetLogFileWriter::SetUpNetExportLogPath,
341 base::Unretained(net_log_file_writer_), path), 353 base::Unretained(net_log_file_writer_), path),
342 // NetExportMessageHandler is tied to the lifetime of the tab 354 // NetExportMessageHandler is tied to the lifetime of the tab
343 // so it cannot be assured that it will be valid when this 355 // so it cannot be assured that it will be valid when this
344 // StartNetLog is called. Instead of using base::Unretained a 356 // StartNetLog is called. Instead of using base::Unretained a
345 // weak pointer is used to adjust for this. 357 // weak pointer is used to adjust for this.
346 base::Bind(&NetExportMessageHandler::StartNetLog, 358 base::Bind(&NetExportMessageHandler::StartNetLog,
347 weak_ptr_factory_.GetWeakPtr())); 359 weak_ptr_factory_.GetWeakPtr()));
348 } 360 }
349 361
350 void NetExportMessageHandler::FileSelectionCanceled(void* params) { 362 void NetExportMessageHandler::FileSelectionCanceled(void* params) {
351 DCHECK(select_file_dialog_); 363 DCHECK(select_file_dialog_);
352 select_file_dialog_ = nullptr; 364 select_file_dialog_ = nullptr;
353 } 365 }
354 366
355 } // namespace 367 } // namespace
356 368
357 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { 369 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) {
358 web_ui->AddMessageHandler(new NetExportMessageHandler()); 370 web_ui->AddMessageHandler(new NetExportMessageHandler());
359 371
360 // Set up the chrome://net-export/ source. 372 // Set up the chrome://net-export/ source.
361 Profile* profile = Profile::FromWebUI(web_ui); 373 Profile* profile = Profile::FromWebUI(web_ui);
362 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); 374 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource());
363 } 375 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698