OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/browser_theme_pack.h" | |
6 | |
7 #include <climits> | |
8 | |
9 #include "app/gfx/codec/png_codec.h" | |
10 #include "app/gfx/skbitmap_operations.h" | |
11 #include "base/data_pack.h" | |
12 #include "base/logging.h" | |
13 #include "base/stl_util-inl.h" | |
14 #include "base/string_util.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/browser_theme_provider.h" | |
17 #include "chrome/browser/chrome_thread.h" | |
18 #include "chrome/browser/theme_resources_util.h" | |
19 #include "chrome/common/extensions/extension.h" | |
20 #include "grit/app_resources.h" | |
21 #include "grit/theme_resources.h" | |
22 #include "net/base/file_stream.h" | |
23 #include "net/base/net_errors.h" | |
24 #include "third_party/skia/include/core/SkBitmap.h" | |
25 #include "third_party/skia/include/core/SkCanvas.h" | |
26 #include "third_party/skia/include/core/SkUnPreMultiply.h" | |
27 | |
28 namespace { | |
29 | |
30 // Version number of the current theme pack. We just throw out and rebuild | |
31 // theme packs that aren't int-equal to this. | |
32 const int kThemePackVersion = 1; | |
33 | |
34 // IDs that are in the DataPack won't clash with the positive integer | |
35 // int32_t. kHeaderID should always have the maximum value because we want the | |
36 // "header" to be written last. That way we can detect whether the pack was | |
37 // successfully written and ignore and regenerate if it was only partially | |
38 // written (i.e. chrome crashed on a different thread while writing the pack). | |
39 const int kHeaderID = UINT_MAX - 1; | |
40 const int kTintsID = UINT_MAX - 2; | |
41 const int kColorsID = UINT_MAX - 3; | |
42 const int kDisplayPropertiesID = UINT_MAX - 4; | |
43 | |
44 // Static size of the tint/color/display property arrays that are mmapped. | |
45 const int kTintArraySize = 6; | |
46 const int kColorArraySize = 19; | |
47 const int kDisplayPropertySize = 3; | |
48 | |
49 // The sum of kFrameBorderThickness and kNonClientRestoredExtraThickness from | |
50 // OpaqueBrowserFrameView. | |
51 const int kRestoredTabVerticalOffset = 15; | |
52 | |
53 | |
54 struct StringToIntTable { | |
55 const char* key; | |
56 int id; | |
57 }; | |
58 | |
59 // Strings used by themes to identify tints in the JSON. | |
60 StringToIntTable kTintTable[] = { | |
61 { "buttons", BrowserThemeProvider::TINT_BUTTONS }, | |
62 { "frame", BrowserThemeProvider::TINT_FRAME }, | |
63 { "frame_inactive", BrowserThemeProvider::TINT_FRAME_INACTIVE }, | |
64 { "frame_incognito_inactive", | |
65 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE }, | |
66 { "background_tab", BrowserThemeProvider::TINT_BACKGROUND_TAB }, | |
67 { NULL, 0 } | |
68 }; | |
69 | |
70 // Strings used by themes to identify colors in the JSON. | |
71 StringToIntTable kColorTable[] = { | |
72 { "frame", BrowserThemeProvider::COLOR_FRAME }, | |
73 { "frame_inactive", BrowserThemeProvider::COLOR_FRAME_INACTIVE }, | |
74 { "frame_incognito", BrowserThemeProvider::COLOR_FRAME_INCOGNITO }, | |
75 { "frame_incognito_inactive", | |
76 BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE }, | |
77 { "toolbar", BrowserThemeProvider::COLOR_TOOLBAR }, | |
78 { "tab_text", BrowserThemeProvider::COLOR_TAB_TEXT }, | |
79 { "tab_background_text", BrowserThemeProvider::COLOR_BACKGROUND_TAB_TEXT }, | |
80 { "bookmark_text", BrowserThemeProvider::COLOR_BOOKMARK_TEXT }, | |
81 { "ntp_background", BrowserThemeProvider::COLOR_NTP_BACKGROUND }, | |
82 { "ntp_text", BrowserThemeProvider::COLOR_NTP_TEXT }, | |
83 { "ntp_link", BrowserThemeProvider::COLOR_NTP_LINK }, | |
84 { "ntp_link_underline", BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE }, | |
85 { "ntp_header", BrowserThemeProvider::COLOR_NTP_HEADER }, | |
86 { "ntp_section", BrowserThemeProvider::COLOR_NTP_SECTION }, | |
87 { "ntp_section_text", BrowserThemeProvider::COLOR_NTP_SECTION_TEXT }, | |
88 { "ntp_section_link", BrowserThemeProvider::COLOR_NTP_SECTION_LINK }, | |
89 { "ntp_section_link_underline", | |
90 BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE }, | |
91 { "control_background", BrowserThemeProvider::COLOR_CONTROL_BACKGROUND }, | |
92 { "button_background", BrowserThemeProvider::COLOR_BUTTON_BACKGROUND }, | |
93 { NULL, 0 } | |
94 }; | |
95 | |
96 // Strings used by themes to identify display properties keys in JSON. | |
97 StringToIntTable kDisplayProperties[] = { | |
98 { "ntp_background_alignment", | |
99 BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT }, | |
100 { "ntp_background_repeat", BrowserThemeProvider::NTP_BACKGROUND_TILING }, | |
101 { "ntp_logo_alternate", BrowserThemeProvider::NTP_LOGO_ALTERNATE }, | |
102 { NULL, 0 } | |
103 }; | |
104 | |
105 // Strings used by the tiling values in JSON. | |
106 StringToIntTable kTilingStrings[] = { | |
107 { "no-repeat", BrowserThemeProvider::NO_REPEAT }, | |
108 { "repeat-x", BrowserThemeProvider::REPEAT_X }, | |
109 { "repeat-y", BrowserThemeProvider::REPEAT_Y }, | |
110 { "repeat", BrowserThemeProvider::REPEAT }, | |
111 { NULL, 0 } | |
112 }; | |
113 | |
114 int GetIntForString(const std::string& key, StringToIntTable* table) { | |
115 for (int i = 0; table[i].key != NULL; ++i) { | |
116 if (base::strcasecmp(key.c_str(), table[i].key) == 0) { | |
117 return table[i].id; | |
118 } | |
119 } | |
120 | |
121 return -1; | |
122 } | |
123 | |
124 struct IntToIntTable { | |
125 int key; | |
126 int value; | |
127 }; | |
128 | |
129 // Mapping used in GenerateFrameImages() to associate frame images with the | |
130 // tint ID that should maybe be applied to it. | |
131 IntToIntTable kFrameTintMap[] = { | |
132 { IDR_THEME_FRAME, BrowserThemeProvider::TINT_FRAME }, | |
133 { IDR_THEME_FRAME_INACTIVE, BrowserThemeProvider::TINT_FRAME_INACTIVE }, | |
134 { IDR_THEME_FRAME_OVERLAY, BrowserThemeProvider::TINT_FRAME }, | |
135 { IDR_THEME_FRAME_OVERLAY_INACTIVE, | |
136 BrowserThemeProvider::TINT_FRAME_INACTIVE }, | |
137 { IDR_THEME_FRAME_INCOGNITO, BrowserThemeProvider::TINT_FRAME_INCOGNITO }, | |
138 { IDR_THEME_FRAME_INCOGNITO_INACTIVE, | |
139 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE } | |
140 }; | |
141 | |
142 // Mapping used in GenerateTabBackgroundImages() to associate what frame image | |
143 // goes with which tab background. | |
144 IntToIntTable kTabBackgroundMap[] = { | |
145 { IDR_THEME_TAB_BACKGROUND, IDR_THEME_FRAME }, | |
146 { IDR_THEME_TAB_BACKGROUND_INCOGNITO, IDR_THEME_FRAME_INCOGNITO } | |
147 }; | |
148 | |
149 // A list of images that don't need tinting or any other modification and can | |
150 // be byte-copied directly into the finished DataPack. This should contain all | |
151 // themeable image IDs that aren't in kFrameTintMap or kTabBackgroundMap. | |
152 const int kPreloadIDs[] = { | |
153 IDR_THEME_TOOLBAR, | |
154 IDR_THEME_NTP_BACKGROUND, | |
155 IDR_THEME_BUTTON_BACKGROUND, | |
156 IDR_THEME_NTP_ATTRIBUTION, | |
157 IDR_THEME_WINDOW_CONTROL_BACKGROUND | |
158 }; | |
159 | |
160 // The image resources that will be tinted by the 'button' tint value. | |
161 const int kToolbarButtonIDs[] = { | |
162 IDR_BACK, IDR_BACK_D, IDR_BACK_H, IDR_BACK_P, | |
163 IDR_FORWARD, IDR_FORWARD_D, IDR_FORWARD_H, IDR_FORWARD_P, | |
164 IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P, | |
165 IDR_HOME, IDR_HOME_H, IDR_HOME_P, | |
166 IDR_STAR, IDR_STAR_NOBORDER, IDR_STAR_NOBORDER_CENTER, IDR_STAR_D, IDR_STAR_H, | |
167 IDR_STAR_P, | |
168 IDR_STARRED, IDR_STARRED_NOBORDER, IDR_STARRED_NOBORDER_CENTER, IDR_STARRED_H, | |
169 IDR_STARRED_P, | |
170 IDR_GO, IDR_GO_NOBORDER, IDR_GO_NOBORDER_CENTER, IDR_GO_H, IDR_GO_P, | |
171 IDR_STOP, IDR_STOP_NOBORDER, IDR_STOP_NOBORDER_CENTER, IDR_STOP_H, IDR_STOP_P, | |
172 IDR_MENU_BOOKMARK, | |
173 IDR_MENU_PAGE, IDR_MENU_PAGE_RTL, | |
174 IDR_MENU_CHROME, IDR_MENU_CHROME_RTL, | |
175 IDR_MENU_DROPARROW, | |
176 IDR_THROBBER, IDR_THROBBER_WAITING, IDR_THROBBER_LIGHT, | |
177 IDR_LOCATIONBG | |
178 }; | |
179 | |
180 // Returns a piece of memory with the contents of the file |path|. | |
181 RefCountedMemory* ReadFileData(const FilePath& path) { | |
182 if (!path.empty()) { | |
183 net::FileStream file; | |
184 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | |
185 if (file.Open(path, flags) == net::OK) { | |
186 int64 avail = file.Available(); | |
187 if (avail > 0 && avail < INT_MAX) { | |
188 size_t size = static_cast<size_t>(avail); | |
189 std::vector<unsigned char> raw_data; | |
190 raw_data.resize(size); | |
191 char* data = reinterpret_cast<char*>(&(raw_data.front())); | |
192 if (file.ReadUntilComplete(data, size) == avail) | |
193 return RefCountedBytes::TakeVector(&raw_data); | |
194 } | |
195 } | |
196 } | |
197 | |
198 return NULL; | |
199 } | |
200 | |
201 // Does error checking for invalid incoming data while trying to read an | |
202 // floaing point value. | |
Miranda Callahan
2009/12/14 20:47:51
floating
| |
203 bool ValidRealValue(ListValue* tint_list, int index, double* out) { | |
204 if (tint_list->GetReal(index, out)) | |
205 return true; | |
206 | |
207 int value = 0; | |
208 if (tint_list->GetInteger(index, &value)) { | |
209 *out = value; | |
210 return true; | |
211 } | |
212 | |
213 return false; | |
214 } | |
215 | |
216 } // namespace | |
217 | |
218 BrowserThemePack::~BrowserThemePack() { | |
219 if (!data_pack_.get()) { | |
220 delete header_; | |
221 delete [] tints_; | |
222 delete [] colors_; | |
223 delete [] display_properties_; | |
224 } | |
225 | |
226 STLDeleteValues(&image_cache_); | |
227 } | |
228 | |
229 // static | |
230 BrowserThemePack* BrowserThemePack::BuildFromExtension(Extension* extension) { | |
231 DCHECK(extension); | |
232 DCHECK(extension->IsTheme()); | |
233 | |
234 BrowserThemePack* pack = new BrowserThemePack; | |
235 pack->BuildHeader(extension); | |
236 pack->BuildTintsFromJSON(extension->GetThemeTints()); | |
237 pack->BuildColorsFromJSON(extension->GetThemeColors()); | |
238 pack->BuildDisplayPropertiesFromJSON(extension->GetThemeDisplayProperties()); | |
239 | |
240 // Builds the images. (Image building is dependent on tints). | |
241 std::map<int, FilePath> file_paths; | |
242 pack->ParseImageNamesFromJSON(extension->GetThemeImages(), | |
243 extension->path(), | |
244 &file_paths); | |
245 | |
246 pack->LoadRawBitmapsTo(file_paths, &pack->image_cache_); | |
247 | |
248 pack->GenerateFrameImages(&pack->image_cache_); | |
249 | |
250 #if !defined(OS_MACOSX) | |
251 // OSX uses its own special buttons that are PDFs that do odd sorts of vector | |
252 // graphics tricks. Other platforms use bitmaps and we must pre-tint them. | |
253 pack->GenerateTintedButtons( | |
254 pack->GetTintInternal(BrowserThemeProvider::TINT_BUTTONS), | |
255 &pack->image_cache_); | |
256 #endif | |
257 | |
258 pack->GenerateTabBackgroundImages(&pack->image_cache_); | |
259 | |
260 // Repack all the images from |image_cache_| into |image_memory_| for | |
261 // writing to the data pack | |
Miranda Callahan
2009/12/14 20:47:51
. at end of sentence
| |
262 pack->RepackImageCacheToImageMemory(); | |
263 | |
264 // The BrowserThemePack is now in a consistent state. | |
265 return pack; | |
266 } | |
267 | |
268 // static | |
269 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( | |
270 FilePath path, const std::string& expected_id) { | |
271 scoped_refptr<BrowserThemePack> pack = new BrowserThemePack; | |
272 pack->data_pack_.reset(new base::DataPack); | |
273 | |
274 if (!pack->data_pack_->Load(path)) { | |
275 LOG(ERROR) << "Failed to load theme data pack."; | |
276 return NULL; | |
277 } | |
278 | |
279 base::StringPiece pointer; | |
280 if (!pack->data_pack_->GetStringPiece(kHeaderID, &pointer)) | |
281 return NULL; | |
282 pack->header_ = reinterpret_cast<BrowserThemePackHeader*>(const_cast<char*>( | |
283 pointer.data())); | |
284 | |
285 if (pack->header_->version != kThemePackVersion) | |
286 return NULL; | |
287 // TODO(erg): Check endianess once DataPack works on the other endian. | |
288 std::string theme_id(reinterpret_cast<char*>(pack->header_->theme_id), | |
289 Extension::kIdSize); | |
290 std::string truncated_id = expected_id.substr(0, Extension::kIdSize); | |
291 if (theme_id != truncated_id) { | |
292 DLOG(ERROR) << "Wrong id: " << theme_id << " vs " << expected_id; | |
293 return NULL; | |
294 } | |
295 | |
296 if (!pack->data_pack_->GetStringPiece(kTintsID, &pointer)) | |
297 return NULL; | |
298 pack->tints_ = reinterpret_cast<TintEntry*>(const_cast<char*>( | |
299 pointer.data())); | |
300 | |
301 if (!pack->data_pack_->GetStringPiece(kColorsID, &pointer)) | |
302 return NULL; | |
303 pack->colors_ = | |
304 reinterpret_cast<ColorPair*>(const_cast<char*>(pointer.data())); | |
305 | |
306 if (!pack->data_pack_->GetStringPiece(kDisplayPropertiesID, &pointer)) | |
307 return NULL; | |
308 pack->display_properties_ = reinterpret_cast<DisplayPropertyPair*>( | |
309 const_cast<char*>(pointer.data())); | |
310 | |
311 return pack; | |
312 } | |
313 | |
314 bool BrowserThemePack::WriteToDisk(FilePath path) const { | |
315 std::map<uint32, base::StringPiece> resources; | |
316 | |
317 resources[kHeaderID] = base::StringPiece( | |
318 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); | |
319 resources[kTintsID] = base::StringPiece( | |
320 reinterpret_cast<const char*>(tints_), sizeof(TintEntry[kTintArraySize])); | |
321 resources[kColorsID] = base::StringPiece( | |
322 reinterpret_cast<const char*>(colors_), | |
323 sizeof(ColorPair[kColorArraySize])); | |
324 resources[kDisplayPropertiesID] = base::StringPiece( | |
325 reinterpret_cast<const char*>(display_properties_), | |
326 sizeof(DisplayPropertyPair[kDisplayPropertySize])); | |
327 | |
328 for (RawImages::const_iterator it = image_memory_.begin(); | |
329 it != image_memory_.end(); ++it) { | |
330 resources[it->first] = base::StringPiece( | |
331 reinterpret_cast<const char*>(it->second->front()), it->second->size()); | |
332 } | |
333 | |
334 return base::DataPack::WritePack(path, resources); | |
335 } | |
336 | |
337 bool BrowserThemePack::GetTint(int id, color_utils::HSL* hsl) const { | |
338 if (tints_) { | |
339 for (int i = 0; i < kTintArraySize; ++i) { | |
340 if (tints_[i].id == id) { | |
341 hsl->h = tints_[i].h; | |
342 hsl->s = tints_[i].s; | |
343 hsl->l = tints_[i].l; | |
344 return true; | |
345 } | |
346 } | |
347 } | |
348 | |
349 return false; | |
350 } | |
351 | |
352 bool BrowserThemePack::GetColor(int id, SkColor* color) const { | |
353 if (colors_) { | |
354 for (int i = 0; i < kColorArraySize; ++i) { | |
355 if (colors_[i].id == id) { | |
356 *color = colors_[i].color; | |
357 return true; | |
358 } | |
359 } | |
360 } | |
361 | |
362 return false; | |
363 } | |
364 | |
365 bool BrowserThemePack::GetDisplayProperty(int id, int* result) const { | |
366 if (display_properties_) { | |
367 for (int i = 0; i < kDisplayPropertySize; ++i) { | |
368 if (display_properties_[i].id == id) { | |
369 *result = display_properties_[i].property; | |
370 return true; | |
371 } | |
372 } | |
373 } | |
374 | |
375 return false; | |
376 } | |
377 | |
378 SkBitmap* BrowserThemePack::GetBitmapNamed(int id) const { | |
379 // Check our cache of prepared images, first. | |
380 ImageCache::const_iterator image_iter = image_cache_.find(id); | |
381 if (image_iter != image_cache_.end()) | |
382 return image_iter->second; | |
383 | |
384 scoped_refptr<RefCountedMemory> memory; | |
385 if (data_pack_.get()) { | |
386 memory = data_pack_->GetStaticMemory(id); | |
387 } else { | |
388 RawImages::const_iterator it = image_memory_.find(id); | |
389 if (it != image_memory_.end()) { | |
390 memory = it->second; | |
391 } | |
392 } | |
393 | |
394 if (memory.get()) { | |
395 // Decode the PNG. | |
396 SkBitmap bitmap; | |
397 if (!gfx::PNGCodec::Decode(memory->front(), memory->size(), | |
398 &bitmap)) { | |
399 NOTREACHED() << "Unable to decode theme image resource " << id | |
400 << " from saved DataPack."; | |
401 return NULL; | |
402 } | |
403 | |
404 SkBitmap* ret = new SkBitmap(bitmap); | |
405 image_cache_[id] = ret; | |
406 | |
407 return ret; | |
408 } | |
409 | |
410 return NULL; | |
411 } | |
412 | |
413 RefCountedMemory* BrowserThemePack::GetRawData(int id) const { | |
414 RefCountedMemory* memory = NULL; | |
415 | |
416 if (data_pack_.get()) { | |
417 memory = data_pack_->GetStaticMemory(id); | |
418 } else { | |
419 RawImages::const_iterator it = image_memory_.find(id); | |
420 if (it != image_memory_.end()) { | |
421 memory = it->second; | |
422 } | |
423 } | |
424 | |
425 return memory; | |
426 } | |
427 | |
428 bool BrowserThemePack::HasCustomImage(int id) const { | |
429 if (data_pack_.get()) { | |
430 base::StringPiece ignored; | |
431 return data_pack_->GetStringPiece(id, &ignored); | |
432 } else { | |
433 return image_memory_.count(id) > 0; | |
434 } | |
435 } | |
436 | |
437 // private: | |
438 | |
439 BrowserThemePack::BrowserThemePack() | |
440 : header_(NULL), | |
441 tints_(NULL), | |
442 colors_(NULL), | |
443 display_properties_(NULL) { | |
444 } | |
445 | |
446 void BrowserThemePack::BuildHeader(Extension* extension) { | |
447 header_ = new BrowserThemePackHeader; | |
448 header_->version = kThemePackVersion; | |
449 | |
450 // TODO(erg): Need to make this endian safe on other computers. Prerequisite | |
451 // is that base::DataPack removes this same check. | |
452 #if defined(__BYTE_ORDER) | |
453 // Linux check | |
454 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, | |
455 datapack_assumes_little_endian); | |
456 #elif defined(__BIG_ENDIAN__) | |
457 // Mac check | |
458 #error DataPack assumes little endian | |
459 #endif | |
460 header_->little_endian = 1; | |
461 | |
462 const std::string& id = extension->id(); | |
463 memcpy(header_->theme_id, id.c_str(), Extension::kIdSize); | |
464 } | |
465 | |
466 void BrowserThemePack::BuildTintsFromJSON(DictionaryValue* tints_value) { | |
467 tints_ = new TintEntry[kTintArraySize]; | |
468 for (int i = 0; i < kTintArraySize; ++i) { | |
469 tints_[i].id = -1; | |
470 tints_[i].h = -1; | |
471 tints_[i].s = -1; | |
472 tints_[i].l = -1; | |
473 } | |
474 | |
475 // Parse the incoming data from |tints_value| into an intermediary structure. | |
476 std::map<int, color_utils::HSL> temp_tints; | |
477 for (DictionaryValue::key_iterator iter(tints_value->begin_keys()); | |
478 iter != tints_value->end_keys(); ++iter) { | |
479 ListValue* tint_list; | |
480 if (tints_value->GetList(*iter, &tint_list) && | |
481 (tint_list->GetSize() == 3)) { | |
482 color_utils::HSL hsl = { -1, -1, -1 }; | |
483 | |
484 if (ValidRealValue(tint_list, 0, &hsl.h) && | |
485 ValidRealValue(tint_list, 1, &hsl.s) && | |
486 ValidRealValue(tint_list, 2, &hsl.l)) { | |
487 | |
488 int id = GetIntForString(WideToUTF8(*iter), kTintTable); | |
489 if (id != -1) { | |
490 temp_tints[id] = hsl; | |
491 } | |
492 } | |
493 } | |
494 } | |
495 | |
496 // Copy data from the intermediary data structure to the array. | |
497 int count = 0; | |
498 for (std::map<int, color_utils::HSL>::const_iterator it = | |
499 temp_tints.begin(); it != temp_tints.end() && count < kTintArraySize; | |
500 ++it, ++count) { | |
501 tints_[count].id = it->first; | |
502 tints_[count].h = it->second.h; | |
503 tints_[count].s = it->second.s; | |
504 tints_[count].l = it->second.l; | |
505 } | |
506 } | |
507 | |
508 void BrowserThemePack::BuildColorsFromJSON(DictionaryValue* colors_value) { | |
509 colors_ = new ColorPair[kColorArraySize]; | |
510 for (int i = 0; i < kColorArraySize; ++i) { | |
511 colors_[i].id = -1; | |
512 colors_[i].color = SkColorSetRGB(0, 0, 0); | |
513 } | |
514 | |
515 std::map<int, SkColor> temp_colors; | |
516 ReadColorsFromJSON(colors_value, &temp_colors); | |
517 GenerateMissingColors(&temp_colors); | |
518 | |
519 // Copy data from the intermediary data structure to the array. | |
520 int count = 0; | |
521 for (std::map<int, SkColor>::const_iterator it = temp_colors.begin(); | |
522 it != temp_colors.end() && count < kColorArraySize; ++it, ++count) { | |
523 colors_[count].id = it->first; | |
524 colors_[count].color = it->second; | |
525 } | |
526 } | |
527 | |
528 void BrowserThemePack::ReadColorsFromJSON( | |
529 DictionaryValue* colors_value, | |
530 std::map<int, SkColor>* temp_colors) { | |
531 // Parse the incoming data from |colors_value| into an intermediary structure. | |
532 for (DictionaryValue::key_iterator iter(colors_value->begin_keys()); | |
533 iter != colors_value->end_keys(); ++iter) { | |
534 ListValue* color_list; | |
535 if (colors_value->GetList(*iter, &color_list) && | |
536 ((color_list->GetSize() == 3) || (color_list->GetSize() == 4))) { | |
537 SkColor color = SK_ColorWHITE; | |
538 int r, g, b; | |
539 if (color_list->GetInteger(0, &r) && | |
540 color_list->GetInteger(1, &g) && | |
541 color_list->GetInteger(2, &b)) { | |
542 if (color_list->GetSize() == 4) { | |
543 double alpha; | |
544 int alpha_int; | |
545 if (color_list->GetReal(3, &alpha)) { | |
546 color = SkColorSetARGB(static_cast<int>(alpha * 255), r, g, b); | |
547 } else if (color_list->GetInteger(3, &alpha_int) && | |
548 (alpha_int == 0 || alpha_int == 1)) { | |
549 color = SkColorSetARGB(alpha_int ? 255 : 0, r, g, b); | |
550 } else { | |
551 // Invalid entry for part 4. | |
552 continue; | |
553 } | |
554 } else { | |
555 color = SkColorSetRGB(r, g, b); | |
556 } | |
557 | |
558 int id = GetIntForString(WideToUTF8(*iter), kColorTable); | |
559 if (id != -1) { | |
560 (*temp_colors)[id] = color; | |
561 } | |
562 } | |
563 } | |
564 } | |
565 } | |
566 | |
567 void BrowserThemePack::GenerateMissingColors( | |
568 std::map<int, SkColor>* colors) { | |
569 // Generate link colors, if missing. (See GetColor()). | |
570 if (!colors->count(BrowserThemeProvider::COLOR_NTP_HEADER) && | |
571 colors->count(BrowserThemeProvider::COLOR_NTP_SECTION)) { | |
572 (*colors)[BrowserThemeProvider::COLOR_NTP_HEADER] = | |
573 (*colors)[BrowserThemeProvider::COLOR_NTP_SECTION]; | |
574 } | |
575 | |
576 if (!colors->count(BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE) && | |
577 colors->count(BrowserThemeProvider::COLOR_NTP_SECTION_LINK)) { | |
578 SkColor color_section_link = | |
579 (*colors)[BrowserThemeProvider::COLOR_NTP_SECTION_LINK]; | |
580 (*colors)[BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE] = | |
581 SkColorSetA(color_section_link, SkColorGetA(color_section_link) / 3); | |
582 } | |
583 | |
584 if (!colors->count(BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE) && | |
585 colors->count(BrowserThemeProvider::COLOR_NTP_LINK)) { | |
586 SkColor color_link = (*colors)[BrowserThemeProvider::COLOR_NTP_LINK]; | |
587 (*colors)[BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE] = | |
588 SkColorSetA(color_link, SkColorGetA(color_link) / 3); | |
589 } | |
590 | |
591 // Generate frame colors, if missing. (See GenerateFrameColors()). | |
592 SkColor frame; | |
593 std::map<int, SkColor>::const_iterator it = | |
594 colors->find(BrowserThemeProvider::COLOR_FRAME); | |
595 if (it != colors->end()) { | |
596 frame = it->second; | |
597 } else { | |
598 frame = BrowserThemeProvider::GetDefaultColor( | |
599 BrowserThemeProvider::COLOR_FRAME); | |
600 } | |
601 | |
602 if (!colors->count(BrowserThemeProvider::COLOR_FRAME)) { | |
603 (*colors)[BrowserThemeProvider::COLOR_FRAME] = | |
604 HSLShift(frame, GetTintInternal(BrowserThemeProvider::TINT_FRAME)); | |
605 } | |
606 if (!colors->count(BrowserThemeProvider::COLOR_FRAME_INACTIVE)) { | |
607 (*colors)[BrowserThemeProvider::COLOR_FRAME_INACTIVE] = | |
608 HSLShift(frame, GetTintInternal( | |
609 BrowserThemeProvider::TINT_FRAME_INACTIVE)); | |
610 } | |
611 if (!colors->count(BrowserThemeProvider::COLOR_FRAME_INCOGNITO)) { | |
612 (*colors)[BrowserThemeProvider::COLOR_FRAME_INCOGNITO] = | |
613 HSLShift(frame, GetTintInternal( | |
614 BrowserThemeProvider::TINT_FRAME_INCOGNITO)); | |
615 } | |
616 if (!colors->count(BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE)) { | |
617 (*colors)[BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE] = | |
618 HSLShift(frame, GetTintInternal( | |
619 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE)); | |
620 } | |
621 } | |
622 | |
623 void BrowserThemePack::BuildDisplayPropertiesFromJSON( | |
624 DictionaryValue* display_properties_value) { | |
625 display_properties_ = new DisplayPropertyPair[kDisplayPropertySize]; | |
626 for (int i = 0; i < kDisplayPropertySize; ++i) { | |
627 display_properties_[i].id = -1; | |
628 display_properties_[i].property = 0; | |
629 } | |
630 | |
631 std::map<int, int> temp_properties; | |
632 for (DictionaryValue::key_iterator iter( | |
633 display_properties_value->begin_keys()); | |
634 iter != display_properties_value->end_keys(); ++iter) { | |
635 int property_id = GetIntForString(WideToUTF8(*iter), kDisplayProperties); | |
636 switch (property_id) { | |
637 case BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT: { | |
638 std::string val; | |
639 if (display_properties_value->GetString(*iter, &val)) { | |
640 temp_properties[BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT] = | |
641 BrowserThemeProvider::StringToAlignment(val); | |
642 } | |
643 break; | |
644 } | |
645 case BrowserThemeProvider::NTP_BACKGROUND_TILING: { | |
646 std::string val; | |
647 if (display_properties_value->GetString(*iter, &val)) { | |
648 temp_properties[BrowserThemeProvider::NTP_BACKGROUND_TILING] = | |
649 GetIntForString(val, kTilingStrings); | |
650 } | |
651 break; | |
652 } | |
653 case BrowserThemeProvider::NTP_LOGO_ALTERNATE: { | |
654 int val = 0; | |
655 if (display_properties_value->GetInteger(*iter, &val)) | |
656 temp_properties[BrowserThemeProvider::NTP_LOGO_ALTERNATE] = val; | |
657 break; | |
658 } | |
659 } | |
660 } | |
661 | |
662 // Copy data from the intermediary data structure to the array. | |
663 int count = 0; | |
664 for (std::map<int, int>::const_iterator it = temp_properties.begin(); | |
665 it != temp_properties.end() && count < kDisplayPropertySize; | |
666 ++it, ++count) { | |
667 display_properties_[count].id = it->first; | |
668 display_properties_[count].property = it->second; | |
669 } | |
670 } | |
671 | |
672 void BrowserThemePack::ParseImageNamesFromJSON( | |
673 DictionaryValue* images_value, | |
674 FilePath images_path, | |
675 std::map<int, FilePath>* file_paths) const { | |
676 for (DictionaryValue::key_iterator iter(images_value->begin_keys()); | |
677 iter != images_value->end_keys(); ++iter) { | |
678 std::string val; | |
679 if (images_value->GetString(*iter, &val)) { | |
680 int id = ThemeResourcesUtil::GetId(WideToUTF8(*iter)); | |
681 if (id != -1) | |
682 (*file_paths)[id] = images_path.AppendASCII(val); | |
683 } | |
684 } | |
685 } | |
686 | |
687 void BrowserThemePack::LoadRawBitmapsTo( | |
688 const std::map<int, FilePath>& file_paths, | |
689 ImageCache* raw_bitmaps) { | |
690 for (std::map<int, FilePath>::const_iterator it = file_paths.begin(); | |
691 it != file_paths.end(); ++it) { | |
692 scoped_refptr<RefCountedMemory> raw_data(ReadFileData(it->second)); | |
693 int id = it->first; | |
694 | |
695 // Some images need to go directly into |image_memory_|. No modification is | |
696 // necessary or desirable. | |
697 bool is_copyable = false; | |
698 for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) { | |
699 if (kPreloadIDs[i] == id) { | |
700 is_copyable = true; | |
701 break; | |
702 } | |
703 } | |
704 | |
705 if (is_copyable) { | |
706 image_memory_[id] = raw_data; | |
707 } else if (raw_data.get() && raw_data->size()) { | |
708 // Decode the PNG. | |
709 SkBitmap bitmap; | |
710 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), | |
711 &bitmap)) { | |
712 (*raw_bitmaps)[it->first] = new SkBitmap(bitmap); | |
713 } else { | |
714 NOTREACHED() << "Unable to decode theme image resource " << it->first; | |
715 } | |
716 } | |
717 } | |
718 } | |
719 | |
720 void BrowserThemePack::GenerateFrameImages(ImageCache* bitmaps) const { | |
721 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
722 | |
723 // Create all the output bitmaps in a separate cache and move them back into | |
724 // the input bitmaps because there can be name collisions. | |
725 ImageCache temp_output; | |
726 | |
727 for (size_t i = 0; i < arraysize(kFrameTintMap); ++i) { | |
728 int id = kFrameTintMap[i].key; | |
729 scoped_ptr<SkBitmap> frame; | |
730 // If there's no frame image provided for the specified id, then load | |
731 // the default provided frame. If that's not provided, skip this whole | |
732 // thing and just use the default images. | |
733 int base_id; | |
734 | |
735 if (id == IDR_THEME_FRAME_INCOGNITO_INACTIVE) { | |
736 base_id = bitmaps->count(IDR_THEME_FRAME_INCOGNITO) ? | |
737 IDR_THEME_FRAME_INCOGNITO : IDR_THEME_FRAME; | |
738 } else if (id == IDR_THEME_FRAME_OVERLAY_INACTIVE) { | |
739 base_id = IDR_THEME_FRAME_OVERLAY; | |
740 } else if (id == IDR_THEME_FRAME_INACTIVE) { | |
741 base_id = IDR_THEME_FRAME; | |
742 } else if (id == IDR_THEME_FRAME_INCOGNITO && | |
743 !bitmaps->count(IDR_THEME_FRAME_INCOGNITO)) { | |
744 base_id = IDR_THEME_FRAME; | |
745 } else { | |
746 base_id = id; | |
747 } | |
748 | |
749 if (bitmaps->count(id)) { | |
750 frame.reset(new SkBitmap(*(*bitmaps)[id])); | |
751 } else if (base_id != id && bitmaps->count(base_id)) { | |
752 frame.reset(new SkBitmap(*(*bitmaps)[base_id])); | |
753 } else if (base_id == IDR_THEME_FRAME_OVERLAY && | |
754 bitmaps->count(IDR_THEME_FRAME)) { | |
755 // If there is no theme overlay, don't tint the default frame, | |
756 // because it will overwrite the custom frame image when we cache and | |
757 // reload from disk. | |
758 frame.reset(NULL); | |
759 } else { | |
760 // If the theme doesn't specify an image, then apply the tint to | |
761 // the default frame. | |
762 frame.reset(new SkBitmap(*rb.GetBitmapNamed(IDR_THEME_FRAME))); | |
763 } | |
764 | |
765 if (frame.get()) { | |
766 temp_output[id] = new SkBitmap( | |
767 SkBitmapOperations::CreateHSLShiftedBitmap( | |
768 *frame, GetTintInternal(kFrameTintMap[i].value))); | |
769 } | |
770 } | |
771 | |
772 MergeImageCaches(temp_output, bitmaps); | |
773 } | |
774 | |
775 void BrowserThemePack::GenerateTintedButtons( | |
776 color_utils::HSL button_tint, | |
777 ImageCache* processed_bitmaps) const { | |
778 if (button_tint.h != -1 || button_tint.s != -1 || button_tint.l != -1) { | |
779 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
780 for (size_t i = 0; i < arraysize(kToolbarButtonIDs); ++i) { | |
781 scoped_ptr<SkBitmap> button( | |
782 new SkBitmap(*rb.GetBitmapNamed(kToolbarButtonIDs[i]))); | |
783 (*processed_bitmaps)[kToolbarButtonIDs[i]] = new SkBitmap( | |
784 SkBitmapOperations::CreateHSLShiftedBitmap(*button, button_tint)); | |
785 } | |
786 } | |
787 } | |
788 | |
789 void BrowserThemePack::GenerateTabBackgroundImages(ImageCache* bitmaps) const { | |
790 ImageCache temp_output; | |
791 for (size_t i = 0; i < arraysize(kTabBackgroundMap); ++i) { | |
792 int id = kTabBackgroundMap[i].key; | |
793 int base_id = kTabBackgroundMap[i].value; | |
794 | |
795 // We only need to generate the background tab images if we were provided | |
796 // with a IDR_THEME_FRAME. | |
797 ImageCache::const_iterator it = bitmaps->find(base_id); | |
798 if (it != bitmaps->end()) { | |
799 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap( | |
800 *(it->second), GetTintInternal( | |
801 BrowserThemeProvider::TINT_BACKGROUND_TAB)); | |
802 int vertical_offset = bitmaps->count(id) ? kRestoredTabVerticalOffset : 0; | |
803 SkBitmap* bg_tab = new SkBitmap(SkBitmapOperations::CreateTiledBitmap( | |
804 bg_tint, 0, vertical_offset, bg_tint.width(), bg_tint.height())); | |
805 | |
806 // If they've provided a custom image, overlay it. | |
807 ImageCache::const_iterator overlay_it = bitmaps->find(id); | |
808 if (overlay_it != bitmaps->end()) { | |
809 SkBitmap* overlay = overlay_it->second; | |
810 SkCanvas canvas(*bg_tab); | |
811 for (int x = 0; x < bg_tab->width(); x += overlay->width()) | |
812 canvas.drawBitmap(*overlay, static_cast<SkScalar>(x), 0, NULL); | |
813 } | |
814 | |
815 temp_output[id] = bg_tab; | |
816 } | |
817 } | |
818 | |
819 MergeImageCaches(temp_output, bitmaps); | |
820 } | |
821 | |
822 void BrowserThemePack::RepackImageCacheToImageMemory() { | |
823 // TODO(erg): This shouldn't be done on the main thread. This should be done | |
824 // during the WriteToDisk() method, but will require some tricky locking. | |
825 for (ImageCache::const_iterator it = image_cache_.begin(); | |
826 it != image_cache_.end(); ++it) { | |
827 std::vector<unsigned char> image_data; | |
828 if (!gfx::PNGCodec::EncodeBGRASkBitmap(*(it->second), false, &image_data)) { | |
829 NOTREACHED() << "Image file for resource " << it->first | |
830 << " could not be encoded."; | |
831 } else { | |
832 image_memory_[it->first] = RefCountedBytes::TakeVector(&image_data); | |
833 } | |
834 } | |
835 } | |
836 | |
837 void BrowserThemePack::MergeImageCaches( | |
838 const ImageCache& source, ImageCache* destination) const { | |
839 | |
840 for (ImageCache::const_iterator it = source.begin(); it != source.end(); | |
841 ++it) { | |
842 ImageCache::const_iterator bitmap_it = destination->find(it->first); | |
843 if (bitmap_it != destination->end()) | |
844 delete bitmap_it->second; | |
845 | |
846 (*destination)[it->first] = it->second; | |
847 } | |
848 } | |
849 | |
850 color_utils::HSL BrowserThemePack::GetTintInternal(int id) const { | |
851 if (tints_) { | |
852 for (int i = 0; i < kTintArraySize; ++i) { | |
853 if (tints_[i].id == id) { | |
854 color_utils::HSL hsl; | |
855 hsl.h = tints_[i].h; | |
856 hsl.s = tints_[i].s; | |
857 hsl.l = tints_[i].l; | |
858 return hsl; | |
859 } | |
860 } | |
861 } | |
862 | |
863 return BrowserThemeProvider::GetDefaultTint(id); | |
864 } | |
OLD | NEW |