Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / android / context_menu_helper.cc
blob4ce80fb6834ac0b5aa5e8ad2a5c75df5e8b70828
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 "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
10 #include "content/public/browser/android/content_view_core.h"
11 #include "content/public/browser/android/download_controller_android.h"
12 #include "content/public/common/context_menu_params.h"
13 #include "jni/ContextMenuHelper_jni.h"
14 #include "jni/ContextMenuParams_jni.h"
15 #include "ui/gfx/geometry/point.h"
17 using base::android::ConvertJavaStringToUTF8;
18 using base::android::ConvertUTF8ToJavaString;
19 using base::android::ConvertUTF16ToJavaString;
20 using base::android::ScopedJavaLocalRef;
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ContextMenuHelper);
24 ContextMenuHelper::ContextMenuHelper(content::WebContents* web_contents)
25 : web_contents_(web_contents) {
26 JNIEnv* env = base::android::AttachCurrentThread();
27 java_obj_.Reset(
28 env,
29 Java_ContextMenuHelper_create(env, reinterpret_cast<long>(this)).obj());
30 DCHECK(!java_obj_.is_null());
33 ContextMenuHelper::~ContextMenuHelper() {
34 JNIEnv* env = base::android::AttachCurrentThread();
35 Java_ContextMenuHelper_destroy(env, java_obj_.obj());
38 void ContextMenuHelper::ShowContextMenu(
39 const content::ContextMenuParams& params) {
40 content::ContentViewCore* content_view_core =
41 content::ContentViewCore::FromWebContents(web_contents_);
43 if (!content_view_core)
44 return;
46 base::android::ScopedJavaLocalRef<jobject> jcontent_view_core(
47 content_view_core->GetJavaObject());
49 if (jcontent_view_core.is_null())
50 return;
52 JNIEnv* env = base::android::AttachCurrentThread();
53 context_menu_params_ = params;
54 Java_ContextMenuHelper_showContextMenu(
55 env,
56 java_obj_.obj(),
57 jcontent_view_core.obj(),
58 ContextMenuHelper::CreateJavaContextMenuParams(params).obj());
61 void ContextMenuHelper::SetPopulator(jobject jpopulator) {
62 JNIEnv* env = base::android::AttachCurrentThread();
63 Java_ContextMenuHelper_setPopulator(env, java_obj_.obj(), jpopulator);
66 base::android::ScopedJavaLocalRef<jobject>
67 ContextMenuHelper::CreateJavaContextMenuParams(
68 const content::ContextMenuParams& params) {
69 GURL sanitizedReferrer = (params.frame_url.is_empty() ?
70 params.page_url : params.frame_url).GetAsReferrer();
72 std::map<std::string, std::string>::const_iterator it =
73 params.properties.find(data_reduction_proxy::chrome_proxy_header());
74 bool image_was_fetched_lo_fi =
75 it != params.properties.end() &&
76 it->second == data_reduction_proxy::chrome_proxy_lo_fi_directive();
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.page_url.spec()).obj(),
84 ConvertUTF8ToJavaString(env, params.link_url.spec()).obj(),
85 ConvertUTF16ToJavaString(env, params.link_text).obj(),
86 ConvertUTF8ToJavaString(env, params.unfiltered_link_url.spec()).obj(),
87 ConvertUTF8ToJavaString(env, params.src_url.spec()).obj(),
88 ConvertUTF16ToJavaString(env, params.title_text).obj(),
89 image_was_fetched_lo_fi,
90 ConvertUTF8ToJavaString(env, sanitizedReferrer.spec()).obj(),
91 params.referrer_policy);
93 return jmenu_info;
96 void ContextMenuHelper::OnStartDownload(JNIEnv* env,
97 jobject obj,
98 jboolean jis_link,
99 jstring jheaders) {
100 std::string headers(ConvertJavaStringToUTF8(env, jheaders));
101 content::DownloadControllerAndroid::Get()->StartContextMenuDownload(
102 context_menu_params_,
103 web_contents_,
104 jis_link,
105 headers);
108 bool RegisterContextMenuHelper(JNIEnv* env) {
109 return RegisterNativesImpl(env) &&
110 ContextMenuParamsAndroid::RegisterNativesImpl(env);