[email protected] | ca90e82b | 2012-03-07 18:30:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 5 | #include "content/browser/webui/shared_resources_data_source.h" |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 8 | #include <string> |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 9 | |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 10 | #include "base/feature_list.h" |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | be22ebe | 2013-01-23 17:03:08 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 90fa265 | 2012-04-24 16:18:35 | [diff] [blame] | 13 | #include "base/memory/ref_counted_memory.h" |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
[email protected] | 30e4f87 | 2013-08-17 13:54:47 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 16 | #include "base/task/post_task.h" |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 17 | #include "build/build_config.h" |
Kyle Horimoto | a69ab46 | 2019-04-23 00:08:21 | [diff] [blame^] | 18 | #include "content/grit/content_resources.h" |
| 19 | #include "content/grit/content_resources_map.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 20 | #include "content/public/browser/browser_task_traits.h" |
tzik | bd171b1 | 2017-02-23 04:24:04 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 22 | #include "content/public/browser/web_contents.h" |
[email protected] | be22ebe | 2013-01-23 17:03:08 | [diff] [blame] | 23 | #include "content/public/common/content_client.h" |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 24 | #include "content/public/common/content_features.h" |
[email protected] | be22ebe | 2013-01-23 17:03:08 | [diff] [blame] | 25 | #include "content/public/common/url_constants.h" |
Yuzhu Shen | 2efe4277 | 2017-11-11 02:01:09 | [diff] [blame] | 26 | #include "mojo/public/js/grit/mojo_bindings_resources.h" |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 27 | #include "mojo/public/js/grit/mojo_bindings_resources_map.h" |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 28 | #include "ui/base/layout.h" |
| 29 | #include "ui/base/webui/web_ui_util.h" |
| 30 | #include "ui/resources/grit/webui_resources.h" |
tfarina | 272c8d7 | 2014-09-07 05:48:46 | [diff] [blame] | 31 | #include "ui/resources/grit/webui_resources_map.h" |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 32 | |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 33 | #if defined(OS_CHROMEOS) |
| 34 | #include "chromeos/grit/chromeos_resources.h" |
| 35 | #include "chromeos/grit/chromeos_resources_map.h" |
| 36 | #endif |
| 37 | |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 38 | #if defined(OS_WIN) |
| 39 | #include "base/strings/utf_string_conversions.h" |
| 40 | #endif |
| 41 | |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 42 | namespace content { |
| 43 | |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 44 | namespace { |
| 45 | |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 46 | struct IdrGzipped { |
| 47 | int idr; |
| 48 | bool gzipped; |
| 49 | }; |
Takuto Ikuta | adf31eb | 2019-01-05 00:32:48 | [diff] [blame] | 50 | using ResourcesMap = std::unordered_map<std::string, IdrGzipped>; |
[email protected] | 30e4f87 | 2013-08-17 13:54:47 | [diff] [blame] | 51 | |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 52 | #if defined(OS_CHROMEOS) |
| 53 | const char kPolymerHtml[] = "polymer/v1_0/polymer/polymer.html"; |
| 54 | const char kPolymerJs[] = "polymer/v1_0/polymer/polymer-extracted.js"; |
| 55 | const char kPolymer2Html[] = "polymer/v1_0/polymer2/polymer.html"; |
| 56 | const char kPolymer2Js[] = "polymer/v1_0/polymer2/polymer-extracted.js"; |
rbpotter | 360a055 | 2019-02-27 05:15:32 | [diff] [blame] | 57 | const char kHtmlImportsJs[] = "polymer/v1_0/html-imports/html-imports.min.js"; |
| 58 | const char kHtmlImportsV0Js[] = |
| 59 | "polymer/v1_0/html-imports-v0/html-imports.min.js"; |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 60 | |
| 61 | // Utility for determining if both Polymer 1 and Polymer 2 are needed. |
| 62 | bool UsingMultiplePolymerVersions() { |
| 63 | return base::FeatureList::IsEnabled(features::kWebUIPolymer2) && |
| 64 | base::FeatureList::IsEnabled(features::kWebUIPolymer2Exceptions); |
| 65 | } |
| 66 | #endif // defined(OS_CHROMEOS) |
| 67 | |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 68 | const std::map<std::string, std::string> CreatePathPrefixAliasesMap() { |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 69 | // TODO(rkc): Once we have a separate source for apps, remove '*/apps/' |
| 70 | // aliases. |
| 71 | std::map<std::string, std::string> aliases = { |
| 72 | {"../../../third_party/polymer/v1_0/components-chromium/", |
| 73 | "polymer/v1_0/"}, |
| 74 | {"../../../third_party/web-animations-js/sources/", |
| 75 | "polymer/v1_0/web-animations-js/"}, |
| 76 | {"../../views/resources/default_100_percent/common/", "images/apps/"}, |
| 77 | {"../../views/resources/default_200_percent/common/", "images/2x/apps/"}, |
| 78 | {"../../webui/resources/cr_components/", "cr_components/"}, |
| 79 | {"../../webui/resources/cr_elements/", "cr_elements/"}, |
| 80 | }; |
| 81 | |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 82 | #if defined(OS_CHROMEOS) |
| 83 | if (UsingMultiplePolymerVersions()) |
| 84 | return aliases; |
| 85 | #endif // defined(OS_CHROMEOS) |
| 86 | |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 87 | #if !defined(OS_ANDROID) |
| 88 | if (base::FeatureList::IsEnabled(features::kWebUIPolymer2)) { |
| 89 | aliases["../../../third_party/polymer/v1_0/components-chromium/polymer2/"] = |
| 90 | "polymer/v1_0/polymer/"; |
rbpotter | 360a055 | 2019-02-27 05:15:32 | [diff] [blame] | 91 | } else { |
| 92 | aliases |
| 93 | ["../../../third_party/polymer/v1_0/components-chromium/" |
| 94 | "html-imports-v0/"] = "polymer/v1_0/html-imports/"; |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 95 | } |
| 96 | #endif // !defined(OS_ANDROID) |
| 97 | return aliases; |
| 98 | } |
| 99 | |
Kyle Horimoto | a69ab46 | 2019-04-23 00:08:21 | [diff] [blame^] | 100 | const std::map<int, std::string> CreateContentResourceIdToAliasMap() { |
| 101 | return std::map<int, std::string>{ |
| 102 | {IDR_URL_MOJO_JS, "js/url.mojom-lite.js"}, |
| 103 | }; |
| 104 | } |
| 105 | |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 106 | const std::map<int, std::string> CreateMojoResourceIdToAliasMap() { |
| 107 | return std::map<int, std::string> { |
Reilly Grant | dfcfe313 | 2019-03-21 12:46:56 | [diff] [blame] | 108 | {IDR_MOJO_MOJO_BINDINGS_LITE_JS, "js/mojo_bindings_lite.js"}, |
Nancy Li | 19c9bbc | 2019-03-05 20:56:56 | [diff] [blame] | 109 | {IDR_MOJO_BIG_BUFFER_MOJOM_LITE_JS, "js/big_buffer.mojom-lite.js"}, |
| 110 | {IDR_MOJO_FILE_MOJOM_LITE_JS, "js/file.mojom-lite.js"}, |
| 111 | {IDR_MOJO_STRING16_MOJOM_LITE_JS, "js/string16.mojom-lite.js"}, |
Sigurdur Asgeirsson | 10609e5c | 2018-10-10 19:58:49 | [diff] [blame] | 112 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
Reilly Grant | 9a1f2d2 | 2019-02-21 02:47:03 | [diff] [blame] | 113 | {IDR_MOJO_TIME_MOJOM_LITE_JS, "js/time.mojom-lite.js"}, |
Sigurdur Asgeirsson | 10609e5c | 2018-10-10 19:58:49 | [diff] [blame] | 114 | #endif // defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 115 | }; |
| 116 | } |
| 117 | |
| 118 | #if defined(OS_CHROMEOS) |
| 119 | const std::map<int, std::string> CreateChromeosMojoResourceIdToAliasMap() { |
| 120 | return std::map<int, std::string>{ |
Reilly Grant | 9a1f2d2 | 2019-02-21 02:47:03 | [diff] [blame] | 121 | {IDR_MULTIDEVICE_DEVICE_SYNC_MOJOM_LITE_JS, |
| 122 | "js/chromeos/device_sync.mojom-lite.js"}, |
| 123 | {IDR_MULTIDEVICE_MULTIDEVICE_SETUP_MOJOM_LITE_JS, |
| 124 | "js/chromeos/multidevice_setup.mojom-lite.js"}, |
| 125 | {IDR_MULTIDEVICE_MULTIDEVICE_SETUP_CONSTANTS_MOJOM_LITE_JS, |
| 126 | "js/chromeos/multidevice_setup_constants.mojom-lite.js"}, |
| 127 | {IDR_MULTIDEVICE_MULTIDEVICE_TYPES_MOJOM_LITE_JS, |
| 128 | "js/chromeos/multidevice_types.mojom-lite.js"}, |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 129 | }; |
| 130 | } |
| 131 | #endif // !defined(OS_CHROMEOS) |
| 132 | |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 133 | #if !defined(OS_ANDROID) |
| 134 | bool ShouldIgnore(std::string resource) { |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 135 | #if defined(OS_CHROMEOS) |
| 136 | if (UsingMultiplePolymerVersions()) |
| 137 | return false; |
| 138 | #endif // defined(OS_CHROMEOS) |
| 139 | |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 140 | if (base::FeatureList::IsEnabled(features::kWebUIPolymer2) && |
rbpotter | 360a055 | 2019-02-27 05:15:32 | [diff] [blame] | 141 | (base::StartsWith( |
| 142 | resource, |
| 143 | "../../../third_party/polymer/v1_0/components-chromium/polymer/", |
| 144 | base::CompareCase::SENSITIVE) || |
| 145 | base::StartsWith(resource, |
| 146 | "../../../third_party/polymer/v1_0/components-chromium/" |
| 147 | "html-imports-v0/", |
| 148 | base::CompareCase::SENSITIVE))) { |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | if (!base::FeatureList::IsEnabled(features::kWebUIPolymer2) && |
rbpotter | 360a055 | 2019-02-27 05:15:32 | [diff] [blame] | 153 | (base::StartsWith( |
| 154 | resource, |
| 155 | "../../../third_party/polymer/v1_0/components-chromium/polymer2/", |
| 156 | base::CompareCase::SENSITIVE) || |
| 157 | base::StartsWith(resource, |
| 158 | "../../../third_party/polymer/v1_0/components-chromium/" |
| 159 | "html-imports/", |
| 160 | base::CompareCase::SENSITIVE))) { |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | #endif // !defined(OS_ANDROID) |
[email protected] | 30e4f87 | 2013-08-17 13:54:47 | [diff] [blame] | 167 | |
dzhioev | 0b46032 | 2014-12-03 13:10:42 | [diff] [blame] | 168 | void AddResource(const std::string& path, |
| 169 | int resource_id, |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 170 | bool gzipped, |
dzhioev | 0b46032 | 2014-12-03 13:10:42 | [diff] [blame] | 171 | ResourcesMap* resources_map) { |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 172 | IdrGzipped idr_gzipped = {resource_id, gzipped}; |
| 173 | if (!resources_map->insert(std::make_pair(path, idr_gzipped)).second) |
dzhioev | 0b46032 | 2014-12-03 13:10:42 | [diff] [blame] | 174 | NOTREACHED() << "Redefinition of '" << path << "'"; |
[email protected] | 30e4f87 | 2013-08-17 13:54:47 | [diff] [blame] | 175 | } |
| 176 | |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 177 | void AddResourcesToMap(ResourcesMap* resources_map) { |
| 178 | const std::map<std::string, std::string> aliases = |
| 179 | CreatePathPrefixAliasesMap(); |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 180 | |
[email protected] | e28b283e4 | 2013-01-17 17:50:15 | [diff] [blame] | 181 | for (size_t i = 0; i < kWebuiResourcesSize; ++i) { |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 182 | const auto& resource = kWebuiResources[i]; |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 183 | |
| 184 | #if !defined(OS_ANDROID) |
| 185 | if (ShouldIgnore(resource.name)) |
| 186 | continue; |
| 187 | #endif // !defined(OS_ANDROID) |
| 188 | |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 189 | AddResource(resource.name, resource.value, resource.gzipped, resources_map); |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 190 | |
| 191 | for (auto it = aliases.begin(); it != aliases.end(); ++it) { |
| 192 | if (base::StartsWith(resource.name, it->first, |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame] | 193 | base::CompareCase::SENSITIVE)) { |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 194 | std::string resource_name(resource.name); |
dpapad | 2313a4f9 | 2018-07-17 17:03:09 | [diff] [blame] | 195 | AddResource(it->second + resource_name.substr(it->first.length()), |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 196 | resource.value, resource.gzipped, resources_map); |
dzhioev | 0b46032 | 2014-12-03 13:10:42 | [diff] [blame] | 197 | } |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 198 | } |
| 199 | } |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Adds |resources| to |resources_map|, but renames each resource according to |
| 203 | // the scheme in |resource_aliases|, which maps from resource ID to resource |
| 204 | // alias. Note that resources which do not have an alias will not be added. |
| 205 | void AddAliasedResourcesToMap( |
| 206 | const std::map<int, std::string>& resource_aliases, |
| 207 | const GzippedGritResourceMap resources[], |
| 208 | size_t resources_size, |
| 209 | ResourcesMap* resources_map) { |
| 210 | for (size_t i = 0; i < resources_size; ++i) { |
| 211 | const auto& resource = resources[i]; |
| 212 | |
| 213 | const auto it = resource_aliases.find(resource.value); |
| 214 | if (it == resource_aliases.end()) |
| 215 | continue; |
| 216 | |
| 217 | AddResource(it->second, resource.value, resource.gzipped, resources_map); |
Ken Rockot | ea19acc | 2017-06-07 00:34:31 | [diff] [blame] | 218 | } |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | const ResourcesMap* CreateResourcesMap() { |
| 222 | ResourcesMap* result = new ResourcesMap(); |
| 223 | AddResourcesToMap(result); |
Kyle Horimoto | a69ab46 | 2019-04-23 00:08:21 | [diff] [blame^] | 224 | AddAliasedResourcesToMap(CreateContentResourceIdToAliasMap(), |
| 225 | kContentResources, kContentResourcesSize, result); |
Kyle Horimoto | 93a81e47 | 2018-09-21 23:30:50 | [diff] [blame] | 226 | AddAliasedResourcesToMap(CreateMojoResourceIdToAliasMap(), |
| 227 | kMojoBindingsResources, kMojoBindingsResourcesSize, |
| 228 | result); |
| 229 | #if defined(OS_CHROMEOS) |
| 230 | AddAliasedResourcesToMap(CreateChromeosMojoResourceIdToAliasMap(), |
| 231 | kChromeosResources, kChromeosResourcesSize, result); |
| 232 | #endif // !defined(OS_CHROMEOS) |
dzhioev | 0b46032 | 2014-12-03 13:10:42 | [diff] [blame] | 233 | return result; |
| 234 | } |
| 235 | |
| 236 | const ResourcesMap& GetResourcesMap() { |
| 237 | // This pointer will be intentionally leaked on shutdown. |
| 238 | static const ResourcesMap* resources_map = CreateResourcesMap(); |
| 239 | return *resources_map; |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 240 | } |
| 241 | |
tzik | bd171b1 | 2017-02-23 04:24:04 | [diff] [blame] | 242 | int GetIdrForPath(const std::string& path) { |
| 243 | const ResourcesMap& resources_map = GetResourcesMap(); |
| 244 | auto it = resources_map.find(path); |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 245 | return it != resources_map.end() ? it->second.idr : -1; |
tzik | bd171b1 | 2017-02-23 04:24:04 | [diff] [blame] | 246 | } |
| 247 | |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 248 | } // namespace |
| 249 | |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 250 | SharedResourcesDataSource::SharedResourcesDataSource() { |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | SharedResourcesDataSource::~SharedResourcesDataSource() { |
| 254 | } |
| 255 | |
[email protected] | 305b8e8 | 2013-04-17 16:12:33 | [diff] [blame] | 256 | std::string SharedResourcesDataSource::GetSource() const { |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 257 | return kChromeUIResourcesHost; |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 260 | void SharedResourcesDataSource::StartDataRequest( |
| 261 | const std::string& path, |
jam | edcd8b01 | 2016-09-20 02:03:58 | [diff] [blame] | 262 | const ResourceRequestInfo::WebContentsGetter& wc_getter, |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 263 | const URLDataSource::GotDataCallback& callback) { |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 264 | std::string updated_path = path; |
| 265 | #if defined(OS_CHROMEOS) |
| 266 | // If this is a Polymer request and multiple Polymer versions are enabled, |
| 267 | // return the Polymer 2 path unless the request is from the |
| 268 | // |disabled_polymer2_host_|. |
rbpotter | 360a055 | 2019-02-27 05:15:32 | [diff] [blame] | 269 | if ((path == kPolymerHtml || path == kPolymerJs || path == kHtmlImportsJs) && |
| 270 | UsingMultiplePolymerVersions()) { |
| 271 | bool polymer2 = !IsPolymer2DisabledForPage(wc_getter); |
| 272 | if (polymer2 && (path == kPolymerHtml || path == kPolymerJs)) { |
| 273 | updated_path = path == kPolymerHtml ? kPolymer2Html : kPolymer2Js; |
| 274 | } else if (!polymer2 && path == kHtmlImportsJs) { |
| 275 | updated_path = kHtmlImportsV0Js; |
| 276 | } |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 277 | } |
| 278 | #endif // defined(OS_CHROMEOS) |
| 279 | |
| 280 | int idr = GetIdrForPath(updated_path); |
| 281 | DCHECK_NE(-1, idr) << " path: " << updated_path; |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 282 | scoped_refptr<base::RefCountedMemory> bytes; |
| 283 | |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 284 | if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) { |
bauerb | d039918 | 2015-01-26 10:20:45 | [diff] [blame] | 285 | std::string css = webui::GetWebUiCssTextDefaults(); |
| 286 | bytes = base::RefCountedString::TakeString(&css); |
edwardjung | b7dac1a | 2016-01-15 14:18:35 | [diff] [blame] | 287 | } else if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) { |
| 288 | std::string css = webui::GetWebUiCssTextDefaultsMd(); |
| 289 | bytes = base::RefCountedString::TakeString(&css); |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 290 | } else { |
dbeam | 9e1b0f0 | 2014-12-03 01:34:47 | [diff] [blame] | 291 | bytes = GetContentClient()->GetDataResourceBytes(idr); |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 292 | } |
[email protected] | 47b541d9 | 2012-03-27 19:47:49 | [diff] [blame] | 293 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 294 | callback.Run(bytes.get()); |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 295 | } |
| 296 | |
toyoshim | bb7ab18 | 2017-03-30 05:28:08 | [diff] [blame] | 297 | bool SharedResourcesDataSource::AllowCaching() const { |
| 298 | // Should not be cached to reflect dynamically-generated contents that may |
| 299 | // depend on the current locale. |
| 300 | return false; |
| 301 | } |
| 302 | |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 303 | std::string SharedResourcesDataSource::GetMimeType( |
| 304 | const std::string& path) const { |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 305 | if (path.empty()) |
| 306 | return "text/html"; |
[email protected] | db728196 | 2013-01-17 19:58:00 | [diff] [blame] | 307 | |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 308 | #if defined(OS_WIN) |
| 309 | base::FilePath file(base::UTF8ToWide(path)); |
dbeam | e3081ca | 2016-06-01 02:28:05 | [diff] [blame] | 310 | std::string extension = base::WideToUTF8(file.FinalExtension()); |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 311 | #else |
| 312 | base::FilePath file(path); |
dbeam | e3081ca | 2016-06-01 02:28:05 | [diff] [blame] | 313 | std::string extension = file.FinalExtension(); |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 314 | #endif |
| 315 | |
dbeam | e3081ca | 2016-06-01 02:28:05 | [diff] [blame] | 316 | if (!extension.empty()) |
| 317 | extension.erase(0, 1); |
| 318 | |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 319 | if (extension == "html") |
| 320 | return "text/html"; |
| 321 | |
| 322 | if (extension == "css") |
| 323 | return "text/css"; |
| 324 | |
| 325 | if (extension == "js") |
| 326 | return "application/javascript"; |
| 327 | |
| 328 | if (extension == "png") |
| 329 | return "image/png"; |
| 330 | |
| 331 | if (extension == "gif") |
| 332 | return "image/gif"; |
| 333 | |
| 334 | if (extension == "svg") |
| 335 | return "image/svg+xml"; |
| 336 | |
| 337 | if (extension == "woff2") |
| 338 | return "application/font-woff2"; |
| 339 | |
dbeam | e3081ca | 2016-06-01 02:28:05 | [diff] [blame] | 340 | NOTREACHED() << path; |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 341 | return "text/plain"; |
| 342 | } |
| 343 | |
dpapad | f9becc9 | 2019-01-15 03:39:29 | [diff] [blame] | 344 | bool SharedResourcesDataSource::ShouldServeMimeTypeAsContentTypeHeader() const { |
| 345 | return true; |
| 346 | } |
| 347 | |
gab | 143ea62 | 2016-11-03 12:47:00 | [diff] [blame] | 348 | scoped_refptr<base::SingleThreadTaskRunner> |
| 349 | SharedResourcesDataSource::TaskRunnerForRequestPath( |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 350 | const std::string& path) const { |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 351 | // Since WebContentsGetter can only be run on the UI thread, always return |
| 352 | // a task runner if we need to choose between Polymer resources based on the |
| 353 | // WebContents that is requesting the resource. |
| 354 | // TODO (rbpotter): Remove this once the OOBE Polymer 2 migration is complete. |
| 355 | #if defined(OS_CHROMEOS) |
| 356 | if (UsingMultiplePolymerVersions()) |
| 357 | return base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI}); |
| 358 | #endif // defined(OS_CHROMEOS) |
| 359 | |
tzik | bd171b1 | 2017-02-23 04:24:04 | [diff] [blame] | 360 | int idr = GetIdrForPath(path); |
| 361 | if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS || |
| 362 | idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) { |
| 363 | // Use UI thread to load CSS since its construction touches non-thread-safe |
| 364 | // gfx::Font names in ui::ResourceBundle. |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 365 | return base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::UI}); |
tzik | bd171b1 | 2017-02-23 04:24:04 | [diff] [blame] | 366 | } |
| 367 | |
dbeam | 8678179 | 2016-04-30 03:26:27 | [diff] [blame] | 368 | return nullptr; |
[email protected] | 755e1b73 | 2010-06-24 23:28:53 | [diff] [blame] | 369 | } |
dzhioev | fd3b257 | 2014-10-10 18:55:45 | [diff] [blame] | 370 | |
| 371 | std::string |
| 372 | SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( |
| 373 | const std::string& origin) const { |
| 374 | // For now we give access only for "chrome://*" origins. |
| 375 | // According to CORS spec, Access-Control-Allow-Origin header doesn't support |
| 376 | // wildcards, so we need to set its value explicitly by passing the |origin| |
| 377 | // back. |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 378 | std::string allowed_origin_prefix = kChromeUIScheme; |
dzhioev | fd3b257 | 2014-10-10 18:55:45 | [diff] [blame] | 379 | allowed_origin_prefix += "://"; |
markusheintz | 74e10b2 | 2016-07-08 13:19:36 | [diff] [blame] | 380 | if (!base::StartsWith(origin, allowed_origin_prefix, |
| 381 | base::CompareCase::SENSITIVE)) { |
sammc | f01aed196 | 2014-12-18 06:35:28 | [diff] [blame] | 382 | return "null"; |
markusheintz | 74e10b2 | 2016-07-08 13:19:36 | [diff] [blame] | 383 | } |
dzhioev | fd3b257 | 2014-10-10 18:55:45 | [diff] [blame] | 384 | return origin; |
| 385 | } |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 386 | |
yzshen | 1a479778 | 2017-06-09 08:11:33 | [diff] [blame] | 387 | bool SharedResourcesDataSource::IsGzipped(const std::string& path) const { |
Dan Beam | 78cd41e3 | 2017-12-21 04:23:02 | [diff] [blame] | 388 | auto it = GetResourcesMap().find(path); |
| 389 | DCHECK(it != GetResourcesMap().end()) << "missing shared resource: " << path; |
| 390 | return it != GetResourcesMap().end() ? it->second.gzipped : false; |
yzshen | 1a479778 | 2017-06-09 08:11:33 | [diff] [blame] | 391 | } |
| 392 | |
rbpotter | 7d574ca | 2019-01-30 22:16:17 | [diff] [blame] | 393 | #if defined(OS_CHROMEOS) |
| 394 | void SharedResourcesDataSource::DisablePolymer2ForHost( |
| 395 | const std::string& host) { |
| 396 | DCHECK(disabled_polymer2_host_.empty() || host == disabled_polymer2_host_); |
| 397 | disabled_polymer2_host_ = host; |
| 398 | } |
| 399 | |
| 400 | // Returns true if the WebContents making the request has disabled Polymer 2. |
| 401 | bool SharedResourcesDataSource::IsPolymer2DisabledForPage( |
| 402 | const ResourceRequestInfo::WebContentsGetter& wc_getter) { |
| 403 | // Return false in these cases, which sometimes occur in tests. |
| 404 | if (!wc_getter) |
| 405 | return false; |
| 406 | |
| 407 | content::WebContents* web_contents = wc_getter.Run(); |
| 408 | if (!web_contents) |
| 409 | return false; |
| 410 | |
| 411 | return web_contents->GetLastCommittedURL().host_piece() == |
| 412 | disabled_polymer2_host_; |
| 413 | } |
| 414 | #endif // defined(OS_CHROMEOS) |
dbeam | 41c9c72 | 2014-11-26 03:59:06 | [diff] [blame] | 415 | } // namespace content |