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 base::android::ScopedJavaLocalRef
<jobject
> CreateJavaNavigationEntry(
27 content::NavigationEntry
* entry
,
31 // Get the details of the current entry
32 ScopedJavaLocalRef
<jstring
> j_url(
33 ConvertUTF8ToJavaString(env
, entry
->GetURL().spec()));
34 ScopedJavaLocalRef
<jstring
> j_virtual_url(
35 ConvertUTF8ToJavaString(env
, entry
->GetVirtualURL().spec()));
36 ScopedJavaLocalRef
<jstring
> j_original_url(
37 ConvertUTF8ToJavaString(env
, entry
->GetOriginalRequestURL().spec()));
38 ScopedJavaLocalRef
<jstring
> j_title(
39 ConvertUTF16ToJavaString(env
, entry
->GetTitle()));
40 ScopedJavaLocalRef
<jobject
> j_bitmap
;
41 const content::FaviconStatus
& status
= entry
->GetFavicon();
42 if (status
.valid
&& status
.image
.ToSkBitmap()->getSize() > 0)
43 j_bitmap
= gfx::ConvertToJavaBitmap(status
.image
.ToSkBitmap());
45 return content::Java_NavigationControllerImpl_createNavigationEntry(
55 static void AddNavigationEntryToHistory(JNIEnv
* env
,
57 content::NavigationEntry
* entry
,
59 content::Java_NavigationControllerImpl_addToNavigationHistory(
62 CreateJavaNavigationEntry(env
, entry
, index
).obj());
70 bool NavigationControllerAndroid::Register(JNIEnv
* env
) {
71 return RegisterNativesImpl(env
);
74 NavigationControllerAndroid::NavigationControllerAndroid(
75 NavigationController
* navigation_controller
)
76 : navigation_controller_(navigation_controller
) {
77 JNIEnv
* env
= AttachCurrentThread();
79 Java_NavigationControllerImpl_create(
80 env
, reinterpret_cast<intptr_t>(this)).obj());
83 NavigationControllerAndroid::~NavigationControllerAndroid() {
84 Java_NavigationControllerImpl_destroy(AttachCurrentThread(), obj_
.obj());
87 base::android::ScopedJavaLocalRef
<jobject
>
88 NavigationControllerAndroid::GetJavaObject() {
89 return base::android::ScopedJavaLocalRef
<jobject
>(obj_
);
92 jboolean
NavigationControllerAndroid::CanGoBack(JNIEnv
* env
, jobject obj
) {
93 return navigation_controller_
->CanGoBack();
96 jboolean
NavigationControllerAndroid::CanGoForward(JNIEnv
* env
,
98 return navigation_controller_
->CanGoForward();
101 jboolean
NavigationControllerAndroid::CanGoToOffset(JNIEnv
* env
,
104 return navigation_controller_
->CanGoToOffset(offset
);
107 void NavigationControllerAndroid::GoBack(JNIEnv
* env
, jobject obj
) {
108 navigation_controller_
->GoBack();
111 void NavigationControllerAndroid::GoForward(JNIEnv
* env
, jobject obj
) {
112 navigation_controller_
->GoForward();
115 void NavigationControllerAndroid::GoToOffset(JNIEnv
* env
,
118 navigation_controller_
->GoToOffset(offset
);
121 jboolean
NavigationControllerAndroid::IsInitialNavigation(JNIEnv
* env
,
123 return navigation_controller_
->IsInitialNavigation();
126 void NavigationControllerAndroid::LoadIfNecessary(JNIEnv
* env
, jobject obj
) {
127 navigation_controller_
->LoadIfNecessary();
130 void NavigationControllerAndroid::ContinuePendingReload(JNIEnv
* env
,
132 navigation_controller_
->ContinuePendingReload();
135 void NavigationControllerAndroid::Reload(JNIEnv
* env
,
137 jboolean check_for_repost
) {
138 navigation_controller_
->Reload(check_for_repost
);
141 void NavigationControllerAndroid::ReloadIgnoringCache(
144 jboolean check_for_repost
) {
145 navigation_controller_
->ReloadIgnoringCache(check_for_repost
);
148 void NavigationControllerAndroid::RequestRestoreLoad(JNIEnv
* env
, jobject obj
) {
149 navigation_controller_
->SetNeedsReload();
152 void NavigationControllerAndroid::CancelPendingReload(JNIEnv
* env
,
154 navigation_controller_
->CancelPendingReload();
157 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv
* env
,
160 navigation_controller_
->GoToIndex(index
);
163 void NavigationControllerAndroid::LoadUrl(JNIEnv
* env
,
167 jint transition_type
,
168 jstring j_referrer_url
,
169 jint referrer_policy
,
170 jint ua_override_option
,
171 jstring extra_headers
,
172 jbyteArray post_data
,
173 jstring base_url_for_data_url
,
174 jstring virtual_url_for_data_url
,
175 jboolean can_load_local_resources
,
176 jboolean is_renderer_initiated
) {
178 NavigationController::LoadURLParams
params(
179 GURL(ConvertJavaStringToUTF8(env
, url
)));
182 static_cast<NavigationController::LoadURLType
>(load_url_type
);
183 params
.transition_type
= ui::PageTransitionFromInt(transition_type
);
184 params
.override_user_agent
=
185 static_cast<NavigationController::UserAgentOverrideOption
>(
187 params
.can_load_local_resources
= can_load_local_resources
;
188 params
.is_renderer_initiated
= is_renderer_initiated
;
191 params
.extra_headers
= ConvertJavaStringToUTF8(env
, extra_headers
);
194 std::vector
<uint8
> http_body_vector
;
195 base::android::JavaByteArrayToByteVector(env
, post_data
, &http_body_vector
);
196 params
.browser_initiated_post_data
=
197 base::RefCountedBytes::TakeVector(&http_body_vector
);
200 if (base_url_for_data_url
) {
201 params
.base_url_for_data_url
=
202 GURL(ConvertJavaStringToUTF8(env
, base_url_for_data_url
));
205 if (virtual_url_for_data_url
) {
206 params
.virtual_url_for_data_url
=
207 GURL(ConvertJavaStringToUTF8(env
, virtual_url_for_data_url
));
210 if (j_referrer_url
) {
211 params
.referrer
= content::Referrer(
212 GURL(ConvertJavaStringToUTF8(env
, j_referrer_url
)),
213 static_cast<blink::WebReferrerPolicy
>(referrer_policy
));
216 navigation_controller_
->LoadURLWithParams(params
);
219 void NavigationControllerAndroid::ClearHistory(JNIEnv
* env
, jobject obj
) {
220 // TODO(creis): Do callers of this need to know if it fails?
221 if (navigation_controller_
->CanPruneAllButLastCommitted())
222 navigation_controller_
->PruneAllButLastCommitted();
225 jint
NavigationControllerAndroid::GetNavigationHistory(JNIEnv
* env
,
228 // Iterate through navigation entries to populate the list
229 int count
= navigation_controller_
->GetEntryCount();
230 for (int i
= 0; i
< count
; ++i
) {
231 AddNavigationEntryToHistory(
232 env
, history
, navigation_controller_
->GetEntryAtIndex(i
), i
);
235 return navigation_controller_
->GetCurrentEntryIndex();
238 void NavigationControllerAndroid::GetDirectedNavigationHistory(
244 // Iterate through navigation entries to populate the list
245 int count
= navigation_controller_
->GetEntryCount();
247 int increment_value
= is_forward
? 1 : -1;
248 for (int i
= navigation_controller_
->GetCurrentEntryIndex() + increment_value
;
250 i
+= increment_value
) {
251 if (num_added
>= max_entries
)
254 AddNavigationEntryToHistory(
255 env
, history
, navigation_controller_
->GetEntryAtIndex(i
), i
);
260 ScopedJavaLocalRef
<jstring
>
261 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry(
264 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
266 return ScopedJavaLocalRef
<jstring
>(env
, NULL
);
267 return ConvertUTF8ToJavaString(env
, entry
->GetOriginalRequestURL().spec());
270 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv
* env
,
272 content::SSLHostStateDelegate
* delegate
=
273 navigation_controller_
->GetBrowserContext()->GetSSLHostStateDelegate();
278 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv
* env
,
280 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
281 return entry
&& entry
->GetIsOverridingUserAgent();
284 void NavigationControllerAndroid::SetUseDesktopUserAgent(
288 jboolean reload_on_state_change
) {
289 if (GetUseDesktopUserAgent(env
, obj
) == enabled
)
292 // Make sure the navigation entry actually exists.
293 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
297 // Set the flag in the NavigationEntry.
298 entry
->SetIsOverridingUserAgent(enabled
);
300 // Send the override to the renderer.
301 if (reload_on_state_change
) {
302 // Reloading the page will send the override down as part of the
303 // navigation IPC message.
304 navigation_controller_
->ReloadOriginalRequestURL(false);
308 base::android::ScopedJavaLocalRef
<jobject
>
309 NavigationControllerAndroid::GetEntryAtIndex(JNIEnv
* env
,
312 if (index
< 0 || index
>= navigation_controller_
->GetEntryCount())
313 return base::android::ScopedJavaLocalRef
<jobject
>();
315 content::NavigationEntry
* entry
=
316 navigation_controller_
->GetEntryAtIndex(index
);
317 return CreateJavaNavigationEntry(env
, entry
, index
);
320 base::android::ScopedJavaLocalRef
<jobject
>
321 NavigationControllerAndroid::GetPendingEntry(JNIEnv
* env
, jobject obj
) {
322 content::NavigationEntry
* entry
= navigation_controller_
->GetPendingEntry();
325 return base::android::ScopedJavaLocalRef
<jobject
>();
327 return CreateJavaNavigationEntry(
328 env
, entry
, navigation_controller_
->GetPendingEntryIndex());
331 jint
NavigationControllerAndroid::GetLastCommittedEntryIndex(JNIEnv
* env
,
333 return navigation_controller_
->GetLastCommittedEntryIndex();
336 jboolean
NavigationControllerAndroid::RemoveEntryAtIndex(JNIEnv
* env
,
339 return navigation_controller_
->RemoveEntryAtIndex(index
);
342 } // namespace content