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 MediaDownloadInProductHelpManager. |
| 107 class TabAndroid::MediaDownloadInProductHelp |
| 108 : public blink::mojom::MediaDownloadInProductHelp { |
| 109 public: |
| 110 MediaDownloadInProductHelp( |
| 111 content::RenderFrameHost* render_frame_host, |
| 112 TabAndroid* tab, |
| 113 blink::mojom::MediaDownloadInProductHelpRequest request) |
| 114 : render_frame_host_(render_frame_host), |
| 115 tab_(tab), |
| 116 binding_(this, std::move(request)) { |
| 117 DCHECK(render_frame_host_); |
| 118 DCHECK(tab_); |
| 119 |
| 120 binding_.set_connection_error_handler( |
| 121 base::BindOnce(&TabAndroid::OnMediaDownloadInProductHelpConnectionError, |
| 122 base::Unretained(tab_))); |
| 123 } |
| 124 ~MediaDownloadInProductHelp() override = default; |
| 125 |
| 126 // blink::mojom::MediaPromoUI implementation. |
| 127 void ShowInProductHelpWidget(const gfx::Rect& rect) override { |
| 128 tab_->ShowMediaDownloadInProductHelp(rect); |
| 129 } |
| 130 |
| 131 content::RenderFrameHost* render_frame_host() const { |
| 132 return render_frame_host_; |
| 133 } |
| 134 |
| 135 private: |
| 136 // The |manager_| and |render_frame_host_| outlive this class. |
| 137 content::RenderFrameHost* render_frame_host_; |
| 138 TabAndroid* tab_; |
| 139 mojo::Binding<blink::mojom::MediaDownloadInProductHelp> binding_; |
| 140 }; |
| 141 |
101 TabAndroid* TabAndroid::FromWebContents( | 142 TabAndroid* TabAndroid::FromWebContents( |
102 const content::WebContents* web_contents) { | 143 const content::WebContents* web_contents) { |
103 const CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents( | 144 const CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents( |
104 web_contents); | 145 web_contents); |
105 if (!core_tab_helper) | 146 if (!core_tab_helper) |
106 return NULL; | 147 return NULL; |
107 | 148 |
108 CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate(); | 149 CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate(); |
109 if (!core_delegate) | 150 if (!core_delegate) |
110 return NULL; | 151 return NULL; |
111 | 152 |
112 return static_cast<TabAndroid*>(core_delegate); | 153 return static_cast<TabAndroid*>(core_delegate); |
113 } | 154 } |
114 | 155 |
115 TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, const JavaRef<jobject>& obj) { | 156 TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, const JavaRef<jobject>& obj) { |
116 return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj)); | 157 return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj)); |
117 } | 158 } |
118 | 159 |
119 void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) { | 160 void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) { |
120 DCHECK(web_contents); | 161 DCHECK(web_contents); |
121 | 162 |
122 TabHelpers::AttachTabHelpers(web_contents, base::nullopt); | 163 TabHelpers::AttachTabHelpers(web_contents, base::nullopt); |
123 } | 164 } |
124 | 165 |
125 TabAndroid::TabAndroid(JNIEnv* env, const JavaRef<jobject>& obj) | 166 TabAndroid::TabAndroid(JNIEnv* env, const JavaRef<jobject>& obj) |
126 : weak_java_tab_(env, obj), | 167 : weak_java_tab_(env, obj), |
127 content_layer_(cc::Layer::Create()), | 168 content_layer_(cc::Layer::Create()), |
128 tab_content_manager_(NULL), | 169 tab_content_manager_(NULL), |
129 synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)), | 170 synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)), |
130 embedded_media_experience_enabled_(false) { | 171 embedded_media_experience_enabled_(false), |
| 172 weak_factory_(this) { |
131 Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this)); | 173 Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this)); |
132 } | 174 } |
133 | 175 |
134 TabAndroid::~TabAndroid() { | 176 TabAndroid::~TabAndroid() { |
135 GetContentLayer()->RemoveAllChildren(); | 177 GetContentLayer()->RemoveAllChildren(); |
136 JNIEnv* env = base::android::AttachCurrentThread(); | 178 JNIEnv* env = base::android::AttachCurrentThread(); |
137 Java_Tab_clearNativePtr(env, weak_java_tab_.get(env)); | 179 Java_Tab_clearNativePtr(env, weak_java_tab_.get(env)); |
138 } | 180 } |
139 | 181 |
140 base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() { | 182 base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 const JavaParamRef<jobject>& obj, | 372 const JavaParamRef<jobject>& obj, |
331 jboolean incognito, | 373 jboolean incognito, |
332 jboolean is_background_tab, | 374 jboolean is_background_tab, |
333 const JavaParamRef<jobject>& jweb_contents, | 375 const JavaParamRef<jobject>& jweb_contents, |
334 const JavaParamRef<jobject>& jweb_contents_delegate, | 376 const JavaParamRef<jobject>& jweb_contents_delegate, |
335 const JavaParamRef<jobject>& jcontext_menu_populator) { | 377 const JavaParamRef<jobject>& jcontext_menu_populator) { |
336 web_contents_.reset(content::WebContents::FromJavaWebContents(jweb_contents)); | 378 web_contents_.reset(content::WebContents::FromJavaWebContents(jweb_contents)); |
337 DCHECK(web_contents_.get()); | 379 DCHECK(web_contents_.get()); |
338 | 380 |
339 AttachTabHelpers(web_contents_.get()); | 381 AttachTabHelpers(web_contents_.get()); |
| 382 WebContentsObserver::Observe(web_contents_.get()); |
340 | 383 |
341 SetWindowSessionID(session_window_id_.id()); | 384 SetWindowSessionID(session_window_id_.id()); |
342 | 385 |
343 session_tab_id_.set_id( | 386 session_tab_id_.set_id( |
344 SessionTabHelper::FromWebContents(web_contents())->session_id().id()); | 387 SessionTabHelper::FromWebContents(web_contents())->session_id().id()); |
345 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator( | 388 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator( |
346 jcontext_menu_populator); | 389 jcontext_menu_populator); |
347 ViewAndroidHelper::FromWebContents(web_contents())-> | 390 ViewAndroidHelper::FromWebContents(web_contents())-> |
348 SetViewAndroid(web_contents()->GetNativeView()); | 391 SetViewAndroid(web_contents()->GetNativeView()); |
349 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this); | 392 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 | 448 |
406 notification_registrar_.Remove( | 449 notification_registrar_.Remove( |
407 this, | 450 this, |
408 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, | 451 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, |
409 content::Source<content::WebContents>(web_contents())); | 452 content::Source<content::WebContents>(web_contents())); |
410 notification_registrar_.Remove( | 453 notification_registrar_.Remove( |
411 this, | 454 this, |
412 content::NOTIFICATION_NAV_ENTRY_CHANGED, | 455 content::NOTIFICATION_NAV_ENTRY_CHANGED, |
413 content::Source<content::NavigationController>( | 456 content::Source<content::NavigationController>( |
414 &web_contents()->GetController())); | 457 &web_contents()->GetController())); |
| 458 WebContentsObserver::Observe(nullptr); |
415 | 459 |
416 favicon::FaviconDriver* favicon_driver = | 460 favicon::FaviconDriver* favicon_driver = |
417 favicon::ContentFaviconDriver::FromWebContents(web_contents_.get()); | 461 favicon::ContentFaviconDriver::FromWebContents(web_contents_.get()); |
418 | 462 |
419 if (favicon_driver) | 463 if (favicon_driver) |
420 favicon_driver->RemoveObserver(this); | 464 favicon_driver->RemoveObserver(this); |
421 | 465 |
422 web_contents()->SetDelegate(NULL); | 466 web_contents()->SetDelegate(NULL); |
423 | 467 |
424 if (delete_native) { | 468 if (delete_native) { |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 if (tab_content_manager_) | 859 if (tab_content_manager_) |
816 tab_content_manager_->AttachLiveLayer(GetAndroidId(), GetContentLayer()); | 860 tab_content_manager_->AttachLiveLayer(GetAndroidId(), GetContentLayer()); |
817 } | 861 } |
818 | 862 |
819 void TabAndroid::ClearThumbnailPlaceholder(JNIEnv* env, | 863 void TabAndroid::ClearThumbnailPlaceholder(JNIEnv* env, |
820 const JavaParamRef<jobject>& obj) { | 864 const JavaParamRef<jobject>& obj) { |
821 if (tab_content_manager_) | 865 if (tab_content_manager_) |
822 tab_content_manager_->NativeRemoveTabThumbnail(GetAndroidId()); | 866 tab_content_manager_->NativeRemoveTabThumbnail(GetAndroidId()); |
823 } | 867 } |
824 | 868 |
| 869 void TabAndroid::RenderFrameCreated( |
| 870 content::RenderFrameHost* render_frame_host) { |
| 871 if (base::FeatureList::IsEnabled( |
| 872 feature_engagement_tracker::kIPHMediaDownloadFeature)) { |
| 873 // Register the service for the renderer to request showing the UI. |
| 874 render_frame_host->GetInterfaceRegistry()->AddInterface( |
| 875 base::Bind(&TabAndroid::CreateInProductHelpService, |
| 876 weak_factory_.GetWeakPtr(), render_frame_host)); |
| 877 } |
| 878 } |
| 879 |
| 880 void TabAndroid::RenderFrameDeleted( |
| 881 content::RenderFrameHost* render_frame_host) { |
| 882 if (media_in_product_help_ && |
| 883 media_in_product_help_->render_frame_host() == render_frame_host) { |
| 884 DismissMediaDownloadInProductHelp(); |
| 885 } |
| 886 } |
| 887 |
| 888 void TabAndroid::ShowMediaDownloadInProductHelp( |
| 889 const gfx::Rect& rect_in_frame) { |
| 890 DCHECK(web_contents_); |
| 891 |
| 892 // We need to account for the browser controls offset to get the location for |
| 893 // the widget in the view. |
| 894 float content_offset = web_contents_->GetNativeView()->content_offset(); |
| 895 gfx::Rect rect_in_view(rect_in_frame.x(), rect_in_frame.y() + content_offset, |
| 896 rect_in_frame.width(), rect_in_frame.height()); |
| 897 gfx::Rect rect_in_view_scaled = gfx::ScaleToEnclosingRectSafe( |
| 898 rect_in_view, |
| 899 ui::GetScaleFactorForNativeView(web_contents_->GetNativeView())); |
| 900 |
| 901 JNIEnv* env = base::android::AttachCurrentThread(); |
| 902 Java_Tab_showMediaDownloadInProductHelp( |
| 903 env, weak_java_tab_.get(env), rect_in_view_scaled.x(), |
| 904 rect_in_view_scaled.y(), rect_in_view_scaled.width(), |
| 905 rect_in_view_scaled.height()); |
| 906 } |
| 907 |
| 908 void TabAndroid::DismissMediaDownloadInProductHelp() { |
| 909 JNIEnv* env = base::android::AttachCurrentThread(); |
| 910 Java_Tab_hideMediaDownloadInProductHelp(env, weak_java_tab_.get(env)); |
| 911 } |
| 912 |
| 913 void TabAndroid::MediaDownloadInProductHelpDismissed( |
| 914 JNIEnv* env, |
| 915 const base::android::JavaParamRef<jobject>& obj) { |
| 916 DCHECK(media_in_product_help_); |
| 917 media_in_product_help_.reset(); |
| 918 } |
| 919 |
| 920 void TabAndroid::CreateInProductHelpService( |
| 921 content::RenderFrameHost* render_frame_host, |
| 922 const service_manager::BindSourceInfo& source_info, |
| 923 blink::mojom::MediaDownloadInProductHelpRequest request) { |
| 924 // If we are showing the UI already, ignore the request. |
| 925 if (media_in_product_help_) |
| 926 return; |
| 927 |
| 928 media_in_product_help_ = base::MakeUnique<MediaDownloadInProductHelp>( |
| 929 render_frame_host, this, std::move(request)); |
| 930 } |
| 931 |
| 932 void TabAndroid::OnMediaDownloadInProductHelpConnectionError() { |
| 933 DCHECK(media_in_product_help_); |
| 934 DismissMediaDownloadInProductHelp(); |
| 935 } |
| 936 |
825 scoped_refptr<content::DevToolsAgentHost> TabAndroid::GetDevToolsAgentHost() { | 937 scoped_refptr<content::DevToolsAgentHost> TabAndroid::GetDevToolsAgentHost() { |
826 return devtools_host_; | 938 return devtools_host_; |
827 } | 939 } |
828 | 940 |
829 void TabAndroid::SetDevToolsAgentHost( | 941 void TabAndroid::SetDevToolsAgentHost( |
830 scoped_refptr<content::DevToolsAgentHost> host) { | 942 scoped_refptr<content::DevToolsAgentHost> host) { |
831 devtools_host_ = std::move(host); | 943 devtools_host_ = std::move(host); |
832 } | 944 } |
833 | 945 |
834 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 946 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
835 TRACE_EVENT0("native", "TabAndroid::Init"); | 947 TRACE_EVENT0("native", "TabAndroid::Init"); |
836 // This will automatically bind to the Java object and pass ownership there. | 948 // This will automatically bind to the Java object and pass ownership there. |
837 new TabAndroid(env, obj); | 949 new TabAndroid(env, obj); |
838 } | 950 } |
839 | 951 |
840 // static | 952 // static |
841 bool TabAndroid::RegisterTabAndroid(JNIEnv* env) { | 953 bool TabAndroid::RegisterTabAndroid(JNIEnv* env) { |
842 return RegisterNativesImpl(env); | 954 return RegisterNativesImpl(env); |
843 } | 955 } |
OLD | NEW |