| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/dom_ui/print_preview_ui_html_source.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/message_loop.h" |
| 11 #include "base/shared_memory.h" |
| 12 #include "base/string_piece.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/common/jstemplate_builder.h" |
| 15 #include "chrome/common/url_constants.h" |
| 16 #include "grit/browser_resources.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 void SetLocalizedStrings(DictionaryValue* localized_strings) { |
| 24 localized_strings->SetString(std::string("title"), |
| 25 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE)); |
| 26 localized_strings->SetString(std::string("loading"), |
| 27 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOADING)); |
| 28 localized_strings->SetString(std::string("noPlugin"), |
| 29 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN)); |
| 30 localized_strings->SetString(std::string("noPrinter"), |
| 31 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PRINTER)); |
| 32 |
| 33 localized_strings->SetString(std::string("printButton"), |
| 34 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON)); |
| 35 localized_strings->SetString(std::string("cancelButton"), |
| 36 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON)); |
| 37 |
| 38 localized_strings->SetString(std::string("optionAllPages"), |
| 39 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES)); |
| 40 localized_strings->SetString(std::string("optionBw"), |
| 41 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW)); |
| 42 localized_strings->SetString(std::string("optionCollate"), |
| 43 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE)); |
| 44 localized_strings->SetString(std::string("optionColor"), |
| 45 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR)); |
| 46 localized_strings->SetString(std::string("optionLandscape"), |
| 47 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE)); |
| 48 localized_strings->SetString(std::string("optionPortrait"), |
| 49 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT)); |
| 50 localized_strings->SetString(std::string("optionTwoSided"), |
| 51 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED)); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() |
| 57 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), |
| 58 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { |
| 59 } |
| 60 |
| 61 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { |
| 62 delete data_.first; |
| 63 } |
| 64 |
| 65 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { |
| 66 *data = data_; |
| 67 } |
| 68 |
| 69 void PrintPreviewUIHTMLSource::SetPrintPreviewData( |
| 70 const PrintPreviewData& data) { |
| 71 delete data_.first; |
| 72 data_ = data; |
| 73 } |
| 74 |
| 75 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
| 76 bool is_off_the_record, |
| 77 int request_id) { |
| 78 if (path.empty()) { |
| 79 // Print Preview Index page. |
| 80 DictionaryValue localized_strings; |
| 81 SetLocalizedStrings(&localized_strings); |
| 82 SetFontAndTextDirection(&localized_strings); |
| 83 |
| 84 static const base::StringPiece print_html( |
| 85 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 86 IDR_PRINT_PREVIEW_HTML)); |
| 87 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 88 print_html, &localized_strings); |
| 89 |
| 90 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 91 html_bytes->data.resize(full_html.size()); |
| 92 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 93 |
| 94 SendResponse(request_id, html_bytes); |
| 95 return; |
| 96 } else if (path == "print.pdf") { |
| 97 // Print Preview data. |
| 98 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); |
| 99 uint32 preview_data_size = data_.second; |
| 100 |
| 101 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 102 html_bytes->data.resize(preview_data_size); |
| 103 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); |
| 104 for (uint32 i = 0; i < preview_data_size; ++i, ++it) |
| 105 *it = *(preview_data + i); |
| 106 SendResponse(request_id, html_bytes); |
| 107 return; |
| 108 } else { |
| 109 // Invalid request. |
| 110 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| 111 SendResponse(request_id, empty_bytes); |
| 112 return; |
| 113 } |
| 114 } |
| 115 |
| 116 std::string PrintPreviewUIHTMLSource::GetMimeType( |
| 117 const std::string& path) const { |
| 118 // Print Preview Index page. |
| 119 if (path.empty()) |
| 120 return "text/html"; |
| 121 // Print Preview data. |
| 122 return "application/pdf"; |
| 123 } |
| OLD | NEW |