1 // Copyright 2013 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.
5 #include "content/browser/frame_host/navigation_controller_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "content/browser/frame_host/navigation_entry_impl.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/ssl_host_state_delegate.h"
14 #include "jni/NavigationControllerImpl_jni.h"
15 #include "ui/gfx/android/java_bitmap.h"
17 using base::android::AttachCurrentThread
;
18 using base::android::ConvertJavaStringToUTF16
;
19 using base::android::ConvertJavaStringToUTF8
;
20 using base::android::ConvertUTF16ToJavaString
;
21 using base::android::ConvertUTF8ToJavaString
;
25 static void AddNavigationEntryToHistory(JNIEnv
* env
,
28 content::NavigationEntry
* entry
,
30 // Get the details of the current entry
31 ScopedJavaLocalRef
<jstring
> j_url(
32 ConvertUTF8ToJavaString(env
, entry
->GetURL().spec()));
33 ScopedJavaLocalRef
<jstring
> j_virtual_url(
34 ConvertUTF8ToJavaString(env
, entry
->GetVirtualURL().spec()));
35 ScopedJavaLocalRef
<jstring
> j_original_url(
36 ConvertUTF8ToJavaString(env
, entry
->GetOriginalRequestURL().spec()));
37 ScopedJavaLocalRef
<jstring
> j_title(
38 ConvertUTF16ToJavaString(env
, entry
->GetTitle()));
39 ScopedJavaLocalRef
<jobject
> j_bitmap
;
40 const content::FaviconStatus
& status
= entry
->GetFavicon();
41 if (status
.valid
&& status
.image
.ToSkBitmap()->getSize() > 0)
42 j_bitmap
= gfx::ConvertToJavaBitmap(status
.image
.ToSkBitmap());
44 // Add the item to the list
45 content::Java_NavigationControllerImpl_addToNavigationHistory(
62 bool NavigationControllerAndroid::Register(JNIEnv
* env
) {
63 return RegisterNativesImpl(env
);
66 NavigationControllerAndroid::NavigationControllerAndroid(
67 NavigationController
* navigation_controller
)
68 : navigation_controller_(navigation_controller
) {
69 JNIEnv
* env
= AttachCurrentThread();
71 Java_NavigationControllerImpl_create(
72 env
, reinterpret_cast<intptr_t>(this)).obj());
75 NavigationControllerAndroid::~NavigationControllerAndroid() {
76 Java_NavigationControllerImpl_destroy(AttachCurrentThread(), obj_
.obj());
79 base::android::ScopedJavaLocalRef
<jobject
>
80 NavigationControllerAndroid::GetJavaObject() {
81 return base::android::ScopedJavaLocalRef
<jobject
>(obj_
);
84 jboolean
NavigationControllerAndroid::CanGoBack(JNIEnv
* env
, jobject obj
) {
85 return navigation_controller_
->CanGoBack();
88 jboolean
NavigationControllerAndroid::CanGoForward(JNIEnv
* env
,
90 return navigation_controller_
->CanGoForward();
93 jboolean
NavigationControllerAndroid::CanGoToOffset(JNIEnv
* env
,
96 return navigation_controller_
->CanGoToOffset(offset
);
99 void NavigationControllerAndroid::GoBack(JNIEnv
* env
, jobject obj
) {
100 navigation_controller_
->GoBack();
103 void NavigationControllerAndroid::GoForward(JNIEnv
* env
, jobject obj
) {
104 navigation_controller_
->GoForward();
107 void NavigationControllerAndroid::GoToOffset(JNIEnv
* env
,
110 navigation_controller_
->GoToOffset(offset
);
113 void NavigationControllerAndroid::LoadIfNecessary(JNIEnv
* env
, jobject obj
) {
114 navigation_controller_
->LoadIfNecessary();
117 void NavigationControllerAndroid::ContinuePendingReload(JNIEnv
* env
,
119 navigation_controller_
->ContinuePendingReload();
122 void NavigationControllerAndroid::Reload(JNIEnv
* env
,
124 jboolean check_for_repost
) {
125 navigation_controller_
->Reload(check_for_repost
);
128 void NavigationControllerAndroid::ReloadIgnoringCache(
131 jboolean check_for_repost
) {
132 navigation_controller_
->ReloadIgnoringCache(check_for_repost
);
135 void NavigationControllerAndroid::RequestRestoreLoad(JNIEnv
* env
, jobject obj
) {
136 navigation_controller_
->SetNeedsReload();
139 void NavigationControllerAndroid::CancelPendingReload(JNIEnv
* env
,
141 navigation_controller_
->CancelPendingReload();
144 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv
* env
,
147 navigation_controller_
->GoToIndex(index
);
150 void NavigationControllerAndroid::LoadUrl(JNIEnv
* env
,
154 jint transition_type
,
155 jstring j_referrer_url
,
156 jint referrer_policy
,
157 jint ua_override_option
,
158 jstring extra_headers
,
159 jbyteArray post_data
,
160 jstring base_url_for_data_url
,
161 jstring virtual_url_for_data_url
,
162 jboolean can_load_local_resources
,
163 jboolean is_renderer_initiated
) {
165 NavigationController::LoadURLParams
params(
166 GURL(ConvertJavaStringToUTF8(env
, url
)));
169 static_cast<NavigationController::LoadURLType
>(load_url_type
);
170 params
.transition_type
= PageTransitionFromInt(transition_type
);
171 params
.override_user_agent
=
172 static_cast<NavigationController::UserAgentOverrideOption
>(
174 params
.can_load_local_resources
= can_load_local_resources
;
175 params
.is_renderer_initiated
= is_renderer_initiated
;
178 params
.extra_headers
= ConvertJavaStringToUTF8(env
, extra_headers
);
181 std::vector
<uint8
> http_body_vector
;
182 base::android::JavaByteArrayToByteVector(env
, post_data
, &http_body_vector
);
183 params
.browser_initiated_post_data
=
184 base::RefCountedBytes::TakeVector(&http_body_vector
);
187 if (base_url_for_data_url
) {
188 params
.base_url_for_data_url
=
189 GURL(ConvertJavaStringToUTF8(env
, base_url_for_data_url
));
192 if (virtual_url_for_data_url
) {
193 params
.virtual_url_for_data_url
=
194 GURL(ConvertJavaStringToUTF8(env
, virtual_url_for_data_url
));
197 if (j_referrer_url
) {
198 params
.referrer
= content::Referrer(
199 GURL(ConvertJavaStringToUTF8(env
, j_referrer_url
)),
200 static_cast<blink::WebReferrerPolicy
>(referrer_policy
));
203 navigation_controller_
->LoadURLWithParams(params
);
206 void NavigationControllerAndroid::ClearHistory(JNIEnv
* env
, jobject obj
) {
207 // TODO(creis): Do callers of this need to know if it fails?
208 if (navigation_controller_
->CanPruneAllButLastCommitted())
209 navigation_controller_
->PruneAllButLastCommitted();
212 jint
NavigationControllerAndroid::GetNavigationHistory(JNIEnv
* env
,
215 // Iterate through navigation entries to populate the list
216 int count
= navigation_controller_
->GetEntryCount();
217 for (int i
= 0; i
< count
; ++i
) {
218 AddNavigationEntryToHistory(
219 env
, obj
, history
, navigation_controller_
->GetEntryAtIndex(i
), i
);
222 return navigation_controller_
->GetCurrentEntryIndex();
225 void NavigationControllerAndroid::GetDirectedNavigationHistory(
231 // Iterate through navigation entries to populate the list
232 int count
= navigation_controller_
->GetEntryCount();
234 int increment_value
= is_forward
? 1 : -1;
235 for (int i
= navigation_controller_
->GetCurrentEntryIndex() + increment_value
;
237 i
+= increment_value
) {
238 if (num_added
>= max_entries
)
241 AddNavigationEntryToHistory(
242 env
, obj
, history
, navigation_controller_
->GetEntryAtIndex(i
), i
);
247 ScopedJavaLocalRef
<jstring
>
248 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry(
251 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
253 return ScopedJavaLocalRef
<jstring
>(env
, NULL
);
254 return ConvertUTF8ToJavaString(env
, entry
->GetOriginalRequestURL().spec());
257 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv
* env
,
259 content::SSLHostStateDelegate
* delegate
=
260 navigation_controller_
->GetBrowserContext()->GetSSLHostStateDelegate();
265 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv
* env
,
267 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
268 return entry
&& entry
->GetIsOverridingUserAgent();
271 void NavigationControllerAndroid::SetUseDesktopUserAgent(
275 jboolean reload_on_state_change
) {
276 if (GetUseDesktopUserAgent(env
, obj
) == enabled
)
279 // Make sure the navigation entry actually exists.
280 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
284 // Set the flag in the NavigationEntry.
285 entry
->SetIsOverridingUserAgent(enabled
);
287 // Send the override to the renderer.
288 if (reload_on_state_change
) {
289 // Reloading the page will send the override down as part of the
290 // navigation IPC message.
291 navigation_controller_
->ReloadOriginalRequestURL(false);
295 } // namespace content