blob: 3cffff66e5c8eedf35c61ad08235954f9368e6ec [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Arthur Sonzogni892217ca2019-01-17 10:27:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/android/navigation_handle_proxy.h"
6
arthursonzognid95ed0bc2019-02-20 10:36:527#include "base/android/jni_android.h"
8#include "base/android/jni_string.h"
9#include "base/android/scoped_java_ref.h"
Arthur Sonzogni892217ca2019-01-17 10:27:0810#include "content/public/browser/navigation_handle.h"
Michael Thiessen0db917762025-01-22 16:58:4611#include "content/public/browser/web_contents.h"
Min Qinec46b452024-08-13 17:45:5712#include "content/public/common/content_client.h"
David Sandersd0bc2c42022-02-20 15:50:2313#include "net/http/http_response_headers.h"
Michael Thiessene9bfb1a2020-09-22 22:59:2814#include "url/android/gurl_android.h"
15#include "url/gurl.h"
arthursonzognid95ed0bc2019-02-20 10:36:5216
Andrew Grieveecb885bb2024-05-29 18:14:1917// Must come after all headers that specialize FromJniType() / ToJniType().
18#include "content/public/android/content_jni_headers/NavigationHandle_jni.h"
19
arthursonzognid95ed0bc2019-02-20 10:36:5220using base::android::AttachCurrentThread;
arthursonzognid95ed0bc2019-02-20 10:36:5221using base::android::JavaParamRef;
Arthur Sonzogni892217ca2019-01-17 10:27:0822
23namespace content {
24
arthursonzognid95ed0bc2019-02-20 10:36:5225NavigationHandleProxy::NavigationHandleProxy(
26 NavigationHandle* cpp_navigation_handle)
27 : cpp_navigation_handle_(cpp_navigation_handle) {
28 JNIEnv* env = AttachCurrentThread();
Michael Thiessend9fc0662021-06-22 22:29:1829
Gang Wu0c3ad3c2024-01-26 00:39:0730 java_navigation_handle_ = Java_NavigationHandle_Constructor(
Peter E Conn4e4b9cc2025-05-02 08:54:2831 env, reinterpret_cast<jlong>(cpp_navigation_handle),
32 url::GURLAndroid::FromNativeGURL(env, cpp_navigation_handle_->GetURL()),
33 cpp_navigation_handle_->IsRendererInitiated(),
34 cpp_navigation_handle_->GetReloadType() != content::ReloadType::NONE,
35 cpp_navigation_handle_->IsHistory(),
36 cpp_navigation_handle_->IsHistory() &&
37 cpp_navigation_handle_->GetNavigationEntryOffset() < 0,
38 cpp_navigation_handle_->IsHistory() &&
39 cpp_navigation_handle_->GetNavigationEntryOffset() > 0,
40 cpp_navigation_handle_->GetRestoreType() ==
41 content::RestoreType::kRestored);
Pete Williamson1d87f4622022-10-18 19:36:4342}
43
44void NavigationHandleProxy::DidStart() {
45 JNIEnv* env = AttachCurrentThread();
46
47 // Set all these methods on the Java side over JNI with a new JNI method.
Michael Thiessen0db917762025-01-22 16:58:4648 Java_NavigationHandle_didStart(
49 env, java_navigation_handle_,
Michael Thiessen9fca5b92022-02-11 00:19:0850 url::GURLAndroid::FromNativeGURL(
51 env, cpp_navigation_handle_->GetReferrer().url),
Michael Thiessenca245a382022-02-21 16:11:1752 url::GURLAndroid::FromNativeGURL(
53 env, cpp_navigation_handle_->GetBaseURLForDataURL()),
Dave Tapuska421f9a32021-06-10 20:18:4354 cpp_navigation_handle_->IsInPrimaryMainFrame(),
arthursonzognid95ed0bc2019-02-20 10:36:5255 cpp_navigation_handle_->IsSameDocument(),
Michael Thiessend9fc0662021-06-22 22:29:1856 cpp_navigation_handle_->GetInitiatorOrigin()
Andrew Grieve6a37bcd2024-09-26 21:47:4457 ? cpp_navigation_handle_->GetInitiatorOrigin()->ToJavaObject(env)
Michael Thiessend9fc0662021-06-22 22:29:1858 : nullptr,
Andrew Paseltiner00f60aca2022-03-31 17:26:3159 cpp_navigation_handle_->GetPageTransition(),
Michael Thiessen9fca5b92022-02-11 00:19:0860 cpp_navigation_handle_->IsPost(),
61 cpp_navigation_handle_->HasUserGesture(),
62 cpp_navigation_handle_->WasServerRedirect(),
63 cpp_navigation_handle_->IsExternalProtocol(),
Michael Thiessen203b7afc2022-03-07 21:47:2064 cpp_navigation_handle_->GetNavigationId(),
Michael Thiessenf6fce172022-06-07 22:15:3365 cpp_navigation_handle_->IsPageActivation(),
Min Qin9d3dca5c2024-05-10 16:51:5166 cpp_navigation_handle_->IsPdf(),
Min Qin3d7b0d32024-05-22 06:57:4867 base::android::ConvertUTF8ToJavaString(env, GetMimeType()),
Michael Thiessen0db917762025-01-22 16:58:4668 cpp_navigation_handle_->GetWebContents()->GetJavaWebContents());
arthursonzognid95ed0bc2019-02-20 10:36:5269}
70
71void NavigationHandleProxy::DidRedirect() {
72 JNIEnv* env = AttachCurrentThread();
73 Java_NavigationHandle_didRedirect(
74 env, java_navigation_handle_,
Michael Thiessena81a10d2022-07-14 23:12:0175 url::GURLAndroid::FromNativeGURL(env, cpp_navigation_handle_->GetURL()),
76 cpp_navigation_handle_->IsExternalProtocol());
arthursonzognid95ed0bc2019-02-20 10:36:5277}
78
79void NavigationHandleProxy::DidFinish() {
80 JNIEnv* env = AttachCurrentThread();
81 // Matches logic in
82 // components/navigation_interception/navigation_params_android.cc
Michael Thiessene9bfb1a2020-09-22 22:59:2883 const GURL& gurl = cpp_navigation_handle_->GetBaseURLForDataURL().is_empty()
84 ? cpp_navigation_handle_->GetURL()
85 : cpp_navigation_handle_->GetBaseURLForDataURL();
arthursonzognid95ed0bc2019-02-20 10:36:5286
Kevin McNee2c06ed362022-06-02 19:39:4087 bool is_primary_main_frame_fragment_navigation =
88 cpp_navigation_handle_->IsInPrimaryMainFrame() &&
89 cpp_navigation_handle_->IsSameDocument();
arthursonzognid95ed0bc2019-02-20 10:36:5290
Kevin McNee2c06ed362022-06-02 19:39:4091 if (is_primary_main_frame_fragment_navigation &&
92 cpp_navigation_handle_->HasCommitted()) {
arthursonzognid95ed0bc2019-02-20 10:36:5293 // See http://crbug.com/251330 for why it's determined this way.
arthursonzognid95ed0bc2019-02-20 10:36:5294 bool urls_same_ignoring_fragment =
Etienne Noel8e790322022-11-08 19:19:1895 cpp_navigation_handle_->GetURL().EqualsIgnoringRef(
96 cpp_navigation_handle_->GetPreviousPrimaryMainFrameURL());
Kevin McNee2c06ed362022-06-02 19:39:4097 is_primary_main_frame_fragment_navigation = urls_same_ignoring_fragment;
arthursonzognid95ed0bc2019-02-20 10:36:5298 }
99
Wei-Yin Chen (陳威尹)4c5bf662019-03-01 03:38:57100 bool is_valid_search_form_url =
Jan Wilken Dörrief11b4c22020-02-07 23:34:29101 cpp_navigation_handle_->GetSearchableFormURL() != ""
Wei-Yin Chen (陳威尹)4c5bf662019-03-01 03:38:57102 ? cpp_navigation_handle_->GetSearchableFormURL().is_valid()
103 : false;
104
arthursonzognid95ed0bc2019-02-20 10:36:52105 Java_NavigationHandle_didFinish(
Michael Thiessene9bfb1a2020-09-22 22:59:28106 env, java_navigation_handle_, url::GURLAndroid::FromNativeGURL(env, gurl),
arthursonzognid95ed0bc2019-02-20 10:36:52107 cpp_navigation_handle_->IsErrorPage(),
Kevin McNee2c06ed362022-06-02 19:39:40108 cpp_navigation_handle_->HasCommitted(),
109 is_primary_main_frame_fragment_navigation,
Wei-Yin Chen (陳威尹)4c5bf662019-03-01 03:38:57110 cpp_navigation_handle_->IsDownload(), is_valid_search_form_url,
Michael Thiessen6e40296d2022-02-02 16:39:36111 cpp_navigation_handle_->GetPageTransition(),
arthursonzognid95ed0bc2019-02-20 10:36:52112 cpp_navigation_handle_->GetNetErrorCode(),
113 // TODO(shaktisahu): Change default status to -1 after fixing
114 // crbug/690041.
115 cpp_navigation_handle_->GetResponseHeaders()
116 ? cpp_navigation_handle_->GetResponseHeaders()->response_code()
Michael Thiessena81a10d2022-07-14 23:12:01117 : 200,
Shu Yang59ad37be2024-03-19 17:54:38118 cpp_navigation_handle_->IsExternalProtocol(),
Min Qin9d3dca5c2024-05-10 16:51:51119 cpp_navigation_handle_->IsPdf(),
Min Qin3d7b0d32024-05-22 06:57:48120 base::android::ConvertUTF8ToJavaString(env, GetMimeType()),
Rakina Zata Amni65a5dc42025-03-03 02:51:55121 cpp_navigation_handle_->GetWebContents()->GetPrimaryPage().GetJavaPage());
arthursonzognid95ed0bc2019-02-20 10:36:52122}
123
124NavigationHandleProxy::~NavigationHandleProxy() {
125 JNIEnv* env = AttachCurrentThread();
126 Java_NavigationHandle_release(env, java_navigation_handle_);
127}
128
Min Qin9d3dca5c2024-05-10 16:51:51129std::string NavigationHandleProxy::GetMimeType() const {
130 std::string mime_type;
131 if (cpp_navigation_handle_->GetResponseHeaders()) {
132 cpp_navigation_handle_->GetResponseHeaders()->GetMimeType(&mime_type);
133 }
134 return mime_type;
135}
136
Arthur Sonzogni892217ca2019-01-17 10:27:08137} // namespace content