OLD | NEW |
---|---|
(Empty) | |
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/common/context_menu_params.h" | |
11 #include "jni/ContextMenuHelper_jni.h" | |
12 #include "jni/ContextMenuParams_jni.h" | |
13 #include "ui/gfx/point.h" | |
14 | |
15 using base::android::ConvertUTF8ToJavaString; | |
16 using base::android::ConvertUTF16ToJavaString; | |
17 using base::android::ScopedJavaLocalRef; | |
18 | |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper); | |
20 | |
21 ContextMenuHelper::ContextMenuHelper(content::WebContents* web_contents) | |
22 : web_contents_(web_contents) { | |
23 JNIEnv* env = base::android::AttachCurrentThread(); | |
24 java_obj_.Reset( | |
25 env, | |
26 Java_ContextMenuHelper_create(env, | |
27 reinterpret_cast<long>(this)).obj()); | |
28 DCHECK(!java_obj_.is_null()); | |
29 } | |
30 | |
31 ContextMenuHelper::~ContextMenuHelper() { | |
32 JNIEnv* env = base::android::AttachCurrentThread(); | |
33 Java_ContextMenuHelper_destroy(env, java_obj_.obj()); | |
34 } | |
35 | |
36 void ContextMenuHelper::ShowContextMenu( | |
Ted C
2013/11/19 02:44:03
When the context menu is hidden, don't we need to
David Trainor- moved to gerrit
2013/11/25 19:42:50
We don't *really* need to as the callback is a wea
| |
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. | |
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 | |
61 void ContextMenuHelper::SetPopulator(jobject jpopulator) { | |
62 JNIEnv* env = base::android::AttachCurrentThread(); | |
63 Java_ContextMenuHelper_setPopulator(env, java_obj_.obj(), jpopulator); | |
64 } | |
65 | |
66 const content::ContextMenuParams& | |
67 ContextMenuHelper::GetCurrentContextMenuParams() const { | |
68 return context_menu_params_; | |
69 } | |
70 | |
71 base::android::ScopedJavaLocalRef<jobject> | |
72 ContextMenuHelper::CreateJavaContextMenuParams( | |
73 const content::ContextMenuParams& params) { | |
74 JNIEnv* env = base::android::AttachCurrentThread(); | |
75 base::android::ScopedJavaLocalRef<jobject> jmenu_info = | |
76 AndroidContextMenuParams::Java_ContextMenuParams_create( | |
77 env, | |
78 params.media_type, | |
79 ConvertUTF8ToJavaString(env, params.link_url.spec()).obj(), | |
80 ConvertUTF16ToJavaString(env, params.link_text).obj(), | |
81 ConvertUTF8ToJavaString(env, params.unfiltered_link_url.spec()).obj(), | |
82 ConvertUTF8ToJavaString(env, params.src_url.spec()).obj(), | |
83 ConvertUTF16ToJavaString(env, params.selection_text).obj(), | |
84 params.is_editable); | |
85 | |
86 std::vector<content::MenuItem>::const_iterator i; | |
87 for (i = params.custom_items.begin(); i != params.custom_items.end(); i++) { | |
Ted C
2013/11/19 02:44:03
++i
David Trainor- moved to gerrit
2013/11/25 19:42:50
Done.
| |
88 AndroidContextMenuParams::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 | |
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 | |
107 bool RegisterContextMenuHelper(JNIEnv* env) { | |
108 return RegisterNativesImpl(env) && | |
109 AndroidContextMenuParams::RegisterNativesImpl(env); | |
110 } | |
OLD | NEW |