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/public/feature_constants.h" | |
58 #include "components/feature_engagement/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_; | |
dcheng
2017/08/19 00:30:15
Nit: content::RenderFrameHost* const
Khushal
2017/08/21 23:27:34
Done.
| |
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); | 163 TabHelpers::AttachTabHelpers(web_contents); |
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)); |
174 | |
175 frame_interfaces_.AddInterface(base::Bind( | |
176 &TabAndroid::CreateInProductHelpService, weak_factory_.GetWeakPtr())); | |
132 } | 177 } |
133 | 178 |
134 TabAndroid::~TabAndroid() { | 179 TabAndroid::~TabAndroid() { |
135 GetContentLayer()->RemoveAllChildren(); | 180 GetContentLayer()->RemoveAllChildren(); |
136 JNIEnv* env = base::android::AttachCurrentThread(); | 181 JNIEnv* env = base::android::AttachCurrentThread(); |
137 Java_Tab_clearNativePtr(env, weak_java_tab_.get(env)); | 182 Java_Tab_clearNativePtr(env, weak_java_tab_.get(env)); |
138 } | 183 } |
139 | 184 |
140 base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() { | 185 base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() { |
141 JNIEnv* env = base::android::AttachCurrentThread(); | 186 JNIEnv* env = base::android::AttachCurrentThread(); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 const JavaParamRef<jobject>& obj, | 380 const JavaParamRef<jobject>& obj, |
336 jboolean incognito, | 381 jboolean incognito, |
337 jboolean is_background_tab, | 382 jboolean is_background_tab, |
338 const JavaParamRef<jobject>& jweb_contents, | 383 const JavaParamRef<jobject>& jweb_contents, |
339 const JavaParamRef<jobject>& jweb_contents_delegate, | 384 const JavaParamRef<jobject>& jweb_contents_delegate, |
340 const JavaParamRef<jobject>& jcontext_menu_populator) { | 385 const JavaParamRef<jobject>& jcontext_menu_populator) { |
341 web_contents_.reset(content::WebContents::FromJavaWebContents(jweb_contents)); | 386 web_contents_.reset(content::WebContents::FromJavaWebContents(jweb_contents)); |
342 DCHECK(web_contents_.get()); | 387 DCHECK(web_contents_.get()); |
343 | 388 |
344 AttachTabHelpers(web_contents_.get()); | 389 AttachTabHelpers(web_contents_.get()); |
390 WebContentsObserver::Observe(web_contents_.get()); | |
345 | 391 |
346 SetWindowSessionID(session_window_id_.id()); | 392 SetWindowSessionID(session_window_id_.id()); |
347 | 393 |
348 session_tab_id_.set_id( | 394 session_tab_id_.set_id( |
349 SessionTabHelper::FromWebContents(web_contents())->session_id().id()); | 395 SessionTabHelper::FromWebContents(web_contents())->session_id().id()); |
350 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator( | 396 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator( |
351 jcontext_menu_populator); | 397 jcontext_menu_populator); |
352 ViewAndroidHelper::FromWebContents(web_contents())-> | 398 ViewAndroidHelper::FromWebContents(web_contents())-> |
353 SetViewAndroid(web_contents()->GetNativeView()); | 399 SetViewAndroid(web_contents()->GetNativeView()); |
354 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this); | 400 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 | 456 |
411 notification_registrar_.Remove( | 457 notification_registrar_.Remove( |
412 this, | 458 this, |
413 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, | 459 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, |
414 content::Source<content::WebContents>(web_contents())); | 460 content::Source<content::WebContents>(web_contents())); |
415 notification_registrar_.Remove( | 461 notification_registrar_.Remove( |
416 this, | 462 this, |
417 content::NOTIFICATION_NAV_ENTRY_CHANGED, | 463 content::NOTIFICATION_NAV_ENTRY_CHANGED, |
418 content::Source<content::NavigationController>( | 464 content::Source<content::NavigationController>( |
419 &web_contents()->GetController())); | 465 &web_contents()->GetController())); |
466 WebContentsObserver::Observe(nullptr); | |
420 | 467 |
421 favicon::FaviconDriver* favicon_driver = | 468 favicon::FaviconDriver* favicon_driver = |
422 favicon::ContentFaviconDriver::FromWebContents(web_contents_.get()); | 469 favicon::ContentFaviconDriver::FromWebContents(web_contents_.get()); |
423 | 470 |
424 if (favicon_driver) | 471 if (favicon_driver) |
425 favicon_driver->RemoveObserver(this); | 472 favicon_driver->RemoveObserver(this); |
426 | 473 |
427 web_contents()->SetDelegate(NULL); | 474 web_contents()->SetDelegate(NULL); |
428 | 475 |
429 if (delete_native) { | 476 if (delete_native) { |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
822 if (tab_content_manager_) | 869 if (tab_content_manager_) |
823 tab_content_manager_->AttachLiveLayer(GetAndroidId(), GetContentLayer()); | 870 tab_content_manager_->AttachLiveLayer(GetAndroidId(), GetContentLayer()); |
824 } | 871 } |
825 | 872 |
826 void TabAndroid::ClearThumbnailPlaceholder(JNIEnv* env, | 873 void TabAndroid::ClearThumbnailPlaceholder(JNIEnv* env, |
827 const JavaParamRef<jobject>& obj) { | 874 const JavaParamRef<jobject>& obj) { |
828 if (tab_content_manager_) | 875 if (tab_content_manager_) |
829 tab_content_manager_->NativeRemoveTabThumbnail(GetAndroidId()); | 876 tab_content_manager_->NativeRemoveTabThumbnail(GetAndroidId()); |
830 } | 877 } |
831 | 878 |
879 void TabAndroid::OnInterfaceRequestFromFrame( | |
880 content::RenderFrameHost* render_frame_host, | |
881 const std::string& interface_name, | |
882 mojo::ScopedMessagePipeHandle* interface_pipe) { | |
883 frame_interfaces_.TryBindInterface(interface_name, interface_pipe, | |
884 render_frame_host); | |
885 } | |
886 | |
887 void TabAndroid::RenderFrameDeleted( | |
888 content::RenderFrameHost* render_frame_host) { | |
889 if (media_in_product_help_ && | |
890 media_in_product_help_->render_frame_host() == render_frame_host) { | |
891 DismissMediaDownloadInProductHelp(); | |
892 } | |
893 } | |
894 | |
895 void TabAndroid::ShowMediaDownloadInProductHelp( | |
896 const gfx::Rect& rect_in_frame) { | |
897 DCHECK(web_contents_); | |
898 | |
899 // We need to account for the browser controls offset to get the location for | |
900 // the widget in the view. | |
901 float content_offset = web_contents_->GetNativeView()->content_offset(); | |
902 gfx::Rect rect_in_view(rect_in_frame.x(), rect_in_frame.y() + content_offset, | |
903 rect_in_frame.width(), rect_in_frame.height()); | |
904 gfx::Rect rect_in_view_scaled = gfx::ScaleToEnclosingRectSafe( | |
905 rect_in_view, | |
906 ui::GetScaleFactorForNativeView(web_contents_->GetNativeView())); | |
907 | |
908 JNIEnv* env = base::android::AttachCurrentThread(); | |
909 Java_Tab_showMediaDownloadInProductHelp( | |
910 env, weak_java_tab_.get(env), rect_in_view_scaled.x(), | |
911 rect_in_view_scaled.y(), rect_in_view_scaled.width(), | |
912 rect_in_view_scaled.height()); | |
913 } | |
914 | |
915 void TabAndroid::DismissMediaDownloadInProductHelp() { | |
916 JNIEnv* env = base::android::AttachCurrentThread(); | |
917 Java_Tab_hideMediaDownloadInProductHelp(env, weak_java_tab_.get(env)); | |
918 } | |
919 | |
920 void TabAndroid::MediaDownloadInProductHelpDismissed( | |
921 JNIEnv* env, | |
922 const base::android::JavaParamRef<jobject>& obj) { | |
923 DCHECK(media_in_product_help_); | |
924 media_in_product_help_.reset(); | |
925 } | |
926 | |
927 void TabAndroid::CreateInProductHelpService( | |
928 blink::mojom::MediaDownloadInProductHelpRequest request, | |
929 content::RenderFrameHost* render_frame_host) { | |
930 // If we are showing the UI already, ignore the request. | |
931 if (media_in_product_help_) | |
932 return; | |
933 | |
934 media_in_product_help_ = base::MakeUnique<MediaDownloadInProductHelp>( | |
935 render_frame_host, this, std::move(request)); | |
936 } | |
937 | |
938 void TabAndroid::OnMediaDownloadInProductHelpConnectionError() { | |
939 DCHECK(media_in_product_help_); | |
940 DismissMediaDownloadInProductHelp(); | |
941 } | |
942 | |
832 scoped_refptr<content::DevToolsAgentHost> TabAndroid::GetDevToolsAgentHost() { | 943 scoped_refptr<content::DevToolsAgentHost> TabAndroid::GetDevToolsAgentHost() { |
833 return devtools_host_; | 944 return devtools_host_; |
834 } | 945 } |
835 | 946 |
836 void TabAndroid::SetDevToolsAgentHost( | 947 void TabAndroid::SetDevToolsAgentHost( |
837 scoped_refptr<content::DevToolsAgentHost> host) { | 948 scoped_refptr<content::DevToolsAgentHost> host) { |
838 devtools_host_ = std::move(host); | 949 devtools_host_ = std::move(host); |
839 } | 950 } |
840 | 951 |
841 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 952 static void Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
842 TRACE_EVENT0("native", "TabAndroid::Init"); | 953 TRACE_EVENT0("native", "TabAndroid::Init"); |
843 // This will automatically bind to the Java object and pass ownership there. | 954 // This will automatically bind to the Java object and pass ownership there. |
844 new TabAndroid(env, obj); | 955 new TabAndroid(env, obj); |
845 } | 956 } |
OLD | NEW |