OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/frame_host/navigation_controller_android.h" | 5 #include "content/browser/frame_host/navigation_controller_android.h" |
6 | 6 |
7 #include "base/android/jni_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" |
8 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
| 13 #include "content/public/browser/ssl_host_state_delegate.h" |
9 #include "jni/NavigationControllerImpl_jni.h" | 14 #include "jni/NavigationControllerImpl_jni.h" |
| 15 #include "ui/gfx/android/java_bitmap.h" |
10 | 16 |
11 using base::android::AttachCurrentThread; | 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; |
| 22 namespace { |
| 23 |
| 24 // static |
| 25 static void AddNavigationEntryToHistory(JNIEnv* env, |
| 26 jobject obj, |
| 27 jobject history, |
| 28 content::NavigationEntry* entry, |
| 29 int index) { |
| 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()); |
| 43 |
| 44 // Add the item to the list |
| 45 content::Java_NavigationControllerImpl_addToNavigationHistory( |
| 46 env, |
| 47 obj, |
| 48 history, |
| 49 index, |
| 50 j_url.obj(), |
| 51 j_virtual_url.obj(), |
| 52 j_original_url.obj(), |
| 53 j_title.obj(), |
| 54 j_bitmap.obj()); |
| 55 } |
| 56 |
| 57 } // namespace |
12 | 58 |
13 namespace content { | 59 namespace content { |
14 | 60 |
15 // static | 61 // static |
16 bool NavigationControllerAndroid::Register(JNIEnv* env) { | 62 bool NavigationControllerAndroid::Register(JNIEnv* env) { |
17 return RegisterNativesImpl(env); | 63 return RegisterNativesImpl(env); |
18 } | 64 } |
19 | 65 |
20 NavigationControllerAndroid::NavigationControllerAndroid( | 66 NavigationControllerAndroid::NavigationControllerAndroid( |
21 NavigationController* navigation_controller) | 67 NavigationController* navigation_controller) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 jobject obj) { | 140 jobject obj) { |
95 navigation_controller_->CancelPendingReload(); | 141 navigation_controller_->CancelPendingReload(); |
96 } | 142 } |
97 | 143 |
98 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env, | 144 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env, |
99 jobject obj, | 145 jobject obj, |
100 jint index) { | 146 jint index) { |
101 navigation_controller_->GoToIndex(index); | 147 navigation_controller_->GoToIndex(index); |
102 } | 148 } |
103 | 149 |
| 150 void NavigationControllerAndroid::LoadUrl(JNIEnv* env, |
| 151 jobject obj, |
| 152 jstring url, |
| 153 jint load_url_type, |
| 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) { |
| 164 DCHECK(url); |
| 165 NavigationController::LoadURLParams params( |
| 166 GURL(ConvertJavaStringToUTF8(env, url))); |
| 167 |
| 168 params.load_type = |
| 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>( |
| 173 ua_override_option); |
| 174 params.can_load_local_resources = can_load_local_resources; |
| 175 params.is_renderer_initiated = is_renderer_initiated; |
| 176 |
| 177 if (extra_headers) |
| 178 params.extra_headers = ConvertJavaStringToUTF8(env, extra_headers); |
| 179 |
| 180 if (post_data) { |
| 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); |
| 185 } |
| 186 |
| 187 if (base_url_for_data_url) { |
| 188 params.base_url_for_data_url = |
| 189 GURL(ConvertJavaStringToUTF8(env, base_url_for_data_url)); |
| 190 } |
| 191 |
| 192 if (virtual_url_for_data_url) { |
| 193 params.virtual_url_for_data_url = |
| 194 GURL(ConvertJavaStringToUTF8(env, virtual_url_for_data_url)); |
| 195 } |
| 196 |
| 197 if (j_referrer_url) { |
| 198 params.referrer = content::Referrer( |
| 199 GURL(ConvertJavaStringToUTF8(env, j_referrer_url)), |
| 200 static_cast<blink::WebReferrerPolicy>(referrer_policy)); |
| 201 } |
| 202 |
| 203 navigation_controller_->LoadURLWithParams(params); |
| 204 } |
| 205 |
| 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(); |
| 210 } |
| 211 |
| 212 jint NavigationControllerAndroid::GetNavigationHistory(JNIEnv* env, |
| 213 jobject obj, |
| 214 jobject history) { |
| 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); |
| 220 } |
| 221 |
| 222 return navigation_controller_->GetCurrentEntryIndex(); |
| 223 } |
| 224 |
| 225 void NavigationControllerAndroid::GetDirectedNavigationHistory( |
| 226 JNIEnv* env, |
| 227 jobject obj, |
| 228 jobject history, |
| 229 jboolean is_forward, |
| 230 jint max_entries) { |
| 231 // Iterate through navigation entries to populate the list |
| 232 int count = navigation_controller_->GetEntryCount(); |
| 233 int num_added = 0; |
| 234 int increment_value = is_forward ? 1 : -1; |
| 235 for (int i = navigation_controller_->GetCurrentEntryIndex() + increment_value; |
| 236 i >= 0 && i < count; |
| 237 i += increment_value) { |
| 238 if (num_added >= max_entries) |
| 239 break; |
| 240 |
| 241 AddNavigationEntryToHistory( |
| 242 env, obj, history, navigation_controller_->GetEntryAtIndex(i), i); |
| 243 num_added++; |
| 244 } |
| 245 } |
| 246 |
| 247 ScopedJavaLocalRef<jstring> |
| 248 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry( |
| 249 JNIEnv* env, |
| 250 jobject obj) { |
| 251 NavigationEntry* entry = navigation_controller_->GetVisibleEntry(); |
| 252 if (entry == NULL) |
| 253 return ScopedJavaLocalRef<jstring>(env, NULL); |
| 254 return ConvertUTF8ToJavaString(env, entry->GetOriginalRequestURL().spec()); |
| 255 } |
| 256 |
| 257 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv* env, |
| 258 jobject obj) { |
| 259 content::SSLHostStateDelegate* delegate = |
| 260 navigation_controller_->GetBrowserContext()->GetSSLHostStateDelegate(); |
| 261 if (delegate) |
| 262 delegate->Clear(); |
| 263 } |
| 264 |
| 265 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv* env, |
| 266 jobject obj) { |
| 267 NavigationEntry* entry = navigation_controller_->GetVisibleEntry(); |
| 268 return entry && entry->GetIsOverridingUserAgent(); |
| 269 } |
| 270 |
| 271 void NavigationControllerAndroid::SetUseDesktopUserAgent( |
| 272 JNIEnv* env, |
| 273 jobject obj, |
| 274 jboolean enabled, |
| 275 jboolean reload_on_state_change) { |
| 276 if (GetUseDesktopUserAgent(env, obj) == enabled) |
| 277 return; |
| 278 |
| 279 // Make sure the navigation entry actually exists. |
| 280 NavigationEntry* entry = navigation_controller_->GetVisibleEntry(); |
| 281 if (!entry) |
| 282 return; |
| 283 |
| 284 // Set the flag in the NavigationEntry. |
| 285 entry->SetIsOverridingUserAgent(enabled); |
| 286 |
| 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); |
| 292 } |
| 293 } |
| 294 |
104 } // namespace content | 295 } // namespace content |
OLD | NEW |