content: Rename WebKitTestController to BlinkTestController.
[chromium-blink-merge.git] / components / webui_generator / view.h
blob934fa3da7807aac66ae743c228d2828d6f7d32ce
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, scoped_ptr<View>>& children() {
75 return children_;
78 // Adds |child| to the list of children of |this|. Can be called only from
79 // CreateAndAddChildren() override.
80 void AddChild(View* child);
82 virtual std::string GetType() = 0;
84 // Implementation should create an instance of view-model for this view.
85 virtual ViewModel* CreateViewModel() = 0;
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;
91 private:
92 // Called by |child| view, when it is ready.
93 void OnChildReady(View* child);
95 // Called when all the children created by CreatedAndAddChildren() are ready.
96 void OnChildrenReady();
98 void set_parent(View* parent) { parent_ = parent; }
100 View* parent_;
101 std::string id_;
102 std::string path_;
104 // Number of child views that are ready.
105 int ready_children_;
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_;
113 DISALLOW_COPY_AND_ASSIGN(View);
116 } // namespace webui_generator
118 #endif // CHROME_BROWSER_UI_WEBUI_WUG_VIEW_H_