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" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 child->RemoveFromParent(); | 95 child->RemoveFromParent(); |
96 child->parent_ = this; | 96 child->parent_ = this; |
97 } | 97 } |
98 | 98 |
99 void ViewAndroid::RemoveFromParent() { | 99 void ViewAndroid::RemoveFromParent() { |
100 if (parent_) | 100 if (parent_) |
101 parent_->RemoveChild(this); | 101 parent_->RemoveChild(this); |
102 } | 102 } |
103 | 103 |
104 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | 104 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { |
105 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 105 JNIEnv* env = base::android::AttachCurrentThread(); |
| 106 ScopedJavaLocalRef<jobject> delegate = GetViewAndroidDelegate().get(env); |
106 if (delegate.is_null()) | 107 if (delegate.is_null()) |
107 return ViewAndroid::ScopedAnchorView(); | 108 return ViewAndroid::ScopedAnchorView(); |
108 | 109 |
109 JNIEnv* env = base::android::AttachCurrentThread(); | |
110 return ViewAndroid::ScopedAnchorView( | 110 return ViewAndroid::ScopedAnchorView( |
111 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); | 111 env, Java_ViewAndroidDelegate_acquireView(env, delegate), delegate); |
112 } | 112 } |
113 | 113 |
114 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, | 114 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, |
115 const gfx::RectF& bounds) { | 115 const gfx::RectF& bounds) { |
116 if (bounds.IsEmpty()) | 116 if (bounds.IsEmpty()) |
117 return; | 117 return; |
118 | 118 |
119 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 119 JNIEnv* env = base::android::AttachCurrentThread(); |
| 120 ScopedJavaLocalRef<jobject> delegate = GetViewAndroidDelegate().get(env); |
120 if (delegate.is_null()) | 121 if (delegate.is_null()) |
121 return; | 122 return; |
122 | 123 |
123 float scale = display::Screen::GetScreen() | 124 float scale = display::Screen::GetScreen() |
124 ->GetDisplayNearestWindow(this) | 125 ->GetDisplayNearestWindow(this) |
125 .device_scale_factor(); | 126 .device_scale_factor(); |
126 int left_margin = std::round(bounds.x() * scale); | 127 int left_margin = std::round(bounds.x() * scale); |
127 // TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's | 128 // TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's |
128 // specific to a given web contents/render widget. | 129 // specific to a given web contents/render widget. |
129 int top_margin = std::round( | 130 int top_margin = std::round( |
130 (GetWindowAndroid()->content_offset().y() + bounds.y()) * scale); | 131 (GetWindowAndroid()->content_offset().y() + bounds.y()) * scale); |
131 JNIEnv* env = base::android::AttachCurrentThread(); | |
132 Java_ViewAndroidDelegate_setViewPosition( | 132 Java_ViewAndroidDelegate_setViewPosition( |
133 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), | 133 env, delegate, anchor, bounds.x(), bounds.y(), bounds.width(), |
134 bounds.height(), scale, left_margin, top_margin); | 134 bounds.height(), scale, left_margin, top_margin); |
135 } | 135 } |
136 | 136 |
137 void ViewAndroid::RemoveChild(ViewAndroid* child) { | 137 void ViewAndroid::RemoveChild(ViewAndroid* child) { |
138 DCHECK(child); | 138 DCHECK(child); |
139 DCHECK_EQ(child->parent_, this); | 139 DCHECK_EQ(child->parent_, this); |
140 | 140 |
141 std::list<ViewAndroid*>::iterator it = | 141 std::list<ViewAndroid*>::iterator it = |
142 std::find(children_.begin(), children_.end(), child); | 142 std::find(children_.begin(), children_.end(), child); |
143 DCHECK(it != children_.end()); | 143 DCHECK(it != children_.end()); |
144 children_.erase(it); | 144 children_.erase(it); |
145 child->parent_ = nullptr; | 145 child->parent_ = nullptr; |
146 } | 146 } |
147 | 147 |
148 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 148 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
149 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 149 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
150 } | 150 } |
151 | 151 |
152 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate() | 152 const JavaObjectWeakGlobalRef ViewAndroid::GetViewAndroidDelegate() |
153 const { | 153 const { |
154 JNIEnv* env = base::android::AttachCurrentThread(); | 154 JNIEnv* env = base::android::AttachCurrentThread(); |
155 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); | 155 ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); |
156 if (!delegate.is_null()) | 156 if (!delegate.is_null()) |
157 return delegate; | 157 return delegate_; |
158 | 158 |
159 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; | 159 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; |
160 } | 160 } |
161 | 161 |
162 cc::Layer* ViewAndroid::GetLayer() const { | 162 cc::Layer* ViewAndroid::GetLayer() const { |
163 return layer_.get(); | 163 return layer_.get(); |
164 } | 164 } |
165 | 165 |
166 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 166 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
167 layer_ = layer; | 167 layer_ = layer; |
168 } | 168 } |
169 | 169 |
170 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 170 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
171 const JavaRef<jobject>& jimage) { | 171 const JavaRef<jobject>& jimage) { |
172 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | 172 JNIEnv* env = base::android::AttachCurrentThread(); |
| 173 ScopedJavaLocalRef<jobject> delegate = delegate_.get(env); |
173 if (delegate.is_null()) | 174 if (delegate.is_null()) |
174 return; | 175 return; |
175 JNIEnv* env = base::android::AttachCurrentThread(); | |
176 Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, jimage); | 176 Java_ViewAndroidDelegate_startDragAndDrop(env, delegate, jtext, jimage); |
177 } | 177 } |
178 | 178 |
179 } // namespace ui | 179 } // namespace ui |
OLD | NEW |