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" | |
| 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" | |
| 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; | |
| 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) { |
| 158 if (!last_saved_path_) { | |
| 159 last_saved_path_ = new FilePath(); | |
|
Lei Zhang
2011/04/14 21:00:37
I think you're going to leak the new FilePath if y
dpapad
2011/04/14 21:25:32
I did this because that is what the style guide su
Lei Zhang
2011/04/14 21:29:56
Oh right. Have you considered using a LazyInstance
| |
| 160 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 161 PathService::Get(chrome::DIR_USER_DOCUMENTS, last_saved_path_); | |
|
Lei Zhang
2011/04/14 21:00:37
You can do this in later in SelectFile().
dpapad
2011/04/14 21:25:32
Done.
| |
| 162 } | |
| 148 } | 163 } |
| 149 | 164 |
| 150 PrintPreviewHandler::~PrintPreviewHandler() { | 165 PrintPreviewHandler::~PrintPreviewHandler() { |
| 151 if (select_file_dialog_.get()) | 166 if (select_file_dialog_.get()) |
| 152 select_file_dialog_->ListenerDestroyed(); | 167 select_file_dialog_->ListenerDestroyed(); |
| 153 } | 168 } |
| 154 | 169 |
| 155 void PrintPreviewHandler::RegisterMessages() { | 170 void PrintPreviewHandler::RegisterMessages() { |
| 156 web_ui_->RegisterMessageCallback("getPrinters", | 171 web_ui_->RegisterMessageCallback("getPrinters", |
| 157 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters)); | 172 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); | 210 TabContents* initiator_tab = GetInitiatorTab(web_ui_->tab_contents()); |
| 196 if (initiator_tab) { | 211 if (initiator_tab) { |
| 197 RenderViewHost* rvh = initiator_tab->render_view_host(); | 212 RenderViewHost* rvh = initiator_tab->render_view_host(); |
| 198 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); | 213 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); |
| 199 } | 214 } |
| 200 | 215 |
| 201 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 216 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 202 if (!settings.get()) | 217 if (!settings.get()) |
| 203 return; | 218 return; |
| 204 | 219 |
| 205 bool print_to_pdf; | 220 bool print_to_pdf = false; |
| 206 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 221 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 207 | 222 |
| 208 if (print_to_pdf) { | 223 if (print_to_pdf) { |
| 209 SelectFile(); | 224 // Pre-populating select file dialog with print job title. |
| 225 TabContentsWrapper* wrapper = | |
| 226 TabContentsWrapper::GetCurrentWrapperForContents( | |
| 227 web_ui_->tab_contents()); | |
| 228 | |
| 229 string16 print_job_title_temp = wrapper->print_view_manager() | |
| 230 ->RenderSourceName(); | |
|
Lei Zhang
2011/04/14 21:00:37
nit: this looks kind of weird style-wise.
dpapad
2011/04/14 21:25:32
Done.
| |
| 231 | |
| 232 if (print_job_title_temp.length() == 0) | |
|
Lei Zhang
2011/04/14 21:00:37
You don't need this. PrintViewManager::RenderSourc
dpapad
2011/04/14 21:25:32
Done.
| |
| 233 print_job_title_temp = l10n_util::GetStringUTF16( | |
| 234 IDS_DEFAULT_PRINT_DOCUMENT_TITLE); | |
| 235 #if defined(OS_WIN) | |
| 236 FilePath::StringType print_job_title(print_job_title_temp); | |
| 237 #else | |
|
dpapad
2011/04/14 02:33:35
Should this be #elif defined(OS_POSIX)?
Lei Zhang
2011/04/14 21:00:37
Sure.
dpapad
2011/04/14 21:25:32
Done.
| |
| 238 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_temp); | |
| 239 #endif | |
| 240 | |
| 241 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | |
| 242 FilePath default_filename(print_job_title); | |
| 243 default_filename = | |
| 244 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | |
| 245 | |
| 246 SelectFile(default_filename); | |
| 210 } else { | 247 } else { |
| 211 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); | 248 RenderViewHost* rvh = web_ui_->GetRenderViewHost(); |
| 212 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 249 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 213 } | 250 } |
| 214 } | 251 } |
| 215 | 252 |
| 216 void PrintPreviewHandler::SendPrinterList( | 253 void PrintPreviewHandler::SendPrinterList( |
| 217 const ListValue& printers, | 254 const ListValue& printers, |
| 218 const FundamentalValue& default_printer_index) { | 255 const FundamentalValue& default_printer_index) { |
| 219 web_ui_->CallJavascriptFunction("setPrinters", printers, | 256 web_ui_->CallJavascriptFunction("setPrinters", printers, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 233 void PrintPreviewHandler::ProcessLandscapeSetting( | 270 void PrintPreviewHandler::ProcessLandscapeSetting( |
| 234 const DictionaryValue& settings) { | 271 const DictionaryValue& settings) { |
| 235 bool landscape = kLandscapeDefaultValue; | 272 bool landscape = kLandscapeDefaultValue; |
| 236 settings.GetBoolean(printing::kSettingLandscape, &landscape); | 273 settings.GetBoolean(printing::kSettingLandscape, &landscape); |
| 237 if (landscape != landscape_) { | 274 if (landscape != landscape_) { |
| 238 landscape_ = landscape; | 275 landscape_ = landscape; |
| 239 need_to_generate_preview_ = true; | 276 need_to_generate_preview_ = true; |
| 240 } | 277 } |
| 241 } | 278 } |
| 242 | 279 |
| 243 void PrintPreviewHandler::SelectFile() { | 280 void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { |
| 244 SelectFileDialog::FileTypeInfo file_type_info; | 281 SelectFileDialog::FileTypeInfo file_type_info; |
| 245 file_type_info.extensions.resize(1); | 282 file_type_info.extensions.resize(1); |
| 246 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); | 283 file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("pdf")); |
| 247 | 284 |
| 248 if (!select_file_dialog_.get()) | 285 if (!select_file_dialog_.get()) |
| 249 select_file_dialog_ = SelectFileDialog::Create(this); | 286 select_file_dialog_ = SelectFileDialog::Create(this); |
| 250 | 287 |
| 251 select_file_dialog_->SelectFile( | 288 select_file_dialog_->SelectFile( |
| 252 SelectFileDialog::SELECT_SAVEAS_FILE, | 289 SelectFileDialog::SELECT_SAVEAS_FILE, |
| 253 string16(), | 290 string16(), |
| 254 FilePath(), | 291 last_saved_path_->Append(default_filename), |
| 255 &file_type_info, | 292 &file_type_info, |
| 256 0, | 293 0, |
| 257 FILE_PATH_LITERAL(""), | 294 FILE_PATH_LITERAL(""), |
| 258 platform_util::GetTopLevel( | 295 platform_util::GetTopLevel( |
| 259 web_ui_->tab_contents()->GetNativeView()), | 296 web_ui_->tab_contents()->GetNativeView()), |
| 260 NULL); | 297 NULL); |
| 261 } | 298 } |
| 262 | 299 |
| 263 void PrintPreviewHandler::FileSelected(const FilePath& path, | 300 void PrintPreviewHandler::FileSelected(const FilePath& path, |
| 264 int index, void* params) { | 301 int index, void* params) { |
| 265 PrintPreviewUIHTMLSource::PrintPreviewData data; | 302 PrintPreviewUIHTMLSource::PrintPreviewData data; |
| 266 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); | 303 PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_); |
| 267 pp_ui->html_source()->GetPrintPreviewData(&data); | 304 pp_ui->html_source()->GetPrintPreviewData(&data); |
| 268 DCHECK(data.first != NULL); | 305 DCHECK(data.first != NULL); |
| 269 DCHECK(data.second > 0); | 306 DCHECK(data.second > 0); |
| 270 | 307 |
| 271 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; | 308 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
| 272 metafile->InitFromData(data.first->memory(), data.second); | 309 metafile->InitFromData(data.first->memory(), data.second); |
| 273 | 310 |
| 311 // Updating last_saved_path_ to the newly selected folder. | |
| 312 *last_saved_path_ = path.DirName(); | |
| 313 | |
| 274 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | 314 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
| 275 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 315 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 276 } | 316 } |
| OLD | NEW |