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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/android/context_menu_helper.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "content/public/browser/android/content_view_core.h"
10 #include "content/public/browser/android/download_controller_android.h"
11 #include "content/public/common/context_menu_params.h"
12 #include "jni/ContextMenuHelper_jni.h"
13 #include "jni/ContextMenuParams_jni.h"
14 #include "ui/gfx/point.h"
16 using base::android::ConvertUTF8ToJavaString
;
17 using base::android::ConvertUTF16ToJavaString
;
18 using base::android::ScopedJavaLocalRef
;
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper
);
22 ContextMenuHelper::ContextMenuHelper(content::WebContents
* web_contents
)
23 : web_contents_(web_contents
) {
24 JNIEnv
* env
= base::android::AttachCurrentThread();
27 Java_ContextMenuHelper_create(env
, reinterpret_cast<long>(this)).obj());
28 DCHECK(!java_obj_
.is_null());
31 ContextMenuHelper::~ContextMenuHelper() {
32 JNIEnv
* env
= base::android::AttachCurrentThread();
33 Java_ContextMenuHelper_destroy(env
, java_obj_
.obj());
36 void ContextMenuHelper::ShowContextMenu(
37 const content::ContextMenuParams
& params
) {
38 content::ContentViewCore
* content_view_core
=
39 content::ContentViewCore::FromWebContents(web_contents_
);
41 if (!content_view_core
)
44 JNIEnv
* env
= base::android::AttachCurrentThread();
45 context_menu_params_
= params
;
46 Java_ContextMenuHelper_showContextMenu(
49 content_view_core
->GetJavaObject().obj(),
50 ContextMenuHelper::CreateJavaContextMenuParams(params
).obj());
53 // Called to show a custom context menu. Used by the NTP.
54 void ContextMenuHelper::ShowCustomContextMenu(
55 const content::ContextMenuParams
& params
,
56 const base::Callback
<void(int)>& callback
) {
57 context_menu_callback_
= callback
;
58 ShowContextMenu(params
);
61 void ContextMenuHelper::SetPopulator(jobject jpopulator
) {
62 JNIEnv
* env
= base::android::AttachCurrentThread();
63 Java_ContextMenuHelper_setPopulator(env
, java_obj_
.obj(), jpopulator
);
66 base::android::ScopedJavaLocalRef
<jobject
>
67 ContextMenuHelper::CreateJavaContextMenuParams(
68 const content::ContextMenuParams
& params
) {
69 JNIEnv
* env
= base::android::AttachCurrentThread();
70 base::android::ScopedJavaLocalRef
<jobject
> jmenu_info
=
71 ContextMenuParamsAndroid::Java_ContextMenuParams_create(
74 ConvertUTF8ToJavaString(env
, params
.link_url
.spec()).obj(),
75 ConvertUTF16ToJavaString(env
, params
.link_text
).obj(),
76 ConvertUTF8ToJavaString(env
, params
.unfiltered_link_url
.spec()).obj(),
77 ConvertUTF8ToJavaString(env
, params
.src_url
.spec()).obj(),
78 ConvertUTF16ToJavaString(env
, params
.selection_text
).obj(),
81 std::vector
<content::MenuItem
>::const_iterator i
;
82 for (i
= params
.custom_items
.begin(); i
!= params
.custom_items
.end(); ++i
) {
83 ContextMenuParamsAndroid::Java_ContextMenuParams_addCustomItem(
86 ConvertUTF16ToJavaString(env
, i
->label
).obj(),
93 void ContextMenuHelper::OnCustomItemSelected(JNIEnv
* env
,
96 if (!context_menu_callback_
.is_null()) {
97 context_menu_callback_
.Run(action
);
98 context_menu_callback_
.Reset();
102 void ContextMenuHelper::OnStartDownload(JNIEnv
* env
,
105 content::DownloadControllerAndroid::Get()->StartContextMenuDownload(
106 context_menu_params_
,
111 bool RegisterContextMenuHelper(JNIEnv
* env
) {
112 return RegisterNativesImpl(env
) &&
113 ContextMenuParamsAndroid::RegisterNativesImpl(env
);