Commit c9ff196e authored by mukai's avatar mukai Committed by Commit bot

Make sure ResourceBundle::GetImageSkiaNamed running on UI thread.

Because ImageSkia is not thread-safe, we should restrict
the caller thread of GetImageSkiaNamed() to the UI thread only.

BUG=449277
R=oshima@chromium.org, xiyuan@chromium.org
TEST=manually

Review URL: https://codereview.chromium.org/874483007

Cr-Commit-Position: refs/heads/master@{#312991}
parent a3523bc7
...@@ -40,6 +40,24 @@ std::string GetThemePath() { ...@@ -40,6 +40,24 @@ std::string GetThemePath() {
static const char* kNewTabCSSPath = "css/new_tab_theme.css"; static const char* kNewTabCSSPath = "css/new_tab_theme.css";
static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css";
void ProcessImageOnUIThread(const gfx::ImageSkia& image,
float scale_factor,
scoped_refptr<base::RefCountedBytes> data) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale_factor);
gfx::PNGCodec::EncodeBGRASkBitmap(
rep.sk_bitmap(), false /* discard transparency */, &data->data());
}
void ProcessResourceOnUIThread(int resource_id,
float scale_factor,
scoped_refptr<base::RefCountedBytes> data) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ProcessImageOnUIThread(
*ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id),
scale_factor, data);
}
} // namespace } // namespace
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
...@@ -179,24 +197,23 @@ void ThemeSource::SendThemeImage( ...@@ -179,24 +197,23 @@ void ThemeSource::SendThemeImage(
// rescale the bitmap if its backend doesn't contain the representation for // rescale the bitmap if its backend doesn't contain the representation for
// the specified scale factor. This is the fallback path in case chrome is // the specified scale factor. This is the fallback path in case chrome is
// shipped without 2x resource pack but needs to use HighDPI display, which // shipped without 2x resource pack but needs to use HighDPI display, which
// can happen in ChromeOS. // can happen in ChromeOS or Linux.
// TODO(mukai): remove this method itself when we ship 2x resource to all scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
// ChromeOS devices.
gfx::ImageSkia image;
if (BrowserThemePack::IsPersistentImageID(resource_id)) { if (BrowserThemePack::IsPersistentImageID(resource_id)) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
DCHECK(tp); DCHECK(tp);
image = *tp->GetImageSkiaNamed(resource_id); ProcessImageOnUIThread(*tp->GetImageSkiaNamed(resource_id), scale_factor,
data);
callback.Run(data.get());
} else { } else {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
image = *rb.GetImageSkiaNamed(resource_id); // Fetching image data in ResourceBundle should happen on the UI thread. See
// crbug.com/449277
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&ProcessResourceOnUIThread, resource_id, scale_factor, data),
base::Bind(callback, data));
} }
const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale_factor);
scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
gfx::PNGCodec::EncodeBGRASkBitmap(
rep.sk_bitmap(), false /* discard transparency */, &data->data());
callback.Run(data.get());
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment