Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: chrome/browser/printing/print_preview_pdf_generated_browsertest.cc

Issue 799643004: Combine PDF plugin into the Chromium binary. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <algorithm> 5 #include <algorithm>
6 #include <fstream> 6 #include <fstream>
7 #include <iostream> 7 #include <iostream>
8 #include <iterator> 8 #include <iterator>
9 #include <limits> 9 #include <limits>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/callback.h" 15 #include "base/callback.h"
16 #include "base/files/file.h" 16 #include "base/files/file.h"
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h" 18 #include "base/files/file_util.h"
19 #include "base/files/scoped_temp_dir.h" 19 #include "base/files/scoped_temp_dir.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/md5.h" 21 #include "base/md5.h"
22 #include "base/memory/scoped_ptr.h" 22 #include "base/memory/scoped_ptr.h"
23 #include "base/path_service.h" 23 #include "base/path_service.h"
24 #include "base/run_loop.h" 24 #include "base/run_loop.h"
25 #include "base/scoped_native_library.h"
26 #include "base/strings/string_split.h" 25 #include "base/strings/string_split.h"
27 #include "base/strings/utf_string_conversions.h" 26 #include "base/strings/utf_string_conversions.h"
28 #include "chrome/browser/printing/print_preview_dialog_controller.h" 27 #include "chrome/browser/printing/print_preview_dialog_controller.h"
29 #include "chrome/browser/ui/browser.h" 28 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_commands.h" 29 #include "chrome/browser/ui/browser_commands.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" 30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" 31 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
33 #include "chrome/common/chrome_paths.h" 32 #include "chrome/common/chrome_paths.h"
34 #include "chrome/test/base/in_process_browser_test.h" 33 #include "chrome/test/base/in_process_browser_test.h"
35 #include "chrome/test/base/ui_test_utils.h" 34 #include "chrome/test/base/ui_test_utils.h"
36 #include "components/printing/common/print_messages.h" 35 #include "components/printing/common/print_messages.h"
37 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
38 #include "content/public/browser/web_ui_message_handler.h" 37 #include "content/public/browser/web_ui_message_handler.h"
39 #include "content/public/test/browser_test_utils.h" 38 #include "content/public/test/browser_test_utils.h"
40 #include "ipc/ipc_message_macros.h" 39 #include "ipc/ipc_message_macros.h"
41 #include "net/base/filename_util.h" 40 #include "net/base/filename_util.h"
41 #include "pdf/pdf.h"
42 #include "printing/pdf_render_settings.h" 42 #include "printing/pdf_render_settings.h"
43 #include "printing/units.h" 43 #include "printing/units.h"
44 #include "ui/gfx/codec/png_codec.h" 44 #include "ui/gfx/codec/png_codec.h"
45 #include "ui/gfx/geometry/rect.h" 45 #include "ui/gfx/geometry/rect.h"
46 #include "url/gurl.h" 46 #include "url/gurl.h"
47 47
48 #if defined(OS_WIN) 48 #if defined(OS_WIN)
49 #include <fcntl.h> 49 #include <fcntl.h>
50 #include <io.h> 50 #include <io.h>
51 #endif 51 #endif
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 chrome::Print(browser()); 316 chrome::Print(browser());
317 loop.Run(); 317 loop.Run();
318 318
319 // Need to check whether the save was successful. Ending the loop only 319 // Need to check whether the save was successful. Ending the loop only
320 // means the save was attempted. 320 // means the save was attempted.
321 base::File pdf_file( 321 base::File pdf_file(
322 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); 322 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ);
323 ASSERT_TRUE(pdf_file.IsValid()); 323 ASSERT_TRUE(pdf_file.IsValid());
324 } 324 }
325 325
326 // Initializes function pointers from the PDF library. Called once when the
327 // test starts. The library is closed when the browser test ends.
328 void InitPdfFunctions() {
329 base::FilePath pdf_module_path;
330
331 ASSERT_TRUE(PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_module_path));
332 ASSERT_TRUE(base::PathExists(pdf_module_path));
333 pdf_lib_.Reset(base::LoadNativeLibrary(pdf_module_path, NULL));
334
335 ASSERT_TRUE(pdf_lib_.is_valid());
336 pdf_to_bitmap_func_ =
337 reinterpret_cast<PDFPageToBitmapProc>(
338 pdf_lib_.GetFunctionPointer("RenderPDFPageToBitmap"));
339
340 pdf_doc_info_func_ =
341 reinterpret_cast<GetPDFDocInfoProc>(
342 pdf_lib_.GetFunctionPointer("GetPDFDocInfo"));
343
344 pdf_page_size_func_ =
345 reinterpret_cast<GetPDFPageSizeByIndexProc>(
346 pdf_lib_.GetFunctionPointer("GetPDFPageSizeByIndex"));
347
348 ASSERT_TRUE(pdf_to_bitmap_func_);
349 ASSERT_TRUE(pdf_doc_info_func_);
350 ASSERT_TRUE(pdf_page_size_func_);
351 }
352
353 // Converts the PDF to a PNG file so that the layout test can do an image 326 // Converts the PDF to a PNG file so that the layout test can do an image
354 // diff on this image and a reference image. 327 // diff on this image and a reference image.
355 void PdfToPng() { 328 void PdfToPng() {
356 int num_pages; 329 int num_pages;
357 double max_width_in_points = 0; 330 double max_width_in_points = 0;
358 std::vector<uint8_t> bitmap_data; 331 std::vector<uint8_t> bitmap_data;
359 double total_height_in_pixels = 0; 332 double total_height_in_pixels = 0;
360 std::string pdf_data; 333 std::string pdf_data;
361 334
362 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); 335 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data));
363 ASSERT_TRUE(pdf_doc_info_func_(pdf_data.data(), 336 ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(),
364 pdf_data.size(), 337 pdf_data.size(),
365 &num_pages, 338 &num_pages,
366 &max_width_in_points)); 339 &max_width_in_points));
367 340
368 ASSERT_GT(num_pages, 0); 341 ASSERT_GT(num_pages, 0);
369 double max_width_in_pixels = 342 double max_width_in_pixels =
370 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); 343 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi);
371 344
372 for (int i = 0; i < num_pages; ++i) { 345 for (int i = 0; i < num_pages; ++i) {
373 double width_in_points, height_in_points; 346 double width_in_points, height_in_points;
374 ASSERT_TRUE(pdf_page_size_func_(pdf_data.data(), 347 ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(pdf_data.data(),
375 pdf_data.size(), 348 pdf_data.size(),
376 i, 349 i,
377 &width_in_points, 350 &width_in_points,
378 &height_in_points)); 351 &height_in_points));
379 352
380 double width_in_pixels = ConvertUnitDouble( 353 double width_in_pixels = ConvertUnitDouble(
381 width_in_points, kPointsPerInch, kDpi); 354 width_in_points, kPointsPerInch, kDpi);
382 double height_in_pixels = ConvertUnitDouble( 355 double height_in_pixels = ConvertUnitDouble(
383 height_in_points, kPointsPerInch, kDpi); 356 height_in_points, kPointsPerInch, kDpi);
384 357
385 // The image will be rotated if |width_in_pixels| is greater than 358 // The image will be rotated if |width_in_pixels| is greater than
386 // |height_in_pixels|. This is because the page will be rotated to fit 359 // |height_in_pixels|. This is because the page will be rotated to fit
387 // within a piece of paper. Therefore, |width_in_pixels| and 360 // within a piece of paper. Therefore, |width_in_pixels| and
388 // |height_in_pixels| have to be swapped or else they won't reflect the 361 // |height_in_pixels| have to be swapped or else they won't reflect the
389 // dimensions of the rotated page. 362 // dimensions of the rotated page.
390 if (width_in_pixels > height_in_pixels) 363 if (width_in_pixels > height_in_pixels)
391 std::swap(width_in_pixels, height_in_pixels); 364 std::swap(width_in_pixels, height_in_pixels);
392 365
393 total_height_in_pixels += height_in_pixels; 366 total_height_in_pixels += height_in_pixels;
394 gfx::Rect rect(width_in_pixels, height_in_pixels); 367 gfx::Rect rect(width_in_pixels, height_in_pixels);
395 PdfRenderSettings settings(rect, kDpi, true); 368 PdfRenderSettings settings(rect, kDpi, true);
396 369
397 int int_max = std::numeric_limits<int>::max(); 370 int int_max = std::numeric_limits<int>::max();
398 if (settings.area().width() > int_max / kColorChannels || 371 if (settings.area().width() > int_max / kColorChannels ||
399 settings.area().height() > int_max / (kColorChannels * 372 settings.area().height() > int_max / (kColorChannels *
400 settings.area().width())) { 373 settings.area().width())) {
401 FAIL() << "The dimensions of the image are too large." 374 FAIL() << "The dimensions of the image are too large."
402 << "Decrease the DPI or the dimensions of the image."; 375 << "Decrease the DPI or the dimensions of the image.";
403 } 376 }
404 377
405 std::vector<uint8_t> page_bitmap_data( 378 std::vector<uint8_t> page_bitmap_data(
406 kColorChannels * settings.area().size().GetArea()); 379 kColorChannels * settings.area().size().GetArea());
407 380
408 ASSERT_TRUE(pdf_to_bitmap_func_(pdf_data.data(), 381 ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
409 pdf_data.size(), 382 pdf_data.data(),
410 i, 383 pdf_data.size(),
411 page_bitmap_data.data(), 384 i,
412 settings.area().size().width(), 385 page_bitmap_data.data(),
413 settings.area().size().height(), 386 settings.area().size().width(),
414 settings.dpi(), 387 settings.area().size().height(),
415 settings.dpi(), 388 settings.dpi(),
416 true)); 389 true));
417 FillPng(&page_bitmap_data, 390 FillPng(&page_bitmap_data,
418 width_in_pixels, 391 width_in_pixels,
419 max_width_in_pixels, 392 max_width_in_pixels,
420 settings.area().size().height()); 393 settings.area().size().height());
421 bitmap_data.insert(bitmap_data.end(), 394 bitmap_data.insert(bitmap_data.end(),
422 page_bitmap_data.begin(), 395 page_bitmap_data.begin(),
423 page_bitmap_data.end()); 396 page_bitmap_data.end());
424 } 397 }
425 398
426 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); 399 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 png_rect.size(), 538 png_rect.size(),
566 png_rect.size().width() * kColorChannels, 539 png_rect.size().width() * kColorChannels,
567 false, 540 false,
568 comments, 541 comments,
569 &png_output_)); 542 &png_output_));
570 } 543 }
571 544
572 scoped_ptr<PrintPreviewObserver> print_preview_observer_; 545 scoped_ptr<PrintPreviewObserver> print_preview_observer_;
573 base::FilePath pdf_file_save_path_; 546 base::FilePath pdf_file_save_path_;
574 547
575 // These typedefs are function pointers to pdflib functions that give
576 // information about the PDF as a whole and about specific pages.
577
578 // Converts the PDF to a bitmap.
579 typedef bool (*PDFPageToBitmapProc)(const void* pdf_buffer,
580 int pdf_buffer_size,
581 int page_number,
582 void* bitmap_buffer,
583 int bitmap_width,
584 int bitmap_height,
585 int dpi_x,
586 int dpi_y,
587 bool autorotate);
588
589 // Gets the page count and maximum page width of the PDF in points.
590 typedef bool (*GetPDFDocInfoProc)(const void* pdf_buffer,
591 int buffer_size,
592 int* pages_count,
593 double* max_page_width);
594
595 // Gets the dimensions of a specific page within a PDF.
596 typedef bool (*GetPDFPageSizeByIndexProc)(const void* pdf_buffer,
597 int buffer_size,
598 int index,
599 double* width,
600 double* height);
601
602 // Instantiations of the function pointers described above.
603 PDFPageToBitmapProc pdf_to_bitmap_func_;
604 GetPDFDocInfoProc pdf_doc_info_func_;
605 GetPDFPageSizeByIndexProc pdf_page_size_func_;
606
607 // Used to open up the pdf plugin, which contains the functions above.
608 base::ScopedNativeLibrary pdf_lib_;
609
610 // Vector for storing the PNG to be sent to the layout test framework. 548 // Vector for storing the PNG to be sent to the layout test framework.
611 // TODO(ivandavid): Eventually change this to uint32_t and make everything 549 // TODO(ivandavid): Eventually change this to uint32_t and make everything
612 // work with that. It might be a bit tricky to fix everything to work with 550 // work with that. It might be a bit tricky to fix everything to work with
613 // uint32_t, but not too tricky. 551 // uint32_t, but not too tricky.
614 std::vector<unsigned char> png_output_; 552 std::vector<unsigned char> png_output_;
615 553
616 // Image hash of the bitmap that is turned into a PNG. The hash is put into 554 // Image hash of the bitmap that is turned into a PNG. The hash is put into
617 // the PNG as a comment, as it is needed by the layout test framework. 555 // the PNG as a comment, as it is needed by the layout test framework.
618 base::MD5Digest hash_; 556 base::MD5Digest hash_;
619 557
(...skipping 14 matching lines...) Expand all
634 // - Print webpage to a pdf 572 // - Print webpage to a pdf
635 // - Convert pdf to a png 573 // - Convert pdf to a png
636 // - Send png to layout test framework, where it doesn an image diff 574 // - Send png to layout test framework, where it doesn an image diff
637 // on the image sent by this test and a reference image. 575 // on the image sent by this test and a reference image.
638 // 576 //
639 // Throughout this code, there will be |std::cout| statements. The layout test 577 // Throughout this code, there will be |std::cout| statements. The layout test
640 // framework uses stdout to get data from the browser test and uses stdin 578 // framework uses stdout to get data from the browser test and uses stdin
641 // to send data to the browser test. Writing "EOF\n" to |std::cout| indicates 579 // to send data to the browser test. Writing "EOF\n" to |std::cout| indicates
642 // that whatever block of data that the test was expecting has been completely 580 // that whatever block of data that the test was expecting has been completely
643 // sent. Sometimes EOF is printed to stderr because the test will expect it 581 // sent. Sometimes EOF is printed to stderr because the test will expect it
644 // from stderr in addition to stdout for certain blocks of data. 582 // from stderr in addition to stdout for certain blocks of data.=
645 InitPdfFunctions();
646 SetupStdinAndSavePath(); 583 SetupStdinAndSavePath();
647 584
648 while (true) { 585 while (true) {
649 std::string input; 586 std::string input;
650 while (input.empty()) { 587 while (input.empty()) {
651 std::getline(std::cin, input); 588 std::getline(std::cin, input);
652 if (std::cin.eof()) 589 if (std::cin.eof())
653 std::cin.clear(); 590 std::cin.clear();
654 } 591 }
655 592
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 // waiting for this message and start waiting for the image data. 630 // waiting for this message and start waiting for the image data.
694 std::cout << "#EOF\n"; 631 std::cout << "#EOF\n";
695 std::cout.flush(); 632 std::cout.flush();
696 633
697 SendPng(); 634 SendPng();
698 Reset(); 635 Reset();
699 } 636 }
700 } 637 }
701 638
702 } // namespace printing 639 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698