| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/i18n/file_util_icu.h" |
| 9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/path_service.h" |
| 10 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 15 #include "base/values.h" |
| 12 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
| 13 #include "chrome/browser/printing/print_preview_tab_controller.h" | 17 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 14 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" | 19 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" |
| 15 #include "chrome/browser/ui/webui/print_preview_ui.h" | 20 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 21 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/print_messages.h" | 22 #include "chrome/common/print_messages.h" |
| 17 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
| 18 #include "content/browser/renderer_host/render_view_host.h" | 24 #include "content/browser/renderer_host/render_view_host.h" |
| 19 #include "content/browser/tab_contents/tab_contents.h" | 25 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "printing/backend/print_backend.h" | 26 #include "printing/backend/print_backend.h" |
| 21 #include "printing/metafile.h" | 27 #include "printing/metafile.h" |
| 22 #include "printing/metafile_impl.h" | 28 #include "printing/metafile_impl.h" |
| 23 #include "printing/print_job_constants.h" | 29 #include "printing/print_job_constants.h" |
| 24 | 30 |
| 25 namespace { | 31 namespace { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } | 139 } |
| 134 | 140 |
| 135 private: | 141 private: |
| 136 // The metafile holding the PDF data. | 142 // The metafile holding the PDF data. |
| 137 scoped_ptr<printing::Metafile> metafile_; | 143 scoped_ptr<printing::Metafile> metafile_; |
| 138 | 144 |
| 139 // The absolute path where the file will be saved. | 145 // The absolute path where the file will be saved. |
| 140 FilePath path_; | 146 FilePath path_; |
| 141 }; | 147 }; |
| 142 | 148 |
| 149 // static |
| 150 FilePath* PrintPreviewHandler::last_saved_path_ = NULL; |
| 151 |
| 143 PrintPreviewHandler::PrintPreviewHandler() | 152 PrintPreviewHandler::PrintPreviewHandler() |
| 144 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), | 153 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), |
| 145 need_to_generate_preview_(true), | 154 need_to_generate_preview_(true), |
| 146 color_(kColorDefaultValue), | 155 color_(kColorDefaultValue), |
| 147 landscape_(kLandscapeDefaultValue) { | 156 landscape_(kLandscapeDefaultValue) { |
| 148 } | 157 } |
| 149 | 158 |
| 150 PrintPreviewHandler::~PrintPreviewHandler() { | 159 PrintPreviewHandler::~PrintPreviewHandler() { |
| 151 if (select_file_dialog_.get()) | 160 if (select_file_dialog_.get()) |
| 152 select_file_dialog_->ListenerDestroyed(); | 161 select_file_dialog_->ListenerDestroyed(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); | 204 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); |
| 196 if (initiator_tab) { | 205 if (initiator_tab) { |
| 197 RenderViewHost* rvh = initiator_tab->render_view_host(); | 206 RenderViewHost* rvh = initiator_tab->render_view_host(); |
| 198 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); | 207 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); |
| 199 } | 208 } |
| 200 | 209 |
| 201 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 210 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 202 if (!settings.get()) | 211 if (!settings.get()) |
| 203 return; | 212 return; |
| 204 | 213 |
| 205 bool print_to_pdf; | 214 bool print_to_pdf = false; |
| 206 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 215 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 207 | 216 |
| 208 if (print_to_pdf) { | 217 if (print_to_pdf) { |
| 209 SelectFile(); | 218 // Pre-populating select file dialog with print job title. |
| 219 TabContentsWrapper* wrapper = |
| 220 TabContentsWrapper::GetCurrentWrapperForContents( |
| 221 web_ui_->tab_contents()); |
| 222 |
| 223 string16 print_job_title_utf16 = |
| 224 wrapper->print_view_manager()->RenderSourceName(); |
| 225 |
| 226 #if defined(OS_WIN) |
| 227 FilePath::StringType print_job_title(print_job_title_utf16); |
| 228 #elif defined(OS_POSIX) |
| 229 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); |
| 230 #endif |
| 231 |
| 232 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); |
| 233 FilePath default_filename(print_job_title); |
| 234 default_filename = |
| 235 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); |
| 236 |
| 237 SelectFile(default_filename); |
| 210 } else { | 238 } else { |
| 211 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); | 239 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); |
| 212 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 240 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 213 } | 241 } |
| 214 } | 242 } |
| 215 | 243 |
| 216 void PrintPreviewHandler::SendPrinterList( | 244 void PrintPreviewHandler::SendPrinterList( |
| 217 const ListValue& printers, | 245 const ListValue& printers, |
| 218 const FundamentalValue& default_printer_index) { | 246 const FundamentalValue& default_printer_index) { |
| 219 web_ui_->CallJavascriptFunction("setPrinters", printers, | 247 web_ui_->CallJavascriptFunction("setPrinters", printers, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 233 void PrintPreviewHandler::ProcessLandscapeSetting( | 261 void PrintPreviewHandler::ProcessLandscapeSetting( |
| 234 const DictionaryValue& settings) { | 262 const DictionaryValue& settings) { |
| 235 bool landscape = kLandscapeDefaultValue; | 263 bool landscape = kLandscapeDefaultValue; |
| 236 settings.GetBoolean(printing::kSettingLandscape, &landscape); | 264 settings.GetBoolean(printing::kSettingLandscape, &landscape); |
| 237 if (landscape != landscape_) { | 265 if (landscape != landscape_) { |
| 238 landscape_ = landscape; | 266 landscape_ = landscape; |
| 239 need_to_generate_preview_ = true; | 267 need_to_generate_preview_ = true; |
| 240 } | 268 } |
| 241 } | 269 } |
| 242 | 270 |
| 243 void PrintPreviewHandler::SelectFile() { | 271 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { |
| 244 SelectFileDialog::FileTypeInfo file_type_info; | 272 SelectFileDialog::FileTypeInfo file_type_info; |
| 245 file_type_info.extensions.resize(1); | 273 file_type_info.extensions.resize(1); |
| 246 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 274 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 247 | 275 |
| 276 // Initializing last_saved_path_ if it is not already initialized. |
| 277 if (!last_saved_path_) { |
| 278 last_saved_path_ = new FilePath(); |
| 279 // Allowing IO operation temporarily. It is ok to do so here because |
| 280 // the select file dialog performs IO anyway in order to display the |
| 281 // folders and also it is modal. |
| 282 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 283 PathService::Get(chrome::DIR_USER_DOCUMENTS, last_saved_path_); |
| 284 } |
| 285 |
| 248 if (!select_file_dialog_.get()) | 286 if (!select_file_dialog_.get()) |
| 249 select_file_dialog_ = SelectFileDialog::Create(this); | 287 select_file_dialog_ = SelectFileDialog::Create(this); |
| 250 | 288 |
| 251 select_file_dialog_->SelectFile( | 289 select_file_dialog_->SelectFile( |
| 252 SelectFileDialog::SELECT_SAVEAS_FILE, | 290 SelectFileDialog::SELECT_SAVEAS_FILE, |
| 253 string16(), | 291 string16(), |
| 254 FilePath(), | 292 last_saved_path_->Append(default_filename), |
| 255 &file_type_info, | 293 &file_type_info, |
| 256 0, | 294 0, |
| 257 FILE_PATH_LITERAL(""), | 295 FILE_PATH_LITERAL(""), |
| 258 platform_util::GetTopLevel( | 296 platform_util::GetTopLevel( |
| 259 web_ui_->tab_contents()->GetNativeView()), | 297 web_ui_->tab_contents()->GetNativeView()), |
| 260 NULL); | 298 NULL); |
| 261 } | 299 } |
| 262 | 300 |
| 263 void PrintPreviewHandler::FileSelected(const FilePath& path, | 301 void PrintPreviewHandler::FileSelected(const FilePath& path, |
| 264 int index, void* params) { | 302 int index, void* params) { |
| 265 PrintPreviewUIHTMLSource::PrintPreviewData data; | 303 PrintPreviewUIHTMLSource::PrintPreviewData data; |
| 266 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); | 304 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); |
| 267 pp_ui->html_source()->GetPrintPreviewData(&data); | 305 pp_ui->html_source()->GetPrintPreviewData(&data); |
| 268 DCHECK(data.first != NULL); | 306 DCHECK(data.first != NULL); |
| 269 DCHECK(data.second > 0); | 307 DCHECK(data.second > 0); |
| 270 | 308 |
| 271 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; | 309 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
| 272 metafile->InitFromData(data.first->memory(), data.second); | 310 metafile->InitFromData(data.first->memory(), data.second); |
| 273 | 311 |
| 312 // Updating last_saved_path_ to the newly selected folder. |
| 313 *last_saved_path_ = path.DirName(); |
| 314 |
| 274 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | 315 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
| 275 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 316 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 276 } | 317 } |
| OLD | NEW |