Chromium Code Reviews| 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" |
| 26 #include "grit/generated_resources.h" | |
|
Lei Zhang
2011/04/14 22:53:43
no longer needed
| |
| 20 #include "printing/backend/print_backend.h" | 27 #include "printing/backend/print_backend.h" |
| 21 #include "printing/metafile.h" | 28 #include "printing/metafile.h" |
| 22 #include "printing/metafile_impl.h" | 29 #include "printing/metafile_impl.h" |
| 23 #include "printing/print_job_constants.h" | 30 #include "printing/print_job_constants.h" |
| 31 #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.
| |
| 24 | 32 |
| 25 namespace { | 33 namespace { |
| 26 | 34 |
| 27 const bool kColorDefaultValue = false; | 35 const bool kColorDefaultValue = false; |
| 28 const bool kLandscapeDefaultValue = false; | 36 const bool kLandscapeDefaultValue = false; |
| 29 | 37 |
| 30 TabContents* GetInitiatorTab(TabContents* preview_tab) { | 38 TabContents* GetInitiatorTab(TabContents* preview_tab) { |
| 31 printing::PrintPreviewTabController* tab_controller = | 39 printing::PrintPreviewTabController* tab_controller = |
| 32 printing::PrintPreviewTabController::GetInstance(); | 40 printing::PrintPreviewTabController::GetInstance(); |
| 33 if (!tab_controller) | 41 if (!tab_controller) |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 } | 141 } |
| 134 | 142 |
| 135 private: | 143 private: |
| 136 // The metafile holding the PDF data. | 144 // The metafile holding the PDF data. |
| 137 scoped_ptr<printing::Metafile> metafile_; | 145 scoped_ptr<printing::Metafile> metafile_; |
| 138 | 146 |
| 139 // The absolute path where the file will be saved. | 147 // The absolute path where the file will be saved. |
| 140 FilePath path_; | 148 FilePath path_; |
| 141 }; | 149 }; |
| 142 | 150 |
| 151 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.
| |
| 152 | |
| 143 PrintPreviewHandler::PrintPreviewHandler() | 153 PrintPreviewHandler::PrintPreviewHandler() |
| 144 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), | 154 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), |
| 145 need_to_generate_preview_(true), | 155 need_to_generate_preview_(true), |
| 146 color_(kColorDefaultValue), | 156 color_(kColorDefaultValue), |
| 147 landscape_(kLandscapeDefaultValue) { | 157 landscape_(kLandscapeDefaultValue) { |
| 148 } | 158 } |
| 149 | 159 |
| 150 PrintPreviewHandler::~PrintPreviewHandler() { | 160 PrintPreviewHandler::~PrintPreviewHandler() { |
| 151 if (select_file_dialog_.get()) | 161 if (select_file_dialog_.get()) |
| 152 select_file_dialog_->ListenerDestroyed(); | 162 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()); | 205 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); |
| 196 if (initiator_tab) { | 206 if (initiator_tab) { |
| 197 RenderViewHost* rvh = initiator_tab->render_view_host(); | 207 RenderViewHost* rvh = initiator_tab->render_view_host(); |
| 198 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); | 208 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); |
| 199 } | 209 } |
| 200 | 210 |
| 201 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 211 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 202 if (!settings.get()) | 212 if (!settings.get()) |
| 203 return; | 213 return; |
| 204 | 214 |
| 205 bool print_to_pdf; | 215 bool print_to_pdf = false; |
| 206 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 216 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 207 | 217 |
| 208 if (print_to_pdf) { | 218 if (print_to_pdf) { |
| 209 SelectFile(); | 219 // Pre-populating select file dialog with print job title. |
| 220 TabContentsWrapper* wrapper = | |
| 221 TabContentsWrapper::GetCurrentWrapperForContents( | |
| 222 web_ui_->tab_contents()); | |
| 223 | |
| 224 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.
| |
| 225 wrapper->print_view_manager()->RenderSourceName(); | |
| 226 | |
| 227 #if defined(OS_WIN) | |
| 228 FilePath::StringType print_job_title(print_job_title_temp); | |
| 229 #elif defined(OS_POSIX) | |
| 230 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_temp); | |
| 231 #endif | |
| 232 | |
| 233 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | |
| 234 FilePath default_filename(print_job_title); | |
| 235 default_filename = | |
| 236 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | |
| 237 | |
| 238 SelectFile(default_filename); | |
| 210 } else { | 239 } else { |
| 211 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); | 240 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); |
| 212 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 241 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 213 } | 242 } |
| 214 } | 243 } |
| 215 | 244 |
| 216 void PrintPreviewHandler::SendPrinterList( | 245 void PrintPreviewHandler::SendPrinterList( |
| 217 const ListValue& printers, | 246 const ListValue& printers, |
| 218 const FundamentalValue& default_printer_index) { | 247 const FundamentalValue& default_printer_index) { |
| 219 web_ui_->CallJavascriptFunction("setPrinters", printers, | 248 web_ui_->CallJavascriptFunction("setPrinters", printers, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 233 void PrintPreviewHandler::ProcessLandscapeSetting( | 262 void PrintPreviewHandler::ProcessLandscapeSetting( |
| 234 const DictionaryValue& settings) { | 263 const DictionaryValue& settings) { |
| 235 bool landscape = kLandscapeDefaultValue; | 264 bool landscape = kLandscapeDefaultValue; |
| 236 settings.GetBoolean(printing::kSettingLandscape, &landscape); | 265 settings.GetBoolean(printing::kSettingLandscape, &landscape); |
| 237 if (landscape != landscape_) { | 266 if (landscape != landscape_) { |
| 238 landscape_ = landscape; | 267 landscape_ = landscape; |
| 239 need_to_generate_preview_ = true; | 268 need_to_generate_preview_ = true; |
| 240 } | 269 } |
| 241 } | 270 } |
| 242 | 271 |
| 243 void PrintPreviewHandler::SelectFile() { | 272 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { |
| 244 SelectFileDialog::FileTypeInfo file_type_info; | 273 SelectFileDialog::FileTypeInfo file_type_info; |
| 245 file_type_info.extensions.resize(1); | 274 file_type_info.extensions.resize(1); |
| 246 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 275 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 247 | 276 |
| 277 // Initializing last_saved_path_ if it is not already initialized. | |
| 278 if (!last_saved_path_) { | |
| 279 last_saved_path_ = new FilePath(); | |
| 280 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.
| |
| 281 PathService::Get(chrome::DIR_USER_DOCUMENTS, last_saved_path_); | |
| 282 } | |
| 283 | |
| 248 if (!select_file_dialog_.get()) | 284 if (!select_file_dialog_.get()) |
| 249 select_file_dialog_ = SelectFileDialog::Create(this); | 285 select_file_dialog_ = SelectFileDialog::Create(this); |
| 250 | 286 |
| 251 select_file_dialog_->SelectFile( | 287 select_file_dialog_->SelectFile( |
| 252 SelectFileDialog::SELECT_SAVEAS_FILE, | 288 SelectFileDialog::SELECT_SAVEAS_FILE, |
| 253 string16(), | 289 string16(), |
| 254 FilePath(), | 290 last_saved_path_->Append(default_filename), |
| 255 &file_type_info, | 291 &file_type_info, |
| 256 0, | 292 0, |
| 257 FILE_PATH_LITERAL(""), | 293 FILE_PATH_LITERAL(""), |
| 258 platform_util::GetTopLevel( | 294 platform_util::GetTopLevel( |
| 259 web_ui_->tab_contents()->GetNativeView()), | 295 web_ui_->tab_contents()->GetNativeView()), |
| 260 NULL); | 296 NULL); |
| 261 } | 297 } |
| 262 | 298 |
| 263 void PrintPreviewHandler::FileSelected(const FilePath& path, | 299 void PrintPreviewHandler::FileSelected(const FilePath& path, |
| 264 int index, void* params) { | 300 int index, void* params) { |
| 265 PrintPreviewUIHTMLSource::PrintPreviewData data; | 301 PrintPreviewUIHTMLSource::PrintPreviewData data; |
| 266 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); | 302 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); |
| 267 pp_ui->html_source()->GetPrintPreviewData(&data); | 303 pp_ui->html_source()->GetPrintPreviewData(&data); |
| 268 DCHECK(data.first != NULL); | 304 DCHECK(data.first != NULL); |
| 269 DCHECK(data.second > 0); | 305 DCHECK(data.second > 0); |
| 270 | 306 |
| 271 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; | 307 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
| 272 metafile->InitFromData(data.first->memory(), data.second); | 308 metafile->InitFromData(data.first->memory(), data.second); |
| 273 | 309 |
| 310 // Updating last_saved_path_ to the newly selected folder. | |
| 311 *last_saved_path_ = path.DirName(); | |
| 312 | |
| 274 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | 313 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
| 275 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 314 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 276 } | 315 } |
| OLD | NEW |