File: context_menu_helper.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (125 lines) | stat: -rw-r--r-- 4,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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/browser/android/download_controller_android.h"
#include "content/public/common/context_menu_params.h"
#include "jni/ContextMenuHelper_jni.h"
#include "jni/ContextMenuParams_jni.h"
#include "ui/gfx/geometry/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(
    const content::ContextMenuParams& params) {
  content::ContentViewCore* content_view_core =
      content::ContentViewCore::FromWebContents(web_contents_);

  if (!content_view_core)
    return;

  base::android::ScopedJavaLocalRef<jobject> jcontent_view_core(
      content_view_core->GetJavaObject());

  if (jcontent_view_core.is_null())
    return;

  JNIEnv* env = base::android::AttachCurrentThread();
  context_menu_params_ = params;
  Java_ContextMenuHelper_showContextMenu(
      env,
      java_obj_.obj(),
      jcontent_view_core.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);
}

base::android::ScopedJavaLocalRef<jobject>
ContextMenuHelper::CreateJavaContextMenuParams(
    const content::ContextMenuParams& params) {
  GURL sanitizedReferrer = (params.frame_url.is_empty() ?
      params.page_url : params.frame_url).GetAsReferrer();

  JNIEnv* env = base::android::AttachCurrentThread();
  base::android::ScopedJavaLocalRef<jobject> jmenu_info =
      ContextMenuParamsAndroid::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,
          ConvertUTF8ToJavaString(env, sanitizedReferrer.spec()).obj(),
          params.referrer_policy);

  std::vector<content::MenuItem>::const_iterator i;
  for (i = params.custom_items.begin(); i != params.custom_items.end(); ++i) {
    ContextMenuParamsAndroid::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();
  }
}

void ContextMenuHelper::OnStartDownload(JNIEnv* env,
                                        jobject obj,
                                        jboolean jis_link) {
  content::DownloadControllerAndroid::Get()->StartContextMenuDownload(
      context_menu_params_,
      web_contents_,
      jis_link);
}

bool RegisterContextMenuHelper(JNIEnv* env) {
  return RegisterNativesImpl(env) &&
         ContextMenuParamsAndroid::RegisterNativesImpl(env);
}