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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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) { | |
Evan Stade
2016/10/03 20:16:59
nit: style guide says NewTabCssOnUiThread
Alexei Svitkine (slow)
2016/10/03 21:02:45
Done - changed the other function in this file too
| |
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) |
71 : profile_(profile->GetOriginalProfile()) { | 79 : profile_(profile), |
72 // NB: it's important that this is |profile| and not |profile_|. | 80 original_profile_(profile->GetOriginalProfile()) { |
Evan Stade
2016/10/03 20:16:59
why not just inline profile_->GetOriginalProfile w
Alexei Svitkine (slow)
2016/10/03 21:02:45
Done.
| |
73 NTPResourceCache::WindowType win_type = | |
74 NTPResourceCache::GetWindowType(profile, nullptr); | |
75 css_bytes_ = | |
76 NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(win_type); | |
77 } | 81 } |
78 | 82 |
79 ThemeSource::~ThemeSource() { | 83 ThemeSource::~ThemeSource() { |
80 } | 84 } |
81 | 85 |
82 std::string ThemeSource::GetSource() const { | 86 std::string ThemeSource::GetSource() const { |
83 return chrome::kChromeUIThemeHost; | 87 return chrome::kChromeUIThemeHost; |
84 } | 88 } |
85 | 89 |
86 void ThemeSource::StartDataRequest( | 90 void ThemeSource::StartDataRequest( |
87 const std::string& path, | 91 const std::string& path, |
88 int render_process_id, | 92 int render_process_id, |
89 int render_frame_id, | 93 int render_frame_id, |
90 const content::URLDataSource::GotDataCallback& callback) { | 94 const content::URLDataSource::GotDataCallback& callback) { |
91 // Default scale factor if not specified. | 95 // Default scale factor if not specified. |
92 float scale = 1.0f; | 96 float scale = 1.0f; |
93 std::string parsed_path; | 97 std::string parsed_path; |
94 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, &scale); | 98 webui::ParsePathAndScale(GetThemeUrl(path), &parsed_path, &scale); |
95 | 99 |
96 if (IsNewTabCssPath(parsed_path)) { | 100 if (IsNewTabCssPath(parsed_path)) { |
97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
98 callback.Run(css_bytes_.get()); | 102 // NB: it's important that this is |profile_| and not |original_profile_|. |
103 content::BrowserThread::PostTaskAndReplyWithResult( | |
104 content::BrowserThread::UI, FROM_HERE, | |
105 base::Bind(&GetNewTabCSSOnUIThread, profile_), | |
106 callback); | |
99 return; | 107 return; |
100 } | 108 } |
101 | 109 |
102 int resource_id = -1; | 110 int resource_id = -1; |
103 if (parsed_path == "current-channel-logo") { | 111 if (parsed_path == "current-channel-logo") { |
104 switch (chrome::GetChannel()) { | 112 switch (chrome::GetChannel()) { |
105 #if defined(GOOGLE_CHROME_BUILD) | 113 #if defined(GOOGLE_CHROME_BUILD) |
106 case version_info::Channel::CANARY: | 114 case version_info::Channel::CANARY: |
107 resource_id = IDR_PRODUCT_LOGO_32_CANARY; | 115 resource_id = IDR_PRODUCT_LOGO_32_CANARY; |
108 break; | 116 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. | 176 // have to go back to the UI thread to send the data. |
169 return nullptr; | 177 return nullptr; |
170 } | 178 } |
171 | 179 |
172 // If it's not a themeable image, we don't need to go to the UI thread. | 180 // 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); | 181 int resource_id = ResourcesUtil::GetThemeResourceId(parsed_path); |
174 return BrowserThemePack::IsPersistentImageID(resource_id) ? | 182 return BrowserThemePack::IsPersistentImageID(resource_id) ? |
175 content::URLDataSource::MessageLoopForRequestPath(path) : nullptr; | 183 content::URLDataSource::MessageLoopForRequestPath(path) : nullptr; |
176 } | 184 } |
177 | 185 |
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 { | 186 bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const { |
185 return request->url().SchemeIs(chrome::kChromeSearchScheme) ? | 187 return request->url().SchemeIs(chrome::kChromeSearchScheme) ? |
186 InstantIOContext::ShouldServiceRequest(request) : | 188 InstantIOContext::ShouldServiceRequest(request) : |
187 URLDataSource::ShouldServiceRequest(request); | 189 URLDataSource::ShouldServiceRequest(request); |
188 } | 190 } |
189 | 191 |
190 //////////////////////////////////////////////////////////////////////////////// | 192 //////////////////////////////////////////////////////////////////////////////// |
191 // ThemeSource, private: | 193 // ThemeSource, private: |
192 | 194 |
193 void ThemeSource::SendThemeBitmap( | 195 void ThemeSource::SendThemeBitmap( |
194 const content::URLDataSource::GotDataCallback& callback, | 196 const content::URLDataSource::GotDataCallback& callback, |
195 int resource_id, | 197 int resource_id, |
196 float scale) { | 198 float scale) { |
197 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 199 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
198 if (BrowserThemePack::IsPersistentImageID(resource_id)) { | 200 if (BrowserThemePack::IsPersistentImageID(resource_id)) { |
199 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
200 scoped_refptr<base::RefCountedMemory> image_data( | 202 scoped_refptr<base::RefCountedMemory> image_data( |
201 ThemeService::GetThemeProviderForProfile(profile_).GetRawData( | 203 ThemeService::GetThemeProviderForProfile(original_profile_).GetRawData( |
202 resource_id, scale_factor)); | 204 resource_id, scale_factor)); |
203 callback.Run(image_data.get()); | 205 callback.Run(image_data.get()); |
204 } else { | 206 } else { |
205 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 207 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
206 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 208 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
207 callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); | 209 callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); |
208 } | 210 } |
209 } | 211 } |
210 | 212 |
211 void ThemeSource::SendThemeImage( | 213 void ThemeSource::SendThemeImage( |
212 const content::URLDataSource::GotDataCallback& callback, | 214 const content::URLDataSource::GotDataCallback& callback, |
213 int resource_id, | 215 int resource_id, |
214 float scale) { | 216 float scale) { |
215 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); | 217 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
216 if (BrowserThemePack::IsPersistentImageID(resource_id)) { | 218 if (BrowserThemePack::IsPersistentImageID(resource_id)) { |
217 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
218 const ui::ThemeProvider& tp = | 220 const ui::ThemeProvider& tp = |
219 ThemeService::GetThemeProviderForProfile(profile_); | 221 ThemeService::GetThemeProviderForProfile(original_profile_); |
220 ProcessImageOnUIThread(*tp.GetImageSkiaNamed(resource_id), scale, data); | 222 ProcessImageOnUIThread(*tp.GetImageSkiaNamed(resource_id), scale, data); |
221 callback.Run(data.get()); | 223 callback.Run(data.get()); |
222 } else { | 224 } else { |
223 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 225 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
224 // Fetching image data in ResourceBundle should happen on the UI thread. See | 226 // Fetching image data in ResourceBundle should happen on the UI thread. See |
225 // crbug.com/449277 | 227 // crbug.com/449277 |
226 content::BrowserThread::PostTaskAndReply( | 228 content::BrowserThread::PostTaskAndReply( |
227 content::BrowserThread::UI, FROM_HERE, | 229 content::BrowserThread::UI, FROM_HERE, |
228 base::Bind(&ProcessResourceOnUIThread, resource_id, scale, data), | 230 base::Bind(&ProcessResourceOnUIThread, resource_id, scale, data), |
229 base::Bind(callback, data)); | 231 base::Bind(callback, data)); |
230 } | 232 } |
231 } | 233 } |
OLD | NEW |