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