OLD | NEW |
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 26 matching lines...) Expand all Loading... |
37 std::string(chrome::kChromeUIThemeHost) + "/" + path); | 37 std::string(chrome::kChromeUIThemeHost) + "/" + path); |
38 } | 38 } |
39 | 39 |
40 bool IsNewTabCssPath(const std::string& path) { | 40 bool IsNewTabCssPath(const std::string& path) { |
41 static const char kNewTabCSSPath[] = "css/new_tab_theme.css"; | 41 static const char kNewTabCSSPath[] = "css/new_tab_theme.css"; |
42 static const char kIncognitoNewTabCSSPath[] = | 42 static const char kIncognitoNewTabCSSPath[] = |
43 "css/incognito_new_tab_theme.css"; | 43 "css/incognito_new_tab_theme.css"; |
44 return (path == kNewTabCSSPath) || (path == kIncognitoNewTabCSSPath); | 44 return (path == kNewTabCSSPath) || (path == kIncognitoNewTabCSSPath); |
45 } | 45 } |
46 | 46 |
47 void ProcessImageOnUIThread(const gfx::ImageSkia& image, | 47 void ProcessImageOnUiThread(const gfx::ImageSkia& image, |
48 float scale, | 48 float scale, |
49 scoped_refptr<base::RefCountedBytes> data) { | 49 scoped_refptr<base::RefCountedBytes> data) { |
50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
51 const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale); | 51 const gfx::ImageSkiaRep& rep = image.GetRepresentation(scale); |
52 gfx::PNGCodec::EncodeBGRASkBitmap( | 52 gfx::PNGCodec::EncodeBGRASkBitmap( |
53 rep.sk_bitmap(), false /* discard transparency */, &data->data()); | 53 rep.sk_bitmap(), false /* discard transparency */, &data->data()); |
54 } | 54 } |
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 |
65 } // namespace | 73 } // namespace |
66 | 74 |
67 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
68 // ThemeSource, public: | 76 // ThemeSource, public: |
69 | 77 |
70 ThemeSource::ThemeSource(Profile* profile) | 78 ThemeSource::ThemeSource(Profile* profile) : profile_(profile) {} |
71 : profile_(profile->GetOriginalProfile()) { | |
72 // NB: it's important that this is |profile| and not |profile_|. | |
73 NTPResourceCache::WindowType win_type = | |
74 NTPResourceCache::GetWindowType(profile, nullptr); | |
75 css_bytes_ = | |
76 NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(win_type); | |
77 } | |
78 | 79 |
79 ThemeSource::~ThemeSource() { | 80 ThemeSource::~ThemeSource() { |
80 } | 81 } |
81 | 82 |
82 std::string ThemeSource::GetSource() const { | 83 std::string ThemeSource::GetSource() const { |
83 return chrome::kChromeUIThemeHost; | 84 return chrome::kChromeUIThemeHost; |
84 } | 85 } |
85 | 86 |
86 void ThemeSource::StartDataRequest( | 87 void ThemeSource::StartDataRequest( |
87 const std::string& path, | 88 const std::string& path, |
88 int render_process_id, | 89 int render_process_id, |
89 int render_frame_id, | 90 int render_frame_id, |
90 const content::URLDataSource::GotDataCallback& callback) { | 91 const content::URLDataSource::GotDataCallback& callback) { |
91 // Default scale factor if not specified. | 92 // Default scale factor if not specified. |
92 float scale = 1.0f; | 93 float scale = 1.0f; |
93 std::string parsed_path; | 94 std::string parsed_path; |
94 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, &scale); | 95 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, &scale); |
95 | 96 |
96 if (IsNewTabCssPath(parsed_path)) { | 97 if (IsNewTabCssPath(parsed_path)) { |
97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
98 callback.Run(css_bytes_.get()); | 99 // NB: it's important that this is |profile_| and not |original_profile_|. |
| 100 content::BrowserThread::PostTaskAndReplyWithResult( |
| 101 content::BrowserThread::UI, FROM_HERE, |
| 102 base::Bind(&GetNewTabCSSOnUiThread, profile_), callback); |
99 return; | 103 return; |
100 } | 104 } |
101 | 105 |
102 int resource_id = -1; | 106 int resource_id = -1; |
103 if (parsed_path == "current-channel-logo") { | 107 if (parsed_path == "current-channel-logo") { |
104 switch (chrome::GetChannel()) { | 108 switch (chrome::GetChannel()) { |
105 #if defined(GOOGLE_CHROME_BUILD) | 109 #if defined(GOOGLE_CHROME_BUILD) |
106 case version_info::Channel::CANARY: | 110 case version_info::Channel::CANARY: |
107 resource_id = IDR_PRODUCT_LOGO_32_CANARY; | 111 resource_id = IDR_PRODUCT_LOGO_32_CANARY; |
108 break; | 112 break; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 // have to go back to the UI thread to send the data. | 172 // have to go back to the UI thread to send the data. |
169 return nullptr; | 173 return nullptr; |
170 } | 174 } |
171 | 175 |
172 // If it's not a themeable image, we don't need to go to the UI thread. | 176 // If it's not a themeable image, we don't need to go to the UI thread. |
173 int resource_id = ResourcesUtil::GetThemeResourceId(parsed_path); | 177 int resource_id = ResourcesUtil::GetThemeResourceId(parsed_path); |
174 return BrowserThemePack::IsPersistentImageID(resource_id) ? | 178 return BrowserThemePack::IsPersistentImageID(resource_id) ? |
175 content::URLDataSource::MessageLoopForRequestPath(path) : nullptr; | 179 content::URLDataSource::MessageLoopForRequestPath(path) : nullptr; |
176 } | 180 } |
177 | 181 |
178 bool ThemeSource::ShouldReplaceExistingSource() const { | |
179 // We currently get the css_bytes_ in the ThemeSource constructor, so we need | |
180 // to recreate the source itself when a theme changes. | |
181 return true; | |
182 } | |
183 | |
184 bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const { | 182 bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const { |
185 return request->url().SchemeIs(chrome::kChromeSearchScheme) ? | 183 return request->url().SchemeIs(chrome::kChromeSearchScheme) ? |
186 InstantIOContext::ShouldServiceRequest(request) : | 184 InstantIOContext::ShouldServiceRequest(request) : |
187 URLDataSource::ShouldServiceRequest(request); | 185 URLDataSource::ShouldServiceRequest(request); |
188 } | 186 } |
189 | 187 |
190 //////////////////////////////////////////////////////////////////////////////// | 188 //////////////////////////////////////////////////////////////////////////////// |
191 // ThemeSource, private: | 189 // ThemeSource, private: |
192 | 190 |
193 void ThemeSource::SendThemeBitmap( | 191 void ThemeSource::SendThemeBitmap( |
194 const content::URLDataSource::GotDataCallback& callback, | 192 const content::URLDataSource::GotDataCallback& callback, |
195 int resource_id, | 193 int resource_id, |
196 float scale) { | 194 float scale) { |
197 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 195 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
198 if (BrowserThemePack::IsPersistentImageID(resource_id)) { | 196 if (BrowserThemePack::IsPersistentImageID(resource_id)) { |
199 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
200 scoped_refptr<base::RefCountedMemory> image_data( | 198 scoped_refptr<base::RefCountedMemory> image_data( |
201 ThemeService::GetThemeProviderForProfile(profile_).GetRawData( | 199 ThemeService::GetThemeProviderForProfile(profile_->GetOriginalProfile()) |
202 resource_id, scale_factor)); | 200 .GetRawData(resource_id, scale_factor)); |
203 callback.Run(image_data.get()); | 201 callback.Run(image_data.get()); |
204 } else { | 202 } else { |
205 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 203 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
206 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 204 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
207 callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); | 205 callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); |
208 } | 206 } |
209 } | 207 } |
210 | 208 |
211 void ThemeSource::SendThemeImage( | 209 void ThemeSource::SendThemeImage( |
212 const content::URLDataSource::GotDataCallback& callback, | 210 const content::URLDataSource::GotDataCallback& callback, |
213 int resource_id, | 211 int resource_id, |
214 float scale) { | 212 float scale) { |
215 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); | 213 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
216 if (BrowserThemePack::IsPersistentImageID(resource_id)) { | 214 if (BrowserThemePack::IsPersistentImageID(resource_id)) { |
217 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 215 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
218 const ui::ThemeProvider& tp = | 216 const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile( |
219 ThemeService::GetThemeProviderForProfile(profile_); | 217 profile_->GetOriginalProfile()); |
220 ProcessImageOnUIThread(*tp.GetImageSkiaNamed(resource_id), scale, data); | 218 ProcessImageOnUiThread(*tp.GetImageSkiaNamed(resource_id), scale, data); |
221 callback.Run(data.get()); | 219 callback.Run(data.get()); |
222 } else { | 220 } else { |
223 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 221 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
224 // 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 |
225 // crbug.com/449277 | 223 // crbug.com/449277 |
226 content::BrowserThread::PostTaskAndReply( | 224 content::BrowserThread::PostTaskAndReply( |
227 content::BrowserThread::UI, FROM_HERE, | 225 content::BrowserThread::UI, FROM_HERE, |
228 base::Bind(&ProcessResourceOnUIThread, resource_id, scale, data), | 226 base::Bind(&ProcessResourceOnUiThread, resource_id, scale, data), |
229 base::Bind(callback, data)); | 227 base::Bind(callback, data)); |
230 } | 228 } |
231 } | 229 } |
OLD | NEW |