Rebaseline global-interface-listing-expected.txt
[chromium-blink-merge.git] / android_webview / native / aw_contents.cc
blob307bb8e06aa08f714fb49e4b324bca0fd62d6055
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"
7 #include <limits>
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/aw_webview_lifecycle_observer.h"
29 #include "android_webview/native/java_browser_view_renderer_helper.h"
30 #include "android_webview/native/permission/aw_permission_request.h"
31 #include "android_webview/native/permission/permission_request_handler.h"
32 #include "android_webview/native/permission/simple_permission_request.h"
33 #include "android_webview/native/state_serializer.h"
34 #include "android_webview/public/browser/draw_gl.h"
35 #include "base/android/jni_android.h"
36 #include "base/android/jni_array.h"
37 #include "base/android/jni_string.h"
38 #include "base/android/locale_utils.h"
39 #include "base/android/scoped_java_ref.h"
40 #include "base/atomicops.h"
41 #include "base/bind.h"
42 #include "base/callback.h"
43 #include "base/memory/memory_pressure_listener.h"
44 #include "base/message_loop/message_loop.h"
45 #include "base/pickle.h"
46 #include "base/strings/string16.h"
47 #include "base/supports_user_data.h"
48 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
49 #include "components/autofill/core/browser/autofill_manager.h"
50 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
51 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
52 #include "components/navigation_interception/intercept_navigation_delegate.h"
53 #include "content/public/browser/android/content_view_core.h"
54 #include "content/public/browser/android/synchronous_compositor.h"
55 #include "content/public/browser/browser_thread.h"
56 #include "content/public/browser/cert_store.h"
57 #include "content/public/browser/child_process_security_policy.h"
58 #include "content/public/browser/favicon_status.h"
59 #include "content/public/browser/message_port_provider.h"
60 #include "content/public/browser/navigation_entry.h"
61 #include "content/public/browser/render_frame_host.h"
62 #include "content/public/browser/render_process_host.h"
63 #include "content/public/browser/render_view_host.h"
64 #include "content/public/browser/web_contents.h"
65 #include "content/public/common/message_port_types.h"
66 #include "content/public/common/renderer_preferences.h"
67 #include "content/public/common/ssl_status.h"
68 #include "jni/AwContents_jni.h"
69 #include "net/base/auth.h"
70 #include "net/cert/x509_certificate.h"
71 #include "third_party/skia/include/core/SkPicture.h"
72 #include "ui/gfx/android/java_bitmap.h"
73 #include "ui/gfx/geometry/rect_f.h"
74 #include "ui/gfx/geometry/size.h"
75 #include "ui/gfx/image/image.h"
77 struct AwDrawSWFunctionTable;
79 using autofill::ContentAutofillDriverFactory;
80 using autofill::AutofillManager;
81 using base::android::AttachCurrentThread;
82 using base::android::ConvertJavaStringToUTF16;
83 using base::android::ConvertJavaStringToUTF8;
84 using base::android::ConvertUTF16ToJavaString;
85 using base::android::ConvertUTF8ToJavaString;
86 using base::android::JavaRef;
87 using base::android::ScopedJavaGlobalRef;
88 using base::android::ScopedJavaLocalRef;
89 using data_reduction_proxy::DataReductionProxySettings;
90 using navigation_interception::InterceptNavigationDelegate;
91 using content::BrowserThread;
92 using content::ContentViewCore;
93 using content::WebContents;
95 extern "C" {
96 static AwDrawGLFunction DrawGLFunction;
97 static void DrawGLFunction(long view_context,
98 AwDrawGLInfo* draw_info,
99 void* spare) {
100 // |view_context| is the value that was returned from the java
101 // AwContents.onPrepareDrawGL; this cast must match the code there.
102 reinterpret_cast<android_webview::SharedRendererState*>(view_context)
103 ->DrawGL(draw_info);
107 namespace android_webview {
109 namespace {
111 bool g_should_download_favicons = false;
113 bool g_force_auxiliary_bitmap_rendering = false;
115 std::string g_locale;
117 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey;
119 class AwContentsUserData : public base::SupportsUserData::Data {
120 public:
121 AwContentsUserData(AwContents* ptr) : contents_(ptr) {}
123 static AwContents* GetContents(WebContents* web_contents) {
124 if (!web_contents)
125 return NULL;
126 AwContentsUserData* data = static_cast<AwContentsUserData*>(
127 web_contents->GetUserData(kAwContentsUserDataKey));
128 return data ? data->contents_ : NULL;
131 private:
132 AwContents* contents_;
135 base::subtle::Atomic32 g_instance_count = 0;
137 void OnIoThreadClientReady(content::RenderFrameHost* rfh) {
138 int render_process_id = rfh->GetProcess()->GetID();
139 int render_frame_id = rfh->GetRoutingID();
140 AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
141 render_process_id, render_frame_id);
144 } // namespace
146 // static
147 AwContents* AwContents::FromWebContents(WebContents* web_contents) {
148 DCHECK_CURRENTLY_ON(BrowserThread::UI);
149 return AwContentsUserData::GetContents(web_contents);
152 // static
153 AwContents* AwContents::FromID(int render_process_id, int render_view_id) {
154 const content::RenderViewHost* rvh =
155 content::RenderViewHost::FromID(render_process_id, render_view_id);
156 if (!rvh) return NULL;
157 content::WebContents* web_contents =
158 content::WebContents::FromRenderViewHost(rvh);
159 if (!web_contents) return NULL;
160 return FromWebContents(web_contents);
163 // static
164 void SetLocale(JNIEnv* env, jclass, jstring locale) {
165 g_locale = ConvertJavaStringToUTF8(env, locale);
168 // static
169 std::string AwContents::GetLocale() {
170 return g_locale;
173 // static
174 AwBrowserPermissionRequestDelegate* AwBrowserPermissionRequestDelegate::FromID(
175 int render_process_id, int render_frame_id) {
176 AwContents* aw_contents = AwContents::FromWebContents(
177 content::WebContents::FromRenderFrameHost(
178 content::RenderFrameHost::FromID(render_process_id,
179 render_frame_id)));
180 return implicit_cast<AwBrowserPermissionRequestDelegate*>(aw_contents);
183 AwContents::AwContents(scoped_ptr<WebContents> web_contents)
184 : web_contents_(web_contents.Pass()),
185 browser_view_renderer_(
186 this,
187 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)),
188 renderer_manager_key_(GLViewRendererManager::GetInstance()->NullKey()) {
189 int32_t current_instance_count =
190 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count, 1);
191 icon_helper_.reset(new IconHelper(web_contents_.get()));
192 icon_helper_->SetListener(this);
193 web_contents_->SetUserData(android_webview::kAwContentsUserDataKey,
194 new AwContentsUserData(this));
195 browser_view_renderer_.RegisterWithWebContents(web_contents_.get());
196 render_view_host_ext_.reset(
197 new AwRenderViewHostExt(this, web_contents_.get()));
199 permission_request_handler_.reset(
200 new PermissionRequestHandler(this, web_contents_.get()));
202 AwAutofillClient* autofill_manager_delegate =
203 AwAutofillClient::FromWebContents(web_contents_.get());
204 InitDataReductionProxyIfNecessary();
205 if (autofill_manager_delegate)
206 InitAutofillIfNecessary(autofill_manager_delegate->GetSaveFormData());
207 content::SynchronousCompositor::SetClientForWebContents(
208 web_contents_.get(), &browser_view_renderer_);
209 if (current_instance_count == 1)
210 AwWebViewLifecycleObserver::OnFirstWebViewCreated();
213 void AwContents::SetJavaPeers(JNIEnv* env,
214 jobject obj,
215 jobject aw_contents,
216 jobject web_contents_delegate,
217 jobject contents_client_bridge,
218 jobject io_thread_client,
219 jobject intercept_navigation_delegate) {
220 DCHECK_CURRENTLY_ON(BrowserThread::UI);
221 // The |aw_content| param is technically spurious as it duplicates |obj| but
222 // is passed over anyway to make the binding more explicit.
223 java_ref_ = JavaObjectWeakGlobalRef(env, aw_contents);
225 web_contents_delegate_.reset(
226 new AwWebContentsDelegate(env, web_contents_delegate));
227 web_contents_->SetDelegate(web_contents_delegate_.get());
229 contents_client_bridge_.reset(
230 new AwContentsClientBridge(env, contents_client_bridge));
231 AwContentsClientBridgeBase::Associate(web_contents_.get(),
232 contents_client_bridge_.get());
234 AwContentsIoThreadClientImpl::Associate(
235 web_contents_.get(), ScopedJavaLocalRef<jobject>(env, io_thread_client));
237 InterceptNavigationDelegate::Associate(
238 web_contents_.get(),
239 make_scoped_ptr(new InterceptNavigationDelegate(
240 env, intercept_navigation_delegate)));
242 // Finally, having setup the associations, release any deferred requests
243 web_contents_->ForEachFrame(base::Bind(&OnIoThreadClientReady));
246 void AwContents::SetSaveFormData(bool enabled) {
247 DCHECK_CURRENTLY_ON(BrowserThread::UI);
248 InitAutofillIfNecessary(enabled);
249 // We need to check for the existence, since autofill_manager_delegate
250 // may not be created when the setting is false.
251 if (AwAutofillClient::FromWebContents(web_contents_.get())) {
252 AwAutofillClient::FromWebContents(web_contents_.get())->
253 SetSaveFormData(enabled);
257 void AwContents::InitDataReductionProxyIfNecessary() {
258 AwBrowserContext* browser_context =
259 AwBrowserContext::FromWebContents(web_contents_.get());
260 browser_context->CreateUserPrefServiceIfNecessary();
263 void AwContents::InitAutofillIfNecessary(bool enabled) {
264 // Do not initialize if the feature is not enabled.
265 if (!enabled)
266 return;
267 // Check if the autofill driver factory already exists.
268 content::WebContents* web_contents = web_contents_.get();
269 if (ContentAutofillDriverFactory::FromWebContents(web_contents))
270 return;
272 AwBrowserContext::FromWebContents(web_contents)->
273 CreateUserPrefServiceIfNecessary();
274 AwAutofillClient::CreateForWebContents(web_contents);
275 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
276 web_contents, AwAutofillClient::FromWebContents(web_contents),
277 base::android::GetDefaultLocale(),
278 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
281 void AwContents::SetAwAutofillClient(jobject client) {
282 DCHECK_CURRENTLY_ON(BrowserThread::UI);
283 JNIEnv* env = AttachCurrentThread();
284 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
285 if (obj.is_null())
286 return;
287 Java_AwContents_setAwAutofillClient(env, obj.obj(), client);
290 AwContents::~AwContents() {
291 DCHECK_EQ(this, AwContents::FromWebContents(web_contents_.get()));
292 content::SynchronousCompositor::SetClientForWebContents(web_contents_.get(),
293 NULL);
294 web_contents_->RemoveUserData(kAwContentsUserDataKey);
295 if (find_helper_.get())
296 find_helper_->SetListener(NULL);
297 if (icon_helper_.get())
298 icon_helper_->SetListener(NULL);
299 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count, -1);
300 // When the last WebView is destroyed free all discardable memory allocated by
301 // Chromium, because the app process may continue to run for a long time
302 // without ever using another WebView.
303 if (base::subtle::NoBarrier_Load(&g_instance_count) == 0) {
304 // TODO(timvolodine): consider moving NotifyMemoryPressure to
305 // AwWebViewLifecycleObserver (crbug.com/522988).
306 base::MemoryPressureListener::NotifyMemoryPressure(
307 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
308 AwWebViewLifecycleObserver::OnLastWebViewDestroyed();
312 base::android::ScopedJavaLocalRef<jobject>
313 AwContents::GetWebContents(JNIEnv* env, jobject obj) {
314 DCHECK_CURRENTLY_ON(BrowserThread::UI);
315 DCHECK(web_contents_);
316 if (!web_contents_)
317 return base::android::ScopedJavaLocalRef<jobject>();
319 return web_contents_->GetJavaWebContents();
322 void AwContents::Destroy(JNIEnv* env, jobject obj) {
323 java_ref_.reset();
324 delete this;
327 static jlong Init(JNIEnv* env, jclass, jobject browser_context) {
328 // TODO(joth): Use |browser_context| to get the native BrowserContext, rather
329 // than hard-code the default instance lookup here.
330 scoped_ptr<WebContents> web_contents(content::WebContents::Create(
331 content::WebContents::CreateParams(AwBrowserContext::GetDefault())));
332 // Return an 'uninitialized' instance; most work is deferred until the
333 // subsequent SetJavaPeers() call.
334 return reinterpret_cast<intptr_t>(new AwContents(web_contents.Pass()));
337 static void SetForceAuxiliaryBitmapRendering(
338 JNIEnv* env,
339 jclass,
340 jboolean force_auxiliary_bitmap_rendering) {
341 g_force_auxiliary_bitmap_rendering = force_auxiliary_bitmap_rendering;
344 static void SetAwDrawSWFunctionTable(JNIEnv* env, jclass,
345 jlong function_table) {
346 RasterHelperSetAwDrawSWFunctionTable(
347 reinterpret_cast<AwDrawSWFunctionTable*>(function_table));
350 static void SetAwDrawGLFunctionTable(JNIEnv* env, jclass,
351 jlong function_table) {
354 static jlong GetAwDrawGLFunction(JNIEnv* env, jclass) {
355 return reinterpret_cast<intptr_t>(&DrawGLFunction);
358 // static
359 jint GetNativeInstanceCount(JNIEnv* env, jclass) {
360 return base::subtle::NoBarrier_Load(&g_instance_count);
363 jlong AwContents::GetAwDrawGLViewContext(JNIEnv* env, jobject obj) {
364 DCHECK_CURRENTLY_ON(BrowserThread::UI);
365 return reinterpret_cast<intptr_t>(
366 browser_view_renderer_.GetAwDrawGLViewContext());
369 namespace {
370 void DocumentHasImagesCallback(const ScopedJavaGlobalRef<jobject>& message,
371 bool has_images) {
372 Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(),
373 has_images,
374 message.obj());
376 } // namespace
378 void AwContents::DocumentHasImages(JNIEnv* env, jobject obj, jobject message) {
379 DCHECK_CURRENTLY_ON(BrowserThread::UI);
380 ScopedJavaGlobalRef<jobject> j_message;
381 j_message.Reset(env, message);
382 render_view_host_ext_->DocumentHasImages(
383 base::Bind(&DocumentHasImagesCallback, j_message));
386 namespace {
387 void GenerateMHTMLCallback(ScopedJavaGlobalRef<jobject>* callback,
388 const base::FilePath& path, int64 size) {
389 JNIEnv* env = AttachCurrentThread();
390 // Android files are UTF8, so the path conversion below is safe.
391 Java_AwContents_generateMHTMLCallback(
392 env,
393 ConvertUTF8ToJavaString(env, path.AsUTF8Unsafe()).obj(),
394 size, callback->obj());
396 } // namespace
398 void AwContents::GenerateMHTML(JNIEnv* env, jobject obj,
399 jstring jpath, jobject callback) {
400 DCHECK_CURRENTLY_ON(BrowserThread::UI);
401 ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>();
402 j_callback->Reset(env, callback);
403 base::FilePath target_path(ConvertJavaStringToUTF8(env, jpath));
404 web_contents_->GenerateMHTML(
405 target_path,
406 base::Bind(&GenerateMHTMLCallback, base::Owned(j_callback), target_path));
409 void AwContents::CreatePdfExporter(JNIEnv* env,
410 jobject obj,
411 jobject pdfExporter) {
412 pdf_exporter_.reset(
413 new AwPdfExporter(env,
414 pdfExporter,
415 web_contents_.get()));
418 bool AwContents::OnReceivedHttpAuthRequest(const JavaRef<jobject>& handler,
419 const std::string& host,
420 const std::string& realm) {
421 DCHECK_CURRENTLY_ON(BrowserThread::UI);
422 JNIEnv* env = AttachCurrentThread();
423 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
424 if (obj.is_null())
425 return false;
427 ScopedJavaLocalRef<jstring> jhost = ConvertUTF8ToJavaString(env, host);
428 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm);
429 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
430 "onReceivedHttpAuthRequest");
431 Java_AwContents_onReceivedHttpAuthRequest(env, obj.obj(), handler.obj(),
432 jhost.obj(), jrealm.obj());
433 return true;
436 void AwContents::SetOffscreenPreRaster(bool enabled) {
437 DCHECK_CURRENTLY_ON(BrowserThread::UI);
438 browser_view_renderer_.SetOffscreenPreRaster(enabled);
441 void AwContents::AddVisitedLinks(JNIEnv* env,
442 jobject obj,
443 jobjectArray jvisited_links) {
444 DCHECK_CURRENTLY_ON(BrowserThread::UI);
445 std::vector<base::string16> visited_link_strings;
446 base::android::AppendJavaStringArrayToStringVector(
447 env, jvisited_links, &visited_link_strings);
449 std::vector<GURL> visited_link_gurls;
450 std::vector<base::string16>::const_iterator itr;
451 for (itr = visited_link_strings.begin(); itr != visited_link_strings.end();
452 ++itr) {
453 visited_link_gurls.push_back(GURL(*itr));
456 AwBrowserContext::FromWebContents(web_contents_.get())
457 ->AddVisitedURLs(visited_link_gurls);
460 bool RegisterAwContents(JNIEnv* env) {
461 return RegisterNativesImpl(env);
464 namespace {
466 void ShowGeolocationPromptHelperTask(const JavaObjectWeakGlobalRef& java_ref,
467 const GURL& origin) {
468 JNIEnv* env = AttachCurrentThread();
469 ScopedJavaLocalRef<jobject> j_ref = java_ref.get(env);
470 if (j_ref.obj()) {
471 ScopedJavaLocalRef<jstring> j_origin(
472 ConvertUTF8ToJavaString(env, origin.spec()));
473 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
474 "onGeolocationPermissionsShowPrompt");
475 Java_AwContents_onGeolocationPermissionsShowPrompt(env,
476 j_ref.obj(),
477 j_origin.obj());
481 void ShowGeolocationPromptHelper(const JavaObjectWeakGlobalRef& java_ref,
482 const GURL& origin) {
483 JNIEnv* env = AttachCurrentThread();
484 if (java_ref.get(env).obj()) {
485 content::BrowserThread::PostTask(
486 content::BrowserThread::UI,
487 FROM_HERE,
488 base::Bind(&ShowGeolocationPromptHelperTask,
489 java_ref,
490 origin));
494 } // anonymous namespace
496 void AwContents::ShowGeolocationPrompt(const GURL& requesting_frame,
497 base::Callback<void(bool)> callback) {
498 DCHECK_CURRENTLY_ON(BrowserThread::UI);
500 GURL origin = requesting_frame.GetOrigin();
501 bool show_prompt = pending_geolocation_prompts_.empty();
502 pending_geolocation_prompts_.push_back(OriginCallback(origin, callback));
503 if (show_prompt) {
504 ShowGeolocationPromptHelper(java_ref_, origin);
508 // Invoked from Java
509 void AwContents::InvokeGeolocationCallback(JNIEnv* env,
510 jobject obj,
511 jboolean value,
512 jstring origin) {
513 DCHECK_CURRENTLY_ON(BrowserThread::UI);
514 if (pending_geolocation_prompts_.empty())
515 return;
517 GURL callback_origin(base::android::ConvertJavaStringToUTF16(env, origin));
518 if (callback_origin.GetOrigin() ==
519 pending_geolocation_prompts_.front().first) {
520 pending_geolocation_prompts_.front().second.Run(value);
521 pending_geolocation_prompts_.pop_front();
522 if (!pending_geolocation_prompts_.empty()) {
523 ShowGeolocationPromptHelper(java_ref_,
524 pending_geolocation_prompts_.front().first);
529 void AwContents::HideGeolocationPrompt(const GURL& origin) {
530 DCHECK_CURRENTLY_ON(BrowserThread::UI);
531 bool removed_current_outstanding_callback = false;
532 std::list<OriginCallback>::iterator it = pending_geolocation_prompts_.begin();
533 while (it != pending_geolocation_prompts_.end()) {
534 if ((*it).first == origin.GetOrigin()) {
535 if (it == pending_geolocation_prompts_.begin()) {
536 removed_current_outstanding_callback = true;
538 it = pending_geolocation_prompts_.erase(it);
539 } else {
540 ++it;
544 if (removed_current_outstanding_callback) {
545 JNIEnv* env = AttachCurrentThread();
546 ScopedJavaLocalRef<jobject> j_ref = java_ref_.get(env);
547 if (j_ref.obj()) {
548 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
549 "onGeolocationPermissionsHidePrompt");
550 Java_AwContents_onGeolocationPermissionsHidePrompt(env, j_ref.obj());
552 if (!pending_geolocation_prompts_.empty()) {
553 ShowGeolocationPromptHelper(java_ref_,
554 pending_geolocation_prompts_.front().first);
559 void AwContents::OnPermissionRequest(
560 base::android::ScopedJavaLocalRef<jobject> j_request,
561 AwPermissionRequest* request) {
562 DCHECK(!j_request.is_null());
563 DCHECK(request);
565 JNIEnv* env = AttachCurrentThread();
566 ScopedJavaLocalRef<jobject> j_ref = java_ref_.get(env);
567 if (j_ref.is_null()) {
568 permission_request_handler_->CancelRequest(request->GetOrigin(),
569 request->GetResources());
570 return;
573 Java_AwContents_onPermissionRequest(env, j_ref.obj(), j_request.obj());
576 void AwContents::OnPermissionRequestCanceled(AwPermissionRequest* request) {
577 JNIEnv* env = AttachCurrentThread();
578 ScopedJavaLocalRef<jobject> j_request = request->GetJavaObject();
579 ScopedJavaLocalRef<jobject> j_ref = java_ref_.get(env);
580 if (j_request.is_null() || j_ref.is_null())
581 return;
583 Java_AwContents_onPermissionRequestCanceled(
584 env, j_ref.obj(), j_request.obj());
587 void AwContents::PreauthorizePermission(
588 JNIEnv* env,
589 jobject obj,
590 jstring origin,
591 jlong resources) {
592 permission_request_handler_->PreauthorizePermission(
593 GURL(base::android::ConvertJavaStringToUTF8(env, origin)), resources);
596 void AwContents::RequestProtectedMediaIdentifierPermission(
597 const GURL& origin,
598 const base::Callback<void(bool)>& callback) {
599 permission_request_handler_->SendRequest(
600 scoped_ptr<AwPermissionRequestDelegate>(new SimplePermissionRequest(
601 origin, AwPermissionRequest::ProtectedMediaId, callback)));
604 void AwContents::CancelProtectedMediaIdentifierPermissionRequests(
605 const GURL& origin) {
606 permission_request_handler_->CancelRequest(
607 origin, AwPermissionRequest::ProtectedMediaId);
610 void AwContents::RequestGeolocationPermission(
611 const GURL& origin,
612 const base::Callback<void(bool)>& callback) {
613 JNIEnv* env = AttachCurrentThread();
614 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
615 if (obj.is_null())
616 return;
618 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env, obj.obj())) {
619 ShowGeolocationPrompt(origin, callback);
620 return;
622 permission_request_handler_->SendRequest(
623 scoped_ptr<AwPermissionRequestDelegate>(new SimplePermissionRequest(
624 origin, AwPermissionRequest::Geolocation, callback)));
627 void AwContents::CancelGeolocationPermissionRequests(const GURL& origin) {
628 JNIEnv* env = AttachCurrentThread();
629 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
630 if (obj.is_null())
631 return;
633 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env, obj.obj())) {
634 HideGeolocationPrompt(origin);
635 return;
637 permission_request_handler_->CancelRequest(
638 origin, AwPermissionRequest::Geolocation);
641 void AwContents::RequestMIDISysexPermission(
642 const GURL& origin,
643 const base::Callback<void(bool)>& callback) {
644 permission_request_handler_->SendRequest(
645 scoped_ptr<AwPermissionRequestDelegate>(new SimplePermissionRequest(
646 origin, AwPermissionRequest::MIDISysex, callback)));
649 void AwContents::CancelMIDISysexPermissionRequests(const GURL& origin) {
650 permission_request_handler_->CancelRequest(
651 origin, AwPermissionRequest::AwPermissionRequest::MIDISysex);
654 void AwContents::FindAllAsync(JNIEnv* env, jobject obj, jstring search_string) {
655 DCHECK_CURRENTLY_ON(BrowserThread::UI);
656 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env, search_string));
659 void AwContents::FindNext(JNIEnv* env, jobject obj, jboolean forward) {
660 DCHECK_CURRENTLY_ON(BrowserThread::UI);
661 GetFindHelper()->FindNext(forward);
664 void AwContents::ClearMatches(JNIEnv* env, jobject obj) {
665 DCHECK_CURRENTLY_ON(BrowserThread::UI);
666 GetFindHelper()->ClearMatches();
669 void AwContents::ClearCache(
670 JNIEnv* env,
671 jobject obj,
672 jboolean include_disk_files) {
673 DCHECK_CURRENTLY_ON(BrowserThread::UI);
674 render_view_host_ext_->ClearCache();
676 if (include_disk_files) {
677 RemoveHttpDiskCache(web_contents_->GetBrowserContext(),
678 web_contents_->GetRoutingID());
682 FindHelper* AwContents::GetFindHelper() {
683 DCHECK_CURRENTLY_ON(BrowserThread::UI);
684 if (!find_helper_.get()) {
685 find_helper_.reset(new FindHelper(web_contents_.get()));
686 find_helper_->SetListener(this);
688 return find_helper_.get();
691 void AwContents::OnFindResultReceived(int active_ordinal,
692 int match_count,
693 bool finished) {
694 DCHECK_CURRENTLY_ON(BrowserThread::UI);
695 JNIEnv* env = AttachCurrentThread();
696 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
697 if (obj.is_null())
698 return;
700 Java_AwContents_onFindResultReceived(
701 env, obj.obj(), active_ordinal, match_count, finished);
704 bool AwContents::ShouldDownloadFavicon(const GURL& icon_url) {
705 return g_should_download_favicons;
708 void AwContents::OnReceivedIcon(const GURL& icon_url, const SkBitmap& bitmap) {
709 DCHECK_CURRENTLY_ON(BrowserThread::UI);
710 JNIEnv* env = AttachCurrentThread();
711 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
712 if (obj.is_null())
713 return;
715 content::NavigationEntry* entry =
716 web_contents_->GetController().GetLastCommittedEntry();
718 if (entry) {
719 entry->GetFavicon().valid = true;
720 entry->GetFavicon().url = icon_url;
721 entry->GetFavicon().image = gfx::Image::CreateFrom1xBitmap(bitmap);
724 Java_AwContents_onReceivedIcon(
725 env, obj.obj(), gfx::ConvertToJavaBitmap(&bitmap).obj());
728 void AwContents::OnReceivedTouchIconUrl(const std::string& url,
729 bool precomposed) {
730 DCHECK_CURRENTLY_ON(BrowserThread::UI);
731 JNIEnv* env = AttachCurrentThread();
732 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
733 if (obj.is_null())
734 return;
736 Java_AwContents_onReceivedTouchIconUrl(
737 env, obj.obj(), ConvertUTF8ToJavaString(env, url).obj(), precomposed);
740 bool AwContents::RequestDrawGL(bool wait_for_completion) {
741 DCHECK_CURRENTLY_ON(BrowserThread::UI);
742 JNIEnv* env = AttachCurrentThread();
743 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
744 if (obj.is_null())
745 return false;
746 return Java_AwContents_requestDrawGL(env, obj.obj(), wait_for_completion);
749 void AwContents::PostInvalidate() {
750 DCHECK_CURRENTLY_ON(BrowserThread::UI);
751 JNIEnv* env = AttachCurrentThread();
752 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
753 if (!obj.is_null())
754 Java_AwContents_postInvalidateOnAnimation(env, obj.obj());
757 void AwContents::OnNewPicture() {
758 DCHECK_CURRENTLY_ON(BrowserThread::UI);
759 JNIEnv* env = AttachCurrentThread();
760 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
761 if (!obj.is_null()) {
762 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
763 "onNewPicture");
764 Java_AwContents_onNewPicture(env, obj.obj());
768 base::android::ScopedJavaLocalRef<jbyteArray> AwContents::GetCertificate(
769 JNIEnv* env,
770 jobject obj) {
771 DCHECK_CURRENTLY_ON(BrowserThread::UI);
772 content::NavigationEntry* entry =
773 web_contents_->GetController().GetLastCommittedEntry();
774 if (!entry)
775 return ScopedJavaLocalRef<jbyteArray>();
776 // Get the certificate
777 int cert_id = entry->GetSSL().cert_id;
778 scoped_refptr<net::X509Certificate> cert;
779 bool ok = content::CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
780 if (!ok)
781 return ScopedJavaLocalRef<jbyteArray>();
783 // Convert the certificate and return it
784 std::string der_string;
785 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string);
786 return base::android::ToJavaByteArray(env,
787 reinterpret_cast<const uint8*>(der_string.data()), der_string.length());
790 void AwContents::RequestNewHitTestDataAt(JNIEnv* env,
791 jobject obj,
792 jfloat x,
793 jfloat y,
794 jfloat touch_major) {
795 DCHECK_CURRENTLY_ON(BrowserThread::UI);
796 gfx::PointF touch_center(x, y);
797 gfx::SizeF touch_area(touch_major, touch_major);
798 render_view_host_ext_->RequestNewHitTestDataAt(touch_center, touch_area);
801 void AwContents::UpdateLastHitTestData(JNIEnv* env, jobject obj) {
802 DCHECK_CURRENTLY_ON(BrowserThread::UI);
803 if (!render_view_host_ext_->HasNewHitTestData()) return;
805 const AwHitTestData& data = render_view_host_ext_->GetLastHitTestData();
806 render_view_host_ext_->MarkHitTestDataRead();
808 // Make sure to null the Java object if data is empty/invalid.
809 ScopedJavaLocalRef<jstring> extra_data_for_type;
810 if (data.extra_data_for_type.length())
811 extra_data_for_type = ConvertUTF8ToJavaString(
812 env, data.extra_data_for_type);
814 ScopedJavaLocalRef<jstring> href;
815 if (data.href.length())
816 href = ConvertUTF16ToJavaString(env, data.href);
818 ScopedJavaLocalRef<jstring> anchor_text;
819 if (data.anchor_text.length())
820 anchor_text = ConvertUTF16ToJavaString(env, data.anchor_text);
822 ScopedJavaLocalRef<jstring> img_src;
823 if (data.img_src.is_valid())
824 img_src = ConvertUTF8ToJavaString(env, data.img_src.spec());
826 Java_AwContents_updateHitTestData(env,
827 obj,
828 data.type,
829 extra_data_for_type.obj(),
830 href.obj(),
831 anchor_text.obj(),
832 img_src.obj());
835 void AwContents::OnSizeChanged(JNIEnv* env, jobject obj,
836 int w, int h, int ow, int oh) {
837 DCHECK_CURRENTLY_ON(BrowserThread::UI);
838 browser_view_renderer_.OnSizeChanged(w, h);
841 void AwContents::SetViewVisibility(JNIEnv* env, jobject obj, bool visible) {
842 DCHECK_CURRENTLY_ON(BrowserThread::UI);
843 browser_view_renderer_.SetViewVisibility(visible);
846 void AwContents::SetWindowVisibility(JNIEnv* env, jobject obj, bool visible) {
847 DCHECK_CURRENTLY_ON(BrowserThread::UI);
848 browser_view_renderer_.SetWindowVisibility(visible);
851 void AwContents::SetIsPaused(JNIEnv* env, jobject obj, bool paused) {
852 DCHECK_CURRENTLY_ON(BrowserThread::UI);
853 browser_view_renderer_.SetIsPaused(paused);
854 ContentViewCore* cvc =
855 ContentViewCore::FromWebContents(web_contents_.get());
856 if (cvc) {
857 cvc->PauseOrResumeGeolocation(paused);
861 void AwContents::OnAttachedToWindow(JNIEnv* env, jobject obj, int w, int h) {
862 DCHECK_CURRENTLY_ON(BrowserThread::UI);
863 browser_view_renderer_.OnAttachedToWindow(w, h);
866 void AwContents::OnDetachedFromWindow(JNIEnv* env, jobject obj) {
867 DCHECK_CURRENTLY_ON(BrowserThread::UI);
868 browser_view_renderer_.OnDetachedFromWindow();
871 void AwContents::DetachFunctorFromView() {
872 JNIEnv* env = AttachCurrentThread();
873 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
874 if (!obj.is_null())
875 Java_AwContents_detachFunctorFromView(env, obj.obj());
878 base::android::ScopedJavaLocalRef<jbyteArray>
879 AwContents::GetOpaqueState(JNIEnv* env, jobject obj) {
880 DCHECK_CURRENTLY_ON(BrowserThread::UI);
881 // Required optimization in WebViewClassic to not save any state if
882 // there has been no navigations.
883 if (!web_contents_->GetController().GetEntryCount())
884 return ScopedJavaLocalRef<jbyteArray>();
886 base::Pickle pickle;
887 if (!WriteToPickle(*web_contents_, &pickle)) {
888 return ScopedJavaLocalRef<jbyteArray>();
889 } else {
890 return base::android::ToJavaByteArray(env,
891 reinterpret_cast<const uint8*>(pickle.data()), pickle.size());
895 jboolean AwContents::RestoreFromOpaqueState(
896 JNIEnv* env, jobject obj, jbyteArray state) {
897 DCHECK_CURRENTLY_ON(BrowserThread::UI);
898 // TODO(boliu): This copy can be optimized out if this is a performance
899 // problem.
900 std::vector<uint8> state_vector;
901 base::android::JavaByteArrayToByteVector(env, state, &state_vector);
903 base::Pickle pickle(reinterpret_cast<const char*>(state_vector.data()),
904 state_vector.size());
905 base::PickleIterator iterator(pickle);
907 return RestoreFromPickle(&iterator, web_contents_.get());
910 bool AwContents::OnDraw(JNIEnv* env,
911 jobject obj,
912 jobject canvas,
913 jboolean is_hardware_accelerated,
914 jint scroll_x,
915 jint scroll_y,
916 jint visible_left,
917 jint visible_top,
918 jint visible_right,
919 jint visible_bottom) {
920 DCHECK_CURRENTLY_ON(BrowserThread::UI);
921 gfx::Vector2d scroll(scroll_x, scroll_y);
922 browser_view_renderer_.PrepareToDraw(
923 scroll, gfx::Rect(visible_left, visible_top, visible_right - visible_left,
924 visible_bottom - visible_top));
925 if (is_hardware_accelerated && browser_view_renderer_.attached_to_window() &&
926 !g_force_auxiliary_bitmap_rendering) {
927 return browser_view_renderer_.OnDrawHardware();
930 gfx::Size view_size = browser_view_renderer_.size();
931 if (view_size.IsEmpty()) {
932 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
933 TRACE_EVENT_SCOPE_THREAD);
934 return false;
937 // TODO(hush): Right now webview size is passed in as the auxiliary bitmap
938 // size, which might hurt performace (only for software draws with auxiliary
939 // bitmap). For better performance, get global visible rect, transform it
940 // from screen space to view space, then intersect with the webview in
941 // viewspace. Use the resulting rect as the auxiliary bitmap.
942 scoped_ptr<SoftwareCanvasHolder> canvas_holder = SoftwareCanvasHolder::Create(
943 canvas, scroll, view_size, g_force_auxiliary_bitmap_rendering);
944 if (!canvas_holder || !canvas_holder->GetCanvas()) {
945 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_NoSoftwareCanvas",
946 TRACE_EVENT_SCOPE_THREAD);
947 return false;
949 return browser_view_renderer_.OnDrawSoftware(canvas_holder->GetCanvas());
952 void AwContents::SetPendingWebContentsForPopup(
953 scoped_ptr<content::WebContents> pending) {
954 DCHECK_CURRENTLY_ON(BrowserThread::UI);
955 if (pending_contents_.get()) {
956 // TODO(benm): Support holding multiple pop up window requests.
957 LOG(WARNING) << "Blocking popup window creation as an outstanding "
958 << "popup window is still pending.";
959 base::MessageLoop::current()->DeleteSoon(FROM_HERE, pending.release());
960 return;
962 pending_contents_.reset(new AwContents(pending.Pass()));
963 // Set dip_scale for pending contents, which is necessary for the later
964 // SynchronousCompositor and InputHandler setup.
965 pending_contents_->SetDipScaleInternal(browser_view_renderer_.dip_scale());
968 void AwContents::FocusFirstNode(JNIEnv* env, jobject obj) {
969 DCHECK_CURRENTLY_ON(BrowserThread::UI);
970 web_contents_->FocusThroughTabTraversal(false);
973 void AwContents::SetBackgroundColor(JNIEnv* env, jobject obj, jint color) {
974 DCHECK_CURRENTLY_ON(BrowserThread::UI);
975 render_view_host_ext_->SetBackgroundColor(color);
978 void AwContents::OnComputeScroll(JNIEnv* env,
979 jobject obj,
980 jlong animation_time_millis) {
981 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
982 browser_view_renderer_.OnComputeScroll(
983 base::TimeTicks() +
984 base::TimeDelta::FromMilliseconds(animation_time_millis));
987 jlong AwContents::ReleasePopupAwContents(JNIEnv* env, jobject obj) {
988 DCHECK_CURRENTLY_ON(BrowserThread::UI);
989 return reinterpret_cast<intptr_t>(pending_contents_.release());
992 gfx::Point AwContents::GetLocationOnScreen() {
993 DCHECK_CURRENTLY_ON(BrowserThread::UI);
994 JNIEnv* env = AttachCurrentThread();
995 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
996 if (obj.is_null())
997 return gfx::Point();
998 std::vector<int> location;
999 base::android::JavaIntArrayToIntVector(
1000 env,
1001 Java_AwContents_getLocationOnScreen(env, obj.obj()).obj(),
1002 &location);
1003 return gfx::Point(location[0], location[1]);
1006 void AwContents::ScrollContainerViewTo(gfx::Vector2d new_value) {
1007 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1008 JNIEnv* env = AttachCurrentThread();
1009 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1010 if (obj.is_null())
1011 return;
1012 Java_AwContents_scrollContainerViewTo(
1013 env, obj.obj(), new_value.x(), new_value.y());
1016 bool AwContents::IsSmoothScrollingActive() const {
1017 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1018 JNIEnv* env = AttachCurrentThread();
1019 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1020 if (obj.is_null())
1021 return false;
1022 return Java_AwContents_isSmoothScrollingActive(env, obj.obj());
1025 void AwContents::UpdateScrollState(gfx::Vector2d max_scroll_offset,
1026 gfx::SizeF contents_size_dip,
1027 float page_scale_factor,
1028 float min_page_scale_factor,
1029 float max_page_scale_factor) {
1030 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1031 JNIEnv* env = AttachCurrentThread();
1032 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1033 if (obj.is_null())
1034 return;
1035 Java_AwContents_updateScrollState(env,
1036 obj.obj(),
1037 max_scroll_offset.x(),
1038 max_scroll_offset.y(),
1039 contents_size_dip.width(),
1040 contents_size_dip.height(),
1041 page_scale_factor,
1042 min_page_scale_factor,
1043 max_page_scale_factor);
1046 void AwContents::DidOverscroll(gfx::Vector2d overscroll_delta,
1047 gfx::Vector2dF overscroll_velocity) {
1048 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1049 JNIEnv* env = AttachCurrentThread();
1050 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1051 if (obj.is_null())
1052 return;
1053 Java_AwContents_didOverscroll(env, obj.obj(), overscroll_delta.x(),
1054 overscroll_delta.y(), overscroll_velocity.x(),
1055 overscroll_velocity.y());
1058 void AwContents::SetDipScale(JNIEnv* env, jobject obj, jfloat dip_scale) {
1059 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1060 SetDipScaleInternal(dip_scale);
1063 void AwContents::SetDipScaleInternal(float dip_scale) {
1064 browser_view_renderer_.SetDipScale(dip_scale);
1067 void AwContents::ScrollTo(JNIEnv* env, jobject obj, jint x, jint y) {
1068 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1069 browser_view_renderer_.ScrollTo(gfx::Vector2d(x, y));
1072 void AwContents::OnWebLayoutPageScaleFactorChanged(float page_scale_factor) {
1073 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1074 JNIEnv* env = AttachCurrentThread();
1075 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1076 if (obj.is_null())
1077 return;
1078 Java_AwContents_onWebLayoutPageScaleFactorChanged(env, obj.obj(),
1079 page_scale_factor);
1082 void AwContents::OnWebLayoutContentsSizeChanged(
1083 const gfx::Size& contents_size) {
1084 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1085 JNIEnv* env = AttachCurrentThread();
1086 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1087 if (obj.is_null())
1088 return;
1089 Java_AwContents_onWebLayoutContentsSizeChanged(
1090 env, obj.obj(), contents_size.width(), contents_size.height());
1093 jlong AwContents::CapturePicture(JNIEnv* env,
1094 jobject obj,
1095 int width,
1096 int height) {
1097 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1098 return reinterpret_cast<intptr_t>(
1099 new AwPicture(browser_view_renderer_.CapturePicture(width, height)));
1102 void AwContents::EnableOnNewPicture(JNIEnv* env,
1103 jobject obj,
1104 jboolean enabled) {
1105 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1106 browser_view_renderer_.EnableOnNewPicture(enabled);
1109 namespace {
1110 void InvokeVisualStateCallback(const JavaObjectWeakGlobalRef& java_ref,
1111 long request_id,
1112 ScopedJavaGlobalRef<jobject>* callback,
1113 bool result) {
1114 JNIEnv* env = AttachCurrentThread();
1115 ScopedJavaLocalRef<jobject> obj = java_ref.get(env);
1116 if (obj.is_null())
1117 return;
1118 Java_AwContents_invokeVisualStateCallback(
1119 env, obj.obj(), callback->obj(), request_id);
1121 } // namespace
1123 void AwContents::InsertVisualStateCallback(
1124 JNIEnv* env, jobject obj, long request_id, jobject callback) {
1125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1126 ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>();
1127 j_callback->Reset(env, callback);
1128 web_contents_->GetMainFrame()->InsertVisualStateCallback(
1129 base::Bind(&InvokeVisualStateCallback, java_ref_, request_id,
1130 base::Owned(j_callback)));
1133 void AwContents::ClearView(JNIEnv* env, jobject obj) {
1134 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1135 browser_view_renderer_.ClearView();
1138 void AwContents::SetExtraHeadersForUrl(JNIEnv* env, jobject obj,
1139 jstring url, jstring jextra_headers) {
1140 std::string extra_headers;
1141 if (jextra_headers)
1142 extra_headers = ConvertJavaStringToUTF8(env, jextra_headers);
1143 AwResourceContext* resource_context = static_cast<AwResourceContext*>(
1144 AwBrowserContext::FromWebContents(web_contents_.get())->
1145 GetResourceContext());
1146 resource_context->SetExtraHeaders(GURL(ConvertJavaStringToUTF8(env, url)),
1147 extra_headers);
1150 void AwContents::SetJsOnlineProperty(JNIEnv* env,
1151 jobject obj,
1152 jboolean network_up) {
1153 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1154 render_view_host_ext_->SetJsOnlineProperty(network_up);
1157 void AwContents::TrimMemory(JNIEnv* env,
1158 jobject obj,
1159 jint level,
1160 jboolean visible) {
1161 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1162 browser_view_renderer_.TrimMemory(level, visible);
1165 // TODO(sgurun) add support for posting a frame whose name is known (only
1166 // main frame is supported at this time, see crbug.com/389721)
1167 void AwContents::PostMessageToFrame(JNIEnv* env, jobject obj,
1168 jstring frame_name, jstring message, jstring target_origin,
1169 jintArray sent_ports) {
1171 // Use an empty source origin for android webview.
1172 base::string16 source_origin;
1173 base::string16 j_target_origin(ConvertJavaStringToUTF16(env, target_origin));
1174 base::string16 j_message(ConvertJavaStringToUTF16(env, message));
1175 std::vector<int> j_ports;
1177 if (sent_ports != nullptr) {
1178 base::android::JavaIntArrayToIntVector(env, sent_ports, &j_ports);
1179 BrowserThread::PostTask(
1180 BrowserThread::IO,
1181 FROM_HERE,
1182 base::Bind(&AwMessagePortServiceImpl::RemoveSentPorts,
1183 base::Unretained(AwMessagePortServiceImpl::GetInstance()),
1184 j_ports));
1186 std::vector<content::TransferredMessagePort> ports(j_ports.size());
1187 for (size_t i = 0; i < j_ports.size(); ++i)
1188 ports[i].id = j_ports[i];
1189 content::MessagePortProvider::PostMessageToFrame(web_contents_.get(),
1190 source_origin,
1191 j_target_origin,
1192 j_message,
1193 ports);
1196 scoped_refptr<AwMessagePortMessageFilter>
1197 AwContents::GetMessagePortMessageFilter() {
1198 // Create a message port message filter if necessary
1199 if (message_port_message_filter_.get() == nullptr) {
1200 message_port_message_filter_ =
1201 new AwMessagePortMessageFilter(
1202 web_contents_->GetMainFrame()->GetRoutingID());
1203 web_contents_->GetRenderProcessHost()->AddFilter(
1204 message_port_message_filter_.get());
1206 return message_port_message_filter_;
1209 void AwContents::CreateMessageChannel(JNIEnv* env, jobject obj,
1210 jobjectArray ports) {
1212 AwMessagePortServiceImpl::GetInstance()->CreateMessageChannel(env, ports,
1213 GetMessagePortMessageFilter());
1216 void AwContents::GrantFileSchemeAccesstoChildProcess(JNIEnv* env, jobject obj) {
1217 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
1218 web_contents_->GetRenderProcessHost()->GetID(), url::kFileScheme);
1221 void SetShouldDownloadFavicons(JNIEnv* env, jclass jclazz) {
1222 g_should_download_favicons = true;
1225 } // namespace android_webview