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