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