Make castv2 performance test work.
[chromium-blink-merge.git] / components / webui_generator / view_model.h
bloba7021e0e693f11c9ee9d69e111e0242d39b02e76
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_MODEL_H_
6 #define CHROME_BROWSER_UI_WEBUI_WUG_VIEW_MODEL_H_
8 #include <vector>
10 #include "base/macros.h"
11 #include "components/login/screens/screen_context.h"
12 #include "components/webui_generator/export.h"
14 namespace base {
15 class DictionaryValue;
18 namespace webui_generator {
20 using Context = login::ScreenContext;
21 class View;
23 // Base class for view-model. Usually View is responsible for creating and
24 // initializing instances of this class.
25 class WUG_EXPORT ViewModel {
26 public:
27 ViewModel();
28 virtual ~ViewModel();
30 void SetView(View* view);
32 // Called by view to notify that children are ready.
33 void OnChildrenReady();
35 // Called by view when it became bound (meaning that all changes in context
36 // will be reflected in view right after they are made).
37 virtual void OnViewBound() = 0;
39 // Notifies view-model about context changes happended on view side.
40 // View-model corrects its copy of context according to changes in |diff|.
41 void UpdateContext(const base::DictionaryValue& diff);
43 // Notifies view-model about |event| sent from view.
44 virtual void OnEvent(const std::string& event);
46 protected:
47 // Scoped context editor, which automatically commits all pending
48 // context changes on destruction.
49 class ContextEditor {
50 public:
51 using KeyType = ::login::ScreenContext::KeyType;
52 using String16List = ::login::String16List;
53 using StringList = ::login::StringList;
55 explicit ContextEditor(ViewModel& screen);
56 ~ContextEditor();
58 const ContextEditor& SetBoolean(const KeyType& key, bool value) const;
59 const ContextEditor& SetInteger(const KeyType& key, int value) const;
60 const ContextEditor& SetDouble(const KeyType& key, double value) const;
61 const ContextEditor& SetString(const KeyType& key,
62 const std::string& value) const;
63 const ContextEditor& SetString(const KeyType& key,
64 const base::string16& value) const;
65 const ContextEditor& SetStringList(const KeyType& key,
66 const StringList& value) const;
67 const ContextEditor& SetString16List(const KeyType& key,
68 const String16List& value) const;
70 private:
71 ViewModel& view_model_;
72 Context& context_;
75 // This method is called in a time appropriate for initialization. At this
76 // point it is allowed to make changes in the context, but it is not allowed
77 // to access child view-models.
78 virtual void Initialize() = 0;
80 // This method is called when all children are ready.
81 virtual void OnAfterChildrenReady() = 0;
83 // This method is called after context was changed on view side.
84 // |chanaged_keys| contains a list of keys of changed fields.
85 virtual void OnContextChanged(const std::vector<std::string>& changed_keys) {}
87 Context& context() { return context_; }
88 const Context& context() const { return context_; }
90 View* view() const { return view_; }
92 // Returns scoped context editor. The editor or it's copies should not outlive
93 // current BaseScreen instance.
94 ContextEditor GetContextEditor();
96 // Notifies view about changes made in context.
97 void CommitContextChanges();
99 private:
100 Context context_;
101 View* view_;
103 DISALLOW_COPY_AND_ASSIGN(ViewModel);
106 } // namespace webui_generator
108 #endif // CHROME_BROWSER_UI_WEBUI_WUG_VIEW_MODEL_H_