OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_ |
| 7 |
| 8 #include "base/containers/scoped_ptr_hash_map.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "components/webui_generator/export.h" |
| 12 |
| 13 namespace base { |
| 14 class DictionaryValue; |
| 15 } |
| 16 |
| 17 namespace webui_generator { |
| 18 |
| 19 class ViewModel; |
| 20 |
| 21 /** |
| 22 * Base block of Web UI (in terms of Web UI Generator). Every View instance is |
| 23 * associated with single ViewModel instance which provides a context for the |
| 24 * view. Views are hierarchical. Every child of view has an unique id. |
| 25 */ |
| 26 class WUG_EXPORT View { |
| 27 public: |
| 28 explicit View(const std::string& id); |
| 29 virtual ~View(); |
| 30 |
| 31 // Should be called before using the view. This method creates children (using |
| 32 // CreateAndAddChildren() method) and a view-model (using CreateViewModel() |
| 33 // method). Then it recursively calls Init() for the children. When |
| 34 // all the children and the view-model are ready, OnReady() method is called. |
| 35 // Overridden methods should call the base implementation. |
| 36 virtual void Init(); |
| 37 |
| 38 // Root view is a view without parent. |
| 39 // Root view should have id equal to "WUG_ROOT". |
| 40 bool IsRootView() const; |
| 41 |
| 42 ViewModel* GetViewModel() const; |
| 43 |
| 44 // Called by view-model when it is ready. |
| 45 void OnViewModelReady(); |
| 46 |
| 47 const std::string& id() const { return id_; } |
| 48 |
| 49 // Every view has an unique path in a view hierarchy which is: |
| 50 // a) |id| for the root view; |
| 51 // b) concatenation of parent's path, $-sign and |id| for not-root views. |
| 52 const std::string& path() const { return path_; } |
| 53 |
| 54 // Returns a child with a given |id|. Returns |nullptr| if such child doesn't |
| 55 // exist. |
| 56 View* GetChild(const std::string& id) const; |
| 57 |
| 58 // Called by view-model when it changes the context. |
| 59 virtual void OnContextChanged(const base::DictionaryValue& diff) = 0; |
| 60 |
| 61 protected: |
| 62 // Called when view is ready, which means view-model and all children are |
| 63 // ready. Overridden methods should call the base implementation. |
| 64 virtual void OnReady(); |
| 65 |
| 66 // Forwards context changes stored in |diff| to view-model. |
| 67 void UpdateContext(const base::DictionaryValue& diff); |
| 68 |
| 69 // Forwards |event| to view-model. |
| 70 void HandleEvent(const std::string& event); |
| 71 |
| 72 bool ready() const { return ready_; } |
| 73 |
| 74 base::ScopedPtrHashMap<std::string, View>& children() { return children_; } |
| 75 |
| 76 // Adds |child| to the list of children of |this|. Can be called only from |
| 77 // CreateAndAddChildren() override. |
| 78 void AddChild(View* child); |
| 79 |
| 80 virtual std::string GetType() = 0; |
| 81 |
| 82 // Implementation should create an instance of view-model for this view. |
| 83 virtual ViewModel* CreateViewModel() = 0; |
| 84 |
| 85 // Implementation should create and add children to |this| view. This method |
| 86 // is an only place where it is allowed to add children. |
| 87 virtual void CreateAndAddChildren() = 0; |
| 88 |
| 89 private: |
| 90 // Called by |child| view, when it is ready. |
| 91 void OnChildReady(View* child); |
| 92 |
| 93 // Called when all the children created by CreatedAndAddChildren() are ready. |
| 94 void OnChildrenReady(); |
| 95 |
| 96 void set_parent(View* parent) { parent_ = parent; } |
| 97 |
| 98 View* parent_; |
| 99 std::string id_; |
| 100 std::string path_; |
| 101 |
| 102 // Number of child views that are ready. |
| 103 int ready_children_; |
| 104 |
| 105 bool view_model_ready_; |
| 106 bool ready_; |
| 107 base::ScopedPtrHashMap<std::string, View> children_; |
| 108 scoped_ptr<ViewModel> view_model_; |
| 109 base::WeakPtrFactory<View> weak_factory_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(View); |
| 112 }; |
| 113 |
| 114 } // namespace webui_generator |
| 115 |
| 116 #endif // CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_ |
OLD | NEW |