Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / webui_generator / web_ui_view.cc
blob54b8668eaae398f16fde39153c41f95f4bccbc2d
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 #include "components/webui_generator/web_ui_view.h"
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"
13 namespace {
15 const char kEventCallback[] = "eventFired";
16 const char kContextChangedCallback[] = "contextChanged";
17 const char kReadyCallback[] = "ready";
19 const char kMethodContextChanged[] = "contextChanged";
21 } // namespace
23 namespace webui_generator {
25 WebUIView::WebUIView(content::WebUI* web_ui, const std::string& id)
26 : View(id), web_ui_(web_ui), html_ready_(false), view_bound_(false) {
29 WebUIView::~WebUIView() {
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);
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)));
50 void WebUIView::OnReady() {
51 View::OnReady();
52 if (html_ready_)
53 Bind();
56 void WebUIView::OnContextChanged(const base::DictionaryValue& diff) {
57 if (view_bound_)
58 web_ui_->CallJavascriptFunction(path() + "$" + kMethodContextChanged, diff);
60 if (!pending_context_changes_)
61 pending_context_changes_.reset(new Context());
63 pending_context_changes_->ApplyChanges(diff, nullptr);
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);
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);
86 if (it == resources->end())
87 return false;
89 got_data_callback.Run(it->second.get());
90 return true;
93 void WebUIView::HandleHTMLReady() {
94 html_ready_ = true;
95 if (ready())
96 Bind();
99 void WebUIView::HandleContextChanged(const base::DictionaryValue* diff) {
100 UpdateContext(*diff);
103 void WebUIView::Bind() {
104 view_bound_ = true;
105 if (pending_context_changes_)
106 OnContextChanged(pending_context_changes_->storage());
108 GetViewModel()->OnViewBound();
111 } // namespace webui_generator