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

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: rebase (swapped CL sequence order) 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/bind.h"
9 #include "base/containers/hash_tables.h" 10 #include "base/containers/hash_tables.h"
10 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h" 13 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "content/public/browser/browser_thread.h"
15 #include "content/public/common/content_client.h" 17 #include "content/public/common/content_client.h"
16 #include "content/public/common/url_constants.h" 18 #include "content/public/common/url_constants.h"
17 #include "ui/base/layout.h" 19 #include "ui/base/layout.h"
18 #include "ui/base/webui/web_ui_util.h" 20 #include "ui/base/webui/web_ui_util.h"
19 #include "ui/resources/grit/webui_resources.h" 21 #include "ui/resources/grit/webui_resources.h"
20 #include "ui/resources/grit/webui_resources_map.h" 22 #include "ui/resources/grit/webui_resources_map.h"
21 23
22 #if defined(OS_WIN) 24 #if defined(OS_WIN)
23 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
24 #endif 26 #endif
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 64
63 return result; 65 return result;
64 } 66 }
65 67
66 const ResourcesMap& GetResourcesMap() { 68 const ResourcesMap& GetResourcesMap() {
67 // This pointer will be intentionally leaked on shutdown. 69 // This pointer will be intentionally leaked on shutdown.
68 static const ResourcesMap* resources_map = CreateResourcesMap(); 70 static const ResourcesMap* resources_map = CreateResourcesMap();
69 return *resources_map; 71 return *resources_map;
70 } 72 }
71 73
74 scoped_refptr<base::RefCountedMemory> GetResourceBytesOnUIThread(int idr) {
75 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) {
76 std::string css = webui::GetWebUiCssTextDefaults();
77 return base::RefCountedString::TakeString(&css);
78 }
79
80 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) {
81 std::string css = webui::GetWebUiCssTextDefaultsMd();
82 return base::RefCountedString::TakeString(&css);
83 }
84
85 return GetContentClient()->GetDataResourceBytes(idr);
86 }
87
72 } // namespace 88 } // namespace
73 89
74 SharedResourcesDataSource::SharedResourcesDataSource() { 90 SharedResourcesDataSource::SharedResourcesDataSource() {
75 } 91 }
76 92
77 SharedResourcesDataSource::~SharedResourcesDataSource() { 93 SharedResourcesDataSource::~SharedResourcesDataSource() {
78 } 94 }
79 95
80 std::string SharedResourcesDataSource::GetSource() const { 96 std::string SharedResourcesDataSource::GetSource() const {
81 return kChromeUIResourcesHost; 97 return kChromeUIResourcesHost;
82 } 98 }
83 99
84 void SharedResourcesDataSource::StartDataRequest( 100 void SharedResourcesDataSource::StartDataRequest(
85 const std::string& path, 101 const std::string& path,
86 const ResourceRequestInfo::WebContentsGetter& wc_getter, 102 const ResourceRequestInfo::WebContentsGetter& wc_getter,
87 const URLDataSource::GotDataCallback& callback) { 103 const URLDataSource::GotDataCallback& callback) {
88 const ResourcesMap& resources_map = GetResourcesMap(); 104 const ResourcesMap& resources_map = GetResourcesMap();
89 auto it = resources_map.find(path); 105 auto it = resources_map.find(path);
90 int idr = (it != resources_map.end()) ? it->second : -1; 106 int idr = (it != resources_map.end()) ? it->second : -1;
91 DCHECK_NE(-1, idr) << " path: " << path; 107 DCHECK_NE(-1, idr) << " path: " << path;
92 scoped_refptr<base::RefCountedMemory> bytes;
93 108
94 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) { 109 BrowserThread::PostTaskAndReplyWithResult(
95 std::string css = webui::GetWebUiCssTextDefaults(); 110 BrowserThread::UI, FROM_HERE,
96 bytes = base::RefCountedString::TakeString(&css); 111 base::Bind(&GetResourceBytesOnUIThread, idr), callback);
97 } else if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) {
98 std::string css = webui::GetWebUiCssTextDefaultsMd();
99 bytes = base::RefCountedString::TakeString(&css);
100 } else {
101 bytes = GetContentClient()->GetDataResourceBytes(idr);
102 }
103
104 callback.Run(bytes.get());
105 } 112 }
106 113
107 std::string SharedResourcesDataSource::GetMimeType( 114 std::string SharedResourcesDataSource::GetMimeType(
108 const std::string& path) const { 115 const std::string& path) const {
109 if (path.empty()) 116 if (path.empty())
110 return "text/html"; 117 return "text/html";
111 118
112 #if defined(OS_WIN) 119 #if defined(OS_WIN)
113 base::FilePath file(base::UTF8ToWide(path)); 120 base::FilePath file(base::UTF8ToWide(path));
114 std::string extension = base::WideToUTF8(file.FinalExtension()); 121 std::string extension = base::WideToUTF8(file.FinalExtension());
(...skipping 26 matching lines...) Expand all
141 if (extension == "woff2") 148 if (extension == "woff2")
142 return "application/font-woff2"; 149 return "application/font-woff2";
143 150
144 NOTREACHED() << path; 151 NOTREACHED() << path;
145 return "text/plain"; 152 return "text/plain";
146 } 153 }
147 154
148 scoped_refptr<base::SingleThreadTaskRunner> 155 scoped_refptr<base::SingleThreadTaskRunner>
149 SharedResourcesDataSource::TaskRunnerForRequestPath( 156 SharedResourcesDataSource::TaskRunnerForRequestPath(
150 const std::string& path) const { 157 const std::string& path) const {
151 return nullptr; 158 return nullptr;
Dan Beam 2017/02/22 22:56:10 i think you should only return nullptr when GetMim
tzik 2017/02/22 23:47:02 Sounds nice! Updated. Let me use "text/css" since
152 } 159 }
153 160
154 std::string 161 std::string
155 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( 162 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
156 const std::string& origin) const { 163 const std::string& origin) const {
157 // For now we give access only for "chrome://*" origins. 164 // For now we give access only for "chrome://*" origins.
158 // According to CORS spec, Access-Control-Allow-Origin header doesn't support 165 // 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| 166 // wildcards, so we need to set its value explicitly by passing the |origin|
160 // back. 167 // back.
161 std::string allowed_origin_prefix = kChromeUIScheme; 168 std::string allowed_origin_prefix = kChromeUIScheme;
162 allowed_origin_prefix += "://"; 169 allowed_origin_prefix += "://";
163 if (!base::StartsWith(origin, allowed_origin_prefix, 170 if (!base::StartsWith(origin, allowed_origin_prefix,
164 base::CompareCase::SENSITIVE)) { 171 base::CompareCase::SENSITIVE)) {
165 return "null"; 172 return "null";
166 } 173 }
167 return origin; 174 return origin;
168 } 175 }
169 176
170 } // namespace content 177 } // 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