Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ui/base/webui/jstemplate_builder.h" | 8 #include "ui/base/webui/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/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "ui/base/layout.h" | 14 #include "ui/base/layout.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/resources/grit/webui_resources.h" | 16 #include "ui/resources/grit/webui_resources.h" |
| 17 | 17 |
| 18 namespace webui { | |
| 19 | |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // Non-zero when building version 2 templates. See UseVersion2 class. | 22 // Appends a script tag with a variable name |templateData| that has the JSON |
| 21 int g_version2 = 0; | 23 // assigned to it. |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace webui { | |
| 26 | |
| 27 UseVersion2::UseVersion2() { | |
| 28 g_version2++; | |
| 29 } | |
| 30 | |
| 31 UseVersion2::~UseVersion2() { | |
| 32 g_version2--; | |
| 33 } | |
| 34 | |
| 35 std::string GetTemplateHtml(const base::StringPiece& html_template, | |
| 36 const base::DictionaryValue* json, | |
| 37 const base::StringPiece& template_id) { | |
| 38 std::string output(html_template.data(), html_template.size()); | |
| 39 AppendJsonHtml(json, &output); | |
| 40 AppendJsTemplateSourceHtml(&output); | |
| 41 AppendJsTemplateProcessHtml(template_id, &output); | |
| 42 return output; | |
| 43 } | |
| 44 | |
| 45 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, | |
| 46 const base::DictionaryValue* json) { | |
| 47 std::string output(html_template.data(), html_template.size()); | |
| 48 AppendJsonHtml(json, &output); | |
| 49 AppendI18nTemplateSourceHtml(&output); | |
| 50 AppendI18nTemplateProcessHtml(&output); | |
| 51 return output; | |
| 52 } | |
| 53 | |
| 54 std::string GetTemplatesHtml(const base::StringPiece& html_template, | |
| 55 const base::DictionaryValue* json, | |
| 56 const base::StringPiece& template_id) { | |
| 57 std::string output(html_template.data(), html_template.size()); | |
| 58 AppendI18nTemplateSourceHtml(&output); | |
| 59 AppendJsTemplateSourceHtml(&output); | |
| 60 AppendJsonHtml(json, &output); | |
| 61 AppendI18nTemplateProcessHtml(&output); | |
| 62 AppendJsTemplateProcessHtml(template_id, &output); | |
| 63 return output; | |
| 64 } | |
| 65 | |
| 66 void AppendJsonHtml(const base::DictionaryValue* json, std::string* output) { | 24 void AppendJsonHtml(const base::DictionaryValue* json, std::string* output) { |
| 67 std::string javascript_string; | 25 std::string javascript_string; |
| 68 AppendJsonJS(json, &javascript_string); | 26 AppendJsonJS(json, &javascript_string); |
| 69 | 27 |
| 70 // </ confuses the HTML parser because it could be a </script> tag. So we | 28 // </ confuses the HTML parser because it could be a </script> tag. So we |
| 71 // replace </ with <\/. The extra \ will be ignored by the JS engine. | 29 // replace </ with <\/. The extra \ will be ignored by the JS engine. |
| 72 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); | 30 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); |
| 73 | 31 |
| 74 output->append("<script>"); | 32 output->append("<script>"); |
| 75 output->append(javascript_string); | 33 output->append(javascript_string); |
| 76 output->append("</script>"); | 34 output->append("</script>"); |
| 77 } | 35 } |
| 78 | 36 |
| 79 void AppendJsonJS(const base::DictionaryValue* json, std::string* output) { | 37 // Appends the source for JsTemplates in a script tag. |
| 80 // Convert the template data to a json string. | 38 void AppendLoadTimeData(std::string* output) { |
| 81 DCHECK(json) << "must include json data structure"; | 39 // fetch and cache the pointer of the jstemplate resource source text. |
| 40 static const base::StringPiece jstemplate_src( | |
|
sky
2014/09/24 23:24:37
Style guide says: "Objects with static storage dur
Evan Stade
2014/09/25 00:02:54
I guess that copying a StringPiece is cheap, so I
| |
| 41 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 42 IDR_WEBUI_JS_LOAD_TIME_DATA)); | |
| 82 | 43 |
| 83 std::string jstext; | 44 if (jstemplate_src.empty()) { |
| 84 JSONStringValueSerializer serializer(&jstext); | 45 NOTREACHED() << "Unable to get loadTimeData src"; |
| 85 serializer.Serialize(*json); | 46 return; |
| 86 output->append(g_version2 ? "loadTimeData.data = " : "var templateData = "); | 47 } |
| 87 output->append(jstext); | 48 |
| 88 output->append(";"); | 49 output->append("<script>"); |
| 50 output->append(jstemplate_src.data(), jstemplate_src.size()); | |
| 51 output->append("</script>"); | |
| 89 } | 52 } |
| 90 | 53 |
| 54 // Appends the source for JsTemplates in a script tag. | |
| 91 void AppendJsTemplateSourceHtml(std::string* output) { | 55 void AppendJsTemplateSourceHtml(std::string* output) { |
| 92 // fetch and cache the pointer of the jstemplate resource source text. | 56 // fetch and cache the pointer of the jstemplate resource source text. |
| 93 static const base::StringPiece jstemplate_src( | 57 static const base::StringPiece jstemplate_src( |
| 94 ResourceBundle::GetSharedInstance().GetRawDataResource( | 58 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 95 IDR_WEBUI_JSTEMPLATE_JS)); | 59 IDR_WEBUI_JSTEMPLATE_JS)); |
| 96 | 60 |
| 97 if (jstemplate_src.empty()) { | 61 if (jstemplate_src.empty()) { |
| 98 NOTREACHED() << "Unable to get jstemplate src"; | 62 NOTREACHED() << "Unable to get jstemplate src"; |
| 99 return; | 63 return; |
| 100 } | 64 } |
| 101 | 65 |
| 102 output->append("<script>"); | 66 output->append("<script>"); |
| 103 output->append(jstemplate_src.data(), jstemplate_src.size()); | 67 output->append(jstemplate_src.data(), jstemplate_src.size()); |
| 104 output->append("</script>"); | 68 output->append("</script>"); |
| 105 } | 69 } |
| 106 | 70 |
| 107 void AppendJsTemplateProcessHtml(const base::StringPiece& template_id, | 71 // Appends the code that processes the JsTemplate with the JSON. You should |
| 108 std::string* output) { | 72 // call AppendJsTemplateSourceHtml and AppendJsonHtml before calling this. |
| 73 void AppendJsTemplateProcessHtml( | |
| 74 const base::StringPiece& template_id, | |
| 75 std::string* output) { | |
| 109 output->append("<script>"); | 76 output->append("<script>"); |
| 110 output->append("var tp = document.getElementById('"); | 77 output->append("var tp = document.getElementById('"); |
| 111 output->append(template_id.data(), template_id.size()); | 78 output->append(template_id.data(), template_id.size()); |
| 112 output->append("');"); | 79 output->append("');"); |
| 113 output->append("jstProcess(new JsEvalContext(templateData), tp);"); | 80 output->append("jstProcess(loadTimeData.createJsEvalContext(), tp);"); |
| 114 output->append("</script>"); | 81 output->append("</script>"); |
| 115 } | 82 } |
| 116 | 83 |
| 84 // Appends the source for i18n Templates in a script tag. | |
| 117 void AppendI18nTemplateSourceHtml(std::string* output) { | 85 void AppendI18nTemplateSourceHtml(std::string* output) { |
| 118 // fetch and cache the pointer of the jstemplate resource source text. | |
| 119 static const base::StringPiece i18n_template_src( | 86 static const base::StringPiece i18n_template_src( |
| 120 ResourceBundle::GetSharedInstance().GetRawDataResource( | 87 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 121 IDR_WEBUI_I18N_TEMPLATE_JS)); | |
| 122 static const base::StringPiece i18n_template2_src( | |
| 123 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 124 IDR_WEBUI_I18N_TEMPLATE2_JS)); | 88 IDR_WEBUI_I18N_TEMPLATE2_JS)); |
| 125 const base::StringPiece* template_src = g_version2 ? | |
| 126 &i18n_template2_src : &i18n_template_src; | |
| 127 | 89 |
| 128 if (template_src->empty()) { | 90 if (i18n_template_src.empty()) { |
| 129 NOTREACHED() << "Unable to get i18n template src"; | 91 NOTREACHED() << "Unable to get i18n template src"; |
| 130 return; | 92 return; |
| 131 } | 93 } |
| 132 | 94 |
| 133 output->append("<script>"); | 95 output->append("<script>"); |
| 134 output->append(template_src->data(), template_src->size()); | 96 output->append(i18n_template_src.data(), i18n_template_src.size()); |
| 135 output->append("</script>"); | 97 output->append("</script>"); |
| 136 } | 98 } |
| 137 | 99 |
| 138 void AppendI18nTemplateProcessHtml(std::string* output) { | 100 } // namespace |
| 139 if (g_version2) | |
| 140 return; | |
| 141 | 101 |
| 142 static const base::StringPiece i18n_process_src( | 102 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, |
| 143 ResourceBundle::GetSharedInstance().GetRawDataResource( | 103 const base::DictionaryValue* json) { |
| 144 IDR_WEBUI_I18N_PROCESS_JS)); | 104 std::string output(html_template.data(), html_template.size()); |
| 105 AppendJsonHtml(json, &output); | |
| 106 AppendI18nTemplateSourceHtml(&output); | |
| 107 return output; | |
| 108 } | |
| 145 | 109 |
| 146 if (i18n_process_src.empty()) { | 110 std::string GetTemplatesHtml(const base::StringPiece& html_template, |
| 147 NOTREACHED() << "Unable to get i18n process src"; | 111 const base::DictionaryValue* json, |
| 148 return; | 112 const base::StringPiece& template_id) { |
| 149 } | 113 std::string output(html_template.data(), html_template.size()); |
| 114 AppendLoadTimeData(&output); | |
| 115 AppendJsonHtml(json, &output); | |
| 116 AppendI18nTemplateSourceHtml(&output); | |
| 117 AppendJsTemplateSourceHtml(&output); | |
| 118 AppendJsTemplateProcessHtml(template_id, &output); | |
| 119 return output; | |
| 120 } | |
| 150 | 121 |
| 151 output->append("<script>"); | 122 void AppendJsonJS(const base::DictionaryValue* json, std::string* output) { |
| 152 output->append(i18n_process_src.data(), i18n_process_src.size()); | 123 // Convert the template data to a json string. |
| 153 output->append("</script>"); | 124 DCHECK(json) << "must include json data structure"; |
| 125 | |
| 126 std::string jstext; | |
| 127 JSONStringValueSerializer serializer(&jstext); | |
| 128 serializer.Serialize(*json); | |
| 129 output->append("loadTimeData.data = "); | |
| 130 output->append(jstext); | |
| 131 output->append(";"); | |
| 154 } | 132 } |
| 155 | 133 |
| 156 } // namespace webui | 134 } // namespace webui |
| OLD | NEW |