| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/renderer/mock_printer.h" | 5 #include "chrome/renderer/mock_printer.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/gfx/png_encoder.h" | |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/md5.h" | |
| 11 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
| 12 #include "chrome/common/ipc_message_utils.h" | 10 #include "chrome/common/ipc_message_utils.h" |
| 13 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 13 |
| 16 MockPrinter::MockPrinter() | 14 MockPrinter::MockPrinter() |
| 17 : printable_width_(0), | 15 : printable_width_(0), |
| 18 printable_height_(0), | 16 printable_height_(0), |
| 19 dpi_(72), | 17 dpi_(72), |
| 20 max_shrink_(2.0), | 18 max_shrink_(2.0), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 107 |
| 110 #if defined(OS_WIN) | 108 #if defined(OS_WIN) |
| 111 // Load the EMF data sent from a RenderView object and create a PageData | 109 // Load the EMF data sent from a RenderView object and create a PageData |
| 112 // object. | 110 // object. |
| 113 // We duplicate the given file handle when creating a base::SharedMemory | 111 // We duplicate the given file handle when creating a base::SharedMemory |
| 114 // instance so that its destructor closes the copy. | 112 // instance so that its destructor closes the copy. |
| 115 EXPECT_GT(params.data_size, 0U); | 113 EXPECT_GT(params.data_size, 0U); |
| 116 base::SharedMemory emf_data(params.metafile_data_handle, true, | 114 base::SharedMemory emf_data(params.metafile_data_handle, true, |
| 117 GetCurrentProcess()); | 115 GetCurrentProcess()); |
| 118 emf_data.Map(params.data_size); | 116 emf_data.Map(params.data_size); |
| 119 MockPrinterPage* page_data = driver_.LoadSource(emf_data.memory(), | 117 printing::NativeMetafile metafile; |
| 120 params.data_size); | 118 metafile.CreateFromData(emf_data.memory(), params.data_size); |
| 119 printing::Image image(metafile); |
| 120 MockPrinterPage* page_data = new MockPrinterPage(emf_data.memory(), |
| 121 params.data_size, |
| 122 image); |
| 121 if (!page_data) { | 123 if (!page_data) { |
| 122 printer_status_ = PRINTER_ERROR; | 124 printer_status_ = PRINTER_ERROR; |
| 123 return; | 125 return; |
| 124 } | 126 } |
| 125 | 127 |
| 126 scoped_refptr<MockPrinterPage> page(page_data); | 128 scoped_refptr<MockPrinterPage> page(page_data); |
| 127 pages_.push_back(page); | 129 pages_.push_back(page); |
| 128 #endif | 130 #endif |
| 129 | 131 |
| 130 // We finish printing a printing job. | 132 // We finish printing a printing job. |
| 131 // Reset the job status and the printer status. | 133 // Reset the job status and the printer status. |
| 132 ++page_number_; | 134 ++page_number_; |
| 133 if (number_pages_ == page_number_) | 135 if (number_pages_ == page_number_) |
| 134 ResetPrinter(); | 136 ResetPrinter(); |
| 135 } | 137 } |
| 136 | 138 |
| 137 int MockPrinter::GetPrintedPages() const { | 139 int MockPrinter::GetPrintedPages() const { |
| 138 if (printer_status_ != PRINTER_READY) | 140 if (printer_status_ != PRINTER_READY) |
| 139 return -1; | 141 return -1; |
| 140 return page_number_; | 142 return page_number_; |
| 141 } | 143 } |
| 142 | 144 |
| 145 const MockPrinterPage* MockPrinter::GetPrintedPage(size_t pageno) const { |
| 146 if (pages_.size() > pageno) |
| 147 return pages_[pageno]; |
| 148 else |
| 149 return NULL; |
| 150 } |
| 151 |
| 143 int MockPrinter::GetWidth(size_t page) const { | 152 int MockPrinter::GetWidth(size_t page) const { |
| 144 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | 153 if (printer_status_ != PRINTER_READY || page >= pages_.size()) |
| 145 return -1; | 154 return -1; |
| 146 return pages_[page]->width(); | 155 return pages_[page]->width(); |
| 147 } | 156 } |
| 148 | 157 |
| 149 int MockPrinter::GetHeight(size_t page) const { | 158 int MockPrinter::GetHeight(size_t page) const { |
| 150 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | 159 if (printer_status_ != PRINTER_READY || page >= pages_.size()) |
| 151 return -1; | 160 return -1; |
| 152 return pages_[page]->height(); | 161 return pages_[page]->height(); |
| 153 } | 162 } |
| 154 | 163 |
| 155 bool MockPrinter::GetSourceChecksum(size_t page, std::string* checksum) const { | |
| 156 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | |
| 157 return false; | |
| 158 return GetChecksum(pages_[page]->source_data(), pages_[page]->source_size(), | |
| 159 checksum); | |
| 160 } | |
| 161 | |
| 162 bool MockPrinter::GetBitmapChecksum(size_t page, std::string* checksum) const { | 164 bool MockPrinter::GetBitmapChecksum(size_t page, std::string* checksum) const { |
| 163 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | 165 if (printer_status_ != PRINTER_READY || page >= pages_.size()) |
| 164 return false; | 166 return false; |
| 165 return GetChecksum(pages_[page]->bitmap_data(), pages_[page]->bitmap_size(), | 167 *checksum = pages_[page]->image().checksum(); |
| 166 checksum); | |
| 167 } | |
| 168 | |
| 169 bool MockPrinter::GetBitmap(size_t page, | |
| 170 const void** data, | |
| 171 size_t* size) const { | |
| 172 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | |
| 173 return false; | |
| 174 *data = pages_[page]->bitmap_data(); | |
| 175 *size = pages_[page]->bitmap_size(); | |
| 176 return true; | 168 return true; |
| 177 } | 169 } |
| 178 | 170 |
| 179 bool MockPrinter::SaveSource(size_t page, | 171 bool MockPrinter::SaveSource(size_t page, |
| 180 const std::wstring& filename) const { | 172 const std::wstring& filename) const { |
| 181 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | 173 if (printer_status_ != PRINTER_READY || page >= pages_.size()) |
| 182 return false; | 174 return false; |
| 183 const uint8* source_data = pages_[page]->source_data(); | 175 const uint8* source_data = pages_[page]->source_data(); |
| 184 size_t source_size = pages_[page]->source_size(); | 176 size_t source_size = pages_[page]->source_size(); |
| 185 file_util::WriteFile(filename, reinterpret_cast<const char*>(source_data), | 177 file_util::WriteFile(filename, reinterpret_cast<const char*>(source_data), |
| 186 source_size); | 178 source_size); |
| 187 return true; | 179 return true; |
| 188 } | 180 } |
| 189 | 181 |
| 190 bool MockPrinter::SaveBitmap(size_t page, | 182 bool MockPrinter::SaveBitmap(size_t page, |
| 191 const std::wstring& filename) const { | 183 const std::wstring& filename) const { |
| 192 if (printer_status_ != PRINTER_READY || page >= pages_.size()) | 184 if (printer_status_ != PRINTER_READY || page >= pages_.size()) |
| 193 return false; | 185 return false; |
| 194 int width = pages_[page]->width(); | 186 |
| 195 int height = pages_[page]->height(); | 187 pages_[page]->image().SaveToPng(filename); |
| 196 int row_byte_width = width * 4; | |
| 197 const uint8* bitmap_data = pages_[page]->bitmap_data(); | |
| 198 std::vector<unsigned char> compressed; | |
| 199 PNGEncoder::Encode(bitmap_data, | |
| 200 PNGEncoder::FORMAT_BGRA, width, height, | |
| 201 row_byte_width, true, &compressed); | |
| 202 file_util::WriteFile(filename, | |
| 203 reinterpret_cast<char*>(&*compressed.begin()), | |
| 204 compressed.size()); | |
| 205 return true; | 188 return true; |
| 206 } | 189 } |
| 207 | 190 |
| 208 int MockPrinter::CreateDocumentCookie() { | 191 int MockPrinter::CreateDocumentCookie() { |
| 209 return ++current_document_cookie_; | 192 return ++current_document_cookie_; |
| 210 } | 193 } |
| 211 | 194 |
| 212 bool MockPrinter::GetChecksum(const void* data, | |
| 213 size_t size, | |
| 214 std::string* checksum) const { | |
| 215 MD5Digest digest; | |
| 216 MD5Sum(data, size, &digest); | |
| 217 checksum->assign(HexEncode(&digest, sizeof(digest))); | |
| 218 return true; | |
| 219 } | |
| OLD | NEW |