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/renderer_preferences.h"
64 #include "content/public/common/ssl_status.h"
65 #include "jni/AwContents_jni.h"
66 #include "net/base/auth.h"
67 #include "net/cert/x509_certificate.h"
68 #include "third_party/skia/include/core/SkPicture.h"
69 #include "ui/gfx/android/java_bitmap.h"
70 #include "ui/gfx/geometry/rect_f.h"
71 #include "ui/gfx/geometry/size.h"
72 #include "ui/gfx/image/image.h"
74 struct AwDrawSWFunctionTable
;
76 using autofill::ContentAutofillDriverFactory
;
77 using autofill::AutofillManager
;
78 using base::android::AttachCurrentThread
;
79 using base::android::ConvertJavaStringToUTF16
;
80 using base::android::ConvertJavaStringToUTF8
;
81 using base::android::ConvertUTF16ToJavaString
;
82 using base::android::ConvertUTF8ToJavaString
;
83 using base::android::JavaRef
;
84 using base::android::ScopedJavaGlobalRef
;
85 using base::android::ScopedJavaLocalRef
;
86 using data_reduction_proxy::DataReductionProxySettings
;
87 using navigation_interception::InterceptNavigationDelegate
;
88 using content::BrowserThread
;
89 using content::ContentViewCore
;
90 using content::WebContents
;
93 static AwDrawGLFunction DrawGLFunction
;
94 static void DrawGLFunction(long view_context
,
95 AwDrawGLInfo
* draw_info
,
97 // |view_context| is the value that was returned from the java
98 // AwContents.onPrepareDrawGL; this cast must match the code there.
99 reinterpret_cast<android_webview::SharedRendererState
*>(view_context
)
104 namespace android_webview
{
108 bool g_should_download_favicons
= false;
110 const void* kAwContentsUserDataKey
= &kAwContentsUserDataKey
;
112 class AwContentsUserData
: public base::SupportsUserData::Data
{
114 AwContentsUserData(AwContents
* ptr
) : contents_(ptr
) {}
116 static AwContents
* GetContents(WebContents
* web_contents
) {
119 AwContentsUserData
* data
= static_cast<AwContentsUserData
*>(
120 web_contents
->GetUserData(kAwContentsUserDataKey
));
121 return data
? data
->contents_
: NULL
;
125 AwContents
* contents_
;
128 base::subtle::Atomic32 g_instance_count
= 0;
130 void OnIoThreadClientReady(content::RenderFrameHost
* rfh
) {
131 int render_process_id
= rfh
->GetProcess()->GetID();
132 int render_frame_id
= rfh
->GetRoutingID();
133 AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
134 render_process_id
, render_frame_id
);
140 AwContents
* AwContents::FromWebContents(WebContents
* web_contents
) {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
142 return AwContentsUserData::GetContents(web_contents
);
146 AwContents
* AwContents::FromID(int render_process_id
, int render_view_id
) {
147 const content::RenderViewHost
* rvh
=
148 content::RenderViewHost::FromID(render_process_id
, render_view_id
);
149 if (!rvh
) return NULL
;
150 content::WebContents
* web_contents
=
151 content::WebContents::FromRenderViewHost(rvh
);
152 if (!web_contents
) return NULL
;
153 return FromWebContents(web_contents
);
157 AwBrowserPermissionRequestDelegate
* AwBrowserPermissionRequestDelegate::FromID(
158 int render_process_id
, int render_view_id
) {
159 AwContents
* aw_contents
= AwContents::FromID(render_process_id
,
161 return implicit_cast
<AwBrowserPermissionRequestDelegate
*>(aw_contents
);
164 AwContents::AwContents(scoped_ptr
<WebContents
> web_contents
)
165 : web_contents_(web_contents
.Pass()),
166 browser_view_renderer_(
168 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)),
169 renderer_manager_key_(GLViewRendererManager::GetInstance()->NullKey()) {
170 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count
, 1);
171 icon_helper_
.reset(new IconHelper(web_contents_
.get()));
172 icon_helper_
->SetListener(this);
173 web_contents_
->SetUserData(kAwContentsUserDataKey
,
174 new AwContentsUserData(this));
175 render_view_host_ext_
.reset(
176 new AwRenderViewHostExt(this, web_contents_
.get()));
178 permission_request_handler_
.reset(
179 new PermissionRequestHandler(this, web_contents_
.get()));
181 AwAutofillClient
* autofill_manager_delegate
=
182 AwAutofillClient::FromWebContents(web_contents_
.get());
183 InitDataReductionProxyIfNecessary();
184 if (autofill_manager_delegate
)
185 InitAutofillIfNecessary(autofill_manager_delegate
->GetSaveFormData());
186 content::SynchronousCompositor::SetClientForWebContents(
187 web_contents_
.get(), &browser_view_renderer_
);
190 void AwContents::SetJavaPeers(JNIEnv
* env
,
193 jobject web_contents_delegate
,
194 jobject contents_client_bridge
,
195 jobject io_thread_client
,
196 jobject intercept_navigation_delegate
) {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
198 // The |aw_content| param is technically spurious as it duplicates |obj| but
199 // is passed over anyway to make the binding more explicit.
200 java_ref_
= JavaObjectWeakGlobalRef(env
, aw_contents
);
202 web_contents_delegate_
.reset(
203 new AwWebContentsDelegate(env
, web_contents_delegate
));
204 web_contents_
->SetDelegate(web_contents_delegate_
.get());
206 contents_client_bridge_
.reset(
207 new AwContentsClientBridge(env
, contents_client_bridge
));
208 AwContentsClientBridgeBase::Associate(web_contents_
.get(),
209 contents_client_bridge_
.get());
211 AwContentsIoThreadClientImpl::Associate(
212 web_contents_
.get(), ScopedJavaLocalRef
<jobject
>(env
, io_thread_client
));
214 InterceptNavigationDelegate::Associate(
216 make_scoped_ptr(new InterceptNavigationDelegate(
217 env
, intercept_navigation_delegate
)));
219 // Finally, having setup the associations, release any deferred requests
220 web_contents_
->ForEachFrame(base::Bind(&OnIoThreadClientReady
));
223 void AwContents::SetSaveFormData(bool enabled
) {
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
225 InitAutofillIfNecessary(enabled
);
226 // We need to check for the existence, since autofill_manager_delegate
227 // may not be created when the setting is false.
228 if (AwAutofillClient::FromWebContents(web_contents_
.get())) {
229 AwAutofillClient::FromWebContents(web_contents_
.get())->
230 SetSaveFormData(enabled
);
234 void AwContents::InitDataReductionProxyIfNecessary() {
235 AwBrowserContext
* browser_context
=
236 AwBrowserContext::FromWebContents(web_contents_
.get());
237 browser_context
->CreateUserPrefServiceIfNecessary();
240 void AwContents::InitAutofillIfNecessary(bool enabled
) {
241 // Do not initialize if the feature is not enabled.
244 // Check if the autofill driver factory already exists.
245 content::WebContents
* web_contents
= web_contents_
.get();
246 if (ContentAutofillDriverFactory::FromWebContents(web_contents
))
249 AwBrowserContext::FromWebContents(web_contents
)->
250 CreateUserPrefServiceIfNecessary();
251 AwAutofillClient::CreateForWebContents(web_contents
);
252 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
253 web_contents
, AwAutofillClient::FromWebContents(web_contents
),
254 base::android::GetDefaultLocale(),
255 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER
);
258 void AwContents::SetAwAutofillClient(jobject client
) {
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
260 JNIEnv
* env
= AttachCurrentThread();
261 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
264 Java_AwContents_setAwAutofillClient(env
, obj
.obj(), client
);
267 AwContents::~AwContents() {
268 DCHECK_EQ(this, AwContents::FromWebContents(web_contents_
.get()));
269 content::SynchronousCompositor::SetClientForWebContents(web_contents_
.get(),
271 web_contents_
->RemoveUserData(kAwContentsUserDataKey
);
272 if (find_helper_
.get())
273 find_helper_
->SetListener(NULL
);
274 if (icon_helper_
.get())
275 icon_helper_
->SetListener(NULL
);
276 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count
, -1);
277 // When the last WebView is destroyed free all discardable memory allocated by
278 // Chromium, because the app process may continue to run for a long time
279 // without ever using another WebView.
280 if (base::subtle::NoBarrier_Load(&g_instance_count
) == 0) {
281 base::MemoryPressureListener::NotifyMemoryPressure(
282 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
286 base::android::ScopedJavaLocalRef
<jobject
>
287 AwContents::GetWebContents(JNIEnv
* env
, jobject obj
) {
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
289 DCHECK(web_contents_
);
291 return base::android::ScopedJavaLocalRef
<jobject
>();
293 return web_contents_
->GetJavaWebContents();
296 void AwContents::Destroy(JNIEnv
* env
, jobject obj
) {
301 static jlong
Init(JNIEnv
* env
, jclass
, jobject browser_context
) {
302 // TODO(joth): Use |browser_context| to get the native BrowserContext, rather
303 // than hard-code the default instance lookup here.
304 scoped_ptr
<WebContents
> web_contents(content::WebContents::Create(
305 content::WebContents::CreateParams(AwBrowserContext::GetDefault())));
306 // Return an 'uninitialized' instance; most work is deferred until the
307 // subsequent SetJavaPeers() call.
308 return reinterpret_cast<intptr_t>(new AwContents(web_contents
.Pass()));
311 static void SetAwDrawSWFunctionTable(JNIEnv
* env
, jclass
,
312 jlong function_table
) {
313 RasterHelperSetAwDrawSWFunctionTable(
314 reinterpret_cast<AwDrawSWFunctionTable
*>(function_table
));
317 static void SetAwDrawGLFunctionTable(JNIEnv
* env
, jclass
,
318 jlong function_table
) {
321 static jlong
GetAwDrawGLFunction(JNIEnv
* env
, jclass
) {
322 return reinterpret_cast<intptr_t>(&DrawGLFunction
);
326 jint
GetNativeInstanceCount(JNIEnv
* env
, jclass
) {
327 return base::subtle::NoBarrier_Load(&g_instance_count
);
330 jlong
AwContents::GetAwDrawGLViewContext(JNIEnv
* env
, jobject obj
) {
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
332 return reinterpret_cast<intptr_t>(
333 browser_view_renderer_
.GetAwDrawGLViewContext());
337 void DocumentHasImagesCallback(const ScopedJavaGlobalRef
<jobject
>& message
,
339 Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(),
345 void AwContents::DocumentHasImages(JNIEnv
* env
, jobject obj
, jobject message
) {
346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
347 ScopedJavaGlobalRef
<jobject
> j_message
;
348 j_message
.Reset(env
, message
);
349 render_view_host_ext_
->DocumentHasImages(
350 base::Bind(&DocumentHasImagesCallback
, j_message
));
354 void GenerateMHTMLCallback(ScopedJavaGlobalRef
<jobject
>* callback
,
355 const base::FilePath
& path
, int64 size
) {
356 JNIEnv
* env
= AttachCurrentThread();
357 // Android files are UTF8, so the path conversion below is safe.
358 Java_AwContents_generateMHTMLCallback(
360 ConvertUTF8ToJavaString(env
, path
.AsUTF8Unsafe()).obj(),
361 size
, callback
->obj());
365 void AwContents::GenerateMHTML(JNIEnv
* env
, jobject obj
,
366 jstring jpath
, jobject callback
) {
367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
368 ScopedJavaGlobalRef
<jobject
>* j_callback
= new ScopedJavaGlobalRef
<jobject
>();
369 j_callback
->Reset(env
, callback
);
370 base::FilePath
target_path(ConvertJavaStringToUTF8(env
, jpath
));
371 web_contents_
->GenerateMHTML(
373 base::Bind(&GenerateMHTMLCallback
, base::Owned(j_callback
), target_path
));
376 void AwContents::CreatePdfExporter(JNIEnv
* env
,
378 jobject pdfExporter
) {
380 new AwPdfExporter(env
,
382 web_contents_
.get()));
385 bool AwContents::OnReceivedHttpAuthRequest(const JavaRef
<jobject
>& handler
,
386 const std::string
& host
,
387 const std::string
& realm
) {
388 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
389 JNIEnv
* env
= AttachCurrentThread();
390 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
394 ScopedJavaLocalRef
<jstring
> jhost
= ConvertUTF8ToJavaString(env
, host
);
395 ScopedJavaLocalRef
<jstring
> jrealm
= ConvertUTF8ToJavaString(env
, realm
);
396 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
397 "onReceivedHttpAuthRequest");
398 Java_AwContents_onReceivedHttpAuthRequest(env
, obj
.obj(), handler
.obj(),
399 jhost
.obj(), jrealm
.obj());
403 void AwContents::AddVisitedLinks(JNIEnv
* env
,
405 jobjectArray jvisited_links
) {
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
407 std::vector
<base::string16
> visited_link_strings
;
408 base::android::AppendJavaStringArrayToStringVector(
409 env
, jvisited_links
, &visited_link_strings
);
411 std::vector
<GURL
> visited_link_gurls
;
412 std::vector
<base::string16
>::const_iterator itr
;
413 for (itr
= visited_link_strings
.begin(); itr
!= visited_link_strings
.end();
415 visited_link_gurls
.push_back(GURL(*itr
));
418 AwBrowserContext::FromWebContents(web_contents_
.get())
419 ->AddVisitedURLs(visited_link_gurls
);
422 bool RegisterAwContents(JNIEnv
* env
) {
423 return RegisterNativesImpl(env
);
428 void ShowGeolocationPromptHelperTask(const JavaObjectWeakGlobalRef
& java_ref
,
429 const GURL
& origin
) {
430 JNIEnv
* env
= AttachCurrentThread();
431 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref
.get(env
);
433 ScopedJavaLocalRef
<jstring
> j_origin(
434 ConvertUTF8ToJavaString(env
, origin
.spec()));
435 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
436 "onGeolocationPermissionsShowPrompt");
437 Java_AwContents_onGeolocationPermissionsShowPrompt(env
,
443 void ShowGeolocationPromptHelper(const JavaObjectWeakGlobalRef
& java_ref
,
444 const GURL
& origin
) {
445 JNIEnv
* env
= AttachCurrentThread();
446 if (java_ref
.get(env
).obj()) {
447 content::BrowserThread::PostTask(
448 content::BrowserThread::UI
,
450 base::Bind(&ShowGeolocationPromptHelperTask
,
456 } // anonymous namespace
458 void AwContents::ShowGeolocationPrompt(const GURL
& requesting_frame
,
459 base::Callback
<void(bool)> callback
) {
460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
462 GURL origin
= requesting_frame
.GetOrigin();
463 bool show_prompt
= pending_geolocation_prompts_
.empty();
464 pending_geolocation_prompts_
.push_back(OriginCallback(origin
, callback
));
466 ShowGeolocationPromptHelper(java_ref_
, origin
);
471 void AwContents::InvokeGeolocationCallback(JNIEnv
* env
,
475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
477 GURL
callback_origin(base::android::ConvertJavaStringToUTF16(env
, origin
));
478 if (callback_origin
.GetOrigin() ==
479 pending_geolocation_prompts_
.front().first
) {
480 pending_geolocation_prompts_
.front().second
.Run(value
);
481 pending_geolocation_prompts_
.pop_front();
482 if (!pending_geolocation_prompts_
.empty()) {
483 ShowGeolocationPromptHelper(java_ref_
,
484 pending_geolocation_prompts_
.front().first
);
489 void AwContents::HideGeolocationPrompt(const GURL
& origin
) {
490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
491 bool removed_current_outstanding_callback
= false;
492 std::list
<OriginCallback
>::iterator it
= pending_geolocation_prompts_
.begin();
493 while (it
!= pending_geolocation_prompts_
.end()) {
494 if ((*it
).first
== origin
.GetOrigin()) {
495 if (it
== pending_geolocation_prompts_
.begin()) {
496 removed_current_outstanding_callback
= true;
498 it
= pending_geolocation_prompts_
.erase(it
);
504 if (removed_current_outstanding_callback
) {
505 JNIEnv
* env
= AttachCurrentThread();
506 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
508 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
509 "onGeolocationPermissionsHidePrompt");
510 Java_AwContents_onGeolocationPermissionsHidePrompt(env
, j_ref
.obj());
512 if (!pending_geolocation_prompts_
.empty()) {
513 ShowGeolocationPromptHelper(java_ref_
,
514 pending_geolocation_prompts_
.front().first
);
519 void AwContents::OnPermissionRequest(AwPermissionRequest
* request
) {
520 JNIEnv
* env
= AttachCurrentThread();
521 ScopedJavaLocalRef
<jobject
> j_request
= request
->CreateJavaPeer();
522 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
523 if (j_request
.is_null() || j_ref
.is_null()) {
524 permission_request_handler_
->CancelRequest(
525 request
->GetOrigin(), request
->GetResources());
529 Java_AwContents_onPermissionRequest(env
, j_ref
.obj(), j_request
.obj());
532 void AwContents::OnPermissionRequestCanceled(AwPermissionRequest
* request
) {
533 JNIEnv
* env
= AttachCurrentThread();
534 ScopedJavaLocalRef
<jobject
> j_request
= request
->GetJavaObject();
535 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
536 if (j_request
.is_null() || j_ref
.is_null())
539 Java_AwContents_onPermissionRequestCanceled(
540 env
, j_ref
.obj(), j_request
.obj());
543 void AwContents::PreauthorizePermission(
548 permission_request_handler_
->PreauthorizePermission(
549 GURL(base::android::ConvertJavaStringToUTF8(env
, origin
)), resources
);
552 void AwContents::RequestProtectedMediaIdentifierPermission(
554 const base::Callback
<void(bool)>& callback
) {
555 permission_request_handler_
->SendRequest(
556 scoped_ptr
<AwPermissionRequestDelegate
>(new SimplePermissionRequest(
557 origin
, AwPermissionRequest::ProtectedMediaId
, callback
)));
560 void AwContents::CancelProtectedMediaIdentifierPermissionRequests(
561 const GURL
& origin
) {
562 permission_request_handler_
->CancelRequest(
563 origin
, AwPermissionRequest::ProtectedMediaId
);
566 void AwContents::RequestGeolocationPermission(
568 const base::Callback
<void(bool)>& callback
) {
569 JNIEnv
* env
= AttachCurrentThread();
570 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
574 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env
, obj
.obj())) {
575 ShowGeolocationPrompt(origin
, callback
);
578 permission_request_handler_
->SendRequest(
579 scoped_ptr
<AwPermissionRequestDelegate
>(new SimplePermissionRequest(
580 origin
, AwPermissionRequest::Geolocation
, callback
)));
583 void AwContents::CancelGeolocationPermissionRequests(const GURL
& origin
) {
584 JNIEnv
* env
= AttachCurrentThread();
585 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
589 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env
, obj
.obj())) {
590 HideGeolocationPrompt(origin
);
593 permission_request_handler_
->CancelRequest(
594 origin
, AwPermissionRequest::Geolocation
);
597 void AwContents::FindAllAsync(JNIEnv
* env
, jobject obj
, jstring search_string
) {
598 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
599 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env
, search_string
));
602 void AwContents::FindNext(JNIEnv
* env
, jobject obj
, jboolean forward
) {
603 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
604 GetFindHelper()->FindNext(forward
);
607 void AwContents::ClearMatches(JNIEnv
* env
, jobject obj
) {
608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
609 GetFindHelper()->ClearMatches();
612 void AwContents::ClearCache(
615 jboolean include_disk_files
) {
616 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
617 render_view_host_ext_
->ClearCache();
619 if (include_disk_files
) {
620 RemoveHttpDiskCache(web_contents_
->GetBrowserContext(),
621 web_contents_
->GetRoutingID());
625 FindHelper
* AwContents::GetFindHelper() {
626 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
627 if (!find_helper_
.get()) {
628 find_helper_
.reset(new FindHelper(web_contents_
.get()));
629 find_helper_
->SetListener(this);
631 return find_helper_
.get();
634 void AwContents::OnFindResultReceived(int active_ordinal
,
637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
638 JNIEnv
* env
= AttachCurrentThread();
639 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
643 Java_AwContents_onFindResultReceived(
644 env
, obj
.obj(), active_ordinal
, match_count
, finished
);
647 bool AwContents::ShouldDownloadFavicon(const GURL
& icon_url
) {
648 return g_should_download_favicons
;
651 void AwContents::OnReceivedIcon(const GURL
& icon_url
, const SkBitmap
& bitmap
) {
652 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
653 JNIEnv
* env
= AttachCurrentThread();
654 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
658 content::NavigationEntry
* entry
=
659 web_contents_
->GetController().GetActiveEntry();
662 entry
->GetFavicon().valid
= true;
663 entry
->GetFavicon().url
= icon_url
;
664 entry
->GetFavicon().image
= gfx::Image::CreateFrom1xBitmap(bitmap
);
667 Java_AwContents_onReceivedIcon(
668 env
, obj
.obj(), gfx::ConvertToJavaBitmap(&bitmap
).obj());
671 void AwContents::OnReceivedTouchIconUrl(const std::string
& url
,
673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
674 JNIEnv
* env
= AttachCurrentThread();
675 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
679 Java_AwContents_onReceivedTouchIconUrl(
680 env
, obj
.obj(), ConvertUTF8ToJavaString(env
, url
).obj(), precomposed
);
683 bool AwContents::RequestDrawGL(bool wait_for_completion
) {
684 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
685 JNIEnv
* env
= AttachCurrentThread();
686 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
689 return Java_AwContents_requestDrawGL(env
, obj
.obj(), wait_for_completion
);
692 void AwContents::PostInvalidate() {
693 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
694 JNIEnv
* env
= AttachCurrentThread();
695 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
697 Java_AwContents_postInvalidateOnAnimation(env
, obj
.obj());
700 void AwContents::OnNewPicture() {
701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
702 JNIEnv
* env
= AttachCurrentThread();
703 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
704 if (!obj
.is_null()) {
705 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
707 Java_AwContents_onNewPicture(env
, obj
.obj());
711 base::android::ScopedJavaLocalRef
<jbyteArray
>
712 AwContents::GetCertificate(JNIEnv
* env
,
714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
715 content::NavigationEntry
* entry
=
716 web_contents_
->GetController().GetActiveEntry();
718 return ScopedJavaLocalRef
<jbyteArray
>();
719 // Get the certificate
720 int cert_id
= entry
->GetSSL().cert_id
;
721 scoped_refptr
<net::X509Certificate
> cert
;
722 bool ok
= content::CertStore::GetInstance()->RetrieveCert(cert_id
, &cert
);
724 return ScopedJavaLocalRef
<jbyteArray
>();
726 // Convert the certificate and return it
727 std::string der_string
;
728 net::X509Certificate::GetDEREncoded(cert
->os_cert_handle(), &der_string
);
729 return base::android::ToJavaByteArray(env
,
730 reinterpret_cast<const uint8
*>(der_string
.data()), der_string
.length());
733 void AwContents::RequestNewHitTestDataAt(JNIEnv
* env
,
737 jfloat touch_major
) {
738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
739 gfx::PointF
touch_center(x
, y
);
740 gfx::SizeF
touch_area(touch_major
, touch_major
);
741 render_view_host_ext_
->RequestNewHitTestDataAt(touch_center
, touch_area
);
744 void AwContents::UpdateLastHitTestData(JNIEnv
* env
, jobject obj
) {
745 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
746 if (!render_view_host_ext_
->HasNewHitTestData()) return;
748 const AwHitTestData
& data
= render_view_host_ext_
->GetLastHitTestData();
749 render_view_host_ext_
->MarkHitTestDataRead();
751 // Make sure to null the Java object if data is empty/invalid.
752 ScopedJavaLocalRef
<jstring
> extra_data_for_type
;
753 if (data
.extra_data_for_type
.length())
754 extra_data_for_type
= ConvertUTF8ToJavaString(
755 env
, data
.extra_data_for_type
);
757 ScopedJavaLocalRef
<jstring
> href
;
758 if (data
.href
.length())
759 href
= ConvertUTF16ToJavaString(env
, data
.href
);
761 ScopedJavaLocalRef
<jstring
> anchor_text
;
762 if (data
.anchor_text
.length())
763 anchor_text
= ConvertUTF16ToJavaString(env
, data
.anchor_text
);
765 ScopedJavaLocalRef
<jstring
> img_src
;
766 if (data
.img_src
.is_valid())
767 img_src
= ConvertUTF8ToJavaString(env
, data
.img_src
.spec());
769 Java_AwContents_updateHitTestData(env
,
772 extra_data_for_type
.obj(),
778 void AwContents::OnSizeChanged(JNIEnv
* env
, jobject obj
,
779 int w
, int h
, int ow
, int oh
) {
780 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
781 browser_view_renderer_
.OnSizeChanged(w
, h
);
784 void AwContents::SetViewVisibility(JNIEnv
* env
, jobject obj
, bool visible
) {
785 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
786 browser_view_renderer_
.SetViewVisibility(visible
);
789 void AwContents::SetWindowVisibility(JNIEnv
* env
, jobject obj
, bool visible
) {
790 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
791 browser_view_renderer_
.SetWindowVisibility(visible
);
794 void AwContents::SetIsPaused(JNIEnv
* env
, jobject obj
, bool paused
) {
795 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
796 browser_view_renderer_
.SetIsPaused(paused
);
797 ContentViewCore
* cvc
=
798 ContentViewCore::FromWebContents(web_contents_
.get());
800 cvc
->PauseOrResumeGeolocation(paused
);
804 void AwContents::OnAttachedToWindow(JNIEnv
* env
, jobject obj
, int w
, int h
) {
805 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
806 browser_view_renderer_
.OnAttachedToWindow(w
, h
);
809 void AwContents::OnDetachedFromWindow(JNIEnv
* env
, jobject obj
) {
810 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
811 browser_view_renderer_
.OnDetachedFromWindow();
814 void AwContents::InvalidateOnFunctorDestroy() {
815 JNIEnv
* env
= AttachCurrentThread();
816 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
818 Java_AwContents_invalidateOnFunctorDestroy(env
, obj
.obj());
821 base::android::ScopedJavaLocalRef
<jbyteArray
>
822 AwContents::GetOpaqueState(JNIEnv
* env
, jobject obj
) {
823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
824 // Required optimization in WebViewClassic to not save any state if
825 // there has been no navigations.
826 if (!web_contents_
->GetController().GetEntryCount())
827 return ScopedJavaLocalRef
<jbyteArray
>();
830 if (!WriteToPickle(*web_contents_
, &pickle
)) {
831 return ScopedJavaLocalRef
<jbyteArray
>();
833 return base::android::ToJavaByteArray(env
,
834 reinterpret_cast<const uint8
*>(pickle
.data()), pickle
.size());
838 jboolean
AwContents::RestoreFromOpaqueState(
839 JNIEnv
* env
, jobject obj
, jbyteArray state
) {
840 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
841 // TODO(boliu): This copy can be optimized out if this is a performance
843 std::vector
<uint8
> state_vector
;
844 base::android::JavaByteArrayToByteVector(env
, state
, &state_vector
);
846 Pickle
pickle(reinterpret_cast<const char*>(state_vector
.begin()),
847 state_vector
.size());
848 PickleIterator
iterator(pickle
);
850 return RestoreFromPickle(&iterator
, web_contents_
.get());
853 bool AwContents::OnDraw(JNIEnv
* env
,
856 jboolean is_hardware_accelerated
,
862 jint visible_bottom
) {
863 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
864 gfx::Vector2d
scroll(scroll_x
, scroll_y
);
865 browser_view_renderer_
.PrepareToDraw(
866 scroll
, gfx::Rect(visible_left
, visible_top
, visible_right
- visible_left
,
867 visible_bottom
- visible_top
));
868 if (is_hardware_accelerated
&& browser_view_renderer_
.attached_to_window()) {
869 return browser_view_renderer_
.OnDrawHardware();
872 gfx::Size view_size
= browser_view_renderer_
.size();
873 if (view_size
.IsEmpty()) {
874 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
875 TRACE_EVENT_SCOPE_THREAD
);
879 // TODO(hush): Right now webview size is passed in as the auxiliary bitmap
880 // size, which might hurt performace (only for software draws with auxiliary
881 // bitmap). For better performance, get global visible rect, transform it
882 // from screen space to view space, then intersect with the webview in
883 // viewspace. Use the resulting rect as the auxiliary bitmap.
884 scoped_ptr
<SoftwareCanvasHolder
> canvas_holder
=
885 SoftwareCanvasHolder::Create(canvas
, scroll
, view_size
);
886 if (!canvas_holder
|| !canvas_holder
->GetCanvas()) {
887 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
888 TRACE_EVENT_SCOPE_THREAD
);
891 return browser_view_renderer_
.OnDrawSoftware(canvas_holder
->GetCanvas());
894 void AwContents::SetPendingWebContentsForPopup(
895 scoped_ptr
<content::WebContents
> pending
) {
896 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
897 if (pending_contents_
.get()) {
898 // TODO(benm): Support holding multiple pop up window requests.
899 LOG(WARNING
) << "Blocking popup window creation as an outstanding "
900 << "popup window is still pending.";
901 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, pending
.release());
904 pending_contents_
.reset(new AwContents(pending
.Pass()));
907 void AwContents::FocusFirstNode(JNIEnv
* env
, jobject obj
) {
908 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
909 web_contents_
->FocusThroughTabTraversal(false);
912 void AwContents::SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
) {
913 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
914 render_view_host_ext_
->SetBackgroundColor(color
);
917 jlong
AwContents::ReleasePopupAwContents(JNIEnv
* env
, jobject obj
) {
918 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
919 return reinterpret_cast<intptr_t>(pending_contents_
.release());
922 gfx::Point
AwContents::GetLocationOnScreen() {
923 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
924 JNIEnv
* env
= AttachCurrentThread();
925 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
928 std::vector
<int> location
;
929 base::android::JavaIntArrayToIntVector(
931 Java_AwContents_getLocationOnScreen(env
, obj
.obj()).obj(),
933 return gfx::Point(location
[0], location
[1]);
936 void AwContents::ScrollContainerViewTo(gfx::Vector2d new_value
) {
937 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
938 JNIEnv
* env
= AttachCurrentThread();
939 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
942 Java_AwContents_scrollContainerViewTo(
943 env
, obj
.obj(), new_value
.x(), new_value
.y());
946 bool AwContents::IsFlingActive() const {
947 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
948 JNIEnv
* env
= AttachCurrentThread();
949 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
952 return Java_AwContents_isFlingActive(env
, obj
.obj());
955 void AwContents::UpdateScrollState(gfx::Vector2d max_scroll_offset
,
956 gfx::SizeF contents_size_dip
,
957 float page_scale_factor
,
958 float min_page_scale_factor
,
959 float max_page_scale_factor
) {
960 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
961 JNIEnv
* env
= AttachCurrentThread();
962 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
965 Java_AwContents_updateScrollState(env
,
967 max_scroll_offset
.x(),
968 max_scroll_offset
.y(),
969 contents_size_dip
.width(),
970 contents_size_dip
.height(),
972 min_page_scale_factor
,
973 max_page_scale_factor
);
976 void AwContents::DidOverscroll(gfx::Vector2d overscroll_delta
) {
977 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
978 JNIEnv
* env
= AttachCurrentThread();
979 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
982 Java_AwContents_didOverscroll(
983 env
, obj
.obj(), overscroll_delta
.x(), overscroll_delta
.y());
986 const BrowserViewRenderer
* AwContents::GetBrowserViewRenderer() const {
987 return &browser_view_renderer_
;
990 void AwContents::SetDipScale(JNIEnv
* env
, jobject obj
, jfloat dip_scale
) {
991 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
992 browser_view_renderer_
.SetDipScale(dip_scale
);
995 void AwContents::ScrollTo(JNIEnv
* env
, jobject obj
, jint x
, jint y
) {
996 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
997 browser_view_renderer_
.ScrollTo(gfx::Vector2d(x
, y
));
1000 void AwContents::OnWebLayoutPageScaleFactorChanged(float page_scale_factor
) {
1001 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1002 JNIEnv
* env
= AttachCurrentThread();
1003 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
1006 Java_AwContents_onWebLayoutPageScaleFactorChanged(env
, obj
.obj(),
1010 void AwContents::OnWebLayoutContentsSizeChanged(
1011 const gfx::Size
& contents_size
) {
1012 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1013 JNIEnv
* env
= AttachCurrentThread();
1014 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
1017 Java_AwContents_onWebLayoutContentsSizeChanged(
1018 env
, obj
.obj(), contents_size
.width(), contents_size
.height());
1021 jlong
AwContents::CapturePicture(JNIEnv
* env
,
1025 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1026 return reinterpret_cast<intptr_t>(
1027 new AwPicture(browser_view_renderer_
.CapturePicture(width
, height
)));
1030 void AwContents::EnableOnNewPicture(JNIEnv
* env
,
1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1034 browser_view_renderer_
.EnableOnNewPicture(enabled
);
1037 void AwContents::ClearView(JNIEnv
* env
, jobject obj
) {
1038 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1039 browser_view_renderer_
.ClearView();
1042 void AwContents::SetExtraHeadersForUrl(JNIEnv
* env
, jobject obj
,
1043 jstring url
, jstring jextra_headers
) {
1044 std::string extra_headers
;
1046 extra_headers
= ConvertJavaStringToUTF8(env
, jextra_headers
);
1047 AwResourceContext
* resource_context
= static_cast<AwResourceContext
*>(
1048 AwBrowserContext::FromWebContents(web_contents_
.get())->
1049 GetResourceContext());
1050 resource_context
->SetExtraHeaders(GURL(ConvertJavaStringToUTF8(env
, url
)),
1054 void AwContents::SetJsOnlineProperty(JNIEnv
* env
,
1056 jboolean network_up
) {
1057 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1058 render_view_host_ext_
->SetJsOnlineProperty(network_up
);
1061 void AwContents::TrimMemory(JNIEnv
* env
,
1065 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1066 browser_view_renderer_
.TrimMemory(level
, visible
);
1069 // TODO(sgurun) add support for posting a frame whose name is known (only
1070 // main frame is supported at this time, see crbug.com/389721)
1071 void AwContents::PostMessageToFrame(JNIEnv
* env
, jobject obj
,
1072 jstring frame_name
, jstring message
, jstring source_origin
,
1073 jstring target_origin
, jintArray msgPorts
) {
1075 base::string16
j_source_origin(ConvertJavaStringToUTF16(env
, source_origin
));
1076 base::string16
j_target_origin(ConvertJavaStringToUTF16(env
, target_origin
));
1077 base::string16
j_message(ConvertJavaStringToUTF16(env
, message
));
1078 std::vector
<int> j_ports
;
1079 if (msgPorts
!= nullptr)
1080 base::android::JavaIntArrayToIntVector(env
, msgPorts
, &j_ports
);
1082 content::MessagePortProvider::PostMessageToFrame(web_contents_
.get(),
1089 scoped_refptr
<AwMessagePortMessageFilter
>
1090 AwContents::GetMessagePortMessageFilter() {
1091 // Create a message port message filter if necessary
1092 if (message_port_message_filter_
.get() == nullptr) {
1093 message_port_message_filter_
=
1094 new AwMessagePortMessageFilter(
1095 web_contents_
->GetMainFrame()->GetRoutingID());
1096 web_contents_
->GetRenderProcessHost()->AddFilter(
1097 message_port_message_filter_
.get());
1099 return message_port_message_filter_
;
1102 void AwContents::CreateMessageChannel(JNIEnv
* env
, jobject obj
,
1105 AwMessagePortServiceImpl::GetInstance()->CreateMessageChannel(env
, callback
,
1106 GetMessagePortMessageFilter());
1109 void SetShouldDownloadFavicons(JNIEnv
* env
, jclass jclazz
) {
1110 g_should_download_favicons
= true;
1113 } // namespace android_webview