| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "ios/web/webui/web_ui_ios_data_source_impl.h" | 5 #include "ios/web/webui/web_ui_ios_data_source_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #import "ios/web/public/web_client.h" | 12 #import "ios/web/public/web_client.h" |
| 14 #include "ui/base/webui/jstemplate_builder.h" | 13 #include "ui/base/webui/jstemplate_builder.h" |
| 15 #include "ui/base/webui/web_ui_util.h" | 14 #include "ui/base/webui/web_ui_util.h" |
| 16 | 15 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
| 19 #endif | 18 #endif |
| 20 | 19 |
| 21 namespace web { | 20 namespace web { |
| 22 | 21 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 58 |
| 60 private: | 59 private: |
| 61 WebUIIOSDataSourceImpl* parent_; | 60 WebUIIOSDataSourceImpl* parent_; |
| 62 }; | 61 }; |
| 63 | 62 |
| 64 WebUIIOSDataSourceImpl::WebUIIOSDataSourceImpl(const std::string& source_name) | 63 WebUIIOSDataSourceImpl::WebUIIOSDataSourceImpl(const std::string& source_name) |
| 65 : URLDataSourceIOSImpl(source_name, new InternalDataSource(this)), | 64 : URLDataSourceIOSImpl(source_name, new InternalDataSource(this)), |
| 66 source_name_(source_name), | 65 source_name_(source_name), |
| 67 default_resource_(-1), | 66 default_resource_(-1), |
| 68 deny_xframe_options_(true), | 67 deny_xframe_options_(true), |
| 69 load_time_data_defaults_added_(true), | |
| 70 replace_existing_source_(true) {} | 68 replace_existing_source_(true) {} |
| 71 | 69 |
| 72 WebUIIOSDataSourceImpl::~WebUIIOSDataSourceImpl() {} | 70 WebUIIOSDataSourceImpl::~WebUIIOSDataSourceImpl() {} |
| 73 | 71 |
| 74 void WebUIIOSDataSourceImpl::AddString(const std::string& name, | 72 void WebUIIOSDataSourceImpl::AddString(const std::string& name, |
| 75 const base::string16& value) { | 73 const base::string16& value) { |
| 76 localized_strings_.SetString(name, value); | 74 localized_strings_.SetString(name, value); |
| 77 replacements_[name] = base::UTF16ToUTF8(value); | |
| 78 } | 75 } |
| 79 | 76 |
| 80 void WebUIIOSDataSourceImpl::AddString(const std::string& name, | 77 void WebUIIOSDataSourceImpl::AddString(const std::string& name, |
| 81 const std::string& value) { | 78 const std::string& value) { |
| 82 localized_strings_.SetString(name, value); | 79 localized_strings_.SetString(name, value); |
| 83 replacements_[name] = value; | |
| 84 } | 80 } |
| 85 | 81 |
| 86 void WebUIIOSDataSourceImpl::AddLocalizedString(const std::string& name, | 82 void WebUIIOSDataSourceImpl::AddLocalizedString(const std::string& name, |
| 87 int ids) { | 83 int ids) { |
| 88 localized_strings_.SetString(name, GetWebClient()->GetLocalizedString(ids)); | 84 localized_strings_.SetString(name, GetWebClient()->GetLocalizedString(ids)); |
| 89 replacements_[name] = | |
| 90 base::UTF16ToUTF8(GetWebClient()->GetLocalizedString(ids)); | |
| 91 } | |
| 92 | |
| 93 void WebUIIOSDataSourceImpl::AddLocalizedStrings( | |
| 94 const base::DictionaryValue& localized_strings) { | |
| 95 localized_strings_.MergeDictionary(&localized_strings); | |
| 96 ui::TemplateReplacementsFromDictionaryValue(localized_strings, | |
| 97 &replacements_); | |
| 98 } | 85 } |
| 99 | 86 |
| 100 void WebUIIOSDataSourceImpl::AddBoolean(const std::string& name, bool value) { | 87 void WebUIIOSDataSourceImpl::AddBoolean(const std::string& name, bool value) { |
| 101 localized_strings_.SetBoolean(name, value); | 88 localized_strings_.SetBoolean(name, value); |
| 102 } | 89 } |
| 103 | 90 |
| 104 void WebUIIOSDataSourceImpl::SetJsonPath(const std::string& path) { | 91 void WebUIIOSDataSourceImpl::SetJsonPath(const std::string& path) { |
| 105 json_path_ = path; | 92 json_path_ = path; |
| 106 } | 93 } |
| 107 | 94 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 134 | 121 |
| 135 if (base::EndsWith(path, ".css", base::CompareCase::INSENSITIVE_ASCII)) | 122 if (base::EndsWith(path, ".css", base::CompareCase::INSENSITIVE_ASCII)) |
| 136 return "text/css"; | 123 return "text/css"; |
| 137 | 124 |
| 138 if (base::EndsWith(path, ".svg", base::CompareCase::INSENSITIVE_ASCII)) | 125 if (base::EndsWith(path, ".svg", base::CompareCase::INSENSITIVE_ASCII)) |
| 139 return "image/svg+xml"; | 126 return "image/svg+xml"; |
| 140 | 127 |
| 141 return "text/html"; | 128 return "text/html"; |
| 142 } | 129 } |
| 143 | 130 |
| 144 void WebUIIOSDataSourceImpl::EnsureLoadTimeDataDefaultsAdded() { | |
| 145 if (!load_time_data_defaults_added_) | |
| 146 return; | |
| 147 | |
| 148 load_time_data_defaults_added_ = false; | |
| 149 base::DictionaryValue defaults; | |
| 150 webui::SetLoadTimeDataDefaults(web::GetWebClient()->GetApplicationLocale(), | |
| 151 &defaults); | |
| 152 AddLocalizedStrings(defaults); | |
| 153 } | |
| 154 | |
| 155 void WebUIIOSDataSourceImpl::StartDataRequest( | 131 void WebUIIOSDataSourceImpl::StartDataRequest( |
| 156 const std::string& path, | 132 const std::string& path, |
| 157 const URLDataSourceIOS::GotDataCallback& callback) { | 133 const URLDataSourceIOS::GotDataCallback& callback) { |
| 158 EnsureLoadTimeDataDefaultsAdded(); | |
| 159 | |
| 160 if (!json_path_.empty() && path == json_path_) { | 134 if (!json_path_.empty() && path == json_path_) { |
| 161 SendLocalizedStringsAsJSON(callback); | 135 SendLocalizedStringsAsJSON(callback); |
| 162 return; | 136 return; |
| 163 } | 137 } |
| 164 | 138 |
| 165 int resource_id = default_resource_; | 139 int resource_id = default_resource_; |
| 166 std::map<std::string, int>::iterator result; | 140 std::map<std::string, int>::iterator result; |
| 167 result = path_to_idr_map_.find(path); | 141 result = path_to_idr_map_.find(path); |
| 168 if (result != path_to_idr_map_.end()) | 142 if (result != path_to_idr_map_.end()) |
| 169 resource_id = result->second; | 143 resource_id = result->second; |
| 170 DCHECK_NE(resource_id, -1); | 144 DCHECK_NE(resource_id, -1); |
| 171 SendFromResourceBundle(path, callback, resource_id); | 145 SendFromResourceBundle(callback, resource_id); |
| 172 } | 146 } |
| 173 | 147 |
| 174 void WebUIIOSDataSourceImpl::SendLocalizedStringsAsJSON( | 148 void WebUIIOSDataSourceImpl::SendLocalizedStringsAsJSON( |
| 175 const URLDataSourceIOS::GotDataCallback& callback) { | 149 const URLDataSourceIOS::GotDataCallback& callback) { |
| 176 std::string template_data; | 150 std::string template_data; |
| 151 webui::SetLoadTimeDataDefaults(web::GetWebClient()->GetApplicationLocale(), |
| 152 &localized_strings_); |
| 153 |
| 177 webui::AppendJsonJS(&localized_strings_, &template_data); | 154 webui::AppendJsonJS(&localized_strings_, &template_data); |
| 178 callback.Run(base::RefCountedString::TakeString(&template_data)); | 155 callback.Run(base::RefCountedString::TakeString(&template_data)); |
| 179 } | 156 } |
| 180 | 157 |
| 181 void WebUIIOSDataSourceImpl::SendFromResourceBundle( | 158 void WebUIIOSDataSourceImpl::SendFromResourceBundle( |
| 182 const std::string& path, | |
| 183 const URLDataSourceIOS::GotDataCallback& callback, | 159 const URLDataSourceIOS::GotDataCallback& callback, |
| 184 int idr) { | 160 int idr) { |
| 185 scoped_refptr<base::RefCountedMemory> response( | 161 scoped_refptr<base::RefCountedMemory> response( |
| 186 GetWebClient()->GetDataResourceBytes(idr)); | 162 GetWebClient()->GetDataResourceBytes(idr)); |
| 187 | |
| 188 if (response.get() && GetMimeType(path) == "text/html") { | |
| 189 std::string replaced = ui::ReplaceTemplateExpressions( | |
| 190 base::StringPiece(response->front_as<char>(), response->size()), | |
| 191 replacements_); | |
| 192 response = base::RefCountedString::TakeString(&replaced); | |
| 193 } | |
| 194 | |
| 195 callback.Run(response); | 163 callback.Run(response); |
| 196 } | 164 } |
| 197 | 165 |
| 198 } // namespace web | 166 } // namespace web |
| OLD | NEW |