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