OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/web_contents_delegate_android/validation_message_bubble_and
roid.h" | 5 #include "components/web_contents_delegate_android/validation_message_bubble_and
roid.h" |
6 | 6 |
7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
8 #include "content/public/browser/android/content_view_core.h" | 8 #include "content/public/browser/android/content_view_core.h" |
9 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "jni/ValidationMessageBubble_jni.h" | 11 #include "jni/ValidationMessageBubble_jni.h" |
12 #include "ui/gfx/geometry/rect.h" | 12 #include "ui/gfx/geometry/rect.h" |
13 | 13 |
14 using base::android::ConvertUTF16ToJavaString; | 14 using base::android::ConvertUTF16ToJavaString; |
15 using content::ContentViewCore; | 15 using content::ContentViewCore; |
16 using content::RenderWidgetHost; | 16 using content::RenderWidgetHost; |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 inline ContentViewCore* GetContentViewCoreFrom(RenderWidgetHost* widget_host) { | 20 base::android::ScopedJavaLocalRef<jobject> GetJavaContentViewCoreFrom( |
21 return ContentViewCore::FromWebContents( | 21 RenderWidgetHost* widget_host) { |
22 content::WebContents::FromRenderViewHost( | 22 ContentViewCore* content_view_core = |
| 23 ContentViewCore::FromWebContents(content::WebContents::FromRenderViewHost( |
23 content::RenderViewHost::From(widget_host))); | 24 content::RenderViewHost::From(widget_host))); |
| 25 if (!content_view_core) |
| 26 return base::android::ScopedJavaLocalRef<jobject>(); |
| 27 return content_view_core->GetJavaObject(); |
24 } | 28 } |
25 | |
26 } | 29 } |
27 | 30 |
28 namespace web_contents_delegate_android { | 31 namespace web_contents_delegate_android { |
29 | 32 |
30 ValidationMessageBubbleAndroid::ValidationMessageBubbleAndroid( | 33 ValidationMessageBubbleAndroid::ValidationMessageBubbleAndroid( |
31 RenderWidgetHost* widget_host, | 34 RenderWidgetHost* widget_host, |
32 const gfx::Rect& anchor_in_root_view, | 35 const gfx::Rect& anchor_in_root_view, |
33 const base::string16& main_text, | 36 const base::string16& main_text, |
34 const base::string16& sub_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; |
| 42 |
35 JNIEnv* env = base::android::AttachCurrentThread(); | 43 JNIEnv* env = base::android::AttachCurrentThread(); |
36 java_validation_message_bubble_.Reset( | 44 java_validation_message_bubble_.Reset( |
37 Java_ValidationMessageBubble_createAndShow( | 45 Java_ValidationMessageBubble_createAndShow( |
38 env, | 46 env, |
39 GetContentViewCoreFrom(widget_host)->GetJavaObject().obj(), | 47 java_content_view_core.obj(), |
40 anchor_in_root_view.x(), | 48 anchor_in_root_view.x(), |
41 anchor_in_root_view.y(), | 49 anchor_in_root_view.y(), |
42 anchor_in_root_view.width(), | 50 anchor_in_root_view.width(), |
43 anchor_in_root_view.height(), | 51 anchor_in_root_view.height(), |
44 ConvertUTF16ToJavaString(env, main_text).obj(), | 52 ConvertUTF16ToJavaString(env, main_text).obj(), |
45 ConvertUTF16ToJavaString(env, sub_text).obj())); | 53 ConvertUTF16ToJavaString(env, sub_text).obj())); |
46 } | 54 } |
47 | 55 |
48 ValidationMessageBubbleAndroid::~ValidationMessageBubbleAndroid() { | 56 ValidationMessageBubbleAndroid::~ValidationMessageBubbleAndroid() { |
49 Java_ValidationMessageBubble_close(base::android::AttachCurrentThread(), | 57 Java_ValidationMessageBubble_close(base::android::AttachCurrentThread(), |
50 java_validation_message_bubble_.obj()); | 58 java_validation_message_bubble_.obj()); |
51 } | 59 } |
52 | 60 |
53 void ValidationMessageBubbleAndroid::SetPositionRelativeToAnchor( | 61 void ValidationMessageBubbleAndroid::SetPositionRelativeToAnchor( |
54 RenderWidgetHost* widget_host, const gfx::Rect& anchor_in_root_view) { | 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; |
| 67 |
55 Java_ValidationMessageBubble_setPositionRelativeToAnchor( | 68 Java_ValidationMessageBubble_setPositionRelativeToAnchor( |
56 base::android::AttachCurrentThread(), | 69 base::android::AttachCurrentThread(), |
57 java_validation_message_bubble_.obj(), | 70 java_validation_message_bubble_.obj(), |
58 GetContentViewCoreFrom(widget_host)->GetJavaObject().obj(), | 71 java_content_view_core.obj(), |
59 anchor_in_root_view.x(), | 72 anchor_in_root_view.x(), |
60 anchor_in_root_view.y(), | 73 anchor_in_root_view.y(), |
61 anchor_in_root_view.width(), | 74 anchor_in_root_view.width(), |
62 anchor_in_root_view.height()); | 75 anchor_in_root_view.height()); |
63 } | 76 } |
64 | 77 |
65 // static | 78 // static |
66 bool ValidationMessageBubbleAndroid::Register(JNIEnv* env) { | 79 bool ValidationMessageBubbleAndroid::Register(JNIEnv* env) { |
67 return RegisterNativesImpl(env); | 80 return RegisterNativesImpl(env); |
68 } | 81 } |
69 | 82 |
70 } // namespace web_contents_delegate_android | 83 } // namespace web_contents_delegate_android |
OLD | NEW |