1 // 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 
7 #include "base/bind.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "extensions/buildflags/buildflags.h"
10 #include "printing/buildflags/buildflags.h"
11 
12 #if BUILDFLAG(ENABLE_EXTENSIONS)
13 #include "components/guest_view/browser/guest_view_manager.h"
14 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
15 #endif  // BUILDFLAG(ENABLE_EXTENSIONS)
16 
17 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
18 #include "chrome/browser/printing/print_view_manager.h"
19 #else
20 #include "chrome/browser/printing/print_view_manager_basic.h"
21 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
22 
23 namespace printing {
24 
25 namespace {
26 #if BUILDFLAG(ENABLE_EXTENSIONS)
27 // Stores |guest_contents| in |result| and returns true if |guest_contents| is a
28 // full page MimeHandlerViewGuest plugin. Otherwise, returns false.
StoreFullPagePlugin(content::WebContents ** result,content::WebContents * guest_contents)29 bool StoreFullPagePlugin(content::WebContents** result,
30                          content::WebContents* guest_contents) {
31   auto* guest_view =
32       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 }
39 #endif  // BUILDFLAG(ENABLE_EXTENSIONS)
40 
41 // Pick the right RenderFrameHost based on the WebContentses.
GetRenderFrameHostToUse(content::WebContents * original_contents,content::WebContents * contents_to_use)42 content::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 
50 }  // namespace
51 
StartPrint(content::WebContents * contents,mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer,bool print_preview_disabled,bool has_selection)52 void StartPrint(
53     content::WebContents* contents,
54     mojo::PendingAssociatedRemote<mojom::PrintRenderer> print_renderer,
55     bool print_preview_disabled,
56     bool has_selection) {
57 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
58   using PrintViewManagerImpl = PrintViewManager;
59 #else
60   using PrintViewManagerImpl = PrintViewManagerBasic;
61 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
62 
63   content::WebContents* contents_to_use = GetWebContentsToUse(contents);
64   auto* print_view_manager =
65       PrintViewManagerImpl::FromWebContents(contents_to_use);
66   if (!print_view_manager)
67     return;
68 
69   content::RenderFrameHost* rfh_to_use =
70       GetRenderFrameHostToUse(contents, contents_to_use);
71   if (!rfh_to_use)
72     return;
73 
74 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
75   if (!print_preview_disabled) {
76     if (print_renderer) {
77       print_view_manager->PrintPreviewWithPrintRenderer(
78           rfh_to_use, std::move(print_renderer));
79     } else {
80       print_view_manager->PrintPreviewNow(rfh_to_use, has_selection);
81     }
82     return;
83   }
84 #endif  // ENABLE_PRINT_PREVIEW
85 
86   print_view_manager->PrintNow(rfh_to_use);
87 }
88 
StartBasicPrint(content::WebContents * contents)89 void StartBasicPrint(content::WebContents* contents) {
90 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
91   content::WebContents* contents_to_use = GetWebContentsToUse(contents);
92   PrintViewManager* print_view_manager =
93       PrintViewManager::FromWebContents(contents_to_use);
94   if (!print_view_manager)
95     return;
96 
97   content::RenderFrameHost* rfh_to_use =
98       GetRenderFrameHostToUse(contents, contents_to_use);
99   if (!rfh_to_use)
100     return;
101 
102   print_view_manager->BasicPrint(rfh_to_use);
103 #endif  // ENABLE_PRINT_PREVIEW
104 }
105 
GetFrameToPrint(content::WebContents * contents)106 content::RenderFrameHost* GetFrameToPrint(content::WebContents* contents) {
107   auto* focused_frame = contents->GetFocusedFrame();
108   return (focused_frame && focused_frame->HasSelection())
109              ? focused_frame
110              : contents->GetMainFrame();
111 }
112 
GetWebContentsToUse(content::WebContents * contents)113 content::WebContents* GetWebContentsToUse(content::WebContents* contents) {
114 #if BUILDFLAG(ENABLE_EXTENSIONS)
115   guest_view::GuestViewManager* guest_view_manager =
116       guest_view::GuestViewManager::FromBrowserContext(
117           contents->GetBrowserContext());
118   if (guest_view_manager) {
119     guest_view_manager->ForEachGuest(
120         contents, base::BindRepeating(&StoreFullPagePlugin, &contents));
121   }
122 #endif  // BUILDFLAG(ENABLE_EXTENSIONS)
123   return contents;
124 }
125 
126 }  // namespace printing
127