OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // A helper function for using JsTemplate. See jstemplate_builder.h for more | 5 // A helper function for using JsTemplate. See jstemplate_builder.h for more |
6 // info. | 6 // info. |
7 | 7 |
8 #include "chrome/common/jstemplate_builder.h" | 8 #include "chrome/common/jstemplate_builder.h" |
9 | 9 |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "grit/webui_resources.h" | 14 #include "grit/common_resources.h" |
15 #include "ui/base/layout.h" | 15 #include "ui/base/layout.h" |
16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Non-zero when building version 2 templates. See UseVersion2 class. | 20 // Non-zero when building version 2 templates. See UseVersion2 class. |
21 int g_version2 = 0; | 21 int g_version2 = 0; |
22 | 22 |
23 } // namespace | 23 } // namespace |
24 | 24 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 serializer.Serialize(*json); | 85 serializer.Serialize(*json); |
86 output->append(g_version2 ? "loadTimeData.data = " : "var templateData = "); | 86 output->append(g_version2 ? "loadTimeData.data = " : "var templateData = "); |
87 output->append(jstext); | 87 output->append(jstext); |
88 output->append(";"); | 88 output->append(";"); |
89 } | 89 } |
90 | 90 |
91 void AppendJsTemplateSourceHtml(std::string* output) { | 91 void AppendJsTemplateSourceHtml(std::string* output) { |
92 // fetch and cache the pointer of the jstemplate resource source text. | 92 // fetch and cache the pointer of the jstemplate resource source text. |
93 static const base::StringPiece jstemplate_src( | 93 static const base::StringPiece jstemplate_src( |
94 ResourceBundle::GetSharedInstance().GetRawDataResource( | 94 ResourceBundle::GetSharedInstance().GetRawDataResource( |
95 IDR_WEBUI_JSTEMPLATE_JS)); | 95 IDR_JSTEMPLATE_JS)); |
96 | 96 |
97 if (jstemplate_src.empty()) { | 97 if (jstemplate_src.empty()) { |
98 NOTREACHED() << "Unable to get jstemplate src"; | 98 NOTREACHED() << "Unable to get jstemplate src"; |
99 return; | 99 return; |
100 } | 100 } |
101 | 101 |
102 output->append("<script>"); | 102 output->append("<script>"); |
103 output->append(jstemplate_src.data(), jstemplate_src.size()); | 103 output->append(jstemplate_src.data(), jstemplate_src.size()); |
104 output->append("</script>"); | 104 output->append("</script>"); |
105 } | 105 } |
106 | 106 |
107 void AppendJsTemplateProcessHtml(const base::StringPiece& template_id, | 107 void AppendJsTemplateProcessHtml(const base::StringPiece& template_id, |
108 std::string* output) { | 108 std::string* output) { |
109 output->append("<script>"); | 109 output->append("<script>"); |
110 output->append("var tp = document.getElementById('"); | 110 output->append("var tp = document.getElementById('"); |
111 output->append(template_id.data(), template_id.size()); | 111 output->append(template_id.data(), template_id.size()); |
112 output->append("');"); | 112 output->append("');"); |
113 output->append("jstProcess(new JsEvalContext(templateData), tp);"); | 113 output->append("jstProcess(new JsEvalContext(templateData), tp);"); |
114 output->append("</script>"); | 114 output->append("</script>"); |
115 } | 115 } |
116 | 116 |
117 void AppendI18nTemplateSourceHtml(std::string* output) { | 117 void AppendI18nTemplateSourceHtml(std::string* output) { |
118 // fetch and cache the pointer of the jstemplate resource source text. | 118 // fetch and cache the pointer of the jstemplate resource source text. |
119 static const base::StringPiece i18n_template_src( | 119 static const base::StringPiece i18n_template_src( |
120 ResourceBundle::GetSharedInstance().GetRawDataResource( | 120 ResourceBundle::GetSharedInstance().GetRawDataResource( |
121 IDR_WEBUI_I18N_TEMPLATE_JS)); | 121 IDR_I18N_TEMPLATE_JS)); |
122 static const base::StringPiece i18n_template2_src( | 122 static const base::StringPiece i18n_template2_src( |
123 ResourceBundle::GetSharedInstance().GetRawDataResource( | 123 ResourceBundle::GetSharedInstance().GetRawDataResource( |
124 IDR_WEBUI_I18N_TEMPLATE2_JS)); | 124 IDR_I18N_TEMPLATE2_JS)); |
125 const base::StringPiece* template_src = g_version2 ? | 125 const base::StringPiece* template_src = g_version2 ? |
126 &i18n_template2_src : &i18n_template_src; | 126 &i18n_template2_src : &i18n_template_src; |
127 | 127 |
128 if (template_src->empty()) { | 128 if (template_src->empty()) { |
129 NOTREACHED() << "Unable to get i18n template src"; | 129 NOTREACHED() << "Unable to get i18n template src"; |
130 return; | 130 return; |
131 } | 131 } |
132 | 132 |
133 output->append("<script>"); | 133 output->append("<script>"); |
134 output->append(template_src->data(), template_src->size()); | 134 output->append(template_src->data(), template_src->size()); |
135 output->append("</script>"); | 135 output->append("</script>"); |
136 } | 136 } |
137 | 137 |
138 void AppendI18nTemplateProcessHtml(std::string* output) { | 138 void AppendI18nTemplateProcessHtml(std::string* output) { |
139 if (g_version2) | 139 if (g_version2) |
140 return; | 140 return; |
141 | 141 |
142 static const base::StringPiece i18n_process_src( | 142 static const base::StringPiece i18n_process_src( |
143 ResourceBundle::GetSharedInstance().GetRawDataResource( | 143 ResourceBundle::GetSharedInstance().GetRawDataResource( |
144 IDR_WEBUI_I18N_PROCESS_JS)); | 144 IDR_I18N_PROCESS_JS)); |
145 | 145 |
146 if (i18n_process_src.empty()) { | 146 if (i18n_process_src.empty()) { |
147 NOTREACHED() << "Unable to get i18n process src"; | 147 NOTREACHED() << "Unable to get i18n process src"; |
148 return; | 148 return; |
149 } | 149 } |
150 | 150 |
151 output->append("<script>"); | 151 output->append("<script>"); |
152 output->append(i18n_process_src.data(), i18n_process_src.size()); | 152 output->append(i18n_process_src.data(), i18n_process_src.size()); |
153 output->append("</script>"); | 153 output->append("</script>"); |
154 } | 154 } |
155 | 155 |
156 } // namespace jstemplate_builder | 156 } // namespace jstemplate_builder |
OLD | NEW |