| 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 "ui/android/window_android.h" | 11 #include "ui/android/window_android.h" |
| 12 | 12 |
| 13 | 13 |
| 14 namespace ui { | 14 namespace ui { |
| 15 | 15 |
| 16 using base::android::JavaRef; | 16 using base::android::JavaRef; |
| 17 | 17 |
| 18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate, | 18 ViewAndroid::ViewAndroid(const JavaRef<jobject>& delegate) |
| 19 WindowAndroid* root_window) | 19 : parent_(nullptr), delegate_(delegate) {} |
| 20 : parent_(nullptr), window_(root_window), delegate_(delegate) {} | |
| 21 | 20 |
| 22 ViewAndroid::ViewAndroid() : parent_(nullptr), window_(nullptr) {} | 21 ViewAndroid::ViewAndroid() : parent_(nullptr) {} |
| 23 | 22 |
| 24 ViewAndroid::~ViewAndroid() { | 23 ViewAndroid::~ViewAndroid() { |
| 25 if (parent_) | 24 RemoveFromParent(); |
| 26 parent_->RemoveChild(this); | |
| 27 | 25 |
| 28 for (std::list<ViewAndroid*>::iterator it = children_.begin(); | 26 for (std::list<ViewAndroid*>::iterator it = children_.begin(); |
| 29 it != children_.end(); it++) { | 27 it != children_.end(); it++) { |
| 30 DCHECK_EQ((*it)->parent_, this); | 28 DCHECK_EQ((*it)->parent_, this); |
| 31 (*it)->parent_ = nullptr; | 29 (*it)->parent_ = nullptr; |
| 32 } | 30 } |
| 33 } | 31 } |
| 34 | 32 |
| 35 void ViewAndroid::AddChild(ViewAndroid* child) { | 33 void ViewAndroid::AddChild(ViewAndroid* child) { |
| 36 DCHECK(child); | 34 DCHECK(child); |
| 37 DCHECK(child->window_ == nullptr) << "Children shouldn't have a root window"; | |
| 38 DCHECK(std::find(children_.begin(), children_.end(), child) == | 35 DCHECK(std::find(children_.begin(), children_.end(), child) == |
| 39 children_.end()); | 36 children_.end()); |
| 40 | 37 |
| 41 children_.push_back(child); | 38 children_.push_back(child); |
| 42 if (child->parent_) | 39 if (child->parent_) |
| 43 child->parent_->RemoveChild(child); | 40 child->RemoveFromParent(); |
| 44 child->parent_ = this; | 41 child->parent_ = this; |
| 45 } | 42 } |
| 46 | 43 |
| 44 void ViewAndroid::RemoveFromParent() { |
| 45 if (parent_) |
| 46 parent_->RemoveChild(this); |
| 47 } |
| 48 |
| 47 void ViewAndroid::RemoveChild(ViewAndroid* child) { | 49 void ViewAndroid::RemoveChild(ViewAndroid* child) { |
| 48 DCHECK(child); | 50 DCHECK(child); |
| 49 DCHECK_EQ(child->parent_, this); | 51 DCHECK_EQ(child->parent_, this); |
| 50 | 52 |
| 51 std::list<ViewAndroid*>::iterator it = | 53 std::list<ViewAndroid*>::iterator it = |
| 52 std::find(children_.begin(), children_.end(), child); | 54 std::find(children_.begin(), children_.end(), child); |
| 53 DCHECK(it != children_.end()); | 55 DCHECK(it != children_.end()); |
| 54 children_.erase(it); | 56 children_.erase(it); |
| 55 child->parent_ = nullptr; | 57 child->parent_ = nullptr; |
| 56 } | 58 } |
| 57 | 59 |
| 58 WindowAndroid* ViewAndroid::GetWindowAndroid() const { | 60 WindowAndroid* ViewAndroid::GetWindowAndroid() const { |
| 59 if (window_) | |
| 60 return window_; | |
| 61 | |
| 62 return parent_ ? parent_->GetWindowAndroid() : nullptr; | 61 return parent_ ? parent_->GetWindowAndroid() : nullptr; |
| 63 } | 62 } |
| 64 | 63 |
| 65 void ViewAndroid::SetWindowAndroid(WindowAndroid* root_window) { | |
| 66 window_ = root_window; | |
| 67 DCHECK(parent_ == nullptr) << "Children shouldn't have a root window"; | |
| 68 } | |
| 69 | |
| 70 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() | 64 const JavaRef<jobject>& ViewAndroid::GetViewAndroidDelegate() |
| 71 const { | 65 const { |
| 72 if (!delegate_.is_null()) | 66 if (!delegate_.is_null()) |
| 73 return delegate_; | 67 return delegate_; |
| 74 | 68 |
| 75 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; | 69 return parent_ ? parent_->GetViewAndroidDelegate() : delegate_; |
| 76 } | 70 } |
| 77 | 71 |
| 78 cc::Layer* ViewAndroid::GetLayer() const { | 72 cc::Layer* ViewAndroid::GetLayer() const { |
| 79 return layer_.get(); | 73 return layer_.get(); |
| 80 } | 74 } |
| 81 | 75 |
| 82 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { | 76 void ViewAndroid::SetLayer(scoped_refptr<cc::Layer> layer) { |
| 83 layer_ = layer; | 77 layer_ = layer; |
| 84 } | 78 } |
| 85 | 79 |
| 86 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, | 80 void ViewAndroid::StartDragAndDrop(const JavaRef<jstring>& jtext, |
| 87 const JavaRef<jobject>& jimage) { | 81 const JavaRef<jobject>& jimage) { |
| 88 WindowAndroid* window_android = GetWindowAndroid(); | 82 WindowAndroid* window_android = GetWindowAndroid(); |
| 89 if (!window_android) | 83 if (!window_android) |
| 90 return; | 84 return; |
| 91 | 85 |
| 92 window_android->StartDragAndDrop(GetViewAndroidDelegate(), jtext, jimage); | 86 window_android->StartDragAndDrop(GetViewAndroidDelegate(), jtext, jimage); |
| 93 } | 87 } |
| 94 | 88 |
| 95 } // namespace ui | 89 } // namespace ui |
| OLD | NEW |