summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authorsverrir@google.com <sverrir@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-19 15:58:01 +0000
committersverrir@google.com <sverrir@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-19 15:58:01 +0000
commit8227045e458705c27ca116a02fd7b9b9a75237ae (patch)
tree2e694fc454663eedc62b27e99aeef67d476a09ab /chrome/renderer
parentb0fd9c1c6b3ccf0b436d38d181ab26100b3135a6 (diff)
downloadchromium_src-8227045e458705c27ca116a02fd7b9b9a75237ae.zip
chromium_src-8227045e458705c27ca116a02fd7b9b9a75237ae.tar.gz
chromium_src-8227045e458705c27ca116a02fd7b9b9a75237ae.tar.bz2
Add Print Selection support to Chrome. This change is fairly involved since this means that the printing is done async instead of the fully synchronous mode the normal full page printing is.
This means we create an in memory copy of the selected text for printing. This is the next step to move to fully async printing with print frame support. This change also removes the print on demand functionality that was no longer used. BUG=http://crbug.com/1682 TEST=The print dialog on Windows now contains an option to print selection only. Test that with various pages and various selections. Review URL: http://codereview.chromium.org/125082 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18815 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/print_web_view_helper.cc77
-rw-r--r--chrome/renderer/print_web_view_helper.h14
-rw-r--r--chrome/renderer/render_view.cc17
-rw-r--r--chrome/renderer/render_view.h6
4 files changed, 91 insertions, 23 deletions
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc
index 049dbb8..ab61664 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -13,6 +13,8 @@
#include "printing/units.h"
#include "webkit/api/public/WebScreenInfo.h"
#include "webkit/api/public/WebSize.h"
+#include "webkit/api/public/WebURL.h"
+#include "webkit/api/public/WebURLRequest.h"
#include "webkit/glue/webframe.h"
#if defined(OS_WIN)
@@ -86,6 +88,11 @@ class PrepareFrameAndViewForPrint {
void PrintWebViewHelper::SyncPrint(WebFrame* frame) {
#if defined(OS_WIN)
+
+ // If still not finished with earlier print request simply ignore.
+ if (IsPrinting())
+ return;
+
// Retrieve the default print settings to calculate the expected number of
// pages.
ViewMsg_Print_Params default_settings;
@@ -126,24 +133,21 @@ void PrintWebViewHelper::SyncPrint(WebFrame* frame) {
render_view_->host_window(),
default_settings.document_cookie,
expected_pages_count,
- false, // has_selection
+ frame->HasSelection(),
&print_settings);
if (Send(msg)) {
msg = NULL;
- // Printing selection only not supported yet.
- if (print_settings.params.selection_only) {
- NOTIMPLEMENTED();
- }
-
// If the settings are invalid, early quit.
if (print_settings.params.dpi &&
print_settings.params.document_cookie) {
- // Render the printed pages. It will implicitly revert the document to
- // display CSS media type.
- PrintPages(print_settings, frame);
- // All went well.
- return;
+ if (print_settings.params.selection_only) {
+ CopyAndPrint(print_settings, frame);
+ } else {
+ // TODO: Always copy before printing.
+ PrintPages(print_settings, frame);
+ }
+ return; // All went well.
} else {
// The user cancelled.
}
@@ -158,15 +162,52 @@ void PrintWebViewHelper::SyncPrint(WebFrame* frame) {
// Send() failed.
NOTREACHED();
}
- // TODO(maruel): bug 1123882 Alert the user that printing failed.
+ DidFinishPrinting(false);
#else // defined(OS_WIN)
// TODO(port): print not implemented
NOTIMPLEMENTED();
#endif
}
+void PrintWebViewHelper::DidFinishPrinting(bool success) {
+ if (print_web_view_.get()) {
+ print_web_view_->Close();
+ print_web_view_.release(); // Close deletes object.
+ print_pages_params_.reset();
+ }
+
+ if (!success) {
+ // TODO(sverrir): http://crbug.com/6833 Show some kind of notification.
+ }
+}
+
+bool PrintWebViewHelper::CopyAndPrint(const ViewMsg_PrintPages_Params& params,
+ WebFrame* web_frame) {
+ // Create a new WebView with the same settings as the current display one.
+ // Except that we disable javascript (don't want any active content running
+ // on the page).
+ WebPreferences prefs = web_frame->GetView()->GetPreferences();
+ prefs.javascript_enabled = false;
+ prefs.java_enabled = false;
+ print_web_view_.reset(WebView::Create(this, prefs));
+
+ print_pages_params_.reset(new ViewMsg_PrintPages_Params(params));
+ print_pages_params_->pages.clear(); // Print all pages of selection.
+
+ std::string html = web_frame->GetSelection(true);
+ std::string url_str = "data:text/html;charset=utf-8,";
+ url_str.append(html);
+ GURL url(url_str);
+
+ // When loading is done this will call DidStopLoading that will do the
+ // actual printing.
+ print_web_view_->GetMainFrame()->LoadRequest(WebKit::WebURLRequest(url));
+
+ return true;
+}
+
void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params,
- WebFrame* frame) {
+ WebFrame* frame) {
PrepareFrameAndViewForPrint prep_frame_view(params.params,
frame,
frame->GetView());
@@ -314,12 +355,14 @@ int32 PrintWebViewHelper::routing_id() {
return render_view_->routing_id();
}
-void PrintWebViewHelper::GetWindowRect(WebWidget* webwidget,
- WebKit::WebRect* rect) {
- NOTREACHED();
+void PrintWebViewHelper::DidStopLoading(WebView* webview) {
+ DCHECK(print_pages_params_.get() != NULL);
+ DCHECK_EQ(webview, print_web_view_.get());
+ PrintPages(*print_pages_params_.get(), print_web_view_->GetMainFrame());
}
-void PrintWebViewHelper::DidStopLoading(WebView* webview) {
+void PrintWebViewHelper::GetWindowRect(WebWidget* webwidget,
+ WebKit::WebRect* rect) {
NOTREACHED();
}
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index 1a56bd9..c2a02c2 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -20,6 +20,7 @@ class Message;
class RenderView;
class WebView;
+struct ViewMsg_Print_Params;
struct ViewMsg_PrintPage_Params;
struct ViewMsg_PrintPages_Params;
@@ -36,7 +37,17 @@ class PrintWebViewHelper : public WebViewDelegate {
void SyncPrint(WebFrame* frame);
+ // Is there a background print in progress?
+ bool IsPrinting() {
+ return print_web_view_.get() != NULL;
+ }
+
+ // Notification when printing is done - signal teardown
+ void DidFinishPrinting(bool success);
+
protected:
+ bool CopyAndPrint(const ViewMsg_PrintPages_Params& params,
+ WebFrame* web_frame);
// Prints the page listed in |params|.
void PrintPage(const ViewMsg_PrintPage_Params& params,
@@ -44,6 +55,7 @@ class PrintWebViewHelper : public WebViewDelegate {
WebFrame* frame);
// Prints all the pages listed in |params|.
+ // It will implicitly revert the document to display CSS media type.
void PrintPages(const ViewMsg_PrintPages_Params& params, WebFrame* frame);
// IPC::Message::Sender
@@ -52,7 +64,6 @@ class PrintWebViewHelper : public WebViewDelegate {
int32 routing_id();
// WebViewDeletegate
- virtual void DidStartLoading(WebView* webview) {}
virtual void DidStopLoading(WebView* webview);
virtual gfx::NativeViewId GetContainingView(WebWidget* webwidget);
virtual void DidInvalidateRect(WebWidget* webwidget,
@@ -85,6 +96,7 @@ class PrintWebViewHelper : public WebViewDelegate {
private:
RenderView* render_view_;
scoped_ptr<WebView> print_web_view_;
+ scoped_ptr<ViewMsg_PrintPages_Params> print_pages_params_;
private:
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper);
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index 6151878..8189dad 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -339,6 +339,7 @@ void RenderView::OnMessageReceived(const IPC::Message& message) {
IPC_BEGIN_MESSAGE_MAP(RenderView, message)
IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, SendThumbnail)
IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages)
+ IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone)
IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate)
IPC_MESSAGE_HANDLER(ViewMsg_Stop, OnStop)
IPC_MESSAGE_HANDLER(ViewMsg_LoadAlternateHTMLText, OnLoadAlternateHTMLText)
@@ -454,6 +455,15 @@ void RenderView::OnPrintPages() {
}
}
+void RenderView::OnPrintingDone(int document_cookie, bool success) {
+ // Ignoring document cookie here since only one print job can be outstanding
+ // per renderer and document_cookie is 0 when printing is successful.
+ DCHECK(print_helper_.get());
+ if (print_helper_.get() != NULL) {
+ print_helper_->DidFinishPrinting(success);
+ }
+}
+
void RenderView::CapturePageInfo(int load_id, bool preliminary_capture) {
if (load_id != page_id_)
return; // this capture call is no longer relevant due to navigation
@@ -2217,9 +2227,10 @@ void RenderView::SetInputMethodState(bool enabled) {
}
void RenderView::ScriptedPrint(WebFrame* frame) {
- print_render_view_.reset(new PrintWebViewHelper(this));
- print_render_view_->SyncPrint(frame);
- print_render_view_.reset();
+ if (print_helper_.get() == NULL) {
+ print_helper_.reset(new PrintWebViewHelper(this));
+ }
+ print_helper_->SyncPrint(frame);
}
void RenderView::WebInspectorOpened(int num_resources) {
diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h
index 5e9d75b..19f3c43 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -461,6 +461,7 @@ class RenderView : public RenderWidget,
// RenderView IPC message handlers
void SendThumbnail();
void OnPrintPages();
+ void OnPrintingDone(int document_cookie, bool success);
void OnNavigate(const ViewMsg_Navigate_Params& params);
void OnStop();
void OnLoadAlternateHTMLText(const std::string& html_contents,
@@ -793,8 +794,9 @@ class RenderView : public RenderWidget,
// to the new navigation is created. See DidCreateDataSource.
scoped_ptr<NavigationState> pending_navigation_state_;
- // Need for printing
- scoped_ptr<PrintWebViewHelper> print_render_view_;
+ // PrintWebViewHelper handles printing. Note that this object is constructed
+ // when printing for the first time but only destroyed with the RenderView.
+ scoped_ptr<PrintWebViewHelper> print_helper_;
RendererPreferences renderer_preferences_;