Chromium Code Reviews| Index: components/wug/web_ui_view.cc |
| diff --git a/components/wug/web_ui_view.cc b/components/wug/web_ui_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4502180a23b38e46a3d0ae446dfdb7a5b8cd77fb |
| --- /dev/null |
| +++ b/components/wug/web_ui_view.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/wug/web_ui_view.h" |
| + |
| +#include "base/bind_helpers.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "components/login/localized_values_builder.h" |
| +#include "components/wug/data_source_util.h" |
| +#include "components/wug/view_model.h" |
| + |
| +namespace { |
| + |
| +const char kEventCallback[] = "eventFired"; |
| +const char kContextChangedCallback[] = "contextChanged"; |
| +const char kReadyCallback[] = "ready"; |
| + |
| +const char kMethodContextChanged[] = "contextChanged"; |
|
Nikita (slow)
2015/02/24 15:22:43
nit: Should this be the same as kContextChangedCal
dzhioev (left Google)
2015/02/26 14:01:38
No, it shouldn't
|
| + |
| +} // namespace |
| + |
| +namespace wug { |
| + |
| +WebUIView::WebUIView(content::WebUI* web_ui, const std::string& id) |
| + : View(id), web_ui_(web_ui), html_ready_(false), view_bound_(false) { |
| +} |
| + |
| +WebUIView::~WebUIView() { |
| +} |
| + |
| +void WebUIView::Init() { |
| + View::Init(); |
| + AddCallback(path() + "$" + kEventCallback, &WebUIView::HandleEvent); |
| + AddCallback(path() + "$" + kContextChangedCallback, |
| + &WebUIView::HandleContextChanged); |
| + AddCallback(path() + "$" + kReadyCallback, &WebUIView::HandleHTMLReady); |
| +} |
| + |
| +void WebUIView::SetUpDataSource(content::WebUIDataSource* data_source) { |
| + DCHECK(IsRootView()); |
| + wug::SetUpDataSource(data_source); |
| + ResourcesMap* resources_map = new ResourcesMap(); |
| + SetUpDataSourceInternal(data_source, resources_map); |
| + data_source->SetRequestFilter(base::Bind(&WebUIView::HandleDataRequest, |
| + base::Unretained(this), |
| + base::Owned(resources_map))); |
| +} |
| + |
| +void WebUIView::OnReady() { |
| + View::OnReady(); |
| + if (html_ready_) |
| + Bind(); |
| +} |
| + |
| +void WebUIView::OnContextChanged(const base::DictionaryValue& diff) { |
| + if (view_bound_) { |
|
Nikita (slow)
2015/02/24 15:22:43
nit: drop {}
dzhioev (left Google)
2015/02/26 14:01:39
Done.
|
| + web_ui_->CallJavascriptFunction(path() + "$" + kMethodContextChanged, diff); |
| + } |
| + if (!pending_context_changes_) |
| + pending_context_changes_.reset(new Context()); |
| + pending_context_changes_->ApplyChanges(diff, nullptr); |
|
Nikita (slow)
2015/02/24 15:22:44
nit: insert empty line
dzhioev (left Google)
2015/02/26 14:01:38
Done.
|
| +} |
| + |
| +void WebUIView::SetUpDataSourceInternal(content::WebUIDataSource* data_source, |
| + ResourcesMap* resources_map) { |
| + base::DictionaryValue strings; |
| + auto builder = make_scoped_ptr( |
| + new ::login::LocalizedValuesBuilder(GetType() + "$", &strings)); |
| + AddLocalizedValues(builder.get()); |
| + data_source->AddLocalizedStrings(strings); |
| + AddResources(resources_map); |
| + for (auto& id_child : children()) { |
| + static_cast<WebUIView*>(id_child.second) |
| + ->SetUpDataSourceInternal(data_source, resources_map); |
| + } |
| +} |
| + |
| +bool WebUIView::HandleDataRequest( |
| + const ResourcesMap* resources, |
| + const std::string& path, |
| + const content::WebUIDataSource::GotDataCallback& got_data_callback) { |
| + auto it = resources->find(path); |
| + if (it == resources->end()) { |
|
Nikita (slow)
2015/02/24 15:22:43
nit: drop {}
dzhioev (left Google)
2015/02/26 14:01:39
Done.
|
| + return false; |
| + } |
| + got_data_callback.Run(it->second.get()); |
|
Nikita (slow)
2015/02/24 15:22:43
nit: insert empty line
dzhioev (left Google)
2015/02/26 14:01:38
Done.
|
| + return true; |
| +} |
| + |
| +void WebUIView::HandleHTMLReady() { |
| + html_ready_ = true; |
| + if (ready()) |
| + Bind(); |
| +} |
| + |
| +void WebUIView::HandleContextChanged(const base::DictionaryValue* diff) { |
| + UpdateContext(*diff); |
| +} |
| + |
| +void WebUIView::Bind() { |
| + view_bound_ = true; |
| + if (pending_context_changes_) { |
|
Nikita (slow)
2015/02/24 15:22:43
nit: drop {}
dzhioev (left Google)
2015/02/26 14:01:39
Done.
|
| + OnContextChanged(pending_context_changes_->storage()); |
| + } |
| + GetViewModel()->OnViewBound(); |
|
Nikita (slow)
2015/02/24 15:22:43
nit: insert empty line
dzhioev (left Google)
2015/02/26 14:01:39
Done.
|
| +} |
| + |
| +} // namespace wug |