// Copyright 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "android_webview/native/aw_contents.h" #include "android_webview/browser/aw_browser_context.h" #include "android_webview/browser/aw_browser_main_parts.h" #include "android_webview/browser/browser_view_renderer_impl.h" #include "android_webview/browser/gpu_memory_buffer_impl.h" #include "android_webview/browser/net_disk_cache_remover.h" #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h" #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h" #include "android_webview/common/aw_hit_test_data.h" #include "android_webview/native/aw_browser_dependency_factory.h" #include "android_webview/native/aw_contents_client_bridge.h" #include "android_webview/native/aw_contents_io_thread_client_impl.h" #include "android_webview/native/aw_web_contents_delegate.h" #include "android_webview/native/java_browser_view_renderer_helper.h" #include "android_webview/native/state_serializer.h" #include "android_webview/public/browser/draw_gl.h" #include "base/android/jni_android.h" #include "base/android/jni_array.h" #include "base/android/jni_string.h" #include "base/android/scoped_java_ref.h" #include "base/bind.h" #include "base/callback.h" #include "base/message_loop.h" #include "base/pickle.h" #include "base/string16.h" #include "base/supports_user_data.h" #include "components/navigation_interception/intercept_navigation_delegate.h" #include "content/public/browser/android/content_view_core.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/cert_store.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/common/ssl_status.h" #include "jni/AwContents_jni.h" #include "net/cert/x509_certificate.h" #include "ui/gfx/android/java_bitmap.h" struct AwDrawSWFunctionTable; struct AwDrawGLFunctionTable; using base::android::AttachCurrentThread; using base::android::ConvertJavaStringToUTF16; using base::android::ConvertJavaStringToUTF8; using base::android::ConvertUTF16ToJavaString; using base::android::ConvertUTF8ToJavaString; using base::android::JavaRef; using base::android::ScopedJavaGlobalRef; using base::android::ScopedJavaLocalRef; using components::InterceptNavigationDelegate; using content::BrowserThread; using content::ContentViewCore; using content::WebContents; extern "C" { static AwDrawGLFunction DrawGLFunction; static void DrawGLFunction(int view_context, AwDrawGLInfo* draw_info, void* spare) { // |view_context| is the value that was returned from the java // AwContents.onPrepareDrawGL; this cast must match the code there. reinterpret_cast(view_context)->DrawGL( draw_info); } } namespace android_webview { namespace { static JavaBrowserViewRendererHelper java_renderer_helper; const void* kAwContentsUserDataKey = &kAwContentsUserDataKey; class AwContentsUserData : public base::SupportsUserData::Data { public: AwContentsUserData(AwContents* ptr) : contents_(ptr) {} static AwContents* GetContents(WebContents* web_contents) { if (!web_contents) return NULL; AwContentsUserData* data = reinterpret_cast( web_contents->GetUserData(kAwContentsUserDataKey)); return data ? data->contents_ : NULL; } private: AwContents* contents_; }; } // namespace // static AwContents* AwContents::FromWebContents(WebContents* web_contents) { return AwContentsUserData::GetContents(web_contents); } // static AwContents* AwContents::FromID(int render_process_id, int render_view_id) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); const content::RenderViewHost* rvh = content::RenderViewHost::FromID(render_process_id, render_view_id); if (!rvh) return NULL; content::WebContents* web_contents = content::WebContents::FromRenderViewHost(rvh); if (!web_contents) return NULL; return FromWebContents(web_contents); } AwContents::AwContents(JNIEnv* env, jobject obj, jobject web_contents_delegate, jobject contents_client_bridge) : java_ref_(env, obj), web_contents_delegate_( new AwWebContentsDelegate(env, web_contents_delegate)), contents_client_bridge_( new AwContentsClientBridge(env, contents_client_bridge)), ALLOW_THIS_IN_INITIALIZER_LIST(browser_view_renderer_( BrowserViewRendererImpl::Create(this, &java_renderer_helper))) { android_webview::AwBrowserDependencyFactory* dependency_factory = android_webview::AwBrowserDependencyFactory::GetInstance(); // TODO(joth): rather than create and set the WebContents here, expose the // factory method to java side and have that orchestrate the construction // order. SetWebContents(dependency_factory->CreateWebContents()); } void AwContents::SetWebContents(content::WebContents* web_contents) { web_contents_.reset(web_contents); if (find_helper_.get()) { find_helper_->SetListener(NULL); } icon_helper_.reset(new IconHelper(web_contents_.get())); icon_helper_->SetListener(this); web_contents_->SetUserData(kAwContentsUserDataKey, new AwContentsUserData(this)); AwContentsClientBridgeBase::Associate(web_contents_.get(), contents_client_bridge_.get()); web_contents_->SetDelegate(web_contents_delegate_.get()); render_view_host_ext_.reset(new AwRenderViewHostExt(web_contents_.get())); } void AwContents::SetWebContents(JNIEnv* env, jobject obj, jint new_wc) { SetWebContents(reinterpret_cast(new_wc)); } AwContents::~AwContents() { DCHECK(AwContents::FromWebContents(web_contents_.get()) == this); web_contents_->RemoveUserData(kAwContentsUserDataKey); if (find_helper_.get()) find_helper_->SetListener(NULL); if (icon_helper_.get()) icon_helper_->SetListener(NULL); } jint AwContents::GetWebContents(JNIEnv* env, jobject obj) { return reinterpret_cast(web_contents_.get()); } void AwContents::DidInitializeContentViewCore(JNIEnv* env, jobject obj, jint content_view_core) { ContentViewCore* core = reinterpret_cast(content_view_core); DCHECK(core == ContentViewCore::FromWebContents(web_contents_.get())); browser_view_renderer_->SetContents(core); } void AwContents::Destroy(JNIEnv* env, jobject obj) { delete this; } // static void SetAwDrawSWFunctionTable(JNIEnv* env, jclass, jint function_table) { BrowserViewRendererImpl::SetAwDrawSWFunctionTable( reinterpret_cast(function_table)); } // static void SetAwDrawGLFunctionTable(JNIEnv* env, jclass, jint function_table) { GpuMemoryBufferImpl::SetAwDrawGLFunctionTable( reinterpret_cast(function_table)); } // static jint GetAwDrawGLFunction(JNIEnv* env, jclass) { return reinterpret_cast(&DrawGLFunction); } jint AwContents::GetAwDrawGLViewContext(JNIEnv* env, jobject obj) { return reinterpret_cast(browser_view_renderer_.get()); } namespace { void DocumentHasImagesCallback(const ScopedJavaGlobalRef& message, bool has_images) { Java_AwContents_onDocumentHasImagesResponse(AttachCurrentThread(), has_images, message.obj()); } } // namespace void AwContents::DocumentHasImages(JNIEnv* env, jobject obj, jobject message) { ScopedJavaGlobalRef j_message; j_message.Reset(env, message); render_view_host_ext_->DocumentHasImages( base::Bind(&DocumentHasImagesCallback, j_message)); } namespace { void GenerateMHTMLCallback(ScopedJavaGlobalRef* callback, const base::FilePath& path, int64 size) { JNIEnv* env = AttachCurrentThread(); // Android files are UTF8, so the path conversion below is safe. Java_AwContents_generateMHTMLCallback( env, ConvertUTF8ToJavaString(env, path.AsUTF8Unsafe()).obj(), size, callback->obj()); } } // namespace void AwContents::GenerateMHTML(JNIEnv* env, jobject obj, jstring jpath, jobject callback) { ScopedJavaGlobalRef* j_callback = new ScopedJavaGlobalRef(); j_callback->Reset(env, callback); web_contents_->GenerateMHTML( base::FilePath(ConvertJavaStringToUTF8(env, jpath)), base::Bind(&GenerateMHTMLCallback, base::Owned(j_callback))); } void AwContents::PerformLongClick() { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (obj.is_null()) return; Java_AwContents_performLongClick(env, obj.obj()); } bool AwContents::OnReceivedHttpAuthRequest(const JavaRef& handler, const std::string& host, const std::string& realm) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (obj.is_null()) return false; ScopedJavaLocalRef jhost = ConvertUTF8ToJavaString(env, host); ScopedJavaLocalRef jrealm = ConvertUTF8ToJavaString(env, realm); Java_AwContents_onReceivedHttpAuthRequest(env, obj.obj(), handler.obj(), jhost.obj(), jrealm.obj()); return true; } void AwContents::SetIoThreadClient(JNIEnv* env, jobject obj, jobject client) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); AwContentsIoThreadClientImpl::Associate( web_contents_.get(), ScopedJavaLocalRef(env, client)); int child_id = web_contents_->GetRenderProcessHost()->GetID(); int route_id = web_contents_->GetRoutingID(); AwResourceDispatcherHostDelegate::OnIoThreadClientReady(child_id, route_id); } void AwContents::SetInterceptNavigationDelegate(JNIEnv* env, jobject obj, jobject delegate) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); InterceptNavigationDelegate::Associate( web_contents_.get(), make_scoped_ptr(new InterceptNavigationDelegate(env, delegate))); } void AwContents::AddVisitedLinks(JNIEnv* env, jobject obj, jobjectArray jvisited_links) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); std::vector visited_link_strings; base::android::AppendJavaStringArrayToStringVector( env, jvisited_links, &visited_link_strings); std::vector visited_link_gurls; for (std::vector::const_iterator itr = visited_link_strings.begin(); itr != visited_link_strings.end(); ++itr) { visited_link_gurls.push_back(GURL(*itr)); } AwBrowserContext::FromWebContents(web_contents_.get()) ->AddVisitedURLs(visited_link_gurls); } static jint Init(JNIEnv* env, jobject obj, jobject web_contents_delegate, jobject contents_client_bridge) { AwContents* tab = new AwContents(env, obj, web_contents_delegate, contents_client_bridge); return reinterpret_cast(tab); } bool RegisterAwContents(JNIEnv* env) { return RegisterNativesImpl(env) >= 0; } namespace { void ShowGeolocationPromptHelperTask(const JavaObjectWeakGlobalRef& java_ref, const GURL& origin) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef j_ref = java_ref.get(env); if (j_ref.obj()) { ScopedJavaLocalRef j_origin( ConvertUTF8ToJavaString(env, origin.spec())); Java_AwContents_onGeolocationPermissionsShowPrompt(env, j_ref.obj(), j_origin.obj()); } } void ShowGeolocationPromptHelper(const JavaObjectWeakGlobalRef& java_ref, const GURL& origin) { JNIEnv* env = AttachCurrentThread(); if (java_ref.get(env).obj()) { content::BrowserThread::PostTask( content::BrowserThread::UI, FROM_HERE, base::Bind(&ShowGeolocationPromptHelperTask, java_ref, origin)); } } } // anonymous namespace void AwContents::ShowGeolocationPrompt(const GURL& requesting_frame, base::Callback callback) { GURL origin = requesting_frame.GetOrigin(); bool show_prompt = pending_geolocation_prompts_.empty(); pending_geolocation_prompts_.push_back(OriginCallback(origin, callback)); if (show_prompt) { ShowGeolocationPromptHelper(java_ref_, origin); } } // Invoked from Java void AwContents::InvokeGeolocationCallback(JNIEnv* env, jobject obj, jboolean value, jstring origin) { GURL callback_origin(base::android::ConvertJavaStringToUTF16(env, origin)); if (callback_origin.GetOrigin() == pending_geolocation_prompts_.front().first) { pending_geolocation_prompts_.front().second.Run(value); pending_geolocation_prompts_.pop_front(); if (!pending_geolocation_prompts_.empty()) { ShowGeolocationPromptHelper(java_ref_, pending_geolocation_prompts_.front().first); } } } void AwContents::HideGeolocationPrompt(const GURL& origin) { bool removed_current_outstanding_callback = false; std::list::iterator it = pending_geolocation_prompts_.begin(); while (it != pending_geolocation_prompts_.end()) { if ((*it).first == origin.GetOrigin()) { if (it == pending_geolocation_prompts_.begin()) { removed_current_outstanding_callback = true; } it = pending_geolocation_prompts_.erase(it); } else { ++it; } } if (removed_current_outstanding_callback) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef j_ref = java_ref_.get(env); if (j_ref.obj()) { Java_AwContents_onGeolocationPermissionsHidePrompt(env, j_ref.obj()); } if (!pending_geolocation_prompts_.empty()) { ShowGeolocationPromptHelper(java_ref_, pending_geolocation_prompts_.front().first); } } } void AwContents::FindAllAsync(JNIEnv* env, jobject obj, jstring search_string) { GetFindHelper()->FindAllAsync(ConvertJavaStringToUTF16(env, search_string)); } void AwContents::FindNext(JNIEnv* env, jobject obj, jboolean forward) { GetFindHelper()->FindNext(forward); } void AwContents::ClearMatches(JNIEnv* env, jobject obj) { GetFindHelper()->ClearMatches(); } void AwContents::ClearCache( JNIEnv* env, jobject obj, jboolean include_disk_files) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); render_view_host_ext_->ClearCache(); if (include_disk_files) { RemoveHttpDiskCache(web_contents_->GetBrowserContext(), web_contents_->GetRoutingID()); } } FindHelper* AwContents::GetFindHelper() { if (!find_helper_.get()) { find_helper_.reset(new FindHelper(web_contents_.get())); find_helper_->SetListener(this); } return find_helper_.get(); } void AwContents::OnFindResultReceived(int active_ordinal, int match_count, bool finished) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (obj.is_null()) return; Java_AwContents_onFindResultReceived( env, obj.obj(), active_ordinal, match_count, finished); } void AwContents::OnReceivedIcon(const SkBitmap& bitmap) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (obj.is_null()) return; Java_AwContents_onReceivedIcon( env, obj.obj(), gfx::ConvertToJavaBitmap(&bitmap).obj()); content::NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); if (!entry || entry->GetURL().is_empty()) return; // TODO(acleung): Get the last history entry and set // the icon. } void AwContents::OnReceivedTouchIconUrl(const std::string& url, bool precomposed) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (obj.is_null()) return; Java_AwContents_onReceivedTouchIconUrl( env, obj.obj(), ConvertUTF8ToJavaString(env, url).obj(), precomposed); } void AwContents::Invalidate() { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (!obj.is_null()) Java_AwContents_invalidate(env, obj.obj()); } void AwContents::OnNewPicture(const JavaRef& picture) { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (!obj.is_null()) Java_AwContents_onNewPicture(env, obj.obj(), picture.obj()); } base::android::ScopedJavaLocalRef AwContents::GetCertificate(JNIEnv* env, jobject obj) { content::NavigationEntry* entry = web_contents_->GetController().GetActiveEntry(); if (!entry) return ScopedJavaLocalRef(); // Get the certificate int cert_id = entry->GetSSL().cert_id; scoped_refptr cert; bool ok = content::CertStore::GetInstance()->RetrieveCert(cert_id, &cert); if (!ok) return ScopedJavaLocalRef(); // Convert the certificate and return it std::string der_string; net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string); return base::android::ToJavaByteArray(env, reinterpret_cast(der_string.data()), der_string.length()); } void AwContents::RequestNewHitTestDataAt(JNIEnv* env, jobject obj, jint x, jint y) { render_view_host_ext_->RequestNewHitTestDataAt(x, y); } void AwContents::UpdateLastHitTestData(JNIEnv* env, jobject obj) { if (!render_view_host_ext_->HasNewHitTestData()) return; const AwHitTestData& data = render_view_host_ext_->GetLastHitTestData(); render_view_host_ext_->MarkHitTestDataRead(); // Make sure to null the Java object if data is empty/invalid. ScopedJavaLocalRef extra_data_for_type; if (data.extra_data_for_type.length()) extra_data_for_type = ConvertUTF8ToJavaString( env, data.extra_data_for_type); ScopedJavaLocalRef href; if (data.href.length()) href = ConvertUTF16ToJavaString(env, data.href); ScopedJavaLocalRef anchor_text; if (data.anchor_text.length()) anchor_text = ConvertUTF16ToJavaString(env, data.anchor_text); ScopedJavaLocalRef img_src; if (data.img_src.is_valid()) img_src = ConvertUTF8ToJavaString(env, data.img_src.spec()); Java_AwContents_updateHitTestData(env, obj, data.type, extra_data_for_type.obj(), href.obj(), anchor_text.obj(), img_src.obj()); } void AwContents::OnSizeChanged(JNIEnv* env, jobject obj, int w, int h, int ow, int oh) { browser_view_renderer_->OnSizeChanged(w, h); } void AwContents::SetWindowViewVisibility(JNIEnv* env, jobject obj, bool window_visible, bool view_visible) { browser_view_renderer_->OnVisibilityChanged(window_visible, view_visible); } void AwContents::OnAttachedToWindow(JNIEnv* env, jobject obj, int w, int h) { browser_view_renderer_->OnAttachedToWindow(w, h); } void AwContents::OnDetachedFromWindow(JNIEnv* env, jobject obj) { browser_view_renderer_->OnDetachedFromWindow(); } base::android::ScopedJavaLocalRef AwContents::GetOpaqueState(JNIEnv* env, jobject obj) { // Required optimization in WebViewClassic to not save any state if // there has been no navigations. if (!web_contents_->GetController().GetEntryCount()) return ScopedJavaLocalRef(); Pickle pickle; if (!WriteToPickle(*web_contents_, &pickle)) { return ScopedJavaLocalRef(); } else { return base::android::ToJavaByteArray(env, reinterpret_cast(pickle.data()), pickle.size()); } } jboolean AwContents::RestoreFromOpaqueState( JNIEnv* env, jobject obj, jbyteArray state) { // TODO(boliu): This copy can be optimized out if this is a performance // problem. std::vector state_vector; base::android::JavaByteArrayToByteVector(env, state, &state_vector); Pickle pickle(reinterpret_cast(state_vector.begin()), state_vector.size()); PickleIterator iterator(pickle); return RestoreFromPickle(&iterator, web_contents_.get()); } bool AwContents::DrawSW(JNIEnv* env, jobject obj, jobject canvas, jint clip_x, jint clip_y, jint clip_w, jint clip_h) { return browser_view_renderer_->DrawSW( canvas, gfx::Rect(clip_x, clip_y, clip_w, clip_h)); } void AwContents::SetScrollForHWFrame(JNIEnv* env, jobject obj, int scroll_x, int scroll_y) { browser_view_renderer_->SetScrollForHWFrame(scroll_x, scroll_y); } void AwContents::SetPendingWebContentsForPopup( scoped_ptr pending) { if (pending_contents_.get()) { // TODO(benm): Support holding multiple pop up window requests. LOG(WARNING) << "Blocking popup window creation as an outstanding " << "popup window is still pending."; MessageLoop::current()->DeleteSoon(FROM_HERE, pending.release()); return; } pending_contents_ = pending.Pass(); } void AwContents::FocusFirstNode(JNIEnv* env, jobject obj) { web_contents_->FocusThroughTabTraversal(false); } jint AwContents::ReleasePopupWebContents(JNIEnv* env, jobject obj) { return reinterpret_cast(pending_contents_.release()); } gfx::Point AwContents::GetLocationOnScreen() { JNIEnv* env = AttachCurrentThread(); ScopedJavaLocalRef obj = java_ref_.get(env); if (obj.is_null()) return gfx::Point(); std::vector location; base::android::JavaIntArrayToIntVector( env, Java_AwContents_getLocationOnScreen(env, obj.obj()).obj(), &location); return gfx::Point(location[0], location[1]); } ScopedJavaLocalRef AwContents::CapturePicture(JNIEnv* env, jobject obj) { return browser_view_renderer_->CapturePicture(); } void AwContents::EnableOnNewPicture(JNIEnv* env, jobject obj, jboolean enabled, jboolean invalidation_only) { BrowserViewRenderer::OnNewPictureMode mode = BrowserViewRenderer::kOnNewPictureDisabled; if (enabled) { mode = invalidation_only ? BrowserViewRenderer::kOnNewPictureInvalidationOnly : BrowserViewRenderer::kOnNewPictureEnabled; } browser_view_renderer_->EnableOnNewPicture(mode); } } // namespace android_webview