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 #include "components/webui_generator/web_ui_view.h" |
| 6 |
| 7 #include "base/bind_helpers.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "components/login/localized_values_builder.h" |
| 10 #include "components/webui_generator/data_source_util.h" |
| 11 #include "components/webui_generator/view_model.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const char kEventCallback[] = "eventFired"; |
| 16 const char kContextChangedCallback[] = "contextChanged"; |
| 17 const char kReadyCallback[] = "ready"; |
| 18 |
| 19 const char kMethodContextChanged[] = "contextChanged"; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace webui_generator { |
| 24 |
| 25 WebUIView::WebUIView(content::WebUI* web_ui, const std::string& id) |
| 26 : View(id), web_ui_(web_ui), html_ready_(false), view_bound_(false) { |
| 27 } |
| 28 |
| 29 WebUIView::~WebUIView() { |
| 30 } |
| 31 |
| 32 void WebUIView::Init() { |
| 33 View::Init(); |
| 34 AddCallback(path() + "$" + kEventCallback, &WebUIView::HandleEvent); |
| 35 AddCallback(path() + "$" + kContextChangedCallback, |
| 36 &WebUIView::HandleContextChanged); |
| 37 AddCallback(path() + "$" + kReadyCallback, &WebUIView::HandleHTMLReady); |
| 38 } |
| 39 |
| 40 void WebUIView::SetUpDataSource(content::WebUIDataSource* data_source) { |
| 41 DCHECK(IsRootView()); |
| 42 webui_generator::SetUpDataSource(data_source); |
| 43 ResourcesMap* resources_map = new ResourcesMap(); |
| 44 SetUpDataSourceInternal(data_source, resources_map); |
| 45 data_source->SetRequestFilter(base::Bind(&WebUIView::HandleDataRequest, |
| 46 base::Unretained(this), |
| 47 base::Owned(resources_map))); |
| 48 } |
| 49 |
| 50 void WebUIView::OnReady() { |
| 51 View::OnReady(); |
| 52 if (html_ready_) |
| 53 Bind(); |
| 54 } |
| 55 |
| 56 void WebUIView::OnContextChanged(const base::DictionaryValue& diff) { |
| 57 if (view_bound_) |
| 58 web_ui_->CallJavascriptFunction(path() + "$" + kMethodContextChanged, diff); |
| 59 |
| 60 if (!pending_context_changes_) |
| 61 pending_context_changes_.reset(new Context()); |
| 62 |
| 63 pending_context_changes_->ApplyChanges(diff, nullptr); |
| 64 } |
| 65 |
| 66 void WebUIView::SetUpDataSourceInternal(content::WebUIDataSource* data_source, |
| 67 ResourcesMap* resources_map) { |
| 68 base::DictionaryValue strings; |
| 69 auto builder = make_scoped_ptr( |
| 70 new ::login::LocalizedValuesBuilder(GetType() + "$", &strings)); |
| 71 AddLocalizedValues(builder.get()); |
| 72 data_source->AddLocalizedStrings(strings); |
| 73 AddResources(resources_map); |
| 74 for (auto& id_child : children()) { |
| 75 static_cast<WebUIView*>(id_child.second) |
| 76 ->SetUpDataSourceInternal(data_source, resources_map); |
| 77 } |
| 78 } |
| 79 |
| 80 bool WebUIView::HandleDataRequest( |
| 81 const ResourcesMap* resources, |
| 82 const std::string& path, |
| 83 const content::WebUIDataSource::GotDataCallback& got_data_callback) { |
| 84 auto it = resources->find(path); |
| 85 |
| 86 if (it == resources->end()) |
| 87 return false; |
| 88 |
| 89 got_data_callback.Run(it->second.get()); |
| 90 return true; |
| 91 } |
| 92 |
| 93 void WebUIView::HandleHTMLReady() { |
| 94 html_ready_ = true; |
| 95 if (ready()) |
| 96 Bind(); |
| 97 } |
| 98 |
| 99 void WebUIView::HandleContextChanged(const base::DictionaryValue* diff) { |
| 100 UpdateContext(*diff); |
| 101 } |
| 102 |
| 103 void WebUIView::Bind() { |
| 104 view_bound_ = true; |
| 105 if (pending_context_changes_) |
| 106 OnContextChanged(pending_context_changes_->storage()); |
| 107 |
| 108 GetViewModel()->OnViewBound(); |
| 109 } |
| 110 |
| 111 } // namespace webui_generator |
OLD | NEW |