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/themes/browser_theme_pack.h" | 5 #include "chrome/browser/themes/browser_theme_pack.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 using content::BrowserThread; | 43 using content::BrowserThread; |
44 using extensions::Extension; | 44 using extensions::Extension; |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
48 // Version number of the current theme pack. We just throw out and rebuild | 48 // Version number of the current theme pack. We just throw out and rebuild |
49 // theme packs that aren't int-equal to this. Increment this number if you | 49 // theme packs that aren't int-equal to this. Increment this number if you |
50 // change default theme assets or if you need themes to recreate their generated | 50 // change default theme assets or if you need themes to recreate their generated |
51 // images (which are cached). | 51 // images (which are cached). |
52 const int kThemePackVersion = 42; | 52 const int kThemePackVersion = 43; |
53 | 53 |
54 // IDs that are in the DataPack won't clash with the positive integer | 54 // IDs that are in the DataPack won't clash with the positive integer |
55 // uint16_t. kHeaderID should always have the maximum value because we want the | 55 // uint16_t. kHeaderID should always have the maximum value because we want the |
56 // "header" to be written last. That way we can detect whether the pack was | 56 // "header" to be written last. That way we can detect whether the pack was |
57 // successfully written and ignore and regenerate if it was only partially | 57 // successfully written and ignore and regenerate if it was only partially |
58 // written (i.e. chrome crashed on a different thread while writing the pack). | 58 // written (i.e. chrome crashed on a different thread while writing the pack). |
59 const int kMaxID = 0x0000FFFF; // Max unsigned 16-bit int. | 59 const int kMaxID = 0x0000FFFF; // Max unsigned 16-bit int. |
60 const int kHeaderID = kMaxID - 1; | 60 const int kHeaderID = kMaxID - 1; |
61 const int kTintsID = kMaxID - 2; | 61 const int kTintsID = kMaxID - 2; |
62 const int kColorsID = kMaxID - 3; | 62 const int kColorsID = kMaxID - 3; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // someone adds a new resource. | 95 // someone adds a new resource. |
96 int idr_id; | 96 int idr_id; |
97 | 97 |
98 // String to check for when parsing theme manifests or NULL if this isn't | 98 // String to check for when parsing theme manifests or NULL if this isn't |
99 // supposed to be changeable by the user. | 99 // supposed to be changeable by the user. |
100 const char* const key; | 100 const char* const key; |
101 }; | 101 }; |
102 | 102 |
103 // IDR_* resource names change whenever new resources are added; use persistent | 103 // IDR_* resource names change whenever new resources are added; use persistent |
104 // IDs when storing to a cached pack. | 104 // IDs when storing to a cached pack. |
105 // | |
106 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These | |
107 // should be removed once the cocoa port no longer uses them. | |
108 PersistingImagesTable kPersistingImages[] = { | 105 PersistingImagesTable kPersistingImages[] = { |
109 {PRS_THEME_FRAME, IDR_THEME_FRAME, "theme_frame"}, | 106 {PRS_THEME_FRAME, IDR_THEME_FRAME, "theme_frame"}, |
110 {PRS_THEME_FRAME_INACTIVE, IDR_THEME_FRAME_INACTIVE, | 107 {PRS_THEME_FRAME_INACTIVE, IDR_THEME_FRAME_INACTIVE, |
111 "theme_frame_inactive"}, | 108 "theme_frame_inactive"}, |
112 {PRS_THEME_FRAME_INCOGNITO, IDR_THEME_FRAME_INCOGNITO, | 109 {PRS_THEME_FRAME_INCOGNITO, IDR_THEME_FRAME_INCOGNITO, |
113 "theme_frame_incognito"}, | 110 "theme_frame_incognito"}, |
114 {PRS_THEME_FRAME_INCOGNITO_INACTIVE, IDR_THEME_FRAME_INCOGNITO_INACTIVE, | 111 {PRS_THEME_FRAME_INCOGNITO_INACTIVE, IDR_THEME_FRAME_INCOGNITO_INACTIVE, |
115 "theme_frame_incognito_inactive"}, | 112 "theme_frame_incognito_inactive"}, |
116 {PRS_THEME_TOOLBAR, IDR_THEME_TOOLBAR, "theme_toolbar"}, | 113 {PRS_THEME_TOOLBAR, IDR_THEME_TOOLBAR, "theme_toolbar"}, |
117 {PRS_THEME_TAB_BACKGROUND, IDR_THEME_TAB_BACKGROUND, | 114 {PRS_THEME_TAB_BACKGROUND, IDR_THEME_TAB_BACKGROUND, |
118 "theme_tab_background"}, | 115 "theme_tab_background"}, |
119 #if !defined(OS_MACOSX) | 116 #if !defined(OS_MACOSX) |
120 {PRS_THEME_TAB_BACKGROUND_INCOGNITO, IDR_THEME_TAB_BACKGROUND_INCOGNITO, | 117 {PRS_THEME_TAB_BACKGROUND_INCOGNITO, IDR_THEME_TAB_BACKGROUND_INCOGNITO, |
121 "theme_tab_background_incognito"}, | 118 "theme_tab_background_incognito"}, |
122 #endif | 119 #endif |
123 {PRS_THEME_TAB_BACKGROUND_V, IDR_THEME_TAB_BACKGROUND_V, | 120 {PRS_THEME_TAB_BACKGROUND_V, IDR_THEME_TAB_BACKGROUND_V, |
124 "theme_tab_background_v"}, | 121 "theme_tab_background_v"}, |
125 {PRS_THEME_NTP_BACKGROUND, IDR_THEME_NTP_BACKGROUND, | 122 {PRS_THEME_NTP_BACKGROUND, IDR_THEME_NTP_BACKGROUND, |
126 "theme_ntp_background"}, | 123 "theme_ntp_background"}, |
127 {PRS_THEME_FRAME_OVERLAY, IDR_THEME_FRAME_OVERLAY, "theme_frame_overlay"}, | 124 {PRS_THEME_FRAME_OVERLAY, IDR_THEME_FRAME_OVERLAY, "theme_frame_overlay"}, |
128 {PRS_THEME_FRAME_OVERLAY_INACTIVE, IDR_THEME_FRAME_OVERLAY_INACTIVE, | 125 {PRS_THEME_FRAME_OVERLAY_INACTIVE, IDR_THEME_FRAME_OVERLAY_INACTIVE, |
129 "theme_frame_overlay_inactive"}, | 126 "theme_frame_overlay_inactive"}, |
130 {PRS_THEME_BUTTON_BACKGROUND, IDR_THEME_BUTTON_BACKGROUND, | 127 {PRS_THEME_BUTTON_BACKGROUND, IDR_THEME_BUTTON_BACKGROUND, |
131 "theme_button_background"}, | 128 "theme_button_background"}, |
132 {PRS_THEME_NTP_ATTRIBUTION, IDR_THEME_NTP_ATTRIBUTION, | 129 {PRS_THEME_NTP_ATTRIBUTION, IDR_THEME_NTP_ATTRIBUTION, |
133 "theme_ntp_attribution"}, | 130 "theme_ntp_attribution"}, |
134 {PRS_THEME_WINDOW_CONTROL_BACKGROUND, IDR_THEME_WINDOW_CONTROL_BACKGROUND, | 131 {PRS_THEME_WINDOW_CONTROL_BACKGROUND, IDR_THEME_WINDOW_CONTROL_BACKGROUND, |
135 "theme_window_control_background"}, | 132 "theme_window_control_background"}, |
136 | |
137 // The rest of these entries have no key because they can't be overridden | |
138 // from the json manifest. | |
139 {15, IDR_BACK, NULL}, | |
140 {16, IDR_BACK_D, NULL}, | |
141 {17, IDR_BACK_H, NULL}, | |
142 {18, IDR_BACK_P, NULL}, | |
143 {19, IDR_FORWARD, NULL}, | |
144 {20, IDR_FORWARD_D, NULL}, | |
145 {21, IDR_FORWARD_H, NULL}, | |
146 {22, IDR_FORWARD_P, NULL}, | |
147 #if defined(OS_MACOSX) | |
148 {23, IDR_HOME, NULL}, | |
149 {24, IDR_HOME_H, NULL}, | |
150 {25, IDR_HOME_P, NULL}, | |
151 #endif | |
152 {26, IDR_RELOAD, NULL}, | |
153 {27, IDR_RELOAD_H, NULL}, | |
154 {28, IDR_RELOAD_P, NULL}, | |
155 {29, IDR_STOP, NULL}, | |
156 {30, IDR_STOP_D, NULL}, | |
157 {31, IDR_STOP_H, NULL}, | |
158 {32, IDR_STOP_P, NULL}, | |
159 {33, IDR_TOOLS, NULL}, | |
160 {34, IDR_TOOLS_H, NULL}, | |
161 {35, IDR_TOOLS_P, NULL}, | |
162 {36, IDR_MENU_DROPARROW, NULL}, | |
163 {37, IDR_TOOLBAR_BEZEL_HOVER, NULL}, | |
164 {38, IDR_TOOLBAR_BEZEL_PRESSED, NULL}, | |
165 {39, IDR_TOOLS_BAR, NULL}, | |
166 }; | 133 }; |
167 const size_t kPersistingImagesLength = arraysize(kPersistingImages); | 134 const size_t kPersistingImagesLength = arraysize(kPersistingImages); |
168 | 135 |
169 int GetPersistentIDByNameHelper(const std::string& key, | 136 int GetPersistentIDByNameHelper(const std::string& key, |
170 const PersistingImagesTable* image_table, | 137 const PersistingImagesTable* image_table, |
171 size_t image_table_size) { | 138 size_t image_table_size) { |
172 for (size_t i = 0; i < image_table_size; ++i) { | 139 for (size_t i = 0; i < image_table_size; ++i) { |
173 if (image_table[i].key && | 140 if (image_table[i].key && |
174 base::LowerCaseEqualsASCII(key, image_table[i].key)) { | 141 base::LowerCaseEqualsASCII(key, image_table[i].key)) { |
175 return image_table[i].persistent_id; | 142 return image_table[i].persistent_id; |
(...skipping 1067 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1243 if (!is_copyable && !image_skia.isNull()) | 1210 if (!is_copyable && !image_skia.isNull()) |
1244 (*image_cache)[prs_id] = gfx::Image(image_skia); | 1211 (*image_cache)[prs_id] = gfx::Image(image_skia); |
1245 } | 1212 } |
1246 | 1213 |
1247 return true; | 1214 return true; |
1248 } | 1215 } |
1249 | 1216 |
1250 void BrowserThemePack::CreateImages(ImageCache* images) const { | 1217 void BrowserThemePack::CreateImages(ImageCache* images) const { |
1251 CropImages(images); | 1218 CropImages(images); |
1252 CreateFrameImages(images); | 1219 CreateFrameImages(images); |
1253 CreateTintedButtons(GetTintInternal(ThemeProperties::TINT_BUTTONS), images); | |
1254 CreateTabBackgroundImages(images); | 1220 CreateTabBackgroundImages(images); |
1255 } | 1221 } |
1256 | 1222 |
1257 void BrowserThemePack::CropImages(ImageCache* images) const { | 1223 void BrowserThemePack::CropImages(ImageCache* images) const { |
1258 bool has_frame_border = HasFrameBorder(); | 1224 bool has_frame_border = HasFrameBorder(); |
1259 for (size_t i = 0; i < arraysize(kImagesToCrop); ++i) { | 1225 for (size_t i = 0; i < arraysize(kImagesToCrop); ++i) { |
1260 if (has_frame_border && kImagesToCrop[i].skip_if_frame_border) | 1226 if (has_frame_border && kImagesToCrop[i].skip_if_frame_border) |
1261 continue; | 1227 continue; |
1262 | 1228 |
1263 int prs_id = kImagesToCrop[i].prs_id; | 1229 int prs_id = kImagesToCrop[i].prs_id; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1319 frame = rb.GetImageNamed(IDR_THEME_FRAME); | 1285 frame = rb.GetImageNamed(IDR_THEME_FRAME); |
1320 } | 1286 } |
1321 if (!frame.IsEmpty()) { | 1287 if (!frame.IsEmpty()) { |
1322 temp_output[prs_id] = CreateHSLShiftedImage( | 1288 temp_output[prs_id] = CreateHSLShiftedImage( |
1323 frame, GetTintInternal(kFrameTintMap[i].value)); | 1289 frame, GetTintInternal(kFrameTintMap[i].value)); |
1324 } | 1290 } |
1325 } | 1291 } |
1326 MergeImageCaches(temp_output, images); | 1292 MergeImageCaches(temp_output, images); |
1327 } | 1293 } |
1328 | 1294 |
1329 void BrowserThemePack::CreateTintedButtons( | |
1330 const color_utils::HSL& button_tint, | |
1331 ImageCache* processed_images) const { | |
1332 if (button_tint.h != -1 || button_tint.s != -1 || button_tint.l != -1) { | |
1333 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
1334 const std::set<int>& idr_ids = | |
1335 ThemeProperties::GetTintableToolbarButtons(); | |
1336 for (std::set<int>::const_iterator it = idr_ids.begin(); | |
1337 it != idr_ids.end(); ++it) { | |
1338 int prs_id = GetPersistentIDByIDR(*it); | |
1339 DCHECK(prs_id > 0); | |
1340 | |
1341 // Fetch the image by IDR... | |
1342 gfx::Image& button = rb.GetImageNamed(*it); | |
1343 | |
1344 // but save a version with the persistent ID. | |
1345 (*processed_images)[prs_id] = | |
1346 CreateHSLShiftedImage(button, button_tint); | |
1347 } | |
1348 } | |
1349 } | |
1350 | |
1351 void BrowserThemePack::CreateTabBackgroundImages(ImageCache* images) const { | 1295 void BrowserThemePack::CreateTabBackgroundImages(ImageCache* images) const { |
1352 ImageCache temp_output; | 1296 ImageCache temp_output; |
1353 for (size_t i = 0; i < arraysize(kTabBackgroundMap); ++i) { | 1297 for (size_t i = 0; i < arraysize(kTabBackgroundMap); ++i) { |
1354 int prs_id = kTabBackgroundMap[i].key; | 1298 int prs_id = kTabBackgroundMap[i].key; |
1355 int prs_base_id = kTabBackgroundMap[i].value; | 1299 int prs_base_id = kTabBackgroundMap[i].value; |
1356 | 1300 |
1357 // We only need to generate the background tab images if we were provided | 1301 // We only need to generate the background tab images if we were provided |
1358 // with a PRS_THEME_FRAME. | 1302 // with a PRS_THEME_FRAME. |
1359 ImageCache::const_iterator it = images->find(prs_base_id); | 1303 ImageCache::const_iterator it = images->find(prs_base_id); |
1360 if (it != images->end()) { | 1304 if (it != images->end()) { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1532 false, | 1476 false, |
1533 &bitmap_data)) { | 1477 &bitmap_data)) { |
1534 NOTREACHED() << "Unable to encode theme image for prs_id=" | 1478 NOTREACHED() << "Unable to encode theme image for prs_id=" |
1535 << prs_id << " for scale_factor=" << scale_factors_[i]; | 1479 << prs_id << " for scale_factor=" << scale_factors_[i]; |
1536 break; | 1480 break; |
1537 } | 1481 } |
1538 image_memory_[scaled_raw_id] = | 1482 image_memory_[scaled_raw_id] = |
1539 base::RefCountedBytes::TakeVector(&bitmap_data); | 1483 base::RefCountedBytes::TakeVector(&bitmap_data); |
1540 } | 1484 } |
1541 } | 1485 } |
OLD | NEW |