Index: components/wug/view.cc |
diff --git a/components/wug/view.cc b/components/wug/view.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..85704aa140385483af631e72ccae7b77e16fdcb1 |
--- /dev/null |
+++ b/components/wug/view.cc |
@@ -0,0 +1,102 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/wug/view.h" |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "components/wug/view_model.h" |
+ |
+namespace wug { |
+ |
+View::View(const std::string& id) |
+ : parent_(NULL), |
+ id_(id), |
+ ready_children_(0), |
+ view_model_ready_(false), |
+ ready_(false), |
+ weak_factory_(this) { |
+} |
+ |
+View::~View() { |
+} |
+ |
+void View::Init() { |
+ CreateAndAddChildren(); |
+ |
+ if (!IsRootView()) |
+ path_ = parent_->path() + "$"; |
+ |
+ path_ += id_; |
+ view_model_.reset(CreateViewModel()); |
+ view_model_->SetView(this); |
+ |
+ for (auto& id_child : children_) |
+ id_child.second->Init(); |
+ |
+ if (children_.empty()) |
+ OnChildrenReady(); |
+} |
+ |
+bool View::IsRootView() const { |
+ return !parent_; |
+} |
+ |
+ViewModel* View::GetViewModel() const { |
+ return view_model_.get(); |
+} |
+ |
+void View::OnViewModelReady() { |
+ view_model_ready_ = true; |
+ if (ready_children_ == static_cast<int>(children_.size())) |
+ OnReady(); |
+} |
+ |
+View* View::GetChild(const std::string& id) const { |
+ auto it = children_.find(id); |
+ if (it == children_.end()) |
+ return nullptr; |
+ |
+ return it->second; |
+} |
+ |
+void View::OnReady() { |
+ if (ready_) |
+ return; |
+ |
+ ready_ = true; |
+ if (!IsRootView()) |
+ parent_->OnChildReady(this); |
+} |
+ |
+void View::UpdateContext(const base::DictionaryValue& diff) { |
+ DCHECK(ready_); |
+ view_model_->UpdateContext(diff); |
+} |
+ |
+void View::HandleEvent(const std::string& event) { |
+ DCHECK(ready_); |
+ view_model_->OnEvent(event); |
+} |
+ |
+void View::AddChild(View* child) { |
+ DCHECK(children_.find(child->id()) == children_.end()); |
+ DCHECK(!child->id().empty()); |
+ children_.set(child->id(), make_scoped_ptr(child)); |
+ child->set_parent(this); |
+} |
+ |
+void View::OnChildReady(View* child) { |
+ ++ready_children_; |
+ if (ready_children_ == static_cast<int>(children_.size())) |
+ OnChildrenReady(); |
+} |
+ |
+void View::OnChildrenReady() { |
+ view_model_->OnChildrenReady(); |
+ if (view_model_ready_) |
+ OnReady(); |
+} |
+ |
+} // namespace wug |