Chromium Code Reviews| 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 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()) { | |
| 51 Java_ViewAndroidDelegate_removeView(env, | |
| 52 delegate_.get(env).obj(), | |
|
boliu
2016/07/29 20:05:15
null check delegate_
Jinsuk Kim
2016/08/01 02:02:47
view_ != null && delegate_ == null is DCHECK'd in
| |
| 53 view_.get(env).obj()); | |
| 54 } | |
| 55 view_.reset(); | |
| 56 } | |
| 57 | |
| 58 void ViewAndroid::ScopedAnchorView::Reset() { | |
| 59 view_.reset(); | |
| 60 delegate_.reset(); | |
| 61 } | |
| 62 | |
| 63 const base::android::ScopedJavaLocalRef<jobject> | |
| 64 ViewAndroid::ScopedAnchorView::view() const { | |
| 65 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 66 return view_.get(env); | |
| 67 } | |
| 68 | |
| 18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) | 69 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) |
| 19 : parent_(nullptr), delegate_(delegate) {} | 70 : parent_(nullptr) |
| 71 , delegate_(base::android::AttachCurrentThread(), | |
| 72 delegate.obj()) {} | |
| 20 | 73 |
| 21 ViewAndroid::ViewAndroid() : parent_(nullptr) {} | 74 ViewAndroid::ViewAndroid() : parent_(nullptr) {} |
| 22 | 75 |
| 23 ViewAndroid::~ViewAndroid() { | 76 ViewAndroid::~ViewAndroid() { |
| 24 RemoveFromParent(); | 77 RemoveFromParent(); |
| 25 | 78 |
| 26 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 79 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
| 27 it != children_.end(); it++) { | 80 it != children_.end(); it++) { |
| 28 DCHECK_EQ((*it)->parent_, this); | 81 DCHECK_EQ((*it)->parent_, this); |
| 29 (*it)->parent_ = nullptr; | 82 (*it)->parent_ = nullptr; |
| 30 } | 83 } |
| 31 } | 84 } |
| 32 | 85 |
| 86 bool ViewAndroid::RegisterViewAndroid(JNIEnv* env) { | |
| 87 return RegisterNativesImpl(env); | |
| 88 } | |
| 89 | |
| 33 void ViewAndroid::AddChild(ViewAndroid* child) { | 90 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 34 DCHECK(child); | 91 DCHECK(child); |
| 35 DCHECK(std::find(children_.begin(), children_.end(), child) == | 92 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 36 children_.end()); | 93 children_.end()); |
| 37 | 94 |
| 38 children_.push_back(child); | 95 children_.push_back(child); |
| 39 if (child->parent_) | 96 if (child->parent_) |
| 40 child->RemoveFromParent(); | 97 child->RemoveFromParent(); |
| 41 child->parent_ = this; | 98 child->parent_ = this; |
| 42 } | 99 } |
| 43 | 100 |
| 44 void ViewAndroid::RemoveFromParent() { | 101 void ViewAndroid::RemoveFromParent() { |
| 45 if (parent_) | 102 if (parent_) |
| 46 parent_->RemoveChild(this); | 103 parent_->RemoveChild(this); |
| 47 } | 104 } |
| 48 | 105 |
| 106 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() { | |
| 107 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 108 ScopedJavaLocalRef<jobject> delegate(delegate_.get(env)); | |
| 109 if (delegate.is_null()) | |
| 110 return parent_ ? parent_->AcquireAnchorView() | |
| 111 : ViewAndroid::ScopedAnchorView(); | |
| 112 | |
| 113 return ViewAndroid::ScopedAnchorView( | |
| 114 env, | |
| 115 Java_ViewAndroidDelegate_acquireView(env, delegate.obj()), | |
| 116 delegate); | |
| 117 } | |
| 118 | |
| 119 void ViewAndroid::SetAnchorRect(const JavaRef<jobject>& anchor, | |
| 120 const gfx::RectF& bounds) { | |
| 121 if (bounds.IsEmpty()) | |
| 122 return; | |
| 123 | |
| 124 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 125 ScopedJavaLocalRef<jobject> delegate(delegate_.get(env)); | |
| 126 if (delegate.is_null()) | |
| 127 return; | |
| 128 | |
| 129 float scale = GetScaleFactorForNativeView(this); | |
| 130 int left_margin = std::round(bounds.x() * scale); | |
| 131 // TODO(jinsukkim): Move content_offset() to ViewAndroid, since it's | |
| 132 // specific to a given web contents/render widget. | |
| 133 float content_offset_y_pix = GetWindowAndroid()->content_offset().y(); | |
| 134 int top_margin = std::round(content_offset_y_pix + bounds.y() * scale); | |
| 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 JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() const { |
| 65 const { | 163 JNIEnv* env = base::android::AttachCurrentThread(); |
| 66 if (!delegate_.is_null()) | 164 const ScopedJavaLocalRef<jobject>& delegate = delegate_.get(env); |
|
boliu
2016/07/29 20:05:15
ditto
sgurun-gerrit only
2016/07/29 23:44:06
and also what clang caught:
/include -fno-except
Jinsuk Kim
2016/08/01 02:02:47
Fixed
Jinsuk Kim
2016/08/01 02:02:47
Done.
| |
| 67 return delegate_; | 165 if (!delegate.is_null()) |
| 166 return delegate; | |
| 68 | 167 |
| 69 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; | 168 return parent_ ? parent_->GetViewAndroidDelegate() : delegate; |
| 70 } | 169 } |
| 71 | 170 |
| 72 cc::Layer* ViewAndroid::GetLayer() const { | 171 cc::Layer* ViewAndroid::GetLayer() const { |
| 73 return layer_.get(); | 172 return layer_.get(); |
| 74 } | 173 } |
| 75 | 174 |
| 76 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 175 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 77 layer_ = layer; | 176 layer_ = layer; |
| 78 } | 177 } |
| 79 | 178 |
| 80 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 179 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 81 const JavaRef<jobject>& jimage) { | 180 const JavaRef<jobject>& jimage) { |
| 82 WindowAndroid* window_android = GetWindowAndroid(); | 181 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate()); |
| 83 if (!window_android) | 182 if (delegate.is_null()) |
| 84 return; | 183 return; |
| 85 | 184 JNIEnv* env = base::android::AttachCurrentThread(); |
| 86 window_android->StartDragAndDrop(GetViewAndroidDelegate(), jtext, jimage); | 185 Java_ViewAndroidDelegate_startDragAndDrop(env, |
| 186 delegate.obj(), | |
| 187 jtext.obj(), | |
| 188 jimage.obj()); | |
| 87 } | 189 } |
| 88 | 190 |
| 89 } // namespace ui | 191 } // namespace ui |
| OLD | NEW |