1 // Copyright 2012 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 "android_webview/native/aw_contents.h"
9 #include "android_webview/browser/aw_browser_context.h"
10 #include "android_webview/browser/aw_browser_main_parts.h"
11 #include "android_webview/browser/aw_resource_context.h"
12 #include "android_webview/browser/browser_view_renderer.h"
13 #include "android_webview/browser/deferred_gpu_command_service.h"
14 #include "android_webview/browser/net_disk_cache_remover.h"
15 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
16 #include "android_webview/browser/scoped_app_gl_state_restore.h"
17 #include "android_webview/browser/shared_renderer_state.h"
18 #include "android_webview/common/aw_hit_test_data.h"
19 #include "android_webview/common/devtools_instrumentation.h"
20 #include "android_webview/native/aw_autofill_client.h"
21 #include "android_webview/native/aw_browser_dependency_factory.h"
22 #include "android_webview/native/aw_contents_client_bridge.h"
23 #include "android_webview/native/aw_contents_io_thread_client_impl.h"
24 #include "android_webview/native/aw_message_port_service_impl.h"
25 #include "android_webview/native/aw_pdf_exporter.h"
26 #include "android_webview/native/aw_picture.h"
27 #include "android_webview/native/aw_web_contents_delegate.h"
28 #include "android_webview/native/java_browser_view_renderer_helper.h"
29 #include "android_webview/native/permission/aw_permission_request.h"
30 #include "android_webview/native/permission/permission_request_handler.h"
31 #include "android_webview/native/permission/simple_permission_request.h"
32 #include "android_webview/native/state_serializer.h"
33 #include "android_webview/public/browser/draw_gl.h"
34 #include "base/android/jni_android.h"
35 #include "base/android/jni_array.h"
36 #include "base/android/jni_string.h"
37 #include "base/android/locale_utils.h"
38 #include "base/android/scoped_java_ref.h"
39 #include "base/atomicops.h"
40 #include "base/bind.h"
41 #include "base/callback.h"
42 #include "base/memory/memory_pressure_listener.h"
43 #include "base/message_loop/message_loop.h"
44 #include "base/pickle.h"
45 #include "base/strings/string16.h"
46 #include "base/supports_user_data.h"
47 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
48 #include "components/autofill/core/browser/autofill_manager.h"
49 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
50 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
51 #include "components/navigation_interception/intercept_navigation_delegate.h"
52 #include "content/public/browser/android/content_view_core.h"
53 #include "content/public/browser/android/synchronous_compositor.h"
54 #include "content/public/browser/browser_thread.h"
55 #include "content/public/browser/cert_store.h"
56 #include "content/public/browser/favicon_status.h"
57 #include "content/public/browser/message_port_provider.h"
58 #include "content/public/browser/navigation_entry.h"
59 #include "content/public/browser/render_frame_host.h"
60 #include "content/public/browser/render_process_host.h"
61 #include "content/public/browser/render_view_host.h"
62 #include "content/public/browser/web_contents.h"
63 #include "content/public/common/message_port_types.h"
64 #include "content/public/common/renderer_preferences.h"
65 #include "content/public/common/ssl_status.h"
66 #include "jni/AwContents_jni.h"
67 #include "net/base/auth.h"
68 #include "net/cert/x509_certificate.h"
69 #include "third_party/skia/include/core/SkPicture.h"
70 #include "ui/gfx/android/java_bitmap.h"
71 #include "ui/gfx/geometry/rect_f.h"
72 #include "ui/gfx/geometry/size.h"
73 #include "ui/gfx/image/image.h"
75 struct AwDrawSWFunctionTable
;
77 using autofill::ContentAutofillDriverFactory
;
78 using autofill::AutofillManager
;
79 using base::android::AttachCurrentThread
;
80 using base::android::ConvertJavaStringToUTF16
;
81 using base::android::ConvertJavaStringToUTF8
;
82 using base::android::ConvertUTF16ToJavaString
;
83 using base::android::ConvertUTF8ToJavaString
;
84 using base::android::JavaRef
;
85 using base::android::ScopedJavaGlobalRef
;
86 using base::android::ScopedJavaLocalRef
;
87 using data_reduction_proxy::DataReductionProxySettings
;
88 using navigation_interception::InterceptNavigationDelegate
;
89 using content::BrowserThread
;
90 using content::ContentViewCore
;
91 using content::WebContents
;
94 static AwDrawGLFunction DrawGLFunction
;
95 static void DrawGLFunction(long view_context
,
96 AwDrawGLInfo
* draw_info
,
98 // |view_context| is the value that was returned from the java
99 // AwContents.onPrepareDrawGL; this cast must match the code there.
100 reinterpret_cast<android_webview::SharedRendererState
*>(view_context
)
105 namespace android_webview
{
109 bool g_should_download_favicons
= false;
111 const void* kAwContentsUserDataKey
= &kAwContentsUserDataKey
;
113 class AwContentsUserData
: public base::SupportsUserData::Data
{
115 AwContentsUserData(AwContents
* ptr
) : contents_(ptr
) {}
117 static AwContents
* GetContents(WebContents
* web_contents
) {
120 AwContentsUserData
* data
= static_cast<AwContentsUserData
*>(
121 web_contents
->GetUserData(kAwContentsUserDataKey
));
122 return data
? data
->contents_
: NULL
;
126 AwContents
* contents_
;
129 base::subtle::Atomic32 g_instance_count
= 0;
131 void OnIoThreadClientReady(content::RenderFrameHost
* rfh
) {
132 int render_process_id
= rfh
->GetProcess()->GetID();
133 int render_frame_id
= rfh
->GetRoutingID();
134 AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
135 render_process_id
, render_frame_id
);
141 AwContents
* AwContents::FromWebContents(WebContents
* web_contents
) {
142 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
143 return AwContentsUserData::GetContents(web_contents
);
147 AwContents
* AwContents::FromID(int render_process_id
, int render_view_id
) {
148 const content::RenderViewHost
* rvh
=
149 content::RenderViewHost::FromID(render_process_id
, render_view_id
);
150 if (!rvh
) return NULL
;
151 content::WebContents
* web_contents
=
152 content::WebContents::FromRenderViewHost(rvh
);
153 if (!web_contents
) return NULL
;
154 return FromWebContents(web_contents
);
158 AwBrowserPermissionRequestDelegate
* AwBrowserPermissionRequestDelegate::FromID(
159 int render_process_id
, int render_view_id
) {
160 AwContents
* aw_contents
= AwContents::FromID(render_process_id
,
162 return implicit_cast
<AwBrowserPermissionRequestDelegate
*>(aw_contents
);
165 AwContents::AwContents(scoped_ptr
<WebContents
> web_contents
)
166 : web_contents_(web_contents
.Pass()),
167 browser_view_renderer_(
169 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)),
170 renderer_manager_key_(GLViewRendererManager::GetInstance()->NullKey()) {
171 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count
, 1);
172 icon_helper_
.reset(new IconHelper(web_contents_
.get()));
173 icon_helper_
->SetListener(this);
174 web_contents_
->SetUserData(android_webview::kAwContentsUserDataKey
,
175 new AwContentsUserData(this));
176 browser_view_renderer_
.RegisterWithWebContents(web_contents_
.get());
177 render_view_host_ext_
.reset(
178 new AwRenderViewHostExt(this, web_contents_
.get()));
180 permission_request_handler_
.reset(
181 new PermissionRequestHandler(this, web_contents_
.get()));
183 AwAutofillClient
* autofill_manager_delegate
=
184 AwAutofillClient::FromWebContents(web_contents_
.get());
185 InitDataReductionProxyIfNecessary();
186 if (autofill_manager_delegate
)
187 InitAutofillIfNecessary(autofill_manager_delegate
->GetSaveFormData());
188 content::SynchronousCompositor::SetClientForWebContents(
189 web_contents_
.get(), &browser_view_renderer_
);
192 void AwContents::SetJavaPeers(JNIEnv
* env
,
195 jobject web_contents_delegate
,
196 jobject contents_client_bridge
,
197 jobject io_thread_client
,
198 jobject intercept_navigation_delegate
) {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
200 // The |aw_content| param is technically spurious as it duplicates |obj| but
201 // is passed over anyway to make the binding more explicit.
202 java_ref_
= JavaObjectWeakGlobalRef(env
, aw_contents
);
204 web_contents_delegate_
.reset(
205 new AwWebContentsDelegate(env
, web_contents_delegate
));
206 web_contents_
->SetDelegate(web_contents_delegate_
.get());
208 contents_client_bridge_
.reset(
209 new AwContentsClientBridge(env
, contents_client_bridge
));
210 AwContentsClientBridgeBase::Associate(web_contents_
.get(),
211 contents_client_bridge_
.get());
213 AwContentsIoThreadClientImpl::Associate(
214 web_contents_
.get(), ScopedJavaLocalRef
<jobject
>(env
, io_thread_client
));
216 InterceptNavigationDelegate::Associate(
218 make_scoped_ptr(new InterceptNavigationDelegate(
219 env
, intercept_navigation_delegate
)));
221 // Finally, having setup the associations, release any deferred requests
222 web_contents_
->ForEachFrame(base::Bind(&OnIoThreadClientReady
));
225 void AwContents::SetSaveFormData(bool enabled
) {
226 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
227 InitAutofillIfNecessary(enabled
);
228 // We need to check for the existence, since autofill_manager_delegate
229 // may not be created when the setting is false.
230 if (AwAutofillClient::FromWebContents(web_contents_
.get())) {
231 AwAutofillClient::FromWebContents(web_contents_
.get())->
232 SetSaveFormData(enabled
);
236 void AwContents::InitDataReductionProxyIfNecessary() {
237 AwBrowserContext
* browser_context
=
238 AwBrowserContext::FromWebContents(web_contents_
.get());
239 browser_context
->CreateUserPrefServiceIfNecessary();
242 void AwContents::InitAutofillIfNecessary(bool enabled
) {
243 // Do not initialize if the feature is not enabled.
246 // Check if the autofill driver factory already exists.
247 content::WebContents
* web_contents
= web_contents_
.get();
248 if (ContentAutofillDriverFactory::FromWebContents(web_contents
))
251 AwBrowserContext::FromWebContents(web_contents
)->
252 CreateUserPrefServiceIfNecessary();
253 AwAutofillClient::CreateForWebContents(web_contents
);
254 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
255 web_contents
, AwAutofillClient::FromWebContents(web_contents
),
256 base::android::GetDefaultLocale(),
257 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER
);
260 void AwContents::SetAwAutofillClient(jobject client
) {
261 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
262 JNIEnv
* env
= AttachCurrentThread();
263 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
266 Java_AwContents_setAwAutofillClient(env
, obj
.obj(), client
);
269 AwContents::~AwContents() {
270 DCHECK_EQ(this, AwContents::FromWebContents(web_contents_
.get()));
271 content::SynchronousCompositor::SetClientForWebContents(web_contents_
.get(),
273 web_contents_
->RemoveUserData(kAwContentsUserDataKey
);
274 if (find_helper_
.get())
275 find_helper_
->SetListener(NULL
);
276 if (icon_helper_
.get())
277 icon_helper_
->SetListener(NULL
);
278 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count
, -1);
279 // When the last WebView is destroyed free all discardable memory allocated by
280 // Chromium, because the app process may continue to run for a long time
281 // without ever using another WebView.
282 if (base::subtle::NoBarrier_Load(&g_instance_count
) == 0) {
283 base::MemoryPressureListener::NotifyMemoryPressure(
284 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
288 base::android::ScopedJavaLocalRef
<jobject
>
289 AwContents::GetWebContents(JNIEnv
* env
, jobject obj
) {
290 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
291 DCHECK(web_contents_
);
293 return base::android::ScopedJavaLocalRef
<jobject
>();
295 return web_contents_
->GetJavaWebContents();
298 void AwContents::Destroy(JNIEnv
* env
, jobject obj
) {
303 static jlong
Init(JNIEnv
* env
, jclass
, jobject browser_context
) {
304 // TODO(joth): Use |browser_context| to get the native BrowserContext, rather
305 // than hard-code the default instance lookup here.
306 scoped_ptr
<WebContents
> web_contents(content::WebContents::Create(
307 content::WebContents::CreateParams(AwBrowserContext::GetDefault())));
308 // Return an 'uninitialized' instance; most work is deferred until the
309 // subsequent SetJavaPeers() call.
310 return reinterpret_cast<intptr_t>(new AwContents(web_contents
.Pass()));
313 static void SetAwDrawSWFunctionTable(JNIEnv
* env
, jclass
,
314 jlong function_table
) {
315 RasterHelperSetAwDrawSWFunctionTable(
316 reinterpret_cast<AwDrawSWFunctionTable
*>(function_table
));
319 static void SetAwDrawGLFunctionTable(JNIEnv
* env
, jclass
,
320 jlong function_table
) {
323 static jlong
GetAwDrawGLFunction(JNIEnv
* env
, jclass
) {
324 return reinterpret_cast<intptr_t>(&DrawGLFunction
);
328 jint
GetNativeInstanceCount(JNIEnv
* env
, jclass
) {
329 return base::subtle::NoBarrier_Load(&g_instance_count
);
332 jlong
AwContents::GetAwDrawGLViewContext(JNIEnv
* env
, jobject obj
) {
333 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
334 return reinterpret_cast<intptr_t>(
335 browser_view_renderer_
.GetAwDrawGLViewContext());
339 void DocumentHasImagesCallback(const ScopedJavaGlobalRef
<jobject
>& message
,
341 Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(),
347 void AwContents::DocumentHasImages(JNIEnv
* env
, jobject obj
, jobject message
) {
348 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
349 ScopedJavaGlobalRef
<jobject
> j_message
;
350 j_message
.Reset(env
, message
);
351 render_view_host_ext_
->DocumentHasImages(
352 base::Bind(&DocumentHasImagesCallback
, j_message
));
356 void GenerateMHTMLCallback(ScopedJavaGlobalRef
<jobject
>* callback
,
357 const base::FilePath
& path
, int64 size
) {
358 JNIEnv
* env
= AttachCurrentThread();
359 // Android files are UTF8, so the path conversion below is safe.
360 Java_AwContents_generateMHTMLCallback(
362 ConvertUTF8ToJavaString(env
, path
.AsUTF8Unsafe()).obj(),
363 size
, callback
->obj());
367 void AwContents::GenerateMHTML(JNIEnv
* env
, jobject obj
,
368 jstring jpath
, jobject callback
) {
369 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
370 ScopedJavaGlobalRef
<jobject
>* j_callback
= new ScopedJavaGlobalRef
<jobject
>();
371 j_callback
->Reset(env
, callback
);
372 base::FilePath
target_path(ConvertJavaStringToUTF8(env
, jpath
));
373 web_contents_
->GenerateMHTML(
375 base::Bind(&GenerateMHTMLCallback
, base::Owned(j_callback
), target_path
));
378 void AwContents::CreatePdfExporter(JNIEnv
* env
,
380 jobject pdfExporter
) {
382 new AwPdfExporter(env
,
384 web_contents_
.get()));
387 bool AwContents::OnReceivedHttpAuthRequest(const JavaRef
<jobject
>& handler
,
388 const std::string
& host
,
389 const std::string
& realm
) {
390 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
391 JNIEnv
* env
= AttachCurrentThread();
392 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
396 ScopedJavaLocalRef
<jstring
> jhost
= ConvertUTF8ToJavaString(env
, host
);
397 ScopedJavaLocalRef
<jstring
> jrealm
= ConvertUTF8ToJavaString(env
, realm
);
398 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
399 "onReceivedHttpAuthRequest");
400 Java_AwContents_onReceivedHttpAuthRequest(env
, obj
.obj(), handler
.obj(),
401 jhost
.obj(), jrealm
.obj());
405 void AwContents::SetOffscreenPreRaster(bool enabled
) {
406 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
407 browser_view_renderer_
.SetOffscreenPreRaster(enabled
);
410 void AwContents::AddVisitedLinks(JNIEnv
* env
,
412 jobjectArray jvisited_links
) {
413 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
414 std::vector
<base::string16
> visited_link_strings
;
415 base::android::AppendJavaStringArrayToStringVector(
416 env
, jvisited_links
, &visited_link_strings
);
418 std::vector
<GURL
> visited_link_gurls
;
419 std::vector
<base::string16
>::const_iterator itr
;
420 for (itr
= visited_link_strings
.begin(); itr
!= visited_link_strings
.end();
422 visited_link_gurls
.push_back(GURL(*itr
));
425 AwBrowserContext::FromWebContents(web_contents_
.get())
426 ->AddVisitedURLs(visited_link_gurls
);
429 bool RegisterAwContents(JNIEnv
* env
) {
430 return RegisterNativesImpl(env
);
435 void ShowGeolocationPromptHelperTask(const JavaObjectWeakGlobalRef
& java_ref
,
436 const GURL
& origin
) {
437 JNIEnv
* env
= AttachCurrentThread();
438 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref
.get(env
);
440 ScopedJavaLocalRef
<jstring
> j_origin(
441 ConvertUTF8ToJavaString(env
, origin
.spec()));
442 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
443 "onGeolocationPermissionsShowPrompt");
444 Java_AwContents_onGeolocationPermissionsShowPrompt(env
,
450 void ShowGeolocationPromptHelper(const JavaObjectWeakGlobalRef
& java_ref
,
451 const GURL
& origin
) {
452 JNIEnv
* env
= AttachCurrentThread();
453 if (java_ref
.get(env
).obj()) {
454 content::BrowserThread::PostTask(
455 content::BrowserThread::UI
,
457 base::Bind(&ShowGeolocationPromptHelperTask
,
463 } // anonymous namespace
465 void AwContents::ShowGeolocationPrompt(const GURL
& requesting_frame
,
466 base::Callback
<void(bool)> callback
) {
467 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
469 GURL origin
= requesting_frame
.GetOrigin();
470 bool show_prompt
= pending_geolocation_prompts_
.empty();
471 pending_geolocation_prompts_
.push_back(OriginCallback(origin
, callback
));
473 ShowGeolocationPromptHelper(java_ref_
, origin
);
478 void AwContents::InvokeGeolocationCallback(JNIEnv
* env
,
482 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
484 GURL
callback_origin(base::android::ConvertJavaStringToUTF16(env
, origin
));
485 if (callback_origin
.GetOrigin() ==
486 pending_geolocation_prompts_
.front().first
) {
487 pending_geolocation_prompts_
.front().second
.Run(value
);
488 pending_geolocation_prompts_
.pop_front();
489 if (!pending_geolocation_prompts_
.empty()) {
490 ShowGeolocationPromptHelper(java_ref_
,
491 pending_geolocation_prompts_
.front().first
);
496 void AwContents::HideGeolocationPrompt(const GURL
& origin
) {
497 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
498 bool removed_current_outstanding_callback
= false;
499 std::list
<OriginCallback
>::iterator it
= pending_geolocation_prompts_
.begin();
500 while (it
!= pending_geolocation_prompts_
.end()) {
501 if ((*it
).first
== origin
.GetOrigin()) {
502 if (it
== pending_geolocation_prompts_
.begin()) {
503 removed_current_outstanding_callback
= true;
505 it
= pending_geolocation_prompts_
.erase(it
);
511 if (removed_current_outstanding_callback
) {
512 JNIEnv
* env
= AttachCurrentThread();
513 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
515 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
516 "onGeolocationPermissionsHidePrompt");
517 Java_AwContents_onGeolocationPermissionsHidePrompt(env
, j_ref
.obj());
519 if (!pending_geolocation_prompts_
.empty()) {
520 ShowGeolocationPromptHelper(java_ref_
,
521 pending_geolocation_prompts_
.front().first
);
526 void AwContents::OnPermissionRequest(AwPermissionRequest
* request
) {
527 JNIEnv
* env
= AttachCurrentThread();
528 ScopedJavaLocalRef
<jobject
> j_request
= request
->CreateJavaPeer();
529 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
530 if (j_request
.is_null() || j_ref
.is_null()) {
531 permission_request_handler_
->CancelRequest(
532 request
->GetOrigin(), request
->GetResources());
536 Java_AwContents_onPermissionRequest(env
, j_ref
.obj(), j_request
.obj());
539 void AwContents::OnPermissionRequestCanceled(AwPermissionRequest
* request
) {
540 JNIEnv
* env
= AttachCurrentThread();
541 ScopedJavaLocalRef
<jobject
> j_request
= request
->GetJavaObject();
542 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
543 if (j_request
.is_null() || j_ref
.is_null())
546 Java_AwContents_onPermissionRequestCanceled(
547 env
, j_ref
.obj(), j_request
.obj());
550 void AwContents::PreauthorizePermission(
555 permission_request_handler_
->PreauthorizePermission(
556 GURL(base::android::ConvertJavaStringToUTF8(env
, origin
)), resources
);
559 void AwContents::RequestProtectedMediaIdentifierPermission(
561 const base::Callback
<void(bool)>& callback
) {
562 permission_request_handler_
->SendRequest(
563 scoped_ptr
<AwPermissionRequestDelegate
>(new SimplePermissionRequest(
564 origin
, AwPermissionRequest::ProtectedMediaId
, callback
)));
567 void AwContents::CancelProtectedMediaIdentifierPermissionRequests(
568 const GURL
& origin
) {
569 permission_request_handler_
->CancelRequest(
570 origin
, AwPermissionRequest::ProtectedMediaId
);
573 void AwContents::RequestGeolocationPermission(
575 const base::Callback
<void(bool)>& callback
) {
576 JNIEnv
* env
= AttachCurrentThread();
577 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
581 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env
, obj
.obj())) {
582 ShowGeolocationPrompt(origin
, callback
);
585 permission_request_handler_
->SendRequest(
586 scoped_ptr
<AwPermissionRequestDelegate
>(new SimplePermissionRequest(
587 origin
, AwPermissionRequest::Geolocation
, callback
)));
590 void AwContents::CancelGeolocationPermissionRequests(const GURL
& origin
) {
591 JNIEnv
* env
= AttachCurrentThread();
592 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
596 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env
, obj
.obj())) {
597 HideGeolocationPrompt(origin
);
600 permission_request_handler_
->CancelRequest(
601 origin
, AwPermissionRequest::Geolocation
);
604 void AwContents::FindAllAsync(JNIEnv
* env
, jobject obj
, jstring search_string
) {
605 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
606 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env
, search_string
));
609 void AwContents::FindNext(JNIEnv
* env
, jobject obj
, jboolean forward
) {
610 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
611 GetFindHelper()->FindNext(forward
);
614 void AwContents::ClearMatches(JNIEnv
* env
, jobject obj
) {
615 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
616 GetFindHelper()->ClearMatches();
619 void AwContents::ClearCache(
622 jboolean include_disk_files
) {
623 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
624 render_view_host_ext_
->ClearCache();
626 if (include_disk_files
) {
627 RemoveHttpDiskCache(web_contents_
->GetBrowserContext(),
628 web_contents_
->GetRoutingID());
632 FindHelper
* AwContents::GetFindHelper() {
633 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
634 if (!find_helper_
.get()) {
635 find_helper_
.reset(new FindHelper(web_contents_
.get()));
636 find_helper_
->SetListener(this);
638 return find_helper_
.get();
641 void AwContents::OnFindResultReceived(int active_ordinal
,
644 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
645 JNIEnv
* env
= AttachCurrentThread();
646 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
650 Java_AwContents_onFindResultReceived(
651 env
, obj
.obj(), active_ordinal
, match_count
, finished
);
654 bool AwContents::ShouldDownloadFavicon(const GURL
& icon_url
) {
655 return g_should_download_favicons
;
658 void AwContents::OnReceivedIcon(const GURL
& icon_url
, const SkBitmap
& bitmap
) {
659 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
660 JNIEnv
* env
= AttachCurrentThread();
661 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
665 content::NavigationEntry
* entry
=
666 web_contents_
->GetController().GetActiveEntry();
669 entry
->GetFavicon().valid
= true;
670 entry
->GetFavicon().url
= icon_url
;
671 entry
->GetFavicon().image
= gfx::Image::CreateFrom1xBitmap(bitmap
);
674 Java_AwContents_onReceivedIcon(
675 env
, obj
.obj(), gfx::ConvertToJavaBitmap(&bitmap
).obj());
678 void AwContents::OnReceivedTouchIconUrl(const std::string
& url
,
680 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
681 JNIEnv
* env
= AttachCurrentThread();
682 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
686 Java_AwContents_onReceivedTouchIconUrl(
687 env
, obj
.obj(), ConvertUTF8ToJavaString(env
, url
).obj(), precomposed
);
690 bool AwContents::RequestDrawGL(bool wait_for_completion
) {
691 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
692 JNIEnv
* env
= AttachCurrentThread();
693 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
696 return Java_AwContents_requestDrawGL(env
, obj
.obj(), wait_for_completion
);
699 void AwContents::PostInvalidate() {
700 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
701 JNIEnv
* env
= AttachCurrentThread();
702 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
704 Java_AwContents_postInvalidateOnAnimation(env
, obj
.obj());
707 void AwContents::OnNewPicture() {
708 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
709 JNIEnv
* env
= AttachCurrentThread();
710 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
711 if (!obj
.is_null()) {
712 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
714 Java_AwContents_onNewPicture(env
, obj
.obj());
718 base::android::ScopedJavaLocalRef
<jbyteArray
>
719 AwContents::GetCertificate(JNIEnv
* env
,
721 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
722 content::NavigationEntry
* entry
=
723 web_contents_
->GetController().GetActiveEntry();
725 return ScopedJavaLocalRef
<jbyteArray
>();
726 // Get the certificate
727 int cert_id
= entry
->GetSSL().cert_id
;
728 scoped_refptr
<net::X509Certificate
> cert
;
729 bool ok
= content::CertStore::GetInstance()->RetrieveCert(cert_id
, &cert
);
731 return ScopedJavaLocalRef
<jbyteArray
>();
733 // Convert the certificate and return it
734 std::string der_string
;
735 net::X509Certificate::GetDEREncoded(cert
->os_cert_handle(), &der_string
);
736 return base::android::ToJavaByteArray(env
,
737 reinterpret_cast<const uint8
*>(der_string
.data()), der_string
.length());
740 void AwContents::RequestNewHitTestDataAt(JNIEnv
* env
,
744 jfloat touch_major
) {
745 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
746 gfx::PointF
touch_center(x
, y
);
747 gfx::SizeF
touch_area(touch_major
, touch_major
);
748 render_view_host_ext_
->RequestNewHitTestDataAt(touch_center
, touch_area
);
751 void AwContents::UpdateLastHitTestData(JNIEnv
* env
, jobject obj
) {
752 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
753 if (!render_view_host_ext_
->HasNewHitTestData()) return;
755 const AwHitTestData
& data
= render_view_host_ext_
->GetLastHitTestData();
756 render_view_host_ext_
->MarkHitTestDataRead();
758 // Make sure to null the Java object if data is empty/invalid.
759 ScopedJavaLocalRef
<jstring
> extra_data_for_type
;
760 if (data
.extra_data_for_type
.length())
761 extra_data_for_type
= ConvertUTF8ToJavaString(
762 env
, data
.extra_data_for_type
);
764 ScopedJavaLocalRef
<jstring
> href
;
765 if (data
.href
.length())
766 href
= ConvertUTF16ToJavaString(env
, data
.href
);
768 ScopedJavaLocalRef
<jstring
> anchor_text
;
769 if (data
.anchor_text
.length())
770 anchor_text
= ConvertUTF16ToJavaString(env
, data
.anchor_text
);
772 ScopedJavaLocalRef
<jstring
> img_src
;
773 if (data
.img_src
.is_valid())
774 img_src
= ConvertUTF8ToJavaString(env
, data
.img_src
.spec());
776 Java_AwContents_updateHitTestData(env
,
779 extra_data_for_type
.obj(),
785 void AwContents::OnSizeChanged(JNIEnv
* env
, jobject obj
,
786 int w
, int h
, int ow
, int oh
) {
787 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
788 browser_view_renderer_
.OnSizeChanged(w
, h
);
791 void AwContents::SetViewVisibility(JNIEnv
* env
, jobject obj
, bool visible
) {
792 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
793 browser_view_renderer_
.SetViewVisibility(visible
);
796 void AwContents::SetWindowVisibility(JNIEnv
* env
, jobject obj
, bool visible
) {
797 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
798 browser_view_renderer_
.SetWindowVisibility(visible
);
801 void AwContents::SetIsPaused(JNIEnv
* env
, jobject obj
, bool paused
) {
802 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
803 browser_view_renderer_
.SetIsPaused(paused
);
804 ContentViewCore
* cvc
=
805 ContentViewCore::FromWebContents(web_contents_
.get());
807 cvc
->PauseOrResumeGeolocation(paused
);
811 void AwContents::OnAttachedToWindow(JNIEnv
* env
, jobject obj
, int w
, int h
) {
812 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
813 browser_view_renderer_
.OnAttachedToWindow(w
, h
);
816 void AwContents::OnDetachedFromWindow(JNIEnv
* env
, jobject obj
) {
817 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
818 browser_view_renderer_
.OnDetachedFromWindow();
821 void AwContents::DetachFunctorFromView() {
822 JNIEnv
* env
= AttachCurrentThread();
823 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
825 Java_AwContents_detachFunctorFromView(env
, obj
.obj());
828 base::android::ScopedJavaLocalRef
<jbyteArray
>
829 AwContents::GetOpaqueState(JNIEnv
* env
, jobject obj
) {
830 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
831 // Required optimization in WebViewClassic to not save any state if
832 // there has been no navigations.
833 if (!web_contents_
->GetController().GetEntryCount())
834 return ScopedJavaLocalRef
<jbyteArray
>();
837 if (!WriteToPickle(*web_contents_
, &pickle
)) {
838 return ScopedJavaLocalRef
<jbyteArray
>();
840 return base::android::ToJavaByteArray(env
,
841 reinterpret_cast<const uint8
*>(pickle
.data()), pickle
.size());
845 jboolean
AwContents::RestoreFromOpaqueState(
846 JNIEnv
* env
, jobject obj
, jbyteArray state
) {
847 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
848 // TODO(boliu): This copy can be optimized out if this is a performance
850 std::vector
<uint8
> state_vector
;
851 base::android::JavaByteArrayToByteVector(env
, state
, &state_vector
);
853 Pickle
pickle(reinterpret_cast<const char*>(state_vector
.begin()),
854 state_vector
.size());
855 PickleIterator
iterator(pickle
);
857 return RestoreFromPickle(&iterator
, web_contents_
.get());
860 bool AwContents::OnDraw(JNIEnv
* env
,
863 jboolean is_hardware_accelerated
,
869 jint visible_bottom
) {
870 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
871 gfx::Vector2d
scroll(scroll_x
, scroll_y
);
872 browser_view_renderer_
.PrepareToDraw(
873 scroll
, gfx::Rect(visible_left
, visible_top
, visible_right
- visible_left
,
874 visible_bottom
- visible_top
));
875 if (is_hardware_accelerated
&& browser_view_renderer_
.attached_to_window()) {
876 return browser_view_renderer_
.OnDrawHardware();
879 gfx::Size view_size
= browser_view_renderer_
.size();
880 if (view_size
.IsEmpty()) {
881 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
882 TRACE_EVENT_SCOPE_THREAD
);
886 // TODO(hush): Right now webview size is passed in as the auxiliary bitmap
887 // size, which might hurt performace (only for software draws with auxiliary
888 // bitmap). For better performance, get global visible rect, transform it
889 // from screen space to view space, then intersect with the webview in
890 // viewspace. Use the resulting rect as the auxiliary bitmap.
891 scoped_ptr
<SoftwareCanvasHolder
> canvas_holder
=
892 SoftwareCanvasHolder::Create(canvas
, scroll
, view_size
);
893 if (!canvas_holder
|| !canvas_holder
->GetCanvas()) {
894 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
895 TRACE_EVENT_SCOPE_THREAD
);
898 return browser_view_renderer_
.OnDrawSoftware(canvas_holder
->GetCanvas());
901 void AwContents::SetPendingWebContentsForPopup(
902 scoped_ptr
<content::WebContents
> pending
) {
903 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
904 if (pending_contents_
.get()) {
905 // TODO(benm): Support holding multiple pop up window requests.
906 LOG(WARNING
) << "Blocking popup window creation as an outstanding "
907 << "popup window is still pending.";
908 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, pending
.release());
911 pending_contents_
.reset(new AwContents(pending
.Pass()));
912 // Set dip_scale for pending contents, which is necessary for the later
913 // SynchronousCompositor and InputHandler setup.
914 pending_contents_
->SetDipScaleInternal(browser_view_renderer_
.dip_scale());
917 void AwContents::FocusFirstNode(JNIEnv
* env
, jobject obj
) {
918 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
919 web_contents_
->FocusThroughTabTraversal(false);
922 void AwContents::SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
) {
923 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
924 render_view_host_ext_
->SetBackgroundColor(color
);
927 jlong
AwContents::ReleasePopupAwContents(JNIEnv
* env
, jobject obj
) {
928 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
929 return reinterpret_cast<intptr_t>(pending_contents_
.release());
932 gfx::Point
AwContents::GetLocationOnScreen() {
933 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
934 JNIEnv
* env
= AttachCurrentThread();
935 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
938 std::vector
<int> location
;
939 base::android::JavaIntArrayToIntVector(
941 Java_AwContents_getLocationOnScreen(env
, obj
.obj()).obj(),
943 return gfx::Point(location
[0], location
[1]);
946 void AwContents::ScrollContainerViewTo(gfx::Vector2d new_value
) {
947 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
948 JNIEnv
* env
= AttachCurrentThread();
949 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
952 Java_AwContents_scrollContainerViewTo(
953 env
, obj
.obj(), new_value
.x(), new_value
.y());
956 bool AwContents::IsFlingActive() const {
957 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
958 JNIEnv
* env
= AttachCurrentThread();
959 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
962 return Java_AwContents_isFlingActive(env
, obj
.obj());
965 void AwContents::UpdateScrollState(gfx::Vector2d max_scroll_offset
,
966 gfx::SizeF contents_size_dip
,
967 float page_scale_factor
,
968 float min_page_scale_factor
,
969 float max_page_scale_factor
) {
970 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
971 JNIEnv
* env
= AttachCurrentThread();
972 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
975 Java_AwContents_updateScrollState(env
,
977 max_scroll_offset
.x(),
978 max_scroll_offset
.y(),
979 contents_size_dip
.width(),
980 contents_size_dip
.height(),
982 min_page_scale_factor
,
983 max_page_scale_factor
);
986 void AwContents::DidOverscroll(gfx::Vector2d overscroll_delta
) {
987 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
988 JNIEnv
* env
= AttachCurrentThread();
989 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
992 Java_AwContents_didOverscroll(
993 env
, obj
.obj(), overscroll_delta
.x(), overscroll_delta
.y());
996 void AwContents::SetDipScale(JNIEnv
* env
, jobject obj
, jfloat dip_scale
) {
997 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
998 SetDipScaleInternal(dip_scale
);
1001 void AwContents::SetDipScaleInternal(float dip_scale
) {
1002 browser_view_renderer_
.SetDipScale(dip_scale
);
1005 void AwContents::ScrollTo(JNIEnv
* env
, jobject obj
, jint x
, jint y
) {
1006 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1007 browser_view_renderer_
.ScrollTo(gfx::Vector2d(x
, y
));
1010 void AwContents::OnWebLayoutPageScaleFactorChanged(float page_scale_factor
) {
1011 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1012 JNIEnv
* env
= AttachCurrentThread();
1013 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
1016 Java_AwContents_onWebLayoutPageScaleFactorChanged(env
, obj
.obj(),
1020 void AwContents::OnWebLayoutContentsSizeChanged(
1021 const gfx::Size
& contents_size
) {
1022 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1023 JNIEnv
* env
= AttachCurrentThread();
1024 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
1027 Java_AwContents_onWebLayoutContentsSizeChanged(
1028 env
, obj
.obj(), contents_size
.width(), contents_size
.height());
1031 jlong
AwContents::CapturePicture(JNIEnv
* env
,
1035 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1036 return reinterpret_cast<intptr_t>(
1037 new AwPicture(browser_view_renderer_
.CapturePicture(width
, height
)));
1040 void AwContents::EnableOnNewPicture(JNIEnv
* env
,
1043 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1044 browser_view_renderer_
.EnableOnNewPicture(enabled
);
1048 void InvokeVisualStateCallback(const JavaObjectWeakGlobalRef
& java_ref
,
1050 ScopedJavaGlobalRef
<jobject
>* callback
,
1052 JNIEnv
* env
= AttachCurrentThread();
1053 ScopedJavaLocalRef
<jobject
> obj
= java_ref
.get(env
);
1056 Java_AwContents_invokeVisualStateCallback(
1057 env
, obj
.obj(), callback
->obj(), request_id
);
1061 void AwContents::InsertVisualStateCallback(
1062 JNIEnv
* env
, jobject obj
, long request_id
, jobject callback
) {
1063 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1064 ScopedJavaGlobalRef
<jobject
>* j_callback
= new ScopedJavaGlobalRef
<jobject
>();
1065 j_callback
->Reset(env
, callback
);
1066 web_contents_
->GetMainFrame()->InsertVisualStateCallback(
1067 base::Bind(&InvokeVisualStateCallback
, java_ref_
, request_id
,
1068 base::Owned(j_callback
)));
1071 void AwContents::ClearView(JNIEnv
* env
, jobject obj
) {
1072 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1073 browser_view_renderer_
.ClearView();
1076 void AwContents::SetExtraHeadersForUrl(JNIEnv
* env
, jobject obj
,
1077 jstring url
, jstring jextra_headers
) {
1078 std::string extra_headers
;
1080 extra_headers
= ConvertJavaStringToUTF8(env
, jextra_headers
);
1081 AwResourceContext
* resource_context
= static_cast<AwResourceContext
*>(
1082 AwBrowserContext::FromWebContents(web_contents_
.get())->
1083 GetResourceContext());
1084 resource_context
->SetExtraHeaders(GURL(ConvertJavaStringToUTF8(env
, url
)),
1088 void AwContents::SetJsOnlineProperty(JNIEnv
* env
,
1090 jboolean network_up
) {
1091 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1092 render_view_host_ext_
->SetJsOnlineProperty(network_up
);
1095 void AwContents::TrimMemory(JNIEnv
* env
,
1099 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1100 browser_view_renderer_
.TrimMemory(level
, visible
);
1103 // TODO(sgurun) add support for posting a frame whose name is known (only
1104 // main frame is supported at this time, see crbug.com/389721)
1105 void AwContents::PostMessageToFrame(JNIEnv
* env
, jobject obj
,
1106 jstring frame_name
, jstring message
, jstring target_origin
,
1107 jintArray sent_ports
) {
1109 // Use an empty source origin for android webview.
1110 base::string16 source_origin
;
1111 base::string16
j_target_origin(ConvertJavaStringToUTF16(env
, target_origin
));
1112 base::string16
j_message(ConvertJavaStringToUTF16(env
, message
));
1113 std::vector
<int> j_ports
;
1115 if (sent_ports
!= nullptr) {
1116 base::android::JavaIntArrayToIntVector(env
, sent_ports
, &j_ports
);
1117 BrowserThread::PostTask(
1120 base::Bind(&AwMessagePortServiceImpl::RemoveSentPorts
,
1121 base::Unretained(AwMessagePortServiceImpl::GetInstance()),
1124 std::vector
<content::TransferredMessagePort
> ports(j_ports
.size());
1125 for (size_t i
= 0; i
< j_ports
.size(); ++i
)
1126 ports
[i
].id
= j_ports
[i
];
1127 content::MessagePortProvider::PostMessageToFrame(web_contents_
.get(),
1134 scoped_refptr
<AwMessagePortMessageFilter
>
1135 AwContents::GetMessagePortMessageFilter() {
1136 // Create a message port message filter if necessary
1137 if (message_port_message_filter_
.get() == nullptr) {
1138 message_port_message_filter_
=
1139 new AwMessagePortMessageFilter(
1140 web_contents_
->GetMainFrame()->GetRoutingID());
1141 web_contents_
->GetRenderProcessHost()->AddFilter(
1142 message_port_message_filter_
.get());
1144 return message_port_message_filter_
;
1147 void AwContents::CreateMessageChannel(JNIEnv
* env
, jobject obj
,
1148 jobjectArray ports
) {
1150 AwMessagePortServiceImpl::GetInstance()->CreateMessageChannel(env
, ports
,
1151 GetMessagePortMessageFilter());
1154 void SetShouldDownloadFavicons(JNIEnv
* env
, jclass jclazz
) {
1155 g_should_download_favicons
= true;
1158 } // namespace android_webview