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 "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/resources_util.h" | 11 #include "chrome/browser/resources_util.h" |
11 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
12 #include "chrome/browser/themes/theme_service_factory.h" | 13 #include "chrome/browser/themes/theme_service_factory.h" |
13 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" | 14 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" |
14 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" | 15 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" |
15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
18 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/base/theme_provider.h" | 20 #include "ui/base/theme_provider.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 | 23 |
| 24 namespace { |
| 25 |
23 // use a resource map rather than hard-coded strings. | 26 // use a resource map rather than hard-coded strings. |
24 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; | 27 static const char* kNewTabCSSPath = "css/new_tab_theme.css"; |
25 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; | 28 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css"; |
26 | 29 |
27 static std::string StripQueryParams(const std::string& path) { | 30 std::string StripQueryParams(const std::string& path) { |
28 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" + | 31 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" + |
29 std::string(chrome::kChromeUIThemePath) + "/" + path); | 32 std::string(chrome::kChromeUIThemePath) + "/" + path); |
30 return path_url.path().substr(1); // path() always includes a leading '/'. | 33 return path_url.path().substr(1); // path() always includes a leading '/'. |
31 } | 34 } |
32 | 35 |
| 36 std::string ParsePathAndScale(const std::string& path, float* scale_factor) { |
| 37 // Our path may include cachebuster arguments, so trim them off. |
| 38 std::string uncached_path = StripQueryParams(path); |
| 39 |
| 40 // Detect and parse resource string ending in @<scale>x. |
| 41 std::size_t pos = uncached_path.rfind('@'); |
| 42 if (pos != std::string::npos && |
| 43 uncached_path[uncached_path.length()-1] == 'x') { |
| 44 double scale_d; |
| 45 if (scale_factor && |
| 46 base::StringToDouble( |
| 47 uncached_path.substr(pos + 1, uncached_path.length() - pos - 2), |
| 48 &scale_d)) { |
| 49 *scale_factor = scale_d; |
| 50 } |
| 51 // Strip scale factor specification from path. |
| 52 uncached_path = uncached_path.substr(0, pos); |
| 53 } |
| 54 return uncached_path; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
33 //////////////////////////////////////////////////////////////////////////////// | 59 //////////////////////////////////////////////////////////////////////////////// |
34 // ThemeSource, public: | 60 // ThemeSource, public: |
35 | 61 |
36 ThemeSource::ThemeSource(Profile* profile) | 62 ThemeSource::ThemeSource(Profile* profile) |
37 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), | 63 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), |
38 profile_(profile->GetOriginalProfile()) { | 64 profile_(profile->GetOriginalProfile()) { |
39 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( | 65 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( |
40 profile->IsOffTheRecord()); | 66 profile->IsOffTheRecord()); |
41 } | 67 } |
42 | 68 |
43 ThemeSource::~ThemeSource() { | 69 ThemeSource::~ThemeSource() { |
44 } | 70 } |
45 | 71 |
46 void ThemeSource::StartDataRequest(const std::string& path, | 72 void ThemeSource::StartDataRequest(const std::string& path, |
47 bool is_incognito, | 73 bool is_incognito, |
48 int request_id) { | 74 int request_id) { |
49 // Our path may include cachebuster arguments, so trim them off. | 75 // Default scale factor if not specified. |
50 std::string uncached_path = StripQueryParams(path); | 76 float scale_factor = 1; |
| 77 std::string uncached_path = ParsePathAndScale(path, &scale_factor); |
51 | 78 |
52 if (uncached_path == kNewTabCSSPath || | 79 if (uncached_path == kNewTabCSSPath || |
53 uncached_path == kNewIncognitoTabCSSPath) { | 80 uncached_path == kNewIncognitoTabCSSPath) { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
55 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || | 82 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || |
56 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); | 83 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); |
57 | 84 |
58 SendResponse(request_id, css_bytes_); | 85 SendResponse(request_id, css_bytes_); |
59 return; | 86 return; |
60 } else { | 87 } else { |
61 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 88 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
62 if (resource_id != -1) { | 89 if (resource_id != -1) { |
63 SendThemeBitmap(request_id, resource_id); | 90 SendThemeBitmap(request_id, resource_id, scale_factor); |
64 return; | 91 return; |
65 } | 92 } |
66 } | 93 } |
67 // We don't have any data to send back. | 94 // We don't have any data to send back. |
68 SendResponse(request_id, NULL); | 95 SendResponse(request_id, NULL); |
69 } | 96 } |
70 | 97 |
71 std::string ThemeSource::GetMimeType(const std::string& path) const { | 98 std::string ThemeSource::GetMimeType(const std::string& path) const { |
72 std::string uncached_path = StripQueryParams(path); | 99 std::string uncached_path = ParsePathAndScale(path, NULL); |
73 | 100 |
74 if (uncached_path == kNewTabCSSPath || | 101 if (uncached_path == kNewTabCSSPath || |
75 uncached_path == kNewIncognitoTabCSSPath) { | 102 uncached_path == kNewIncognitoTabCSSPath) { |
76 return "text/css"; | 103 return "text/css"; |
77 } | 104 } |
78 | 105 |
79 return "image/png"; | 106 return "image/png"; |
80 } | 107 } |
81 | 108 |
82 MessageLoop* ThemeSource::MessageLoopForRequestPath( | 109 MessageLoop* ThemeSource::MessageLoopForRequestPath( |
83 const std::string& path) const { | 110 const std::string& path) const { |
84 std::string uncached_path = StripQueryParams(path); | 111 std::string uncached_path = ParsePathAndScale(path, NULL); |
85 | 112 |
86 if (uncached_path == kNewTabCSSPath || | 113 if (uncached_path == kNewTabCSSPath || |
87 uncached_path == kNewIncognitoTabCSSPath) { | 114 uncached_path == kNewIncognitoTabCSSPath) { |
88 // We generated and cached this when we initialized the object. We don't | 115 // We generated and cached this when we initialized the object. We don't |
89 // have to go back to the UI thread to send the data. | 116 // have to go back to the UI thread to send the data. |
90 return NULL; | 117 return NULL; |
91 } | 118 } |
92 | 119 |
93 // If it's not a themeable image, we don't need to go to the UI thread. | 120 // If it's not a themeable image, we don't need to go to the UI thread. |
94 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); | 121 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); |
95 if (!ThemeService::IsThemeableImage(resource_id)) | 122 if (!ThemeService::IsThemeableImage(resource_id)) |
96 return NULL; | 123 return NULL; |
97 | 124 |
98 return DataSource::MessageLoopForRequestPath(path); | 125 return DataSource::MessageLoopForRequestPath(path); |
99 } | 126 } |
100 | 127 |
101 bool ThemeSource::ShouldReplaceExistingSource() const { | 128 bool ThemeSource::ShouldReplaceExistingSource() const { |
102 // We currently get the css_bytes_ in the ThemeSource constructor, so we need | 129 // We currently get the css_bytes_ in the ThemeSource constructor, so we need |
103 // to recreate the source itself when a theme changes. | 130 // to recreate the source itself when a theme changes. |
104 return true; | 131 return true; |
105 } | 132 } |
106 | 133 |
107 //////////////////////////////////////////////////////////////////////////////// | 134 //////////////////////////////////////////////////////////////////////////////// |
108 // ThemeSource, private: | 135 // ThemeSource, private: |
109 | 136 |
110 void ThemeSource::SendThemeBitmap(int request_id, int resource_id) { | 137 void ThemeSource::SendThemeBitmap(int request_id, |
| 138 int resource_id, |
| 139 float scale_factor) { |
111 if (ThemeService::IsThemeableImage(resource_id)) { | 140 if (ThemeService::IsThemeableImage(resource_id)) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
113 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 142 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
114 DCHECK(tp); | 143 DCHECK(tp); |
115 | 144 |
| 145 // TODO(flackr): Pass scale factor when fetching themeable images. |
116 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( | 146 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( |
117 resource_id)); | 147 resource_id)); |
118 SendResponse(request_id, image_data); | 148 SendResponse(request_id, image_data); |
119 } else { | 149 } else { |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
121 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 151 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
122 SendResponse(request_id, rb.LoadDataResourceBytes(resource_id)); | 152 SendResponse(request_id, |
| 153 rb.LoadImageResourceBytes(resource_id, scale_factor)); |
123 } | 154 } |
124 } | 155 } |
OLD | NEW |