| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/services/public/cpp/view_manager/view.h" | 5 #include "mojo/services/public/cpp/view_manager/view.h" |
| 6 | 6 |
| 7 #include "mojo/public/cpp/application/service_provider_impl.h" | 7 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 8 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" | 8 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" |
| 9 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | 9 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" |
| 10 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 10 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 if (visible_ == value) | 218 if (visible_ == value) |
| 219 return; | 219 return; |
| 220 | 220 |
| 221 if (manager_) | 221 if (manager_) |
| 222 static_cast<ViewManagerClientImpl*>(manager_)->SetVisible(id_, value); | 222 static_cast<ViewManagerClientImpl*>(manager_)->SetVisible(id_, value); |
| 223 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanging(this)); | 223 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanging(this)); |
| 224 visible_ = value; | 224 visible_ = value; |
| 225 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanged(this)); | 225 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanged(this)); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void View::SetProperty(const std::string& name, | 228 void View::SetSharedProperty(const std::string& name, |
| 229 const std::vector<uint8_t>* value) { | 229 const std::vector<uint8_t>* value) { |
| 230 std::vector<uint8_t> old_value; | 230 std::vector<uint8_t> old_value; |
| 231 std::vector<uint8_t>* old_value_ptr = nullptr; | 231 std::vector<uint8_t>* old_value_ptr = nullptr; |
| 232 auto it = properties_.find(name); | 232 auto it = properties_.find(name); |
| 233 if (it != properties_.end()) { | 233 if (it != properties_.end()) { |
| 234 old_value = it->second; | 234 old_value = it->second; |
| 235 old_value_ptr = &old_value; | 235 old_value_ptr = &old_value; |
| 236 | 236 |
| 237 if (value && old_value == *value) | 237 if (value && old_value == *value) |
| 238 return; | 238 return; |
| 239 } else if (!value) { | 239 } else if (!value) { |
| 240 // This property isn't set in |properties_| and |value| is NULL, so there's | 240 // This property isn't set in |properties_| and |value| is NULL, so there's |
| 241 // no change. | 241 // no change. |
| 242 return; | 242 return; |
| 243 } | 243 } |
| 244 | 244 |
| 245 if (value) { | 245 if (value) { |
| 246 properties_[name] = *value; | 246 properties_[name] = *value; |
| 247 } else if (it != properties_.end()) { | 247 } else if (it != properties_.end()) { |
| 248 properties_.erase(it); | 248 properties_.erase(it); |
| 249 } | 249 } |
| 250 | 250 |
| 251 FOR_EACH_OBSERVER(ViewObserver, observers_, | 251 FOR_EACH_OBSERVER( |
| 252 OnViewPropertyChanged(this, name, old_value_ptr, value)); | 252 ViewObserver, observers_, |
| 253 OnViewSharedPropertyChanged(this, name, old_value_ptr, value)); |
| 253 } | 254 } |
| 254 | 255 |
| 255 bool View::IsDrawn() const { | 256 bool View::IsDrawn() const { |
| 256 if (!visible_) | 257 if (!visible_) |
| 257 return false; | 258 return false; |
| 258 return parent_ ? parent_->IsDrawn() : drawn_; | 259 return parent_ ? parent_->IsDrawn() : drawn_; |
| 259 } | 260 } |
| 260 | 261 |
| 261 void View::AddObserver(ViewObserver* observer) { | 262 void View::AddObserver(ViewObserver* observer) { |
| 262 observers_.AddObserver(observer); | 263 observers_.AddObserver(observer); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 380 } |
| 380 | 381 |
| 381 View::~View() { | 382 View::~View() { |
| 382 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroying(this)); | 383 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroying(this)); |
| 383 if (parent_) | 384 if (parent_) |
| 384 parent_->LocalRemoveChild(this); | 385 parent_->LocalRemoveChild(this); |
| 385 // TODO(beng): It'd be better to do this via a destruction observer in the | 386 // TODO(beng): It'd be better to do this via a destruction observer in the |
| 386 // ViewManagerClientImpl. | 387 // ViewManagerClientImpl. |
| 387 if (manager_) | 388 if (manager_) |
| 388 static_cast<ViewManagerClientImpl*>(manager_)->RemoveView(id_); | 389 static_cast<ViewManagerClientImpl*>(manager_)->RemoveView(id_); |
| 390 |
| 391 // Clear properties. |
| 392 for (auto& pair : prop_map_) { |
| 393 if (pair.second.deallocator) |
| 394 (*pair.second.deallocator)(pair.second.value); |
| 395 } |
| 396 prop_map_.clear(); |
| 397 |
| 389 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroyed(this)); | 398 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroyed(this)); |
| 390 } | 399 } |
| 391 | 400 |
| 392 //////////////////////////////////////////////////////////////////////////////// | 401 //////////////////////////////////////////////////////////////////////////////// |
| 393 // View, private: | 402 // View, private: |
| 394 | 403 |
| 395 View::View(ViewManager* manager) | 404 View::View(ViewManager* manager) |
| 396 : manager_(manager), | 405 : manager_(manager), |
| 397 id_(static_cast<ViewManagerClientImpl*>(manager_)->CreateView()), | 406 id_(static_cast<ViewManagerClientImpl*>(manager_)->CreateView()), |
| 398 parent_(NULL), | 407 parent_(NULL), |
| 399 visible_(true), | 408 visible_(true), |
| 400 drawn_(false) { | 409 drawn_(false) { |
| 401 } | 410 } |
| 402 | 411 |
| 412 int64 View::SetLocalPropertyInternal(const void* key, |
| 413 const char* name, |
| 414 PropertyDeallocator deallocator, |
| 415 int64 value, |
| 416 int64 default_value) { |
| 417 int64 old = GetLocalPropertyInternal(key, default_value); |
| 418 if (value == default_value) { |
| 419 prop_map_.erase(key); |
| 420 } else { |
| 421 Value prop_value; |
| 422 prop_value.name = name; |
| 423 prop_value.value = value; |
| 424 prop_value.deallocator = deallocator; |
| 425 prop_map_[key] = prop_value; |
| 426 } |
| 427 FOR_EACH_OBSERVER(ViewObserver, observers_, |
| 428 OnViewLocalPropertyChanged(this, key, old)); |
| 429 return old; |
| 430 } |
| 431 |
| 432 int64 View::GetLocalPropertyInternal(const void* key, |
| 433 int64 default_value) const { |
| 434 std::map<const void*, Value>::const_iterator iter = prop_map_.find(key); |
| 435 if (iter == prop_map_.end()) |
| 436 return default_value; |
| 437 return iter->second.value; |
| 438 } |
| 439 |
| 403 void View::LocalDestroy() { | 440 void View::LocalDestroy() { |
| 404 delete this; | 441 delete this; |
| 405 } | 442 } |
| 406 | 443 |
| 407 void View::LocalAddChild(View* child) { | 444 void View::LocalAddChild(View* child) { |
| 408 ScopedTreeNotifier notifier(child, child->parent(), this); | 445 ScopedTreeNotifier notifier(child, child->parent(), this); |
| 409 if (child->parent()) | 446 if (child->parent()) |
| 410 RemoveChildImpl(child, &child->parent_->children_); | 447 RemoveChildImpl(child, &child->parent_->children_); |
| 411 children_.push_back(child); | 448 children_.push_back(child); |
| 412 child->parent_ = this; | 449 child->parent_ = this; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 441 if (IsDrawn() == value) { | 478 if (IsDrawn() == value) { |
| 442 drawn_ = value; | 479 drawn_ = value; |
| 443 return; | 480 return; |
| 444 } | 481 } |
| 445 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanging(this)); | 482 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanging(this)); |
| 446 drawn_ = value; | 483 drawn_ = value; |
| 447 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanged(this)); | 484 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanged(this)); |
| 448 } | 485 } |
| 449 | 486 |
| 450 } // namespace mojo | 487 } // namespace mojo |
| OLD | NEW |