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

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

Issue 2701183002: Avoid touching ResourceBundle fonts from non-UI thread (Closed)
Patch Set: . Created 3 years, 10 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 | « no previous file | 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/files/file_path.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "content/public/browser/browser_thread.h"
15 #include "content/public/common/content_client.h" 16 #include "content/public/common/content_client.h"
16 #include "content/public/common/url_constants.h" 17 #include "content/public/common/url_constants.h"
17 #include "ui/base/layout.h" 18 #include "ui/base/layout.h"
18 #include "ui/base/webui/web_ui_util.h" 19 #include "ui/base/webui/web_ui_util.h"
19 #include "ui/resources/grit/webui_resources.h" 20 #include "ui/resources/grit/webui_resources.h"
20 #include "ui/resources/grit/webui_resources_map.h" 21 #include "ui/resources/grit/webui_resources_map.h"
21 22
22 #if defined(OS_WIN) 23 #if defined(OS_WIN)
23 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
24 #endif 25 #endif
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 63
63 return result; 64 return result;
64 } 65 }
65 66
66 const ResourcesMap& GetResourcesMap() { 67 const ResourcesMap& GetResourcesMap() {
67 // This pointer will be intentionally leaked on shutdown. 68 // This pointer will be intentionally leaked on shutdown.
68 static const ResourcesMap* resources_map = CreateResourcesMap(); 69 static const ResourcesMap* resources_map = CreateResourcesMap();
69 return *resources_map; 70 return *resources_map;
70 } 71 }
71 72
73 int GetIDRForPath(const std::string& path) {
Dan Beam 2017/02/23 02:11:30 GetIdrForPath()
tzik 2017/02/23 03:31:40 Done.
74 const ResourcesMap& resources_map = GetResourcesMap();
75 auto it = resources_map.find(path);
76 return (it != resources_map.end()) ? it->second : -1;
Dan Beam 2017/02/23 02:11:30 nit: return it != resources_map.end() ? it->second
tzik 2017/02/23 03:31:40 Done.
77 }
78
72 } // namespace 79 } // namespace
73 80
74 SharedResourcesDataSource::SharedResourcesDataSource() { 81 SharedResourcesDataSource::SharedResourcesDataSource() {
75 } 82 }
76 83
77 SharedResourcesDataSource::~SharedResourcesDataSource() { 84 SharedResourcesDataSource::~SharedResourcesDataSource() {
78 } 85 }
79 86
80 std::string SharedResourcesDataSource::GetSource() const { 87 std::string SharedResourcesDataSource::GetSource() const {
81 return kChromeUIResourcesHost; 88 return kChromeUIResourcesHost;
82 } 89 }
83 90
84 void SharedResourcesDataSource::StartDataRequest( 91 void SharedResourcesDataSource::StartDataRequest(
85 const std::string& path, 92 const std::string& path,
86 const ResourceRequestInfo::WebContentsGetter& wc_getter, 93 const ResourceRequestInfo::WebContentsGetter& wc_getter,
87 const URLDataSource::GotDataCallback& callback) { 94 const URLDataSource::GotDataCallback& callback) {
88 const ResourcesMap& resources_map = GetResourcesMap(); 95 int idr = GetIDRForPath(path);
89 auto it = resources_map.find(path);
90 int idr = (it != resources_map.end()) ? it->second : -1;
91 DCHECK_NE(-1, idr) << " path: " << path; 96 DCHECK_NE(-1, idr) << " path: " << path;
92 scoped_refptr<base::RefCountedMemory> bytes; 97 scoped_refptr<base::RefCountedMemory> bytes;
93 98
94 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) { 99 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) {
95 std::string css = webui::GetWebUiCssTextDefaults(); 100 std::string css = webui::GetWebUiCssTextDefaults();
96 bytes = base::RefCountedString::TakeString(&css); 101 bytes = base::RefCountedString::TakeString(&css);
97 } else if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) { 102 } else if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) {
98 std::string css = webui::GetWebUiCssTextDefaultsMd(); 103 std::string css = webui::GetWebUiCssTextDefaultsMd();
99 bytes = base::RefCountedString::TakeString(&css); 104 bytes = base::RefCountedString::TakeString(&css);
100 } else { 105 } else {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 if (extension == "woff2") 146 if (extension == "woff2")
142 return "application/font-woff2"; 147 return "application/font-woff2";
143 148
144 NOTREACHED() << path; 149 NOTREACHED() << path;
145 return "text/plain"; 150 return "text/plain";
146 } 151 }
147 152
148 scoped_refptr<base::SingleThreadTaskRunner> 153 scoped_refptr<base::SingleThreadTaskRunner>
149 SharedResourcesDataSource::TaskRunnerForRequestPath( 154 SharedResourcesDataSource::TaskRunnerForRequestPath(
150 const std::string& path) const { 155 const std::string& path) const {
156 int idr = GetIDRForPath(path);
157 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS ||
158 idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) {
159 // Use UI thread to load CSS since its construction touches non-thread-safe
160 // gfx::Font names in ui::ResourceBundle.
161 return URLDataSource::TaskRunnerForRequestPath(path);
162 }
163
151 return nullptr; 164 return nullptr;
152 } 165 }
153 166
154 std::string 167 std::string
155 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( 168 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
156 const std::string& origin) const { 169 const std::string& origin) const {
157 // For now we give access only for "chrome://*" origins. 170 // For now we give access only for "chrome://*" origins.
158 // According to CORS spec, Access-Control-Allow-Origin header doesn't support 171 // According to CORS spec, Access-Control-Allow-Origin header doesn't support
159 // wildcards, so we need to set its value explicitly by passing the |origin| 172 // wildcards, so we need to set its value explicitly by passing the |origin|
160 // back. 173 // back.
161 std::string allowed_origin_prefix = kChromeUIScheme; 174 std::string allowed_origin_prefix = kChromeUIScheme;
162 allowed_origin_prefix += "://"; 175 allowed_origin_prefix += "://";
163 if (!base::StartsWith(origin, allowed_origin_prefix, 176 if (!base::StartsWith(origin, allowed_origin_prefix,
164 base::CompareCase::SENSITIVE)) { 177 base::CompareCase::SENSITIVE)) {
165 return "null"; 178 return "null";
166 } 179 }
167 return origin; 180 return origin;
168 } 181 }
169 182
170 } // namespace content 183 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698