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
16using base::android::ConvertUTF8ToJavaString;
17using base::android::ConvertUTF16ToJavaString;
18using base::android::ScopedJavaLocalRef;
19
20DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper);
21
22ContextMenuHelper::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
31ContextMenuHelper::~ContextMenuHelper() {
32  JNIEnv* env = base::android::AttachCurrentThread();
33  Java_ContextMenuHelper_destroy(env, java_obj_.obj());
34}
35
36void 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.
54void ContextMenuHelper::ShowCustomContextMenu(
55    const content::ContextMenuParams& params,
56    const base::Callback<void(int)>& callback) {
57  context_menu_callback_ = callback;
58  ShowContextMenu(params);
59}
60
61void ContextMenuHelper::SetPopulator(jobject jpopulator) {
62  JNIEnv* env = base::android::AttachCurrentThread();
63  Java_ContextMenuHelper_setPopulator(env, java_obj_.obj(), jpopulator);
64}
65
66base::android::ScopedJavaLocalRef<jobject>
67ContextMenuHelper::CreateJavaContextMenuParams(
68    const content::ContextMenuParams& params) {
69  JNIEnv* env = base::android::AttachCurrentThread();
70  base::android::ScopedJavaLocalRef<jobject> jmenu_info =
71      ContextMenuParamsAndroid::Java_ContextMenuParams_create(
72          env,
73          params.media_type,
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(),
79          params.is_editable);
80
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(
84        env,
85        jmenu_info.obj(),
86        ConvertUTF16ToJavaString(env, i->label).obj(),
87        i->action);
88  }
89
90  return jmenu_info;
91}
92
93void ContextMenuHelper::OnCustomItemSelected(JNIEnv* env,
94                                             jobject obj,
95                                             jint action) {
96  if (!context_menu_callback_.is_null()) {
97    context_menu_callback_.Run(action);
98    context_menu_callback_.Reset();
99  }
100}
101
102void ContextMenuHelper::OnStartDownload(JNIEnv* env,
103                                        jobject obj,
104                                        jboolean jis_link) {
105  content::DownloadControllerAndroid::Get()->StartContextMenuDownload(
106      context_menu_params_,
107      web_contents_,
108      jis_link);
109}
110
111bool RegisterContextMenuHelper(JNIEnv* env) {
112  return RegisterNativesImpl(env) &&
113         ContextMenuParamsAndroid::RegisterNativesImpl(env);
114}
115