ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / webui_generator / web_ui_view.h
blob9fd26cf056e495c67fbfddec1f718405ccfaae60
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_WEB_UI_VIEW_H_
6 #define CHROME_BROWSER_UI_WEBUI_WUG_WEB_UI_VIEW_H_
8 #include <string>
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"
22 namespace base {
23 class DictionaryValue;
26 namespace content {
27 class WebUI;
30 namespace login {
31 class LocalizedValuesBuilder;
34 namespace webui_generator {
36 using Context = login::ScreenContext;
38 /**
39 * View implementation for the WebUI. Represents a single HTML element derieved
40 * from <web-ui-view>.
42 class WUG_EXPORT WebUIView : public View {
43 public:
44 WebUIView(content::WebUI* web_ui, const std::string& id);
45 ~WebUIView() override;
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);
52 // Overridden from View:
53 void Init() override;
55 protected:
56 using ResourcesMap =
57 base::hash_map<std::string, scoped_refptr<base::RefCountedMemory>>;
59 content::WebUI* web_ui() { return web_ui_; }
61 template <typename T>
62 void AddCallback(const std::string& name, void (T::*method)()) {
63 base::Callback<void()> callback =
64 base::Bind(method, base::Unretained(static_cast<T*>(this)));
65 web_ui_->RegisterMessageCallback(
66 name, base::Bind(&::login::CallbackWrapper0, callback));
69 template <typename T, typename A1>
70 void AddCallback(const std::string& name, void (T::*method)(A1 arg1)) {
71 base::Callback<void(A1)> callback =
72 base::Bind(method, base::Unretained(static_cast<T*>(this)));
73 web_ui_->RegisterMessageCallback(
74 name, base::Bind(&::login::CallbackWrapper1<A1>, callback));
77 // Overridden from View:
78 void OnReady() override;
79 void OnContextChanged(const base::DictionaryValue& diff) override;
81 virtual void AddLocalizedValues(::login::LocalizedValuesBuilder* builder) = 0;
82 virtual void AddResources(ResourcesMap* resources_map) = 0;
84 private:
85 // Internal implementation of SetUpDataSource.
86 void SetUpDataSourceInternal(content::WebUIDataSource* data_source,
87 ResourcesMap* resources_map);
89 // Called when corresponding <web-ui-view> is ready.
90 void HandleHTMLReady();
92 // Called when context is changed on JS side.
93 void HandleContextChanged(const base::DictionaryValue* diff);
95 // Called when both |this| and corresponding <web-ui-view> are ready.
96 void Bind();
98 // Request filter for data source. Filter is needed to answer with a generated
99 // HTML and JS code which is not kept in resources.
100 bool HandleDataRequest(
101 const ResourcesMap* resources,
102 const std::string& path,
103 const content::WebUIDataSource::GotDataCallback& got_data_callback);
105 private:
106 content::WebUI* web_ui_;
107 bool html_ready_;
108 bool view_bound_;
109 scoped_ptr<Context> pending_context_changes_;
111 DISALLOW_COPY_AND_ASSIGN(WebUIView);
114 } // namespace webui_generator
116 #endif // CHROME_BROWSER_UI_WEBUI_WUG_WEB_UI_VIEW_H_