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

Side by Side Diff: content/browser/webui/shared_resources_data_source.cc

Issue 1932753006: Figure out the MIME type of shared WebUI resources more efficiently (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: asdf Created 4 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
« no previous file with comments | « content/browser/webui/shared_resources_data_source.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/webui/shared_resources_data_source.h" 5 #include "content/browser/webui/shared_resources_data_source.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/containers/hash_tables.h" 9 #include "base/containers/hash_tables.h"
10 #include "base/files/file_path.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
12 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
13 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "content/public/common/content_client.h" 15 #include "content/public/common/content_client.h"
16 #include "content/public/common/url_constants.h" 16 #include "content/public/common/url_constants.h"
17 #include "net/base/mime_util.h"
18 #include "ui/base/layout.h" 17 #include "ui/base/layout.h"
19 #include "ui/base/webui/web_ui_util.h" 18 #include "ui/base/webui/web_ui_util.h"
20 #include "ui/resources/grit/webui_resources.h" 19 #include "ui/resources/grit/webui_resources.h"
21 #include "ui/resources/grit/webui_resources_map.h" 20 #include "ui/resources/grit/webui_resources_map.h"
22 21
22 #if defined(OS_WIN)
23 #include "base/strings/utf_string_conversions.h"
24 #endif
25
23 namespace content { 26 namespace content {
24 27
25 namespace { 28 namespace {
26 29
27 using ResourcesMap = base::hash_map<std::string, int>; 30 using ResourcesMap = base::hash_map<std::string, int>;
28 31
29 // TODO(rkc): Once we have a separate source for apps, remove '*/apps/' aliases. 32 // TODO(rkc): Once we have a separate source for apps, remove '*/apps/' aliases.
30 const char* const kPathAliases[][2] = { 33 const char* const kPathAliases[][2] = {
31 {"../../../third_party/polymer/v1_0/components-chromium/", "polymer/v1_0/"}, 34 {"../../../third_party/polymer/v1_0/components-chromium/", "polymer/v1_0/"},
32 {"../../../third_party/web-animations-js/sources/", 35 {"../../../third_party/web-animations-js/sources/",
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 bytes = base::RefCountedString::TakeString(&css); 100 bytes = base::RefCountedString::TakeString(&css);
98 } else { 101 } else {
99 bytes = GetContentClient()->GetDataResourceBytes(idr); 102 bytes = GetContentClient()->GetDataResourceBytes(idr);
100 } 103 }
101 104
102 callback.Run(bytes.get()); 105 callback.Run(bytes.get());
103 } 106 }
104 107
105 std::string SharedResourcesDataSource::GetMimeType( 108 std::string SharedResourcesDataSource::GetMimeType(
106 const std::string& path) const { 109 const std::string& path) const {
107 // Requests should not block on the disk! On POSIX this goes to disk. 110 if (path.empty())
108 // http://code.google.com/p/chromium/issues/detail?id=59849 111 return "text/html";
109 112
110 base::ThreadRestrictions::ScopedAllowIO allow_io; 113 #if defined(OS_WIN)
111 std::string mime_type; 114 base::FilePath file(base::UTF8ToWide(path));
112 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type); 115 std::string extension = base::WideToUTF8(file.FinalExtension()).substr(1);
113 return mime_type; 116 #else
117 base::FilePath file(path);
118 std::string extension = file.FinalExtension().substr(1);
119 #endif
120
121 if (extension == "html")
122 return "text/html";
123
124 if (extension == "css")
125 return "text/css";
126
127 if (extension == "js")
128 return "application/javascript";
129
130 if (extension == "png")
131 return "image/png";
132
133 if (extension == "gif")
134 return "image/gif";
135
136 if (extension == "svg")
137 return "image/svg+xml";
138
139 if (extension == "woff2")
140 return "application/font-woff2";
141
142 CHECK(false) << path;
143 return "text/plain";
144 }
145
146 base::MessageLoop* SharedResourcesDataSource::MessageLoopForRequestPath(
147 const std::string& path) const {
148 return nullptr;
114 } 149 }
115 150
116 std::string 151 std::string
117 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( 152 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
118 const std::string& origin) const { 153 const std::string& origin) const {
119 // For now we give access only for "chrome://*" origins. 154 // For now we give access only for "chrome://*" origins.
120 // According to CORS spec, Access-Control-Allow-Origin header doesn't support 155 // According to CORS spec, Access-Control-Allow-Origin header doesn't support
121 // wildcards, so we need to set its value explicitly by passing the |origin| 156 // wildcards, so we need to set its value explicitly by passing the |origin|
122 // back. 157 // back.
123 std::string allowed_origin_prefix = kChromeUIScheme; 158 std::string allowed_origin_prefix = kChromeUIScheme;
124 allowed_origin_prefix += "://"; 159 allowed_origin_prefix += "://";
125 if (origin.find(allowed_origin_prefix) != 0) 160 if (origin.find(allowed_origin_prefix) != 0)
126 return "null"; 161 return "null";
127 return origin; 162 return origin;
128 } 163 }
129 164
130 } // namespace content 165 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/webui/shared_resources_data_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698