Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / web_contents_delegate_android / validation_message_bubble_android.cc
bloba57763697a0a8a447ec630cecc85eadca7ba8445
1 // Copyright (c) 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 "components/web_contents_delegate_android/validation_message_bubble_android.h"
7 #include "base/android/jni_string.h"
8 #include "content/public/browser/android/content_view_core.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/web_contents.h"
11 #include "jni/ValidationMessageBubble_jni.h"
12 #include "ui/gfx/geometry/rect.h"
14 using base::android::ConvertUTF16ToJavaString;
15 using content::ContentViewCore;
16 using content::RenderWidgetHost;
18 namespace {
20 base::android::ScopedJavaLocalRef<jobject> GetJavaContentViewCoreFrom(
21 RenderWidgetHost* widget_host) {
22 ContentViewCore* content_view_core =
23 ContentViewCore::FromWebContents(content::WebContents::FromRenderViewHost(
24 content::RenderViewHost::From(widget_host)));
25 if (!content_view_core)
26 return base::android::ScopedJavaLocalRef<jobject>();
27 return content_view_core->GetJavaObject();
31 namespace web_contents_delegate_android {
33 ValidationMessageBubbleAndroid::ValidationMessageBubbleAndroid(
34 RenderWidgetHost* widget_host,
35 const gfx::Rect& anchor_in_root_view,
36 const base::string16& main_text,
37 const base::string16& sub_text) {
38 base::android::ScopedJavaLocalRef<jobject> java_content_view_core =
39 GetJavaContentViewCoreFrom(widget_host);
40 if (java_content_view_core.is_null())
41 return;
43 JNIEnv* env = base::android::AttachCurrentThread();
44 java_validation_message_bubble_.Reset(
45 Java_ValidationMessageBubble_createAndShow(
46 env,
47 java_content_view_core.obj(),
48 anchor_in_root_view.x(),
49 anchor_in_root_view.y(),
50 anchor_in_root_view.width(),
51 anchor_in_root_view.height(),
52 ConvertUTF16ToJavaString(env, main_text).obj(),
53 ConvertUTF16ToJavaString(env, sub_text).obj()));
56 ValidationMessageBubbleAndroid::~ValidationMessageBubbleAndroid() {
57 Java_ValidationMessageBubble_close(base::android::AttachCurrentThread(),
58 java_validation_message_bubble_.obj());
61 void ValidationMessageBubbleAndroid::SetPositionRelativeToAnchor(
62 RenderWidgetHost* widget_host, const gfx::Rect& anchor_in_root_view) {
63 base::android::ScopedJavaLocalRef<jobject> java_content_view_core =
64 GetJavaContentViewCoreFrom(widget_host);
65 if (java_content_view_core.is_null())
66 return;
68 Java_ValidationMessageBubble_setPositionRelativeToAnchor(
69 base::android::AttachCurrentThread(),
70 java_validation_message_bubble_.obj(),
71 java_content_view_core.obj(),
72 anchor_in_root_view.x(),
73 anchor_in_root_view.y(),
74 anchor_in_root_view.width(),
75 anchor_in_root_view.height());
78 // static
79 bool ValidationMessageBubbleAndroid::Register(JNIEnv* env) {
80 return RegisterNativesImpl(env);
83 } // namespace web_contents_delegate_android