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/wug/export.h" |
| 18 #include "components/wug/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 wug { |
| 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> |
| 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)); |
| 67 } |
| 68 |
| 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)); |
| 75 } |
| 76 |
| 77 // Overridden from View: |
| 78 void OnReady() override; |
| 79 void OnContextChanged(const base::DictionaryValue& diff) override; |
| 80 |
| 81 virtual void AddLocalizedValues(::login::LocalizedValuesBuilder* builder) = 0; |
| 82 virtual void AddResources(ResourcesMap* resources_map) = 0; |
| 83 |
| 84 private: |
| 85 // Internal implementation of SetUpDataSource. |
| 86 void SetUpDataSourceInternal(content::WebUIDataSource* data_source, |
| 87 ResourcesMap* resources_map); |
| 88 |
| 89 // Called when corresponding <web-ui-view> is ready. |
| 90 void HandleHTMLReady(); |
| 91 |
| 92 // Called when context is changed on JS side. |
| 93 void HandleContextChanged(const base::DictionaryValue* diff); |
| 94 |
| 95 // Called when both |this| and corresponding <web-ui-view> are ready. |
| 96 void Bind(); |
| 97 |
| 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); |
| 104 |
| 105 private: |
| 106 content::WebUI* web_ui_; |
| 107 bool html_ready_; |
| 108 bool view_bound_; |
| 109 scoped_ptr<Context> pending_context_changes_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(WebUIView); |
| 112 }; |
| 113 |
| 114 } // namespace wug |
| 115 |
| 116 #endif // CHROME_BROWSER_UI_WEBUI_WUG_WEB_UI_VIEW_H_ |
OLD | NEW |