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/browser/ssl/ssl_host_state.h" |
8 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
9 #include "jni/NavigationControllerImpl_jni.h" | 13 #include "jni/NavigationControllerImpl_jni.h" |
| 14 #include "ui/gfx/android/java_bitmap.h" |
10 | 15 |
11 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
| 17 using base::android::ConvertJavaStringToUTF16; |
| 18 using base::android::ConvertJavaStringToUTF8; |
| 19 using base::android::ConvertUTF16ToJavaString; |
| 20 using base::android::ConvertUTF8ToJavaString; |
| 21 |
| 22 namespace { |
| 23 |
| 24 // static |
| 25 static void AddNavigationEntryToHistory( |
| 26 JNIEnv* env, |
| 27 jobject obj, |
| 28 jobject history, |
| 29 content::NavigationEntry* entry, |
| 30 int index) { |
| 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()); |
| 44 |
| 45 // Add the item to the list |
| 46 content::Java_NavigationControllerImpl_addToNavigationHistory( |
| 47 env, obj, history, index, j_url.obj(), j_virtual_url.obj(), |
| 48 j_original_url.obj(), j_title.obj(), j_bitmap.obj()); |
| 49 } |
| 50 |
| 51 } // namespace |
12 | 52 |
13 namespace content { | 53 namespace content { |
14 | 54 |
15 // static | 55 // static |
16 bool NavigationControllerAndroid::Register(JNIEnv* env) { | 56 bool NavigationControllerAndroid::Register(JNIEnv* env) { |
17 return RegisterNativesImpl(env); | 57 return RegisterNativesImpl(env); |
18 } | 58 } |
19 | 59 |
20 NavigationControllerAndroid::NavigationControllerAndroid( | 60 NavigationControllerAndroid::NavigationControllerAndroid( |
21 NavigationController* navigation_controller) | 61 NavigationController* navigation_controller) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 jobject obj) { | 134 jobject obj) { |
95 navigation_controller_->CancelPendingReload(); | 135 navigation_controller_->CancelPendingReload(); |
96 } | 136 } |
97 | 137 |
98 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env, | 138 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env, |
99 jobject obj, | 139 jobject obj, |
100 jint index) { | 140 jint index) { |
101 navigation_controller_->GoToIndex(index); | 141 navigation_controller_->GoToIndex(index); |
102 } | 142 } |
103 | 143 |
| 144 void NavigationControllerAndroid::LoadUrl( |
| 145 JNIEnv* env, |
| 146 jobject obj, |
| 147 jstring url, |
| 148 jint load_url_type, |
| 149 jint transition_type, |
| 150 jstring j_referrer_url, |
| 151 jint referrer_policy, |
| 152 jint ua_override_option, |
| 153 jstring extra_headers, |
| 154 jbyteArray post_data, |
| 155 jstring base_url_for_data_url, |
| 156 jstring virtual_url_for_data_url, |
| 157 jboolean can_load_local_resources, |
| 158 jboolean is_renderer_initiated) { |
| 159 DCHECK(url); |
| 160 NavigationController::LoadURLParams params( |
| 161 GURL(ConvertJavaStringToUTF8(env, url))); |
| 162 |
| 163 params.load_type = static_cast<NavigationController::LoadURLType>( |
| 164 load_url_type); |
| 165 params.transition_type = PageTransitionFromInt(transition_type); |
| 166 params.override_user_agent = |
| 167 static_cast<NavigationController::UserAgentOverrideOption>( |
| 168 ua_override_option); |
| 169 params.can_load_local_resources = can_load_local_resources; |
| 170 params.is_renderer_initiated = is_renderer_initiated; |
| 171 |
| 172 if (extra_headers) |
| 173 params.extra_headers = ConvertJavaStringToUTF8(env, extra_headers); |
| 174 |
| 175 if (post_data) { |
| 176 std::vector<uint8> http_body_vector; |
| 177 base::android::JavaByteArrayToByteVector(env, post_data, &http_body_vector); |
| 178 params.browser_initiated_post_data = |
| 179 base::RefCountedBytes::TakeVector(&http_body_vector); |
| 180 } |
| 181 |
| 182 if (base_url_for_data_url) { |
| 183 params.base_url_for_data_url = |
| 184 GURL(ConvertJavaStringToUTF8(env, base_url_for_data_url)); |
| 185 } |
| 186 |
| 187 if (virtual_url_for_data_url) { |
| 188 params.virtual_url_for_data_url = |
| 189 GURL(ConvertJavaStringToUTF8(env, virtual_url_for_data_url)); |
| 190 } |
| 191 |
| 192 if (j_referrer_url) { |
| 193 params.referrer = content::Referrer( |
| 194 GURL(ConvertJavaStringToUTF8(env, j_referrer_url)), |
| 195 static_cast<blink::WebReferrerPolicy>(referrer_policy)); |
| 196 } |
| 197 |
| 198 navigation_controller_->LoadURLWithParams(params); |
| 199 } |
| 200 |
| 201 void NavigationControllerAndroid::ClearHistory(JNIEnv* env, jobject obj) { |
| 202 // TODO(creis): Do callers of this need to know if it fails? |
| 203 if (navigation_controller_->CanPruneAllButLastCommitted()) |
| 204 navigation_controller_->PruneAllButLastCommitted(); |
| 205 } |
| 206 |
| 207 jint NavigationControllerAndroid::GetNavigationHistory( |
| 208 JNIEnv* env, |
| 209 jobject obj, |
| 210 jobject history) { |
| 211 // Iterate through navigation entries to populate the list |
| 212 int count = navigation_controller_->GetEntryCount(); |
| 213 for (int i = 0; i < count; ++i) { |
| 214 AddNavigationEntryToHistory( |
| 215 env, obj, history, navigation_controller_->GetEntryAtIndex(i), i); |
| 216 } |
| 217 |
| 218 return navigation_controller_->GetCurrentEntryIndex(); |
| 219 } |
| 220 |
| 221 void NavigationControllerAndroid::GetDirectedNavigationHistory( |
| 222 JNIEnv* env, |
| 223 jobject obj, |
| 224 jobject history, |
| 225 jboolean is_forward, |
| 226 jint max_entries) { |
| 227 // Iterate through navigation entries to populate the list |
| 228 int count = navigation_controller_->GetEntryCount(); |
| 229 int num_added = 0; |
| 230 int increment_value = is_forward ? 1 : -1; |
| 231 for (int i = navigation_controller_->GetCurrentEntryIndex() + increment_value; |
| 232 i >= 0 && i < count; |
| 233 i += increment_value) { |
| 234 if (num_added >= max_entries) |
| 235 break; |
| 236 |
| 237 AddNavigationEntryToHistory( |
| 238 env, obj, history, navigation_controller_->GetEntryAtIndex(i), i); |
| 239 num_added++; |
| 240 } |
| 241 } |
| 242 |
| 243 ScopedJavaLocalRef<jstring> |
| 244 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry( |
| 245 JNIEnv* env, |
| 246 jobject obj) { |
| 247 NavigationEntry* entry = navigation_controller_->GetVisibleEntry(); |
| 248 if (entry == NULL) |
| 249 return ScopedJavaLocalRef<jstring>(env, NULL); |
| 250 return ConvertUTF8ToJavaString(env, entry->GetOriginalRequestURL().spec()); |
| 251 } |
| 252 |
| 253 void NavigationControllerAndroid::ClearSslPreferences( |
| 254 JNIEnv* env, |
| 255 jobject obj) { |
| 256 SSLHostState* state = SSLHostState::GetFor( |
| 257 navigation_controller_->GetBrowserContext()); |
| 258 state->Clear(); |
| 259 } |
| 260 |
| 261 bool NavigationControllerAndroid::GetUseDesktopUserAgent( |
| 262 JNIEnv* env, |
| 263 jobject obj) { |
| 264 NavigationEntry* entry = navigation_controller_->GetVisibleEntry(); |
| 265 return entry && entry->GetIsOverridingUserAgent(); |
| 266 } |
| 267 |
| 268 void NavigationControllerAndroid::SetUseDesktopUserAgent( |
| 269 JNIEnv* env, |
| 270 jobject obj, |
| 271 jboolean enabled, |
| 272 jboolean reload_on_state_change) { |
| 273 if (GetUseDesktopUserAgent(env, obj) == enabled) |
| 274 return; |
| 275 |
| 276 // Make sure the navigation entry actually exists. |
| 277 NavigationEntry* entry = navigation_controller_->GetVisibleEntry(); |
| 278 if (!entry) |
| 279 return; |
| 280 |
| 281 // Set the flag in the NavigationEntry. |
| 282 entry->SetIsOverridingUserAgent(enabled); |
| 283 |
| 284 // Send the override to the renderer. |
| 285 if (reload_on_state_change) { |
| 286 // Reloading the page will send the override down as part of the |
| 287 // navigation IPC message. |
| 288 navigation_controller_->ReloadOriginalRequestURL(false); |
| 289 } |
| 290 } |
| 291 |
104 } // namespace content | 292 } // namespace content |
OLD | NEW |