| // Copyright 2019 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "base/check_op.h" | |
| #include "base/feature_list.h" | |
| #include "chrome/browser/flags/android/chrome_feature_list.h" | |
| #include "chrome/browser/printing/print_view_manager_basic.h" | |
| #include "chrome/browser/printing/print_view_manager_common.h" | |
| #include "content/public/browser/browser_thread.h" | |
| #include "content/public/browser/render_frame_host.h" | |
| #include "content/public/browser/web_contents.h" | |
| // Must come after all headers that specialize FromJniType() / ToJniType(). | |
| #include "chrome/android/chrome_jni_headers/WebContentsPrinter_jni.h" | |
| using base::android::ScopedJavaLocalRef; | |
| namespace printing { | |
| namespace { | |
| content::RenderFrameHost* GetTargetFrame(content::WebContents* web_contents, | |
| int32_t render_process_id, | |
| int32_t render_frame_id, | |
| bool print_selection_only) { | |
| content::RenderFrameHost* rfh = | |
| content::RenderFrameHost::FromID(render_process_id, render_frame_id); | |
| if (rfh) { | |
| // The caller is expected to pass IDs belonging to `web_contents`; a | |
| // mismatch means the browser-side bookkeeping is inconsistent. | |
| CHECK_EQ(content::WebContents::FromRenderFrameHost(rfh), web_contents); | |
| } | |
| // If the target frame is invalid, inactive, or no longer live: | |
| // - For selection printing, fail safely to avoid printing the whole page. | |
| // - For normal printing, fall back to the primary main frame instead of | |
| // GetFrameToPrint(), which targets a focused subframe when it has a | |
| // selection (e.g. hidden text-input iframes in web editors). | |
| if (!rfh || !rfh->IsActive() || !rfh->IsRenderFrameLive()) { | |
| if (print_selection_only) { | |
| return nullptr; | |
| } | |
| if (base::FeatureList::IsEnabled( | |
| chrome::android::kPrintFallbackToPrimaryMainFrame)) { | |
| rfh = web_contents->GetPrimaryMainFrame(); | |
| } else { | |
| rfh = GetFrameToPrint(web_contents); | |
| } | |
| } | |
| if (!rfh || !rfh->IsActive() || !rfh->IsRenderFrameLive()) { | |
| return nullptr; | |
| } | |
| return rfh; | |
| } | |
| } // namespace | |
| static bool JNI_WebContentsPrinter_InitiatePrint( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& jweb_contents, | |
| int32_t render_process_id, | |
| int32_t render_frame_id, | |
| bool print_selection_only) { | |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| content::WebContents* web_contents = | |
| content::WebContents::FromJavaWebContents(jweb_contents); | |
| if (!web_contents) { | |
| return false; | |
| } | |
| content::RenderFrameHost* rfh = GetTargetFrame( | |
| web_contents, render_process_id, render_frame_id, print_selection_only); | |
| if (!rfh) { | |
| return false; | |
| } | |
| PrintViewManagerBasic* print_view_manager = | |
| PrintViewManagerBasic::FromWebContents(web_contents); | |
| return print_view_manager && print_view_manager->InitiatePrint(rfh); | |
| } | |
| static bool JNI_WebContentsPrinter_Print( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& jweb_contents, | |
| int32_t render_process_id, | |
| int32_t render_frame_id, | |
| bool print_selection_only) { | |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| content::WebContents* web_contents = | |
| content::WebContents::FromJavaWebContents(jweb_contents); | |
| if (!web_contents) { | |
| return false; | |
| } | |
| content::RenderFrameHost* rfh = GetTargetFrame( | |
| web_contents, render_process_id, render_frame_id, print_selection_only); | |
| if (!rfh) { | |
| return false; | |
| } | |
| PrintViewManagerBasic* print_view_manager = | |
| PrintViewManagerBasic::FromWebContents(web_contents); | |
| return print_view_manager && | |
| print_view_manager->PrintNow(rfh, print_selection_only); | |
| } | |
| static void JNI_WebContentsPrinter_FinishPrint( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& jweb_contents, | |
| int32_t render_process_id, | |
| int32_t render_frame_id, | |
| bool print_selection_only) { | |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| content::WebContents* web_contents = | |
| content::WebContents::FromJavaWebContents(jweb_contents); | |
| if (!web_contents) { | |
| return; | |
| } | |
| content::RenderFrameHost* rfh = GetTargetFrame( | |
| web_contents, render_process_id, render_frame_id, print_selection_only); | |
| if (!rfh) { | |
| return; | |
| } | |
| PrintViewManagerBasic* print_view_manager = | |
| PrintViewManagerBasic::FromWebContents(web_contents); | |
| if (print_view_manager) { | |
| print_view_manager->FinishPrint(rfh); | |
| } | |
| } | |
| } // namespace printing | |
| DEFINE_JNI(WebContentsPrinter) |