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/wug/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/wug/data_source_util.h" | |
11 #include "components/wug/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"; | |
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
| |
20 | |
21 } // namespace | |
22 | |
23 namespace wug { | |
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 wug::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_) { | |
Nikita (slow)
2015/02/24 15:22:43
nit: drop {}
dzhioev (left Google)
2015/02/26 14:01:39
Done.
| |
58 web_ui_->CallJavascriptFunction(path() + "$" + kMethodContextChanged, diff); | |
59 } | |
60 if (!pending_context_changes_) | |
61 pending_context_changes_.reset(new Context()); | |
62 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.
| |
63 } | |
64 | |
65 void WebUIView::SetUpDataSourceInternal(content::WebUIDataSource* data_source, | |
66 ResourcesMap* resources_map) { | |
67 base::DictionaryValue strings; | |
68 auto builder = make_scoped_ptr( | |
69 new ::login::LocalizedValuesBuilder(GetType() + "$", &strings)); | |
70 AddLocalizedValues(builder.get()); | |
71 data_source->AddLocalizedStrings(strings); | |
72 AddResources(resources_map); | |
73 for (auto& id_child : children()) { | |
74 static_cast<WebUIView*>(id_child.second) | |
75 ->SetUpDataSourceInternal(data_source, resources_map); | |
76 } | |
77 } | |
78 | |
79 bool WebUIView::HandleDataRequest( | |
80 const ResourcesMap* resources, | |
81 const std::string& path, | |
82 const content::WebUIDataSource::GotDataCallback& got_data_callback) { | |
83 auto it = resources->find(path); | |
84 if (it == resources->end()) { | |
Nikita (slow)
2015/02/24 15:22:43
nit: drop {}
dzhioev (left Google)
2015/02/26 14:01:39
Done.
| |
85 return false; | |
86 } | |
87 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.
| |
88 return true; | |
89 } | |
90 | |
91 void WebUIView::HandleHTMLReady() { | |
92 html_ready_ = true; | |
93 if (ready()) | |
94 Bind(); | |
95 } | |
96 | |
97 void WebUIView::HandleContextChanged(const base::DictionaryValue* diff) { | |
98 UpdateContext(*diff); | |
99 } | |
100 | |
101 void WebUIView::Bind() { | |
102 view_bound_ = true; | |
103 if (pending_context_changes_) { | |
Nikita (slow)
2015/02/24 15:22:43
nit: drop {}
dzhioev (left Google)
2015/02/26 14:01:39
Done.
| |
104 OnContextChanged(pending_context_changes_->storage()); | |
105 } | |
106 GetViewModel()->OnViewBound(); | |
Nikita (slow)
2015/02/24 15:22:43
nit: insert empty line
dzhioev (left Google)
2015/02/26 14:01:39
Done.
| |
107 } | |
108 | |
109 } // namespace wug | |
OLD | NEW |