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

Side by Side Diff: chrome/browser/ui/webui/theme_source.cc

Issue 2886973003: ThemeSource: simplify thread hopping for NTP CSS (Closed)
Patch Set: Created 3 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 | « 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 "chrome/browser/ui/webui/theme_source.h" 5 #include "chrome/browser/ui/webui/theme_source.h"
6 6
7 #include "base/memory/ref_counted_memory.h" 7 #include "base/memory/ref_counted_memory.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 55
56 void ProcessResourceOnUiThread(int resource_id, 56 void ProcessResourceOnUiThread(int resource_id,
57 float scale, 57 float scale,
58 scoped_refptr<base::RefCountedBytes> data) { 58 scoped_refptr<base::RefCountedBytes> data) {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
60 ProcessImageOnUiThread( 60 ProcessImageOnUiThread(
61 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id), 61 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id),
62 scale, data); 62 scale, data);
63 } 63 }
64 64
65 base::RefCountedMemory* GetNewTabCSSOnUiThread(Profile* profile) {
66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
67
68 NTPResourceCache::WindowType type =
69 NTPResourceCache::GetWindowType(profile, nullptr);
70 return NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(type);
71 }
72
73 } // namespace 65 } // namespace
74 66
75 //////////////////////////////////////////////////////////////////////////////// 67 ////////////////////////////////////////////////////////////////////////////////
76 // ThemeSource, public: 68 // ThemeSource, public:
77 69
78 ThemeSource::ThemeSource(Profile* profile) : profile_(profile) {} 70 ThemeSource::ThemeSource(Profile* profile) : profile_(profile) {}
79 71
80 ThemeSource::~ThemeSource() { 72 ThemeSource::~ThemeSource() = default;
81 }
82 73
83 std::string ThemeSource::GetSource() const { 74 std::string ThemeSource::GetSource() const {
84 return chrome::kChromeUIThemeHost; 75 return chrome::kChromeUIThemeHost;
85 } 76 }
86 77
87 void ThemeSource::StartDataRequest( 78 void ThemeSource::StartDataRequest(
88 const std::string& path, 79 const std::string& path,
89 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, 80 const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
90 const content::URLDataSource::GotDataCallback& callback) { 81 const content::URLDataSource::GotDataCallback& callback) {
91 // Default scale factor if not specified. 82 // Default scale factor if not specified.
92 float scale = 1.0f; 83 float scale = 1.0f;
93 std::string parsed_path; 84 std::string parsed_path;
94 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, &scale); 85 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, &scale);
95 86
96 if (IsNewTabCssPath(parsed_path)) { 87 if (IsNewTabCssPath(parsed_path)) {
97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
98 // NB: it's important that this is |profile_| and not |original_profile_|. 89 NTPResourceCache::WindowType type =
99 content::BrowserThread::PostTaskAndReplyWithResult( 90 NTPResourceCache::GetWindowType(profile_, /*render_host=*/nullptr);
100 content::BrowserThread::UI, FROM_HERE, 91 NTPResourceCache* cache = NTPResourceCacheFactory::GetForProfile(profile_);
101 base::Bind(&GetNewTabCSSOnUiThread, profile_), callback); 92 callback.Run(cache->GetNewTabCSS(type));
102 return; 93 return;
103 } 94 }
104 95
105 int resource_id = -1; 96 int resource_id = -1;
106 if (parsed_path == "current-channel-logo") { 97 if (parsed_path == "current-channel-logo") {
107 switch (chrome::GetChannel()) { 98 switch (chrome::GetChannel()) {
108 #if defined(GOOGLE_CHROME_BUILD) 99 #if defined(GOOGLE_CHROME_BUILD)
109 case version_info::Channel::CANARY: 100 case version_info::Channel::CANARY:
110 resource_id = IDR_PRODUCT_LOGO_32_CANARY; 101 resource_id = IDR_PRODUCT_LOGO_32_CANARY;
111 break; 102 break;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, nullptr); 151 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, nullptr);
161 return IsNewTabCssPath(parsed_path) ? "text/css" : "image/png"; 152 return IsNewTabCssPath(parsed_path) ? "text/css" : "image/png";
162 } 153 }
163 154
164 scoped_refptr<base::SingleThreadTaskRunner> 155 scoped_refptr<base::SingleThreadTaskRunner>
165 ThemeSource::TaskRunnerForRequestPath(const std::string& path) const { 156 ThemeSource::TaskRunnerForRequestPath(const std::string& path) const {
166 std::string parsed_path; 157 std::string parsed_path;
167 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, nullptr); 158 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, nullptr);
168 159
169 if (IsNewTabCssPath(parsed_path)) { 160 if (IsNewTabCssPath(parsed_path)) {
170 // We generated and cached this when we initialized the object. We don't 161 // We'll get this data from the NTPResourceCache, which must be accessed on
171 // have to go back to the UI thread to send the data. 162 // the UI thread.
172 return nullptr; 163 return content::URLDataSource::TaskRunnerForRequestPath(path);
173 } 164 }
174 165
175 // If it's not a themeable image, we don't need to go to the UI thread. 166 // If it's not a themeable image, we don't need to go to the UI thread.
176 int resource_id = ResourcesUtil::GetThemeResourceId(parsed_path); 167 int resource_id = ResourcesUtil::GetThemeResourceId(parsed_path);
177 return BrowserThemePack::IsPersistentImageID(resource_id) 168 return BrowserThemePack::IsPersistentImageID(resource_id)
178 ? content::URLDataSource::TaskRunnerForRequestPath(path) 169 ? content::URLDataSource::TaskRunnerForRequestPath(path)
179 : nullptr; 170 : nullptr;
180 } 171 }
181 172
182 bool ThemeSource::AllowCaching() const { 173 bool ThemeSource::AllowCaching() const {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 } else { 220 } else {
230 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 221 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
231 // Fetching image data in ResourceBundle should happen on the UI thread. See 222 // Fetching image data in ResourceBundle should happen on the UI thread. See
232 // crbug.com/449277 223 // crbug.com/449277
233 content::BrowserThread::PostTaskAndReply( 224 content::BrowserThread::PostTaskAndReply(
234 content::BrowserThread::UI, FROM_HERE, 225 content::BrowserThread::UI, FROM_HERE,
235 base::BindOnce(&ProcessResourceOnUiThread, resource_id, scale, data), 226 base::BindOnce(&ProcessResourceOnUiThread, resource_id, scale, data),
236 base::BindOnce(callback, data)); 227 base::BindOnce(callback, data));
237 } 228 }
238 } 229 }
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