[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / content / browser / frame_host / navigation_controller_android.cc
blob3b3d4928159b017cd2e84474b71a665340976c20
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());
55 static void AddNavigationEntryToHistory(JNIEnv* env,
56 jobject history,
57 content::NavigationEntry* entry,
58 int index) {
59 content::Java_NavigationControllerImpl_addToNavigationHistory(
60 env,
61 history,
62 CreateJavaNavigationEntry(env, entry, index).obj());
65 } // namespace
67 namespace content {
69 // static
70 bool NavigationControllerAndroid::Register(JNIEnv* env) {
71 return RegisterNativesImpl(env);
74 NavigationControllerAndroid::NavigationControllerAndroid(
75 NavigationController* navigation_controller)
76 : navigation_controller_(navigation_controller) {
77 JNIEnv* env = AttachCurrentThread();
78 obj_.Reset(env,
79 Java_NavigationControllerImpl_create(
80 env, reinterpret_cast<intptr_t>(this)).obj());
83 NavigationControllerAndroid::~NavigationControllerAndroid() {
84 Java_NavigationControllerImpl_destroy(AttachCurrentThread(), obj_.obj());
87 base::android::ScopedJavaLocalRef<jobject>
88 NavigationControllerAndroid::GetJavaObject() {
89 return base::android::ScopedJavaLocalRef<jobject>(obj_);
92 jboolean NavigationControllerAndroid::CanGoBack(JNIEnv* env, jobject obj) {
93 return navigation_controller_->CanGoBack();
96 jboolean NavigationControllerAndroid::CanGoForward(JNIEnv* env,
97 jobject obj) {
98 return navigation_controller_->CanGoForward();
101 jboolean NavigationControllerAndroid::CanGoToOffset(JNIEnv* env,
102 jobject obj,
103 jint offset) {
104 return navigation_controller_->CanGoToOffset(offset);
107 void NavigationControllerAndroid::GoBack(JNIEnv* env, jobject obj) {
108 navigation_controller_->GoBack();
111 void NavigationControllerAndroid::GoForward(JNIEnv* env, jobject obj) {
112 navigation_controller_->GoForward();
115 void NavigationControllerAndroid::GoToOffset(JNIEnv* env,
116 jobject obj,
117 jint offset) {
118 navigation_controller_->GoToOffset(offset);
121 jboolean NavigationControllerAndroid::IsInitialNavigation(JNIEnv* env,
122 jobject obj) {
123 return navigation_controller_->IsInitialNavigation();
126 void NavigationControllerAndroid::LoadIfNecessary(JNIEnv* env, jobject obj) {
127 navigation_controller_->LoadIfNecessary();
130 void NavigationControllerAndroid::ContinuePendingReload(JNIEnv* env,
131 jobject obj) {
132 navigation_controller_->ContinuePendingReload();
135 void NavigationControllerAndroid::Reload(JNIEnv* env,
136 jobject obj,
137 jboolean check_for_repost) {
138 navigation_controller_->Reload(check_for_repost);
141 void NavigationControllerAndroid::ReloadIgnoringCache(
142 JNIEnv* env,
143 jobject obj,
144 jboolean check_for_repost) {
145 navigation_controller_->ReloadIgnoringCache(check_for_repost);
148 void NavigationControllerAndroid::RequestRestoreLoad(JNIEnv* env, jobject obj) {
149 navigation_controller_->SetNeedsReload();
152 void NavigationControllerAndroid::CancelPendingReload(JNIEnv* env,
153 jobject obj) {
154 navigation_controller_->CancelPendingReload();
157 void NavigationControllerAndroid::GoToNavigationIndex(JNIEnv* env,
158 jobject obj,
159 jint index) {
160 navigation_controller_->GoToIndex(index);
163 void NavigationControllerAndroid::LoadUrl(JNIEnv* env,
164 jobject obj,
165 jstring url,
166 jint load_url_type,
167 jint transition_type,
168 jstring j_referrer_url,
169 jint referrer_policy,
170 jint ua_override_option,
171 jstring extra_headers,
172 jbyteArray post_data,
173 jstring base_url_for_data_url,
174 jstring virtual_url_for_data_url,
175 jboolean can_load_local_resources,
176 jboolean is_renderer_initiated) {
177 DCHECK(url);
178 NavigationController::LoadURLParams params(
179 GURL(ConvertJavaStringToUTF8(env, url)));
181 params.load_type =
182 static_cast<NavigationController::LoadURLType>(load_url_type);
183 params.transition_type = ui::PageTransitionFromInt(transition_type);
184 params.override_user_agent =
185 static_cast<NavigationController::UserAgentOverrideOption>(
186 ua_override_option);
187 params.can_load_local_resources = can_load_local_resources;
188 params.is_renderer_initiated = is_renderer_initiated;
190 if (extra_headers)
191 params.extra_headers = ConvertJavaStringToUTF8(env, extra_headers);
193 if (post_data) {
194 std::vector<uint8> http_body_vector;
195 base::android::JavaByteArrayToByteVector(env, post_data, &http_body_vector);
196 params.browser_initiated_post_data =
197 base::RefCountedBytes::TakeVector(&http_body_vector);
200 if (base_url_for_data_url) {
201 params.base_url_for_data_url =
202 GURL(ConvertJavaStringToUTF8(env, base_url_for_data_url));
205 if (virtual_url_for_data_url) {
206 params.virtual_url_for_data_url =
207 GURL(ConvertJavaStringToUTF8(env, virtual_url_for_data_url));
210 if (j_referrer_url) {
211 params.referrer = content::Referrer(
212 GURL(ConvertJavaStringToUTF8(env, j_referrer_url)),
213 static_cast<blink::WebReferrerPolicy>(referrer_policy));
216 navigation_controller_->LoadURLWithParams(params);
219 void NavigationControllerAndroid::ClearHistory(JNIEnv* env, jobject obj) {
220 // TODO(creis): Do callers of this need to know if it fails?
221 if (navigation_controller_->CanPruneAllButLastCommitted())
222 navigation_controller_->PruneAllButLastCommitted();
225 jint NavigationControllerAndroid::GetNavigationHistory(JNIEnv* env,
226 jobject obj,
227 jobject history) {
228 // Iterate through navigation entries to populate the list
229 int count = navigation_controller_->GetEntryCount();
230 for (int i = 0; i < count; ++i) {
231 AddNavigationEntryToHistory(
232 env, history, navigation_controller_->GetEntryAtIndex(i), i);
235 return navigation_controller_->GetCurrentEntryIndex();
238 void NavigationControllerAndroid::GetDirectedNavigationHistory(
239 JNIEnv* env,
240 jobject obj,
241 jobject history,
242 jboolean is_forward,
243 jint max_entries) {
244 // Iterate through navigation entries to populate the list
245 int count = navigation_controller_->GetEntryCount();
246 int num_added = 0;
247 int increment_value = is_forward ? 1 : -1;
248 for (int i = navigation_controller_->GetCurrentEntryIndex() + increment_value;
249 i >= 0 && i < count;
250 i += increment_value) {
251 if (num_added >= max_entries)
252 break;
254 AddNavigationEntryToHistory(
255 env, history, navigation_controller_->GetEntryAtIndex(i), i);
256 num_added++;
260 ScopedJavaLocalRef<jstring>
261 NavigationControllerAndroid::GetOriginalUrlForVisibleNavigationEntry(
262 JNIEnv* env,
263 jobject obj) {
264 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
265 if (entry == NULL)
266 return ScopedJavaLocalRef<jstring>(env, NULL);
267 return ConvertUTF8ToJavaString(env, entry->GetOriginalRequestURL().spec());
270 void NavigationControllerAndroid::ClearSslPreferences(JNIEnv* env,
271 jobject obj) {
272 content::SSLHostStateDelegate* delegate =
273 navigation_controller_->GetBrowserContext()->GetSSLHostStateDelegate();
274 if (delegate)
275 delegate->Clear();
278 bool NavigationControllerAndroid::GetUseDesktopUserAgent(JNIEnv* env,
279 jobject obj) {
280 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
281 return entry && entry->GetIsOverridingUserAgent();
284 void NavigationControllerAndroid::SetUseDesktopUserAgent(
285 JNIEnv* env,
286 jobject obj,
287 jboolean enabled,
288 jboolean reload_on_state_change) {
289 if (GetUseDesktopUserAgent(env, obj) == enabled)
290 return;
292 // Make sure the navigation entry actually exists.
293 NavigationEntry* entry = navigation_controller_->GetVisibleEntry();
294 if (!entry)
295 return;
297 // Set the flag in the NavigationEntry.
298 entry->SetIsOverridingUserAgent(enabled);
300 // Send the override to the renderer.
301 if (reload_on_state_change) {
302 // Reloading the page will send the override down as part of the
303 // navigation IPC message.
304 navigation_controller_->ReloadOriginalRequestURL(false);
308 base::android::ScopedJavaLocalRef<jobject>
309 NavigationControllerAndroid::GetEntryAtIndex(JNIEnv* env,
310 jobject obj,
311 int index) {
312 if (index < 0 || index >= navigation_controller_->GetEntryCount())
313 return base::android::ScopedJavaLocalRef<jobject>();
315 content::NavigationEntry* entry =
316 navigation_controller_->GetEntryAtIndex(index);
317 return CreateJavaNavigationEntry(env, entry, index);
320 base::android::ScopedJavaLocalRef<jobject>
321 NavigationControllerAndroid::GetPendingEntry(JNIEnv* env, jobject obj) {
322 content::NavigationEntry* entry = navigation_controller_->GetPendingEntry();
324 if (!entry)
325 return base::android::ScopedJavaLocalRef<jobject>();
327 return CreateJavaNavigationEntry(
328 env, entry, navigation_controller_->GetPendingEntryIndex());
331 jint NavigationControllerAndroid::GetLastCommittedEntryIndex(JNIEnv* env,
332 jobject obj) {
333 return navigation_controller_->GetLastCommittedEntryIndex();
336 jboolean NavigationControllerAndroid::RemoveEntryAtIndex(JNIEnv* env,
337 jobject obj,
338 jint index) {
339 return navigation_controller_->RemoveEntryAtIndex(index);
342 } // namespace content