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_
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"
23 class DictionaryValue
;
31 class LocalizedValuesBuilder
;
34 namespace webui_generator
{
36 using Context
= login::ScreenContext
;
39 * View implementation for the WebUI. Represents a single HTML element derieved
42 class WUG_EXPORT WebUIView
: public View
{
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:
57 base::hash_map
<std::string
, scoped_refptr
<base::RefCountedMemory
>>;
59 content::WebUI
* web_ui() { return web_ui_
; }
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
));
69 // Overridden from View:
70 void OnReady() override
;
71 void OnContextChanged(const base::DictionaryValue
& diff
) override
;
73 virtual void AddLocalizedValues(::login::LocalizedValuesBuilder
* builder
) = 0;
74 virtual void AddResources(ResourcesMap
* resources_map
) = 0;
77 // Internal implementation of SetUpDataSource.
78 void SetUpDataSourceInternal(content::WebUIDataSource
* data_source
,
79 ResourcesMap
* resources_map
);
81 // Called when corresponding <web-ui-view> is ready.
82 void HandleHTMLReady();
84 // Called when context is changed on JS side.
85 void HandleContextChanged(const base::DictionaryValue
* diff
);
87 // Called when both |this| and corresponding <web-ui-view> are ready.
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
);
98 content::WebUI
* web_ui_
;
101 scoped_ptr
<Context
> pending_context_changes_
;
103 DISALLOW_COPY_AND_ASSIGN(WebUIView
);
106 } // namespace webui_generator
108 #endif // CHROME_BROWSER_UI_WEBUI_WUG_WEB_UI_VIEW_H_