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" | |
8 | |
7 #if defined(ENABLE_EXTENSIONS) | 9 #if defined(ENABLE_EXTENSIONS) |
8 #include "components/guest_view/browser/guest_view_manager.h" | 10 #include "components/guest_view/browser/guest_view_manager.h" |
9 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues t.h" | 11 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues t.h" |
10 #endif // defined(ENABLE_EXTENSIONS) | 12 #endif // defined(ENABLE_EXTENSIONS) |
11 | 13 |
12 #if defined(ENABLE_PRINT_PREVIEW) | 14 #if defined(ENABLE_PRINT_PREVIEW) |
13 #include "chrome/browser/printing/print_view_manager.h" | 15 #include "chrome/browser/printing/print_view_manager.h" |
14 #else | 16 #else |
15 #include "chrome/browser/printing/print_view_manager_basic.h" | 17 #include "chrome/browser/printing/print_view_manager_basic.h" |
16 #endif // defined(ENABLE_PRINT_PREVIEW) | 18 #endif // defined(ENABLE_PRINT_PREVIEW) |
17 | 19 |
18 namespace printing { | 20 namespace printing { |
21 | |
19 namespace { | 22 namespace { |
20 #if defined(ENABLE_EXTENSIONS) | 23 #if defined(ENABLE_EXTENSIONS) |
21 // Stores |guest_contents| in |result| and returns true if |guest_contents| is a | 24 // Stores |guest_contents| in |result| and returns true if |guest_contents| is a |
22 // full page MimeHandlerViewGuest plugin. Otherwise, returns false. | 25 // full page MimeHandlerViewGuest plugin. Otherwise, returns false. |
23 bool StoreFullPagePlugin(content::WebContents** result, | 26 bool StoreFullPagePlugin(content::WebContents** result, |
24 content::WebContents* guest_contents) { | 27 content::WebContents* guest_contents) { |
25 auto* guest_view = | 28 auto* guest_view = |
26 extensions::MimeHandlerViewGuest::FromWebContents(guest_contents); | 29 extensions::MimeHandlerViewGuest::FromWebContents(guest_contents); |
27 if (guest_view && guest_view->is_full_page_plugin()) { | 30 if (guest_view && guest_view->is_full_page_plugin()) { |
28 *result = guest_contents; | 31 *result = guest_contents; |
(...skipping 12 matching lines...) Expand all Loading... | |
41 contents->GetBrowserContext()); | 44 contents->GetBrowserContext()); |
42 if (guest_view_manager) { | 45 if (guest_view_manager) { |
43 guest_view_manager->ForEachGuest( | 46 guest_view_manager->ForEachGuest( |
44 contents, | 47 contents, |
45 base::Bind(&StoreFullPagePlugin, &contents)); | 48 base::Bind(&StoreFullPagePlugin, &contents)); |
46 } | 49 } |
47 #endif // defined(ENABLE_EXTENSIONS) | 50 #endif // defined(ENABLE_EXTENSIONS) |
48 return contents; | 51 return contents; |
49 } | 52 } |
50 | 53 |
54 // Pick the right RenderFrameHost based on the WebContentses. | |
55 content::RenderFrameHost* GetRenderFrameHostToUse( | |
56 content::WebContents* original_contents, | |
57 content::WebContents* contents_to_use) { | |
58 if (original_contents != contents_to_use) | |
59 return contents_to_use->GetMainFrame(); | |
60 return GetFrameToPrint(contents_to_use); | |
61 } | |
62 | |
51 } // namespace | 63 } // namespace |
52 | 64 |
53 void StartPrint(content::WebContents* contents, | 65 void StartPrint(content::WebContents* contents, |
54 bool print_preview_disabled, | 66 bool print_preview_disabled, |
55 bool selection_only) { | 67 bool has_selection) { |
56 #if defined(ENABLE_PRINT_PREVIEW) | 68 #if defined(ENABLE_PRINT_PREVIEW) |
57 using PrintViewManagerImpl = PrintViewManager; | 69 using PrintViewManagerImpl = PrintViewManager; |
58 #else | 70 #else |
59 using PrintViewManagerImpl = PrintViewManagerBasic; | 71 using PrintViewManagerImpl = PrintViewManagerBasic; |
60 #endif // defined(ENABLE_PRINT_PREVIEW) | 72 #endif // defined(ENABLE_PRINT_PREVIEW) |
61 | 73 |
74 content::WebContents* contents_to_use = GetWebContentsToUse(contents); | |
62 auto* print_view_manager = | 75 auto* print_view_manager = |
63 PrintViewManagerImpl::FromWebContents(GetWebContentsToUse(contents)); | 76 PrintViewManagerImpl::FromWebContents(contents_to_use); |
64 if (!print_view_manager) | 77 if (!print_view_manager) |
65 return; | 78 return; |
79 | |
80 content::RenderFrameHost* rfh_to_use = | |
81 GetRenderFrameHostToUse(contents, contents_to_use); | |
82 if (!rfh_to_use) | |
83 return; | |
84 | |
66 #if defined(ENABLE_PRINT_PREVIEW) | 85 #if defined(ENABLE_PRINT_PREVIEW) |
67 if (!print_preview_disabled) { | 86 if (!print_preview_disabled) { |
68 print_view_manager->PrintPreviewNow(selection_only); | 87 print_view_manager->PrintPreviewNow(rfh_to_use, has_selection); |
69 return; | 88 return; |
70 } | 89 } |
71 #endif // ENABLE_PRINT_PREVIEW | 90 #endif // ENABLE_PRINT_PREVIEW |
72 | 91 |
73 #if defined(ENABLE_BASIC_PRINTING) | 92 #if defined(ENABLE_BASIC_PRINTING) |
74 print_view_manager->PrintNow(); | 93 print_view_manager->PrintNow(rfh_to_use); |
75 #endif // ENABLE_BASIC_PRINTING | 94 #endif // ENABLE_BASIC_PRINTING |
76 } | 95 } |
77 | 96 |
78 #if defined(ENABLE_BASIC_PRINTING) | 97 #if defined(ENABLE_BASIC_PRINTING) |
79 void StartBasicPrint(content::WebContents* contents) { | 98 void StartBasicPrint(content::WebContents* contents) { |
80 #if defined(ENABLE_PRINT_PREVIEW) | 99 #if defined(ENABLE_PRINT_PREVIEW) |
100 content::WebContents* contents_to_use = GetWebContentsToUse(contents); | |
81 PrintViewManager* print_view_manager = | 101 PrintViewManager* print_view_manager = |
82 PrintViewManager::FromWebContents(GetWebContentsToUse(contents)); | 102 PrintViewManager::FromWebContents(contents_to_use); |
83 if (!print_view_manager) | 103 if (!print_view_manager) |
84 return; | 104 return; |
85 print_view_manager->BasicPrint(); | 105 |
106 content::RenderFrameHost* rfh_to_use = | |
107 GetRenderFrameHostToUse(contents, contents_to_use); | |
108 if (!rfh_to_use) | |
109 return; | |
110 | |
111 print_view_manager->BasicPrint(rfh_to_use); | |
86 #endif // ENABLE_PRINT_PREVIEW | 112 #endif // ENABLE_PRINT_PREVIEW |
87 } | 113 } |
88 #endif // ENABLE_BASIC_PRINTING | 114 #endif // ENABLE_BASIC_PRINTING |
89 | 115 |
116 content::RenderFrameHost* GetFrameToPrint(content::WebContents* contents) { | |
117 auto* focused_frame = contents->GetFocusedFrame(); | |
118 return (focused_frame && focused_frame->HasSelection()) | |
119 ? focused_frame | |
120 : contents->GetMainFrame(); | |
nasko
2016/11/02 04:50:37
Would this work correctly if window.print() is cal
Lei Zhang
2016/11/08 11:13:22
You discussed this in an earlier comment. Added a
Lei Zhang
2016/11/12 00:03:19
window.print() doesn't go through this path. This
| |
121 } | |
122 | |
90 } // namespace printing | 123 } // namespace printing |
OLD | NEW |