| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/print_view_manager_common.h" | 5 #include "chrome/browser/printing/print_view_manager_common.h" |
| 6 | 6 |
| 7 #include "content/public/browser/render_frame_host.h" |
| 7 #include "printing/features/features.h" | 8 #include "printing/features/features.h" |
| 8 | 9 |
| 9 #if defined(ENABLE_EXTENSIONS) | 10 #if defined(ENABLE_EXTENSIONS) |
| 10 #include "components/guest_view/browser/guest_view_manager.h" | 11 #include "components/guest_view/browser/guest_view_manager.h" |
| 11 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues
t.h" | 12 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues
t.h" |
| 12 #endif // defined(ENABLE_EXTENSIONS) | 13 #endif // defined(ENABLE_EXTENSIONS) |
| 13 | 14 |
| 14 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 15 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 15 #include "chrome/browser/printing/print_view_manager.h" | 16 #include "chrome/browser/printing/print_view_manager.h" |
| 16 #else | 17 #else |
| 17 #include "chrome/browser/printing/print_view_manager_basic.h" | 18 #include "chrome/browser/printing/print_view_manager_basic.h" |
| 18 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 19 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 19 | 20 |
| 20 namespace printing { | 21 namespace printing { |
| 22 |
| 21 namespace { | 23 namespace { |
| 22 #if defined(ENABLE_EXTENSIONS) | 24 #if defined(ENABLE_EXTENSIONS) |
| 23 // Stores |guest_contents| in |result| and returns true if |guest_contents| is a | 25 // Stores |guest_contents| in |result| and returns true if |guest_contents| is a |
| 24 // full page MimeHandlerViewGuest plugin. Otherwise, returns false. | 26 // full page MimeHandlerViewGuest plugin. Otherwise, returns false. |
| 25 bool StoreFullPagePlugin(content::WebContents** result, | 27 bool StoreFullPagePlugin(content::WebContents** result, |
| 26 content::WebContents* guest_contents) { | 28 content::WebContents* guest_contents) { |
| 27 auto* guest_view = | 29 auto* guest_view = |
| 28 extensions::MimeHandlerViewGuest::FromWebContents(guest_contents); | 30 extensions::MimeHandlerViewGuest::FromWebContents(guest_contents); |
| 29 if (guest_view && guest_view->is_full_page_plugin()) { | 31 if (guest_view && guest_view->is_full_page_plugin()) { |
| 30 *result = guest_contents; | 32 *result = guest_contents; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 43 contents->GetBrowserContext()); | 45 contents->GetBrowserContext()); |
| 44 if (guest_view_manager) { | 46 if (guest_view_manager) { |
| 45 guest_view_manager->ForEachGuest( | 47 guest_view_manager->ForEachGuest( |
| 46 contents, | 48 contents, |
| 47 base::Bind(&StoreFullPagePlugin, &contents)); | 49 base::Bind(&StoreFullPagePlugin, &contents)); |
| 48 } | 50 } |
| 49 #endif // defined(ENABLE_EXTENSIONS) | 51 #endif // defined(ENABLE_EXTENSIONS) |
| 50 return contents; | 52 return contents; |
| 51 } | 53 } |
| 52 | 54 |
| 55 // Pick the right RenderFrameHost based on the WebContentses. |
| 56 content::RenderFrameHost* GetRenderFrameHostToUse( |
| 57 content::WebContents* original_contents, |
| 58 content::WebContents* contents_to_use) { |
| 59 if (original_contents != contents_to_use) |
| 60 return contents_to_use->GetMainFrame(); |
| 61 return GetFrameToPrint(contents_to_use); |
| 62 } |
| 63 |
| 53 } // namespace | 64 } // namespace |
| 54 | 65 |
| 55 void StartPrint(content::WebContents* contents, | 66 void StartPrint(content::WebContents* contents, |
| 56 bool print_preview_disabled, | 67 bool print_preview_disabled, |
| 57 bool selection_only) { | 68 bool has_selection) { |
| 58 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 69 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 59 using PrintViewManagerImpl = PrintViewManager; | 70 using PrintViewManagerImpl = PrintViewManager; |
| 60 #else | 71 #else |
| 61 using PrintViewManagerImpl = PrintViewManagerBasic; | 72 using PrintViewManagerImpl = PrintViewManagerBasic; |
| 62 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 73 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 63 | 74 |
| 75 content::WebContents* contents_to_use = GetWebContentsToUse(contents); |
| 64 auto* print_view_manager = | 76 auto* print_view_manager = |
| 65 PrintViewManagerImpl::FromWebContents(GetWebContentsToUse(contents)); | 77 PrintViewManagerImpl::FromWebContents(contents_to_use); |
| 66 if (!print_view_manager) | 78 if (!print_view_manager) |
| 67 return; | 79 return; |
| 80 |
| 81 content::RenderFrameHost* rfh_to_use = |
| 82 GetRenderFrameHostToUse(contents, contents_to_use); |
| 83 if (!rfh_to_use) |
| 84 return; |
| 85 |
| 68 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 86 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 69 if (!print_preview_disabled) { | 87 if (!print_preview_disabled) { |
| 70 print_view_manager->PrintPreviewNow(selection_only); | 88 print_view_manager->PrintPreviewNow(rfh_to_use, has_selection); |
| 71 return; | 89 return; |
| 72 } | 90 } |
| 73 #endif // ENABLE_PRINT_PREVIEW | 91 #endif // ENABLE_PRINT_PREVIEW |
| 74 | 92 |
| 75 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 93 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 76 print_view_manager->PrintNow(); | 94 print_view_manager->PrintNow(rfh_to_use); |
| 77 #endif // ENABLE_BASIC_PRINTING | 95 #endif // ENABLE_BASIC_PRINTING |
| 78 } | 96 } |
| 79 | 97 |
| 80 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 98 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 81 void StartBasicPrint(content::WebContents* contents) { | 99 void StartBasicPrint(content::WebContents* contents) { |
| 82 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 100 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 101 content::WebContents* contents_to_use = GetWebContentsToUse(contents); |
| 83 PrintViewManager* print_view_manager = | 102 PrintViewManager* print_view_manager = |
| 84 PrintViewManager::FromWebContents(GetWebContentsToUse(contents)); | 103 PrintViewManager::FromWebContents(contents_to_use); |
| 85 if (!print_view_manager) | 104 if (!print_view_manager) |
| 86 return; | 105 return; |
| 87 print_view_manager->BasicPrint(); | 106 |
| 107 content::RenderFrameHost* rfh_to_use = |
| 108 GetRenderFrameHostToUse(contents, contents_to_use); |
| 109 if (!rfh_to_use) |
| 110 return; |
| 111 |
| 112 print_view_manager->BasicPrint(rfh_to_use); |
| 88 #endif // ENABLE_PRINT_PREVIEW | 113 #endif // ENABLE_PRINT_PREVIEW |
| 89 } | 114 } |
| 90 #endif // ENABLE_BASIC_PRINTING | 115 #endif // ENABLE_BASIC_PRINTING |
| 91 | 116 |
| 117 content::RenderFrameHost* GetFrameToPrint(content::WebContents* contents) { |
| 118 // TODO(thestig): Address nasko's comments and check out the case when the |
| 119 // frame that printed is no the focuses frame. |
| 120 auto* focused_frame = contents->GetFocusedFrame(); |
| 121 return (focused_frame && focused_frame->HasSelection()) |
| 122 ? focused_frame |
| 123 : contents->GetMainFrame(); |
| 124 } |
| 125 |
| 92 } // namespace printing | 126 } // namespace printing |
| OLD | NEW |