Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/android/context_menu_helper.h" | 5 #include "chrome/browser/ui/android/context_menu_helper.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/bind_helpers.h" | |
| 12 #include "chrome/browser/android/download/download_controller_base.h" | 13 #include "chrome/browser/android/download/download_controller_base.h" |
| 13 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | 14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
| 15 #include "chrome/common/thumbnail_capturer.mojom.h" | |
| 14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade rs.h" | 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade rs.h" |
| 15 #include "content/public/browser/android/content_view_core.h" | 17 #include "content/public/browser/android/content_view_core.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/common/context_menu_params.h" | 20 #include "content/public/common/context_menu_params.h" |
| 19 #include "jni/ContextMenuHelper_jni.h" | 21 #include "jni/ContextMenuHelper_jni.h" |
| 20 #include "jni/ContextMenuParams_jni.h" | 22 #include "jni/ContextMenuParams_jni.h" |
| 23 #include "services/service_manager/public/cpp/interface_provider.h" | |
| 21 #include "third_party/WebKit/public/web/WebContextMenuData.h" | 24 #include "third_party/WebKit/public/web/WebContextMenuData.h" |
| 22 #include "ui/android/window_android.h" | 25 #include "ui/android/window_android.h" |
| 23 #include "ui/gfx/geometry/point.h" | 26 #include "ui/gfx/geometry/point.h" |
| 24 | 27 |
| 25 using base::android::ConvertJavaStringToUTF8; | 28 using base::android::ConvertJavaStringToUTF8; |
| 26 using base::android::ConvertUTF8ToJavaString; | 29 using base::android::ConvertUTF8ToJavaString; |
| 27 using base::android::ConvertUTF16ToJavaString; | 30 using base::android::ConvertUTF16ToJavaString; |
| 28 using base::android::JavaParamRef; | 31 using base::android::JavaParamRef; |
| 29 | 32 |
| 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper); | 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper); |
| 31 | 34 |
| 32 const int kShareImageMaxWidth = 2048; | 35 const int kShareImageMaxWidth = 2048; |
| 33 const int kShareImageMaxHeight = 2048; | 36 const int kShareImageMaxHeight = 2048; |
| 34 | 37 |
| 35 const char kDataReductionProxyPassthroughHeader[] = | 38 const char kDataReductionProxyPassthroughHeader[] = |
| 36 "Chrome-Proxy: pass-through\r\n"; | 39 "Chrome-Proxy: pass-through\r\n"; |
| 37 | 40 |
| 38 ContextMenuHelper::ContextMenuHelper(content::WebContents* web_contents) | 41 ContextMenuHelper::ContextMenuHelper(content::WebContents* web_contents) |
| 39 : web_contents_(web_contents) { | 42 : web_contents_(web_contents), weak_factory_(this) { |
| 40 JNIEnv* env = base::android::AttachCurrentThread(); | 43 JNIEnv* env = base::android::AttachCurrentThread(); |
| 41 java_obj_.Reset( | 44 java_obj_.Reset( |
| 42 env, | 45 env, |
| 43 Java_ContextMenuHelper_create(env, reinterpret_cast<long>(this)).obj()); | 46 Java_ContextMenuHelper_create(env, reinterpret_cast<long>(this)).obj()); |
| 44 DCHECK(!java_obj_.is_null()); | 47 DCHECK(!java_obj_.is_null()); |
| 45 } | 48 } |
| 46 | 49 |
| 47 ContextMenuHelper::~ContextMenuHelper() { | 50 ContextMenuHelper::~ContextMenuHelper() { |
| 48 JNIEnv* env = base::android::AttachCurrentThread(); | 51 JNIEnv* env = base::android::AttachCurrentThread(); |
| 49 Java_ContextMenuHelper_destroy(env, java_obj_); | 52 Java_ContextMenuHelper_destroy(env, java_obj_); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 render_frame_host, context_menu_params_.src_url); | 145 render_frame_host, context_menu_params_.src_url); |
| 143 } | 146 } |
| 144 | 147 |
| 145 void ContextMenuHelper::ShareImage(JNIEnv* env, | 148 void ContextMenuHelper::ShareImage(JNIEnv* env, |
| 146 const JavaParamRef<jobject>& obj) { | 149 const JavaParamRef<jobject>& obj) { |
| 147 content::RenderFrameHost* render_frame_host = | 150 content::RenderFrameHost* render_frame_host = |
| 148 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); | 151 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
| 149 if (!render_frame_host) | 152 if (!render_frame_host) |
| 150 return; | 153 return; |
| 151 | 154 |
| 152 CoreTabHelper::FromWebContents(web_contents_)-> | 155 chrome::mojom::ThumbnailCapturerPtr thumbnail_capturer; |
| 153 RequestThumbnailForContextNode( | 156 render_frame_host->GetRemoteInterfaces()->GetInterface(&thumbnail_capturer); |
| 154 render_frame_host, | 157 // Bind the InterfacePtr into the callback so that it's kept alive until |
| 155 0, | 158 // there's either a connection error or a response. |
| 156 gfx::Size(kShareImageMaxWidth, kShareImageMaxHeight), | 159 thumbnail_capturer->RequestThumbnailForContextNode( |
| 157 base::Bind(&ContextMenuHelper::OnShareImage, | 160 0, gfx::Size(kShareImageMaxWidth, kShareImageMaxHeight), |
| 158 base::Unretained(this))); | 161 base::Bind(&ContextMenuHelper::OnShareImage, weak_factory_.GetWeakPtr(), |
| 162 base::Passed(&thumbnail_capturer))); | |
|
tibell
2017/03/09 02:21:49
This move is probably undefined behavior since you
watk
2017/03/09 04:01:01
Discussed offline. Looks like this is guaranteed t
| |
| 159 } | 163 } |
| 160 | 164 |
| 161 void ContextMenuHelper::OnShareImage(const std::string& thumbnail_data, | 165 void ContextMenuHelper::OnShareImage( |
| 162 const gfx::Size& original_size) { | 166 chrome::mojom::ThumbnailCapturerPtr thumbnail_capturer, |
| 167 const std::string& thumbnail_data, | |
| 168 const gfx::Size& original_size) { | |
| 163 content::ContentViewCore* content_view_core = | 169 content::ContentViewCore* content_view_core = |
| 164 content::ContentViewCore::FromWebContents(web_contents_); | 170 content::ContentViewCore::FromWebContents(web_contents_); |
| 165 if (!content_view_core) | 171 if (!content_view_core) |
| 166 return; | 172 return; |
| 167 | 173 |
| 168 base::android::ScopedJavaLocalRef<jobject> jwindow_android( | 174 base::android::ScopedJavaLocalRef<jobject> jwindow_android( |
| 169 content_view_core->GetWindowAndroid()->GetJavaObject()); | 175 content_view_core->GetWindowAndroid()->GetJavaObject()); |
| 170 | 176 |
| 171 if (jwindow_android.is_null()) | 177 if (jwindow_android.is_null()) |
| 172 return; | 178 return; |
| 173 | 179 |
| 174 JNIEnv* env = base::android::AttachCurrentThread(); | 180 JNIEnv* env = base::android::AttachCurrentThread(); |
| 175 base::android::ScopedJavaLocalRef<jbyteArray> j_bytes = | 181 base::android::ScopedJavaLocalRef<jbyteArray> j_bytes = |
| 176 base::android::ToJavaByteArray( | 182 base::android::ToJavaByteArray( |
| 177 env, reinterpret_cast<const uint8_t*>(thumbnail_data.data()), | 183 env, reinterpret_cast<const uint8_t*>(thumbnail_data.data()), |
| 178 thumbnail_data.length()); | 184 thumbnail_data.length()); |
| 179 | 185 |
| 180 Java_ContextMenuHelper_onShareImageReceived(env, java_obj_, jwindow_android, | 186 Java_ContextMenuHelper_onShareImageReceived(env, java_obj_, jwindow_android, |
| 181 j_bytes); | 187 j_bytes); |
| 182 } | 188 } |
| 183 | 189 |
| 184 bool RegisterContextMenuHelper(JNIEnv* env) { | 190 bool RegisterContextMenuHelper(JNIEnv* env) { |
| 185 return RegisterNativesImpl(env); | 191 return RegisterNativesImpl(env); |
| 186 } | 192 } |
| OLD | NEW |