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_pdf_exporter.h"
25 #include "android_webview/native/aw_picture.h"
26 #include "android_webview/native/aw_web_contents_delegate.h"
27 #include "android_webview/native/java_browser_view_renderer_helper.h"
28 #include "android_webview/native/permission/aw_permission_request.h"
29 #include "android_webview/native/permission/permission_request_handler.h"
30 #include "android_webview/native/permission/simple_permission_request.h"
31 #include "android_webview/native/state_serializer.h"
32 #include "android_webview/public/browser/draw_gl.h"
33 #include "base/android/jni_android.h"
34 #include "base/android/jni_array.h"
35 #include "base/android/jni_string.h"
36 #include "base/android/locale_utils.h"
37 #include "base/android/scoped_java_ref.h"
38 #include "base/atomicops.h"
39 #include "base/bind.h"
40 #include "base/callback.h"
41 #include "base/memory/memory_pressure_listener.h"
42 #include "base/message_loop/message_loop.h"
43 #include "base/pickle.h"
44 #include "base/strings/string16.h"
45 #include "base/supports_user_data.h"
46 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
47 #include "components/autofill/core/browser/autofill_manager.h"
48 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
49 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
50 #include "components/navigation_interception/intercept_navigation_delegate.h"
51 #include "content/public/browser/android/content_view_core.h"
52 #include "content/public/browser/android/synchronous_compositor.h"
53 #include "content/public/browser/browser_thread.h"
54 #include "content/public/browser/cert_store.h"
55 #include "content/public/browser/favicon_status.h"
56 #include "content/public/browser/message_port_provider.h"
57 #include "content/public/browser/navigation_entry.h"
58 #include "content/public/browser/render_frame_host.h"
59 #include "content/public/browser/render_process_host.h"
60 #include "content/public/browser/render_view_host.h"
61 #include "content/public/browser/web_contents.h"
62 #include "content/public/common/renderer_preferences.h"
63 #include "content/public/common/ssl_status.h"
64 #include "jni/AwContents_jni.h"
65 #include "net/base/auth.h"
66 #include "net/cert/x509_certificate.h"
67 #include "third_party/skia/include/core/SkPicture.h"
68 #include "ui/gfx/android/java_bitmap.h"
69 #include "ui/gfx/geometry/rect_f.h"
70 #include "ui/gfx/image/image.h"
71 #include "ui/gfx/size.h"
73 struct AwDrawSWFunctionTable
;
75 using autofill::ContentAutofillDriverFactory
;
76 using autofill::AutofillManager
;
77 using base::android::AttachCurrentThread
;
78 using base::android::ConvertJavaStringToUTF16
;
79 using base::android::ConvertJavaStringToUTF8
;
80 using base::android::ConvertUTF16ToJavaString
;
81 using base::android::ConvertUTF8ToJavaString
;
82 using base::android::JavaRef
;
83 using base::android::ScopedJavaGlobalRef
;
84 using base::android::ScopedJavaLocalRef
;
85 using data_reduction_proxy::DataReductionProxySettings
;
86 using navigation_interception::InterceptNavigationDelegate
;
87 using content::BrowserThread
;
88 using content::ContentViewCore
;
89 using content::WebContents
;
92 static AwDrawGLFunction DrawGLFunction
;
93 static void DrawGLFunction(long view_context
,
94 AwDrawGLInfo
* draw_info
,
96 // |view_context| is the value that was returned from the java
97 // AwContents.onPrepareDrawGL; this cast must match the code there.
98 reinterpret_cast<android_webview::SharedRendererState
*>(view_context
)
103 namespace android_webview
{
107 bool g_should_download_favicons
= false;
109 const void* kAwContentsUserDataKey
= &kAwContentsUserDataKey
;
111 class AwContentsUserData
: public base::SupportsUserData::Data
{
113 AwContentsUserData(AwContents
* ptr
) : contents_(ptr
) {}
115 static AwContents
* GetContents(WebContents
* web_contents
) {
118 AwContentsUserData
* data
= static_cast<AwContentsUserData
*>(
119 web_contents
->GetUserData(kAwContentsUserDataKey
));
120 return data
? data
->contents_
: NULL
;
124 AwContents
* contents_
;
127 base::subtle::Atomic32 g_instance_count
= 0;
129 void OnIoThreadClientReady(content::RenderFrameHost
* rfh
) {
130 int render_process_id
= rfh
->GetProcess()->GetID();
131 int render_frame_id
= rfh
->GetRoutingID();
132 AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
133 render_process_id
, render_frame_id
);
136 void OnMessageChannelCreated(ScopedJavaGlobalRef
<jobject
>* callback
,
139 JNIEnv
* env
= AttachCurrentThread();
140 Java_AwContents_onMessageChannelCreated(env
, *port1
, *port2
,
144 void PostMessageToFrameOnIOThread(WebContents
* web_contents
,
145 base::string16
* source_origin
,
146 base::string16
* target_origin
,
147 base::string16
* data
,
148 std::vector
<int>* ports
) {
149 content::MessagePortProvider::PostMessageToFrame(web_contents
,
150 *source_origin
, *target_origin
, *data
, *ports
);
156 AwContents
* AwContents::FromWebContents(WebContents
* web_contents
) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
158 return AwContentsUserData::GetContents(web_contents
);
162 AwContents
* AwContents::FromID(int render_process_id
, int render_view_id
) {
163 const content::RenderViewHost
* rvh
=
164 content::RenderViewHost::FromID(render_process_id
, render_view_id
);
165 if (!rvh
) return NULL
;
166 content::WebContents
* web_contents
=
167 content::WebContents::FromRenderViewHost(rvh
);
168 if (!web_contents
) return NULL
;
169 return FromWebContents(web_contents
);
173 AwBrowserPermissionRequestDelegate
* AwBrowserPermissionRequestDelegate::FromID(
174 int render_process_id
, int render_view_id
) {
175 AwContents
* aw_contents
= AwContents::FromID(render_process_id
,
177 return implicit_cast
<AwBrowserPermissionRequestDelegate
*>(aw_contents
);
180 AwContents::AwContents(scoped_ptr
<WebContents
> web_contents
)
181 : web_contents_(web_contents
.Pass()),
182 browser_view_renderer_(
184 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)),
185 renderer_manager_key_(GLViewRendererManager::GetInstance()->NullKey()) {
186 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count
, 1);
187 icon_helper_
.reset(new IconHelper(web_contents_
.get()));
188 icon_helper_
->SetListener(this);
189 web_contents_
->SetUserData(kAwContentsUserDataKey
,
190 new AwContentsUserData(this));
191 render_view_host_ext_
.reset(
192 new AwRenderViewHostExt(this, web_contents_
.get()));
194 permission_request_handler_
.reset(
195 new PermissionRequestHandler(this, web_contents_
.get()));
197 AwAutofillClient
* autofill_manager_delegate
=
198 AwAutofillClient::FromWebContents(web_contents_
.get());
199 InitDataReductionProxyIfNecessary();
200 if (autofill_manager_delegate
)
201 InitAutofillIfNecessary(autofill_manager_delegate
->GetSaveFormData());
202 content::SynchronousCompositor::SetClientForWebContents(
203 web_contents_
.get(), &browser_view_renderer_
);
206 void AwContents::SetJavaPeers(JNIEnv
* env
,
209 jobject web_contents_delegate
,
210 jobject contents_client_bridge
,
211 jobject io_thread_client
,
212 jobject intercept_navigation_delegate
) {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
214 // The |aw_content| param is technically spurious as it duplicates |obj| but
215 // is passed over anyway to make the binding more explicit.
216 java_ref_
= JavaObjectWeakGlobalRef(env
, aw_contents
);
218 web_contents_delegate_
.reset(
219 new AwWebContentsDelegate(env
, web_contents_delegate
));
220 web_contents_
->SetDelegate(web_contents_delegate_
.get());
222 contents_client_bridge_
.reset(
223 new AwContentsClientBridge(env
, contents_client_bridge
));
224 AwContentsClientBridgeBase::Associate(web_contents_
.get(),
225 contents_client_bridge_
.get());
227 AwContentsIoThreadClientImpl::Associate(
228 web_contents_
.get(), ScopedJavaLocalRef
<jobject
>(env
, io_thread_client
));
230 InterceptNavigationDelegate::Associate(
232 make_scoped_ptr(new InterceptNavigationDelegate(
233 env
, intercept_navigation_delegate
)));
235 // Finally, having setup the associations, release any deferred requests
236 web_contents_
->ForEachFrame(base::Bind(&OnIoThreadClientReady
));
239 void AwContents::SetSaveFormData(bool enabled
) {
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
241 InitAutofillIfNecessary(enabled
);
242 // We need to check for the existence, since autofill_manager_delegate
243 // may not be created when the setting is false.
244 if (AwAutofillClient::FromWebContents(web_contents_
.get())) {
245 AwAutofillClient::FromWebContents(web_contents_
.get())->
246 SetSaveFormData(enabled
);
250 void AwContents::InitDataReductionProxyIfNecessary() {
251 AwBrowserContext
* browser_context
=
252 AwBrowserContext::FromWebContents(web_contents_
.get());
253 browser_context
->CreateUserPrefServiceIfNecessary();
256 void AwContents::InitAutofillIfNecessary(bool enabled
) {
257 // Do not initialize if the feature is not enabled.
260 // Check if the autofill driver factory already exists.
261 content::WebContents
* web_contents
= web_contents_
.get();
262 if (ContentAutofillDriverFactory::FromWebContents(web_contents
))
265 AwBrowserContext::FromWebContents(web_contents
)->
266 CreateUserPrefServiceIfNecessary();
267 AwAutofillClient::CreateForWebContents(web_contents
);
268 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
269 web_contents
, AwAutofillClient::FromWebContents(web_contents
),
270 base::android::GetDefaultLocale(),
271 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER
);
274 void AwContents::SetAwAutofillClient(jobject client
) {
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
276 JNIEnv
* env
= AttachCurrentThread();
277 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
280 Java_AwContents_setAwAutofillClient(env
, obj
.obj(), client
);
283 AwContents::~AwContents() {
284 DCHECK_EQ(this, AwContents::FromWebContents(web_contents_
.get()));
285 content::SynchronousCompositor::SetClientForWebContents(web_contents_
.get(),
287 web_contents_
->RemoveUserData(kAwContentsUserDataKey
);
288 if (find_helper_
.get())
289 find_helper_
->SetListener(NULL
);
290 if (icon_helper_
.get())
291 icon_helper_
->SetListener(NULL
);
292 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count
, -1);
293 // When the last WebView is destroyed free all discardable memory allocated by
294 // Chromium, because the app process may continue to run for a long time
295 // without ever using another WebView.
296 if (base::subtle::NoBarrier_Load(&g_instance_count
) == 0) {
297 base::MemoryPressureListener::NotifyMemoryPressure(
298 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
302 jlong
AwContents::GetWebContents(JNIEnv
* env
, jobject obj
) {
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
304 DCHECK(web_contents_
);
305 return reinterpret_cast<intptr_t>(web_contents_
.get());
308 void AwContents::Destroy(JNIEnv
* env
, jobject obj
) {
313 static jlong
Init(JNIEnv
* env
, jclass
, jobject browser_context
) {
314 // TODO(joth): Use |browser_context| to get the native BrowserContext, rather
315 // than hard-code the default instance lookup here.
316 scoped_ptr
<WebContents
> web_contents(content::WebContents::Create(
317 content::WebContents::CreateParams(AwBrowserContext::GetDefault())));
318 // Return an 'uninitialized' instance; most work is deferred until the
319 // subsequent SetJavaPeers() call.
320 return reinterpret_cast<intptr_t>(new AwContents(web_contents
.Pass()));
323 static void SetAwDrawSWFunctionTable(JNIEnv
* env
, jclass
,
324 jlong function_table
) {
325 RasterHelperSetAwDrawSWFunctionTable(
326 reinterpret_cast<AwDrawSWFunctionTable
*>(function_table
));
329 static void SetAwDrawGLFunctionTable(JNIEnv
* env
, jclass
,
330 jlong function_table
) {
333 static jlong
GetAwDrawGLFunction(JNIEnv
* env
, jclass
) {
334 return reinterpret_cast<intptr_t>(&DrawGLFunction
);
338 jint
GetNativeInstanceCount(JNIEnv
* env
, jclass
) {
339 return base::subtle::NoBarrier_Load(&g_instance_count
);
342 jlong
AwContents::GetAwDrawGLViewContext(JNIEnv
* env
, jobject obj
) {
343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
344 return reinterpret_cast<intptr_t>(
345 browser_view_renderer_
.GetAwDrawGLViewContext());
349 void DocumentHasImagesCallback(const ScopedJavaGlobalRef
<jobject
>& message
,
351 Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(),
357 void AwContents::DocumentHasImages(JNIEnv
* env
, jobject obj
, jobject message
) {
358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
359 ScopedJavaGlobalRef
<jobject
> j_message
;
360 j_message
.Reset(env
, message
);
361 render_view_host_ext_
->DocumentHasImages(
362 base::Bind(&DocumentHasImagesCallback
, j_message
));
366 void GenerateMHTMLCallback(ScopedJavaGlobalRef
<jobject
>* callback
,
367 const base::FilePath
& path
, int64 size
) {
368 JNIEnv
* env
= AttachCurrentThread();
369 // Android files are UTF8, so the path conversion below is safe.
370 Java_AwContents_generateMHTMLCallback(
372 ConvertUTF8ToJavaString(env
, path
.AsUTF8Unsafe()).obj(),
373 size
, callback
->obj());
377 void AwContents::GenerateMHTML(JNIEnv
* env
, jobject obj
,
378 jstring jpath
, jobject callback
) {
379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
380 ScopedJavaGlobalRef
<jobject
>* j_callback
= new ScopedJavaGlobalRef
<jobject
>();
381 j_callback
->Reset(env
, callback
);
382 base::FilePath
target_path(ConvertJavaStringToUTF8(env
, jpath
));
383 web_contents_
->GenerateMHTML(
385 base::Bind(&GenerateMHTMLCallback
, base::Owned(j_callback
), target_path
));
388 void AwContents::CreatePdfExporter(JNIEnv
* env
,
390 jobject pdfExporter
) {
392 new AwPdfExporter(env
,
394 web_contents_
.get()));
397 bool AwContents::OnReceivedHttpAuthRequest(const JavaRef
<jobject
>& handler
,
398 const std::string
& host
,
399 const std::string
& realm
) {
400 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
401 JNIEnv
* env
= AttachCurrentThread();
402 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
406 ScopedJavaLocalRef
<jstring
> jhost
= ConvertUTF8ToJavaString(env
, host
);
407 ScopedJavaLocalRef
<jstring
> jrealm
= ConvertUTF8ToJavaString(env
, realm
);
408 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
409 "onReceivedHttpAuthRequest");
410 Java_AwContents_onReceivedHttpAuthRequest(env
, obj
.obj(), handler
.obj(),
411 jhost
.obj(), jrealm
.obj());
415 void AwContents::AddVisitedLinks(JNIEnv
* env
,
417 jobjectArray jvisited_links
) {
418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
419 std::vector
<base::string16
> visited_link_strings
;
420 base::android::AppendJavaStringArrayToStringVector(
421 env
, jvisited_links
, &visited_link_strings
);
423 std::vector
<GURL
> visited_link_gurls
;
424 std::vector
<base::string16
>::const_iterator itr
;
425 for (itr
= visited_link_strings
.begin(); itr
!= visited_link_strings
.end();
427 visited_link_gurls
.push_back(GURL(*itr
));
430 AwBrowserContext::FromWebContents(web_contents_
.get())
431 ->AddVisitedURLs(visited_link_gurls
);
434 bool RegisterAwContents(JNIEnv
* env
) {
435 return RegisterNativesImpl(env
);
440 void ShowGeolocationPromptHelperTask(const JavaObjectWeakGlobalRef
& java_ref
,
441 const GURL
& origin
) {
442 JNIEnv
* env
= AttachCurrentThread();
443 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref
.get(env
);
445 ScopedJavaLocalRef
<jstring
> j_origin(
446 ConvertUTF8ToJavaString(env
, origin
.spec()));
447 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
448 "onGeolocationPermissionsShowPrompt");
449 Java_AwContents_onGeolocationPermissionsShowPrompt(env
,
455 void ShowGeolocationPromptHelper(const JavaObjectWeakGlobalRef
& java_ref
,
456 const GURL
& origin
) {
457 JNIEnv
* env
= AttachCurrentThread();
458 if (java_ref
.get(env
).obj()) {
459 content::BrowserThread::PostTask(
460 content::BrowserThread::UI
,
462 base::Bind(&ShowGeolocationPromptHelperTask
,
468 } // anonymous namespace
470 void AwContents::ShowGeolocationPrompt(const GURL
& requesting_frame
,
471 base::Callback
<void(bool)> callback
) {
472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
474 GURL origin
= requesting_frame
.GetOrigin();
475 bool show_prompt
= pending_geolocation_prompts_
.empty();
476 pending_geolocation_prompts_
.push_back(OriginCallback(origin
, callback
));
478 ShowGeolocationPromptHelper(java_ref_
, origin
);
483 void AwContents::InvokeGeolocationCallback(JNIEnv
* env
,
487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
489 GURL
callback_origin(base::android::ConvertJavaStringToUTF16(env
, origin
));
490 if (callback_origin
.GetOrigin() ==
491 pending_geolocation_prompts_
.front().first
) {
492 pending_geolocation_prompts_
.front().second
.Run(value
);
493 pending_geolocation_prompts_
.pop_front();
494 if (!pending_geolocation_prompts_
.empty()) {
495 ShowGeolocationPromptHelper(java_ref_
,
496 pending_geolocation_prompts_
.front().first
);
501 void AwContents::HideGeolocationPrompt(const GURL
& origin
) {
502 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
503 bool removed_current_outstanding_callback
= false;
504 std::list
<OriginCallback
>::iterator it
= pending_geolocation_prompts_
.begin();
505 while (it
!= pending_geolocation_prompts_
.end()) {
506 if ((*it
).first
== origin
.GetOrigin()) {
507 if (it
== pending_geolocation_prompts_
.begin()) {
508 removed_current_outstanding_callback
= true;
510 it
= pending_geolocation_prompts_
.erase(it
);
516 if (removed_current_outstanding_callback
) {
517 JNIEnv
* env
= AttachCurrentThread();
518 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
520 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
521 "onGeolocationPermissionsHidePrompt");
522 Java_AwContents_onGeolocationPermissionsHidePrompt(env
, j_ref
.obj());
524 if (!pending_geolocation_prompts_
.empty()) {
525 ShowGeolocationPromptHelper(java_ref_
,
526 pending_geolocation_prompts_
.front().first
);
531 void AwContents::OnPermissionRequest(AwPermissionRequest
* request
) {
532 JNIEnv
* env
= AttachCurrentThread();
533 ScopedJavaLocalRef
<jobject
> j_request
= request
->CreateJavaPeer();
534 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
535 if (j_request
.is_null() || j_ref
.is_null()) {
536 permission_request_handler_
->CancelRequest(
537 request
->GetOrigin(), request
->GetResources());
541 Java_AwContents_onPermissionRequest(env
, j_ref
.obj(), j_request
.obj());
544 void AwContents::OnPermissionRequestCanceled(AwPermissionRequest
* request
) {
545 JNIEnv
* env
= AttachCurrentThread();
546 ScopedJavaLocalRef
<jobject
> j_request
= request
->GetJavaObject();
547 ScopedJavaLocalRef
<jobject
> j_ref
= java_ref_
.get(env
);
548 if (j_request
.is_null() || j_ref
.is_null())
551 Java_AwContents_onPermissionRequestCanceled(
552 env
, j_ref
.obj(), j_request
.obj());
555 void AwContents::PreauthorizePermission(
560 permission_request_handler_
->PreauthorizePermission(
561 GURL(base::android::ConvertJavaStringToUTF8(env
, origin
)), resources
);
564 void AwContents::RequestProtectedMediaIdentifierPermission(
566 const base::Callback
<void(bool)>& callback
) {
567 permission_request_handler_
->SendRequest(
568 scoped_ptr
<AwPermissionRequestDelegate
>(new SimplePermissionRequest(
569 origin
, AwPermissionRequest::ProtectedMediaId
, callback
)));
572 void AwContents::CancelProtectedMediaIdentifierPermissionRequests(
573 const GURL
& origin
) {
574 permission_request_handler_
->CancelRequest(
575 origin
, AwPermissionRequest::ProtectedMediaId
);
578 void AwContents::RequestGeolocationPermission(
580 const base::Callback
<void(bool)>& callback
) {
581 JNIEnv
* env
= AttachCurrentThread();
582 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
586 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env
, obj
.obj())) {
587 ShowGeolocationPrompt(origin
, callback
);
590 permission_request_handler_
->SendRequest(
591 scoped_ptr
<AwPermissionRequestDelegate
>(new SimplePermissionRequest(
592 origin
, AwPermissionRequest::Geolocation
, callback
)));
595 void AwContents::CancelGeolocationPermissionRequests(const GURL
& origin
) {
596 JNIEnv
* env
= AttachCurrentThread();
597 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
601 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env
, obj
.obj())) {
602 HideGeolocationPrompt(origin
);
605 permission_request_handler_
->CancelRequest(
606 origin
, AwPermissionRequest::Geolocation
);
609 void AwContents::FindAllAsync(JNIEnv
* env
, jobject obj
, jstring search_string
) {
610 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
611 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env
, search_string
));
614 void AwContents::FindNext(JNIEnv
* env
, jobject obj
, jboolean forward
) {
615 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
616 GetFindHelper()->FindNext(forward
);
619 void AwContents::ClearMatches(JNIEnv
* env
, jobject obj
) {
620 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
621 GetFindHelper()->ClearMatches();
624 void AwContents::ClearCache(
627 jboolean include_disk_files
) {
628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
629 render_view_host_ext_
->ClearCache();
631 if (include_disk_files
) {
632 RemoveHttpDiskCache(web_contents_
->GetBrowserContext(),
633 web_contents_
->GetRoutingID());
637 FindHelper
* AwContents::GetFindHelper() {
638 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
639 if (!find_helper_
.get()) {
640 find_helper_
.reset(new FindHelper(web_contents_
.get()));
641 find_helper_
->SetListener(this);
643 return find_helper_
.get();
646 void AwContents::OnFindResultReceived(int active_ordinal
,
649 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
650 JNIEnv
* env
= AttachCurrentThread();
651 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
655 Java_AwContents_onFindResultReceived(
656 env
, obj
.obj(), active_ordinal
, match_count
, finished
);
659 bool AwContents::ShouldDownloadFavicon(const GURL
& icon_url
) {
660 return g_should_download_favicons
;
663 void AwContents::OnReceivedIcon(const GURL
& icon_url
, const SkBitmap
& bitmap
) {
664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
665 JNIEnv
* env
= AttachCurrentThread();
666 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
670 content::NavigationEntry
* entry
=
671 web_contents_
->GetController().GetActiveEntry();
674 entry
->GetFavicon().valid
= true;
675 entry
->GetFavicon().url
= icon_url
;
676 entry
->GetFavicon().image
= gfx::Image::CreateFrom1xBitmap(bitmap
);
679 Java_AwContents_onReceivedIcon(
680 env
, obj
.obj(), gfx::ConvertToJavaBitmap(&bitmap
).obj());
683 void AwContents::OnReceivedTouchIconUrl(const std::string
& url
,
685 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
686 JNIEnv
* env
= AttachCurrentThread();
687 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
691 Java_AwContents_onReceivedTouchIconUrl(
692 env
, obj
.obj(), ConvertUTF8ToJavaString(env
, url
).obj(), precomposed
);
695 bool AwContents::RequestDrawGL(bool wait_for_completion
) {
696 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
697 JNIEnv
* env
= AttachCurrentThread();
698 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
701 return Java_AwContents_requestDrawGL(env
, obj
.obj(), wait_for_completion
);
704 void AwContents::PostInvalidate() {
705 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
706 JNIEnv
* env
= AttachCurrentThread();
707 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
709 Java_AwContents_postInvalidateOnAnimation(env
, obj
.obj());
712 void AwContents::OnNewPicture() {
713 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
714 JNIEnv
* env
= AttachCurrentThread();
715 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
716 if (!obj
.is_null()) {
717 devtools_instrumentation::ScopedEmbedderCallbackTask
embedder_callback(
719 Java_AwContents_onNewPicture(env
, obj
.obj());
723 base::android::ScopedJavaLocalRef
<jbyteArray
>
724 AwContents::GetCertificate(JNIEnv
* env
,
726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
727 content::NavigationEntry
* entry
=
728 web_contents_
->GetController().GetActiveEntry();
730 return ScopedJavaLocalRef
<jbyteArray
>();
731 // Get the certificate
732 int cert_id
= entry
->GetSSL().cert_id
;
733 scoped_refptr
<net::X509Certificate
> cert
;
734 bool ok
= content::CertStore::GetInstance()->RetrieveCert(cert_id
, &cert
);
736 return ScopedJavaLocalRef
<jbyteArray
>();
738 // Convert the certificate and return it
739 std::string der_string
;
740 net::X509Certificate::GetDEREncoded(cert
->os_cert_handle(), &der_string
);
741 return base::android::ToJavaByteArray(env
,
742 reinterpret_cast<const uint8
*>(der_string
.data()), der_string
.length());
745 void AwContents::RequestNewHitTestDataAt(JNIEnv
* env
,
749 jfloat touch_major
) {
750 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
751 gfx::PointF
touch_center(x
, y
);
752 gfx::SizeF
touch_area(touch_major
, touch_major
);
753 render_view_host_ext_
->RequestNewHitTestDataAt(touch_center
, touch_area
);
756 void AwContents::UpdateLastHitTestData(JNIEnv
* env
, jobject obj
) {
757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
758 if (!render_view_host_ext_
->HasNewHitTestData()) return;
760 const AwHitTestData
& data
= render_view_host_ext_
->GetLastHitTestData();
761 render_view_host_ext_
->MarkHitTestDataRead();
763 // Make sure to null the Java object if data is empty/invalid.
764 ScopedJavaLocalRef
<jstring
> extra_data_for_type
;
765 if (data
.extra_data_for_type
.length())
766 extra_data_for_type
= ConvertUTF8ToJavaString(
767 env
, data
.extra_data_for_type
);
769 ScopedJavaLocalRef
<jstring
> href
;
770 if (data
.href
.length())
771 href
= ConvertUTF16ToJavaString(env
, data
.href
);
773 ScopedJavaLocalRef
<jstring
> anchor_text
;
774 if (data
.anchor_text
.length())
775 anchor_text
= ConvertUTF16ToJavaString(env
, data
.anchor_text
);
777 ScopedJavaLocalRef
<jstring
> img_src
;
778 if (data
.img_src
.is_valid())
779 img_src
= ConvertUTF8ToJavaString(env
, data
.img_src
.spec());
781 Java_AwContents_updateHitTestData(env
,
784 extra_data_for_type
.obj(),
790 void AwContents::OnSizeChanged(JNIEnv
* env
, jobject obj
,
791 int w
, int h
, int ow
, int oh
) {
792 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
793 browser_view_renderer_
.OnSizeChanged(w
, h
);
796 void AwContents::SetViewVisibility(JNIEnv
* env
, jobject obj
, bool visible
) {
797 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
798 browser_view_renderer_
.SetViewVisibility(visible
);
801 void AwContents::SetWindowVisibility(JNIEnv
* env
, jobject obj
, bool visible
) {
802 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
803 browser_view_renderer_
.SetWindowVisibility(visible
);
806 void AwContents::SetIsPaused(JNIEnv
* env
, jobject obj
, bool paused
) {
807 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
808 browser_view_renderer_
.SetIsPaused(paused
);
809 ContentViewCore
* cvc
=
810 ContentViewCore::FromWebContents(web_contents_
.get());
812 cvc
->PauseOrResumeGeolocation(paused
);
816 void AwContents::OnAttachedToWindow(JNIEnv
* env
, jobject obj
, int w
, int h
) {
817 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
818 browser_view_renderer_
.OnAttachedToWindow(w
, h
);
821 void AwContents::OnDetachedFromWindow(JNIEnv
* env
, jobject obj
) {
822 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
823 browser_view_renderer_
.OnDetachedFromWindow();
826 void AwContents::InvalidateOnFunctorDestroy() {
827 JNIEnv
* env
= AttachCurrentThread();
828 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
830 Java_AwContents_invalidateOnFunctorDestroy(env
, obj
.obj());
833 base::android::ScopedJavaLocalRef
<jbyteArray
>
834 AwContents::GetOpaqueState(JNIEnv
* env
, jobject obj
) {
835 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
836 // Required optimization in WebViewClassic to not save any state if
837 // there has been no navigations.
838 if (!web_contents_
->GetController().GetEntryCount())
839 return ScopedJavaLocalRef
<jbyteArray
>();
842 if (!WriteToPickle(*web_contents_
, &pickle
)) {
843 return ScopedJavaLocalRef
<jbyteArray
>();
845 return base::android::ToJavaByteArray(env
,
846 reinterpret_cast<const uint8
*>(pickle
.data()), pickle
.size());
850 jboolean
AwContents::RestoreFromOpaqueState(
851 JNIEnv
* env
, jobject obj
, jbyteArray state
) {
852 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
853 // TODO(boliu): This copy can be optimized out if this is a performance
855 std::vector
<uint8
> state_vector
;
856 base::android::JavaByteArrayToByteVector(env
, state
, &state_vector
);
858 Pickle
pickle(reinterpret_cast<const char*>(state_vector
.begin()),
859 state_vector
.size());
860 PickleIterator
iterator(pickle
);
862 return RestoreFromPickle(&iterator
, web_contents_
.get());
865 bool AwContents::OnDraw(JNIEnv
* env
,
868 jboolean is_hardware_accelerated
,
874 jint visible_bottom
) {
875 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
876 gfx::Vector2d
scroll(scroll_x
, scroll_y
);
877 browser_view_renderer_
.PrepareToDraw(
878 scroll
, gfx::Rect(visible_left
, visible_top
, visible_right
- visible_left
,
879 visible_bottom
- visible_top
));
880 if (is_hardware_accelerated
&& browser_view_renderer_
.attached_to_window()) {
881 return browser_view_renderer_
.OnDrawHardware();
884 gfx::Size view_size
= browser_view_renderer_
.size();
885 if (view_size
.IsEmpty()) {
886 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
887 TRACE_EVENT_SCOPE_THREAD
);
891 // TODO(hush): Right now webview size is passed in as the auxiliary bitmap
892 // size, which might hurt performace (only for software draws with auxiliary
893 // bitmap). For better performance, get global visible rect, transform it
894 // from screen space to view space, then intersect with the webview in
895 // viewspace. Use the resulting rect as the auxiliary bitmap.
896 scoped_ptr
<SoftwareCanvasHolder
> canvas_holder
=
897 SoftwareCanvasHolder::Create(canvas
, scroll
, view_size
);
898 if (!canvas_holder
|| !canvas_holder
->GetCanvas()) {
899 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
900 TRACE_EVENT_SCOPE_THREAD
);
903 return browser_view_renderer_
.OnDrawSoftware(canvas_holder
->GetCanvas());
906 void AwContents::SetPendingWebContentsForPopup(
907 scoped_ptr
<content::WebContents
> pending
) {
908 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
909 if (pending_contents_
.get()) {
910 // TODO(benm): Support holding multiple pop up window requests.
911 LOG(WARNING
) << "Blocking popup window creation as an outstanding "
912 << "popup window is still pending.";
913 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, pending
.release());
916 pending_contents_
.reset(new AwContents(pending
.Pass()));
919 void AwContents::FocusFirstNode(JNIEnv
* env
, jobject obj
) {
920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
921 web_contents_
->FocusThroughTabTraversal(false);
924 void AwContents::SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
) {
925 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
926 render_view_host_ext_
->SetBackgroundColor(color
);
929 jlong
AwContents::ReleasePopupAwContents(JNIEnv
* env
, jobject obj
) {
930 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
931 return reinterpret_cast<intptr_t>(pending_contents_
.release());
934 gfx::Point
AwContents::GetLocationOnScreen() {
935 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
936 JNIEnv
* env
= AttachCurrentThread();
937 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
940 std::vector
<int> location
;
941 base::android::JavaIntArrayToIntVector(
943 Java_AwContents_getLocationOnScreen(env
, obj
.obj()).obj(),
945 return gfx::Point(location
[0], location
[1]);
948 void AwContents::ScrollContainerViewTo(gfx::Vector2d new_value
) {
949 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
950 JNIEnv
* env
= AttachCurrentThread();
951 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
954 Java_AwContents_scrollContainerViewTo(
955 env
, obj
.obj(), new_value
.x(), new_value
.y());
958 bool AwContents::IsFlingActive() const {
959 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
960 JNIEnv
* env
= AttachCurrentThread();
961 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
964 return Java_AwContents_isFlingActive(env
, obj
.obj());
967 void AwContents::UpdateScrollState(gfx::Vector2d max_scroll_offset
,
968 gfx::SizeF contents_size_dip
,
969 float page_scale_factor
,
970 float min_page_scale_factor
,
971 float max_page_scale_factor
) {
972 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
973 JNIEnv
* env
= AttachCurrentThread();
974 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
977 Java_AwContents_updateScrollState(env
,
979 max_scroll_offset
.x(),
980 max_scroll_offset
.y(),
981 contents_size_dip
.width(),
982 contents_size_dip
.height(),
984 min_page_scale_factor
,
985 max_page_scale_factor
);
988 void AwContents::DidOverscroll(gfx::Vector2d overscroll_delta
) {
989 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
990 JNIEnv
* env
= AttachCurrentThread();
991 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
994 Java_AwContents_didOverscroll(
995 env
, obj
.obj(), overscroll_delta
.x(), overscroll_delta
.y());
998 const BrowserViewRenderer
* AwContents::GetBrowserViewRenderer() const {
999 return &browser_view_renderer_
;
1002 void AwContents::SetDipScale(JNIEnv
* env
, jobject obj
, jfloat dip_scale
) {
1003 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1004 browser_view_renderer_
.SetDipScale(dip_scale
);
1007 void AwContents::ScrollTo(JNIEnv
* env
, jobject obj
, jint x
, jint y
) {
1008 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1009 browser_view_renderer_
.ScrollTo(gfx::Vector2d(x
, y
));
1012 void AwContents::OnWebLayoutPageScaleFactorChanged(float page_scale_factor
) {
1013 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1014 JNIEnv
* env
= AttachCurrentThread();
1015 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
1018 Java_AwContents_onWebLayoutPageScaleFactorChanged(env
, obj
.obj(),
1022 void AwContents::OnWebLayoutContentsSizeChanged(
1023 const gfx::Size
& contents_size
) {
1024 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1025 JNIEnv
* env
= AttachCurrentThread();
1026 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
1029 Java_AwContents_onWebLayoutContentsSizeChanged(
1030 env
, obj
.obj(), contents_size
.width(), contents_size
.height());
1033 jlong
AwContents::CapturePicture(JNIEnv
* env
,
1037 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1038 return reinterpret_cast<intptr_t>(
1039 new AwPicture(browser_view_renderer_
.CapturePicture(width
, height
)));
1042 void AwContents::EnableOnNewPicture(JNIEnv
* env
,
1045 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1046 browser_view_renderer_
.EnableOnNewPicture(enabled
);
1049 void AwContents::ClearView(JNIEnv
* env
, jobject obj
) {
1050 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1051 browser_view_renderer_
.ClearView();
1054 void AwContents::SetExtraHeadersForUrl(JNIEnv
* env
, jobject obj
,
1055 jstring url
, jstring jextra_headers
) {
1056 std::string extra_headers
;
1058 extra_headers
= ConvertJavaStringToUTF8(env
, jextra_headers
);
1059 AwResourceContext
* resource_context
= static_cast<AwResourceContext
*>(
1060 AwBrowserContext::FromWebContents(web_contents_
.get())->
1061 GetResourceContext());
1062 resource_context
->SetExtraHeaders(GURL(ConvertJavaStringToUTF8(env
, url
)),
1066 void AwContents::SetJsOnlineProperty(JNIEnv
* env
,
1068 jboolean network_up
) {
1069 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1070 render_view_host_ext_
->SetJsOnlineProperty(network_up
);
1073 void AwContents::TrimMemory(JNIEnv
* env
,
1077 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
1078 browser_view_renderer_
.TrimMemory(level
, visible
);
1081 // TODO(sgurun) add support for posting a frame whose name is known (only
1082 // main frame is supported at this time, see crbug.com/389721)
1083 void AwContents::PostMessageToFrame(JNIEnv
* env
, jobject obj
,
1084 jstring frame_name
, jstring message
, jstring source_origin
,
1085 jstring target_origin
, jintArray msgPorts
) {
1087 base::string16
* j_source_origin
= new base::string16
;
1088 ConvertJavaStringToUTF16(env
, source_origin
, j_source_origin
);
1089 base::string16
* j_target_origin
= new base::string16
;
1090 ConvertJavaStringToUTF16(env
, target_origin
, j_target_origin
);
1091 base::string16
* j_message
= new base::string16
;
1092 ConvertJavaStringToUTF16(env
, message
, j_message
);
1093 std::vector
<int>* j_ports
= new std::vector
<int>;
1095 if (msgPorts
!= nullptr)
1096 base::android::JavaIntArrayToIntVector(env
, msgPorts
, j_ports
);
1098 BrowserThread::PostTask(
1101 base::Bind(&PostMessageToFrameOnIOThread
,
1102 web_contents_
.get(),
1103 base::Owned(j_source_origin
),
1104 base::Owned(j_target_origin
),
1105 base::Owned(j_message
),
1106 base::Owned(j_ports
)));
1109 void AwContents::CreateMessageChannel(JNIEnv
* env
, jobject obj
,
1111 ScopedJavaGlobalRef
<jobject
>* j_callback
= new ScopedJavaGlobalRef
<jobject
>();
1112 j_callback
->Reset(env
, callback
);
1114 int* port1
= new int;
1115 int* port2
= new int;
1116 BrowserThread::PostTaskAndReply(
1119 base::Bind(&content::MessagePortProvider::CreateMessageChannel
,
1120 web_contents_
.get(),
1123 base::Bind(&OnMessageChannelCreated
,
1124 base::Owned(j_callback
),
1126 base::Owned(port2
)));
1130 void SetShouldDownloadFavicons(JNIEnv
* env
, jclass jclazz
) {
1131 g_should_download_favicons
= true;
1134 } // namespace android_webview