Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1104)

Unified Diff: chrome/browser/ui/android/context_menu_helper.cc

Issue 73173002: Add ContextMenu support upstream for Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix bug in clipboard Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/android/context_menu_helper.cc
diff --git a/chrome/browser/ui/android/context_menu_helper.cc b/chrome/browser/ui/android/context_menu_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e0283435b95daf8586397c86d686a282c7e41e63
--- /dev/null
+++ b/chrome/browser/ui/android/context_menu_helper.cc
@@ -0,0 +1,110 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/android/context_menu_helper.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "content/public/browser/android/content_view_core.h"
+#include "content/public/common/context_menu_params.h"
+#include "jni/ContextMenuHelper_jni.h"
+#include "jni/ContextMenuParams_jni.h"
+#include "ui/gfx/point.h"
+
+using base::android::ConvertUTF8ToJavaString;
+using base::android::ConvertUTF16ToJavaString;
+using base::android::ScopedJavaLocalRef;
+
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper);
+
+ContextMenuHelper::ContextMenuHelper(content::WebContents* web_contents)
+ : web_contents_(web_contents) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ java_obj_.Reset(
+ env,
+ Java_ContextMenuHelper_create(env,
+ reinterpret_cast<long>(this)).obj());
+ DCHECK(!java_obj_.is_null());
+}
+
+ContextMenuHelper::~ContextMenuHelper() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ Java_ContextMenuHelper_destroy(env, java_obj_.obj());
+}
+
+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
+ const content::ContextMenuParams& params) {
+ content::ContentViewCore* content_view_core =
+ content::ContentViewCore::FromWebContents(web_contents_);
+
+ if (!content_view_core)
+ return;
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ context_menu_params_ = params;
+ Java_ContextMenuHelper_showContextMenu(
+ env,
+ java_obj_.obj(),
+ content_view_core->GetJavaObject().obj(),
+ ContextMenuHelper::CreateJavaContextMenuParams(params).obj());
+}
+
+// Called to show a custom context menu. Used by the NTP.
+void ContextMenuHelper::ShowCustomContextMenu(
+ const content::ContextMenuParams& params,
+ const base::Callback<void(int)>& callback) {
+ context_menu_callback_ = callback;
+ ShowContextMenu(params);
+}
+
+void ContextMenuHelper::SetPopulator(jobject jpopulator) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ Java_ContextMenuHelper_setPopulator(env, java_obj_.obj(), jpopulator);
+}
+
+const content::ContextMenuParams&
+ContextMenuHelper::GetCurrentContextMenuParams() const {
+ return context_menu_params_;
+}
+
+base::android::ScopedJavaLocalRef<jobject>
+ContextMenuHelper::CreateJavaContextMenuParams(
+ const content::ContextMenuParams& params) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ base::android::ScopedJavaLocalRef<jobject> jmenu_info =
+ AndroidContextMenuParams::Java_ContextMenuParams_create(
+ env,
+ params.media_type,
+ ConvertUTF8ToJavaString(env, params.link_url.spec()).obj(),
+ ConvertUTF16ToJavaString(env, params.link_text).obj(),
+ ConvertUTF8ToJavaString(env, params.unfiltered_link_url.spec()).obj(),
+ ConvertUTF8ToJavaString(env, params.src_url.spec()).obj(),
+ ConvertUTF16ToJavaString(env, params.selection_text).obj(),
+ params.is_editable);
+
+ std::vector<content::MenuItem>::const_iterator i;
+ 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.
+ AndroidContextMenuParams::Java_ContextMenuParams_addCustomItem(
+ env,
+ jmenu_info.obj(),
+ ConvertUTF16ToJavaString(env, i->label).obj(),
+ i->action);
+ }
+
+ return jmenu_info;
+}
+
+void ContextMenuHelper::OnCustomItemSelected(JNIEnv* env,
+ jobject obj,
+ jint action) {
+ if (!context_menu_callback_.is_null()) {
+ context_menu_callback_.Run(action);
+ context_menu_callback_.Reset();
+ }
+}
+
+bool RegisterContextMenuHelper(JNIEnv* env) {
+ return RegisterNativesImpl(env) &&
+ AndroidContextMenuParams::RegisterNativesImpl(env);
+}

Powered by Google App Engine
This is Rietveld 408576698