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_WEB_UI_VIEW_H_ | |
6 #define CHROME_BROWSER_UI_WEBUI_WUG_WEB_UI_VIEW_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/containers/hash_tables.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted_memory.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "components/login/base_screen_handler_utils.h" | |
16 #include "components/login/screens/screen_context.h" | |
17 #include "components/webui_generator/export.h" | |
18 #include "components/webui_generator/view.h" | |
19 #include "content/public/browser/web_ui.h" | |
20 #include "content/public/browser/web_ui_data_source.h" | |
21 | |
22 namespace base { | |
23 class DictionaryValue; | |
24 } | |
25 | |
26 namespace content { | |
27 class WebUI; | |
28 } | |
29 | |
30 namespace login { | |
31 class LocalizedValuesBuilder; | |
32 } | |
33 | |
34 namespace webui_generator { | |
35 | |
36 using Context = login::ScreenContext; | |
37 | |
38 /** | |
39 * View implementation for the WebUI. Represents a single HTML element derieved | |
40 * from <web-ui-view>. | |
41 */ | |
42 class WUG_EXPORT WebUIView : public View { | |
43 public: | |
44 WebUIView(content::WebUI* web_ui, const std::string& id); | |
45 ~WebUIView() override; | |
46 | |
47 // Should be called for |data_source|, where this views will be used. | |
48 // Sets up |data_source| for children of |this| as well, that is why this | |
49 // method only shouldn't be called for non-root views. | |
50 void SetUpDataSource(content::WebUIDataSource* data_source); | |
51 | |
52 // Overridden from View: | |
53 void Init() override; | |
54 | |
55 protected: | |
56 using ResourcesMap = | |
57 base::hash_map<std::string, scoped_refptr<base::RefCountedMemory>>; | |
58 | |
59 content::WebUI* web_ui() { return web_ui_; } | |
60 | |
61 template <typename T, typename... Args> | |
62 void AddCallback(const std::string& name, void (T::*method)(Args...)) { | |
63 base::Callback<void(Args...)> callback = | |
64 base::Bind(method, base::Unretained(static_cast<T*>(this))); | |
65 web_ui_->RegisterMessageCallback( | |
66 name, base::Bind(&::login::CallbackWrapper<Args...>, callback)); | |
67 } | |
68 | |
69 // Overridden from View: | |
70 void OnReady() override; | |
71 void OnContextChanged(const base::DictionaryValue& diff) override; | |
72 | |
73 virtual void AddLocalizedValues(::login::LocalizedValuesBuilder* builder) = 0; | |
74 virtual void AddResources(ResourcesMap* resources_map) = 0; | |
75 | |
76 private: | |
77 // Internal implementation of SetUpDataSource. | |
78 void SetUpDataSourceInternal(content::WebUIDataSource* data_source, | |
79 ResourcesMap* resources_map); | |
80 | |
81 // Called when corresponding <web-ui-view> is ready. | |
82 void HandleHTMLReady(); | |
83 | |
84 // Called when context is changed on JS side. | |
85 void HandleContextChanged(const base::DictionaryValue* diff); | |
86 | |
87 // Called when both |this| and corresponding <web-ui-view> are ready. | |
88 void Bind(); | |
89 | |
90 // Request filter for data source. Filter is needed to answer with a generated | |
91 // HTML and JS code which is not kept in resources. | |
92 bool HandleDataRequest( | |
93 const ResourcesMap* resources, | |
94 const std::string& path, | |
95 const content::WebUIDataSource::GotDataCallback& got_data_callback); | |
96 | |
97 private: | |
98 content::WebUI* web_ui_; | |
99 bool html_ready_; | |
100 bool view_bound_; | |
101 scoped_ptr<Context> pending_context_changes_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(WebUIView); | |
104 }; | |
105 | |
106 } // namespace webui_generator | |
107 | |
108 #endif // CHROME_BROWSER_UI_WEBUI_WUG_WEB_UI_VIEW_H_ | |
OLD | NEW |