Implement SSLKEYLOGFILE for OpenSSL.
[chromium-blink-merge.git] / content / browser / frame_host / navigation_controller_android.cc
blob4f3f45a684f393ae7777c948f04b77d3378ed0bc
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;
22 namespace {
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());
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());
57 } // namespace
59 namespace content {
61 // static
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();
70 obj_.Reset(env,
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,
89 jobject obj) {
90 return navigation_controller_->CanGoForward();
93 jboolean NavigationControllerAndroid::CanGoToOffset(JNIEnv* env,
94 jobject obj,
95 jint offset) {
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,
108 jobject obj,
109 jint offset) {
110 navigation_controller_->GoToOffset(offset);
113 void NavigationControllerAndroid::LoadIfNecessary(JNIEnv* env, jobject obj) {
114 navigation_controller_->LoadIfNecessary();
117 void NavigationControllerAndroid::ContinuePendingReload(JNIEnv* env,
118 jobject obj) {
119 navigation_controller_->ContinuePendingReload();
122 void NavigationControllerAndroid::Reload(JNIEnv* env,
123 jobject obj,
124 jboolean check_for_repost) {
125 navigation_controller_->Reload(check_for_repost);
128 void NavigationControllerAndroid::ReloadIgnoringCache(
129 JNIEnv* env,
130 jobject obj,
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,
140 jobject obj) {
141 navigation_controller_->CancelPendingReload();
144 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env,
145 jobject obj,
146 jint index) {
147 navigation_controller_->GoToIndex(index);
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)));
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;
177 if (extra_headers)
178 params.extra_headers = ConvertJavaStringToUTF8(env, extra_headers);
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);
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,
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);
222 return navigation_controller_->GetCurrentEntryIndex();
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;
241 AddNavigationEntryToHistory(
242 env, obj, history, navigation_controller_->GetEntryAtIndex(i), i);
243 num_added++;
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());
257 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv* env,
258 jobject obj) {
259 content::SSLHostStateDelegate* delegate =
260 navigation_controller_->GetBrowserContext()->GetSSLHostStateDelegate();
261 if (delegate)
262 delegate->Clear();
265 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv* env,
266 jobject obj) {
267 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
268 return entry && entry->GetIsOverridingUserAgent();
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;
279 // Make sure the navigation entry actually exists.
280 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
281 if (!entry)
282 return;
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