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"
14 class DictionaryValue
;
17 namespace webui_generator
{
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
{
28 explicit View(const std::string
& id
);
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.
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
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;
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;
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
; }
102 // Number of child views that are ready.
105 bool view_model_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_