Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview_handler.cc |
| diff --git a/chrome/browser/ui/webui/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview_handler.cc |
| index 9811baf05001402a808f2ac6898a8fa00ca94682..15d90d2fe39adc83dd9424459688e9896485e6c0 100644 |
| --- a/chrome/browser/ui/webui/print_preview_handler.cc |
| +++ b/chrome/browser/ui/webui/print_preview_handler.cc |
| @@ -6,21 +6,29 @@ |
| #include <string> |
| +#include "base/i18n/file_util_icu.h" |
| #include "base/json/json_reader.h" |
| +#include "base/path_service.h" |
| #include "base/threading/thread.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "base/utf_string_conversions.h" |
| #include "base/values.h" |
| #include "chrome/browser/platform_util.h" |
| #include "chrome/browser/printing/print_preview_tab_controller.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" |
| #include "chrome/browser/ui/webui/print_preview_ui.h" |
| +#include "chrome/common/chrome_paths.h" |
| #include "chrome/common/print_messages.h" |
| #include "content/browser/browser_thread.h" |
| #include "content/browser/renderer_host/render_view_host.h" |
| #include "content/browser/tab_contents/tab_contents.h" |
| +#include "grit/generated_resources.h" |
|
Lei Zhang
2011/04/14 22:53:43
no longer needed
|
| #include "printing/backend/print_backend.h" |
| #include "printing/metafile.h" |
| #include "printing/metafile_impl.h" |
| #include "printing/print_job_constants.h" |
| +#include "ui/base/l10n/l10n_util.h" |
|
Lei Zhang
2011/04/14 22:53:43
no longer needed
dpapad
2011/04/14 23:38:59
Thanks for catching these.
|
| namespace { |
| @@ -140,6 +148,8 @@ class PrintToPdfTask : public Task { |
| FilePath path_; |
| }; |
| +FilePath* PrintPreviewHandler::last_saved_path_ = NULL; |
|
Lei Zhang
2011/04/14 22:53:43
add // static comment
dpapad
2011/04/14 23:38:59
Done.
|
| + |
| PrintPreviewHandler::PrintPreviewHandler() |
| : print_backend_(printing::PrintBackend::CreateInstance(NULL)), |
| need_to_generate_preview_(true), |
| @@ -202,11 +212,30 @@ void PrintPreviewHandler::HandlePrint(const ListValue* args) { |
| if (!settings.get()) |
| return; |
| - bool print_to_pdf; |
| + bool print_to_pdf = false; |
| settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| if (print_to_pdf) { |
| - SelectFile(); |
| + // Pre-populating select file dialog with print job title. |
| + TabContentsWrapper* wrapper = |
| + TabContentsWrapper::GetCurrentWrapperForContents( |
| + web_ui_->tab_contents()); |
| + |
| + string16 print_job_title_temp = |
|
Lei Zhang
2011/04/14 22:53:43
how about s/temp/utf16/
dpapad
2011/04/14 23:38:59
Done.
|
| + wrapper->print_view_manager()->RenderSourceName(); |
| + |
| +#if defined(OS_WIN) |
| + FilePath::StringType print_job_title(print_job_title_temp); |
| +#elif defined(OS_POSIX) |
| + FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_temp); |
| +#endif |
| + |
| + file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); |
| + FilePath default_filename(print_job_title); |
| + default_filename = |
| + default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); |
| + |
| + SelectFile(default_filename); |
| } else { |
| RenderViewHost* rvh = web_ui_->GetRenderViewHost(); |
| rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| @@ -240,18 +269,25 @@ void PrintPreviewHandler::ProcessLandscapeSetting( |
| } |
| } |
| -void PrintPreviewHandler::SelectFile() { |
| +void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { |
| SelectFileDialog::FileTypeInfo file_type_info; |
| file_type_info.extensions.resize(1); |
| file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| + // Initializing last_saved_path_ if it is not already initialized. |
| + if (!last_saved_path_) { |
| + last_saved_path_ = new FilePath(); |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
|
Lei Zhang
2011/04/14 22:53:43
You should add a comment to explain why this is ok
dpapad
2011/04/14 23:38:59
Done.
|
| + PathService::Get(chrome::DIR_USER_DOCUMENTS, last_saved_path_); |
| + } |
| + |
| if (!select_file_dialog_.get()) |
| select_file_dialog_ = SelectFileDialog::Create(this); |
| select_file_dialog_->SelectFile( |
| SelectFileDialog::SELECT_SAVEAS_FILE, |
| string16(), |
| - FilePath(), |
| + last_saved_path_->Append(default_filename), |
| &file_type_info, |
| 0, |
| FILE_PATH_LITERAL(""), |
| @@ -271,6 +307,9 @@ void PrintPreviewHandler::FileSelected(const FilePath& path, |
| printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
| metafile->InitFromData(data.first->memory(), data.second); |
| + // Updating last_saved_path_ to the newly selected folder. |
| + *last_saved_path_ = path.DirName(); |
| + |
| PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
| BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| } |