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" | |
12 #include "ui/android/window_android.h" | |
13 #include "ui/base/layout.h" | |
11 | 14 |
12 namespace ui { | 15 namespace ui { |
13 | 16 |
14 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
15 using base::android::JavaRef; | 18 using base::android::JavaRef; |
16 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
17 | 20 |
21 ViewAndroid::ScopedAnchorView::ScopedAnchorView( | |
22 const JavaRef<jobject>& jview, | |
23 const JavaRef<jobject>& jdelegate) | |
24 : view_(jview), delegate_(jdelegate) { } | |
no sievers
2016/07/19 22:29:30
nit: DCHECK(!jdelegate.is_null() || jview.is_null(
Jinsuk Kim
2016/07/20 00:55:18
Done.
| |
25 | |
26 ViewAndroid::ScopedAnchorView::ScopedAnchorView() | |
27 : ScopedAnchorView(nullptr, nullptr) { } | |
28 | |
29 ViewAndroid::ScopedAnchorView::ScopedAnchorView(ScopedAnchorView&& other) { | |
30 JNIEnv* env = base::android::AttachCurrentThread(); | |
31 view_.Reset(env, other.view_.Release()); | |
32 delegate_.Reset(env, other.delegate_.Release()); | |
33 } | |
34 | |
35 ViewAndroid::ScopedAnchorView& | |
36 ViewAndroid::ScopedAnchorView::operator=(ScopedAnchorView&& other) { | |
37 JNIEnv* env = base::android::AttachCurrentThread(); | |
no sievers
2016/07/19 22:29:30
only assign if (this != &other)
Jinsuk Kim
2016/07/20 00:55:18
Done.
| |
38 view_.Reset(env, other.view_.Release()); | |
39 delegate_.Reset(env, other.delegate_.Release()); | |
40 return *this; | |
41 } | |
42 | |
43 ViewAndroid::ScopedAnchorView::~ScopedAnchorView() { | |
44 JNIEnv* env = base::android::AttachCurrentThread(); | |
45 if (!delegate_.is_null()) | |
no sievers
2016/07/19 22:29:30
better to check if (!view_.is_null()) (and see DCH
Jinsuk Kim
2016/07/20 00:55:18
Done.
| |
46 Java_ViewAndroidDelegate_removeView(env, delegate_.obj(), view_.obj()); | |
47 view_.Reset(); | |
48 } | |
49 | |
18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate, | 50 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate, |
19 WindowAndroid* root_window) | 51 WindowAndroid* root_window) |
20 : parent_(nullptr), window_(root_window), delegate_(delegate) {} | 52 : parent_(nullptr) |
53 , window_(root_window) | |
54 , delegate_(delegate) {} | |
21 | 55 |
22 ViewAndroid::ViewAndroid() : parent_(nullptr), window_(nullptr) {} | 56 ViewAndroid::ViewAndroid() : parent_(nullptr), window_(nullptr) {} |
23 | 57 |
58 bool ViewAndroid::RegisterViewAndroid(JNIEnv* env) { | |
59 return RegisterNativesImpl(env); | |
60 } | |
61 | |
24 ViewAndroid::~ViewAndroid() { | 62 ViewAndroid::~ViewAndroid() { |
25 if (parent_) | 63 if (parent_) |
26 parent_->RemoveChild(this); | 64 parent_->RemoveChild(this); |
27 | 65 |
28 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 66 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
29 it != children_.end(); it++) { | 67 it != children_.end(); it++) { |
30 DCHECK_EQ((*it)->parent_, this); | 68 DCHECK_EQ((*it)->parent_, this); |
31 (*it)->parent_ = nullptr; | 69 (*it)->parent_ = nullptr; |
32 } | 70 } |
33 } | 71 } |
(...skipping 14 matching lines...) Expand all Loading... | |
48 DCHECK(child); | 86 DCHECK(child); |
49 DCHECK_EQ(child->parent_, this); | 87 DCHECK_EQ(child->parent_, this); |
50 | 88 |
51 std::list<ViewAndroid*>::iterator it = | 89 std::list<ViewAndroid*>::iterator it = |
52 std::find(children_.begin(), children_.end(), child); | 90 std::find(children_.begin(), children_.end(), child); |
53 DCHECK(it != children_.end()); | 91 DCHECK(it != children_.end()); |
54 children_.erase(it); | 92 children_.erase(it); |
55 child->parent_ = nullptr; | 93 child->parent_ = nullptr; |
56 } | 94 } |
57 | 95 |
96 | |
97 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | |
98 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | |
99 if (delegate.is_null()) | |
100 return ViewAndroid::ScopedAnchorView(); | |
101 | |
102 JNIEnv* env = base::android::AttachCurrentThread(); | |
103 return ViewAndroid::ScopedAnchorView( | |
104 Java_ViewAndroidDelegate_acquireView(env, delegate.obj()), | |
105 delegate); | |
106 } | |
107 | |
108 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, | |
109 const gfx::RectF& bounds) { | |
110 if (bounds.IsEmpty()) return; | |
111 | |
112 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); | |
113 if (delegate.is_null()) | |
114 return; | |
115 | |
116 JNIEnv* env = base::android::AttachCurrentThread(); | |
117 float scale = GetScaleFactorForNativeView(this); | |
118 int left_margin = std::round(bounds.x() * scale); | |
119 // TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's | |
120 // specific to a given web contents/render widget. | |
121 float content_offset_y_pix = GetWindowAndroid()->content_offset().y(); | |
122 int top_margin = std::round(content_offset_y_pix + bounds.y() * scale); | |
123 Java_ViewAndroidDelegate_setViewPosition(env, | |
124 delegate.obj(), | |
125 anchor.obj(), | |
126 bounds.x(), | |
127 bounds.y(), | |
128 bounds.width(), | |
129 bounds.height(), | |
130 scale, | |
131 left_margin, | |
132 top_margin); | |
133 } | |
134 | |
135 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() const { | |
136 if (!delegate_.is_null()) | |
137 return delegate_; | |
138 | |
139 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; | |
140 } | |
141 | |
58 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 142 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
59 if (window_) | 143 if (window_) |
60 return window_; | 144 return window_; |
61 | 145 |
62 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 146 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
63 } | 147 } |
64 | 148 |
65 void ViewAndroid::SetWindowAndroid(WindowAndroid* root_window) { | 149 void ViewAndroid::SetWindowAndroid(WindowAndroid* root_window) { |
66 window_ = root_window; | 150 window_ = root_window; |
67 DCHECK(parent_ == nullptr) << "Children shouldn't have a root window"; | 151 DCHECK(parent_ == nullptr) << "Children shouldn't have a root window"; |
68 } | 152 } |
69 | 153 |
70 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() | |
71 const { | |
72 if (!delegate_.is_null()) | |
73 return delegate_; | |
74 | |
75 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; | |
76 } | |
77 | |
78 cc::Layer* ViewAndroid::GetLayer() const { | 154 cc::Layer* ViewAndroid::GetLayer() const { |
79 return layer_.get(); | 155 return layer_.get(); |
80 } | 156 } |
81 | 157 |
82 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 158 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
83 layer_ = layer; | 159 layer_ = layer; |
84 } | 160 } |
85 | 161 |
86 } // namespace ui | 162 } // namespace ui |
OLD | NEW |