sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 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 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 7 | #include "base/bind.h" |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 8 | #include "content/public/browser/render_frame_host.h" |
Scott Violet | c8240b0 | 2018-03-08 22:03:59 | [diff] [blame] | 9 | #include "extensions/buildflags/buildflags.h" |
Scott Violet | 318a55f | 2018-03-30 19:08:19 | [diff] [blame] | 10 | #include "printing/buildflags/buildflags.h" |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 11 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 12 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fsamuel | 8dfa19a | 2015-05-05 01:00:39 | [diff] [blame] | 13 | #include "components/guest_view/browser/guest_view_manager.h" |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 14 | #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 15 | #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 16 | |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 17 | #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 18 | #include "chrome/browser/printing/print_view_manager.h" |
| 19 | #else |
| 20 | #include "chrome/browser/printing/print_view_manager_basic.h" |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 21 | #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 22 | |
| 23 | namespace printing { |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 24 | |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 25 | namespace { |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 26 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 27 | // Stores |guest_contents| in |result| and returns true if |guest_contents| is a |
| 28 | // full page MimeHandlerViewGuest plugin. Otherwise, returns false. |
| 29 | bool StoreFullPagePlugin(content::WebContents** result, |
| 30 | content::WebContents* guest_contents) { |
vmpstr | 19df825 | 2016-07-29 19:17:48 | [diff] [blame] | 31 | auto* guest_view = |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 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 | } |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 39 | #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 40 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 41 | // Pick the right RenderFrameHost based on the WebContentses. |
| 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 | |
sammc | af1979e | 2014-11-25 01:01:08 | [diff] [blame] | 50 | } // namespace |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 51 | |
| 52 | void StartPrint(content::WebContents* contents, |
| 53 | bool print_preview_disabled, |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 54 | bool has_selection) { |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 55 | #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 56 | using PrintViewManagerImpl = PrintViewManager; |
| 57 | #else |
| 58 | using PrintViewManagerImpl = PrintViewManagerBasic; |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 59 | #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 60 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 61 | content::WebContents* contents_to_use = GetWebContentsToUse(contents); |
vmpstr | 19df825 | 2016-07-29 19:17:48 | [diff] [blame] | 62 | auto* print_view_manager = |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 63 | PrintViewManagerImpl::FromWebContents(contents_to_use); |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 64 | if (!print_view_manager) |
| 65 | return; |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 66 | |
| 67 | content::RenderFrameHost* rfh_to_use = |
| 68 | GetRenderFrameHostToUse(contents, contents_to_use); |
| 69 | if (!rfh_to_use) |
| 70 | return; |
| 71 | |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 72 | #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 73 | if (!print_preview_disabled) { |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 74 | print_view_manager->PrintPreviewNow(rfh_to_use, has_selection); |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | #endif // ENABLE_PRINT_PREVIEW |
| 78 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 79 | print_view_manager->PrintNow(rfh_to_use); |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 80 | } |
| 81 | |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 82 | void StartBasicPrint(content::WebContents* contents) { |
Brett Wilson | 65f951c | 2016-11-03 22:06:12 | [diff] [blame] | 83 | #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 84 | content::WebContents* contents_to_use = GetWebContentsToUse(contents); |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 85 | PrintViewManager* print_view_manager = |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 86 | PrintViewManager::FromWebContents(contents_to_use); |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 87 | if (!print_view_manager) |
| 88 | return; |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 89 | |
| 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); |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 96 | #endif // ENABLE_PRINT_PREVIEW |
| 97 | } |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 98 | |
thestig | cb959ce | 2016-11-17 05:56:32 | [diff] [blame] | 99 | content::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 Schettler | 1cf12be | 2019-08-15 21:28:11 | [diff] [blame] | 106 | content::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 | |
sammc | 3d0df3a | 2014-11-24 21:25:38 | [diff] [blame] | 119 | } // namespace printing |