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" |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 12 #include "chrome/browser/platform_util.h" | 14 #include "chrome/browser/platform_util.h" |
| 13 #include "chrome/browser/printing/print_preview_tab_controller.h" | 15 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 14 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" | 16 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" |
| 15 #include "chrome/browser/ui/webui/print_preview_ui.h" | 17 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 18 #include "chrome/common/chrome_paths.h" | |
| 16 #include "chrome/common/print_messages.h" | 19 #include "chrome/common/print_messages.h" |
| 17 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
| 18 #include "content/browser/renderer_host/render_view_host.h" | 21 #include "content/browser/renderer_host/render_view_host.h" |
| 19 #include "content/browser/tab_contents/tab_contents.h" | 22 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "printing/backend/print_backend.h" | 23 #include "printing/backend/print_backend.h" |
| 21 #include "printing/native_metafile_factory.h" | 24 #include "printing/native_metafile_factory.h" |
| 22 #include "printing/native_metafile.h" | 25 #include "printing/native_metafile.h" |
| 23 #include "printing/print_job_constants.h" | 26 #include "printing/print_job_constants.h" |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); | 191 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); |
| 189 if (initiator_tab) { | 192 if (initiator_tab) { |
| 190 RenderViewHost* rvh = initiator_tab->render_view_host(); | 193 RenderViewHost* rvh = initiator_tab->render_view_host(); |
| 191 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); | 194 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); |
| 192 } | 195 } |
| 193 | 196 |
| 194 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 197 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 195 if (!settings.get()) | 198 if (!settings.get()) |
| 196 return; | 199 return; |
| 197 | 200 |
| 198 bool print_to_pdf; | 201 bool print_to_pdf; |
|
Lei Zhang
2011/04/01 01:05:35
Can you initialize this to false? Missed it in the
dpapad
2011/04/01 02:32:15
Done.
| |
| 199 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 202 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 200 | 203 |
| 201 if (print_to_pdf) { | 204 if (print_to_pdf) { |
| 202 SelectFile(); | 205 // Pre-populating select file dialog with print job title. |
| 206 std::string print_job_title; | |
| 207 settings->GetString(printing::kPrintJobTitle, &print_job_title); | |
|
Lei Zhang
2011/04/01 01:05:35
Check the return value and handle both the failure
dpapad
2011/04/01 02:32:15
Done.
| |
| 208 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | |
|
Lei Zhang
2011/04/01 01:05:35
This won't work on Windows. You need to write this
dpapad
2011/04/01 02:32:15
Done, without the ifdefs (thanks to overloading ;>
| |
| 209 FilePath default_path; | |
| 210 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path); | |
| 211 FilePath default_filename(print_job_title + ".pdf"); | |
| 212 | |
| 213 SelectFile(default_path.Append(default_filename)); | |
| 203 } else { | 214 } else { |
| 204 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); | 215 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); |
| 205 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 216 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 206 } | 217 } |
| 207 } | 218 } |
| 208 | 219 |
| 209 void PrintPreviewHandler::SendPrinterList(const ListValue& printers) { | 220 void PrintPreviewHandler::SendPrinterList(const ListValue& printers) { |
| 210 web_ui_->CallJavascriptFunction("setPrinters", printers); | 221 web_ui_->CallJavascriptFunction("setPrinters", printers); |
| 211 } | 222 } |
| 212 | 223 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 223 void PrintPreviewHandler::ProcessLandscapeSetting( | 234 void PrintPreviewHandler::ProcessLandscapeSetting( |
| 224 const DictionaryValue& settings) { | 235 const DictionaryValue& settings) { |
| 225 bool landscape = kLandscapeDefaultValue; | 236 bool landscape = kLandscapeDefaultValue; |
| 226 settings.GetBoolean(printing::kSettingLandscape, &landscape); | 237 settings.GetBoolean(printing::kSettingLandscape, &landscape); |
| 227 if (landscape != landscape_) { | 238 if (landscape != landscape_) { |
| 228 landscape_ = landscape; | 239 landscape_ = landscape; |
| 229 need_to_generate_preview_ = true; | 240 need_to_generate_preview_ = true; |
| 230 } | 241 } |
| 231 } | 242 } |
| 232 | 243 |
| 233 void PrintPreviewHandler::SelectFile() { | 244 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { |
| 234 SelectFileDialog::FileTypeInfo file_type_info; | 245 SelectFileDialog::FileTypeInfo file_type_info; |
| 235 file_type_info.extensions.resize(1); | 246 file_type_info.extensions.resize(1); |
| 236 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 247 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 237 | 248 |
| 238 if (!select_file_dialog_.get()) | 249 if (!select_file_dialog_.get()) |
| 239 select_file_dialog_ = SelectFileDialog::Create(this); | 250 select_file_dialog_ = SelectFileDialog::Create(this); |
| 240 | 251 |
| 241 select_file_dialog_->SelectFile( | 252 select_file_dialog_->SelectFile( |
| 242 SelectFileDialog::SELECT_SAVEAS_FILE, | 253 SelectFileDialog::SELECT_SAVEAS_FILE, |
| 243 string16(), | 254 string16(), |
| 244 FilePath(), | 255 default_filename, |
| 245 &file_type_info, | 256 &file_type_info, |
| 246 0, | 257 0, |
| 247 FILE_PATH_LITERAL(""), | 258 FILE_PATH_LITERAL(""), |
| 248 platform_util::GetTopLevel( | 259 platform_util::GetTopLevel( |
| 249 web_ui_->tab_contents()->GetNativeView()), | 260 web_ui_->tab_contents()->GetNativeView()), |
| 250 NULL); | 261 NULL); |
| 251 } | 262 } |
| 252 | 263 |
| 253 void PrintPreviewHandler::FileSelected(const FilePath& path, | 264 void PrintPreviewHandler::FileSelected(const FilePath& path, |
| 254 int index, void* params) { | 265 int index, void* params) { |
| 255 #if defined(OS_POSIX) | 266 #if defined(OS_POSIX) |
| 256 PrintPreviewUIHTMLSource::PrintPreviewData data; | 267 PrintPreviewUIHTMLSource::PrintPreviewData data; |
| 257 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); | 268 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); |
| 258 pp_ui->html_source()->GetPrintPreviewData(&data); | 269 pp_ui->html_source()->GetPrintPreviewData(&data); |
| 259 DCHECK(data.first != NULL); | 270 DCHECK(data.first != NULL); |
| 260 DCHECK(data.second > 0); | 271 DCHECK(data.second > 0); |
| 261 | 272 |
| 262 printing::NativeMetafile* metafile = | 273 printing::NativeMetafile* metafile = |
| 263 printing::NativeMetafileFactory::CreateFromData(data.first->memory(), | 274 printing::NativeMetafileFactory::CreateFromData(data.first->memory(), |
| 264 data.second); | 275 data.second); |
| 265 metafile->FinishDocument(); | 276 metafile->FinishDocument(); |
| 266 | 277 |
| 267 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | 278 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
| 268 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 279 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 269 #else | 280 #else |
| 270 NOTIMPLEMENTED(); | 281 NOTIMPLEMENTED(); |
| 271 #endif | 282 #endif |
| 272 } | 283 } |
| OLD | NEW |