| 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" |
| 14 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 15 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
| 15 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 16 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
| 16 #include "chrome/browser/ui/webui/web_ui_util.h" | 17 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 17 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 20 #include "ui/base/layout.h" | 21 #include "ui/base/layout.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/base/theme_provider.h" | 23 #include "ui/base/theme_provider.h" |
| 23 | 24 |
| 24 using content::BrowserThread; | 25 using content::BrowserThread; |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 std::string GetThemePath() { | 29 std::string GetThemePath() { |
| 29 return std::string(chrome::kChromeUIScheme) + | 30 return std::string(chrome::kChromeUIScheme) + |
| 30 "://" + std::string(chrome::kChromeUIThemePath) + "/"; | 31 "://" + std::string(chrome::kChromeUIThemePath) + "/"; |
| 31 } | 32 } |
| 32 | 33 |
| 33 // use a resource map rather than hard-coded strings. | 34 // use a resource map rather than hard-coded strings. |
| 34 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; | 35 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; |
| 35 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; | 36 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; |
| 36 | 37 |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 39 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
| 40 // ThemeSource, public: | 41 // ThemeSource, public: |
| 41 | 42 |
| 42 ThemeSource::ThemeSource(Profile* profile) | 43 ThemeSource::ThemeSource(Profile* profile) |
| 43 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), | 44 : profile_(profile->GetOriginalProfile()) { |
| 44 profile_(profile->GetOriginalProfile()) { | |
| 45 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( | 45 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( |
| 46 profile->IsOffTheRecord()); | 46 profile->IsOffTheRecord()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 ThemeSource::~ThemeSource() { | 49 ThemeSource::~ThemeSource() { |
| 50 } | 50 } |
| 51 | 51 |
| 52 std::string ThemeSource::GetSource() { |
| 53 return chrome::kChromeUIThemePath; |
| 54 } |
| 55 |
| 52 void ThemeSource::StartDataRequest(const std::string& path, | 56 void ThemeSource::StartDataRequest(const std::string& path, |
| 53 bool is_incognito, | 57 bool is_incognito, |
| 54 int request_id) { | 58 int request_id) { |
| 55 // Default scale factor if not specified. | 59 // Default scale factor if not specified. |
| 56 ui::ScaleFactor scale_factor; | 60 ui::ScaleFactor scale_factor; |
| 57 std::string uncached_path; | 61 std::string uncached_path; |
| 58 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), | 62 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), |
| 59 &uncached_path, | 63 &uncached_path, |
| 60 &scale_factor); | 64 &scale_factor); |
| 61 | 65 |
| 62 if (uncached_path == kNewTabCSSPath || | 66 if (uncached_path == kNewTabCSSPath || |
| 63 uncached_path == kNewIncognitoTabCSSPath) { | 67 uncached_path == kNewIncognitoTabCSSPath) { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || | 69 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || |
| 66 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); | 70 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); |
| 67 | 71 |
| 68 SendResponse(request_id, css_bytes_); | 72 url_data_source()->SendResponse(request_id, css_bytes_); |
| 69 return; | 73 return; |
| 70 } else { | |
| 71 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | |
| 72 if (resource_id != -1) { | |
| 73 SendThemeBitmap(request_id, resource_id, scale_factor); | |
| 74 return; | |
| 75 } | |
| 76 } | 74 } |
| 75 |
| 76 |
| 77 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 78 if (resource_id != -1) { |
| 79 SendThemeBitmap(request_id, resource_id, scale_factor); |
| 80 return; |
| 81 } |
| 82 |
| 77 // We don't have any data to send back. | 83 // We don't have any data to send back. |
| 78 SendResponse(request_id, NULL); | 84 url_data_source()->SendResponse(request_id, NULL); |
| 79 } | 85 } |
| 80 | 86 |
| 81 std::string ThemeSource::GetMimeType(const std::string& path) const { | 87 std::string ThemeSource::GetMimeType(const std::string& path) const { |
| 82 std::string uncached_path; | 88 std::string uncached_path; |
| 83 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), | 89 web_ui_util::ParsePathAndScale(GURL(GetThemePath() + path), |
| 84 &uncached_path, NULL); | 90 &uncached_path, NULL); |
| 85 | 91 |
| 86 if (uncached_path == kNewTabCSSPath || | 92 if (uncached_path == kNewTabCSSPath || |
| 87 uncached_path == kNewIncognitoTabCSSPath) { | 93 uncached_path == kNewIncognitoTabCSSPath) { |
| 88 return "text/css"; | 94 return "text/css"; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 102 // 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 |
| 103 // 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. |
| 104 return NULL; | 110 return NULL; |
| 105 } | 111 } |
| 106 | 112 |
| 107 // 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. |
| 108 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 114 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
| 109 if (!ThemeService::IsThemeableImage(resource_id)) | 115 if (!ThemeService::IsThemeableImage(resource_id)) |
| 110 return NULL; | 116 return NULL; |
| 111 | 117 |
| 112 return DataSource::MessageLoopForRequestPath(path); | 118 return content::URLDataSourceDelegate::MessageLoopForRequestPath(path); |
| 113 } | 119 } |
| 114 | 120 |
| 115 bool ThemeSource::ShouldReplaceExistingSource() const { | 121 bool ThemeSource::ShouldReplaceExistingSource() const { |
| 116 // 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 |
| 117 // to recreate the source itself when a theme changes. | 123 // to recreate the source itself when a theme changes. |
| 118 return true; | 124 return true; |
| 119 } | 125 } |
| 120 | 126 |
| 121 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
| 122 // ThemeSource, private: | 128 // ThemeSource, private: |
| 123 | 129 |
| 124 void ThemeSource::SendThemeBitmap(int request_id, | 130 void ThemeSource::SendThemeBitmap(int request_id, |
| 125 int resource_id, | 131 int resource_id, |
| 126 ui::ScaleFactor scale_factor) { | 132 ui::ScaleFactor scale_factor) { |
| 127 if (ThemeService::IsThemeableImage(resource_id)) { | 133 if (ThemeService::IsThemeableImage(resource_id)) { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 129 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 135 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 130 DCHECK(tp); | 136 DCHECK(tp); |
| 131 | 137 |
| 132 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( | 138 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( |
| 133 resource_id, scale_factor)); | 139 resource_id, scale_factor)); |
| 134 SendResponse(request_id, image_data); | 140 url_data_source()->SendResponse(request_id, image_data); |
| 135 } else { | 141 } else { |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 137 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 143 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 138 SendResponse( | 144 url_data_source()->SendResponse( |
| 139 request_id, | 145 request_id, |
| 140 rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); | 146 rb.LoadDataResourceBytesForScale(resource_id, scale_factor)); |
| 141 } | 147 } |
| 142 } | 148 } |
| OLD | NEW |