| 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.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/resources_util.h" | 11 #include "chrome/browser/resources_util.h" |
| 12 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
| 13 #include "chrome/browser/themes/theme_service_factory.h" | 13 #include "chrome/browser/themes/theme_service_factory.h" |
| 14 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 15 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 14 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
| 16 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 15 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
| 17 #include "chrome/browser/ui/webui/web_ui_util.h" | 16 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 18 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 19 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 20 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 21 #include "ui/base/layout.h" | 20 #include "ui/base/layout.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/base/theme_provider.h" | 22 #include "ui/base/theme_provider.h" |
| 24 | 23 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 46 profile->IsOffTheRecord()); | 45 profile->IsOffTheRecord()); |
| 47 } | 46 } |
| 48 | 47 |
| 49 ThemeSource::~ThemeSource() { | 48 ThemeSource::~ThemeSource() { |
| 50 } | 49 } |
| 51 | 50 |
| 52 std::string ThemeSource::GetSource() { | 51 std::string ThemeSource::GetSource() { |
| 53 return chrome::kChromeUIThemePath; | 52 return chrome::kChromeUIThemePath; |
| 54 } | 53 } |
| 55 | 54 |
| 56 void ThemeSource::StartDataRequest(const std::string& path, | 55 void ThemeSource::StartDataRequest( |
| 57 bool is_incognito, | 56 const std::string& path, |
| 58 int request_id) { | 57 bool is_incognito, |
| 58 const content::URLDataSource::GotDataCallback& callback) { |
| 59 // Default scale factor if not specified. | 59 // Default scale factor if not specified. |
| 60 ui::ScaleFactor scale_factor; | 60 ui::ScaleFactor scale_factor; |
| 61 std::string uncached_path; | 61 std::string uncached_path; |
| 62 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), | 62 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), |
| 63 &uncached_path, | 63 &uncached_path, |
| 64 &scale_factor); | 64 &scale_factor); |
| 65 | 65 |
| 66 if (uncached_path == kNewTabCSSPath || | 66 if (uncached_path == kNewTabCSSPath || |
| 67 uncached_path == kNewIncognitoTabCSSPath) { | 67 uncached_path == kNewIncognitoTabCSSPath) { |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 69 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || | 69 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || |
| 70 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); | 70 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); |
| 71 | 71 |
| 72 url_data_source()->SendResponse(request_id, css_bytes_); | 72 callback.Run(css_bytes_); |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 77 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 78 if (resource_id != -1) { | 78 if (resource_id != -1) { |
| 79 SendThemeBitmap(request_id, resource_id, scale_factor); | 79 SendThemeBitmap(callback, resource_id, scale_factor); |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // We don't have any data to send back. | 83 // We don't have any data to send back. |
| 84 url_data_source()->SendResponse(request_id, NULL); | 84 callback.Run(NULL); |
| 85 } | 85 } |
| 86 | 86 |
| 87 std::string ThemeSource::GetMimeType(const std::string& path) const { | 87 std::string ThemeSource::GetMimeType(const std::string& path) const { |
| 88 std::string uncached_path; | 88 std::string uncached_path; |
| 89 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), | 89 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), |
| 90 &uncached_path, NULL); | 90 &uncached_path, NULL); |
| 91 | 91 |
| 92 if (uncached_path == kNewTabCSSPath || | 92 if (uncached_path == kNewTabCSSPath || |
| 93 uncached_path == kNewIncognitoTabCSSPath) { | 93 uncached_path == kNewIncognitoTabCSSPath) { |
| 94 return "text/css"; | 94 return "text/css"; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 108 // We generated and cached this when we initialized the object. We don't | 108 // We generated and cached this when we initialized the object. We don't |
| 109 // have to go back to the UI thread to send the data. | 109 // have to go back to the UI thread to send the data. |
| 110 return NULL; | 110 return NULL; |
| 111 } | 111 } |
| 112 | 112 |
| 113 // If it's not a themeable image, we don't need to go to the UI thread. | 113 // If it's not a themeable image, we don't need to go to the UI thread. |
| 114 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 114 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 115 if (!ThemeService::IsThemeableImage(resource_id)) | 115 if (!ThemeService::IsThemeableImage(resource_id)) |
| 116 return NULL; | 116 return NULL; |
| 117 | 117 |
| 118 return content::URLDataSourceDelegate::MessageLoopForRequestPath(path); | 118 return content::URLDataSource::MessageLoopForRequestPath(path); |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool ThemeSource::ShouldReplaceExistingSource() const { | 121 bool ThemeSource::ShouldReplaceExistingSource() const { |
| 122 // We currently get the css_bytes_ in the ThemeSource constructor, so we need | 122 // We currently get the css_bytes_ in the ThemeSource constructor, so we need |
| 123 // to recreate the source itself when a theme changes. | 123 // to recreate the source itself when a theme changes. |
| 124 return true; | 124 return true; |
| 125 } | 125 } |
| 126 | 126 |
| 127 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
| 128 // ThemeSource, private: | 128 // ThemeSource, private: |
| 129 | 129 |
| 130 void ThemeSource::SendThemeBitmap(int request_id, | 130 void ThemeSource::SendThemeBitmap( |
| 131 int resource_id, | 131 const content::URLDataSource::GotDataCallback& callback, |
| 132 ui::ScaleFactor scale_factor) { | 132 int resource_id, |
| 133 ui::ScaleFactor scale_factor) { |
| 133 if (ThemeService::IsThemeableImage(resource_id)) { | 134 if (ThemeService::IsThemeableImage(resource_id)) { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 135 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 136 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 136 DCHECK(tp); | 137 DCHECK(tp); |
| 137 | 138 |
| 138 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( | 139 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( |
| 139 resource_id, scale_factor)); | 140 resource_id, scale_factor)); |
| 140 url_data_source()->SendResponse(request_id, image_data); | 141 callback.Run(image_data); |
| 141 } else { | 142 } else { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 143 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 144 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 144 url_data_source()->SendResponse( | 145 callback.Run(rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); |
| 145 request_id, | |
| 146 rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); | |
| 147 } | 146 } |
| 148 } | 147 } |
| OLD | NEW |