Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / android_webview / native / aw_contents.cc
blob5c551dc4c4cec7fd6a64a8dc2285c0ef4596a115
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/command_line.h"
44 #include "base/memory/memory_pressure_listener.h"
45 #include "base/message_loop/message_loop.h"
46 #include "base/pickle.h"
47 #include "base/strings/string16.h"
48 #include "base/supports_user_data.h"
49 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
50 #include "components/autofill/core/browser/autofill_manager.h"
51 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
52 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
53 #include "components/navigation_interception/intercept_navigation_delegate.h"
54 #include "content/public/browser/android/content_view_core.h"
55 #include "content/public/browser/android/synchronous_compositor.h"
56 #include "content/public/browser/browser_thread.h"
57 #include "content/public/browser/cert_store.h"
58 #include "content/public/browser/child_process_security_policy.h"
59 #include "content/public/browser/favicon_status.h"
60 #include "content/public/browser/message_port_provider.h"
61 #include "content/public/browser/navigation_entry.h"
62 #include "content/public/browser/render_frame_host.h"
63 #include "content/public/browser/render_process_host.h"
64 #include "content/public/browser/render_view_host.h"
65 #include "content/public/browser/web_contents.h"
66 #include "content/public/common/content_switches.h"
67 #include "content/public/common/message_port_types.h"
68 #include "content/public/common/renderer_preferences.h"
69 #include "content/public/common/ssl_status.h"
70 #include "jni/AwContents_jni.h"
71 #include "net/base/auth.h"
72 #include "net/cert/x509_certificate.h"
73 #include "third_party/skia/include/core/SkPicture.h"
74 #include "ui/gfx/android/java_bitmap.h"
75 #include "ui/gfx/geometry/rect_f.h"
76 #include "ui/gfx/geometry/size.h"
77 #include "ui/gfx/image/image.h"
79 struct AwDrawSWFunctionTable;
81 using autofill::ContentAutofillDriverFactory;
82 using autofill::AutofillManager;
83 using base::android::AttachCurrentThread;
84 using base::android::ConvertJavaStringToUTF16;
85 using base::android::ConvertJavaStringToUTF8;
86 using base::android::ConvertUTF16ToJavaString;
87 using base::android::ConvertUTF8ToJavaString;
88 using base::android::JavaRef;
89 using base::android::ScopedJavaGlobalRef;
90 using base::android::ScopedJavaLocalRef;
91 using data_reduction_proxy::DataReductionProxySettings;
92 using navigation_interception::InterceptNavigationDelegate;
93 using content::BrowserThread;
94 using content::ContentViewCore;
95 using content::WebContents;
97 extern "C" {
98 static AwDrawGLFunction DrawGLFunction;
99 static void DrawGLFunction(long view_context,
100 AwDrawGLInfo* draw_info,
101 void* spare) {
102 // |view_context| is the value that was returned from the java
103 // AwContents.onPrepareDrawGL; this cast must match the code there.
104 reinterpret_cast<android_webview::SharedRendererState*>(view_context)
105 ->DrawGL(draw_info);
109 namespace android_webview {
111 namespace {
113 bool g_should_download_favicons = false;
115 bool g_force_auxiliary_bitmap_rendering = false;
117 std::string g_locale;
119 const void* kAwContentsUserDataKey = &kAwContentsUserDataKey;
121 class AwContentsUserData : public base::SupportsUserData::Data {
122 public:
123 explicit AwContentsUserData(AwContents* ptr) : contents_(ptr) {}
125 static AwContents* GetContents(WebContents* web_contents) {
126 if (!web_contents)
127 return NULL;
128 AwContentsUserData* data = static_cast<AwContentsUserData*>(
129 web_contents->GetUserData(kAwContentsUserDataKey));
130 return data ? data->contents_ : NULL;
133 private:
134 AwContents* contents_;
137 base::subtle::Atomic32 g_instance_count = 0;
139 void OnIoThreadClientReady(content::RenderFrameHost* rfh) {
140 int render_process_id = rfh->GetProcess()->GetID();
141 int render_frame_id = rfh->GetRoutingID();
142 AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
143 render_process_id, render_frame_id);
146 } // namespace
148 // static
149 AwContents* AwContents::FromWebContents(WebContents* web_contents) {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI);
151 return AwContentsUserData::GetContents(web_contents);
154 // static
155 AwContents* AwContents::FromID(int render_process_id, int render_view_id) {
156 const content::RenderViewHost* rvh =
157 content::RenderViewHost::FromID(render_process_id, render_view_id);
158 if (!rvh) return NULL;
159 content::WebContents* web_contents =
160 content::WebContents::FromRenderViewHost(rvh);
161 if (!web_contents) return NULL;
162 return FromWebContents(web_contents);
165 // static
166 void SetLocale(JNIEnv* env,
167 const JavaParamRef<jclass>&,
168 const JavaParamRef<jstring>& locale) {
169 g_locale = ConvertJavaStringToUTF8(env, locale);
172 // static
173 std::string AwContents::GetLocale() {
174 return g_locale;
177 // static
178 AwBrowserPermissionRequestDelegate* AwBrowserPermissionRequestDelegate::FromID(
179 int render_process_id, int render_frame_id) {
180 AwContents* aw_contents = AwContents::FromWebContents(
181 content::WebContents::FromRenderFrameHost(
182 content::RenderFrameHost::FromID(render_process_id,
183 render_frame_id)));
184 return implicit_cast<AwBrowserPermissionRequestDelegate*>(aw_contents);
187 AwContents::AwContents(scoped_ptr<WebContents> web_contents)
188 : web_contents_(web_contents.Pass()),
189 browser_view_renderer_(
190 this,
191 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)),
192 renderer_manager_key_(GLViewRendererManager::GetInstance()->NullKey()) {
193 int32_t current_instance_count =
194 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count, 1);
195 icon_helper_.reset(new IconHelper(web_contents_.get()));
196 icon_helper_->SetListener(this);
197 web_contents_->SetUserData(android_webview::kAwContentsUserDataKey,
198 new AwContentsUserData(this));
199 browser_view_renderer_.RegisterWithWebContents(web_contents_.get());
200 render_view_host_ext_.reset(
201 new AwRenderViewHostExt(this, web_contents_.get()));
203 permission_request_handler_.reset(
204 new PermissionRequestHandler(this, web_contents_.get()));
206 AwAutofillClient* autofill_manager_delegate =
207 AwAutofillClient::FromWebContents(web_contents_.get());
208 if (autofill_manager_delegate)
209 InitAutofillIfNecessary(autofill_manager_delegate->GetSaveFormData());
210 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
211 switches::kSingleProcess)) {
212 // TODO(boliu): Figure out how to deal with this more nicely.
213 content::SynchronousCompositor::SetClientForWebContents(
214 web_contents_.get(), &browser_view_renderer_);
216 if (current_instance_count == 1)
217 AwWebViewLifecycleObserver::OnFirstWebViewCreated();
220 void AwContents::SetJavaPeers(JNIEnv* env,
221 jobject obj,
222 jobject aw_contents,
223 jobject web_contents_delegate,
224 jobject contents_client_bridge,
225 jobject io_thread_client,
226 jobject intercept_navigation_delegate) {
227 DCHECK_CURRENTLY_ON(BrowserThread::UI);
228 // The |aw_content| param is technically spurious as it duplicates |obj| but
229 // is passed over anyway to make the binding more explicit.
230 java_ref_ = JavaObjectWeakGlobalRef(env, aw_contents);
232 web_contents_delegate_.reset(
233 new AwWebContentsDelegate(env, web_contents_delegate));
234 web_contents_->SetDelegate(web_contents_delegate_.get());
236 contents_client_bridge_.reset(
237 new AwContentsClientBridge(env, contents_client_bridge));
238 AwContentsClientBridgeBase::Associate(web_contents_.get(),
239 contents_client_bridge_.get());
241 AwContentsIoThreadClientImpl::Associate(
242 web_contents_.get(), ScopedJavaLocalRef<jobject>(env, io_thread_client));
244 InterceptNavigationDelegate::Associate(
245 web_contents_.get(),
246 make_scoped_ptr(new InterceptNavigationDelegate(
247 env, intercept_navigation_delegate)));
249 // Finally, having setup the associations, release any deferred requests
250 web_contents_->ForEachFrame(base::Bind(&OnIoThreadClientReady));
253 void AwContents::SetSaveFormData(bool enabled) {
254 DCHECK_CURRENTLY_ON(BrowserThread::UI);
255 InitAutofillIfNecessary(enabled);
256 // We need to check for the existence, since autofill_manager_delegate
257 // may not be created when the setting is false.
258 if (AwAutofillClient::FromWebContents(web_contents_.get())) {
259 AwAutofillClient::FromWebContents(web_contents_.get())->
260 SetSaveFormData(enabled);
264 void AwContents::InitAutofillIfNecessary(bool enabled) {
265 // Do not initialize if the feature is not enabled.
266 if (!enabled)
267 return;
268 // Check if the autofill driver factory already exists.
269 content::WebContents* web_contents = web_contents_.get();
270 if (ContentAutofillDriverFactory::FromWebContents(web_contents))
271 return;
273 AwAutofillClient::CreateForWebContents(web_contents);
274 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
275 web_contents, AwAutofillClient::FromWebContents(web_contents),
276 base::android::GetDefaultLocale(),
277 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
280 void AwContents::SetAwAutofillClient(jobject client) {
281 DCHECK_CURRENTLY_ON(BrowserThread::UI);
282 JNIEnv* env = AttachCurrentThread();
283 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
284 if (obj.is_null())
285 return;
286 Java_AwContents_setAwAutofillClient(env, obj.obj(), client);
289 AwContents::~AwContents() {
290 DCHECK_EQ(this, AwContents::FromWebContents(web_contents_.get()));
291 content::SynchronousCompositor::SetClientForWebContents(web_contents_.get(),
292 NULL);
293 web_contents_->RemoveUserData(kAwContentsUserDataKey);
294 if (find_helper_.get())
295 find_helper_->SetListener(NULL);
296 if (icon_helper_.get())
297 icon_helper_->SetListener(NULL);
298 base::subtle::NoBarrier_AtomicIncrement(&g_instance_count, -1);
299 // When the last WebView is destroyed free all discardable memory allocated by
300 // Chromium, because the app process may continue to run for a long time
301 // without ever using another WebView.
302 if (base::subtle::NoBarrier_Load(&g_instance_count) == 0) {
303 // TODO(timvolodine): consider moving NotifyMemoryPressure to
304 // AwWebViewLifecycleObserver (crbug.com/522988).
305 base::MemoryPressureListener::NotifyMemoryPressure(
306 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
307 AwWebViewLifecycleObserver::OnLastWebViewDestroyed();
311 base::android::ScopedJavaLocalRef<jobject>
312 AwContents::GetWebContents(JNIEnv* env, jobject obj) {
313 DCHECK_CURRENTLY_ON(BrowserThread::UI);
314 DCHECK(web_contents_);
315 if (!web_contents_)
316 return base::android::ScopedJavaLocalRef<jobject>();
318 return web_contents_->GetJavaWebContents();
321 void AwContents::Destroy(JNIEnv* env, jobject obj) {
322 java_ref_.reset();
323 delete this;
326 static jlong Init(JNIEnv* env,
327 const JavaParamRef<jclass>&,
328 const JavaParamRef<jobject>& browser_context) {
329 // TODO(joth): Use |browser_context| to get the native BrowserContext, rather
330 // than hard-code the default instance lookup here.
331 scoped_ptr<WebContents> web_contents(content::WebContents::Create(
332 content::WebContents::CreateParams(AwBrowserContext::GetDefault())));
333 // Return an 'uninitialized' instance; most work is deferred until the
334 // subsequent SetJavaPeers() call.
335 return reinterpret_cast<intptr_t>(new AwContents(web_contents.Pass()));
338 static void SetForceAuxiliaryBitmapRendering(
339 JNIEnv* env,
340 const JavaParamRef<jclass>&,
341 jboolean force_auxiliary_bitmap_rendering) {
342 g_force_auxiliary_bitmap_rendering = force_auxiliary_bitmap_rendering;
345 static void SetAwDrawSWFunctionTable(JNIEnv* env,
346 const JavaParamRef<jclass>&,
347 jlong function_table) {
348 RasterHelperSetAwDrawSWFunctionTable(
349 reinterpret_cast<AwDrawSWFunctionTable*>(function_table));
352 static void SetAwDrawGLFunctionTable(JNIEnv* env,
353 const JavaParamRef<jclass>&,
354 jlong function_table) {}
356 static jlong GetAwDrawGLFunction(JNIEnv* env, const JavaParamRef<jclass>&) {
357 return reinterpret_cast<intptr_t>(&DrawGLFunction);
360 // static
361 jint GetNativeInstanceCount(JNIEnv* env, const JavaParamRef<jclass>&) {
362 return base::subtle::NoBarrier_Load(&g_instance_count);
365 jlong AwContents::GetAwDrawGLViewContext(JNIEnv* env, jobject obj) {
366 DCHECK_CURRENTLY_ON(BrowserThread::UI);
367 return reinterpret_cast<intptr_t>(
368 browser_view_renderer_.GetAwDrawGLViewContext());
371 namespace {
372 void DocumentHasImagesCallback(const ScopedJavaGlobalRef<jobject>& message,
373 bool has_images) {
374 Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(),
375 has_images,
376 message.obj());
378 } // namespace
380 void AwContents::DocumentHasImages(JNIEnv* env, jobject obj, jobject message) {
381 DCHECK_CURRENTLY_ON(BrowserThread::UI);
382 ScopedJavaGlobalRef<jobject> j_message;
383 j_message.Reset(env, message);
384 render_view_host_ext_->DocumentHasImages(
385 base::Bind(&DocumentHasImagesCallback, j_message));
388 namespace {
389 void GenerateMHTMLCallback(ScopedJavaGlobalRef<jobject>* callback,
390 const base::FilePath& path, int64 size) {
391 JNIEnv* env = AttachCurrentThread();
392 // Android files are UTF8, so the path conversion below is safe.
393 Java_AwContents_generateMHTMLCallback(
394 env,
395 ConvertUTF8ToJavaString(env, path.AsUTF8Unsafe()).obj(),
396 size, callback->obj());
398 } // namespace
400 void AwContents::GenerateMHTML(JNIEnv* env, jobject obj,
401 jstring jpath, jobject callback) {
402 DCHECK_CURRENTLY_ON(BrowserThread::UI);
403 ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>();
404 j_callback->Reset(env, callback);
405 base::FilePath target_path(ConvertJavaStringToUTF8(env, jpath));
406 web_contents_->GenerateMHTML(
407 target_path,
408 base::Bind(&GenerateMHTMLCallback, base::Owned(j_callback), target_path));
411 void AwContents::CreatePdfExporter(JNIEnv* env,
412 jobject obj,
413 jobject pdfExporter) {
414 pdf_exporter_.reset(
415 new AwPdfExporter(env,
416 pdfExporter,
417 web_contents_.get()));
420 bool AwContents::OnReceivedHttpAuthRequest(const JavaRef<jobject>& handler,
421 const std::string& host,
422 const std::string& realm) {
423 DCHECK_CURRENTLY_ON(BrowserThread::UI);
424 JNIEnv* env = AttachCurrentThread();
425 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
426 if (obj.is_null())
427 return false;
429 ScopedJavaLocalRef<jstring> jhost = ConvertUTF8ToJavaString(env, host);
430 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm);
431 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
432 "onReceivedHttpAuthRequest");
433 Java_AwContents_onReceivedHttpAuthRequest(env, obj.obj(), handler.obj(),
434 jhost.obj(), jrealm.obj());
435 return true;
438 void AwContents::SetOffscreenPreRaster(bool enabled) {
439 DCHECK_CURRENTLY_ON(BrowserThread::UI);
440 browser_view_renderer_.SetOffscreenPreRaster(enabled);
443 void AwContents::AddVisitedLinks(JNIEnv* env,
444 jobject obj,
445 jobjectArray jvisited_links) {
446 DCHECK_CURRENTLY_ON(BrowserThread::UI);
447 std::vector<base::string16> visited_link_strings;
448 base::android::AppendJavaStringArrayToStringVector(
449 env, jvisited_links, &visited_link_strings);
451 std::vector<GURL> visited_link_gurls;
452 std::vector<base::string16>::const_iterator itr;
453 for (itr = visited_link_strings.begin(); itr != visited_link_strings.end();
454 ++itr) {
455 visited_link_gurls.push_back(GURL(*itr));
458 AwBrowserContext::FromWebContents(web_contents_.get())
459 ->AddVisitedURLs(visited_link_gurls);
462 bool RegisterAwContents(JNIEnv* env) {
463 return RegisterNativesImpl(env);
466 namespace {
468 void ShowGeolocationPromptHelperTask(const JavaObjectWeakGlobalRef& java_ref,
469 const GURL& origin) {
470 JNIEnv* env = AttachCurrentThread();
471 ScopedJavaLocalRef<jobject> j_ref = java_ref.get(env);
472 if (j_ref.obj()) {
473 ScopedJavaLocalRef<jstring> j_origin(
474 ConvertUTF8ToJavaString(env, origin.spec()));
475 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
476 "onGeolocationPermissionsShowPrompt");
477 Java_AwContents_onGeolocationPermissionsShowPrompt(env,
478 j_ref.obj(),
479 j_origin.obj());
483 void ShowGeolocationPromptHelper(const JavaObjectWeakGlobalRef& java_ref,
484 const GURL& origin) {
485 JNIEnv* env = AttachCurrentThread();
486 if (java_ref.get(env).obj()) {
487 content::BrowserThread::PostTask(
488 content::BrowserThread::UI,
489 FROM_HERE,
490 base::Bind(&ShowGeolocationPromptHelperTask,
491 java_ref,
492 origin));
496 } // anonymous namespace
498 void AwContents::ShowGeolocationPrompt(const GURL& requesting_frame,
499 base::Callback<void(bool)> callback) {
500 DCHECK_CURRENTLY_ON(BrowserThread::UI);
502 GURL origin = requesting_frame.GetOrigin();
503 bool show_prompt = pending_geolocation_prompts_.empty();
504 pending_geolocation_prompts_.push_back(OriginCallback(origin, callback));
505 if (show_prompt) {
506 ShowGeolocationPromptHelper(java_ref_, origin);
510 // Invoked from Java
511 void AwContents::InvokeGeolocationCallback(JNIEnv* env,
512 jobject obj,
513 jboolean value,
514 jstring origin) {
515 DCHECK_CURRENTLY_ON(BrowserThread::UI);
516 if (pending_geolocation_prompts_.empty())
517 return;
519 GURL callback_origin(base::android::ConvertJavaStringToUTF16(env, origin));
520 if (callback_origin.GetOrigin() ==
521 pending_geolocation_prompts_.front().first) {
522 pending_geolocation_prompts_.front().second.Run(value);
523 pending_geolocation_prompts_.pop_front();
524 if (!pending_geolocation_prompts_.empty()) {
525 ShowGeolocationPromptHelper(java_ref_,
526 pending_geolocation_prompts_.front().first);
531 void AwContents::HideGeolocationPrompt(const GURL& origin) {
532 DCHECK_CURRENTLY_ON(BrowserThread::UI);
533 bool removed_current_outstanding_callback = false;
534 std::list<OriginCallback>::iterator it = pending_geolocation_prompts_.begin();
535 while (it != pending_geolocation_prompts_.end()) {
536 if ((*it).first == origin.GetOrigin()) {
537 if (it == pending_geolocation_prompts_.begin()) {
538 removed_current_outstanding_callback = true;
540 it = pending_geolocation_prompts_.erase(it);
541 } else {
542 ++it;
546 if (removed_current_outstanding_callback) {
547 JNIEnv* env = AttachCurrentThread();
548 ScopedJavaLocalRef<jobject> j_ref = java_ref_.get(env);
549 if (j_ref.obj()) {
550 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
551 "onGeolocationPermissionsHidePrompt");
552 Java_AwContents_onGeolocationPermissionsHidePrompt(env, j_ref.obj());
554 if (!pending_geolocation_prompts_.empty()) {
555 ShowGeolocationPromptHelper(java_ref_,
556 pending_geolocation_prompts_.front().first);
561 void AwContents::OnPermissionRequest(
562 base::android::ScopedJavaLocalRef<jobject> j_request,
563 AwPermissionRequest* request) {
564 DCHECK(!j_request.is_null());
565 DCHECK(request);
567 JNIEnv* env = AttachCurrentThread();
568 ScopedJavaLocalRef<jobject> j_ref = java_ref_.get(env);
569 if (j_ref.is_null()) {
570 permission_request_handler_->CancelRequest(request->GetOrigin(),
571 request->GetResources());
572 return;
575 Java_AwContents_onPermissionRequest(env, j_ref.obj(), j_request.obj());
578 void AwContents::OnPermissionRequestCanceled(AwPermissionRequest* request) {
579 JNIEnv* env = AttachCurrentThread();
580 ScopedJavaLocalRef<jobject> j_request = request->GetJavaObject();
581 ScopedJavaLocalRef<jobject> j_ref = java_ref_.get(env);
582 if (j_request.is_null() || j_ref.is_null())
583 return;
585 Java_AwContents_onPermissionRequestCanceled(
586 env, j_ref.obj(), j_request.obj());
589 void AwContents::PreauthorizePermission(
590 JNIEnv* env,
591 jobject obj,
592 jstring origin,
593 jlong resources) {
594 permission_request_handler_->PreauthorizePermission(
595 GURL(base::android::ConvertJavaStringToUTF8(env, origin)), resources);
598 void AwContents::RequestProtectedMediaIdentifierPermission(
599 const GURL& origin,
600 const base::Callback<void(bool)>& callback) {
601 permission_request_handler_->SendRequest(
602 scoped_ptr<AwPermissionRequestDelegate>(new SimplePermissionRequest(
603 origin, AwPermissionRequest::ProtectedMediaId, callback)));
606 void AwContents::CancelProtectedMediaIdentifierPermissionRequests(
607 const GURL& origin) {
608 permission_request_handler_->CancelRequest(
609 origin, AwPermissionRequest::ProtectedMediaId);
612 void AwContents::RequestGeolocationPermission(
613 const GURL& origin,
614 const base::Callback<void(bool)>& callback) {
615 JNIEnv* env = AttachCurrentThread();
616 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
617 if (obj.is_null())
618 return;
620 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env, obj.obj())) {
621 ShowGeolocationPrompt(origin, callback);
622 return;
624 permission_request_handler_->SendRequest(
625 scoped_ptr<AwPermissionRequestDelegate>(new SimplePermissionRequest(
626 origin, AwPermissionRequest::Geolocation, callback)));
629 void AwContents::CancelGeolocationPermissionRequests(const GURL& origin) {
630 JNIEnv* env = AttachCurrentThread();
631 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
632 if (obj.is_null())
633 return;
635 if (Java_AwContents_useLegacyGeolocationPermissionAPI(env, obj.obj())) {
636 HideGeolocationPrompt(origin);
637 return;
639 permission_request_handler_->CancelRequest(
640 origin, AwPermissionRequest::Geolocation);
643 void AwContents::RequestMIDISysexPermission(
644 const GURL& origin,
645 const base::Callback<void(bool)>& callback) {
646 permission_request_handler_->SendRequest(
647 scoped_ptr<AwPermissionRequestDelegate>(new SimplePermissionRequest(
648 origin, AwPermissionRequest::MIDISysex, callback)));
651 void AwContents::CancelMIDISysexPermissionRequests(const GURL& origin) {
652 permission_request_handler_->CancelRequest(
653 origin, AwPermissionRequest::AwPermissionRequest::MIDISysex);
656 void AwContents::FindAllAsync(JNIEnv* env, jobject obj, jstring search_string) {
657 DCHECK_CURRENTLY_ON(BrowserThread::UI);
658 GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env, search_string));
661 void AwContents::FindNext(JNIEnv* env, jobject obj, jboolean forward) {
662 DCHECK_CURRENTLY_ON(BrowserThread::UI);
663 GetFindHelper()->FindNext(forward);
666 void AwContents::ClearMatches(JNIEnv* env, jobject obj) {
667 DCHECK_CURRENTLY_ON(BrowserThread::UI);
668 GetFindHelper()->ClearMatches();
671 void AwContents::ClearCache(
672 JNIEnv* env,
673 jobject obj,
674 jboolean include_disk_files) {
675 DCHECK_CURRENTLY_ON(BrowserThread::UI);
676 render_view_host_ext_->ClearCache();
678 if (include_disk_files) {
679 RemoveHttpDiskCache(web_contents_->GetBrowserContext(),
680 web_contents_->GetRoutingID());
684 FindHelper* AwContents::GetFindHelper() {
685 DCHECK_CURRENTLY_ON(BrowserThread::UI);
686 if (!find_helper_.get()) {
687 find_helper_.reset(new FindHelper(web_contents_.get()));
688 find_helper_->SetListener(this);
690 return find_helper_.get();
693 void AwContents::OnFindResultReceived(int active_ordinal,
694 int match_count,
695 bool finished) {
696 DCHECK_CURRENTLY_ON(BrowserThread::UI);
697 JNIEnv* env = AttachCurrentThread();
698 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
699 if (obj.is_null())
700 return;
702 Java_AwContents_onFindResultReceived(
703 env, obj.obj(), active_ordinal, match_count, finished);
706 bool AwContents::ShouldDownloadFavicon(const GURL& icon_url) {
707 return g_should_download_favicons;
710 void AwContents::OnReceivedIcon(const GURL& icon_url, const SkBitmap& bitmap) {
711 DCHECK_CURRENTLY_ON(BrowserThread::UI);
712 JNIEnv* env = AttachCurrentThread();
713 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
714 if (obj.is_null())
715 return;
717 content::NavigationEntry* entry =
718 web_contents_->GetController().GetLastCommittedEntry();
720 if (entry) {
721 entry->GetFavicon().valid = true;
722 entry->GetFavicon().url = icon_url;
723 entry->GetFavicon().image = gfx::Image::CreateFrom1xBitmap(bitmap);
726 Java_AwContents_onReceivedIcon(
727 env, obj.obj(), gfx::ConvertToJavaBitmap(&bitmap).obj());
730 void AwContents::OnReceivedTouchIconUrl(const std::string& url,
731 bool precomposed) {
732 DCHECK_CURRENTLY_ON(BrowserThread::UI);
733 JNIEnv* env = AttachCurrentThread();
734 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
735 if (obj.is_null())
736 return;
738 Java_AwContents_onReceivedTouchIconUrl(
739 env, obj.obj(), ConvertUTF8ToJavaString(env, url).obj(), precomposed);
742 bool AwContents::RequestDrawGL(bool wait_for_completion) {
743 DCHECK_CURRENTLY_ON(BrowserThread::UI);
744 JNIEnv* env = AttachCurrentThread();
745 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
746 if (obj.is_null())
747 return false;
748 return Java_AwContents_requestDrawGL(env, obj.obj(), wait_for_completion);
751 void AwContents::PostInvalidate() {
752 DCHECK_CURRENTLY_ON(BrowserThread::UI);
753 JNIEnv* env = AttachCurrentThread();
754 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
755 if (!obj.is_null())
756 Java_AwContents_postInvalidateOnAnimation(env, obj.obj());
759 void AwContents::OnNewPicture() {
760 DCHECK_CURRENTLY_ON(BrowserThread::UI);
761 JNIEnv* env = AttachCurrentThread();
762 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
763 if (!obj.is_null()) {
764 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
765 "onNewPicture");
766 Java_AwContents_onNewPicture(env, obj.obj());
770 base::android::ScopedJavaLocalRef<jbyteArray> AwContents::GetCertificate(
771 JNIEnv* env,
772 jobject obj) {
773 DCHECK_CURRENTLY_ON(BrowserThread::UI);
774 content::NavigationEntry* entry =
775 web_contents_->GetController().GetLastCommittedEntry();
776 if (!entry)
777 return ScopedJavaLocalRef<jbyteArray>();
778 // Get the certificate
779 int cert_id = entry->GetSSL().cert_id;
780 scoped_refptr<net::X509Certificate> cert;
781 bool ok = content::CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
782 if (!ok)
783 return ScopedJavaLocalRef<jbyteArray>();
785 // Convert the certificate and return it
786 std::string der_string;
787 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string);
788 return base::android::ToJavaByteArray(env,
789 reinterpret_cast<const uint8*>(der_string.data()), der_string.length());
792 void AwContents::RequestNewHitTestDataAt(JNIEnv* env,
793 jobject obj,
794 jfloat x,
795 jfloat y,
796 jfloat touch_major) {
797 DCHECK_CURRENTLY_ON(BrowserThread::UI);
798 gfx::PointF touch_center(x, y);
799 gfx::SizeF touch_area(touch_major, touch_major);
800 render_view_host_ext_->RequestNewHitTestDataAt(touch_center, touch_area);
803 void AwContents::UpdateLastHitTestData(JNIEnv* env, jobject obj) {
804 DCHECK_CURRENTLY_ON(BrowserThread::UI);
805 if (!render_view_host_ext_->HasNewHitTestData()) return;
807 const AwHitTestData& data = render_view_host_ext_->GetLastHitTestData();
808 render_view_host_ext_->MarkHitTestDataRead();
810 // Make sure to null the Java object if data is empty/invalid.
811 ScopedJavaLocalRef<jstring> extra_data_for_type;
812 if (data.extra_data_for_type.length())
813 extra_data_for_type = ConvertUTF8ToJavaString(
814 env, data.extra_data_for_type);
816 ScopedJavaLocalRef<jstring> href;
817 if (data.href.length())
818 href = ConvertUTF16ToJavaString(env, data.href);
820 ScopedJavaLocalRef<jstring> anchor_text;
821 if (data.anchor_text.length())
822 anchor_text = ConvertUTF16ToJavaString(env, data.anchor_text);
824 ScopedJavaLocalRef<jstring> img_src;
825 if (data.img_src.is_valid())
826 img_src = ConvertUTF8ToJavaString(env, data.img_src.spec());
828 Java_AwContents_updateHitTestData(env,
829 obj,
830 data.type,
831 extra_data_for_type.obj(),
832 href.obj(),
833 anchor_text.obj(),
834 img_src.obj());
837 void AwContents::OnSizeChanged(JNIEnv* env, jobject obj,
838 int w, int h, int ow, int oh) {
839 DCHECK_CURRENTLY_ON(BrowserThread::UI);
840 browser_view_renderer_.OnSizeChanged(w, h);
843 void AwContents::SetViewVisibility(JNIEnv* env, jobject obj, bool visible) {
844 DCHECK_CURRENTLY_ON(BrowserThread::UI);
845 browser_view_renderer_.SetViewVisibility(visible);
848 void AwContents::SetWindowVisibility(JNIEnv* env, jobject obj, bool visible) {
849 DCHECK_CURRENTLY_ON(BrowserThread::UI);
850 browser_view_renderer_.SetWindowVisibility(visible);
853 void AwContents::SetIsPaused(JNIEnv* env, jobject obj, bool paused) {
854 DCHECK_CURRENTLY_ON(BrowserThread::UI);
855 browser_view_renderer_.SetIsPaused(paused);
856 ContentViewCore* cvc =
857 ContentViewCore::FromWebContents(web_contents_.get());
858 if (cvc) {
859 cvc->PauseOrResumeGeolocation(paused);
863 void AwContents::OnAttachedToWindow(JNIEnv* env, jobject obj, int w, int h) {
864 DCHECK_CURRENTLY_ON(BrowserThread::UI);
865 browser_view_renderer_.OnAttachedToWindow(w, h);
868 void AwContents::OnDetachedFromWindow(JNIEnv* env, jobject obj) {
869 DCHECK_CURRENTLY_ON(BrowserThread::UI);
870 browser_view_renderer_.OnDetachedFromWindow();
873 void AwContents::DetachFunctorFromView() {
874 JNIEnv* env = AttachCurrentThread();
875 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
876 if (!obj.is_null())
877 Java_AwContents_detachFunctorFromView(env, obj.obj());
880 base::android::ScopedJavaLocalRef<jbyteArray>
881 AwContents::GetOpaqueState(JNIEnv* env, jobject obj) {
882 DCHECK_CURRENTLY_ON(BrowserThread::UI);
883 // Required optimization in WebViewClassic to not save any state if
884 // there has been no navigations.
885 if (!web_contents_->GetController().GetEntryCount())
886 return ScopedJavaLocalRef<jbyteArray>();
888 base::Pickle pickle;
889 if (!WriteToPickle(*web_contents_, &pickle)) {
890 return ScopedJavaLocalRef<jbyteArray>();
891 } else {
892 return base::android::ToJavaByteArray(env,
893 reinterpret_cast<const uint8*>(pickle.data()), pickle.size());
897 jboolean AwContents::RestoreFromOpaqueState(
898 JNIEnv* env, jobject obj, jbyteArray state) {
899 DCHECK_CURRENTLY_ON(BrowserThread::UI);
900 // TODO(boliu): This copy can be optimized out if this is a performance
901 // problem.
902 std::vector<uint8> state_vector;
903 base::android::JavaByteArrayToByteVector(env, state, &state_vector);
905 base::Pickle pickle(reinterpret_cast<const char*>(state_vector.data()),
906 state_vector.size());
907 base::PickleIterator iterator(pickle);
909 return RestoreFromPickle(&iterator, web_contents_.get());
912 bool AwContents::OnDraw(JNIEnv* env,
913 jobject obj,
914 jobject canvas,
915 jboolean is_hardware_accelerated,
916 jint scroll_x,
917 jint scroll_y,
918 jint visible_left,
919 jint visible_top,
920 jint visible_right,
921 jint visible_bottom) {
922 DCHECK_CURRENTLY_ON(BrowserThread::UI);
923 gfx::Vector2d scroll(scroll_x, scroll_y);
924 browser_view_renderer_.PrepareToDraw(
925 scroll, gfx::Rect(visible_left, visible_top, visible_right - visible_left,
926 visible_bottom - visible_top));
927 if (is_hardware_accelerated && browser_view_renderer_.attached_to_window() &&
928 !g_force_auxiliary_bitmap_rendering) {
929 return browser_view_renderer_.OnDrawHardware();
932 gfx::Size view_size = browser_view_renderer_.size();
933 if (view_size.IsEmpty()) {
934 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_EmptySize",
935 TRACE_EVENT_SCOPE_THREAD);
936 return false;
939 // TODO(hush): Right now webview size is passed in as the auxiliary bitmap
940 // size, which might hurt performace (only for software draws with auxiliary
941 // bitmap). For better performance, get global visible rect, transform it
942 // from screen space to view space, then intersect with the webview in
943 // viewspace. Use the resulting rect as the auxiliary bitmap.
944 scoped_ptr<SoftwareCanvasHolder> canvas_holder = SoftwareCanvasHolder::Create(
945 canvas, scroll, view_size, g_force_auxiliary_bitmap_rendering);
946 if (!canvas_holder || !canvas_holder->GetCanvas()) {
947 TRACE_EVENT_INSTANT0("android_webview", "EarlyOut_NoSoftwareCanvas",
948 TRACE_EVENT_SCOPE_THREAD);
949 return false;
951 return browser_view_renderer_.OnDrawSoftware(canvas_holder->GetCanvas());
954 void AwContents::SetPendingWebContentsForPopup(
955 scoped_ptr<content::WebContents> pending) {
956 DCHECK_CURRENTLY_ON(BrowserThread::UI);
957 if (pending_contents_.get()) {
958 // TODO(benm): Support holding multiple pop up window requests.
959 LOG(WARNING) << "Blocking popup window creation as an outstanding "
960 << "popup window is still pending.";
961 base::MessageLoop::current()->DeleteSoon(FROM_HERE, pending.release());
962 return;
964 pending_contents_.reset(new AwContents(pending.Pass()));
965 // Set dip_scale for pending contents, which is necessary for the later
966 // SynchronousCompositor and InputHandler setup.
967 pending_contents_->SetDipScaleInternal(browser_view_renderer_.dip_scale());
970 void AwContents::FocusFirstNode(JNIEnv* env, jobject obj) {
971 DCHECK_CURRENTLY_ON(BrowserThread::UI);
972 web_contents_->FocusThroughTabTraversal(false);
975 void AwContents::SetBackgroundColor(JNIEnv* env, jobject obj, jint color) {
976 DCHECK_CURRENTLY_ON(BrowserThread::UI);
977 render_view_host_ext_->SetBackgroundColor(color);
980 void AwContents::OnComputeScroll(JNIEnv* env,
981 jobject obj,
982 jlong animation_time_millis) {
983 DCHECK_CURRENTLY_ON(BrowserThread::UI);
984 browser_view_renderer_.OnComputeScroll(
985 base::TimeTicks() +
986 base::TimeDelta::FromMilliseconds(animation_time_millis));
989 jlong AwContents::ReleasePopupAwContents(JNIEnv* env, jobject obj) {
990 DCHECK_CURRENTLY_ON(BrowserThread::UI);
991 return reinterpret_cast<intptr_t>(pending_contents_.release());
994 gfx::Point AwContents::GetLocationOnScreen() {
995 DCHECK_CURRENTLY_ON(BrowserThread::UI);
996 JNIEnv* env = AttachCurrentThread();
997 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
998 if (obj.is_null())
999 return gfx::Point();
1000 std::vector<int> location;
1001 base::android::JavaIntArrayToIntVector(
1002 env,
1003 Java_AwContents_getLocationOnScreen(env, obj.obj()).obj(),
1004 &location);
1005 return gfx::Point(location[0], location[1]);
1008 void AwContents::ScrollContainerViewTo(gfx::Vector2d new_value) {
1009 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1010 JNIEnv* env = AttachCurrentThread();
1011 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1012 if (obj.is_null())
1013 return;
1014 Java_AwContents_scrollContainerViewTo(
1015 env, obj.obj(), new_value.x(), new_value.y());
1018 void AwContents::UpdateScrollState(gfx::Vector2d max_scroll_offset,
1019 gfx::SizeF contents_size_dip,
1020 float page_scale_factor,
1021 float min_page_scale_factor,
1022 float max_page_scale_factor) {
1023 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1024 JNIEnv* env = AttachCurrentThread();
1025 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1026 if (obj.is_null())
1027 return;
1028 Java_AwContents_updateScrollState(env,
1029 obj.obj(),
1030 max_scroll_offset.x(),
1031 max_scroll_offset.y(),
1032 contents_size_dip.width(),
1033 contents_size_dip.height(),
1034 page_scale_factor,
1035 min_page_scale_factor,
1036 max_page_scale_factor);
1039 void AwContents::DidOverscroll(gfx::Vector2d overscroll_delta,
1040 gfx::Vector2dF overscroll_velocity) {
1041 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1042 JNIEnv* env = AttachCurrentThread();
1043 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1044 if (obj.is_null())
1045 return;
1046 Java_AwContents_didOverscroll(env, obj.obj(), overscroll_delta.x(),
1047 overscroll_delta.y(), overscroll_velocity.x(),
1048 overscroll_velocity.y());
1051 void AwContents::SetDipScale(JNIEnv* env, jobject obj, jfloat dip_scale) {
1052 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1053 SetDipScaleInternal(dip_scale);
1056 void AwContents::SetDipScaleInternal(float dip_scale) {
1057 browser_view_renderer_.SetDipScale(dip_scale);
1060 void AwContents::ScrollTo(JNIEnv* env, jobject obj, jint x, jint y) {
1061 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1062 browser_view_renderer_.ScrollTo(gfx::Vector2d(x, y));
1065 void AwContents::SmoothScroll(JNIEnv* env,
1066 jobject obj,
1067 jint target_x,
1068 jint target_y,
1069 jlong duration_ms) {
1070 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1072 float scale = browser_view_renderer_.dip_scale() *
1073 browser_view_renderer_.page_scale_factor();
1074 render_view_host_ext_->SmoothScroll(target_x / scale, target_y / scale,
1075 duration_ms);
1078 void AwContents::OnWebLayoutPageScaleFactorChanged(float page_scale_factor) {
1079 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1080 JNIEnv* env = AttachCurrentThread();
1081 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1082 if (obj.is_null())
1083 return;
1084 Java_AwContents_onWebLayoutPageScaleFactorChanged(env, obj.obj(),
1085 page_scale_factor);
1088 void AwContents::OnWebLayoutContentsSizeChanged(
1089 const gfx::Size& contents_size) {
1090 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1091 JNIEnv* env = AttachCurrentThread();
1092 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1093 if (obj.is_null())
1094 return;
1095 Java_AwContents_onWebLayoutContentsSizeChanged(
1096 env, obj.obj(), contents_size.width(), contents_size.height());
1099 jlong AwContents::CapturePicture(JNIEnv* env,
1100 jobject obj,
1101 int width,
1102 int height) {
1103 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1104 return reinterpret_cast<intptr_t>(
1105 new AwPicture(browser_view_renderer_.CapturePicture(width, height)));
1108 void AwContents::EnableOnNewPicture(JNIEnv* env,
1109 jobject obj,
1110 jboolean enabled) {
1111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1112 browser_view_renderer_.EnableOnNewPicture(enabled);
1115 namespace {
1116 void InvokeVisualStateCallback(const JavaObjectWeakGlobalRef& java_ref,
1117 long request_id,
1118 ScopedJavaGlobalRef<jobject>* callback,
1119 bool result) {
1120 JNIEnv* env = AttachCurrentThread();
1121 ScopedJavaLocalRef<jobject> obj = java_ref.get(env);
1122 if (obj.is_null())
1123 return;
1124 Java_AwContents_invokeVisualStateCallback(
1125 env, obj.obj(), callback->obj(), request_id);
1127 } // namespace
1129 void AwContents::InsertVisualStateCallback(
1130 JNIEnv* env, jobject obj, long request_id, jobject callback) {
1131 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1132 ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>();
1133 j_callback->Reset(env, callback);
1134 web_contents_->GetMainFrame()->InsertVisualStateCallback(
1135 base::Bind(&InvokeVisualStateCallback, java_ref_, request_id,
1136 base::Owned(j_callback)));
1139 void AwContents::ClearView(JNIEnv* env, jobject obj) {
1140 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1141 browser_view_renderer_.ClearView();
1144 void AwContents::SetExtraHeadersForUrl(JNIEnv* env, jobject obj,
1145 jstring url, jstring jextra_headers) {
1146 std::string extra_headers;
1147 if (jextra_headers)
1148 extra_headers = ConvertJavaStringToUTF8(env, jextra_headers);
1149 AwResourceContext* resource_context = static_cast<AwResourceContext*>(
1150 AwBrowserContext::FromWebContents(web_contents_.get())->
1151 GetResourceContext());
1152 resource_context->SetExtraHeaders(GURL(ConvertJavaStringToUTF8(env, url)),
1153 extra_headers);
1156 void AwContents::SetJsOnlineProperty(JNIEnv* env,
1157 jobject obj,
1158 jboolean network_up) {
1159 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1160 render_view_host_ext_->SetJsOnlineProperty(network_up);
1163 void AwContents::TrimMemory(JNIEnv* env,
1164 jobject obj,
1165 jint level,
1166 jboolean visible) {
1167 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1168 browser_view_renderer_.TrimMemory(level, visible);
1171 // TODO(sgurun) add support for posting a frame whose name is known (only
1172 // main frame is supported at this time, see crbug.com/389721)
1173 void AwContents::PostMessageToFrame(JNIEnv* env, jobject obj,
1174 jstring frame_name, jstring message, jstring target_origin,
1175 jintArray sent_ports) {
1177 // Use an empty source origin for android webview.
1178 base::string16 source_origin;
1179 base::string16 j_target_origin(ConvertJavaStringToUTF16(env, target_origin));
1180 base::string16 j_message(ConvertJavaStringToUTF16(env, message));
1181 std::vector<int> j_ports;
1183 if (sent_ports != nullptr) {
1184 base::android::JavaIntArrayToIntVector(env, sent_ports, &j_ports);
1185 BrowserThread::PostTask(
1186 BrowserThread::IO,
1187 FROM_HERE,
1188 base::Bind(&AwMessagePortServiceImpl::RemoveSentPorts,
1189 base::Unretained(AwMessagePortServiceImpl::GetInstance()),
1190 j_ports));
1192 std::vector<content::TransferredMessagePort> ports(j_ports.size());
1193 for (size_t i = 0; i < j_ports.size(); ++i)
1194 ports[i].id = j_ports[i];
1195 content::MessagePortProvider::PostMessageToFrame(web_contents_.get(),
1196 source_origin,
1197 j_target_origin,
1198 j_message,
1199 ports);
1202 scoped_refptr<AwMessagePortMessageFilter>
1203 AwContents::GetMessagePortMessageFilter() {
1204 // Create a message port message filter if necessary
1205 if (message_port_message_filter_.get() == nullptr) {
1206 message_port_message_filter_ =
1207 new AwMessagePortMessageFilter(
1208 web_contents_->GetMainFrame()->GetRoutingID());
1209 web_contents_->GetRenderProcessHost()->AddFilter(
1210 message_port_message_filter_.get());
1212 return message_port_message_filter_;
1215 void AwContents::CreateMessageChannel(JNIEnv* env, jobject obj,
1216 jobjectArray ports) {
1218 AwMessagePortServiceImpl::GetInstance()->CreateMessageChannel(env, ports,
1219 GetMessagePortMessageFilter());
1222 void AwContents::GrantFileSchemeAccesstoChildProcess(JNIEnv* env, jobject obj) {
1223 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
1224 web_contents_->GetRenderProcessHost()->GetID(), url::kFileScheme);
1227 void SetShouldDownloadFavicons(JNIEnv* env,
1228 const JavaParamRef<jclass>& jclazz) {
1229 g_should_download_favicons = true;
1232 } // namespace android_webview