blob: 449b31c33c3ce5c8be7c06a9d3c36bdfeef531b6 [file] [log] [blame]
sammc3d0df3a2014-11-24 21:25:381// Copyright 2014 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#include "chrome/browser/printing/print_view_manager_common.h"
6
Sebastien Marchandf1349f52019-01-25 03:16:417#include "base/bind.h"
thestigcb959ce2016-11-17 05:56:328#include "content/public/browser/render_frame_host.h"
Scott Violetc8240b02018-03-08 22:03:599#include "extensions/buildflags/buildflags.h"
Scott Violet318a55f2018-03-30 19:08:1910#include "printing/buildflags/buildflags.h"
Brett Wilson65f951c2016-11-03 22:06:1211
brettw00899e62016-11-12 02:10:1712#if BUILDFLAG(ENABLE_EXTENSIONS)
fsamuel8dfa19a2015-05-05 01:00:3913#include "components/guest_view/browser/guest_view_manager.h"
sammcaf1979e2014-11-25 01:01:0814#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
brettw00899e62016-11-12 02:10:1715#endif // BUILDFLAG(ENABLE_EXTENSIONS)
sammcaf1979e2014-11-25 01:01:0816
Brett Wilson65f951c2016-11-03 22:06:1217#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
sammc3d0df3a2014-11-24 21:25:3818#include "chrome/browser/printing/print_view_manager.h"
19#else
20#include "chrome/browser/printing/print_view_manager_basic.h"
Brett Wilson65f951c2016-11-03 22:06:1221#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
sammc3d0df3a2014-11-24 21:25:3822
23namespace printing {
thestigcb959ce2016-11-17 05:56:3224
sammcaf1979e2014-11-25 01:01:0825namespace {
brettw00899e62016-11-12 02:10:1726#if BUILDFLAG(ENABLE_EXTENSIONS)
sammcaf1979e2014-11-25 01:01:0827// Stores |guest_contents| in |result| and returns true if |guest_contents| is a
28// full page MimeHandlerViewGuest plugin. Otherwise, returns false.
29bool StoreFullPagePlugin(content::WebContents** result,
30 content::WebContents* guest_contents) {
vmpstr19df8252016-07-29 19:17:4831 auto* guest_view =
sammcaf1979e2014-11-25 01:01:0832 extensions::MimeHandlerViewGuest::FromWebContents(guest_contents);
33 if (guest_view && guest_view->is_full_page_plugin()) {
34 *result = guest_contents;
35 return true;
36 }
37 return false;
38}
brettw00899e62016-11-12 02:10:1739#endif // BUILDFLAG(ENABLE_EXTENSIONS)
sammcaf1979e2014-11-25 01:01:0840
thestigcb959ce2016-11-17 05:56:3241// Pick the right RenderFrameHost based on the WebContentses.
42content::RenderFrameHost* GetRenderFrameHostToUse(
43 content::WebContents* original_contents,
44 content::WebContents* contents_to_use) {
45 if (original_contents != contents_to_use)
46 return contents_to_use->GetMainFrame();
47 return GetFrameToPrint(contents_to_use);
48}
49
sammcaf1979e2014-11-25 01:01:0850} // namespace
sammc3d0df3a2014-11-24 21:25:3851
52void StartPrint(content::WebContents* contents,
53 bool print_preview_disabled,
thestigcb959ce2016-11-17 05:56:3254 bool has_selection) {
Brett Wilson65f951c2016-11-03 22:06:1255#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
sammc3d0df3a2014-11-24 21:25:3856 using PrintViewManagerImpl = PrintViewManager;
57#else
58 using PrintViewManagerImpl = PrintViewManagerBasic;
Brett Wilson65f951c2016-11-03 22:06:1259#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
sammc3d0df3a2014-11-24 21:25:3860
thestigcb959ce2016-11-17 05:56:3261 content::WebContents* contents_to_use = GetWebContentsToUse(contents);
vmpstr19df8252016-07-29 19:17:4862 auto* print_view_manager =
thestigcb959ce2016-11-17 05:56:3263 PrintViewManagerImpl::FromWebContents(contents_to_use);
sammc3d0df3a2014-11-24 21:25:3864 if (!print_view_manager)
65 return;
thestigcb959ce2016-11-17 05:56:3266
67 content::RenderFrameHost* rfh_to_use =
68 GetRenderFrameHostToUse(contents, contents_to_use);
69 if (!rfh_to_use)
70 return;
71
Brett Wilson65f951c2016-11-03 22:06:1272#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
sammc3d0df3a2014-11-24 21:25:3873 if (!print_preview_disabled) {
thestigcb959ce2016-11-17 05:56:3274 print_view_manager->PrintPreviewNow(rfh_to_use, has_selection);
sammc3d0df3a2014-11-24 21:25:3875 return;
76 }
77#endif // ENABLE_PRINT_PREVIEW
78
thestigcb959ce2016-11-17 05:56:3279 print_view_manager->PrintNow(rfh_to_use);
sammc3d0df3a2014-11-24 21:25:3880}
81
sammc3d0df3a2014-11-24 21:25:3882void StartBasicPrint(content::WebContents* contents) {
Brett Wilson65f951c2016-11-03 22:06:1283#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
thestigcb959ce2016-11-17 05:56:3284 content::WebContents* contents_to_use = GetWebContentsToUse(contents);
sammc3d0df3a2014-11-24 21:25:3885 PrintViewManager* print_view_manager =
thestigcb959ce2016-11-17 05:56:3286 PrintViewManager::FromWebContents(contents_to_use);
sammc3d0df3a2014-11-24 21:25:3887 if (!print_view_manager)
88 return;
thestigcb959ce2016-11-17 05:56:3289
90 content::RenderFrameHost* rfh_to_use =
91 GetRenderFrameHostToUse(contents, contents_to_use);
92 if (!rfh_to_use)
93 return;
94
95 print_view_manager->BasicPrint(rfh_to_use);
sammc3d0df3a2014-11-24 21:25:3896#endif // ENABLE_PRINT_PREVIEW
97}
sammc3d0df3a2014-11-24 21:25:3898
thestigcb959ce2016-11-17 05:56:3299content::RenderFrameHost* GetFrameToPrint(content::WebContents* contents) {
100 auto* focused_frame = contents->GetFocusedFrame();
101 return (focused_frame && focused_frame->HasSelection())
102 ? focused_frame
103 : contents->GetMainFrame();
104}
105
Jesse Schettler1cf12be2019-08-15 21:28:11106content::WebContents* GetWebContentsToUse(content::WebContents* contents) {
107#if BUILDFLAG(ENABLE_EXTENSIONS)
108 guest_view::GuestViewManager* guest_view_manager =
109 guest_view::GuestViewManager::FromBrowserContext(
110 contents->GetBrowserContext());
111 if (guest_view_manager) {
112 guest_view_manager->ForEachGuest(
113 contents, base::BindRepeating(&StoreFullPagePlugin, &contents));
114 }
115#endif // BUILDFLAG(ENABLE_EXTENSIONS)
116 return contents;
117}
118
sammc3d0df3a2014-11-24 21:25:38119} // namespace printing