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/gfx/android/device_display_info.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 JNIEnv* env = base::android::AttachCurrentThread(); | |
23 Java_ViewAndroidDelegate_removeView(env, delegate_.obj(), view_.obj()); | |
24 view_.Reset(); | |
25 } | |
26 | |
18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate, | 27 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate, |
19 WindowAndroid* root_window) | 28 WindowAndroid* root_window) |
20 : parent_(nullptr), window_(root_window), delegate_(delegate) {} | 29 : parent_(nullptr) |
30 , window_(root_window) | |
31 , delegate_(delegate) {} | |
21 | 32 |
22 ViewAndroid::ViewAndroid() : parent_(nullptr), window_(nullptr) {} | 33 ViewAndroid::ViewAndroid() : parent_(nullptr), window_(nullptr) {} |
23 | 34 |
35 bool ViewAndroid::RegisterWindowAndroid(JNIEnv* env) { | |
36 return RegisterNativesImpl(env); | |
37 } | |
38 | |
24 ViewAndroid::~ViewAndroid() { | 39 ViewAndroid::~ViewAndroid() { |
25 if (parent_) | 40 if (parent_) |
26 parent_->RemoveChild(this); | 41 parent_->RemoveChild(this); |
27 | 42 |
28 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 43 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
29 it != children_.end(); it++) { | 44 it != children_.end(); it++) { |
30 DCHECK_EQ((*it)->parent_, this); | 45 DCHECK_EQ((*it)->parent_, this); |
31 (*it)->parent_ = nullptr; | 46 (*it)->parent_ = nullptr; |
32 } | 47 } |
33 } | 48 } |
(...skipping 14 matching lines...) Expand all Loading... | |
48 DCHECK(child); | 63 DCHECK(child); |
49 DCHECK_EQ(child->parent_, this); | 64 DCHECK_EQ(child->parent_, this); |
50 | 65 |
51 std::list<ViewAndroid*>::iterator it = | 66 std::list<ViewAndroid*>::iterator it = |
52 std::find(children_.begin(), children_.end(), child); | 67 std::find(children_.begin(), children_.end(), child); |
53 DCHECK(it != children_.end()); | 68 DCHECK(it != children_.end()); |
54 children_.erase(it); | 69 children_.erase(it); |
55 child->parent_ = nullptr; | 70 child->parent_ = nullptr; |
56 } | 71 } |
57 | 72 |
73 | |
74 ViewAndroid::ScopedAnchorView* ViewAndroid::AcquireAnchorView() { | |
75 if (delegate_.is_null()) | |
76 return parent_ ? parent_->AcquireAnchorView() : | |
77 new ViewAndroid::ScopedAnchorView(); | |
78 | |
79 JNIEnv* env = base::android::AttachCurrentThread(); | |
80 return new ViewAndroid::ScopedAnchorView( | |
81 Java_ViewAndroidDelegate_acquireView(env, delegate_.obj()), delegate_); | |
82 } | |
83 | |
84 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, | |
85 const gfx::RectF& bounds) { | |
86 if (bounds.IsEmpty()) return; | |
87 | |
88 if (delegate_.is_null()) { | |
89 if (parent_) parent_->SetAnchorRect(anchor, bounds); | |
90 return; | |
91 } | |
92 | |
93 JNIEnv* env = base::android::AttachCurrentThread(); | |
94 gfx::DeviceDisplayInfo device_info; | |
95 float scale = device_info.GetDIPScale(); | |
no sievers
2016/07/18 22:14:16
Can you call GetScaleFactorForNativeView(this) ins
Jinsuk Kim
2016/07/19 07:08:40
Done.
| |
96 int left_margin = std::round(bounds.x() * scale); | |
97 // TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's | |
98 // specific to a given web contents/render widget. | |
99 float content_offset_y_pix = GetWindowAndroid()->content_offset().y(); | |
100 int top_margin = std::round(content_offset_y_pix + bounds.y() * scale); | |
101 Java_ViewAndroidDelegate_setViewPosition(env, | |
102 delegate_.obj(), | |
103 anchor.obj(), | |
104 bounds.x(), | |
105 bounds.y(), | |
106 bounds.width(), | |
107 bounds.height(), | |
108 scale, | |
109 left_margin, | |
110 top_margin); | |
111 } | |
112 | |
113 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() const { | |
no sievers
2016/07/18 22:14:16
nit: This is unused now. But I wonder if it makes
Jinsuk Kim
2016/07/19 07:08:40
That's neat. Done.
| |
114 if (!delegate_.is_null()) | |
115 return delegate_; | |
116 | |
117 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; | |
118 } | |
119 | |
58 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 120 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
59 if (window_) | 121 if (window_) |
60 return window_; | 122 return window_; |
61 | 123 |
62 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 124 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
63 } | 125 } |
64 | 126 |
65 void ViewAndroid::SetWindowAndroid(WindowAndroid* root_window) { | 127 void ViewAndroid::SetWindowAndroid(WindowAndroid* root_window) { |
66 window_ = root_window; | 128 window_ = root_window; |
67 DCHECK(parent_ == nullptr) << "Children shouldn't have a root window"; | 129 DCHECK(parent_ == nullptr) << "Children shouldn't have a root window"; |
68 } | 130 } |
69 | 131 |
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 { | 132 cc::Layer* ViewAndroid::GetLayer() const { |
79 return layer_.get(); | 133 return layer_.get(); |
80 } | 134 } |
81 | 135 |
82 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 136 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
83 layer_ = layer; | 137 layer_ = layer; |
84 } | 138 } |
85 | 139 |
86 } // namespace ui | 140 } // namespace ui |
OLD | NEW |