Add ICU message format support
[chromium-blink-merge.git] / ios / web / webui / web_ui_ios_data_source_impl.cc
blobe20b66015500f7e8ce62960fa18c702b1a47321e
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
3 // found in the LICENSE file.
5 #include "ios/web/webui/web_ui_ios_data_source_impl.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/strings/string_util.h"
12 #include "ios/web/public/web_client.h"
13 #include "ui/base/webui/jstemplate_builder.h"
14 #include "ui/base/webui/web_ui_util.h"
16 namespace web {
18 // static
19 WebUIIOSDataSource* WebUIIOSDataSource::Create(const std::string& source_name) {
20 return new WebUIIOSDataSourceImpl(source_name);
23 // static
24 void WebUIIOSDataSource::Add(BrowserState* browser_state,
25 WebUIIOSDataSource* source) {
26 URLDataManagerIOS::AddWebUIIOSDataSource(browser_state, source);
29 // Internal class to hide the fact that WebUIIOSDataSourceImpl implements
30 // URLDataSourceIOS.
31 class WebUIIOSDataSourceImpl::InternalDataSource : public URLDataSourceIOS {
32 public:
33 InternalDataSource(WebUIIOSDataSourceImpl* parent) : parent_(parent) {}
35 ~InternalDataSource() override {}
37 // URLDataSourceIOS implementation.
38 std::string GetSource() const override { return parent_->GetSource(); }
39 std::string GetMimeType(const std::string& path) const override {
40 return parent_->GetMimeType(path);
42 void StartDataRequest(
43 const std::string& path,
44 const URLDataSourceIOS::GotDataCallback& callback) override {
45 return parent_->StartDataRequest(path, callback);
47 bool ShouldReplaceExistingSource() const override {
48 return parent_->replace_existing_source_;
50 bool AllowCaching() const override { return false; }
51 bool ShouldDenyXFrameOptions() const override {
52 return parent_->deny_xframe_options_;
55 private:
56 WebUIIOSDataSourceImpl* parent_;
59 WebUIIOSDataSourceImpl::WebUIIOSDataSourceImpl(const std::string& source_name)
60 : URLDataSourceIOSImpl(source_name, new InternalDataSource(this)),
61 source_name_(source_name),
62 default_resource_(-1),
63 deny_xframe_options_(true),
64 disable_set_font_strings_(false),
65 replace_existing_source_(true) {
68 WebUIIOSDataSourceImpl::~WebUIIOSDataSourceImpl() {
71 void WebUIIOSDataSourceImpl::AddString(const std::string& name,
72 const base::string16& value) {
73 localized_strings_.SetString(name, value);
76 void WebUIIOSDataSourceImpl::AddString(const std::string& name,
77 const std::string& value) {
78 localized_strings_.SetString(name, value);
81 void WebUIIOSDataSourceImpl::AddLocalizedString(const std::string& name,
82 int ids) {
83 localized_strings_.SetString(name, GetWebClient()->GetLocalizedString(ids));
86 void WebUIIOSDataSourceImpl::AddBoolean(const std::string& name, bool value) {
87 localized_strings_.SetBoolean(name, value);
90 void WebUIIOSDataSourceImpl::SetJsonPath(const std::string& path) {
91 json_path_ = path;
94 void WebUIIOSDataSourceImpl::AddResourcePath(const std::string& path,
95 int resource_id) {
96 path_to_idr_map_[path] = resource_id;
99 void WebUIIOSDataSourceImpl::SetDefaultResource(int resource_id) {
100 default_resource_ = resource_id;
103 void WebUIIOSDataSourceImpl::DisableDenyXFrameOptions() {
104 deny_xframe_options_ = false;
107 std::string WebUIIOSDataSourceImpl::GetSource() const {
108 return source_name_;
111 std::string WebUIIOSDataSourceImpl::GetMimeType(const std::string& path) const {
112 if (base::EndsWith(path, ".js", base::CompareCase::INSENSITIVE_ASCII))
113 return "application/javascript";
115 if (base::EndsWith(path, ".json", base::CompareCase::INSENSITIVE_ASCII))
116 return "application/json";
118 if (base::EndsWith(path, ".pdf", base::CompareCase::INSENSITIVE_ASCII))
119 return "application/pdf";
121 if (base::EndsWith(path, ".css", base::CompareCase::INSENSITIVE_ASCII))
122 return "text/css";
124 if (base::EndsWith(path, ".svg", base::CompareCase::INSENSITIVE_ASCII))
125 return "image/svg+xml";
127 return "text/html";
130 void WebUIIOSDataSourceImpl::StartDataRequest(
131 const std::string& path,
132 const URLDataSourceIOS::GotDataCallback& callback) {
133 if (!json_path_.empty() && path == json_path_) {
134 SendLocalizedStringsAsJSON(callback);
135 return;
138 int resource_id = default_resource_;
139 std::map<std::string, int>::iterator result;
140 result = path_to_idr_map_.find(path);
141 if (result != path_to_idr_map_.end())
142 resource_id = result->second;
143 DCHECK_NE(resource_id, -1);
144 SendFromResourceBundle(callback, resource_id);
147 void WebUIIOSDataSourceImpl::SendLocalizedStringsAsJSON(
148 const URLDataSourceIOS::GotDataCallback& callback) {
149 std::string template_data;
150 if (!disable_set_font_strings_) {
151 webui::SetLoadTimeDataDefaults(web::GetWebClient()->GetApplicationLocale(),
152 &localized_strings_);
155 webui::AppendJsonJS(&localized_strings_, &template_data);
156 callback.Run(base::RefCountedString::TakeString(&template_data));
159 void WebUIIOSDataSourceImpl::SendFromResourceBundle(
160 const URLDataSourceIOS::GotDataCallback& callback,
161 int idr) {
162 scoped_refptr<base::RefCountedStaticMemory> response(
163 GetWebClient()->GetDataResourceBytes(idr));
164 callback.Run(response.get());
167 } // namespace web