| 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, scoped_ptr<View>>& children() { | |
| 75 return children_; | |
| 76 } | |
| 77 | |
| 78 // Adds |child| to the list of children of |this|. Can be called only from | |
| 79 // CreateAndAddChildren() override. | |
| 80 void AddChild(View* child); | |
| 81 | |
| 82 virtual std::string GetType() = 0; | |
| 83 | |
| 84 // Implementation should create an instance of view-model for this view. | |
| 85 virtual ViewModel* CreateViewModel() = 0; | |
| 86 | |
| 87 // Implementation should create and add children to |this| view. This method | |
| 88 // is an only place where it is allowed to add children. | |
| 89 virtual void CreateAndAddChildren() = 0; | |
| 90 | |
| 91 private: | |
| 92 // Called by |child| view, when it is ready. | |
| 93 void OnChildReady(View* child); | |
| 94 | |
| 95 // Called when all the children created by CreatedAndAddChildren() are ready. | |
| 96 void OnChildrenReady(); | |
| 97 | |
| 98 void set_parent(View* parent) { parent_ = parent; } | |
| 99 | |
| 100 View* parent_; | |
| 101 std::string id_; | |
| 102 std::string path_; | |
| 103 | |
| 104 // Number of child views that are ready. | |
| 105 int ready_children_; | |
| 106 | |
| 107 bool view_model_ready_; | |
| 108 bool ready_; | |
| 109 base::ScopedPtrHashMap<std::string, scoped_ptr<View>> children_; | |
| 110 scoped_ptr<ViewModel> view_model_; | |
| 111 base::WeakPtrFactory<View> weak_factory_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(View); | |
| 114 }; | |
| 115 | |
| 116 } // namespace webui_generator | |
| 117 | |
| 118 #endif // CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_ | |
| OLD | NEW |