Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: ui/android/view_android.cc

Issue 2103243002: Factor out ContentViewAndroidDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use px values/rebased Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/android/view_android.h ('k') | ui/android/window_android.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 const ScopedJavaLocalRef<jobject> view = view_.get(env);
51 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env);
52 if (!view.is_null() && !delegate.is_null()) {
53 Java_ViewAndroidDelegate_removeView(env,
54 delegate.obj(),
55 view.obj());
56 }
57 view_.reset();
58 }
59
60 void ViewAndroid::ScopedAnchorView::Reset() {
61 view_.reset();
62 delegate_.reset();
63 }
64
65 const base::android::ScopedJavaLocalRef<jobject>
66 ViewAndroid::ScopedAnchorView::view() const {
67 JNIEnv* env = base::android::AttachCurrentThread();
68 return view_.get(env);
69 }
70
18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) 71 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate)
19 : parent_(nullptr), delegate_(delegate) {} 72 : parent_(nullptr)
73 , delegate_(base::android::AttachCurrentThread(),
74 delegate.obj()) {}
20 75
21 ViewAndroid::ViewAndroid() : parent_(nullptr) {} 76 ViewAndroid::ViewAndroid() : parent_(nullptr) {}
22 77
23 ViewAndroid::~ViewAndroid() { 78 ViewAndroid::~ViewAndroid() {
24 RemoveFromParent(); 79 RemoveFromParent();
25 80
26 for (std::list<ViewAndroid*>::iterator it = children_.begin(); 81 for (std::list<ViewAndroid*>::iterator it = children_.begin();
27 it != children_.end(); it++) { 82 it != children_.end(); it++) {
28 DCHECK_EQ((*it)->parent_, this); 83 DCHECK_EQ((*it)->parent_, this);
29 (*it)->parent_ = nullptr; 84 (*it)->parent_ = nullptr;
30 } 85 }
31 } 86 }
32 87
33 void ViewAndroid::AddChild(ViewAndroid* child) { 88 void ViewAndroid::AddChild(ViewAndroid* child) {
34 DCHECK(child); 89 DCHECK(child);
35 DCHECK(std::find(children_.begin(), children_.end(), child) == 90 DCHECK(std::find(children_.begin(), children_.end(), child) ==
36 children_.end()); 91 children_.end());
37 92
38 children_.push_back(child); 93 children_.push_back(child);
39 if (child->parent_) 94 if (child->parent_)
40 child->RemoveFromParent(); 95 child->RemoveFromParent();
41 child->parent_ = this; 96 child->parent_ = this;
42 } 97 }
43 98
44 void ViewAndroid::RemoveFromParent() { 99 void ViewAndroid::RemoveFromParent() {
45 if (parent_) 100 if (parent_)
46 parent_->RemoveChild(this); 101 parent_->RemoveChild(this);
47 } 102 }
48 103
104 ViewAndroid::ScopedAnchorView ViewAndroid::AcquireAnchorView() {
105 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
106 if (delegate.is_null())
107 return ViewAndroid::ScopedAnchorView();
108
109 JNIEnv* env = base::android::AttachCurrentThread();
110 return ViewAndroid::ScopedAnchorView(
111 env,
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())
119 return;
120
121 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
122 if (delegate.is_null())
123 return;
124
125 float scale = GetScaleFactorForNativeView(this);
sadrul 2016/08/04 15:54:50 Is this the only API you are using from ui/base? u
Jinsuk Kim 2016/08/04 22:13:06 Pulled in display/{display,screen}.h instead.
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 JNIEnv* env = base::android::AttachCurrentThread();
132 Java_ViewAndroidDelegate_setViewPosition(env,
133 delegate.obj(),
134 anchor.obj(),
135 bounds.x(),
136 bounds.y(),
137 bounds.width(),
138 bounds.height(),
139 scale,
140 left_margin,
141 top_margin);
142 }
143
49 void ViewAndroid::RemoveChild(ViewAndroid* child) { 144 void ViewAndroid::RemoveChild(ViewAndroid* child) {
50 DCHECK(child); 145 DCHECK(child);
51 DCHECK_EQ(child->parent_, this); 146 DCHECK_EQ(child->parent_, this);
52 147
53 std::list<ViewAndroid*>::iterator it = 148 std::list<ViewAndroid*>::iterator it =
54 std::find(children_.begin(), children_.end(), child); 149 std::find(children_.begin(), children_.end(), child);
55 DCHECK(it != children_.end()); 150 DCHECK(it != children_.end());
56 children_.erase(it); 151 children_.erase(it);
57 child->parent_ = nullptr; 152 child->parent_ = nullptr;
58 } 153 }
59 154
60 WindowAndroid* ViewAndroid::GetWindowAndroid() const { 155 WindowAndroid* ViewAndroid::GetWindowAndroid() const {
61 return parent_ ? parent_->GetWindowAndroid() : nullptr; 156 return parent_ ? parent_->GetWindowAndroid() : nullptr;
62 } 157 }
63 158
64 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() 159 const ScopedJavaLocalRef<jobject> ViewAndroid::GetViewAndroidDelegate()
65 const { 160 const {
66 if (!delegate_.is_null()) 161 JNIEnv* env = base::android::AttachCurrentThread();
67 return delegate_; 162 const ScopedJavaLocalRef<jobject> delegate = delegate_.get(env);
163 if (!delegate.is_null())
164 return delegate;
68 165
69 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; 166 return parent_ ? parent_->GetViewAndroidDelegate() : delegate;
70 } 167 }
71 168
72 cc::Layer* ViewAndroid::GetLayer() const { 169 cc::Layer* ViewAndroid::GetLayer() const {
73 return layer_.get(); 170 return layer_.get();
74 } 171 }
75 172
76 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { 173 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) {
77 layer_ = layer; 174 layer_ = layer;
78 } 175 }
79 176
80 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, 177 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext,
81 const JavaRef<jobject>& jimage) { 178 const JavaRef<jobject>& jimage) {
82 WindowAndroid* window_android = GetWindowAndroid(); 179 ScopedJavaLocalRef<jobject> delegate(GetViewAndroidDelegate());
83 if (!window_android) 180 if (delegate.is_null())
84 return; 181 return;
85 182 JNIEnv* env = base::android::AttachCurrentThread();
86 window_android->StartDragAndDrop(GetViewAndroidDelegate(), jtext, jimage); 183 Java_ViewAndroidDelegate_startDragAndDrop(env,
184 delegate.obj(),
185 jtext.obj(),
186 jimage.obj());
87 } 187 }
88 188
89 } // namespace ui 189 } // namespace ui
OLDNEW
« no previous file with comments | « ui/android/view_android.h ('k') | ui/android/window_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698