| 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/common_resources.h" | 14 #include "grit/common_resources.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 | 16 |
| 17 namespace { |
| 18 |
| 19 // True when building version 2 templates. See UseVersion2 class. |
| 20 bool g_version2 = false; |
| 21 |
| 22 } // namespace |
| 23 |
| 17 namespace jstemplate_builder { | 24 namespace jstemplate_builder { |
| 18 | 25 |
| 26 UseVersion2::UseVersion2() : previous_value_(g_version2) { |
| 27 g_version2 = true; |
| 28 } |
| 29 |
| 30 UseVersion2::~UseVersion2() { |
| 31 g_version2 = previous_value_; |
| 32 } |
| 33 |
| 19 std::string GetTemplateHtml(const base::StringPiece& html_template, | 34 std::string GetTemplateHtml(const base::StringPiece& html_template, |
| 20 const DictionaryValue* json, | 35 const DictionaryValue* json, |
| 21 const base::StringPiece& template_id) { | 36 const base::StringPiece& template_id) { |
| 22 std::string output(html_template.data(), html_template.size()); | 37 std::string output(html_template.data(), html_template.size()); |
| 23 AppendJsonHtml(json, &output); | 38 AppendJsonHtml(json, &output); |
| 24 AppendJsTemplateSourceHtml(&output); | 39 AppendJsTemplateSourceHtml(&output); |
| 25 AppendJsTemplateProcessHtml(template_id, &output); | 40 AppendJsTemplateProcessHtml(template_id, &output); |
| 26 return output; | 41 return output; |
| 27 } | 42 } |
| 28 | 43 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 42 AppendI18nTemplateSourceHtml(&output); | 57 AppendI18nTemplateSourceHtml(&output); |
| 43 AppendJsTemplateSourceHtml(&output); | 58 AppendJsTemplateSourceHtml(&output); |
| 44 AppendJsonHtml(json, &output); | 59 AppendJsonHtml(json, &output); |
| 45 AppendI18nTemplateProcessHtml(&output); | 60 AppendI18nTemplateProcessHtml(&output); |
| 46 AppendJsTemplateProcessHtml(template_id, &output); | 61 AppendJsTemplateProcessHtml(template_id, &output); |
| 47 return output; | 62 return output; |
| 48 } | 63 } |
| 49 | 64 |
| 50 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { | 65 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { |
| 51 std::string javascript_string; | 66 std::string javascript_string; |
| 52 AppendJsonJS(json, &javascript_string); | 67 jstemplate_builder::AppendJsonJS(json, &javascript_string); |
| 53 | 68 |
| 54 // </ confuses the HTML parser because it could be a </script> tag. So we | 69 // </ confuses the HTML parser because it could be a </script> tag. So we |
| 55 // replace </ with <\/. The extra \ will be ignored by the JS engine. | 70 // replace </ with <\/. The extra \ will be ignored by the JS engine. |
| 56 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); | 71 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); |
| 57 | 72 |
| 58 output->append("<script>"); | 73 output->append("<script>"); |
| 59 output->append(javascript_string); | 74 output->append(javascript_string); |
| 60 output->append("</script>"); | 75 output->append("</script>"); |
| 61 } | 76 } |
| 62 | 77 |
| 63 void AppendJsonJS(const DictionaryValue* json, std::string* output) { | 78 void AppendJsonJS(const DictionaryValue* json, std::string* output) { |
| 64 // Convert the template data to a json string. | 79 // Convert the template data to a json string. |
| 65 DCHECK(json) << "must include json data structure"; | 80 DCHECK(json) << "must include json data structure"; |
| 66 | 81 |
| 67 std::string jstext; | 82 std::string jstext; |
| 68 JSONStringValueSerializer serializer(&jstext); | 83 JSONStringValueSerializer serializer(&jstext); |
| 69 serializer.Serialize(*json); | 84 serializer.Serialize(*json); |
| 70 output->append("var templateData = "); | 85 output->append(g_version2 ? "loadTimeData.data = " : "var templateData = "); |
| 71 output->append(jstext); | 86 output->append(jstext); |
| 72 output->append(";"); | 87 output->append(";"); |
| 73 } | 88 } |
| 74 | |
| 75 void AppendJsonJS2(const DictionaryValue* json, std::string* output) { | |
| 76 // Convert the template data to a json string. | |
| 77 DCHECK(json) << "must include json data structure"; | |
| 78 | |
| 79 std::string jstext; | |
| 80 JSONStringValueSerializer serializer(&jstext); | |
| 81 serializer.Serialize(*json); | |
| 82 output->append("loadTimeData.data = "); | |
| 83 output->append(jstext); | |
| 84 output->append(";"); | |
| 85 } | |
| 86 | 89 |
| 87 void AppendJsTemplateSourceHtml(std::string* output) { | 90 void AppendJsTemplateSourceHtml(std::string* output) { |
| 88 // fetch and cache the pointer of the jstemplate resource source text. | 91 // fetch and cache the pointer of the jstemplate resource source text. |
| 89 static const base::StringPiece jstemplate_src( | 92 static const base::StringPiece jstemplate_src( |
| 90 ResourceBundle::GetSharedInstance().GetRawDataResource( | 93 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 91 IDR_JSTEMPLATE_JS)); | 94 IDR_JSTEMPLATE_JS)); |
| 92 | 95 |
| 93 if (jstemplate_src.empty()) { | 96 if (jstemplate_src.empty()) { |
| 94 NOTREACHED() << "Unable to get jstemplate src"; | 97 NOTREACHED() << "Unable to get jstemplate src"; |
| 95 return; | 98 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 107 output->append(template_id.data(), template_id.size()); | 110 output->append(template_id.data(), template_id.size()); |
| 108 output->append("');"); | 111 output->append("');"); |
| 109 output->append("jstProcess(new JsEvalContext(templateData), tp);"); | 112 output->append("jstProcess(new JsEvalContext(templateData), tp);"); |
| 110 output->append("</script>"); | 113 output->append("</script>"); |
| 111 } | 114 } |
| 112 | 115 |
| 113 void AppendI18nTemplateSourceHtml(std::string* output) { | 116 void AppendI18nTemplateSourceHtml(std::string* output) { |
| 114 // fetch and cache the pointer of the jstemplate resource source text. | 117 // fetch and cache the pointer of the jstemplate resource source text. |
| 115 static const base::StringPiece i18n_template_src( | 118 static const base::StringPiece i18n_template_src( |
| 116 ResourceBundle::GetSharedInstance().GetRawDataResource( | 119 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 117 IDR_I18N_TEMPLATE_JS)); | 120 g_version2 ? IDR_I18N_TEMPLATE2_JS : IDR_I18N_TEMPLATE_JS)); |
| 118 | 121 |
| 119 if (i18n_template_src.empty()) { | 122 if (i18n_template_src.empty()) { |
| 120 NOTREACHED() << "Unable to get i18n template src"; | 123 NOTREACHED() << "Unable to get i18n template src"; |
| 121 return; | 124 return; |
| 122 } | 125 } |
| 123 | 126 |
| 124 output->append("<script>"); | 127 output->append("<script>"); |
| 125 output->append(i18n_template_src.data(), i18n_template_src.size()); | 128 output->append(i18n_template_src.data(), i18n_template_src.size()); |
| 126 output->append("</script>"); | 129 output->append("</script>"); |
| 127 } | 130 } |
| 128 | 131 |
| 129 void AppendI18nTemplateProcessHtml(std::string* output) { | 132 void AppendI18nTemplateProcessHtml(std::string* output) { |
| 133 if (g_version2) |
| 134 return; |
| 135 |
| 130 static const base::StringPiece i18n_process_src( | 136 static const base::StringPiece i18n_process_src( |
| 131 ResourceBundle::GetSharedInstance().GetRawDataResource( | 137 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 132 IDR_I18N_PROCESS_JS)); | 138 IDR_I18N_PROCESS_JS)); |
| 133 | 139 |
| 134 if (i18n_process_src.empty()) { | 140 if (i18n_process_src.empty()) { |
| 135 NOTREACHED() << "Unable to get i18n process src"; | 141 NOTREACHED() << "Unable to get i18n process src"; |
| 136 return; | 142 return; |
| 137 } | 143 } |
| 138 | 144 |
| 139 output->append("<script>"); | 145 output->append("<script>"); |
| 140 output->append(i18n_process_src.data(), i18n_process_src.size()); | 146 output->append(i18n_process_src.data(), i18n_process_src.size()); |
| 141 output->append("</script>"); | 147 output->append("</script>"); |
| 142 } | 148 } |
| 143 | 149 |
| 144 } // namespace jstemplate_builder | 150 } // namespace jstemplate_builder |
| OLD | NEW |