| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/debugger/devtools_file_util.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/download/download_prefs.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/shell_dialogs.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class SaveAsDialog : public SelectFileDialog::Listener, | |
| 18 public base::RefCounted<SaveAsDialog> { | |
| 19 public: | |
| 20 SaveAsDialog() { | |
| 21 select_file_dialog_ = SelectFileDialog::Create(this); | |
| 22 } | |
| 23 | |
| 24 void Show(Profile* profile, | |
| 25 const std::string& suggested_file_name, | |
| 26 const std::string& content) { | |
| 27 AddRef(); // Balanced in the three listener outcomes. | |
| 28 | |
| 29 content_ = content; | |
| 30 | |
| 31 FilePath default_path; | |
| 32 | |
| 33 std::string file_name; | |
| 34 if (suggested_file_name.length() > 20) | |
| 35 file_name = suggested_file_name.substr(0, 20); | |
| 36 else | |
| 37 file_name = suggested_file_name; | |
| 38 | |
| 39 if (!last_save_path_.empty()) { | |
| 40 default_path = last_save_path_.DirName().AppendASCII(suggested_file_name); | |
| 41 } else { | |
| 42 DownloadPrefs prefs(profile->GetPrefs()); | |
| 43 default_path = prefs.download_path().AppendASCII(suggested_file_name); | |
| 44 } | |
| 45 | |
| 46 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | |
| 47 string16(), | |
| 48 default_path, | |
| 49 NULL, | |
| 50 0, | |
| 51 FILE_PATH_LITERAL(""), | |
| 52 NULL, | |
| 53 NULL, | |
| 54 NULL); | |
| 55 } | |
| 56 | |
| 57 // SelectFileDialog::Listener implementation. | |
| 58 virtual void FileSelected(const FilePath& path, | |
| 59 int index, void* params) { | |
| 60 last_save_path_ = path; | |
| 61 | |
| 62 BrowserThread::PostTask( | |
| 63 BrowserThread::FILE, FROM_HERE, | |
| 64 NewRunnableFunction( | |
| 65 &SaveAsDialog::WriteFile, | |
| 66 path, | |
| 67 content_)); | |
| 68 Release(); // Balanced in ::Show. | |
| 69 } | |
| 70 | |
| 71 virtual void MultiFilesSelected( | |
| 72 const std::vector<FilePath>& files, void* params) { | |
| 73 Release(); // Balanced in ::Show. | |
| 74 NOTREACHED() << "Should not be able to select multiple files"; | |
| 75 } | |
| 76 | |
| 77 virtual void FileSelectionCanceled(void* params) { | |
| 78 Release(); // Balanced in ::Show. | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 friend class base::RefCounted<SaveAsDialog>; | |
| 83 virtual ~SaveAsDialog() {} | |
| 84 | |
| 85 static void WriteFile(FilePath path, const std::string& content) { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 87 DCHECK(!path.empty()); | |
| 88 | |
| 89 file_util::WriteFile(path, content.c_str(), content.length()); | |
| 90 } | |
| 91 scoped_refptr<SelectFileDialog> select_file_dialog_; | |
| 92 static FilePath last_save_path_; | |
| 93 std::string content_; | |
| 94 }; | |
| 95 | |
| 96 // static | |
| 97 FilePath SaveAsDialog::last_save_path_; | |
| 98 | |
| 99 } // namespace | |
| 100 | |
| 101 // static | |
| 102 void DevToolsFileUtil::SaveAs(Profile* profile, | |
| 103 const std::string& suggested_file_name, | |
| 104 const std::string& content) { | |
| 105 scoped_refptr<SaveAsDialog> dialog = new SaveAsDialog(); | |
| 106 dialog->Show(profile, suggested_file_name, content); | |
| 107 } | |
| OLD | NEW |