Make castv2 performance test work.
[chromium-blink-merge.git] / components / webui_generator / view.h
blobb43ad743f067275f010bf5c459796a28291bb837
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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_
6 #define CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_
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"
13 namespace base {
14 class DictionaryValue;
17 namespace webui_generator {
19 class ViewModel;
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.
26 class WUG_EXPORT View {
27 public:
28 explicit View(const std::string& id);
29 virtual ~View();
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();
38 // Root view is a view without parent.
39 // Root view should have id equal to "WUG_ROOT".
40 bool IsRootView() const;
42 ViewModel* GetViewModel() const;
44 // Called by view-model when it is ready.
45 void OnViewModelReady();
47 const std::string& id() const { return id_; }
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_; }
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;
58 // Called by view-model when it changes the context.
59 virtual void OnContextChanged(const base::DictionaryValue& diff) = 0;
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();
66 // Forwards context changes stored in |diff| to view-model.
67 void UpdateContext(const base::DictionaryValue& diff);
69 // Forwards |event| to view-model.
70 void HandleEvent(const std::string& event);
72 bool ready() const { return ready_; }
74 base::ScopedPtrHashMap<std::string, View>& children() { return children_; }
76 // Adds |child| to the list of children of |this|. Can be called only from
77 // CreateAndAddChildren() override.
78 void AddChild(View* child);
80 virtual std::string GetType() = 0;
82 // Implementation should create an instance of view-model for this view.
83 virtual ViewModel* CreateViewModel() = 0;
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;
89 private:
90 // Called by |child| view, when it is ready.
91 void OnChildReady(View* child);
93 // Called when all the children created by CreatedAndAddChildren() are ready.
94 void OnChildrenReady();
96 void set_parent(View* parent) { parent_ = parent; }
98 View* parent_;
99 std::string id_;
100 std::string path_;
102 // Number of child views that are ready.
103 int ready_children_;
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_;
111 DISALLOW_COPY_AND_ASSIGN(View);
114 } // namespace webui_generator
116 #endif // CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_