| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/tab_android.h" | 5 #include "chrome/browser/android/tab_android.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 #include "chrome/browser/ui/tab_helpers.h" | 47 #include "chrome/browser/ui/tab_helpers.h" |
| 48 #include "chrome/common/image_context_menu_renderer.mojom.h" | 48 #include "chrome/common/image_context_menu_renderer.mojom.h" |
| 49 #include "chrome/common/render_messages.h" | 49 #include "chrome/common/render_messages.h" |
| 50 #include "chrome/common/url_constants.h" | 50 #include "chrome/common/url_constants.h" |
| 51 #include "components/bookmarks/browser/bookmark_model.h" | 51 #include "components/bookmarks/browser/bookmark_model.h" |
| 52 #include "components/bookmarks/browser/bookmark_node.h" | 52 #include "components/bookmarks/browser/bookmark_node.h" |
| 53 #include "components/bookmarks/browser/bookmark_utils.h" | 53 #include "components/bookmarks/browser/bookmark_utils.h" |
| 54 #include "components/bookmarks/managed/managed_bookmark_service.h" | 54 #include "components/bookmarks/managed/managed_bookmark_service.h" |
| 55 #include "components/dom_distiller/core/url_utils.h" | 55 #include "components/dom_distiller/core/url_utils.h" |
| 56 #include "components/favicon/content/content_favicon_driver.h" | 56 #include "components/favicon/content/content_favicon_driver.h" |
| 57 #include "components/feature_engagement_tracker/public/feature_constants.h" |
| 58 #include "components/feature_engagement_tracker/public/feature_list.h" |
| 57 #include "components/navigation_interception/intercept_navigation_delegate.h" | 59 #include "components/navigation_interception/intercept_navigation_delegate.h" |
| 58 #include "components/navigation_interception/navigation_params.h" | 60 #include "components/navigation_interception/navigation_params.h" |
| 59 #include "components/sessions/content/content_live_tab.h" | 61 #include "components/sessions/content/content_live_tab.h" |
| 60 #include "components/sessions/core/tab_restore_service.h" | 62 #include "components/sessions/core/tab_restore_service.h" |
| 61 #include "components/url_formatter/url_fixer.h" | 63 #include "components/url_formatter/url_fixer.h" |
| 62 #include "content/public/browser/android/compositor.h" | 64 #include "content/public/browser/android/compositor.h" |
| 63 #include "content/public/browser/browser_thread.h" | 65 #include "content/public/browser/browser_thread.h" |
| 64 #include "content/public/browser/devtools_agent_host.h" | 66 #include "content/public/browser/devtools_agent_host.h" |
| 65 #include "content/public/browser/interstitial_page.h" | 67 #include "content/public/browser/interstitial_page.h" |
| 66 #include "content/public/browser/navigation_entry.h" | 68 #include "content/public/browser/navigation_entry.h" |
| 67 #include "content/public/browser/notification_service.h" | 69 #include "content/public/browser/notification_service.h" |
| 68 #include "content/public/browser/render_frame_host.h" | 70 #include "content/public/browser/render_frame_host.h" |
| 69 #include "content/public/browser/render_process_host.h" | 71 #include "content/public/browser/render_process_host.h" |
| 70 #include "content/public/browser/render_view_host.h" | 72 #include "content/public/browser/render_view_host.h" |
| 71 #include "content/public/browser/web_contents.h" | 73 #include "content/public/browser/web_contents.h" |
| 72 #include "content/public/common/browser_controls_state.h" | 74 #include "content/public/common/browser_controls_state.h" |
| 73 #include "content/public/common/resource_request_body.h" | 75 #include "content/public/common/resource_request_body.h" |
| 74 #include "jni/Tab_jni.h" | 76 #include "jni/Tab_jni.h" |
| 77 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 75 #include "net/base/escape.h" | 78 #include "net/base/escape.h" |
| 79 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 76 #include "services/service_manager/public/cpp/interface_provider.h" | 80 #include "services/service_manager/public/cpp/interface_provider.h" |
| 77 #include "skia/ext/image_operations.h" | 81 #include "skia/ext/image_operations.h" |
| 78 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 82 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
| 79 #include "ui/android/view_android.h" | 83 #include "ui/android/view_android.h" |
| 80 #include "ui/android/window_android.h" | 84 #include "ui/android/window_android.h" |
| 85 #include "ui/base/layout.h" |
| 81 #include "ui/base/resource/resource_bundle.h" | 86 #include "ui/base/resource/resource_bundle.h" |
| 82 #include "ui/base/window_open_disposition.h" | 87 #include "ui/base/window_open_disposition.h" |
| 83 #include "ui/display/display.h" | 88 #include "ui/display/display.h" |
| 84 #include "ui/display/screen.h" | 89 #include "ui/display/screen.h" |
| 85 #include "ui/gfx/android/java_bitmap.h" | 90 #include "ui/gfx/android/java_bitmap.h" |
| 86 #include "ui/gfx/favicon_size.h" | 91 #include "ui/gfx/favicon_size.h" |
| 87 #include "ui/gfx/image/image_skia.h" | 92 #include "ui/gfx/image/image_skia.h" |
| 88 | 93 |
| 89 using base::android::AttachCurrentThread; | 94 using base::android::AttachCurrentThread; |
| 90 using base::android::ConvertUTF8ToJavaString; | 95 using base::android::ConvertUTF8ToJavaString; |
| 91 using base::android::JavaParamRef; | 96 using base::android::JavaParamRef; |
| 92 using base::android::JavaRef; | 97 using base::android::JavaRef; |
| 93 using chrome::android::BackgroundTabManager; | 98 using chrome::android::BackgroundTabManager; |
| 94 using content::BrowserThread; | 99 using content::BrowserThread; |
| 95 using content::GlobalRequestID; | 100 using content::GlobalRequestID; |
| 96 using content::NavigationController; | 101 using content::NavigationController; |
| 97 using content::WebContents; | 102 using content::WebContents; |
| 98 using navigation_interception::InterceptNavigationDelegate; | 103 using navigation_interception::InterceptNavigationDelegate; |
| 99 using navigation_interception::NavigationParams; | 104 using navigation_interception::NavigationParams; |
| 100 | 105 |
| 106 // This class is created and owned by the MediaInProductHelpManager. |
| 107 class TabAndroid::MediaInProductHelp : public blink::mojom::MediaInProductHelp { |
| 108 public: |
| 109 MediaInProductHelp(content::RenderFrameHost* render_frame_host, |
| 110 TabAndroid* tab, |
| 111 blink::mojom::MediaInProductHelpRequest request) |
| 112 : render_frame_host_(render_frame_host), |
| 113 tab_(tab), |
| 114 binding_(this, std::move(request)) { |
| 115 DCHECK(render_frame_host_); |
| 116 DCHECK(tab_); |
| 117 |
| 118 binding_.set_connection_error_handler( |
| 119 base::BindOnce(&TabAndroid::OnMediaInProductHelpConnectionError, |
| 120 base::Unretained(tab_))); |
| 121 } |
| 122 ~MediaInProductHelp() override = default; |
| 123 |
| 124 // blink::mojom::MediaPromoUI implementation. |
| 125 void ShowInProductHelpWidget(const gfx::Rect& rect) override { |
| 126 tab_->ShowMediaDownloadInProductHelp(rect); |
| 127 } |
| 128 |
| 129 content::RenderFrameHost* render_frame_host() const { |
| 130 return render_frame_host_; |
| 131 } |
| 132 |
| 133 private: |
| 134 // The |manager_| and |render_frame_host_| outlive this class. |
| 135 content::RenderFrameHost* render_frame_host_; |
| 136 TabAndroid* tab_; |
| 137 mojo::Binding<blink::mojom::MediaInProductHelp> binding_; |
| 138 }; |
| 139 |
| 101 TabAndroid* TabAndroid::FromWebContents( | 140 TabAndroid* TabAndroid::FromWebContents( |
| 102 const content::WebContents* web_contents) { | 141 const content::WebContents* web_contents) { |
| 103 const CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents( | 142 const CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents( |
| 104 web_contents); | 143 web_contents); |
| 105 if (!core_tab_helper) | 144 if (!core_tab_helper) |
| 106 return NULL; | 145 return NULL; |
| 107 | 146 |
| 108 CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate(); | 147 CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate(); |
| 109 if (!core_delegate) | 148 if (!core_delegate) |
| 110 return NULL; | 149 return NULL; |
| 111 | 150 |
| 112 return static_cast<TabAndroid*>(core_delegate); | 151 return static_cast<TabAndroid*>(core_delegate); |
| 113 } | 152 } |
| 114 | 153 |
| 115 TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, const JavaRef<jobject>& obj) { | 154 TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, const JavaRef<jobject>& obj) { |
| 116 return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj)); | 155 return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj)); |
| 117 } | 156 } |
| 118 | 157 |
| 119 void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) { | 158 void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) { |
| 120 DCHECK(web_contents); | 159 DCHECK(web_contents); |
| 121 | 160 |
| 122 TabHelpers::AttachTabHelpers(web_contents, base::nullopt); | 161 TabHelpers::AttachTabHelpers(web_contents, base::nullopt); |
| 123 } | 162 } |
| 124 | 163 |
| 125 TabAndroid::TabAndroid(JNIEnv* env, const JavaRef<jobject>& obj) | 164 TabAndroid::TabAndroid(JNIEnv* env, const JavaRef<jobject>& obj) |
| 126 : weak_java_tab_(env, obj), | 165 : weak_java_tab_(env, obj), |
| 127 content_layer_(cc::Layer::Create()), | 166 content_layer_(cc::Layer::Create()), |
| 128 tab_content_manager_(NULL), | 167 tab_content_manager_(NULL), |
| 129 synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)), | 168 synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)), |
| 130 embedded_media_experience_enabled_(false) { | 169 embedded_media_experience_enabled_(false), |
| 170 weak_factory_(this) { |
| 131 Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this)); | 171 Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this)); |
| 132 } | 172 } |
| 133 | 173 |
| 134 TabAndroid::~TabAndroid() { | 174 TabAndroid::~TabAndroid() { |
| 135 GetContentLayer()->RemoveAllChildren(); | 175 GetContentLayer()->RemoveAllChildren(); |
| 136 JNIEnv* env = base::android::AttachCurrentThread(); | 176 JNIEnv* env = base::android::AttachCurrentThread(); |
| 137 Java_Tab_clearNativePtr(env, weak_java_tab_.get(env)); | 177 Java_Tab_clearNativePtr(env, weak_java_tab_.get(env)); |
| 138 } | 178 } |
| 139 | 179 |
| 140 base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() { | 180 base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 const JavaParamRef<jobject>& obj, | 370 const JavaParamRef<jobject>& obj, |
| 331 jboolean incognito, | 371 jboolean incognito, |
| 332 jboolean is_background_tab, | 372 jboolean is_background_tab, |
| 333 const JavaParamRef<jobject>& jweb_contents, | 373 const JavaParamRef<jobject>& jweb_contents, |
| 334 const JavaParamRef<jobject>& jweb_contents_delegate, | 374 const JavaParamRef<jobject>& jweb_contents_delegate, |
| 335 const JavaParamRef<jobject>& jcontext_menu_populator) { | 375 const JavaParamRef<jobject>& jcontext_menu_populator) { |
| 336 web_contents_.reset(content::WebContents::FromJavaWebContents(jweb_contents)); | 376 web_contents_.reset(content::WebContents::FromJavaWebContents(jweb_contents)); |
| 337 DCHECK(web_contents_.get()); | 377 DCHECK(web_contents_.get()); |
| 338 | 378 |
| 339 AttachTabHelpers(web_contents_.get()); | 379 AttachTabHelpers(web_contents_.get()); |
| 380 WebContentsObserver::Observe(web_contents_.get()); |
| 340 | 381 |
| 341 SetWindowSessionID(session_window_id_.id()); | 382 SetWindowSessionID(session_window_id_.id()); |
| 342 | 383 |
| 343 session_tab_id_.set_id( | 384 session_tab_id_.set_id( |
| 344 SessionTabHelper::FromWebContents(web_contents())->session_id().id()); | 385 SessionTabHelper::FromWebContents(web_contents())->session_id().id()); |
| 345 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator( | 386 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator( |
| 346 jcontext_menu_populator); | 387 jcontext_menu_populator); |
| 347 ViewAndroidHelper::FromWebContents(web_contents())-> | 388 ViewAndroidHelper::FromWebContents(web_contents())-> |
| 348 SetViewAndroid(web_contents()->GetNativeView()); | 389 SetViewAndroid(web_contents()->GetNativeView()); |
| 349 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this); | 390 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 | 446 |
| 406 notification_registrar_.Remove( | 447 notification_registrar_.Remove( |
| 407 this, | 448 this, |
| 408 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, | 449 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, |
| 409 content::Source<content::WebContents>(web_contents())); | 450 content::Source<content::WebContents>(web_contents())); |
| 410 notification_registrar_.Remove( | 451 notification_registrar_.Remove( |
| 411 this, | 452 this, |
| 412 content::NOTIFICATION_NAV_ENTRY_CHANGED, | 453 content::NOTIFICATION_NAV_ENTRY_CHANGED, |
| 413 content::Source<content::NavigationController>( | 454 content::Source<content::NavigationController>( |
| 414 &web_contents()->GetController())); | 455 &web_contents()->GetController())); |
| 456 WebContentsObserver::Observe(nullptr); |
| 415 | 457 |
| 416 favicon::FaviconDriver* favicon_driver = | 458 favicon::FaviconDriver* favicon_driver = |
| 417 favicon::ContentFaviconDriver::FromWebContents(web_contents_.get()); | 459 favicon::ContentFaviconDriver::FromWebContents(web_contents_.get()); |
| 418 | 460 |
| 419 if (favicon_driver) | 461 if (favicon_driver) |
| 420 favicon_driver->RemoveObserver(this); | 462 favicon_driver->RemoveObserver(this); |
| 421 | 463 |
| 422 web_contents()->SetDelegate(NULL); | 464 web_contents()->SetDelegate(NULL); |
| 423 | 465 |
| 424 if (delete_native) { | 466 if (delete_native) { |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 if (tab_content_manager == tab_content_manager_) | 836 if (tab_content_manager == tab_content_manager_) |
| 795 return; | 837 return; |
| 796 | 838 |
| 797 if (tab_content_manager_) | 839 if (tab_content_manager_) |
| 798 tab_content_manager_->DetachLiveLayer(GetAndroidId(), GetContentLayer()); | 840 tab_content_manager_->DetachLiveLayer(GetAndroidId(), GetContentLayer()); |
| 799 tab_content_manager_ = tab_content_manager; | 841 tab_content_manager_ = tab_content_manager; |
| 800 if (tab_content_manager_) | 842 if (tab_content_manager_) |
| 801 tab_content_manager_->AttachLiveLayer(GetAndroidId(), GetContentLayer()); | 843 tab_content_manager_->AttachLiveLayer(GetAndroidId(), GetContentLayer()); |
| 802 } | 844 } |
| 803 | 845 |
| 846 void TabAndroid::RenderFrameCreated( |
| 847 content::RenderFrameHost* render_frame_host) { |
| 848 if (base::FeatureList::IsEnabled( |
| 849 feature_engagement_tracker::kIPHMediaDownloadFeature)) { |
| 850 // Register the service for the renderer to request showing the UI. |
| 851 render_frame_host->GetInterfaceRegistry()->AddInterface( |
| 852 base::Bind(&TabAndroid::CreateInProductHelpService, |
| 853 weak_factory_.GetWeakPtr(), render_frame_host)); |
| 854 } |
| 855 } |
| 856 |
| 857 void TabAndroid::RenderFrameDeleted( |
| 858 content::RenderFrameHost* render_frame_host) { |
| 859 if (media_in_product_help_ && |
| 860 media_in_product_help_->render_frame_host() == render_frame_host) { |
| 861 DismissMediaDownloadInProductHelp(); |
| 862 } |
| 863 } |
| 864 |
| 865 void TabAndroid::ShowMediaDownloadInProductHelp( |
| 866 const gfx::Rect& rect_in_frame) { |
| 867 DCHECK(web_contents_); |
| 868 |
| 869 // We need to account for the browser controls offset to get the location for |
| 870 // the widget in the view. |
| 871 float content_offset = web_contents_->GetNativeView()->content_offset(); |
| 872 gfx::Rect rect_in_view(rect_in_frame.x(), rect_in_frame.y() + content_offset, |
| 873 rect_in_frame.width(), rect_in_frame.height()); |
| 874 gfx::Rect rect_in_view_scaled = gfx::ScaleToEnclosingRectSafe( |
| 875 rect_in_view, |
| 876 ui::GetScaleFactorForNativeView(web_contents_->GetNativeView())); |
| 877 |
| 878 JNIEnv* env = base::android::AttachCurrentThread(); |
| 879 Java_Tab_showMediaDownloadInProductHelp( |
| 880 env, weak_java_tab_.get(env), rect_in_view_scaled.x(), |
| 881 rect_in_view_scaled.y(), rect_in_view_scaled.width(), |
| 882 rect_in_view_scaled.height()); |
| 883 } |
| 884 |
| 885 void TabAndroid::DismissMediaDownloadInProductHelp() { |
| 886 JNIEnv* env = base::android::AttachCurrentThread(); |
| 887 Java_Tab_hideMediaDownloadInProductHelp(env, weak_java_tab_.get(env)); |
| 888 } |
| 889 |
| 890 void TabAndroid::DownloadMediaInProductHelpDismissed( |
| 891 JNIEnv* env, |
| 892 const base::android::JavaParamRef<jobject>& obj) { |
| 893 DCHECK(media_in_product_help_); |
| 894 media_in_product_help_.reset(); |
| 895 } |
| 896 |
| 897 void TabAndroid::CreateInProductHelpService( |
| 898 content::RenderFrameHost* render_frame_host, |
| 899 const service_manager::BindSourceInfo& source_info, |
| 900 blink::mojom::MediaInProductHelpRequest request) { |
| 901 // If we are showing the UI already, ignore the request. |
| 902 if (media_in_product_help_) |
| 903 return; |
| 904 |
| 905 media_in_product_help_ = base::MakeUnique<MediaInProductHelp>( |
| 906 render_frame_host, this, std::move(request)); |
| 907 } |
| 908 |
| 909 void TabAndroid::OnMediaInProductHelpConnectionError() { |
| 910 DCHECK(media_in_product_help_); |
| 911 DismissMediaDownloadInProductHelp(); |
| 912 } |
| 913 |
| 804 scoped_refptr<content::DevToolsAgentHost> TabAndroid::GetDevToolsAgentHost() { | 914 scoped_refptr<content::DevToolsAgentHost> TabAndroid::GetDevToolsAgentHost() { |
| 805 return devtools_host_; | 915 return devtools_host_; |
| 806 } | 916 } |
| 807 | 917 |
| 808 void TabAndroid::SetDevToolsAgentHost( | 918 void TabAndroid::SetDevToolsAgentHost( |
| 809 scoped_refptr<content::DevToolsAgentHost> host) { | 919 scoped_refptr<content::DevToolsAgentHost> host) { |
| 810 devtools_host_ = std::move(host); | 920 devtools_host_ = std::move(host); |
| 811 } | 921 } |
| 812 | 922 |
| 813 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 923 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 814 TRACE_EVENT0("native", "TabAndroid::Init"); | 924 TRACE_EVENT0("native", "TabAndroid::Init"); |
| 815 // This will automatically bind to the Java object and pass ownership there. | 925 // This will automatically bind to the Java object and pass ownership there. |
| 816 new TabAndroid(env, obj); | 926 new TabAndroid(env, obj); |
| 817 } | 927 } |
| 818 | 928 |
| 819 // static | 929 // static |
| 820 bool TabAndroid::RegisterTabAndroid(JNIEnv* env) { | 930 bool TabAndroid::RegisterTabAndroid(JNIEnv* env) { |
| 821 return RegisterNativesImpl(env); | 931 return RegisterNativesImpl(env); |
| 822 } | 932 } |
| OLD | NEW |