Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / android_webview / renderer / print_web_view_helper.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // TODO(sgurun) copied from chrome/renderer. Remove after crbug.com/322276
6
7 #include "android_webview/renderer/print_web_view_helper.h"
8
9 #include <string>
10
11 #include "android_webview/common/print_messages.h"
12 #include "base/auto_reset.h"
13 #include "base/command_line.h"
14 #include "base/json/json_writer.h"
15 #include "base/logging.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/histogram.h"
18 #include "base/process/process_handle.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "content/public/common/web_preferences.h"
23 #include "content/public/renderer/render_thread.h"
24 #include "content/public/renderer/render_view.h"
25 #include "net/base/escape.h"
26 #include "printing/metafile.h"
27 #include "printing/metafile_impl.h"
28 #include "printing/units.h"
29 #include "skia/ext/vector_platform_device_skia.h"
30 #include "third_party/WebKit/public/platform/WebSize.h"
31 #include "third_party/WebKit/public/platform/WebURLRequest.h"
32 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
33 #include "third_party/WebKit/public/web/WebDocument.h"
34 #include "third_party/WebKit/public/web/WebElement.h"
35 #include "third_party/WebKit/public/web/WebFrameClient.h"
36 #include "third_party/WebKit/public/web/WebLocalFrame.h"
37 #include "third_party/WebKit/public/web/WebPlugin.h"
38 #include "third_party/WebKit/public/web/WebPluginDocument.h"
39 #include "third_party/WebKit/public/web/WebPrintParams.h"
40 #include "third_party/WebKit/public/web/WebPrintScalingOption.h"
41 #include "third_party/WebKit/public/web/WebScriptSource.h"
42 #include "third_party/WebKit/public/web/WebSettings.h"
43 #include "third_party/WebKit/public/web/WebView.h"
44 #include "third_party/WebKit/public/web/WebViewClient.h"
45 #include "ui/base/l10n/l10n_util.h"
46 #include "ui/base/resource/resource_bundle.h"
47
48 // This code is copied from chrome/renderer/printing. Code is slightly
49 // modified to run it with webview, and the modifications are marked
50 // using OS_ANDROID.
51 // TODO(sgurun): remove the code as part of componentization of printing.
52
53 using content::WebPreferences;
54
55 namespace printing {
56
57 namespace {
58
59 enum PrintPreviewHelperEvents {
60   PREVIEW_EVENT_REQUESTED,
61   PREVIEW_EVENT_CACHE_HIT,  // Unused
62   PREVIEW_EVENT_CREATE_DOCUMENT,
63   PREVIEW_EVENT_NEW_SETTINGS,  // Unused
64   PREVIEW_EVENT_MAX,
65 };
66
67 const double kMinDpi = 1.0;
68
69 #if 0
70 // TODO(sgurun) android_webview hack
71 const char kPageLoadScriptFormat[] =
72     "document.open(); document.write(%s); document.close();";
73
74 const char kPageSetupScriptFormat[] = "setup(%s);";
75
76 void ExecuteScript(blink::WebFrame* frame,
77                    const char* script_format,
78                    const base::Value& parameters) {
79   std::string json;
80   base::JSONWriter::Write(&parameters, &json);
81   std::string script = base::StringPrintf(script_format, json.c_str());
82   frame->executeScript(blink::WebString(base::UTF8ToUTF16(script)));
83 }
84 #endif
85
86 int GetDPI(const PrintMsg_Print_Params* print_params) {
87 #if defined(OS_MACOSX)
88   // On the Mac, the printable area is in points, don't do any scaling based
89   // on dpi.
90   return kPointsPerInch;
91 #else
92   return static_cast<int>(print_params->dpi);
93 #endif  // defined(OS_MACOSX)
94 }
95
96 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) {
97   return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() &&
98          !params.printable_area.IsEmpty() && params.document_cookie &&
99          params.desired_dpi && params.max_shrink && params.min_shrink &&
100          params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0);
101 }
102
103 PrintMsg_Print_Params GetCssPrintParams(
104     blink::WebFrame* frame,
105     int page_index,
106     const PrintMsg_Print_Params& page_params) {
107   PrintMsg_Print_Params page_css_params = page_params;
108   int dpi = GetDPI(&page_params);
109
110   blink::WebSize page_size_in_pixels(
111       ConvertUnit(page_params.page_size.width(), dpi, kPixelsPerInch),
112       ConvertUnit(page_params.page_size.height(), dpi, kPixelsPerInch));
113   int margin_top_in_pixels =
114       ConvertUnit(page_params.margin_top, dpi, kPixelsPerInch);
115   int margin_right_in_pixels = ConvertUnit(
116       page_params.page_size.width() -
117       page_params.content_size.width() - page_params.margin_left,
118       dpi, kPixelsPerInch);
119   int margin_bottom_in_pixels = ConvertUnit(
120       page_params.page_size.height() -
121       page_params.content_size.height() - page_params.margin_top,
122       dpi, kPixelsPerInch);
123   int margin_left_in_pixels = ConvertUnit(
124       page_params.margin_left,
125       dpi, kPixelsPerInch);
126
127   blink::WebSize original_page_size_in_pixels = page_size_in_pixels;
128
129   if (frame) {
130     frame->pageSizeAndMarginsInPixels(page_index,
131                                       page_size_in_pixels,
132                                       margin_top_in_pixels,
133                                       margin_right_in_pixels,
134                                       margin_bottom_in_pixels,
135                                       margin_left_in_pixels);
136   }
137
138   int new_content_width = page_size_in_pixels.width -
139                           margin_left_in_pixels - margin_right_in_pixels;
140   int new_content_height = page_size_in_pixels.height -
141                            margin_top_in_pixels - margin_bottom_in_pixels;
142
143   // Invalid page size and/or margins. We just use the default setting.
144   if (new_content_width < 1 || new_content_height < 1) {
145     CHECK(frame != NULL);
146     page_css_params = GetCssPrintParams(NULL, page_index, page_params);
147     return page_css_params;
148   }
149
150   page_css_params.content_size = gfx::Size(
151       ConvertUnit(new_content_width, kPixelsPerInch, dpi),
152       ConvertUnit(new_content_height, kPixelsPerInch, dpi));
153
154   if (original_page_size_in_pixels != page_size_in_pixels) {
155     page_css_params.page_size = gfx::Size(
156         ConvertUnit(page_size_in_pixels.width, kPixelsPerInch, dpi),
157         ConvertUnit(page_size_in_pixels.height, kPixelsPerInch, dpi));
158   } else {
159     // Printing frame doesn't have any page size css. Pixels to dpi conversion
160     // causes rounding off errors. Therefore use the default page size values
161     // directly.
162     page_css_params.page_size = page_params.page_size;
163   }
164
165   page_css_params.margin_top =
166       ConvertUnit(margin_top_in_pixels, kPixelsPerInch, dpi);
167   page_css_params.margin_left =
168       ConvertUnit(margin_left_in_pixels, kPixelsPerInch, dpi);
169   return page_css_params;
170 }
171
172 double FitPrintParamsToPage(const PrintMsg_Print_Params& page_params,
173                             PrintMsg_Print_Params* params_to_fit) {
174   double content_width =
175       static_cast<double>(params_to_fit->content_size.width());
176   double content_height =
177       static_cast<double>(params_to_fit->content_size.height());
178   int default_page_size_height = page_params.page_size.height();
179   int default_page_size_width = page_params.page_size.width();
180   int css_page_size_height = params_to_fit->page_size.height();
181   int css_page_size_width = params_to_fit->page_size.width();
182
183   double scale_factor = 1.0f;
184   if (page_params.page_size == params_to_fit->page_size)
185     return scale_factor;
186
187   if (default_page_size_width < css_page_size_width ||
188       default_page_size_height < css_page_size_height) {
189     double ratio_width =
190         static_cast<double>(default_page_size_width) / css_page_size_width;
191     double ratio_height =
192         static_cast<double>(default_page_size_height) / css_page_size_height;
193     scale_factor = ratio_width < ratio_height ? ratio_width : ratio_height;
194     content_width *= scale_factor;
195     content_height *= scale_factor;
196   }
197   params_to_fit->margin_top = static_cast<int>(
198       (default_page_size_height - css_page_size_height * scale_factor) / 2 +
199       (params_to_fit->margin_top * scale_factor));
200   params_to_fit->margin_left = static_cast<int>(
201       (default_page_size_width - css_page_size_width * scale_factor) / 2 +
202       (params_to_fit->margin_left * scale_factor));
203   params_to_fit->content_size = gfx::Size(
204       static_cast<int>(content_width), static_cast<int>(content_height));
205   params_to_fit->page_size = page_params.page_size;
206   return scale_factor;
207 }
208
209 void CalculatePageLayoutFromPrintParams(
210     const PrintMsg_Print_Params& params,
211     PageSizeMargins* page_layout_in_points) {
212   int dpi = GetDPI(&params);
213   int content_width = params.content_size.width();
214   int content_height = params.content_size.height();
215
216   int margin_bottom = params.page_size.height() -
217                       content_height - params.margin_top;
218   int margin_right = params.page_size.width() -
219                       content_width - params.margin_left;
220
221   page_layout_in_points->content_width =
222       ConvertUnit(content_width, dpi, kPointsPerInch);
223   page_layout_in_points->content_height =
224       ConvertUnit(content_height, dpi, kPointsPerInch);
225   page_layout_in_points->margin_top =
226       ConvertUnit(params.margin_top, dpi, kPointsPerInch);
227   page_layout_in_points->margin_right =
228       ConvertUnit(margin_right, dpi, kPointsPerInch);
229   page_layout_in_points->margin_bottom =
230       ConvertUnit(margin_bottom, dpi, kPointsPerInch);
231   page_layout_in_points->margin_left =
232       ConvertUnit(params.margin_left, dpi, kPointsPerInch);
233 }
234
235 void EnsureOrientationMatches(const PrintMsg_Print_Params& css_params,
236                               PrintMsg_Print_Params* page_params) {
237   if ((page_params->page_size.width() > page_params->page_size.height()) ==
238       (css_params.page_size.width() > css_params.page_size.height())) {
239     return;
240   }
241
242   // Swap the |width| and |height| values.
243   page_params->page_size.SetSize(page_params->page_size.height(),
244                                  page_params->page_size.width());
245   page_params->content_size.SetSize(page_params->content_size.height(),
246                                     page_params->content_size.width());
247   page_params->printable_area.set_size(
248       gfx::Size(page_params->printable_area.height(),
249                 page_params->printable_area.width()));
250 }
251
252 void ComputeWebKitPrintParamsInDesiredDpi(
253     const PrintMsg_Print_Params& print_params,
254     blink::WebPrintParams* webkit_print_params) {
255   int dpi = GetDPI(&print_params);
256   webkit_print_params->printerDPI = dpi;
257   webkit_print_params->printScalingOption = print_params.print_scaling_option;
258
259   webkit_print_params->printContentArea.width =
260       ConvertUnit(print_params.content_size.width(), dpi,
261                   print_params.desired_dpi);
262   webkit_print_params->printContentArea.height =
263       ConvertUnit(print_params.content_size.height(), dpi,
264                   print_params.desired_dpi);
265
266   webkit_print_params->printableArea.x =
267       ConvertUnit(print_params.printable_area.x(), dpi,
268                   print_params.desired_dpi);
269   webkit_print_params->printableArea.y =
270       ConvertUnit(print_params.printable_area.y(), dpi,
271                   print_params.desired_dpi);
272   webkit_print_params->printableArea.width =
273       ConvertUnit(print_params.printable_area.width(), dpi,
274                   print_params.desired_dpi);
275   webkit_print_params->printableArea.height =
276       ConvertUnit(print_params.printable_area.height(),
277                   dpi, print_params.desired_dpi);
278
279   webkit_print_params->paperSize.width =
280       ConvertUnit(print_params.page_size.width(), dpi,
281                   print_params.desired_dpi);
282   webkit_print_params->paperSize.height =
283       ConvertUnit(print_params.page_size.height(), dpi,
284                   print_params.desired_dpi);
285 }
286
287 blink::WebPlugin* GetPlugin(const blink::WebFrame* frame) {
288   return frame->document().isPluginDocument() ?
289          frame->document().to<blink::WebPluginDocument>().plugin() : NULL;
290 }
291
292 bool PrintingNodeOrPdfFrame(const blink::WebFrame* frame,
293                             const blink::WebNode& node) {
294   if (!node.isNull())
295     return true;
296   blink::WebPlugin* plugin = GetPlugin(frame);
297   return plugin && plugin->supportsPaginatedPrint();
298 }
299
300 bool PrintingFrameHasPageSizeStyle(blink::WebFrame* frame,
301                                    int total_page_count) {
302   if (!frame)
303     return false;
304   bool frame_has_custom_page_size_style = false;
305   for (int i = 0; i < total_page_count; ++i) {
306     if (frame->hasCustomPageSizeStyle(i)) {
307       frame_has_custom_page_size_style = true;
308       break;
309     }
310   }
311   return frame_has_custom_page_size_style;
312 }
313
314 MarginType GetMarginsForPdf(blink::WebFrame* frame,
315                             const blink::WebNode& node) {
316   if (frame->isPrintScalingDisabledForPlugin(node))
317     return NO_MARGINS;
318   else
319     return PRINTABLE_AREA_MARGINS;
320 }
321
322 bool FitToPageEnabled(const base::DictionaryValue& job_settings) {
323   bool fit_to_paper_size = false;
324   if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) {
325     NOTREACHED();
326   }
327   return fit_to_paper_size;
328 }
329
330 PrintMsg_Print_Params CalculatePrintParamsForCss(
331     blink::WebFrame* frame,
332     int page_index,
333     const PrintMsg_Print_Params& page_params,
334     bool ignore_css_margins,
335     bool fit_to_page,
336     double* scale_factor) {
337   PrintMsg_Print_Params css_params = GetCssPrintParams(frame, page_index,
338                                                        page_params);
339
340   PrintMsg_Print_Params params = page_params;
341   EnsureOrientationMatches(css_params, &params);
342
343   if (ignore_css_margins && fit_to_page)
344     return params;
345
346   PrintMsg_Print_Params result_params = css_params;
347   if (ignore_css_margins) {
348     result_params.margin_top = params.margin_top;
349     result_params.margin_left = params.margin_left;
350
351     DCHECK(!fit_to_page);
352     // Since we are ignoring the margins, the css page size is no longer
353     // valid.
354     int default_margin_right = params.page_size.width() -
355         params.content_size.width() - params.margin_left;
356     int default_margin_bottom = params.page_size.height() -
357         params.content_size.height() - params.margin_top;
358     result_params.content_size = gfx::Size(
359         result_params.page_size.width() - result_params.margin_left -
360             default_margin_right,
361         result_params.page_size.height() - result_params.margin_top -
362             default_margin_bottom);
363   }
364
365   if (fit_to_page) {
366     double factor = FitPrintParamsToPage(params, &result_params);
367     if (scale_factor)
368       *scale_factor = factor;
369   }
370   return result_params;
371 }
372
373 bool IsPrintPreviewEnabled() {
374   return false;
375 }
376
377 bool IsPrintThrottlingDisabled() {
378   return true;
379 }
380
381 }  // namespace
382
383 FrameReference::FrameReference(blink::WebLocalFrame* frame) {
384   Reset(frame);
385 }
386
387 FrameReference::FrameReference() {
388   Reset(NULL);
389 }
390
391 FrameReference::~FrameReference() {
392 }
393
394 void FrameReference::Reset(blink::WebLocalFrame* frame) {
395   if (frame) {
396     view_ = frame->view();
397     frame_ = frame;
398   } else {
399     view_ = NULL;
400     frame_ = NULL;
401   }
402 }
403
404 blink::WebLocalFrame* FrameReference::GetFrame() {
405   if (view_ == NULL || frame_ == NULL)
406     return NULL;
407   for (blink::WebFrame* frame = view_->mainFrame(); frame != NULL;
408            frame = frame->traverseNext(false)) {
409     if (frame == frame_)
410       return frame_;
411   }
412   return NULL;
413 }
414
415 blink::WebView* FrameReference::view() {
416   return view_;
417 }
418
419 // static - Not anonymous so that platform implementations can use it.
420 void PrintWebViewHelper::PrintHeaderAndFooter(
421     blink::WebCanvas* canvas,
422     int page_number,
423     int total_pages,
424     float webkit_scale_factor,
425     const PageSizeMargins& page_layout,
426     const base::DictionaryValue& header_footer_info,
427     const PrintMsg_Print_Params& params) {
428 #if 0
429   // TODO(sgurun) android_webview hack
430   skia::VectorPlatformDeviceSkia* device =
431       static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice());
432   device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea);
433
434   SkAutoCanvasRestore auto_restore(canvas, true);
435   canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor);
436
437   blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right +
438                             page_layout.content_width,
439                             page_layout.margin_top + page_layout.margin_bottom +
440                             page_layout.content_height);
441
442   blink::WebView* web_view = blink::WebView::create(NULL);
443   web_view->settings()->setJavaScriptEnabled(true);
444   blink::WebFrame* frame = blink::WebLocalFrame::create(NULL)
445   web_view->setMainFrame(web_frame);
446
447   base::StringValue html(
448       ResourceBundle::GetSharedInstance().GetLocalizedString(
449           IDR_PRINT_PREVIEW_PAGE));
450   // Load page with script to avoid async operations.
451   ExecuteScript(frame, kPageLoadScriptFormat, html);
452
453   scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy());
454   options->SetDouble("width", page_size.width);
455   options->SetDouble("height", page_size.height);
456   options->SetDouble("topMargin", page_layout.margin_top);
457   options->SetDouble("bottomMargin", page_layout.margin_bottom);
458   options->SetString("pageNumber",
459                      base::StringPrintf("%d/%d", page_number, total_pages));
460
461   ExecuteScript(frame, kPageSetupScriptFormat, *options);
462
463   blink::WebPrintParams webkit_params(page_size);
464   webkit_params.printerDPI = GetDPI(&params);
465
466   frame->printBegin(webkit_params, WebKit::WebNode(), NULL);
467   frame->printPage(0, canvas);
468   frame->printEnd();
469
470   web_view->close();
471   frame->close();
472
473   device->setDrawingArea(SkPDFDevice::kContent_DrawingArea);
474 #endif
475 }
476
477 // static - Not anonymous so that platform implementations can use it.
478 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame,
479                                             int page_number,
480                                             const gfx::Rect& canvas_area,
481                                             const gfx::Rect& content_area,
482                                             double scale_factor,
483                                             blink::WebCanvas* canvas) {
484   SkAutoCanvasRestore auto_restore(canvas, true);
485   if (content_area != canvas_area) {
486     canvas->translate((content_area.x() - canvas_area.x()) / scale_factor,
487                       (content_area.y() - canvas_area.y()) / scale_factor);
488     SkRect clip_rect(
489         SkRect::MakeXYWH(content_area.origin().x() / scale_factor,
490                          content_area.origin().y() / scale_factor,
491                          content_area.size().width() / scale_factor,
492                          content_area.size().height() / scale_factor));
493     SkIRect clip_int_rect;
494     clip_rect.roundOut(&clip_int_rect);
495     SkRegion clip_region(clip_int_rect);
496     canvas->setClipRegion(clip_region);
497   }
498   return frame->printPage(page_number, canvas);
499 }
500
501 // Class that calls the Begin and End print functions on the frame and changes
502 // the size of the view temporarily to support full page printing..
503 class PrepareFrameAndViewForPrint : public blink::WebViewClient,
504                                     public blink::WebFrameClient {
505  public:
506   PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params,
507                               blink::WebLocalFrame* frame,
508                               const blink::WebNode& node,
509                               bool ignore_css_margins);
510   virtual ~PrepareFrameAndViewForPrint();
511
512   // Optional. Replaces |frame_| with selection if needed. Will call |on_ready|
513   // when completed.
514   void CopySelectionIfNeeded(const WebPreferences& preferences,
515                              const base::Closure& on_ready);
516
517   // Prepares frame for printing.
518   void StartPrinting();
519
520   blink::WebLocalFrame* frame() {
521     return frame_.GetFrame();
522   }
523
524   const blink::WebNode& node() const {
525     return node_to_print_;
526   }
527
528   int GetExpectedPageCount() const {
529     return expected_pages_count_;
530   }
531
532   gfx::Size GetPrintCanvasSize() const;
533
534   void FinishPrinting();
535
536   bool IsLoadingSelection() {
537     // It's not selection if not |owns_web_view_|.
538     return owns_web_view_ && frame() && frame()->isLoading();
539   }
540
541   // TODO(ojan): Remove this override and have this class use a non-null
542   // layerTreeView.
543   // blink::WebViewClient override:
544   virtual bool allowsBrokenNullLayerTreeView() const;
545
546  protected:
547   // blink::WebViewClient override:
548   virtual void didStopLoading();
549
550   // blink::WebFrameClient override:
551   virtual blink::WebFrame* createChildFrame(blink::WebLocalFrame* parent,
552                                             const blink::WebString& name);
553   virtual void frameDetached(blink::WebFrame* frame);
554
555  private:
556   void CallOnReady();
557   void ResizeForPrinting();
558   void RestoreSize();
559   void CopySelection(const WebPreferences& preferences);
560
561   base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_;
562
563   FrameReference frame_;
564   blink::WebNode node_to_print_;
565   bool owns_web_view_;
566   blink::WebPrintParams web_print_params_;
567   gfx::Size prev_view_size_;
568   gfx::Size prev_scroll_offset_;
569   int expected_pages_count_;
570   base::Closure on_ready_;
571   bool should_print_backgrounds_;
572   bool should_print_selection_only_;
573   bool is_printing_started_;
574
575   DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint);
576 };
577
578 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
579     const PrintMsg_Print_Params& params,
580     blink::WebLocalFrame* frame,
581     const blink::WebNode& node,
582     bool ignore_css_margins)
583     : weak_ptr_factory_(this),
584       frame_(frame),
585       node_to_print_(node),
586       owns_web_view_(false),
587       expected_pages_count_(0),
588       should_print_backgrounds_(params.should_print_backgrounds),
589       should_print_selection_only_(params.selection_only),
590       is_printing_started_(false) {
591   PrintMsg_Print_Params print_params = params;
592   if (!should_print_selection_only_ ||
593       !PrintingNodeOrPdfFrame(frame, node_to_print_)) {
594     bool fit_to_page = ignore_css_margins &&
595                        print_params.print_scaling_option ==
596                             blink::WebPrintScalingOptionFitToPrintableArea;
597     ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
598     frame->printBegin(web_print_params_, node_to_print_);
599     print_params = CalculatePrintParamsForCss(frame, 0, print_params,
600                                               ignore_css_margins, fit_to_page,
601                                               NULL);
602     frame->printEnd();
603   }
604   ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
605 }
606
607 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
608   FinishPrinting();
609 }
610
611 void PrepareFrameAndViewForPrint::ResizeForPrinting() {
612   // Layout page according to printer page size. Since WebKit shrinks the
613   // size of the page automatically (from 125% to 200%) we trick it to
614   // think the page is 125% larger so the size of the page is correct for
615   // minimum (default) scaling.
616   // This is important for sites that try to fill the page.
617   gfx::Size print_layout_size(web_print_params_.printContentArea.width,
618                               web_print_params_.printContentArea.height);
619   print_layout_size.set_height(
620       static_cast<int>(static_cast<double>(print_layout_size.height()) * 1.25));
621
622   if (!frame())
623     return;
624   blink::WebView* web_view = frame_.view();
625   // Backup size and offset.
626   if (blink::WebFrame* web_frame = web_view->mainFrame())
627     prev_scroll_offset_ = web_frame->scrollOffset();
628   prev_view_size_ = web_view->size();
629
630   web_view->resize(print_layout_size);
631 }
632
633
634 void PrepareFrameAndViewForPrint::StartPrinting() {
635   ResizeForPrinting();
636   blink::WebView* web_view = frame_.view();
637   web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_);
638   expected_pages_count_ =
639       frame()->printBegin(web_print_params_, node_to_print_);
640   is_printing_started_ = true;
641 }
642
643 void PrepareFrameAndViewForPrint::CopySelectionIfNeeded(
644     const WebPreferences& preferences,
645     const base::Closure& on_ready) {
646   on_ready_ = on_ready;
647   if (should_print_selection_only_)
648     CopySelection(preferences);
649   else
650     didStopLoading();
651 }
652
653 void PrepareFrameAndViewForPrint::CopySelection(
654     const WebPreferences& preferences) {
655   ResizeForPrinting();
656   std::string url_str = "data:text/html;charset=utf-8,";
657   url_str.append(
658       net::EscapeQueryParamValue(frame()->selectionAsMarkup().utf8(), false));
659   RestoreSize();
660   // Create a new WebView with the same settings as the current display one.
661   // Except that we disable javascript (don't want any active content running
662   // on the page).
663   WebPreferences prefs = preferences;
664   prefs.javascript_enabled = false;
665   prefs.java_enabled = false;
666
667   blink::WebView* web_view = blink::WebView::create(this);
668   owns_web_view_ = true;
669   content::RenderView::ApplyWebPreferences(prefs, web_view);
670   web_view->setMainFrame(blink::WebLocalFrame::create(this));
671   frame_.Reset(web_view->mainFrame()->toWebLocalFrame());
672   node_to_print_.reset();
673
674   // When loading is done this will call didStopLoading() and that will do the
675   // actual printing.
676   frame()->loadRequest(blink::WebURLRequest(GURL(url_str)));
677 }
678
679 bool PrepareFrameAndViewForPrint::allowsBrokenNullLayerTreeView() const {
680   return true;
681 }
682
683 void PrepareFrameAndViewForPrint::didStopLoading() {
684   DCHECK(!on_ready_.is_null());
685   // Don't call callback here, because it can delete |this| and WebView that is
686   // called didStopLoading.
687   base::MessageLoop::current()->PostTask(
688       FROM_HERE,
689       base::Bind(&PrepareFrameAndViewForPrint::CallOnReady,
690                  weak_ptr_factory_.GetWeakPtr()));
691 }
692
693 blink::WebFrame* PrepareFrameAndViewForPrint::createChildFrame(
694     blink::WebLocalFrame* parent,
695     const blink::WebString& name) {
696   blink::WebFrame* frame = blink::WebLocalFrame::create(this);
697   parent->appendChild(frame);
698   return frame;
699 }
700
701 void PrepareFrameAndViewForPrint::frameDetached(blink::WebFrame* frame) {
702   if (frame->parent())
703     frame->parent()->removeChild(frame);
704   frame->close();
705 }
706
707 void PrepareFrameAndViewForPrint::CallOnReady() {
708   return on_ready_.Run();  // Can delete |this|.
709 }
710
711 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const {
712   DCHECK(is_printing_started_);
713   return gfx::Size(web_print_params_.printContentArea.width,
714                    web_print_params_.printContentArea.height);
715 }
716
717 void PrepareFrameAndViewForPrint::RestoreSize() {
718   if (frame()) {
719     blink::WebView* web_view = frame_.GetFrame()->view();
720     web_view->resize(prev_view_size_);
721     if (blink::WebFrame* web_frame = web_view->mainFrame())
722       web_frame->setScrollOffset(prev_scroll_offset_);
723   }
724 }
725
726 void PrepareFrameAndViewForPrint::FinishPrinting() {
727   blink::WebFrame* frame = frame_.GetFrame();
728   if (frame) {
729     blink::WebView* web_view = frame->view();
730     if (is_printing_started_) {
731       is_printing_started_ = false;
732       frame->printEnd();
733       if (!owns_web_view_) {
734         web_view->settings()->setShouldPrintBackgrounds(false);
735         RestoreSize();
736       }
737     }
738     if (owns_web_view_) {
739       DCHECK(!frame->isLoading());
740       owns_web_view_ = false;
741       web_view->close();
742     }
743   }
744   frame_.Reset(NULL);
745   on_ready_.Reset();
746 }
747
748 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
749     : content::RenderViewObserver(render_view),
750       content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
751       reset_prep_frame_view_(false),
752       is_preview_enabled_(IsPrintPreviewEnabled()),
753       is_scripted_print_throttling_disabled_(IsPrintThrottlingDisabled()),
754       is_print_ready_metafile_sent_(false),
755       ignore_css_margins_(false),
756       user_cancelled_scripted_print_count_(0),
757       is_scripted_printing_blocked_(false),
758       notify_browser_of_print_failure_(true),
759       print_for_preview_(false),
760       print_node_in_progress_(false),
761       is_loading_(false),
762       is_scripted_preview_delayed_(false),
763       weak_ptr_factory_(this) {
764   // TODO(sgurun) enable window.print() for webview crbug.com/322303
765   SetScriptedPrintBlocked(true);
766 }
767
768 PrintWebViewHelper::~PrintWebViewHelper() {}
769
770 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed(
771     blink::WebFrame* frame, bool user_initiated) {
772 #if defined(OS_ANDROID)
773   return false;
774 #endif  // defined(OS_ANDROID)
775   if (is_scripted_printing_blocked_)
776     return false;
777   // If preview is enabled, then the print dialog is tab modal, and the user
778   // can always close the tab on a mis-behaving page (the system print dialog
779   // is app modal). If the print was initiated through user action, don't
780   // throttle. Or, if the command line flag to skip throttling has been set.
781   if (!is_scripted_print_throttling_disabled_ &&
782       !is_preview_enabled_ &&
783       !user_initiated)
784     return !IsScriptInitiatedPrintTooFrequent(frame);
785   return true;
786 }
787
788 void PrintWebViewHelper::DidStartLoading() {
789   is_loading_ = true;
790 }
791
792 void PrintWebViewHelper::DidStopLoading() {
793   is_loading_ = false;
794   ShowScriptedPrintPreview();
795 }
796
797 // Prints |frame| which called window.print().
798 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame,
799                                    bool user_initiated) {
800   DCHECK(frame);
801
802 #if !defined(OS_ANDROID)
803   // TODO(sgurun) android_webview hack
804   // Allow Prerendering to cancel this print request if necessary.
805   if (prerender::PrerenderHelper::IsPrerendering(render_view())) {
806     Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id()));
807     return;
808   }
809 #endif  // !defined(OS_ANDROID)
810
811   if (!IsScriptInitiatedPrintAllowed(frame, user_initiated))
812     return;
813   IncrementScriptedPrintCount();
814
815   if (is_preview_enabled_) {
816     print_preview_context_.InitWithFrame(frame);
817     RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
818   } else {
819     Print(frame, blink::WebNode());
820   }
821 }
822
823 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
824   bool handled = true;
825   IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
826     IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
827     IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog)
828     IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview)
829     IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
830     IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview)
831     IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
832     IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount,
833                         ResetScriptedPrintCount)
834     IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked,
835                         SetScriptedPrintBlocked)
836     IPC_MESSAGE_UNHANDLED(handled = false)
837     IPC_END_MESSAGE_MAP()
838   return handled;
839 }
840
841 void PrintWebViewHelper::OnPrintForPrintPreview(
842     const base::DictionaryValue& job_settings) {
843   DCHECK(is_preview_enabled_);
844   // If still not finished with earlier print request simply ignore.
845   if (prep_frame_view_)
846     return;
847
848   if (!render_view()->GetWebView())
849     return;
850   blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
851   if (!main_frame)
852     return;
853
854   blink::WebDocument document = main_frame->document();
855   // <object> with id="pdf-viewer" is created in
856   // chrome/browser/resources/print_preview/print_preview.js
857   blink::WebElement pdf_element = document.getElementById("pdf-viewer");
858   if (pdf_element.isNull()) {
859     NOTREACHED();
860     return;
861   }
862
863   // Set |print_for_preview_| flag and autoreset it to back to original
864   // on return.
865   base::AutoReset<bool> set_printing_flag(&print_for_preview_, true);
866
867   blink::WebLocalFrame* pdf_frame = pdf_element.document().frame();
868   if (!UpdatePrintSettings(pdf_frame, pdf_element, job_settings)) {
869     LOG(ERROR) << "UpdatePrintSettings failed";
870     DidFinishPrinting(FAIL_PRINT);
871     return;
872   }
873
874   // Print page onto entire page not just printable area. Preview PDF already
875   // has content in correct position taking into account page size and printable
876   // area.
877   // TODO(vitalybuka) : Make this consistent on all platform. This change
878   // affects Windows only. On Linux and OSX RenderPagesForPrint does not use
879   // printable_area. Also we can't change printable_area deeper inside
880   // RenderPagesForPrint for Windows, because it's used also by native
881   // printing and it expects real printable_area value.
882   // See http://crbug.com/123408
883   PrintMsg_Print_Params& print_params = print_pages_params_->params;
884   print_params.printable_area = gfx::Rect(print_params.page_size);
885
886   // Render Pages for printing.
887   if (!RenderPagesForPrint(pdf_frame, pdf_element)) {
888     LOG(ERROR) << "RenderPagesForPrint failed";
889     DidFinishPrinting(FAIL_PRINT);
890   }
891 }
892
893 bool PrintWebViewHelper::GetPrintFrame(blink::WebLocalFrame** frame) {
894   DCHECK(frame);
895   blink::WebView* webView = render_view()->GetWebView();
896   DCHECK(webView);
897   if (!webView)
898     return false;
899
900   // If the user has selected text in the currently focused frame we print
901   // only that frame (this makes print selection work for multiple frames).
902   blink::WebLocalFrame* focusedFrame =
903       webView->focusedFrame()->toWebLocalFrame();
904   *frame = focusedFrame->hasSelection()
905                ? focusedFrame
906                : webView->mainFrame()->toWebLocalFrame();
907   return true;
908 }
909
910 void PrintWebViewHelper::OnPrintPages() {
911   blink::WebLocalFrame* frame;
912   if (GetPrintFrame(&frame))
913     Print(frame, blink::WebNode());
914 }
915
916 void PrintWebViewHelper::OnPrintForSystemDialog() {
917   blink::WebLocalFrame* frame = print_preview_context_.source_frame();
918   if (!frame) {
919     NOTREACHED();
920     return;
921   }
922
923   Print(frame, print_preview_context_.source_node());
924 }
925
926 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
927     const PageSizeMargins& page_layout_in_points,
928     gfx::Size* page_size,
929     gfx::Rect* content_area) {
930   *page_size = gfx::Size(
931       page_layout_in_points.content_width +
932           page_layout_in_points.margin_right +
933           page_layout_in_points.margin_left,
934       page_layout_in_points.content_height +
935           page_layout_in_points.margin_top +
936           page_layout_in_points.margin_bottom);
937   *content_area = gfx::Rect(page_layout_in_points.margin_left,
938                             page_layout_in_points.margin_top,
939                             page_layout_in_points.content_width,
940                             page_layout_in_points.content_height);
941 }
942
943 void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
944     const base::DictionaryValue& settings) {
945   int margins_type = 0;
946   if (!settings.GetInteger(kSettingMarginsType, &margins_type))
947     margins_type = DEFAULT_MARGINS;
948   ignore_css_margins_ = (margins_type != DEFAULT_MARGINS);
949 }
950
951 bool PrintWebViewHelper::IsPrintToPdfRequested(
952     const base::DictionaryValue& job_settings) {
953   bool print_to_pdf = false;
954   if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf))
955     NOTREACHED();
956   return print_to_pdf;
957 }
958
959 blink::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption(
960     bool source_is_html, const base::DictionaryValue& job_settings,
961     const PrintMsg_Print_Params& params) {
962   DCHECK(!print_for_preview_);
963
964   if (params.print_to_pdf)
965     return blink::WebPrintScalingOptionSourceSize;
966
967   if (!source_is_html) {
968     if (!FitToPageEnabled(job_settings))
969       return blink::WebPrintScalingOptionNone;
970
971     bool no_plugin_scaling =
972         print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin(
973             print_preview_context_.source_node());
974
975     if (params.is_first_request && no_plugin_scaling)
976       return blink::WebPrintScalingOptionNone;
977   }
978   return blink::WebPrintScalingOptionFitToPrintableArea;
979 }
980
981 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) {
982   DCHECK(is_preview_enabled_);
983   print_preview_context_.OnPrintPreview();
984
985   UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
986                             PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX);
987
988   if (!UpdatePrintSettings(print_preview_context_.source_frame(),
989                            print_preview_context_.source_node(), settings)) {
990     if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
991       Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
992           routing_id(), print_pages_params_->params.document_cookie));
993       notify_browser_of_print_failure_ = false;  // Already sent.
994     }
995     DidFinishPrinting(FAIL_PREVIEW);
996     return;
997   }
998
999   // If we are previewing a pdf and the print scaling is disabled, send a
1000   // message to browser.
1001   if (print_pages_params_->params.is_first_request &&
1002       !print_preview_context_.IsModifiable() &&
1003       print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin(
1004           print_preview_context_.source_node())) {
1005     Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id()));
1006   }
1007
1008   is_print_ready_metafile_sent_ = false;
1009
1010   // PDF printer device supports alpha blending.
1011   print_pages_params_->params.supports_alpha_blend = true;
1012
1013   bool generate_draft_pages = false;
1014   if (!settings.GetBoolean(kSettingGenerateDraftData,
1015                            &generate_draft_pages)) {
1016     NOTREACHED();
1017   }
1018   print_preview_context_.set_generate_draft_pages(generate_draft_pages);
1019
1020   PrepareFrameForPreviewDocument();
1021 }
1022
1023 void PrintWebViewHelper::PrepareFrameForPreviewDocument() {
1024   reset_prep_frame_view_ = false;
1025
1026   if (!print_pages_params_ || CheckForCancel()) {
1027     DidFinishPrinting(FAIL_PREVIEW);
1028     return;
1029   }
1030
1031   // Don't reset loading frame or WebKit will fail assert. Just retry when
1032   // current selection is loaded.
1033   if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) {
1034     reset_prep_frame_view_ = true;
1035     return;
1036   }
1037
1038   const PrintMsg_Print_Params& print_params = print_pages_params_->params;
1039   prep_frame_view_.reset(
1040       new PrepareFrameAndViewForPrint(print_params,
1041                                       print_preview_context_.source_frame(),
1042                                       print_preview_context_.source_node(),
1043                                       ignore_css_margins_));
1044   prep_frame_view_->CopySelectionIfNeeded(
1045       render_view()->GetWebkitPreferences(),
1046       base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument,
1047                  base::Unretained(this)));
1048 }
1049
1050 void PrintWebViewHelper::OnFramePreparedForPreviewDocument() {
1051   if (reset_prep_frame_view_) {
1052     PrepareFrameForPreviewDocument();
1053     return;
1054   }
1055   DidFinishPrinting(CreatePreviewDocument() ? OK : FAIL_PREVIEW);
1056 }
1057
1058 bool PrintWebViewHelper::CreatePreviewDocument() {
1059   if (!print_pages_params_ || CheckForCancel())
1060     return false;
1061
1062   UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
1063                             PREVIEW_EVENT_CREATE_DOCUMENT, PREVIEW_EVENT_MAX);
1064
1065   const PrintMsg_Print_Params& print_params = print_pages_params_->params;
1066   const std::vector<int>& pages = print_pages_params_->pages;
1067
1068   if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(),
1069                                                     pages)) {
1070     return false;
1071   }
1072
1073   PageSizeMargins default_page_layout;
1074   ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0,
1075                                   print_params, ignore_css_margins_, NULL,
1076                                   &default_page_layout);
1077
1078   bool has_page_size_style = PrintingFrameHasPageSizeStyle(
1079       print_preview_context_.prepared_frame(),
1080       print_preview_context_.total_page_count());
1081   int dpi = GetDPI(&print_params);
1082
1083   gfx::Rect printable_area_in_points(
1084       ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch),
1085       ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch),
1086       ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch),
1087       ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch));
1088
1089   // Margins: Send default page layout to browser process.
1090   Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
1091                                                 default_page_layout,
1092                                                 printable_area_in_points,
1093                                                 has_page_size_style));
1094
1095   PrintHostMsg_DidGetPreviewPageCount_Params params;
1096   params.page_count = print_preview_context_.total_page_count();
1097   params.is_modifiable = print_preview_context_.IsModifiable();
1098   params.document_cookie = print_params.document_cookie;
1099   params.preview_request_id = print_params.preview_request_id;
1100   params.clear_preview_data = print_preview_context_.generate_draft_pages();
1101   Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params));
1102   if (CheckForCancel())
1103     return false;
1104
1105   while (!print_preview_context_.IsFinalPageRendered()) {
1106     int page_number = print_preview_context_.GetNextPageNumber();
1107     DCHECK_GE(page_number, 0);
1108     if (!RenderPreviewPage(page_number, print_params))
1109       return false;
1110
1111     if (CheckForCancel())
1112       return false;
1113
1114     // We must call PrepareFrameAndViewForPrint::FinishPrinting() (by way of
1115     // print_preview_context_.AllPagesRendered()) before calling
1116     // FinalizePrintReadyDocument() when printing a PDF because the plugin
1117     // code does not generate output until we call FinishPrinting().  We do not
1118     // generate draft pages for PDFs, so IsFinalPageRendered() and
1119     // IsLastPageOfPrintReadyMetafile() will be true in the same iteration of
1120     // the loop.
1121     if (print_preview_context_.IsFinalPageRendered())
1122       print_preview_context_.AllPagesRendered();
1123
1124     if (print_preview_context_.IsLastPageOfPrintReadyMetafile()) {
1125       DCHECK(print_preview_context_.IsModifiable() ||
1126              print_preview_context_.IsFinalPageRendered());
1127       if (!FinalizePrintReadyDocument())
1128         return false;
1129     }
1130   }
1131   print_preview_context_.Finished();
1132   return true;
1133 }
1134
1135 bool PrintWebViewHelper::FinalizePrintReadyDocument() {
1136   DCHECK(!is_print_ready_metafile_sent_);
1137   print_preview_context_.FinalizePrintReadyDocument();
1138
1139   // Get the size of the resulting metafile.
1140   PreviewMetafile* metafile = print_preview_context_.metafile();
1141   uint32 buf_size = metafile->GetDataSize();
1142   DCHECK_GT(buf_size, 0u);
1143
1144   PrintHostMsg_DidPreviewDocument_Params preview_params;
1145   preview_params.reuse_existing_data = false;
1146   preview_params.data_size = buf_size;
1147   preview_params.document_cookie = print_pages_params_->params.document_cookie;
1148   preview_params.expected_pages_count =
1149       print_preview_context_.total_page_count();
1150   preview_params.modifiable = print_preview_context_.IsModifiable();
1151   preview_params.preview_request_id =
1152       print_pages_params_->params.preview_request_id;
1153
1154   // Ask the browser to create the shared memory for us.
1155   if (!CopyMetafileDataToSharedMem(metafile,
1156                                    &(preview_params.metafile_data_handle))) {
1157     LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
1158     print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
1159     return false;
1160   }
1161   is_print_ready_metafile_sent_ = true;
1162
1163   Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), preview_params));
1164   return true;
1165 }
1166
1167 void PrintWebViewHelper::OnPrintingDone(bool success) {
1168   notify_browser_of_print_failure_ = false;
1169   if (!success)
1170     LOG(ERROR) << "Failure in OnPrintingDone";
1171   DidFinishPrinting(success ? OK : FAIL_PRINT);
1172 }
1173
1174 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) {
1175   is_scripted_printing_blocked_ = blocked;
1176 }
1177
1178 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) {
1179   DCHECK(is_preview_enabled_);
1180   blink::WebLocalFrame* frame = NULL;
1181   GetPrintFrame(&frame);
1182   DCHECK(frame);
1183   print_preview_context_.InitWithFrame(frame);
1184   RequestPrintPreview(selection_only ?
1185                       PRINT_PREVIEW_USER_INITIATED_SELECTION :
1186                       PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME);
1187 }
1188
1189 bool PrintWebViewHelper::IsPrintingEnabled() {
1190   bool result = false;
1191   Send(new PrintHostMsg_IsPrintingEnabled(routing_id(), &result));
1192   return result;
1193 }
1194
1195 void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
1196   if (node.isNull() || !node.document().frame()) {
1197     // This can occur when the context menu refers to an invalid WebNode.
1198     // See http://crbug.com/100890#c17 for a repro case.
1199     return;
1200   }
1201
1202   if (print_node_in_progress_) {
1203     // This can happen as a result of processing sync messages when printing
1204     // from ppapi plugins. It's a rare case, so its OK to just fail here.
1205     // See http://crbug.com/159165.
1206     return;
1207   }
1208
1209   print_node_in_progress_ = true;
1210
1211   // Make a copy of the node, in case RenderView::OnContextMenuClosed resets
1212   // its |context_menu_node_|.
1213   if (is_preview_enabled_) {
1214     print_preview_context_.InitWithNode(node);
1215     RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE);
1216   } else {
1217     blink::WebNode duplicate_node(node);
1218     Print(duplicate_node.document().frame(), duplicate_node);
1219   }
1220
1221   print_node_in_progress_ = false;
1222 }
1223
1224 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
1225                                const blink::WebNode& node) {
1226   // If still not finished with earlier print request simply ignore.
1227   if (prep_frame_view_)
1228     return;
1229
1230   FrameReference frame_ref(frame);
1231
1232   int expected_page_count = 0;
1233   if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
1234     DidFinishPrinting(FAIL_PRINT_INIT);
1235     return;  // Failed to init print page settings.
1236   }
1237
1238   // Some full screen plugins can say they don't want to print.
1239   if (!expected_page_count) {
1240     DidFinishPrinting(FAIL_PRINT);
1241     return;
1242   }
1243
1244 #if !defined(OS_ANDROID)
1245   // TODO(sgurun) android_webview hack
1246   // Ask the browser to show UI to retrieve the final print settings.
1247   if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node,
1248                                 expected_page_count)) {
1249     DidFinishPrinting(OK);  // Release resources and fail silently.
1250     return;
1251   }
1252 #endif  // !defined(OS_ANDROID)
1253
1254   // Render Pages for printing.
1255   if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) {
1256     LOG(ERROR) << "RenderPagesForPrint failed";
1257     DidFinishPrinting(FAIL_PRINT);
1258   }
1259   ResetScriptedPrintCount();
1260 }
1261
1262 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
1263   switch (result) {
1264     case OK:
1265       break;
1266
1267     case FAIL_PRINT_INIT:
1268       DCHECK(!notify_browser_of_print_failure_);
1269       break;
1270
1271     case FAIL_PRINT:
1272       if (notify_browser_of_print_failure_ && print_pages_params_.get()) {
1273         int cookie = print_pages_params_->params.document_cookie;
1274         Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
1275       }
1276       break;
1277
1278     case FAIL_PREVIEW:
1279       DCHECK(is_preview_enabled_);
1280       int cookie = print_pages_params_.get() ?
1281           print_pages_params_->params.document_cookie : 0;
1282       if (notify_browser_of_print_failure_) {
1283         LOG(ERROR) << "CreatePreviewDocument failed";
1284         Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie));
1285       } else {
1286         Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie));
1287       }
1288       print_preview_context_.Failed(notify_browser_of_print_failure_);
1289       break;
1290   }
1291
1292   prep_frame_view_.reset();
1293   print_pages_params_.reset();
1294   notify_browser_of_print_failure_ = true;
1295 }
1296
1297 void PrintWebViewHelper::OnFramePreparedForPrintPages() {
1298   PrintPages();
1299   FinishFramePrinting();
1300 }
1301
1302 void PrintWebViewHelper::PrintPages() {
1303   if (!prep_frame_view_)  // Printing is already canceled or failed.
1304     return;
1305   prep_frame_view_->StartPrinting();
1306
1307   int page_count = prep_frame_view_->GetExpectedPageCount();
1308   if (!page_count) {
1309     LOG(ERROR) << "Can't print 0 pages.";
1310     return DidFinishPrinting(FAIL_PRINT);
1311   }
1312
1313   const PrintMsg_PrintPages_Params& params = *print_pages_params_;
1314   const PrintMsg_Print_Params& print_params = params.params;
1315
1316 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
1317   // TODO(vitalybuka): should be page_count or valid pages from params.pages.
1318   // See http://crbug.com/161576
1319   Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(),
1320                                                 print_params.document_cookie,
1321                                                 page_count));
1322 #endif  // !defined(OS_CHROMEOS)
1323
1324   if (print_params.preview_ui_id < 0) {
1325     // Printing for system dialog.
1326     int printed_count = params.pages.empty() ? page_count : params.pages.size();
1327 #if !defined(OS_CHROMEOS)
1328     UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", printed_count);
1329 #else
1330     UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrintWebDialog",
1331                          printed_count);
1332 #endif  // !defined(OS_CHROMEOS)
1333   }
1334
1335
1336   if (!PrintPagesNative(prep_frame_view_->frame(), page_count,
1337                         prep_frame_view_->GetPrintCanvasSize())) {
1338     LOG(ERROR) << "Printing failed.";
1339     return DidFinishPrinting(FAIL_PRINT);
1340   }
1341 }
1342
1343 void PrintWebViewHelper::FinishFramePrinting() {
1344   prep_frame_view_.reset();
1345 }
1346
1347 #if defined(OS_MACOSX) || defined(OS_WIN)
1348 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
1349                                           int page_count,
1350                                           const gfx::Size& canvas_size) {
1351   const PrintMsg_PrintPages_Params& params = *print_pages_params_;
1352   const PrintMsg_Print_Params& print_params = params.params;
1353
1354   PrintMsg_PrintPage_Params page_params;
1355   page_params.params = print_params;
1356   if (params.pages.empty()) {
1357     for (int i = 0; i < page_count; ++i) {
1358       page_params.page_number = i;
1359       PrintPageInternal(page_params, canvas_size, frame);
1360     }
1361   } else {
1362     for (size_t i = 0; i < params.pages.size(); ++i) {
1363       if (params.pages[i] >= page_count)
1364         break;
1365       page_params.page_number = params.pages[i];
1366       PrintPageInternal(page_params, canvas_size, frame);
1367     }
1368   }
1369   return true;
1370 }
1371
1372 #endif  // OS_MACOSX || OS_WIN
1373
1374 // static - Not anonymous so that platform implementations can use it.
1375 void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
1376     blink::WebFrame* frame,
1377     int page_index,
1378     const PrintMsg_Print_Params& page_params,
1379     bool ignore_css_margins,
1380     double* scale_factor,
1381     PageSizeMargins* page_layout_in_points) {
1382   PrintMsg_Print_Params params = CalculatePrintParamsForCss(
1383       frame, page_index, page_params, ignore_css_margins,
1384       page_params.print_scaling_option ==
1385           blink::WebPrintScalingOptionFitToPrintableArea,
1386       scale_factor);
1387   CalculatePageLayoutFromPrintParams(params, page_layout_in_points);
1388 }
1389
1390 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
1391   PrintMsg_PrintPages_Params settings;
1392   Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1393                                                 &settings.params));
1394   // Check if the printer returned any settings, if the settings is empty, we
1395   // can safely assume there are no printer drivers configured. So we safely
1396   // terminate.
1397   bool result = true;
1398   if (!PrintMsg_Print_Params_IsValid(settings.params))
1399     result = false;
1400
1401   if (result &&
1402       (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
1403     // Invalid print page settings.
1404     NOTREACHED();
1405     result = false;
1406   }
1407
1408   // Reset to default values.
1409   ignore_css_margins_ = false;
1410   settings.pages.clear();
1411
1412   settings.params.print_scaling_option =
1413       blink::WebPrintScalingOptionSourceSize;
1414   if (fit_to_paper_size) {
1415     settings.params.print_scaling_option =
1416         blink::WebPrintScalingOptionFitToPrintableArea;
1417   }
1418
1419   print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1420   return result;
1421 }
1422
1423 bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
1424                                                 const blink::WebNode& node,
1425                                                 int* number_of_pages) {
1426   DCHECK(frame);
1427   bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
1428   if (!InitPrintSettings(fit_to_paper_size)) {
1429     notify_browser_of_print_failure_ = false;
1430 #if !defined(OS_ANDROID)
1431     // TODO(sgurun) android_webview hack
1432     render_view()->RunModalAlertDialog(
1433         frame,
1434         l10n_util::GetStringUTF16(IDS_PRINT_INVALID_PRINTER_SETTINGS));
1435 #endif  //  !defined(OS_ANDROID)
1436     return false;
1437   }
1438
1439   const PrintMsg_Print_Params& params = print_pages_params_->params;
1440   PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_);
1441   prepare.StartPrinting();
1442
1443   Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
1444                                              params.document_cookie));
1445   *number_of_pages = prepare.GetExpectedPageCount();
1446   return true;
1447 }
1448
1449 bool PrintWebViewHelper::UpdatePrintSettings(
1450     blink::WebLocalFrame* frame,
1451     const blink::WebNode& node,
1452     const base::DictionaryValue& passed_job_settings) {
1453   DCHECK(is_preview_enabled_);
1454   const base::DictionaryValue* job_settings = &passed_job_settings;
1455   base::DictionaryValue modified_job_settings;
1456   if (job_settings->empty()) {
1457     if (!print_for_preview_)
1458       print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1459     return false;
1460   }
1461
1462   bool source_is_html = true;
1463   if (print_for_preview_) {
1464     if (!job_settings->GetBoolean(kSettingPreviewModifiable, &source_is_html)) {
1465       NOTREACHED();
1466     }
1467   } else {
1468     source_is_html = !PrintingNodeOrPdfFrame(frame, node);
1469   }
1470
1471   if (print_for_preview_ || !source_is_html) {
1472     modified_job_settings.MergeDictionary(job_settings);
1473     modified_job_settings.SetBoolean(kSettingHeaderFooterEnabled, false);
1474     modified_job_settings.SetInteger(kSettingMarginsType, NO_MARGINS);
1475     job_settings = &modified_job_settings;
1476   }
1477
1478   // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
1479   // possible.
1480   int cookie = print_pages_params_.get() ?
1481       print_pages_params_->params.document_cookie : 0;
1482   PrintMsg_PrintPages_Params settings;
1483   Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), cookie, *job_settings,
1484                                             &settings));
1485   print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1486
1487   if (!PrintMsg_Print_Params_IsValid(settings.params)) {
1488     if (!print_for_preview_) {
1489       print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1490     } else {
1491 #if !defined(OS_ANDROID)
1492       // TODO(sgurun) android_webview hack
1493       // PrintForPrintPreview
1494       blink::WebFrame* print_frame = NULL;
1495       // This may not be the right frame, but the alert will be modal,
1496       // therefore it works well enough.
1497       GetPrintFrame(&print_frame);
1498       if (print_frame) {
1499         render_view()->RunModalAlertDialog(
1500             print_frame,
1501             l10n_util::GetStringUTF16(
1502                 IDS_PRINT_INVALID_PRINTER_SETTINGS));
1503       }
1504 #endif  // !defined(OS_ANDROID)
1505     }
1506     return false;
1507   }
1508
1509   if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) {
1510     print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS);
1511     return false;
1512   }
1513
1514   if (!job_settings->GetInteger(kPreviewUIID, &settings.params.preview_ui_id)) {
1515     NOTREACHED();
1516     print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1517     return false;
1518   }
1519
1520   if (!print_for_preview_) {
1521     // Validate expected print preview settings.
1522     if (!job_settings->GetInteger(kPreviewRequestID,
1523                                   &settings.params.preview_request_id) ||
1524         !job_settings->GetBoolean(kIsFirstRequest,
1525                                   &settings.params.is_first_request)) {
1526       NOTREACHED();
1527       print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1528       return false;
1529     }
1530
1531     settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1532     UpdateFrameMarginsCssInfo(*job_settings);
1533     settings.params.print_scaling_option = GetPrintScalingOption(
1534         source_is_html, *job_settings, settings.params);
1535
1536     // Header/Footer: Set |header_footer_info_|.
1537     if (settings.params.display_header_footer) {
1538       header_footer_info_.reset(new base::DictionaryValue());
1539       header_footer_info_->SetDouble(kSettingHeaderFooterDate,
1540                                      base::Time::Now().ToJsTime());
1541       header_footer_info_->SetString(kSettingHeaderFooterURL,
1542                                      settings.params.url);
1543       header_footer_info_->SetString(kSettingHeaderFooterTitle,
1544                                      settings.params.title);
1545     }
1546   }
1547
1548   print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1549   Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
1550                                              settings.params.document_cookie));
1551
1552   return true;
1553 }
1554
1555 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame,
1556                                                   const blink::WebNode& node,
1557                                                   int expected_pages_count) {
1558   PrintHostMsg_ScriptedPrint_Params params;
1559   PrintMsg_PrintPages_Params print_settings;
1560
1561   params.cookie = print_pages_params_->params.document_cookie;
1562   params.has_selection = frame->hasSelection();
1563   params.expected_pages_count = expected_pages_count;
1564   MarginType margin_type = DEFAULT_MARGINS;
1565   if (PrintingNodeOrPdfFrame(frame, node))
1566     margin_type = GetMarginsForPdf(frame, node);
1567   params.margin_type = margin_type;
1568
1569   Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
1570
1571   // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the
1572   // value before and restore it afterwards.
1573   blink::WebPrintScalingOption scaling_option =
1574       print_pages_params_->params.print_scaling_option;
1575
1576   print_pages_params_.reset();
1577   IPC::SyncMessage* msg =
1578       new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
1579   msg->EnableMessagePumping();
1580   Send(msg);
1581   print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings));
1582
1583   print_pages_params_->params.print_scaling_option = scaling_option;
1584   return (print_settings.params.dpi && print_settings.params.document_cookie);
1585 }
1586
1587 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame,
1588                                              const blink::WebNode& node) {
1589   if (!frame || prep_frame_view_)
1590     return false;
1591   const PrintMsg_PrintPages_Params& params = *print_pages_params_;
1592   const PrintMsg_Print_Params& print_params = params.params;
1593   prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
1594       print_params, frame, node, ignore_css_margins_));
1595   DCHECK(!print_pages_params_->params.selection_only ||
1596          print_pages_params_->pages.empty());
1597   prep_frame_view_->CopySelectionIfNeeded(
1598       render_view()->GetWebkitPreferences(),
1599       base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages,
1600                  base::Unretained(this)));
1601   return true;
1602 }
1603
1604 #if defined(OS_POSIX)
1605 bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
1606     Metafile* metafile,
1607     base::SharedMemoryHandle* shared_mem_handle) {
1608   uint32 buf_size = metafile->GetDataSize();
1609   scoped_ptr<base::SharedMemory> shared_buf(
1610       content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(
1611           buf_size).release());
1612
1613   if (shared_buf.get()) {
1614     if (shared_buf->Map(buf_size)) {
1615       metafile->GetData(shared_buf->memory(), buf_size);
1616       shared_buf->GiveToProcess(base::GetCurrentProcessHandle(),
1617                                 shared_mem_handle);
1618       return true;
1619     }
1620   }
1621   NOTREACHED();
1622   return false;
1623 }
1624 #endif  // defined(OS_POSIX)
1625
1626 bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent(
1627     blink::WebFrame* frame) {
1628   const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2;
1629   const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32;
1630   bool too_frequent = false;
1631
1632   // Check if there is script repeatedly trying to print and ignore it if too
1633   // frequent.  The first 3 times, we use a constant wait time, but if this
1634   // gets excessive, we switch to exponential wait time. So for a page that
1635   // calls print() in a loop the user will need to cancel the print dialog
1636   // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds.
1637   // This gives the user time to navigate from the page.
1638   if (user_cancelled_scripted_print_count_ > 0) {
1639     base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_;
1640     int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint;
1641     if (user_cancelled_scripted_print_count_ > 3) {
1642       min_wait_seconds = std::min(
1643           kMinSecondsToIgnoreJavascriptInitiatedPrint <<
1644               (user_cancelled_scripted_print_count_ - 3),
1645           kMaxSecondsToIgnoreJavascriptInitiatedPrint);
1646     }
1647     if (diff.InSeconds() < min_wait_seconds) {
1648       too_frequent = true;
1649     }
1650   }
1651
1652   if (!too_frequent)
1653     return false;
1654
1655   blink::WebString message(
1656       blink::WebString::fromUTF8("Ignoring too frequent calls to print()."));
1657   frame->addMessageToConsole(
1658       blink::WebConsoleMessage(
1659           blink::WebConsoleMessage::LevelWarning, message));
1660   return true;
1661 }
1662
1663 void PrintWebViewHelper::ResetScriptedPrintCount() {
1664   // Reset cancel counter on successful print.
1665   user_cancelled_scripted_print_count_ = 0;
1666 }
1667
1668 void PrintWebViewHelper::IncrementScriptedPrintCount() {
1669   ++user_cancelled_scripted_print_count_;
1670   last_cancelled_script_print_ = base::Time::Now();
1671 }
1672
1673 void PrintWebViewHelper::ShowScriptedPrintPreview() {
1674   if (is_scripted_preview_delayed_) {
1675     is_scripted_preview_delayed_ = false;
1676     Send(new PrintHostMsg_ShowScriptedPrintPreview(routing_id(),
1677             print_preview_context_.IsModifiable()));
1678   }
1679 }
1680
1681 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) {
1682   const bool is_modifiable = print_preview_context_.IsModifiable();
1683   const bool has_selection = print_preview_context_.HasSelection();
1684   PrintHostMsg_RequestPrintPreview_Params params;
1685   params.is_modifiable = is_modifiable;
1686   params.has_selection = has_selection;
1687   switch (type) {
1688     case PRINT_PREVIEW_SCRIPTED: {
1689       // Shows scripted print preview in two stages.
1690       // 1. PrintHostMsg_SetupScriptedPrintPreview blocks this call and JS by
1691       //    pumping messages here.
1692       // 2. PrintHostMsg_ShowScriptedPrintPreview shows preview once the
1693       //    document has been loaded.
1694       is_scripted_preview_delayed_ = true;
1695       if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) {
1696         // Wait for DidStopLoading. Plugins may not know the correct
1697         // |is_modifiable| value until they are fully loaded, which occurs when
1698         // DidStopLoading() is called. Defer showing the preview until then.
1699       } else {
1700         base::MessageLoop::current()->PostTask(
1701             FROM_HERE,
1702             base::Bind(&PrintWebViewHelper::ShowScriptedPrintPreview,
1703                        weak_ptr_factory_.GetWeakPtr()));
1704       }
1705       IPC::SyncMessage* msg =
1706           new PrintHostMsg_SetupScriptedPrintPreview(routing_id());
1707       msg->EnableMessagePumping();
1708       Send(msg);
1709       is_scripted_preview_delayed_ = false;
1710       return;
1711     }
1712     case PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME: {
1713       break;
1714     }
1715     case PRINT_PREVIEW_USER_INITIATED_SELECTION: {
1716       DCHECK(has_selection);
1717       params.selection_only = has_selection;
1718       break;
1719     }
1720     case PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE: {
1721       params.webnode_only = true;
1722       break;
1723     }
1724     default: {
1725       NOTREACHED();
1726       return;
1727     }
1728   }
1729   Send(new PrintHostMsg_RequestPrintPreview(routing_id(), params));
1730 }
1731
1732 bool PrintWebViewHelper::CheckForCancel() {
1733   const PrintMsg_Print_Params& print_params = print_pages_params_->params;
1734   bool cancel = false;
1735   Send(new PrintHostMsg_CheckForCancel(routing_id(),
1736                                        print_params.preview_ui_id,
1737                                        print_params.preview_request_id,
1738                                        &cancel));
1739   if (cancel)
1740     notify_browser_of_print_failure_ = false;
1741   return cancel;
1742 }
1743
1744 bool PrintWebViewHelper::PreviewPageRendered(int page_number,
1745                                              Metafile* metafile) {
1746   DCHECK_GE(page_number, FIRST_PAGE_INDEX);
1747
1748   // For non-modifiable files, |metafile| should be NULL, so do not bother
1749   // sending a message. If we don't generate draft metafiles, |metafile| is
1750   // NULL.
1751   if (!print_preview_context_.IsModifiable() ||
1752       !print_preview_context_.generate_draft_pages()) {
1753     DCHECK(!metafile);
1754     return true;
1755   }
1756
1757   if (!metafile) {
1758     NOTREACHED();
1759     print_preview_context_.set_error(
1760         PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE);
1761     return false;
1762   }
1763
1764   PrintHostMsg_DidPreviewPage_Params preview_page_params;
1765   // Get the size of the resulting metafile.
1766   uint32 buf_size = metafile->GetDataSize();
1767   DCHECK_GT(buf_size, 0u);
1768   if (!CopyMetafileDataToSharedMem(
1769       metafile, &(preview_page_params.metafile_data_handle))) {
1770     LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
1771     print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
1772     return false;
1773   }
1774   preview_page_params.data_size = buf_size;
1775   preview_page_params.page_number = page_number;
1776   preview_page_params.preview_request_id =
1777       print_pages_params_->params.preview_request_id;
1778
1779   Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params));
1780   return true;
1781 }
1782
1783 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext()
1784     : total_page_count_(0),
1785       current_page_index_(0),
1786       generate_draft_pages_(true),
1787       print_ready_metafile_page_count_(0),
1788       error_(PREVIEW_ERROR_NONE),
1789       state_(UNINITIALIZED) {
1790 }
1791
1792 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() {
1793 }
1794
1795 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame(
1796     blink::WebLocalFrame* web_frame) {
1797   DCHECK(web_frame);
1798   DCHECK(!IsRendering());
1799   state_ = INITIALIZED;
1800   source_frame_.Reset(web_frame);
1801   source_node_.reset();
1802 }
1803
1804 void PrintWebViewHelper::PrintPreviewContext::InitWithNode(
1805     const blink::WebNode& web_node) {
1806   DCHECK(!web_node.isNull());
1807   DCHECK(web_node.document().frame());
1808   DCHECK(!IsRendering());
1809   state_ = INITIALIZED;
1810   source_frame_.Reset(web_node.document().frame());
1811   source_node_ = web_node;
1812 }
1813
1814 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
1815   DCHECK_EQ(INITIALIZED, state_);
1816   ClearContext();
1817 }
1818
1819 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
1820     PrepareFrameAndViewForPrint* prepared_frame,
1821     const std::vector<int>& pages) {
1822   DCHECK_EQ(INITIALIZED, state_);
1823   state_ = RENDERING;
1824
1825   // Need to make sure old object gets destroyed first.
1826   prep_frame_view_.reset(prepared_frame);
1827   prep_frame_view_->StartPrinting();
1828
1829   total_page_count_ = prep_frame_view_->GetExpectedPageCount();
1830   if (total_page_count_ == 0) {
1831     LOG(ERROR) << "CreatePreviewDocument got 0 page count";
1832     set_error(PREVIEW_ERROR_ZERO_PAGES);
1833     return false;
1834   }
1835
1836   metafile_.reset(new PreviewMetafile);
1837   if (!metafile_->Init()) {
1838     set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED);
1839     LOG(ERROR) << "PreviewMetafile Init failed";
1840     return false;
1841   }
1842
1843   current_page_index_ = 0;
1844   pages_to_render_ = pages;
1845   // Sort and make unique.
1846   std::sort(pages_to_render_.begin(), pages_to_render_.end());
1847   pages_to_render_.resize(std::unique(pages_to_render_.begin(),
1848                                       pages_to_render_.end()) -
1849                           pages_to_render_.begin());
1850   // Remove invalid pages.
1851   pages_to_render_.resize(std::lower_bound(pages_to_render_.begin(),
1852                                            pages_to_render_.end(),
1853                                            total_page_count_) -
1854                           pages_to_render_.begin());
1855   print_ready_metafile_page_count_ = pages_to_render_.size();
1856   if (pages_to_render_.empty()) {
1857     print_ready_metafile_page_count_ = total_page_count_;
1858     // Render all pages.
1859     for (int i = 0; i < total_page_count_; ++i)
1860       pages_to_render_.push_back(i);
1861   } else if (generate_draft_pages_) {
1862     int pages_index = 0;
1863     for (int i = 0; i < total_page_count_; ++i) {
1864       if (pages_index < print_ready_metafile_page_count_ &&
1865           i == pages_to_render_[pages_index]) {
1866         pages_index++;
1867         continue;
1868       }
1869       pages_to_render_.push_back(i);
1870     }
1871   }
1872
1873   document_render_time_ = base::TimeDelta();
1874   begin_time_ = base::TimeTicks::Now();
1875
1876   return true;
1877 }
1878
1879 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage(
1880     const base::TimeDelta& page_time) {
1881   DCHECK_EQ(RENDERING, state_);
1882   document_render_time_ += page_time;
1883   UMA_HISTOGRAM_TIMES("PrintPreview.RenderPDFPageTime", page_time);
1884 }
1885
1886 void PrintWebViewHelper::PrintPreviewContext::AllPagesRendered() {
1887   DCHECK_EQ(RENDERING, state_);
1888   state_ = DONE;
1889   prep_frame_view_->FinishPrinting();
1890 }
1891
1892 void PrintWebViewHelper::PrintPreviewContext::FinalizePrintReadyDocument() {
1893   DCHECK(IsRendering());
1894
1895   base::TimeTicks begin_time = base::TimeTicks::Now();
1896   metafile_->FinishDocument();
1897
1898   if (print_ready_metafile_page_count_ <= 0) {
1899     NOTREACHED();
1900     return;
1901   }
1902
1903   UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderToPDFTime",
1904                              document_render_time_);
1905   base::TimeDelta total_time = (base::TimeTicks::Now() - begin_time) +
1906                                document_render_time_;
1907   UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTime",
1908                              total_time);
1909   UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage",
1910                              total_time / pages_to_render_.size());
1911 }
1912
1913 void PrintWebViewHelper::PrintPreviewContext::Finished() {
1914   DCHECK_EQ(DONE, state_);
1915   state_ = INITIALIZED;
1916   ClearContext();
1917 }
1918
1919 void PrintWebViewHelper::PrintPreviewContext::Failed(bool report_error) {
1920   DCHECK(state_ == INITIALIZED || state_ == RENDERING);
1921   state_ = INITIALIZED;
1922   if (report_error) {
1923     DCHECK_NE(PREVIEW_ERROR_NONE, error_);
1924     UMA_HISTOGRAM_ENUMERATION("PrintPreview.RendererError", error_,
1925                               PREVIEW_ERROR_LAST_ENUM);
1926   }
1927   ClearContext();
1928 }
1929
1930 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() {
1931   DCHECK_EQ(RENDERING, state_);
1932   if (IsFinalPageRendered())
1933     return -1;
1934   return pages_to_render_[current_page_index_++];
1935 }
1936
1937 bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const {
1938   return state_ == RENDERING || state_ == DONE;
1939 }
1940
1941 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() {
1942   // The only kind of node we can print right now is a PDF node.
1943   return !PrintingNodeOrPdfFrame(source_frame(), source_node_);
1944 }
1945
1946 bool PrintWebViewHelper::PrintPreviewContext::HasSelection() {
1947   return IsModifiable() && source_frame()->hasSelection();
1948 }
1949
1950 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile()
1951     const {
1952   DCHECK(IsRendering());
1953   return current_page_index_ == print_ready_metafile_page_count_;
1954 }
1955
1956 bool  PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const {
1957   DCHECK(IsRendering());
1958   return static_cast<size_t>(current_page_index_) == pages_to_render_.size();
1959 }
1960
1961 void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages(
1962     bool generate_draft_pages) {
1963   DCHECK_EQ(INITIALIZED, state_);
1964   generate_draft_pages_ = generate_draft_pages;
1965 }
1966
1967 void PrintWebViewHelper::PrintPreviewContext::set_error(
1968     enum PrintPreviewErrorBuckets error) {
1969   error_ = error;
1970 }
1971
1972 blink::WebLocalFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() {
1973   DCHECK(state_ != UNINITIALIZED);
1974   return source_frame_.GetFrame();
1975 }
1976
1977 const blink::WebNode&
1978     PrintWebViewHelper::PrintPreviewContext::source_node() const {
1979   DCHECK(state_ != UNINITIALIZED);
1980   return source_node_;
1981 }
1982
1983 blink::WebLocalFrame*
1984 PrintWebViewHelper::PrintPreviewContext::prepared_frame() {
1985   DCHECK(state_ != UNINITIALIZED);
1986   return prep_frame_view_->frame();
1987 }
1988
1989 const blink::WebNode&
1990     PrintWebViewHelper::PrintPreviewContext::prepared_node() const {
1991   DCHECK(state_ != UNINITIALIZED);
1992   return prep_frame_view_->node();
1993 }
1994
1995 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const {
1996   DCHECK(state_ != UNINITIALIZED);
1997   return total_page_count_;
1998 }
1999
2000 bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const {
2001   return generate_draft_pages_;
2002 }
2003
2004 PreviewMetafile* PrintWebViewHelper::PrintPreviewContext::metafile() {
2005   DCHECK(IsRendering());
2006   return metafile_.get();
2007 }
2008
2009 int PrintWebViewHelper::PrintPreviewContext::last_error() const {
2010   return error_;
2011 }
2012
2013 gfx::Size PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
2014   DCHECK(IsRendering());
2015   return prep_frame_view_->GetPrintCanvasSize();
2016 }
2017
2018 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
2019   prep_frame_view_.reset();
2020   metafile_.reset();
2021   pages_to_render_.clear();
2022   error_ = PREVIEW_ERROR_NONE;
2023 }
2024
2025 }  // namespace printing