OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/android/view_android.h" | 5 #include "ui/android/view_android.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "cc/layers/layer.h" | 10 #include "cc/layers/layer.h" |
| 11 #include "jni/ViewAndroidDelegate_jni.h" |
11 #include "ui/android/window_android.h" | 12 #include "ui/android/window_android.h" |
12 | 13 #include "ui/base/layout.h" |
13 | 14 |
14 namespace ui { | 15 namespace ui { |
15 | 16 |
16 using base::android::JavaRef; | 17 using base::android::JavaRef; |
17 | 18 |
| 19 ViewAndroid::ScopedAnchorView::ScopedAnchorView( |
| 20 const JavaRef<jobject>& jview, |
| 21 const JavaRef<jobject>& jdelegate) |
| 22 : view_(jview), delegate_(jdelegate) { |
| 23 // If there's a view, then we need a delegate to remove it. |
| 24 DCHECK(!jdelegate.is_null() || jview.is_null()); |
| 25 } |
| 26 |
| 27 ViewAndroid::ScopedAnchorView::ScopedAnchorView() |
| 28 : ScopedAnchorView(nullptr, nullptr) { } |
| 29 |
| 30 ViewAndroid::ScopedAnchorView::ScopedAnchorView(ScopedAnchorView&& other) { |
| 31 JNIEnv* env = base::android::AttachCurrentThread(); |
| 32 view_.Reset(env, other.view_.Release()); |
| 33 delegate_.Reset(env, other.delegate_.Release()); |
| 34 } |
| 35 |
| 36 ViewAndroid::ScopedAnchorView& |
| 37 ViewAndroid::ScopedAnchorView::operator=(ScopedAnchorView&& other) { |
| 38 JNIEnv* env = base::android::AttachCurrentThread(); |
| 39 if (this != &other) { |
| 40 view_.Reset(env, other.view_.Release()); |
| 41 delegate_.Reset(env, other.delegate_.Release()); |
| 42 } |
| 43 return *this; |
| 44 } |
| 45 |
| 46 ViewAndroid::ScopedAnchorView::~ScopedAnchorView() { |
| 47 JNIEnv* env = base::android::AttachCurrentThread(); |
| 48 if (!view_.is_null()) |
| 49 Java_ViewAndroidDelegate_removeView(env, delegate_.obj(), view_.obj()); |
| 50 view_.Reset(); |
| 51 } |
| 52 |
| 53 void ViewAndroid::ScopedAnchorView::Reset() { |
| 54 view_.Reset(); |
| 55 delegate_.Reset(); |
| 56 } |
| 57 |
18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 58 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) |
19 : parent_(nullptr), delegate_(delegate) {} | 59 : parent_(nullptr), delegate_(delegate) {} |
20 | 60 |
21 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | 61 ViewAndroid::ViewAndroid() : parent_(nullptr) {} |
22 | 62 |
23 ViewAndroid::~ViewAndroid() { | 63 ViewAndroid::~ViewAndroid() { |
24 RemoveFromParent(); | 64 RemoveFromParent(); |
25 | 65 |
26 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 66 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
27 it != children_.end(); it++) { | 67 it != children_.end(); it++) { |
28 DCHECK_EQ((*it)->parent_, this); | 68 DCHECK_EQ((*it)->parent_, this); |
29 (*it)->parent_ = nullptr; | 69 (*it)->parent_ = nullptr; |
30 } | 70 } |
31 } | 71 } |
32 | 72 |
| 73 bool ViewAndroid::RegisterViewAndroid(JNIEnv* env) { |
| 74 return RegisterNativesImpl(env); |
| 75 } |
| 76 |
33 void ViewAndroid::AddChild(ViewAndroid* child) { | 77 void ViewAndroid::AddChild(ViewAndroid* child) { |
34 DCHECK(child); | 78 DCHECK(child); |
35 DCHECK(std::find(children_.begin(), children_.end(), child) == | 79 DCHECK(std::find(children_.begin(), children_.end(), child) == |
36 children_.end()); | 80 children_.end()); |
37 | 81 |
38 children_.push_back(child); | 82 children_.push_back(child); |
39 if (child->parent_) | 83 if (child->parent_) |
40 child->RemoveFromParent(); | 84 child->RemoveFromParent(); |
41 child->parent_ = this; | 85 child->parent_ = this; |
42 } | 86 } |
43 | 87 |
44 void ViewAndroid::RemoveFromParent() { | 88 void ViewAndroid::RemoveFromParent() { |
45 if (parent_) | 89 if (parent_) |
46 parent_->RemoveChild(this); | 90 parent_->RemoveChild(this); |
47 } | 91 } |
48 | 92 |
| 93 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
| 94 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 95 if (delegate.is_null()) |
| 96 return ViewAndroid::ScopedAnchorView(); |
| 97 |
| 98 JNIEnv* env = base::android::AttachCurrentThread(); |
| 99 return ViewAndroid::ScopedAnchorView( |
| 100 Java_ViewAndroidDelegate_acquireView(env, delegate.obj()), |
| 101 delegate); |
| 102 } |
| 103 |
| 104 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, |
| 105 const gfx::RectF& bounds) { |
| 106 if (bounds.IsEmpty()) return; |
| 107 |
| 108 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 109 if (delegate.is_null()) |
| 110 return; |
| 111 |
| 112 JNIEnv* env = base::android::AttachCurrentThread(); |
| 113 float scale = GetScaleFactorForNativeView(this); |
| 114 int left_margin = std::round(bounds.x() * scale); |
| 115 // TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's |
| 116 // specific to a given web contents/render widget. |
| 117 float content_offset_y_pix = GetWindowAndroid()->content_offset().y(); |
| 118 int top_margin = std::round(content_offset_y_pix + bounds.y() * scale); |
| 119 Java_ViewAndroidDelegate_setViewPosition(env, |
| 120 delegate.obj(), |
| 121 anchor.obj(), |
| 122 bounds.x(), |
| 123 bounds.y(), |
| 124 bounds.width(), |
| 125 bounds.height(), |
| 126 scale, |
| 127 left_margin, |
| 128 top_margin); |
| 129 } |
| 130 |
49 void ViewAndroid::RemoveChild(ViewAndroid* child) { | 131 void ViewAndroid::RemoveChild(ViewAndroid* child) { |
50 DCHECK(child); | 132 DCHECK(child); |
51 DCHECK_EQ(child->parent_, this); | 133 DCHECK_EQ(child->parent_, this); |
52 | 134 |
53 std::list<ViewAndroid*>::iterator it = | 135 std::list<ViewAndroid*>::iterator it = |
54 std::find(children_.begin(), children_.end(), child); | 136 std::find(children_.begin(), children_.end(), child); |
55 DCHECK(it != children_.end()); | 137 DCHECK(it != children_.end()); |
56 children_.erase(it); | 138 children_.erase(it); |
57 child->parent_ = nullptr; | 139 child->parent_ = nullptr; |
58 } | 140 } |
59 | 141 |
60 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 142 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
61 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 143 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
62 } | 144 } |
63 | 145 |
64 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() | 146 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() const { |
65 const { | |
66 if (!delegate_.is_null()) | 147 if (!delegate_.is_null()) |
67 return delegate_; | 148 return delegate_; |
68 | 149 |
69 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; | 150 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; |
70 } | 151 } |
71 | 152 |
72 cc::Layer* ViewAndroid::GetLayer() const { | 153 cc::Layer* ViewAndroid::GetLayer() const { |
73 return layer_.get(); | 154 return layer_.get(); |
74 } | 155 } |
75 | 156 |
76 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 157 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
77 layer_ = layer; | 158 layer_ = layer; |
78 } | 159 } |
79 | 160 |
80 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 161 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
81 const JavaRef<jobject>& jimage) { | 162 const JavaRef<jobject>& jimage) { |
82 WindowAndroid* window_android = GetWindowAndroid(); | 163 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
83 if (!window_android) | 164 if (delegate.is_null()) |
84 return; | 165 return; |
85 | 166 JNIEnv* env = base::android::AttachCurrentThread(); |
86 window_android->StartDragAndDrop(GetViewAndroidDelegate(), jtext, jimage); | 167 Java_ViewAndroidDelegate_startDragAndDrop(env, |
| 168 delegate.obj(), |
| 169 jtext.obj(), |
| 170 jimage.obj()); |
87 } | 171 } |
88 | 172 |
89 } // namespace ui | 173 } // namespace ui |
OLD | NEW |