Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: chrome/browser/ui/webui/theme_source.cc

Issue 10387010: Select theme resources from ResourceBundle at requested scale factor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with master. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
19 #include "ui/base/layout.h"
18 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.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 struct ScaleFactorMap {
32 const char* name;
33 ui::ScaleFactor scale_factor;
34 };
35
36 const ScaleFactorMap kScaleFactorMap[] = {
37 { "1x", ui::SCALE_FACTOR_100P },
38 { "2x", ui::SCALE_FACTOR_200P },
39 };
40
41 std::string StripQueryParams(const std::string& path) {
28 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" + 42 GURL path_url = GURL(std::string(chrome::kChromeUIScheme) + "://" +
29 std::string(chrome::kChromeUIThemePath) + "/" + path); 43 std::string(chrome::kChromeUIThemePath) + "/" + path);
30 return path_url.path().substr(1); // path() always includes a leading '/'. 44 return path_url.path().substr(1); // path() always includes a leading '/'.
31 } 45 }
32 46
47 std::string ParsePathAndScale(const std::string& path,
48 ui::ScaleFactor* scale_factor) {
49 // Our path may include cachebuster arguments, so trim them off.
sky 2012/05/16 14:21:21 Set *scale_factor up here so that its always set.
flackr 2012/05/16 14:37:12 Done.
50 std::string uncached_path = StripQueryParams(path);
51
52 // Detect and parse resource string ending in @<scale>x.
53 std::size_t pos = uncached_path.rfind('@');
54 if (pos != std::string::npos) {
55 if (scale_factor) {
56 for (size_t i = 0; i < arraysize(kScaleFactorMap); i++) {
57 if (uncached_path.compare(pos + 1, uncached_path.length() - pos - 1,
58 kScaleFactorMap[i].name) == 0) {
59 *scale_factor = kScaleFactorMap[i].scale_factor;
60 }
61 }
62 }
63 // Strip scale factor specification from path.
64 uncached_path = uncached_path.substr(0, pos);
65 }
66 return uncached_path;
67 }
68
69 } // namespace
70
33 //////////////////////////////////////////////////////////////////////////////// 71 ////////////////////////////////////////////////////////////////////////////////
34 // ThemeSource, public: 72 // ThemeSource, public:
35 73
36 ThemeSource::ThemeSource(Profile* profile) 74 ThemeSource::ThemeSource(Profile* profile)
37 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()), 75 : DataSource(chrome::kChromeUIThemePath, MessageLoop::current()),
38 profile_(profile->GetOriginalProfile()) { 76 profile_(profile->GetOriginalProfile()) {
39 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS( 77 css_bytes_ = NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(
40 profile->IsOffTheRecord()); 78 profile->IsOffTheRecord());
41 } 79 }
42 80
43 ThemeSource::~ThemeSource() { 81 ThemeSource::~ThemeSource() {
44 } 82 }
45 83
46 void ThemeSource::StartDataRequest(const std::string& path, 84 void ThemeSource::StartDataRequest(const std::string& path,
47 bool is_incognito, 85 bool is_incognito,
48 int request_id) { 86 int request_id) {
49 // Our path may include cachebuster arguments, so trim them off. 87 // Default scale factor if not specified.
50 std::string uncached_path = StripQueryParams(path); 88 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P;
89 std::string uncached_path = ParsePathAndScale(path, &scale_factor);
51 90
52 if (uncached_path == kNewTabCSSPath || 91 if (uncached_path == kNewTabCSSPath ||
53 uncached_path == kNewIncognitoTabCSSPath) { 92 uncached_path == kNewIncognitoTabCSSPath) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
55 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) || 94 DCHECK((uncached_path == kNewTabCSSPath && !is_incognito) ||
56 (uncached_path == kNewIncognitoTabCSSPath && is_incognito)); 95 (uncached_path == kNewIncognitoTabCSSPath && is_incognito));
57 96
58 SendResponse(request_id, css_bytes_); 97 SendResponse(request_id, css_bytes_);
59 return; 98 return;
60 } else { 99 } else {
61 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path); 100 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
62 if (resource_id != -1) { 101 if (resource_id != -1) {
63 SendThemeBitmap(request_id, resource_id); 102 SendThemeBitmap(request_id, resource_id, scale_factor);
64 return; 103 return;
65 } 104 }
66 } 105 }
67 // We don't have any data to send back. 106 // We don't have any data to send back.
68 SendResponse(request_id, NULL); 107 SendResponse(request_id, NULL);
69 } 108 }
70 109
71 std::string ThemeSource::GetMimeType(const std::string& path) const { 110 std::string ThemeSource::GetMimeType(const std::string& path) const {
72 std::string uncached_path = StripQueryParams(path); 111 std::string uncached_path = ParsePathAndScale(path, NULL);
73 112
74 if (uncached_path == kNewTabCSSPath || 113 if (uncached_path == kNewTabCSSPath ||
75 uncached_path == kNewIncognitoTabCSSPath) { 114 uncached_path == kNewIncognitoTabCSSPath) {
76 return "text/css"; 115 return "text/css";
77 } 116 }
78 117
79 return "image/png"; 118 return "image/png";
80 } 119 }
81 120
82 MessageLoop* ThemeSource::MessageLoopForRequestPath( 121 MessageLoop* ThemeSource::MessageLoopForRequestPath(
83 const std::string& path) const { 122 const std::string& path) const {
84 std::string uncached_path = StripQueryParams(path); 123 std::string uncached_path = ParsePathAndScale(path, NULL);
85 124
86 if (uncached_path == kNewTabCSSPath || 125 if (uncached_path == kNewTabCSSPath ||
87 uncached_path == kNewIncognitoTabCSSPath) { 126 uncached_path == kNewIncognitoTabCSSPath) {
88 // We generated and cached this when we initialized the object. We don't 127 // 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. 128 // have to go back to the UI thread to send the data.
90 return NULL; 129 return NULL;
91 } 130 }
92 131
93 // If it's not a themeable image, we don't need to go to the UI thread. 132 // 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); 133 int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
95 if (!ThemeService::IsThemeableImage(resource_id)) 134 if (!ThemeService::IsThemeableImage(resource_id))
96 return NULL; 135 return NULL;
97 136
98 return DataSource::MessageLoopForRequestPath(path); 137 return DataSource::MessageLoopForRequestPath(path);
99 } 138 }
100 139
101 bool ThemeSource::ShouldReplaceExistingSource() const { 140 bool ThemeSource::ShouldReplaceExistingSource() const {
102 // We currently get the css_bytes_ in the ThemeSource constructor, so we need 141 // We currently get the css_bytes_ in the ThemeSource constructor, so we need
103 // to recreate the source itself when a theme changes. 142 // to recreate the source itself when a theme changes.
104 return true; 143 return true;
105 } 144 }
106 145
107 //////////////////////////////////////////////////////////////////////////////// 146 ////////////////////////////////////////////////////////////////////////////////
108 // ThemeSource, private: 147 // ThemeSource, private:
109 148
110 void ThemeSource::SendThemeBitmap(int request_id, int resource_id) { 149 void ThemeSource::SendThemeBitmap(int request_id,
150 int resource_id,
151 ui::ScaleFactor scale_factor) {
111 if (ThemeService::IsThemeableImage(resource_id)) { 152 if (ThemeService::IsThemeableImage(resource_id)) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 154 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
114 DCHECK(tp); 155 DCHECK(tp);
115 156
157 // TODO(flackr): Pass scale factor when fetching themeable images.
116 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData( 158 scoped_refptr<base::RefCountedMemory> image_data(tp->GetRawData(
117 resource_id)); 159 resource_id));
118 SendResponse(request_id, image_data); 160 SendResponse(request_id, image_data);
119 } else { 161 } else {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
121 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 163 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
122 SendResponse(request_id, rb.LoadDataResourceBytes(resource_id)); 164 SendResponse(request_id,
165 rb.LoadDataResourceBytes(resource_id, scale_factor));
123 } 166 }
124 } 167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698