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.
4
5 #include "chrome/browser/ui/android/context_menu_helper.h"
6
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"
15
16 using base::android::ConvertUTF8ToJavaString;
17 using base::android::ConvertUTF16ToJavaString;
18 using base::android::ScopedJavaLocalRef;
19
20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper);
21
ContextMenuHelper(content::WebContents * web_contents)22 ContextMenuHelper::ContextMenuHelper(content::WebContents* web_contents)
23 : web_contents_(web_contents) {
24 JNIEnv* env = base::android::AttachCurrentThread();
25 java_obj_.Reset(
26 env,
27 Java_ContextMenuHelper_create(env, reinterpret_cast<long>(this)).obj());
28 DCHECK(!java_obj_.is_null());
29 }
30
~ContextMenuHelper()31 ContextMenuHelper::~ContextMenuHelper() {
32 JNIEnv* env = base::android::AttachCurrentThread();
33 Java_ContextMenuHelper_destroy(env, java_obj_.obj());
34 }
35
ShowContextMenu(const content::ContextMenuParams & params)36 void ContextMenuHelper::ShowContextMenu(
37 const content::ContextMenuParams& params) {
38 content::ContentViewCore* content_view_core =
39 content::ContentViewCore::FromWebContents(web_contents_);
40
41 if (!content_view_core)
42 return;
43
44 JNIEnv* env = base::android::AttachCurrentThread();
45 context_menu_params_ = params;
46 Java_ContextMenuHelper_showContextMenu(
47 env,
48 java_obj_.obj(),
49 content_view_core->GetJavaObject().obj(),
50 ContextMenuHelper::CreateJavaContextMenuParams(params).obj());
51 }
52
53 // Called to show a custom context menu. Used by the NTP.
ShowCustomContextMenu(const content::ContextMenuParams & params,const base::Callback<void (int)> & callback)54 void ContextMenuHelper::ShowCustomContextMenu(
55 const content::ContextMenuParams& params,
56 const base::Callback<void(int)>& callback) {
57 context_menu_callback_ = callback;
58 ShowContextMenu(params);
59 }
60
SetPopulator(jobject jpopulator)61 void ContextMenuHelper::SetPopulator(jobject jpopulator) {
62 JNIEnv* env = base::android::AttachCurrentThread();
63 Java_ContextMenuHelper_setPopulator(env, java_obj_.obj(), jpopulator);
64 }
65
66 base::android::ScopedJavaLocalRef<jobject>
CreateJavaContextMenuParams(const content::ContextMenuParams & params)67 ContextMenuHelper::CreateJavaContextMenuParams(
68 const content::ContextMenuParams& params) {
69 GURL sanitizedReferrer = (params.frame_url.is_empty() ?
70 params.page_url : params.frame_url).GetAsReferrer();
71
72 JNIEnv* env = base::android::AttachCurrentThread();
73 base::android::ScopedJavaLocalRef<jobject> jmenu_info =
74 ContextMenuParamsAndroid::Java_ContextMenuParams_create(
75 env,
76 params.media_type,
77 ConvertUTF8ToJavaString(env, params.link_url.spec()).obj(),
78 ConvertUTF16ToJavaString(env, params.link_text).obj(),
79 ConvertUTF8ToJavaString(env, params.unfiltered_link_url.spec()).obj(),
80 ConvertUTF8ToJavaString(env, params.src_url.spec()).obj(),
81 ConvertUTF16ToJavaString(env, params.selection_text).obj(),
82 params.is_editable,
83 ConvertUTF8ToJavaString(env, sanitizedReferrer.spec()).obj(),
84 params.referrer_policy);
85
86 std::vector<content::MenuItem>::const_iterator i;
87 for (i = params.custom_items.begin(); i != params.custom_items.end(); ++i) {
88 ContextMenuParamsAndroid::Java_ContextMenuParams_addCustomItem(
89 env,
90 jmenu_info.obj(),
91 ConvertUTF16ToJavaString(env, i->label).obj(),
92 i->action);
93 }
94
95 return jmenu_info;
96 }
97
OnCustomItemSelected(JNIEnv * env,jobject obj,jint action)98 void ContextMenuHelper::OnCustomItemSelected(JNIEnv* env,
99 jobject obj,
100 jint action) {
101 if (!context_menu_callback_.is_null()) {
102 context_menu_callback_.Run(action);
103 context_menu_callback_.Reset();
104 }
105 }
106
OnStartDownload(JNIEnv * env,jobject obj,jboolean jis_link)107 void ContextMenuHelper::OnStartDownload(JNIEnv* env,
108 jobject obj,
109 jboolean jis_link) {
110 content::DownloadControllerAndroid::Get()->StartContextMenuDownload(
111 context_menu_params_,
112 web_contents_,
113 jis_link);
114 }
115
RegisterContextMenuHelper(JNIEnv * env)116 bool RegisterContextMenuHelper(JNIEnv* env) {
117 return RegisterNativesImpl(env) &&
118 ContextMenuParamsAndroid::RegisterNativesImpl(env);
119 }
120