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