Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / frame_host / navigation_controller_android.cc
blob4820dc074d6dd567b15d417e91c8596afa204a88
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 base::android::ScopedJavaLocalRef<jobject> CreateJavaNavigationEntry(
26 JNIEnv* env,
27 content::NavigationEntry* entry,
28 int index) {
29 DCHECK(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(
46 env,
47 index,
48 j_url.obj(),
49 j_virtual_url.obj(),
50 j_original_url.obj(),
51 j_title.obj(),
52 j_bitmap.obj(),
53 entry->GetTransitionType());
56 static void AddNavigationEntryToHistory(JNIEnv* env,
57 jobject history,
58 content::NavigationEntry* entry,
59 int index) {
60 content::Java_NavigationControllerImpl_addToNavigationHistory(
61 env,
62 history,
63 CreateJavaNavigationEntry(env, entry, index).obj());
66 } // namespace
68 namespace content {
70 // static
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();
79 obj_.Reset(env,
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,
98 jobject obj) {
99 return navigation_controller_->CanGoForward();
102 jboolean NavigationControllerAndroid::CanGoToOffset(JNIEnv* env,
103 jobject obj,
104 jint offset) {
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,
117 jobject obj,
118 jint offset) {
119 navigation_controller_->GoToOffset(offset);
122 jboolean NavigationControllerAndroid::IsInitialNavigation(JNIEnv* env,
123 jobject obj) {
124 return navigation_controller_->IsInitialNavigation();
127 void NavigationControllerAndroid::LoadIfNecessary(JNIEnv* env, jobject obj) {
128 navigation_controller_->LoadIfNecessary();
131 void NavigationControllerAndroid::ContinuePendingReload(JNIEnv* env,
132 jobject obj) {
133 navigation_controller_->ContinuePendingReload();
136 void NavigationControllerAndroid::Reload(JNIEnv* env,
137 jobject obj,
138 jboolean check_for_repost) {
139 navigation_controller_->Reload(check_for_repost);
142 void NavigationControllerAndroid::ReloadIgnoringCache(
143 JNIEnv* env,
144 jobject obj,
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,
154 jobject obj) {
155 navigation_controller_->CancelPendingReload();
158 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env,
159 jobject obj,
160 jint index) {
161 navigation_controller_->GoToIndex(index);
164 void NavigationControllerAndroid::LoadUrl(JNIEnv* env,
165 jobject obj,
166 jstring url,
167 jint load_url_type,
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) {
178 DCHECK(url);
179 NavigationController::LoadURLParams params(
180 GURL(ConvertJavaStringToUTF8(env, url)));
182 params.load_type =
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>(
187 ua_override_option);
188 params.can_load_local_resources = can_load_local_resources;
189 params.is_renderer_initiated = is_renderer_initiated;
191 if (extra_headers)
192 params.extra_headers = ConvertJavaStringToUTF8(env, extra_headers);
194 if (post_data) {
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,
227 jobject obj,
228 jobject history) {
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(
240 JNIEnv* env,
241 jobject obj,
242 jobject history,
243 jboolean is_forward,
244 jint max_entries) {
245 // Iterate through navigation entries to populate the list
246 int count = navigation_controller_->GetEntryCount();
247 int num_added = 0;
248 int increment_value = is_forward ? 1 : -1;
249 for (int i = navigation_controller_->GetCurrentEntryIndex() + increment_value;
250 i >= 0 && i < count;
251 i += increment_value) {
252 if (num_added >= max_entries)
253 break;
255 AddNavigationEntryToHistory(
256 env, history, navigation_controller_->GetEntryAtIndex(i), i);
257 num_added++;
261 ScopedJavaLocalRef<jstring>
262 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry(
263 JNIEnv* env,
264 jobject obj) {
265 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
266 if (entry == NULL)
267 return ScopedJavaLocalRef<jstring>(env, NULL);
268 return ConvertUTF8ToJavaString(env, entry->GetOriginalRequestURL().spec());
271 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv* env,
272 jobject obj) {
273 content::SSLHostStateDelegate* delegate =
274 navigation_controller_->GetBrowserContext()->GetSSLHostStateDelegate();
275 if (delegate)
276 delegate->Clear();
279 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv* env,
280 jobject obj) {
281 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
282 return entry && entry->GetIsOverridingUserAgent();
285 void NavigationControllerAndroid::SetUseDesktopUserAgent(
286 JNIEnv* env,
287 jobject obj,
288 jboolean enabled,
289 jboolean reload_on_state_change) {
290 if (GetUseDesktopUserAgent(env, obj) == enabled)
291 return;
293 // Make sure the navigation entry actually exists.
294 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
295 if (!entry)
296 return;
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,
311 jobject obj,
312 int index) {
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();
325 if (!entry)
326 return base::android::ScopedJavaLocalRef<jobject>();
328 return CreateJavaNavigationEntry(
329 env, entry, navigation_controller_->GetPendingEntryIndex());
332 jint NavigationControllerAndroid::GetLastCommittedEntryIndex(JNIEnv* env,
333 jobject obj) {
334 return navigation_controller_->GetLastCommittedEntryIndex();
337 jboolean NavigationControllerAndroid::RemoveEntryAtIndex(JNIEnv* env,
338 jobject obj,
339 jint index) {
340 return navigation_controller_->RemoveEntryAtIndex(index);
343 } // namespace content