Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / android / context_menu_helper.cc
blobbfbe8d36e3f267a754cd82bcf8089a70f9219bba
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/geometry/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();
25 java_obj_.Reset(
26 env,
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)
42 return;
44 base::android::ScopedJavaLocalRef<jobject> jcontent_view_core(
45 content_view_core->GetJavaObject());
47 if (jcontent_view_core.is_null())
48 return;
50 JNIEnv* env = base::android::AttachCurrentThread();
51 context_menu_params_ = params;
52 Java_ContextMenuHelper_showContextMenu(
53 env,
54 java_obj_.obj(),
55 jcontent_view_core.obj(),
56 ContextMenuHelper::CreateJavaContextMenuParams(params).obj());
59 // Called to show a custom context menu. Used by the NTP.
60 void ContextMenuHelper::ShowCustomContextMenu(
61 const content::ContextMenuParams& params,
62 const base::Callback<void(int)>& callback) {
63 context_menu_callback_ = callback;
64 ShowContextMenu(params);
67 void ContextMenuHelper::SetPopulator(jobject jpopulator) {
68 JNIEnv* env = base::android::AttachCurrentThread();
69 Java_ContextMenuHelper_setPopulator(env, java_obj_.obj(), jpopulator);
72 base::android::ScopedJavaLocalRef<jobject>
73 ContextMenuHelper::CreateJavaContextMenuParams(
74 const content::ContextMenuParams& params) {
75 GURL sanitizedReferrer = (params.frame_url.is_empty() ?
76 params.page_url : params.frame_url).GetAsReferrer();
78 JNIEnv* env = base::android::AttachCurrentThread();
79 base::android::ScopedJavaLocalRef<jobject> jmenu_info =
80 ContextMenuParamsAndroid::Java_ContextMenuParams_create(
81 env,
82 params.media_type,
83 ConvertUTF8ToJavaString(env, params.link_url.spec()).obj(),
84 ConvertUTF16ToJavaString(env, params.link_text).obj(),
85 ConvertUTF8ToJavaString(env, params.unfiltered_link_url.spec()).obj(),
86 ConvertUTF8ToJavaString(env, params.src_url.spec()).obj(),
87 ConvertUTF16ToJavaString(env, params.selection_text).obj(),
88 params.is_editable,
89 ConvertUTF8ToJavaString(env, sanitizedReferrer.spec()).obj(),
90 params.referrer_policy);
92 std::vector<content::MenuItem>::const_iterator i;
93 for (i = params.custom_items.begin(); i != params.custom_items.end(); ++i) {
94 ContextMenuParamsAndroid::Java_ContextMenuParams_addCustomItem(
95 env,
96 jmenu_info.obj(),
97 ConvertUTF16ToJavaString(env, i->label).obj(),
98 i->action);
101 return jmenu_info;
104 void ContextMenuHelper::OnCustomItemSelected(JNIEnv* env,
105 jobject obj,
106 jint action) {
107 if (!context_menu_callback_.is_null()) {
108 context_menu_callback_.Run(action);
109 context_menu_callback_.Reset();
113 void ContextMenuHelper::OnStartDownload(JNIEnv* env,
114 jobject obj,
115 jboolean jis_link) {
116 content::DownloadControllerAndroid::Get()->StartContextMenuDownload(
117 context_menu_params_,
118 web_contents_,
119 jis_link);
122 bool RegisterContextMenuHelper(JNIEnv* env) {
123 return RegisterNativesImpl(env) &&
124 ContextMenuParamsAndroid::RegisterNativesImpl(env);