Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(702)

Side by Side Diff: ios/web/webui/web_ui_ios_data_source_impl.cc

Issue 1110213002: Upstream most of the iOS WebUI support in ios/web/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ios/web/webui/web_ui_ios_data_source_impl.h"
6
7 #include <string>
8
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"
15
16 namespace web {
17
18 // static
19 WebUIIOSDataSource* WebUIIOSDataSource::Create(const std::string& source_name) {
20 return new WebUIIOSDataSourceImpl(source_name);
21 }
22
23 // static
24 void WebUIIOSDataSource::Add(BrowserState* browser_state,
25 WebUIIOSDataSource* source) {
26 URLDataManagerIOS::AddWebUIIOSDataSource(browser_state, source);
27 }
28
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) {}
34
35 ~InternalDataSource() override {}
36
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);
41 }
42 void StartDataRequest(
43 const std::string& path,
44 const URLDataSourceIOS::GotDataCallback& callback) override {
45 return parent_->StartDataRequest(path, callback);
46 }
47 bool ShouldReplaceExistingSource() const override {
48 return parent_->replace_existing_source_;
49 }
50 bool AllowCaching() const override { return false; }
51 bool ShouldDenyXFrameOptions() const override {
52 return parent_->deny_xframe_options_;
53 }
54
55 private:
56 WebUIIOSDataSourceImpl* parent_;
57 };
58
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) {
66 }
67
68 WebUIIOSDataSourceImpl::~WebUIIOSDataSourceImpl() {
69 }
70
71 void WebUIIOSDataSourceImpl::AddString(const std::string& name,
72 const base::string16& value) {
73 localized_strings_.SetString(name, value);
74 }
75
76 void WebUIIOSDataSourceImpl::AddString(const std::string& name,
77 const std::string& value) {
78 localized_strings_.SetString(name, value);
79 }
80
81 void WebUIIOSDataSourceImpl::AddLocalizedString(const std::string& name,
82 int ids) {
83 localized_strings_.SetString(name, GetWebClient()->GetLocalizedString(ids));
84 }
85
86 void WebUIIOSDataSourceImpl::AddBoolean(const std::string& name, bool value) {
87 localized_strings_.SetBoolean(name, value);
88 }
89
90 void WebUIIOSDataSourceImpl::SetJsonPath(const std::string& path) {
91 json_path_ = path;
92 }
93
94 void WebUIIOSDataSourceImpl::AddResourcePath(const std::string& path,
95 int resource_id) {
96 path_to_idr_map_[path] = resource_id;
97 }
98
99 void WebUIIOSDataSourceImpl::SetDefaultResource(int resource_id) {
100 default_resource_ = resource_id;
101 }
102
103 void WebUIIOSDataSourceImpl::DisableDenyXFrameOptions() {
104 deny_xframe_options_ = false;
105 }
106
107 std::string WebUIIOSDataSourceImpl::GetSource() const {
108 return source_name_;
109 }
110
111 std::string WebUIIOSDataSourceImpl::GetMimeType(const std::string& path) const {
112 if (EndsWith(path, ".js", false))
113 return "application/javascript";
114
115 if (EndsWith(path, ".json", false))
116 return "application/json";
117
118 if (EndsWith(path, ".pdf", false))
119 return "application/pdf";
120
121 if (EndsWith(path, ".css", false))
122 return "text/css";
123
124 if (EndsWith(path, ".svg", false))
125 return "image/svg+xml";
126
127 return "text/html";
128 }
129
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;
136 }
137
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);
145 }
146
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_);
153 }
154
155 webui::AppendJsonJS(&localized_strings_, &template_data);
156 callback.Run(base::RefCountedString::TakeString(&template_data));
157 }
158
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());
165 }
166
167 } // namespace web
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698