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(
53 entry
->GetTransitionType());
56 static void AddNavigationEntryToHistory(JNIEnv
* env
,
58 content::NavigationEntry
* entry
,
60 content::Java_NavigationControllerImpl_addToNavigationHistory(
63 CreateJavaNavigationEntry(env
, entry
, index
).obj());
71 bool NavigationControllerAndroid::Register(JNIEnv
* env
) {
72 return RegisterNativesImpl(env
);
75 NavigationControllerAndroid::NavigationControllerAndroid(
76 NavigationController
* navigation_controller
)
77 : navigation_controller_(navigation_controller
) {
78 JNIEnv
* env
= AttachCurrentThread();
80 Java_NavigationControllerImpl_create(
81 env
, reinterpret_cast<intptr_t>(this)).obj());
84 NavigationControllerAndroid::~NavigationControllerAndroid() {
85 Java_NavigationControllerImpl_destroy(AttachCurrentThread(), obj_
.obj());
88 base::android::ScopedJavaLocalRef
<jobject
>
89 NavigationControllerAndroid::GetJavaObject() {
90 return base::android::ScopedJavaLocalRef
<jobject
>(obj_
);
93 jboolean
NavigationControllerAndroid::CanGoBack(JNIEnv
* env
, jobject obj
) {
94 return navigation_controller_
->CanGoBack();
97 jboolean
NavigationControllerAndroid::CanGoForward(JNIEnv
* env
,
99 return navigation_controller_
->CanGoForward();
102 jboolean
NavigationControllerAndroid::CanGoToOffset(JNIEnv
* env
,
105 return navigation_controller_
->CanGoToOffset(offset
);
108 void NavigationControllerAndroid::GoBack(JNIEnv
* env
, jobject obj
) {
109 navigation_controller_
->GoBack();
112 void NavigationControllerAndroid::GoForward(JNIEnv
* env
, jobject obj
) {
113 navigation_controller_
->GoForward();
116 void NavigationControllerAndroid::GoToOffset(JNIEnv
* env
,
119 navigation_controller_
->GoToOffset(offset
);
122 jboolean
NavigationControllerAndroid::IsInitialNavigation(JNIEnv
* env
,
124 return navigation_controller_
->IsInitialNavigation();
127 void NavigationControllerAndroid::LoadIfNecessary(JNIEnv
* env
, jobject obj
) {
128 navigation_controller_
->LoadIfNecessary();
131 void NavigationControllerAndroid::ContinuePendingReload(JNIEnv
* env
,
133 navigation_controller_
->ContinuePendingReload();
136 void NavigationControllerAndroid::Reload(JNIEnv
* env
,
138 jboolean check_for_repost
) {
139 navigation_controller_
->Reload(check_for_repost
);
142 void NavigationControllerAndroid::ReloadIgnoringCache(
145 jboolean check_for_repost
) {
146 navigation_controller_
->ReloadIgnoringCache(check_for_repost
);
149 void NavigationControllerAndroid::RequestRestoreLoad(JNIEnv
* env
, jobject obj
) {
150 navigation_controller_
->SetNeedsReload();
153 void NavigationControllerAndroid::CancelPendingReload(JNIEnv
* env
,
155 navigation_controller_
->CancelPendingReload();
158 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv
* env
,
161 navigation_controller_
->GoToIndex(index
);
164 void NavigationControllerAndroid::LoadUrl(JNIEnv
* env
,
168 jint transition_type
,
169 jstring j_referrer_url
,
170 jint referrer_policy
,
171 jint ua_override_option
,
172 jstring extra_headers
,
173 jbyteArray post_data
,
174 jstring base_url_for_data_url
,
175 jstring virtual_url_for_data_url
,
176 jboolean can_load_local_resources
,
177 jboolean is_renderer_initiated
) {
179 NavigationController::LoadURLParams
params(
180 GURL(ConvertJavaStringToUTF8(env
, url
)));
183 static_cast<NavigationController::LoadURLType
>(load_url_type
);
184 params
.transition_type
= ui::PageTransitionFromInt(transition_type
);
185 params
.override_user_agent
=
186 static_cast<NavigationController::UserAgentOverrideOption
>(
188 params
.can_load_local_resources
= can_load_local_resources
;
189 params
.is_renderer_initiated
= is_renderer_initiated
;
192 params
.extra_headers
= ConvertJavaStringToUTF8(env
, extra_headers
);
195 std::vector
<uint8
> http_body_vector
;
196 base::android::JavaByteArrayToByteVector(env
, post_data
, &http_body_vector
);
197 params
.browser_initiated_post_data
=
198 base::RefCountedBytes::TakeVector(&http_body_vector
);
201 if (base_url_for_data_url
) {
202 params
.base_url_for_data_url
=
203 GURL(ConvertJavaStringToUTF8(env
, base_url_for_data_url
));
206 if (virtual_url_for_data_url
) {
207 params
.virtual_url_for_data_url
=
208 GURL(ConvertJavaStringToUTF8(env
, virtual_url_for_data_url
));
211 if (j_referrer_url
) {
212 params
.referrer
= content::Referrer(
213 GURL(ConvertJavaStringToUTF8(env
, j_referrer_url
)),
214 static_cast<blink::WebReferrerPolicy
>(referrer_policy
));
217 navigation_controller_
->LoadURLWithParams(params
);
220 void NavigationControllerAndroid::ClearHistory(JNIEnv
* env
, jobject obj
) {
221 // TODO(creis): Do callers of this need to know if it fails?
222 if (navigation_controller_
->CanPruneAllButLastCommitted())
223 navigation_controller_
->PruneAllButLastCommitted();
226 jint
NavigationControllerAndroid::GetNavigationHistory(JNIEnv
* env
,
229 // Iterate through navigation entries to populate the list
230 int count
= navigation_controller_
->GetEntryCount();
231 for (int i
= 0; i
< count
; ++i
) {
232 AddNavigationEntryToHistory(
233 env
, history
, navigation_controller_
->GetEntryAtIndex(i
), i
);
236 return navigation_controller_
->GetCurrentEntryIndex();
239 void NavigationControllerAndroid::GetDirectedNavigationHistory(
245 // Iterate through navigation entries to populate the list
246 int count
= navigation_controller_
->GetEntryCount();
248 int increment_value
= is_forward
? 1 : -1;
249 for (int i
= navigation_controller_
->GetCurrentEntryIndex() + increment_value
;
251 i
+= increment_value
) {
252 if (num_added
>= max_entries
)
255 AddNavigationEntryToHistory(
256 env
, history
, navigation_controller_
->GetEntryAtIndex(i
), i
);
261 ScopedJavaLocalRef
<jstring
>
262 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry(
265 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
267 return ScopedJavaLocalRef
<jstring
>(env
, NULL
);
268 return ConvertUTF8ToJavaString(env
, entry
->GetOriginalRequestURL().spec());
271 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv
* env
,
273 content::SSLHostStateDelegate
* delegate
=
274 navigation_controller_
->GetBrowserContext()->GetSSLHostStateDelegate();
279 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv
* env
,
281 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
282 return entry
&& entry
->GetIsOverridingUserAgent();
285 void NavigationControllerAndroid::SetUseDesktopUserAgent(
289 jboolean reload_on_state_change
) {
290 if (GetUseDesktopUserAgent(env
, obj
) == enabled
)
293 // Make sure the navigation entry actually exists.
294 NavigationEntry
* entry
= navigation_controller_
->GetVisibleEntry();
298 // Set the flag in the NavigationEntry.
299 entry
->SetIsOverridingUserAgent(enabled
);
301 // Send the override to the renderer.
302 if (reload_on_state_change
) {
303 // Reloading the page will send the override down as part of the
304 // navigation IPC message.
305 navigation_controller_
->ReloadOriginalRequestURL(false);
309 base::android::ScopedJavaLocalRef
<jobject
>
310 NavigationControllerAndroid::GetEntryAtIndex(JNIEnv
* env
,
313 if (index
< 0 || index
>= navigation_controller_
->GetEntryCount())
314 return base::android::ScopedJavaLocalRef
<jobject
>();
316 content::NavigationEntry
* entry
=
317 navigation_controller_
->GetEntryAtIndex(index
);
318 return CreateJavaNavigationEntry(env
, entry
, index
);
321 base::android::ScopedJavaLocalRef
<jobject
>
322 NavigationControllerAndroid::GetPendingEntry(JNIEnv
* env
, jobject obj
) {
323 content::NavigationEntry
* entry
= navigation_controller_
->GetPendingEntry();
326 return base::android::ScopedJavaLocalRef
<jobject
>();
328 return CreateJavaNavigationEntry(
329 env
, entry
, navigation_controller_
->GetPendingEntryIndex());
332 jint
NavigationControllerAndroid::GetLastCommittedEntryIndex(JNIEnv
* env
,
334 return navigation_controller_
->GetLastCommittedEntryIndex();
337 jboolean
NavigationControllerAndroid::RemoveEntryAtIndex(JNIEnv
* env
,
340 return navigation_controller_
->RemoveEntryAtIndex(index
);
343 jboolean
NavigationControllerAndroid::CanCopyStateOver(JNIEnv
* env
,
345 return navigation_controller_
->GetEntryCount() == 0 &&
346 !navigation_controller_
->GetPendingEntry();
349 jboolean
NavigationControllerAndroid::CanPruneAllButLastCommitted(JNIEnv
* env
,
351 return navigation_controller_
->CanPruneAllButLastCommitted();
354 void NavigationControllerAndroid::CopyStateFrom(
357 jlong source_navigation_controller_android
) {
358 navigation_controller_
->CopyStateFrom(
359 *(reinterpret_cast<NavigationControllerAndroid
*>(
360 source_navigation_controller_android
)->navigation_controller_
));
363 void NavigationControllerAndroid::CopyStateFromAndPrune(
366 jlong source_navigation_controller_android
,
367 jboolean replace_entry
) {
368 navigation_controller_
->CopyStateFromAndPrune(
369 reinterpret_cast<NavigationControllerAndroid
*>(
370 source_navigation_controller_android
)->navigation_controller_
,
374 } // namespace content