summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/renderer/mock_render_thread.h2
-rw-r--r--chrome/renderer/print_web_view_helper.cc36
-rw-r--r--chrome/renderer/print_web_view_helper.h8
-rw-r--r--chrome/renderer/render_view.h4
-rw-r--r--chrome/renderer/render_view_unittest.cc44
5 files changed, 93 insertions, 1 deletions
diff --git a/chrome/renderer/mock_render_thread.h b/chrome/renderer/mock_render_thread.h
index 057f0cf..69a5186 100644
--- a/chrome/renderer/mock_render_thread.h
+++ b/chrome/renderer/mock_render_thread.h
@@ -70,7 +70,7 @@ class MockRenderThread : public RenderThreadBase {
void SendCloseMessage();
// Returns the pseudo-printer instance.
- const MockPrinter* printer() const { return printer_.get(); }
+ MockPrinter* printer() const { return printer_.get(); }
private:
// This function operates as a regular IPC listener.
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc
index aa7f78f7..04e72b4 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -5,6 +5,7 @@
#include "chrome/renderer/print_web_view_helper.h"
#include "app/l10n_util.h"
+#include "base/gfx/jpeg_codec.h"
#include "base/logging.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/render_view.h"
@@ -139,6 +140,41 @@ void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params,
}
}
+void PrintWebViewHelper::PrintPageAsJPEG(
+ const ViewMsg_PrintPage_Params& params,
+ WebFrame* frame,
+ float zoom_factor,
+ std::vector<unsigned char>* image_data) {
+ PrepareFrameAndViewForPrint prep_frame_view(params.params,
+ frame,
+ frame->view());
+ const gfx::Size& canvas_size(prep_frame_view.GetPrintCanvasSize());
+
+ // Since WebKit extends the page width depending on the magical shrink
+ // factor we make sure the canvas covers the worst case scenario
+ // (x2.0 currently). PrintContext will then set the correct clipping region.
+ int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink);
+ int size_y = static_cast<int>(canvas_size.height() *
+ params.params.max_shrink);
+
+ // Access the bitmap from the canvas device.
+ skia::PlatformCanvas canvas(size_x, size_y, true);
+ frame->printPage(params.page_number, &canvas);
+ const SkBitmap& bitmap = canvas.getDevice()->accessBitmap(false);
+
+ // Encode the SkBitmap to jpeg.
+ SkAutoLockPixels image_lock(bitmap);
+ bool encoded = JPEGCodec::Encode(
+ reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
+ JPEGCodec::FORMAT_BGRA,
+ static_cast<int>(bitmap.width() * zoom_factor),
+ static_cast<int>(bitmap.height() * zoom_factor),
+ static_cast<int>(bitmap.rowBytes()),
+ 90,
+ image_data);
+ DCHECK(encoded);
+}
+
bool PrintWebViewHelper::Send(IPC::Message* msg) {
return render_view_->Send(msg);
}
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index 0c2f0ff..406c807 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -75,6 +75,14 @@ class PrintWebViewHelper : public WebViewDelegate {
// Notification when printing is done - signal teardown
void DidFinishPrinting(bool success);
+ // Prints the page listed in |params| as a JPEG image. The width and height of
+ // the image will scale propotionally given the |zoom_factor| multiplier. The
+ // encoded JPEG data will be written into the supplied vector |image_data|.
+ void PrintPageAsJPEG(const ViewMsg_PrintPage_Params& params,
+ WebKit::WebFrame* frame,
+ float zoom_factor,
+ std::vector<unsigned char>* image_data);
+
protected:
bool CopyAndPrint(const ViewMsg_PrintPages_Params& params,
WebKit::WebFrame* web_frame);
diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h
index 7318da7..c076bc6 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -151,6 +151,10 @@ class RenderView : public RenderWidget,
return view_type_;
}
+ PrintWebViewHelper* print_helper() {
+ return print_helper_.get();
+ }
+
// IPC::Channel::Listener
virtual void OnMessageReceived(const IPC::Message& msg);
diff --git a/chrome/renderer/render_view_unittest.cc b/chrome/renderer/render_view_unittest.cc
index 96bcc8f..6f3d51b 100644
--- a/chrome/renderer/render_view_unittest.cc
+++ b/chrome/renderer/render_view_unittest.cc
@@ -3,9 +3,11 @@
// found in the LICENSE file.
#include "base/file_util.h"
+#include "base/gfx/jpeg_codec.h"
#include "base/shared_memory.h"
#include "chrome/common/native_web_keyboard_event.h"
#include "chrome/common/render_messages.h"
+#include "chrome/renderer/print_web_view_helper.h"
#include "chrome/test/render_view_test.h"
#include "net/base/net_errors.h"
#include "printing/image.h"
@@ -867,3 +869,45 @@ TEST_F(RenderViewTest, DidFailProvisionalLoadWithErrorForCancellation) {
// Frame should stay in view-source mode.
EXPECT_TRUE(web_frame->isViewSourceModeEnabled());
}
+
+// Print page as bitmap test.
+TEST_F(RenderViewTest, OnPrintPageAsBitmap) {
+#if defined(OS_WIN)
+ // Lets simulate a print pages with Hello world.
+ LoadHTML("<body><p>Hello world!</p></body>");
+
+ // Grab the printer settings from the printer.
+ ViewMsg_Print_Params print_settings;
+ MockPrinter* printer(render_thread_.printer());
+ printer->GetDefaultPrintSettings(&print_settings);
+ ViewMsg_PrintPage_Params page_params = ViewMsg_PrintPage_Params();
+ page_params.params = print_settings;
+ page_params.page_number = 0;
+
+ // Fetch the image data from the web frame.
+ std::vector<unsigned char> data;
+ view_->print_helper()->PrintPageAsJPEG(page_params,
+ view_->webview()->GetMainFrame(),
+ 1.0f,
+ &data);
+ std::vector<unsigned char> decoded;
+ int w, h;
+ EXPECT_TRUE(JPEGCodec::Decode(&data[0], data.size(), JPEGCodec::FORMAT_RGBA,
+ &decoded, &w, &h));
+
+ // Check if its not 100% white.
+ bool is_white = true;
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ unsigned char* px = &decoded[(y * w + x) * 4];
+ if (px[0] != 0xFF && px[1] != 0xFF && px[2] != 0xFF) {
+ is_white = false;
+ break;
+ }
+ }
+ }
+ ASSERT_TRUE(!is_white);
+#else
+ NOTIMPLEMENTED();
+#endif
+}