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