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
3 // found in the LICENSE file.
5 #include "chrome/browser/themes/browser_theme_pack.h"
9 #include "base/files/file.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/values.h"
19 #include "chrome/browser/themes/theme_properties.h"
20 #include "chrome/common/extensions/manifest_handlers/theme_handler.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "extensions/common/id_util.h"
23 #include "grit/theme_resources.h"
24 #include "grit/ui_resources.h"
25 #include "third_party/skia/include/core/SkCanvas.h"
26 #include "ui/base/resource/data_pack.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/canvas.h"
29 #include "ui/gfx/codec/png_codec.h"
30 #include "ui/gfx/image/canvas_image_source.h"
31 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/image/image_skia_operations.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/size_conversions.h"
36 #include "ui/gfx/skia_util.h"
38 using content::BrowserThread
;
39 using extensions::Extension
;
43 // Version number of the current theme pack. We just throw out and rebuild
44 // theme packs that aren't int-equal to this. Increment this number if you
45 // change default theme assets.
46 const int kThemePackVersion
= 33;
48 // IDs that are in the DataPack won't clash with the positive integer
49 // uint16. kHeaderID should always have the maximum value because we want the
50 // "header" to be written last. That way we can detect whether the pack was
51 // successfully written and ignore and regenerate if it was only partially
52 // written (i.e. chrome crashed on a different thread while writing the pack).
53 const int kMaxID
= 0x0000FFFF; // Max unsigned 16-bit int.
54 const int kHeaderID
= kMaxID
- 1;
55 const int kTintsID
= kMaxID
- 2;
56 const int kColorsID
= kMaxID
- 3;
57 const int kDisplayPropertiesID
= kMaxID
- 4;
58 const int kSourceImagesID
= kMaxID
- 5;
59 const int kScaleFactorsID
= kMaxID
- 6;
61 // The sum of kFrameBorderThickness and kNonClientRestoredExtraThickness from
62 // OpaqueBrowserFrameView.
63 const int kRestoredTabVerticalOffset
= 15;
65 // Persistent constants for the main images that we need. These have the same
66 // names as their IDR_* counterparts but these values will always stay the
68 const int PRS_THEME_FRAME
= 1;
69 const int PRS_THEME_FRAME_INACTIVE
= 2;
70 const int PRS_THEME_FRAME_INCOGNITO
= 3;
71 const int PRS_THEME_FRAME_INCOGNITO_INACTIVE
= 4;
72 const int PRS_THEME_TOOLBAR
= 5;
73 const int PRS_THEME_TAB_BACKGROUND
= 6;
74 const int PRS_THEME_TAB_BACKGROUND_INCOGNITO
= 7;
75 const int PRS_THEME_TAB_BACKGROUND_V
= 8;
76 const int PRS_THEME_NTP_BACKGROUND
= 9;
77 const int PRS_THEME_FRAME_OVERLAY
= 10;
78 const int PRS_THEME_FRAME_OVERLAY_INACTIVE
= 11;
79 const int PRS_THEME_BUTTON_BACKGROUND
= 12;
80 const int PRS_THEME_NTP_ATTRIBUTION
= 13;
81 const int PRS_THEME_WINDOW_CONTROL_BACKGROUND
= 14;
83 struct PersistingImagesTable
{
84 // A non-changing integer ID meant to be saved in theme packs. This ID must
85 // not change between versions of chrome.
88 // The IDR that depends on the whims of GRIT and therefore changes whenever
89 // someone adds a new resource.
92 // String to check for when parsing theme manifests or NULL if this isn't
93 // supposed to be changeable by the user.
97 // IDR_* resource names change whenever new resources are added; use persistent
98 // IDs when storing to a cached pack.
99 PersistingImagesTable kPersistingImages
[] = {
100 { PRS_THEME_FRAME
, IDR_THEME_FRAME
,
102 { PRS_THEME_FRAME_INACTIVE
, IDR_THEME_FRAME_INACTIVE
,
103 "theme_frame_inactive" },
104 { PRS_THEME_FRAME_INCOGNITO
, IDR_THEME_FRAME_INCOGNITO
,
105 "theme_frame_incognito" },
106 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
, IDR_THEME_FRAME_INCOGNITO_INACTIVE
,
107 "theme_frame_incognito_inactive" },
108 { PRS_THEME_TOOLBAR
, IDR_THEME_TOOLBAR
,
110 { PRS_THEME_TAB_BACKGROUND
, IDR_THEME_TAB_BACKGROUND
,
111 "theme_tab_background" },
112 { PRS_THEME_TAB_BACKGROUND_INCOGNITO
, IDR_THEME_TAB_BACKGROUND_INCOGNITO
,
113 "theme_tab_background_incognito" },
114 { PRS_THEME_TAB_BACKGROUND_V
, IDR_THEME_TAB_BACKGROUND_V
,
115 "theme_tab_background_v"},
116 { PRS_THEME_NTP_BACKGROUND
, IDR_THEME_NTP_BACKGROUND
,
117 "theme_ntp_background" },
118 { PRS_THEME_FRAME_OVERLAY
, IDR_THEME_FRAME_OVERLAY
,
119 "theme_frame_overlay" },
120 { PRS_THEME_FRAME_OVERLAY_INACTIVE
, IDR_THEME_FRAME_OVERLAY_INACTIVE
,
121 "theme_frame_overlay_inactive" },
122 { PRS_THEME_BUTTON_BACKGROUND
, IDR_THEME_BUTTON_BACKGROUND
,
123 "theme_button_background" },
124 { PRS_THEME_NTP_ATTRIBUTION
, IDR_THEME_NTP_ATTRIBUTION
,
125 "theme_ntp_attribution" },
126 { PRS_THEME_WINDOW_CONTROL_BACKGROUND
, IDR_THEME_WINDOW_CONTROL_BACKGROUND
,
127 "theme_window_control_background"},
129 // The rest of these entries have no key because they can't be overridden
130 // from the json manifest.
131 { 15, IDR_BACK
, NULL
},
132 { 16, IDR_BACK_D
, NULL
},
133 { 17, IDR_BACK_H
, NULL
},
134 { 18, IDR_BACK_P
, NULL
},
135 { 19, IDR_FORWARD
, NULL
},
136 { 20, IDR_FORWARD_D
, NULL
},
137 { 21, IDR_FORWARD_H
, NULL
},
138 { 22, IDR_FORWARD_P
, NULL
},
139 { 23, IDR_HOME
, NULL
},
140 { 24, IDR_HOME_H
, NULL
},
141 { 25, IDR_HOME_P
, NULL
},
142 { 26, IDR_RELOAD
, NULL
},
143 { 27, IDR_RELOAD_H
, NULL
},
144 { 28, IDR_RELOAD_P
, NULL
},
145 { 29, IDR_STOP
, NULL
},
146 { 30, IDR_STOP_D
, NULL
},
147 { 31, IDR_STOP_H
, NULL
},
148 { 32, IDR_STOP_P
, NULL
},
149 { 33, IDR_BROWSER_ACTIONS_OVERFLOW
, NULL
},
150 { 34, IDR_BROWSER_ACTIONS_OVERFLOW_H
, NULL
},
151 { 35, IDR_BROWSER_ACTIONS_OVERFLOW_P
, NULL
},
152 { 36, IDR_TOOLS
, NULL
},
153 { 37, IDR_TOOLS_H
, NULL
},
154 { 38, IDR_TOOLS_P
, NULL
},
155 { 39, IDR_MENU_DROPARROW
, NULL
},
156 { 40, IDR_THROBBER
, NULL
},
157 { 41, IDR_THROBBER_WAITING
, NULL
},
158 { 42, IDR_THROBBER_LIGHT
, NULL
},
159 { 43, IDR_TOOLBAR_BEZEL_HOVER
, NULL
},
160 { 44, IDR_TOOLBAR_BEZEL_PRESSED
, NULL
},
161 { 45, IDR_TOOLS_BAR
, NULL
},
163 const size_t kPersistingImagesLength
= arraysize(kPersistingImages
);
166 // Persistent theme ids for Windows.
167 const int PRS_THEME_FRAME_WIN
= 100;
168 const int PRS_THEME_FRAME_INACTIVE_WIN
= 101;
169 const int PRS_THEME_FRAME_INCOGNITO_WIN
= 102;
170 const int PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN
= 103;
171 const int PRS_THEME_TOOLBAR_WIN
= 104;
172 const int PRS_THEME_TAB_BACKGROUND_WIN
= 105;
173 const int PRS_THEME_TAB_BACKGROUND_INCOGNITO_WIN
= 106;
175 // Persistent theme to resource id mapping for Windows AURA.
176 PersistingImagesTable kPersistingImagesWinDesktopAura
[] = {
177 { PRS_THEME_FRAME_WIN
, IDR_THEME_FRAME_WIN
,
179 { PRS_THEME_FRAME_INACTIVE_WIN
, IDR_THEME_FRAME_INACTIVE_WIN
,
180 "theme_frame_inactive" },
181 { PRS_THEME_FRAME_INCOGNITO_WIN
, IDR_THEME_FRAME_INCOGNITO_WIN
,
182 "theme_frame_incognito" },
183 { PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN
,
184 IDR_THEME_FRAME_INCOGNITO_INACTIVE_WIN
,
185 "theme_frame_incognito_inactive" },
186 { PRS_THEME_TOOLBAR_WIN
, IDR_THEME_TOOLBAR_WIN
,
188 { PRS_THEME_TAB_BACKGROUND_WIN
, IDR_THEME_TAB_BACKGROUND_WIN
,
189 "theme_tab_background" },
190 { PRS_THEME_TAB_BACKGROUND_INCOGNITO_WIN
,
191 IDR_THEME_TAB_BACKGROUND_INCOGNITO_WIN
,
192 "theme_tab_background_incognito" },
194 const size_t kPersistingImagesWinDesktopAuraLength
=
195 arraysize(kPersistingImagesWinDesktopAura
);
198 int GetPersistentIDByNameHelper(const std::string
& key
,
199 const PersistingImagesTable
* image_table
,
200 size_t image_table_size
) {
201 for (size_t i
= 0; i
< image_table_size
; ++i
) {
202 if (image_table
[i
].key
!= NULL
&&
203 base::strcasecmp(key
.c_str(), image_table
[i
].key
) == 0) {
204 return image_table
[i
].persistent_id
;
210 int GetPersistentIDByName(const std::string
& key
) {
211 return GetPersistentIDByNameHelper(key
,
213 kPersistingImagesLength
);
216 int GetPersistentIDByIDR(int idr
) {
217 static std::map
<int,int>* lookup_table
= new std::map
<int,int>();
218 if (lookup_table
->empty()) {
219 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
) {
220 int idr
= kPersistingImages
[i
].idr_id
;
221 int prs_id
= kPersistingImages
[i
].persistent_id
;
222 (*lookup_table
)[idr
] = prs_id
;
225 for (size_t i
= 0; i
< kPersistingImagesWinDesktopAuraLength
; ++i
) {
226 int idr
= kPersistingImagesWinDesktopAura
[i
].idr_id
;
227 int prs_id
= kPersistingImagesWinDesktopAura
[i
].persistent_id
;
228 (*lookup_table
)[idr
] = prs_id
;
232 std::map
<int,int>::iterator it
= lookup_table
->find(idr
);
233 return (it
== lookup_table
->end()) ? -1 : it
->second
;
236 // Returns true if the scales in |input| match those in |expected|.
237 // The order must match as the index is used in determining the raw id.
238 bool InputScalesValid(const base::StringPiece
& input
,
239 const std::vector
<ui::ScaleFactor
>& expected
) {
240 size_t scales_size
= static_cast<size_t>(input
.size() / sizeof(float));
241 if (scales_size
!= expected
.size())
243 scoped_ptr
<float[]> scales(new float[scales_size
]);
244 // Do a memcpy to avoid misaligned memory access.
245 memcpy(scales
.get(), input
.data(), input
.size());
246 for (size_t index
= 0; index
< scales_size
; ++index
) {
247 if (scales
[index
] != ui::GetImageScale(expected
[index
]))
253 // Returns |scale_factors| as a string to be written to disk.
254 std::string
GetScaleFactorsAsString(
255 const std::vector
<ui::ScaleFactor
>& scale_factors
) {
256 scoped_ptr
<float[]> scales(new float[scale_factors
.size()]);
257 for (size_t i
= 0; i
< scale_factors
.size(); ++i
)
258 scales
[i
] = ui::GetImageScale(scale_factors
[i
]);
259 std::string out_string
= std::string(
260 reinterpret_cast<const char*>(scales
.get()),
261 scale_factors
.size() * sizeof(float));
265 struct StringToIntTable
{
267 ThemeProperties::OverwritableByUserThemeProperty id
;
270 // Strings used by themes to identify tints in the JSON.
271 StringToIntTable kTintTable
[] = {
272 { "buttons", ThemeProperties::TINT_BUTTONS
},
273 { "frame", ThemeProperties::TINT_FRAME
},
274 { "frame_inactive", ThemeProperties::TINT_FRAME_INACTIVE
},
275 { "frame_incognito", ThemeProperties::TINT_FRAME_INCOGNITO
},
276 { "frame_incognito_inactive",
277 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
278 { "background_tab", ThemeProperties::TINT_BACKGROUND_TAB
},
280 const size_t kTintTableLength
= arraysize(kTintTable
);
282 // Strings used by themes to identify colors in the JSON.
283 StringToIntTable kColorTable
[] = {
284 { "frame", ThemeProperties::COLOR_FRAME
},
285 { "frame_inactive", ThemeProperties::COLOR_FRAME_INACTIVE
},
286 { "frame_incognito", ThemeProperties::COLOR_FRAME_INCOGNITO
},
287 { "frame_incognito_inactive",
288 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
},
289 { "toolbar", ThemeProperties::COLOR_TOOLBAR
},
290 { "tab_text", ThemeProperties::COLOR_TAB_TEXT
},
291 { "tab_background_text", ThemeProperties::COLOR_BACKGROUND_TAB_TEXT
},
292 { "bookmark_text", ThemeProperties::COLOR_BOOKMARK_TEXT
},
293 { "ntp_background", ThemeProperties::COLOR_NTP_BACKGROUND
},
294 { "ntp_text", ThemeProperties::COLOR_NTP_TEXT
},
295 { "ntp_link", ThemeProperties::COLOR_NTP_LINK
},
296 { "ntp_link_underline", ThemeProperties::COLOR_NTP_LINK_UNDERLINE
},
297 { "ntp_header", ThemeProperties::COLOR_NTP_HEADER
},
298 { "ntp_section", ThemeProperties::COLOR_NTP_SECTION
},
299 { "ntp_section_text", ThemeProperties::COLOR_NTP_SECTION_TEXT
},
300 { "ntp_section_link", ThemeProperties::COLOR_NTP_SECTION_LINK
},
301 { "ntp_section_link_underline",
302 ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
},
303 { "button_background", ThemeProperties::COLOR_BUTTON_BACKGROUND
},
305 const size_t kColorTableLength
= arraysize(kColorTable
);
307 // Strings used by themes to identify display properties keys in JSON.
308 StringToIntTable kDisplayProperties
[] = {
309 { "ntp_background_alignment",
310 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
},
311 { "ntp_background_repeat", ThemeProperties::NTP_BACKGROUND_TILING
},
312 { "ntp_logo_alternate", ThemeProperties::NTP_LOGO_ALTERNATE
},
314 const size_t kDisplayPropertiesSize
= arraysize(kDisplayProperties
);
316 int GetIntForString(const std::string
& key
,
317 StringToIntTable
* table
,
318 size_t table_length
) {
319 for (size_t i
= 0; i
< table_length
; ++i
) {
320 if (base::strcasecmp(key
.c_str(), table
[i
].key
) == 0) {
328 struct IntToIntTable
{
333 // Mapping used in CreateFrameImages() to associate frame images with the
334 // tint ID that should maybe be applied to it.
335 IntToIntTable kFrameTintMap
[] = {
336 { PRS_THEME_FRAME
, ThemeProperties::TINT_FRAME
},
337 { PRS_THEME_FRAME_INACTIVE
, ThemeProperties::TINT_FRAME_INACTIVE
},
338 { PRS_THEME_FRAME_OVERLAY
, ThemeProperties::TINT_FRAME
},
339 { PRS_THEME_FRAME_OVERLAY_INACTIVE
,
340 ThemeProperties::TINT_FRAME_INACTIVE
},
341 { PRS_THEME_FRAME_INCOGNITO
, ThemeProperties::TINT_FRAME_INCOGNITO
},
342 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
,
343 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
345 { PRS_THEME_FRAME_WIN
, ThemeProperties::TINT_FRAME
},
346 { PRS_THEME_FRAME_INACTIVE_WIN
, ThemeProperties::TINT_FRAME_INACTIVE
},
347 { PRS_THEME_FRAME_INCOGNITO_WIN
, ThemeProperties::TINT_FRAME_INCOGNITO
},
348 { PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN
,
349 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
353 // Mapping used in GenerateTabBackgroundImages() to associate what frame image
354 // goes with which tab background.
355 IntToIntTable kTabBackgroundMap
[] = {
356 { PRS_THEME_TAB_BACKGROUND
, PRS_THEME_FRAME
},
357 { PRS_THEME_TAB_BACKGROUND_INCOGNITO
, PRS_THEME_FRAME_INCOGNITO
},
359 { PRS_THEME_TAB_BACKGROUND_WIN
, PRS_THEME_FRAME_WIN
},
360 { PRS_THEME_TAB_BACKGROUND_INCOGNITO_WIN
, PRS_THEME_FRAME_INCOGNITO_WIN
},
367 // The maximum useful height of the image at |prs_id|.
370 // Whether cropping the image at |prs_id| should be skipped on OSes which
371 // have a frame border to the left and right of the web contents.
372 // This should be true for images which can be used to decorate the border to
373 // the left and the right of the web contents.
374 bool skip_if_frame_border
;
377 // The images which should be cropped before being saved to the data pack. The
378 // maximum heights are meant to be conservative as to give room for the UI to
379 // change without the maximum heights having to be modified.
380 // |kThemePackVersion| must be incremented if any of the maximum heights below
382 struct CropEntry kImagesToCrop
[] = {
383 { PRS_THEME_FRAME
, 120, true },
384 { PRS_THEME_FRAME_INACTIVE
, 120, true },
385 { PRS_THEME_FRAME_INCOGNITO
, 120, true },
386 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
, 120, true },
387 { PRS_THEME_FRAME_OVERLAY
, 120, true },
388 { PRS_THEME_FRAME_OVERLAY_INACTIVE
, 120, true },
389 { PRS_THEME_TOOLBAR
, 200, false },
390 { PRS_THEME_BUTTON_BACKGROUND
, 60, false },
391 { PRS_THEME_WINDOW_CONTROL_BACKGROUND
, 50, false },
393 { PRS_THEME_TOOLBAR_WIN
, 200, false }
398 // A list of images that don't need tinting or any other modification and can
399 // be byte-copied directly into the finished DataPack. This should contain the
400 // persistent IDs for all themeable image IDs that aren't in kFrameTintMap,
401 // kTabBackgroundMap or kImagesToCrop.
402 const int kPreloadIDs
[] = {
403 PRS_THEME_NTP_BACKGROUND
,
404 PRS_THEME_NTP_ATTRIBUTION
,
407 // Returns true if this OS uses a browser frame which has a non zero width to
408 // the left and the right of the web contents.
409 bool HasFrameBorder() {
410 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
417 // Returns a piece of memory with the contents of the file |path|.
418 base::RefCountedMemory
* ReadFileData(const base::FilePath
& path
) {
420 base::File
file(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
421 if (file
.IsValid()) {
422 int64 length
= file
.GetLength();
423 if (length
> 0 && length
< INT_MAX
) {
424 int size
= static_cast<int>(length
);
425 std::vector
<unsigned char> raw_data
;
426 raw_data
.resize(size
);
427 char* data
= reinterpret_cast<char*>(&(raw_data
.front()));
428 if (file
.ReadAtCurrentPos(data
, size
) == length
)
429 return base::RefCountedBytes::TakeVector(&raw_data
);
437 // Shifts an image's HSL values. The caller is responsible for deleting
438 // the returned image.
439 gfx::Image
CreateHSLShiftedImage(const gfx::Image
& image
,
440 const color_utils::HSL
& hsl_shift
) {
441 const gfx::ImageSkia
* src_image
= image
.ToImageSkia();
442 return gfx::Image(gfx::ImageSkiaOperations::CreateHSLShiftedImage(
443 *src_image
, hsl_shift
));
446 // Computes a bitmap at one scale from a bitmap at a different scale.
447 SkBitmap
CreateLowQualityResizedBitmap(const SkBitmap
& source_bitmap
,
448 ui::ScaleFactor source_scale_factor
,
449 ui::ScaleFactor desired_scale_factor
) {
450 gfx::Size scaled_size
= gfx::ToCeiledSize(
451 gfx::ScaleSize(gfx::Size(source_bitmap
.width(),
452 source_bitmap
.height()),
453 ui::GetImageScale(desired_scale_factor
) /
454 ui::GetImageScale(source_scale_factor
)));
455 SkBitmap scaled_bitmap
;
456 scaled_bitmap
.setConfig(SkBitmap::kARGB_8888_Config
,
458 scaled_size
.height());
459 if (!scaled_bitmap
.allocPixels())
461 scaled_bitmap
.eraseARGB(0, 0, 0, 0);
462 SkCanvas
canvas(scaled_bitmap
);
463 SkRect scaled_bounds
= RectToSkRect(gfx::Rect(scaled_size
));
464 // Note(oshima): The following scaling code doesn't work with
466 canvas
.drawBitmapRect(source_bitmap
, NULL
, scaled_bounds
);
467 return scaled_bitmap
;
470 // A ImageSkiaSource that scales 100P image to the target scale factor
471 // if the ImageSkiaRep for the target scale factor isn't available.
472 class ThemeImageSource
: public gfx::ImageSkiaSource
{
474 explicit ThemeImageSource(const gfx::ImageSkia
& source
) : source_(source
) {
476 virtual ~ThemeImageSource() {}
478 virtual gfx::ImageSkiaRep
GetImageForScale(float scale
) OVERRIDE
{
479 if (source_
.HasRepresentation(scale
))
480 return source_
.GetRepresentation(scale
);
481 const gfx::ImageSkiaRep
& rep_100p
= source_
.GetRepresentation(1.0f
);
482 SkBitmap scaled_bitmap
= CreateLowQualityResizedBitmap(
483 rep_100p
.sk_bitmap(),
484 ui::SCALE_FACTOR_100P
,
485 ui::GetSupportedScaleFactor(scale
));
486 return gfx::ImageSkiaRep(scaled_bitmap
, scale
);
490 const gfx::ImageSkia source_
;
492 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource
);
495 // An ImageSkiaSource that delays decoding PNG data into bitmaps until
496 // needed. Missing data for a scale factor is computed by scaling data for an
497 // available scale factor. Computed bitmaps are stored for future look up.
498 class ThemeImagePngSource
: public gfx::ImageSkiaSource
{
500 typedef std::map
<ui::ScaleFactor
,
501 scoped_refptr
<base::RefCountedMemory
> > PngMap
;
503 explicit ThemeImagePngSource(const PngMap
& png_map
) : png_map_(png_map
) {}
505 virtual ~ThemeImagePngSource() {}
508 virtual gfx::ImageSkiaRep
GetImageForScale(float scale
) OVERRIDE
{
509 ui::ScaleFactor scale_factor
= ui::GetSupportedScaleFactor(scale
);
510 // Look up the bitmap for |scale factor| in the bitmap map. If found
512 BitmapMap::const_iterator exact_bitmap_it
= bitmap_map_
.find(scale_factor
);
513 if (exact_bitmap_it
!= bitmap_map_
.end())
514 return gfx::ImageSkiaRep(exact_bitmap_it
->second
, scale
);
516 // Look up the raw PNG data for |scale_factor| in the png map. If found,
517 // decode it, store the result in the bitmap map and return it.
518 PngMap::const_iterator exact_png_it
= png_map_
.find(scale_factor
);
519 if (exact_png_it
!= png_map_
.end()) {
521 if (!gfx::PNGCodec::Decode(exact_png_it
->second
->front(),
522 exact_png_it
->second
->size(),
525 return gfx::ImageSkiaRep();
527 bitmap_map_
[scale_factor
] = bitmap
;
528 return gfx::ImageSkiaRep(bitmap
, scale
);
531 // Find an available PNG for another scale factor. We want to use the
532 // highest available scale factor.
533 PngMap::const_iterator available_png_it
= png_map_
.end();
534 for (PngMap::const_iterator png_it
= png_map_
.begin();
535 png_it
!= png_map_
.end(); ++png_it
) {
536 if (available_png_it
== png_map_
.end() ||
537 ui::GetImageScale(png_it
->first
) >
538 ui::GetImageScale(available_png_it
->first
)) {
539 available_png_it
= png_it
;
542 if (available_png_it
== png_map_
.end())
543 return gfx::ImageSkiaRep();
544 ui::ScaleFactor available_scale_factor
= available_png_it
->first
;
546 // Look up the bitmap for |available_scale_factor| in the bitmap map.
547 // If not found, decode the corresponging png data, store the result
548 // in the bitmap map.
549 BitmapMap::const_iterator available_bitmap_it
=
550 bitmap_map_
.find(available_scale_factor
);
551 if (available_bitmap_it
== bitmap_map_
.end()) {
552 SkBitmap available_bitmap
;
553 if (!gfx::PNGCodec::Decode(available_png_it
->second
->front(),
554 available_png_it
->second
->size(),
555 &available_bitmap
)) {
557 return gfx::ImageSkiaRep();
559 bitmap_map_
[available_scale_factor
] = available_bitmap
;
560 available_bitmap_it
= bitmap_map_
.find(available_scale_factor
);
563 // Scale the available bitmap to the desired scale factor, store the result
564 // in the bitmap map and return it.
565 SkBitmap scaled_bitmap
= CreateLowQualityResizedBitmap(
566 available_bitmap_it
->second
,
567 available_scale_factor
,
569 bitmap_map_
[scale_factor
] = scaled_bitmap
;
570 return gfx::ImageSkiaRep(scaled_bitmap
, scale
);
575 typedef std::map
<ui::ScaleFactor
, SkBitmap
> BitmapMap
;
576 BitmapMap bitmap_map_
;
578 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource
);
581 class TabBackgroundImageSource
: public gfx::CanvasImageSource
{
583 TabBackgroundImageSource(const gfx::ImageSkia
& image_to_tint
,
584 const gfx::ImageSkia
& overlay
,
585 const color_utils::HSL
& hsl_shift
,
587 : gfx::CanvasImageSource(image_to_tint
.size(), false),
588 image_to_tint_(image_to_tint
),
590 hsl_shift_(hsl_shift
),
591 vertical_offset_(vertical_offset
) {
594 virtual ~TabBackgroundImageSource() {
597 // Overridden from CanvasImageSource:
598 virtual void Draw(gfx::Canvas
* canvas
) OVERRIDE
{
599 gfx::ImageSkia bg_tint
=
600 gfx::ImageSkiaOperations::CreateHSLShiftedImage(image_to_tint_
,
602 canvas
->TileImageInt(bg_tint
, 0, vertical_offset_
, 0, 0,
603 size().width(), size().height());
605 // If they've provided a custom image, overlay it.
606 if (!overlay_
.isNull()) {
607 canvas
->TileImageInt(overlay_
, 0, 0, size().width(),
613 const gfx::ImageSkia image_to_tint_
;
614 const gfx::ImageSkia overlay_
;
615 const color_utils::HSL hsl_shift_
;
616 const int vertical_offset_
;
618 DISALLOW_COPY_AND_ASSIGN(TabBackgroundImageSource
);
623 BrowserThemePack::~BrowserThemePack() {
624 if (!data_pack_
.get()) {
628 delete [] display_properties_
;
629 delete [] source_images_
;
634 scoped_refptr
<BrowserThemePack
> BrowserThemePack::BuildFromExtension(
635 const Extension
* extension
) {
636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
638 DCHECK(extension
->is_theme());
640 scoped_refptr
<BrowserThemePack
> pack(new BrowserThemePack
);
641 pack
->BuildHeader(extension
);
642 pack
->BuildTintsFromJSON(extensions::ThemeInfo::GetTints(extension
));
643 pack
->BuildColorsFromJSON(extensions::ThemeInfo::GetColors(extension
));
644 pack
->BuildDisplayPropertiesFromJSON(
645 extensions::ThemeInfo::GetDisplayProperties(extension
));
647 // Builds the images. (Image building is dependent on tints).
648 FilePathMap file_paths
;
649 pack
->ParseImageNamesFromJSON(
650 extensions::ThemeInfo::GetImages(extension
),
653 pack
->BuildSourceImagesArray(file_paths
);
655 if (!pack
->LoadRawBitmapsTo(file_paths
, &pack
->images_on_ui_thread_
))
658 pack
->CreateImages(&pack
->images_on_ui_thread_
);
660 // Make sure the |images_on_file_thread_| has bitmaps for supported
661 // scale factors before passing to FILE thread.
662 pack
->images_on_file_thread_
= pack
->images_on_ui_thread_
;
663 for (ImageCache::iterator it
= pack
->images_on_file_thread_
.begin();
664 it
!= pack
->images_on_file_thread_
.end(); ++it
) {
665 gfx::ImageSkia
* image_skia
=
666 const_cast<gfx::ImageSkia
*>(it
->second
.ToImageSkia());
667 image_skia
->MakeThreadSafe();
670 // Set ThemeImageSource on |images_on_ui_thread_| to resample the source
671 // image if a caller of BrowserThemePack::GetImageNamed() requests an
672 // ImageSkiaRep for a scale factor not specified by the theme author.
673 // Callers of BrowserThemePack::GetImageNamed() to be able to retrieve
674 // ImageSkiaReps for all supported scale factors.
675 for (ImageCache::iterator it
= pack
->images_on_ui_thread_
.begin();
676 it
!= pack
->images_on_ui_thread_
.end(); ++it
) {
677 const gfx::ImageSkia source_image_skia
= it
->second
.AsImageSkia();
678 ThemeImageSource
* source
= new ThemeImageSource(source_image_skia
);
679 // image_skia takes ownership of source.
680 gfx::ImageSkia
image_skia(source
, source_image_skia
.size());
681 it
->second
= gfx::Image(image_skia
);
684 // Generate raw images (for new-tab-page attribution and background) for
685 // any missing scale from an available scale image.
686 for (size_t i
= 0; i
< arraysize(kPreloadIDs
); ++i
) {
687 pack
->GenerateRawImageForAllSupportedScales(kPreloadIDs
[i
]);
690 // The BrowserThemePack is now in a consistent state.
695 scoped_refptr
<BrowserThemePack
> BrowserThemePack::BuildFromDataPack(
696 const base::FilePath
& path
, const std::string
& expected_id
) {
697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
698 // Allow IO on UI thread due to deep-seated theme design issues.
699 // (see http://crbug.com/80206)
700 base::ThreadRestrictions::ScopedAllowIO allow_io
;
701 scoped_refptr
<BrowserThemePack
> pack(new BrowserThemePack
);
702 // Scale factor parameter is moot as data pack has image resources for all
703 // supported scale factors.
704 pack
->data_pack_
.reset(
705 new ui::DataPack(ui::SCALE_FACTOR_NONE
));
707 if (!pack
->data_pack_
->LoadFromPath(path
)) {
708 LOG(ERROR
) << "Failed to load theme data pack.";
712 base::StringPiece pointer
;
713 if (!pack
->data_pack_
->GetStringPiece(kHeaderID
, &pointer
))
715 pack
->header_
= reinterpret_cast<BrowserThemePackHeader
*>(const_cast<char*>(
718 if (pack
->header_
->version
!= kThemePackVersion
) {
719 DLOG(ERROR
) << "BuildFromDataPack failure! Version mismatch!";
722 // TODO(erg): Check endianess once DataPack works on the other endian.
723 std::string
theme_id(reinterpret_cast<char*>(pack
->header_
->theme_id
),
724 extensions::id_util::kIdSize
);
725 std::string truncated_id
=
726 expected_id
.substr(0, extensions::id_util::kIdSize
);
727 if (theme_id
!= truncated_id
) {
728 DLOG(ERROR
) << "Wrong id: " << theme_id
<< " vs " << expected_id
;
732 if (!pack
->data_pack_
->GetStringPiece(kTintsID
, &pointer
))
734 pack
->tints_
= reinterpret_cast<TintEntry
*>(const_cast<char*>(
737 if (!pack
->data_pack_
->GetStringPiece(kColorsID
, &pointer
))
740 reinterpret_cast<ColorPair
*>(const_cast<char*>(pointer
.data()));
742 if (!pack
->data_pack_
->GetStringPiece(kDisplayPropertiesID
, &pointer
))
744 pack
->display_properties_
= reinterpret_cast<DisplayPropertyPair
*>(
745 const_cast<char*>(pointer
.data()));
747 if (!pack
->data_pack_
->GetStringPiece(kSourceImagesID
, &pointer
))
749 pack
->source_images_
= reinterpret_cast<int*>(
750 const_cast<char*>(pointer
.data()));
752 if (!pack
->data_pack_
->GetStringPiece(kScaleFactorsID
, &pointer
))
755 if (!InputScalesValid(pointer
, pack
->scale_factors_
)) {
756 DLOG(ERROR
) << "BuildFromDataPack failure! The pack scale factors differ "
757 << "from those supported by platform.";
763 void BrowserThemePack::GetThemeableImageIDRs(std::set
<int>* result
) {
768 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
)
769 result
->insert(kPersistingImages
[i
].idr_id
);
772 for (size_t i
= 0; i
< kPersistingImagesWinDesktopAuraLength
; ++i
)
773 result
->insert(kPersistingImagesWinDesktopAura
[i
].idr_id
);
777 bool BrowserThemePack::WriteToDisk(const base::FilePath
& path
) const {
778 // Add resources for each of the property arrays.
779 RawDataForWriting resources
;
780 resources
[kHeaderID
] = base::StringPiece(
781 reinterpret_cast<const char*>(header_
), sizeof(BrowserThemePackHeader
));
782 resources
[kTintsID
] = base::StringPiece(
783 reinterpret_cast<const char*>(tints_
),
784 sizeof(TintEntry
[kTintTableLength
]));
785 resources
[kColorsID
] = base::StringPiece(
786 reinterpret_cast<const char*>(colors_
),
787 sizeof(ColorPair
[kColorTableLength
]));
788 resources
[kDisplayPropertiesID
] = base::StringPiece(
789 reinterpret_cast<const char*>(display_properties_
),
790 sizeof(DisplayPropertyPair
[kDisplayPropertiesSize
]));
792 int source_count
= 1;
793 int* end
= source_images_
;
794 for (; *end
!= -1 ; end
++)
796 resources
[kSourceImagesID
] = base::StringPiece(
797 reinterpret_cast<const char*>(source_images_
),
798 source_count
* sizeof(*source_images_
));
800 // Store results of GetScaleFactorsAsString() in std::string as
801 // base::StringPiece does not copy data in constructor.
802 std::string scale_factors_string
= GetScaleFactorsAsString(scale_factors_
);
803 resources
[kScaleFactorsID
] = scale_factors_string
;
805 AddRawImagesTo(image_memory_
, &resources
);
807 RawImages reencoded_images
;
808 RepackImages(images_on_file_thread_
, &reencoded_images
);
809 AddRawImagesTo(reencoded_images
, &resources
);
811 return ui::DataPack::WritePack(path
, resources
, ui::DataPack::BINARY
);
814 bool BrowserThemePack::GetTint(int id
, color_utils::HSL
* hsl
) const {
816 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
817 if (tints_
[i
].id
== id
) {
818 hsl
->h
= tints_
[i
].h
;
819 hsl
->s
= tints_
[i
].s
;
820 hsl
->l
= tints_
[i
].l
;
829 bool BrowserThemePack::GetColor(int id
, SkColor
* color
) const {
831 for (size_t i
= 0; i
< kColorTableLength
; ++i
) {
832 if (colors_
[i
].id
== id
) {
833 *color
= colors_
[i
].color
;
842 bool BrowserThemePack::GetDisplayProperty(int id
, int* result
) const {
843 if (display_properties_
) {
844 for (size_t i
= 0; i
< kDisplayPropertiesSize
; ++i
) {
845 if (display_properties_
[i
].id
== id
) {
846 *result
= display_properties_
[i
].property
;
855 gfx::Image
BrowserThemePack::GetImageNamed(int idr_id
) {
856 int prs_id
= GetPersistentIDByIDR(idr_id
);
860 // Check if the image is cached.
861 ImageCache::const_iterator image_iter
= images_on_ui_thread_
.find(prs_id
);
862 if (image_iter
!= images_on_ui_thread_
.end())
863 return image_iter
->second
;
865 ThemeImagePngSource::PngMap png_map
;
866 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
867 scoped_refptr
<base::RefCountedMemory
> memory
=
868 GetRawData(idr_id
, scale_factors_
[i
]);
870 png_map
[scale_factors_
[i
]] = memory
;
872 if (!png_map
.empty()) {
873 gfx::ImageSkia
image_skia(new ThemeImagePngSource(png_map
), 1.0f
);
874 // |image_skia| takes ownership of ThemeImagePngSource.
875 gfx::Image ret
= gfx::Image(image_skia
);
876 images_on_ui_thread_
[prs_id
] = ret
;
883 base::RefCountedMemory
* BrowserThemePack::GetRawData(
885 ui::ScaleFactor scale_factor
) const {
886 base::RefCountedMemory
* memory
= NULL
;
887 int prs_id
= GetPersistentIDByIDR(idr_id
);
888 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factor
);
891 if (data_pack_
.get()) {
892 memory
= data_pack_
->GetStaticMemory(raw_id
);
894 RawImages::const_iterator it
= image_memory_
.find(raw_id
);
895 if (it
!= image_memory_
.end()) {
896 memory
= it
->second
.get();
904 bool BrowserThemePack::HasCustomImage(int idr_id
) const {
905 int prs_id
= GetPersistentIDByIDR(idr_id
);
909 int* img
= source_images_
;
910 for (; *img
!= -1; ++img
) {
920 BrowserThemePack::BrowserThemePack()
921 : CustomThemeSupplier(EXTENSION
),
925 display_properties_(NULL
),
926 source_images_(NULL
) {
927 scale_factors_
= ui::GetSupportedScaleFactors();
930 void BrowserThemePack::BuildHeader(const Extension
* extension
) {
931 header_
= new BrowserThemePackHeader
;
932 header_
->version
= kThemePackVersion
;
934 // TODO(erg): Need to make this endian safe on other computers. Prerequisite
935 // is that ui::DataPack removes this same check.
936 #if defined(__BYTE_ORDER)
938 COMPILE_ASSERT(__BYTE_ORDER
== __LITTLE_ENDIAN
,
939 datapack_assumes_little_endian
);
940 #elif defined(__BIG_ENDIAN__)
942 #error DataPack assumes little endian
944 header_
->little_endian
= 1;
946 const std::string
& id
= extension
->id();
947 memcpy(header_
->theme_id
, id
.c_str(), extensions::id_util::kIdSize
);
950 void BrowserThemePack::BuildTintsFromJSON(
951 const base::DictionaryValue
* tints_value
) {
952 tints_
= new TintEntry
[kTintTableLength
];
953 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
963 // Parse the incoming data from |tints_value| into an intermediary structure.
964 std::map
<int, color_utils::HSL
> temp_tints
;
965 for (base::DictionaryValue::Iterator
iter(*tints_value
); !iter
.IsAtEnd();
967 const base::ListValue
* tint_list
;
968 if (iter
.value().GetAsList(&tint_list
) &&
969 (tint_list
->GetSize() == 3)) {
970 color_utils::HSL hsl
= { -1, -1, -1 };
972 if (tint_list
->GetDouble(0, &hsl
.h
) &&
973 tint_list
->GetDouble(1, &hsl
.s
) &&
974 tint_list
->GetDouble(2, &hsl
.l
)) {
975 int id
= GetIntForString(iter
.key(), kTintTable
, kTintTableLength
);
977 temp_tints
[id
] = hsl
;
983 // Copy data from the intermediary data structure to the array.
985 for (std::map
<int, color_utils::HSL
>::const_iterator it
=
987 it
!= temp_tints
.end() && count
< kTintTableLength
;
989 tints_
[count
].id
= it
->first
;
990 tints_
[count
].h
= it
->second
.h
;
991 tints_
[count
].s
= it
->second
.s
;
992 tints_
[count
].l
= it
->second
.l
;
996 void BrowserThemePack::BuildColorsFromJSON(
997 const base::DictionaryValue
* colors_value
) {
998 colors_
= new ColorPair
[kColorTableLength
];
999 for (size_t i
= 0; i
< kColorTableLength
; ++i
) {
1001 colors_
[i
].color
= SkColorSetRGB(0, 0, 0);
1004 std::map
<int, SkColor
> temp_colors
;
1006 ReadColorsFromJSON(colors_value
, &temp_colors
);
1007 GenerateMissingColors(&temp_colors
);
1009 // Copy data from the intermediary data structure to the array.
1011 for (std::map
<int, SkColor
>::const_iterator it
= temp_colors
.begin();
1012 it
!= temp_colors
.end() && count
< kColorTableLength
; ++it
, ++count
) {
1013 colors_
[count
].id
= it
->first
;
1014 colors_
[count
].color
= it
->second
;
1018 void BrowserThemePack::ReadColorsFromJSON(
1019 const base::DictionaryValue
* colors_value
,
1020 std::map
<int, SkColor
>* temp_colors
) {
1021 // Parse the incoming data from |colors_value| into an intermediary structure.
1022 for (base::DictionaryValue::Iterator
iter(*colors_value
); !iter
.IsAtEnd();
1024 const base::ListValue
* color_list
;
1025 if (iter
.value().GetAsList(&color_list
) &&
1026 ((color_list
->GetSize() == 3) || (color_list
->GetSize() == 4))) {
1027 SkColor color
= SK_ColorWHITE
;
1029 if (color_list
->GetInteger(0, &r
) &&
1030 color_list
->GetInteger(1, &g
) &&
1031 color_list
->GetInteger(2, &b
)) {
1032 if (color_list
->GetSize() == 4) {
1035 if (color_list
->GetDouble(3, &alpha
)) {
1036 color
= SkColorSetARGB(static_cast<int>(alpha
* 255), r
, g
, b
);
1037 } else if (color_list
->GetInteger(3, &alpha_int
) &&
1038 (alpha_int
== 0 || alpha_int
== 1)) {
1039 color
= SkColorSetARGB(alpha_int
? 255 : 0, r
, g
, b
);
1041 // Invalid entry for part 4.
1045 color
= SkColorSetRGB(r
, g
, b
);
1048 int id
= GetIntForString(iter
.key(), kColorTable
, kColorTableLength
);
1050 (*temp_colors
)[id
] = color
;
1057 void BrowserThemePack::GenerateMissingColors(
1058 std::map
<int, SkColor
>* colors
) {
1059 // Generate link colors, if missing. (See GetColor()).
1060 if (!colors
->count(ThemeProperties::COLOR_NTP_HEADER
) &&
1061 colors
->count(ThemeProperties::COLOR_NTP_SECTION
)) {
1062 (*colors
)[ThemeProperties::COLOR_NTP_HEADER
] =
1063 (*colors
)[ThemeProperties::COLOR_NTP_SECTION
];
1066 if (!colors
->count(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
) &&
1067 colors
->count(ThemeProperties::COLOR_NTP_SECTION_LINK
)) {
1068 SkColor color_section_link
=
1069 (*colors
)[ThemeProperties::COLOR_NTP_SECTION_LINK
];
1070 (*colors
)[ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
] =
1071 SkColorSetA(color_section_link
, SkColorGetA(color_section_link
) / 3);
1074 if (!colors
->count(ThemeProperties::COLOR_NTP_LINK_UNDERLINE
) &&
1075 colors
->count(ThemeProperties::COLOR_NTP_LINK
)) {
1076 SkColor color_link
= (*colors
)[ThemeProperties::COLOR_NTP_LINK
];
1077 (*colors
)[ThemeProperties::COLOR_NTP_LINK_UNDERLINE
] =
1078 SkColorSetA(color_link
, SkColorGetA(color_link
) / 3);
1081 // Generate frame colors, if missing. (See GenerateFrameColors()).
1083 std::map
<int, SkColor
>::const_iterator it
=
1084 colors
->find(ThemeProperties::COLOR_FRAME
);
1085 if (it
!= colors
->end()) {
1088 frame
= ThemeProperties::GetDefaultColor(
1089 ThemeProperties::COLOR_FRAME
);
1092 if (!colors
->count(ThemeProperties::COLOR_FRAME
)) {
1093 (*colors
)[ThemeProperties::COLOR_FRAME
] =
1094 HSLShift(frame
, GetTintInternal(ThemeProperties::TINT_FRAME
));
1096 if (!colors
->count(ThemeProperties::COLOR_FRAME_INACTIVE
)) {
1097 (*colors
)[ThemeProperties::COLOR_FRAME_INACTIVE
] =
1098 HSLShift(frame
, GetTintInternal(
1099 ThemeProperties::TINT_FRAME_INACTIVE
));
1101 if (!colors
->count(ThemeProperties::COLOR_FRAME_INCOGNITO
)) {
1102 (*colors
)[ThemeProperties::COLOR_FRAME_INCOGNITO
] =
1103 HSLShift(frame
, GetTintInternal(
1104 ThemeProperties::TINT_FRAME_INCOGNITO
));
1106 if (!colors
->count(ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
)) {
1107 (*colors
)[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
] =
1108 HSLShift(frame
, GetTintInternal(
1109 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
));
1113 void BrowserThemePack::BuildDisplayPropertiesFromJSON(
1114 const base::DictionaryValue
* display_properties_value
) {
1115 display_properties_
= new DisplayPropertyPair
[kDisplayPropertiesSize
];
1116 for (size_t i
= 0; i
< kDisplayPropertiesSize
; ++i
) {
1117 display_properties_
[i
].id
= -1;
1118 display_properties_
[i
].property
= 0;
1121 if (!display_properties_value
)
1124 std::map
<int, int> temp_properties
;
1125 for (base::DictionaryValue::Iterator
iter(*display_properties_value
);
1126 !iter
.IsAtEnd(); iter
.Advance()) {
1127 int property_id
= GetIntForString(iter
.key(), kDisplayProperties
,
1128 kDisplayPropertiesSize
);
1129 switch (property_id
) {
1130 case ThemeProperties::NTP_BACKGROUND_ALIGNMENT
: {
1132 if (iter
.value().GetAsString(&val
)) {
1133 temp_properties
[ThemeProperties::NTP_BACKGROUND_ALIGNMENT
] =
1134 ThemeProperties::StringToAlignment(val
);
1138 case ThemeProperties::NTP_BACKGROUND_TILING
: {
1140 if (iter
.value().GetAsString(&val
)) {
1141 temp_properties
[ThemeProperties::NTP_BACKGROUND_TILING
] =
1142 ThemeProperties::StringToTiling(val
);
1146 case ThemeProperties::NTP_LOGO_ALTERNATE
: {
1148 if (iter
.value().GetAsInteger(&val
))
1149 temp_properties
[ThemeProperties::NTP_LOGO_ALTERNATE
] = val
;
1155 // Copy data from the intermediary data structure to the array.
1157 for (std::map
<int, int>::const_iterator it
= temp_properties
.begin();
1158 it
!= temp_properties
.end() && count
< kDisplayPropertiesSize
;
1160 display_properties_
[count
].id
= it
->first
;
1161 display_properties_
[count
].property
= it
->second
;
1165 void BrowserThemePack::ParseImageNamesFromJSON(
1166 const base::DictionaryValue
* images_value
,
1167 const base::FilePath
& images_path
,
1168 FilePathMap
* file_paths
) const {
1172 for (base::DictionaryValue::Iterator
iter(*images_value
); !iter
.IsAtEnd();
1174 if (iter
.value().IsType(base::Value::TYPE_DICTIONARY
)) {
1175 const base::DictionaryValue
* inner_value
= NULL
;
1176 if (iter
.value().GetAsDictionary(&inner_value
)) {
1177 for (base::DictionaryValue::Iterator
inner_iter(*inner_value
);
1178 !inner_iter
.IsAtEnd();
1179 inner_iter
.Advance()) {
1181 ui::ScaleFactor scale_factor
= ui::SCALE_FACTOR_NONE
;
1182 if (GetScaleFactorFromManifestKey(inner_iter
.key(), &scale_factor
) &&
1183 inner_iter
.value().IsType(base::Value::TYPE_STRING
) &&
1184 inner_iter
.value().GetAsString(&name
)) {
1185 AddFileAtScaleToMap(iter
.key(),
1187 images_path
.AppendASCII(name
),
1192 } else if (iter
.value().IsType(base::Value::TYPE_STRING
)) {
1194 if (iter
.value().GetAsString(&name
)) {
1195 AddFileAtScaleToMap(iter
.key(),
1196 ui::SCALE_FACTOR_100P
,
1197 images_path
.AppendASCII(name
),
1204 void BrowserThemePack::AddFileAtScaleToMap(const std::string
& image_name
,
1205 ui::ScaleFactor scale_factor
,
1206 const base::FilePath
& image_path
,
1207 FilePathMap
* file_paths
) const {
1208 int id
= GetPersistentIDByName(image_name
);
1210 (*file_paths
)[id
][scale_factor
] = image_path
;
1212 id
= GetPersistentIDByNameHelper(image_name
,
1213 kPersistingImagesWinDesktopAura
,
1214 kPersistingImagesWinDesktopAuraLength
);
1216 (*file_paths
)[id
][scale_factor
] = image_path
;
1220 void BrowserThemePack::BuildSourceImagesArray(const FilePathMap
& file_paths
) {
1221 std::vector
<int> ids
;
1222 for (FilePathMap::const_iterator it
= file_paths
.begin();
1223 it
!= file_paths
.end(); ++it
) {
1224 ids
.push_back(it
->first
);
1227 source_images_
= new int[ids
.size() + 1];
1228 std::copy(ids
.begin(), ids
.end(), source_images_
);
1229 source_images_
[ids
.size()] = -1;
1232 bool BrowserThemePack::LoadRawBitmapsTo(
1233 const FilePathMap
& file_paths
,
1234 ImageCache
* image_cache
) {
1235 // Themes should be loaded on the file thread, not the UI thread.
1236 // http://crbug.com/61838
1237 base::ThreadRestrictions::ScopedAllowIO allow_io
;
1239 for (FilePathMap::const_iterator it
= file_paths
.begin();
1240 it
!= file_paths
.end(); ++it
) {
1241 int prs_id
= it
->first
;
1242 // Some images need to go directly into |image_memory_|. No modification is
1243 // necessary or desirable.
1244 bool is_copyable
= false;
1245 for (size_t i
= 0; i
< arraysize(kPreloadIDs
); ++i
) {
1246 if (kPreloadIDs
[i
] == prs_id
) {
1251 gfx::ImageSkia image_skia
;
1252 for (int pass
= 0; pass
< 2; ++pass
) {
1253 // Two passes: In the first pass, we process only scale factor
1254 // 100% and in the second pass all other scale factors. We
1255 // process scale factor 100% first because the first image added
1256 // in image_skia.AddRepresentation() determines the DIP size for
1257 // all representations.
1258 for (ScaleFactorToFileMap::const_iterator s2f
= it
->second
.begin();
1259 s2f
!= it
->second
.end(); ++s2f
) {
1260 ui::ScaleFactor scale_factor
= s2f
->first
;
1261 if ((pass
== 0 && scale_factor
!= ui::SCALE_FACTOR_100P
) ||
1262 (pass
== 1 && scale_factor
== ui::SCALE_FACTOR_100P
)) {
1265 scoped_refptr
<base::RefCountedMemory
> raw_data(
1266 ReadFileData(s2f
->second
));
1267 if (!raw_data
.get() || !raw_data
->size()) {
1268 LOG(ERROR
) << "Could not load theme image"
1269 << " prs_id=" << prs_id
1270 << " scale_factor_enum=" << scale_factor
1271 << " file=" << s2f
->second
.value()
1272 << (raw_data
.get() ? " (zero size)" : " (read error)");
1276 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factor
);
1277 image_memory_
[raw_id
] = raw_data
;
1280 if (gfx::PNGCodec::Decode(raw_data
->front(), raw_data
->size(),
1282 image_skia
.AddRepresentation(
1283 gfx::ImageSkiaRep(bitmap
,
1284 ui::GetImageScale(scale_factor
)));
1286 NOTREACHED() << "Unable to decode theme image resource "
1292 if (!is_copyable
&& !image_skia
.isNull())
1293 (*image_cache
)[prs_id
] = gfx::Image(image_skia
);
1299 void BrowserThemePack::CreateImages(ImageCache
* images
) const {
1301 CreateFrameImages(images
);
1302 CreateTintedButtons(GetTintInternal(ThemeProperties::TINT_BUTTONS
), images
);
1303 CreateTabBackgroundImages(images
);
1306 void BrowserThemePack::CropImages(ImageCache
* images
) const {
1307 bool has_frame_border
= HasFrameBorder();
1308 for (size_t i
= 0; i
< arraysize(kImagesToCrop
); ++i
) {
1309 if (has_frame_border
&& kImagesToCrop
[i
].skip_if_frame_border
)
1312 int prs_id
= kImagesToCrop
[i
].prs_id
;
1313 ImageCache::iterator it
= images
->find(prs_id
);
1314 if (it
== images
->end())
1317 int crop_height
= kImagesToCrop
[i
].max_height
;
1318 gfx::ImageSkia image_skia
= it
->second
.AsImageSkia();
1319 (*images
)[prs_id
] = gfx::Image(gfx::ImageSkiaOperations::ExtractSubset(
1320 image_skia
, gfx::Rect(0, 0, image_skia
.width(), crop_height
)));
1324 void BrowserThemePack::CreateFrameImages(ImageCache
* images
) const {
1325 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
1327 // Create all the output images in a separate cache and move them back into
1328 // the input images because there can be name collisions.
1329 ImageCache temp_output
;
1331 for (size_t i
= 0; i
< arraysize(kFrameTintMap
); ++i
) {
1332 int prs_id
= kFrameTintMap
[i
].key
;
1334 // If there's no frame image provided for the specified id, then load
1335 // the default provided frame. If that's not provided, skip this whole
1336 // thing and just use the default images.
1337 int prs_base_id
= 0;
1340 if (prs_id
== PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN
) {
1341 prs_base_id
= images
->count(PRS_THEME_FRAME_INCOGNITO_WIN
) ?
1342 PRS_THEME_FRAME_INCOGNITO_WIN
: PRS_THEME_FRAME_WIN
;
1343 } else if (prs_id
== PRS_THEME_FRAME_INACTIVE_WIN
) {
1344 prs_base_id
= PRS_THEME_FRAME_WIN
;
1345 } else if (prs_id
== PRS_THEME_FRAME_INCOGNITO_WIN
&&
1346 !images
->count(PRS_THEME_FRAME_INCOGNITO_WIN
)) {
1347 prs_base_id
= PRS_THEME_FRAME_WIN
;
1351 if (prs_id
== PRS_THEME_FRAME_INCOGNITO_INACTIVE
) {
1352 prs_base_id
= images
->count(PRS_THEME_FRAME_INCOGNITO
) ?
1353 PRS_THEME_FRAME_INCOGNITO
: PRS_THEME_FRAME
;
1354 } else if (prs_id
== PRS_THEME_FRAME_OVERLAY_INACTIVE
) {
1355 prs_base_id
= PRS_THEME_FRAME_OVERLAY
;
1356 } else if (prs_id
== PRS_THEME_FRAME_INACTIVE
) {
1357 prs_base_id
= PRS_THEME_FRAME
;
1358 } else if (prs_id
== PRS_THEME_FRAME_INCOGNITO
&&
1359 !images
->count(PRS_THEME_FRAME_INCOGNITO
)) {
1360 prs_base_id
= PRS_THEME_FRAME
;
1362 prs_base_id
= prs_id
;
1365 if (images
->count(prs_id
)) {
1366 frame
= (*images
)[prs_id
];
1367 } else if (prs_base_id
!= prs_id
&& images
->count(prs_base_id
)) {
1368 frame
= (*images
)[prs_base_id
];
1369 } else if (prs_base_id
== PRS_THEME_FRAME_OVERLAY
) {
1371 if (images
->count(PRS_THEME_FRAME_WIN
)) {
1373 if (images
->count(PRS_THEME_FRAME
)) {
1375 // If there is no theme overlay, don't tint the default frame,
1376 // because it will overwrite the custom frame image when we cache and
1377 // reload from disk.
1378 frame
= gfx::Image();
1381 // If the theme doesn't specify an image, then apply the tint to
1382 // the default frame.
1383 frame
= rb
.GetImageNamed(IDR_THEME_FRAME
);
1384 #if defined(OS_WIN) && defined(USE_AURA)
1385 if (prs_id
>= PRS_THEME_FRAME_WIN
&&
1386 prs_id
<= PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN
) {
1387 frame
= rb
.GetImageNamed(IDR_THEME_FRAME_WIN
);
1391 if (!frame
.IsEmpty()) {
1392 temp_output
[prs_id
] = CreateHSLShiftedImage(
1393 frame
, GetTintInternal(kFrameTintMap
[i
].value
));
1396 MergeImageCaches(temp_output
, images
);
1399 void BrowserThemePack::CreateTintedButtons(
1400 const color_utils::HSL
& button_tint
,
1401 ImageCache
* processed_images
) const {
1402 if (button_tint
.h
!= -1 || button_tint
.s
!= -1 || button_tint
.l
!= -1) {
1403 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
1404 const std::set
<int>& idr_ids
=
1405 ThemeProperties::GetTintableToolbarButtons();
1406 for (std::set
<int>::const_iterator it
= idr_ids
.begin();
1407 it
!= idr_ids
.end(); ++it
) {
1408 int prs_id
= GetPersistentIDByIDR(*it
);
1411 // Fetch the image by IDR...
1412 gfx::Image
& button
= rb
.GetImageNamed(*it
);
1414 // but save a version with the persistent ID.
1415 (*processed_images
)[prs_id
] =
1416 CreateHSLShiftedImage(button
, button_tint
);
1421 void BrowserThemePack::CreateTabBackgroundImages(ImageCache
* images
) const {
1422 ImageCache temp_output
;
1423 for (size_t i
= 0; i
< arraysize(kTabBackgroundMap
); ++i
) {
1424 int prs_id
= kTabBackgroundMap
[i
].key
;
1425 int prs_base_id
= kTabBackgroundMap
[i
].value
;
1427 // We only need to generate the background tab images if we were provided
1428 // with a PRS_THEME_FRAME.
1429 ImageCache::const_iterator it
= images
->find(prs_base_id
);
1430 if (it
!= images
->end()) {
1431 gfx::ImageSkia image_to_tint
= (it
->second
).AsImageSkia();
1432 color_utils::HSL hsl_shift
= GetTintInternal(
1433 ThemeProperties::TINT_BACKGROUND_TAB
);
1434 int vertical_offset
= images
->count(prs_id
)
1435 ? kRestoredTabVerticalOffset
: 0;
1437 gfx::ImageSkia overlay
;
1438 ImageCache::const_iterator overlay_it
= images
->find(prs_id
);
1439 if (overlay_it
!= images
->end())
1440 overlay
= overlay_it
->second
.AsImageSkia();
1442 gfx::ImageSkiaSource
* source
= new TabBackgroundImageSource(
1443 image_to_tint
, overlay
, hsl_shift
, vertical_offset
);
1444 // ImageSkia takes ownership of |source|.
1445 temp_output
[prs_id
] = gfx::Image(gfx::ImageSkia(source
,
1446 image_to_tint
.size()));
1449 MergeImageCaches(temp_output
, images
);
1452 void BrowserThemePack::RepackImages(const ImageCache
& images
,
1453 RawImages
* reencoded_images
) const {
1454 typedef std::vector
<ui::ScaleFactor
> ScaleFactors
;
1455 for (ImageCache::const_iterator it
= images
.begin();
1456 it
!= images
.end(); ++it
) {
1457 gfx::ImageSkia image_skia
= *it
->second
.ToImageSkia();
1459 typedef std::vector
<gfx::ImageSkiaRep
> ImageSkiaReps
;
1460 ImageSkiaReps image_reps
= image_skia
.image_reps();
1461 if (image_reps
.empty()) {
1462 NOTREACHED() << "No image reps for resource " << it
->first
<< ".";
1464 for (ImageSkiaReps::iterator rep_it
= image_reps
.begin();
1465 rep_it
!= image_reps
.end(); ++rep_it
) {
1466 std::vector
<unsigned char> bitmap_data
;
1467 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it
->sk_bitmap(), false,
1469 NOTREACHED() << "Image file for resource " << it
->first
1470 << " could not be encoded.";
1472 int raw_id
= GetRawIDByPersistentID(
1474 ui::GetSupportedScaleFactor(rep_it
->scale()));
1475 (*reencoded_images
)[raw_id
] =
1476 base::RefCountedBytes::TakeVector(&bitmap_data
);
1481 void BrowserThemePack::MergeImageCaches(
1482 const ImageCache
& source
, ImageCache
* destination
) const {
1483 for (ImageCache::const_iterator it
= source
.begin(); it
!= source
.end();
1485 (*destination
)[it
->first
] = it
->second
;
1489 void BrowserThemePack::AddRawImagesTo(const RawImages
& images
,
1490 RawDataForWriting
* out
) const {
1491 for (RawImages::const_iterator it
= images
.begin(); it
!= images
.end();
1493 (*out
)[it
->first
] = base::StringPiece(
1494 it
->second
->front_as
<char>(), it
->second
->size());
1498 color_utils::HSL
BrowserThemePack::GetTintInternal(int id
) const {
1500 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
1501 if (tints_
[i
].id
== id
) {
1502 color_utils::HSL hsl
;
1503 hsl
.h
= tints_
[i
].h
;
1504 hsl
.s
= tints_
[i
].s
;
1505 hsl
.l
= tints_
[i
].l
;
1511 return ThemeProperties::GetDefaultTint(id
);
1514 int BrowserThemePack::GetRawIDByPersistentID(
1516 ui::ScaleFactor scale_factor
) const {
1520 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1521 if (scale_factors_
[i
] == scale_factor
)
1522 return static_cast<int>(kPersistingImagesLength
* i
) + prs_id
;
1527 bool BrowserThemePack::GetScaleFactorFromManifestKey(
1528 const std::string
& key
,
1529 ui::ScaleFactor
* scale_factor
) const {
1531 if (base::StringToInt(key
, &percent
)) {
1532 float scale
= static_cast<float>(percent
) / 100.0f
;
1533 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1534 if (fabs(ui::GetImageScale(scale_factors_
[i
]) - scale
) < 0.001) {
1535 *scale_factor
= scale_factors_
[i
];
1543 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id
) {
1544 // Compute (by scaling) bitmaps for |prs_id| for any scale factors
1545 // for which the theme author did not provide a bitmap. We compute
1546 // the bitmaps using the highest scale factor that theme author
1548 // Note: We use only supported scale factors. For example, if scale
1549 // factor 2x is supported by the current system, but 1.8x is not and
1550 // if the theme author did not provide an image for 2x but one for
1551 // 1.8x, we will not use the 1.8x image here. Here we will only use
1552 // images provided for scale factors supported by the current system.
1554 // See if any image is missing. If not, we're done.
1555 bool image_missing
= false;
1556 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1557 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1558 if (image_memory_
.find(raw_id
) == image_memory_
.end()) {
1559 image_missing
= true;
1566 // Find available scale factor with highest scale.
1567 ui::ScaleFactor available_scale_factor
= ui::SCALE_FACTOR_NONE
;
1568 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1569 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1570 if ((available_scale_factor
== ui::SCALE_FACTOR_NONE
||
1571 (ui::GetImageScale(scale_factors_
[i
]) >
1572 ui::GetImageScale(available_scale_factor
))) &&
1573 image_memory_
.find(raw_id
) != image_memory_
.end()) {
1574 available_scale_factor
= scale_factors_
[i
];
1577 // If no scale factor is available, we're done.
1578 if (available_scale_factor
== ui::SCALE_FACTOR_NONE
)
1581 // Get bitmap for the available scale factor.
1582 int available_raw_id
= GetRawIDByPersistentID(prs_id
, available_scale_factor
);
1583 RawImages::const_iterator it
= image_memory_
.find(available_raw_id
);
1584 SkBitmap available_bitmap
;
1585 if (!gfx::PNGCodec::Decode(it
->second
->front(),
1587 &available_bitmap
)) {
1588 NOTREACHED() << "Unable to decode theme image for prs_id="
1589 << prs_id
<< " for scale_factor=" << available_scale_factor
;
1593 // Fill in all missing scale factors by scaling the available bitmap.
1594 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1595 int scaled_raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1596 if (image_memory_
.find(scaled_raw_id
) != image_memory_
.end())
1598 SkBitmap scaled_bitmap
=
1599 CreateLowQualityResizedBitmap(available_bitmap
,
1600 available_scale_factor
,
1602 std::vector
<unsigned char> bitmap_data
;
1603 if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap
,
1606 NOTREACHED() << "Unable to encode theme image for prs_id="
1607 << prs_id
<< " for scale_factor=" << scale_factors_
[i
];
1610 image_memory_
[scaled_raw_id
] =
1611 base::RefCountedBytes::TakeVector(&bitmap_data
);