2011-01-05  Alexey Proskuryakov  <ap@apple.com>

        Reviewed by Anders Carlsson.

        https://bugs.webkit.org/show_bug.cgi?id=51973
        Make main frame PDF printing work

        * UIProcess/API/mac/PDFViewController.h:
        * UIProcess/API/mac/PDFViewController.mm: (WebKit::PDFViewController::makePrintOperation):
        Ask PDFDocument to create an NSPrintOperation.

        * UIProcess/API/mac/WKView.h:
        * UIProcess/API/mac/WKView.mm:
        (-[WKView printOperationWithPrintInfo:forFrame:]): Create a new NSPrintOperation, either
        from scratch or from a PDF view.
        (-[WKView canPrintHeadersAndFooters]): Report if the view can be re-paginated to add headers
        and footers.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@75135 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebKit2/ChangeLog b/WebKit2/ChangeLog
index 2dcd2c4..a70c60c 100644
--- a/WebKit2/ChangeLog
+++ b/WebKit2/ChangeLog
@@ -1,3 +1,21 @@
+2011-01-05  Alexey Proskuryakov  <ap@apple.com>
+
+        Reviewed by Anders Carlsson.
+
+        https://bugs.webkit.org/show_bug.cgi?id=51973
+        Make main frame PDF printing work
+
+        * UIProcess/API/mac/PDFViewController.h:
+        * UIProcess/API/mac/PDFViewController.mm: (WebKit::PDFViewController::makePrintOperation):
+        Ask PDFDocument to create an NSPrintOperation.
+
+        * UIProcess/API/mac/WKView.h:
+        * UIProcess/API/mac/WKView.mm:
+        (-[WKView printOperationWithPrintInfo:forFrame:]): Create a new NSPrintOperation, either
+        from scratch or from a PDF view.
+        (-[WKView canPrintHeadersAndFooters]): Report if the view can be re-paginated to add headers
+        and footers.
+
 2011-01-05  Anders Carlsson  <andersca@apple.com>
 
         Reviewed by Sam Weinig.
diff --git a/WebKit2/UIProcess/API/mac/PDFViewController.h b/WebKit2/UIProcess/API/mac/PDFViewController.h
index 51f581a3..15043bd 100644
--- a/WebKit2/UIProcess/API/mac/PDFViewController.h
+++ b/WebKit2/UIProcess/API/mac/PDFViewController.h
@@ -53,6 +53,8 @@
 
     static Class pdfDocumentClass();
     static Class pdfPreviewViewClass();
+
+    NSPrintOperation *makePrintOperation(NSPrintInfo *);
     
 private:
     explicit PDFViewController(WKView *wkView);
diff --git a/WebKit2/UIProcess/API/mac/PDFViewController.mm b/WebKit2/UIProcess/API/mac/PDFViewController.mm
index 476dc7b..e01d0b3 100644
--- a/WebKit2/UIProcess/API/mac/PDFViewController.mm
+++ b/WebKit2/UIProcess/API/mac/PDFViewController.mm
@@ -32,6 +32,10 @@
 @class PDFDocument;
 @class PDFView;
 
+@interface PDFDocument (PDFDocumentDetails)
+- (NSPrintOperation *)getPrintOperationForPrintInfo:(NSPrintInfo *)printInfo autoRotate:(BOOL)doRotate;
+@end
+
 extern "C" NSString *_NSPathForSystemFramework(NSString *framework);
     
 @interface WKPDFView : NSView
@@ -179,4 +183,9 @@
     return pdfKitBundle;
 }
 
+NSPrintOperation *PDFViewController::makePrintOperation(NSPrintInfo *printInfo)
+{
+    return [[m_pdfView document] getPrintOperationForPrintInfo:printInfo autoRotate:YES];
+}
+
 } // namespace WebKit
diff --git a/WebKit2/UIProcess/API/mac/WKView.h b/WebKit2/UIProcess/API/mac/WKView.h
index 10f7688..a63f757 100644
--- a/WebKit2/UIProcess/API/mac/WKView.h
+++ b/WebKit2/UIProcess/API/mac/WKView.h
@@ -36,6 +36,9 @@
 - (id)initWithFrame:(NSRect)frame contextRef:(WKContextRef)contextRef;
 - (id)initWithFrame:(NSRect)frame contextRef:(WKContextRef)contextRef pageGroupRef:(WKPageGroupRef)pageGroupRef;
 
+- (NSPrintOperation *)printOperationWithPrintInfo:(NSPrintInfo *)printInfo forFrame:(WKFrameRef)frameRef;
+- (BOOL)canPrintHeadersAndFooters;
+
 @property(readonly) WKPageRef pageRef;
 
 @property BOOL drawsBackground;
diff --git a/WebKit2/UIProcess/API/mac/WKView.mm b/WebKit2/UIProcess/API/mac/WKView.mm
index a66581a..3b4e1f9 100644
--- a/WebKit2/UIProcess/API/mac/WKView.mm
+++ b/WebKit2/UIProcess/API/mac/WKView.mm
@@ -1196,6 +1196,22 @@
     return (NSInteger)self;
 }
 
+- (NSPrintOperation *)printOperationWithPrintInfo:(NSPrintInfo *)printInfo forFrame:(WKFrameRef)frameRef
+{
+    // Only the top frame can currently contain a PDF view.
+    if (_data->_pdfViewController) {
+        ASSERT(toImpl(frameRef)->isMainFrame());
+        return _data->_pdfViewController->makePrintOperation(printInfo);
+    }
+    return [NSPrintOperation printOperationWithView:self printInfo:printInfo];
+}
+
+- (BOOL)canPrintHeadersAndFooters
+{
+    // PDF documents are already paginated, so we can't change them to add headers and footers.
+    return !_data->_pdfViewController;
+}
+
 @end
 
 @implementation WKView (Internal)