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 "components/crx_file/id_util.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "grit/theme_resources.h"
24 #include "third_party/skia/include/core/SkCanvas.h"
25 #include "ui/base/resource/data_pack.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/codec/png_codec.h"
29 #include "ui/gfx/geometry/size_conversions.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/skia_util.h"
36 #include "ui/resources/grit/ui_resources.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
= 35;
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.
94 const char* const key
;
97 // IDR_* resource names change whenever new resources are added; use persistent
98 // IDs when storing to a cached pack.
100 // TODO(erg): The cocoa port is the last user of the IDR_*_[HP] variants. These
101 // should be removed once the cocoa port no longer uses them.
102 PersistingImagesTable kPersistingImages
[] = {
103 { PRS_THEME_FRAME
, IDR_THEME_FRAME
,
105 { PRS_THEME_FRAME_INACTIVE
, IDR_THEME_FRAME_INACTIVE
,
106 "theme_frame_inactive" },
107 { PRS_THEME_FRAME_INCOGNITO
, IDR_THEME_FRAME_INCOGNITO
,
108 "theme_frame_incognito" },
109 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
, IDR_THEME_FRAME_INCOGNITO_INACTIVE
,
110 "theme_frame_incognito_inactive" },
111 { PRS_THEME_TOOLBAR
, IDR_THEME_TOOLBAR
,
113 { PRS_THEME_TAB_BACKGROUND
, IDR_THEME_TAB_BACKGROUND
,
114 "theme_tab_background" },
115 #if !defined(OS_MACOSX)
116 { PRS_THEME_TAB_BACKGROUND_INCOGNITO
, IDR_THEME_TAB_BACKGROUND_INCOGNITO
,
117 "theme_tab_background_incognito" },
119 { PRS_THEME_TAB_BACKGROUND_V
, IDR_THEME_TAB_BACKGROUND_V
,
120 "theme_tab_background_v"},
121 { PRS_THEME_NTP_BACKGROUND
, IDR_THEME_NTP_BACKGROUND
,
122 "theme_ntp_background" },
123 { PRS_THEME_FRAME_OVERLAY
, IDR_THEME_FRAME_OVERLAY
,
124 "theme_frame_overlay" },
125 { PRS_THEME_FRAME_OVERLAY_INACTIVE
, IDR_THEME_FRAME_OVERLAY_INACTIVE
,
126 "theme_frame_overlay_inactive" },
127 { PRS_THEME_BUTTON_BACKGROUND
, IDR_THEME_BUTTON_BACKGROUND
,
128 "theme_button_background" },
129 { PRS_THEME_NTP_ATTRIBUTION
, IDR_THEME_NTP_ATTRIBUTION
,
130 "theme_ntp_attribution" },
131 { PRS_THEME_WINDOW_CONTROL_BACKGROUND
, IDR_THEME_WINDOW_CONTROL_BACKGROUND
,
132 "theme_window_control_background"},
134 // The rest of these entries have no key because they can't be overridden
135 // from the json manifest.
136 { 15, IDR_BACK
, NULL
},
137 { 16, IDR_BACK_D
, NULL
},
138 { 17, IDR_BACK_H
, NULL
},
139 { 18, IDR_BACK_P
, NULL
},
140 { 19, IDR_FORWARD
, NULL
},
141 { 20, IDR_FORWARD_D
, NULL
},
142 { 21, IDR_FORWARD_H
, NULL
},
143 { 22, IDR_FORWARD_P
, NULL
},
144 { 23, IDR_HOME
, NULL
},
145 { 24, IDR_HOME_H
, NULL
},
146 { 25, IDR_HOME_P
, NULL
},
147 { 26, IDR_RELOAD
, NULL
},
148 { 27, IDR_RELOAD_H
, NULL
},
149 { 28, IDR_RELOAD_P
, NULL
},
150 { 29, IDR_STOP
, NULL
},
151 { 30, IDR_STOP_D
, NULL
},
152 { 31, IDR_STOP_H
, NULL
},
153 { 32, IDR_STOP_P
, NULL
},
154 { 33, IDR_BROWSER_ACTIONS_OVERFLOW
, NULL
},
155 { 34, IDR_BROWSER_ACTIONS_OVERFLOW_H
, NULL
},
156 { 35, IDR_BROWSER_ACTIONS_OVERFLOW_P
, NULL
},
157 { 36, IDR_TOOLS
, NULL
},
158 { 37, IDR_TOOLS_H
, NULL
},
159 { 38, IDR_TOOLS_P
, NULL
},
160 { 39, IDR_MENU_DROPARROW
, NULL
},
161 { 40, IDR_THROBBER
, NULL
},
162 { 41, IDR_THROBBER_WAITING
, NULL
},
163 { 42, IDR_THROBBER_LIGHT
, NULL
},
164 { 43, IDR_TOOLBAR_BEZEL_HOVER
, NULL
},
165 { 44, IDR_TOOLBAR_BEZEL_PRESSED
, NULL
},
166 { 45, IDR_TOOLS_BAR
, NULL
},
168 const size_t kPersistingImagesLength
= arraysize(kPersistingImages
);
170 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
171 // Persistent theme ids for Windows.
172 const int PRS_THEME_FRAME_DESKTOP
= 100;
173 const int PRS_THEME_FRAME_INACTIVE_DESKTOP
= 101;
174 const int PRS_THEME_FRAME_INCOGNITO_DESKTOP
= 102;
175 const int PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
= 103;
176 const int PRS_THEME_TOOLBAR_DESKTOP
= 104;
177 const int PRS_THEME_TAB_BACKGROUND_DESKTOP
= 105;
178 const int PRS_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
= 106;
180 // Persistent theme to resource id mapping for Windows AURA.
181 PersistingImagesTable kPersistingImagesDesktopAura
[] = {
182 { PRS_THEME_FRAME_DESKTOP
, IDR_THEME_FRAME_DESKTOP
,
184 { PRS_THEME_FRAME_INACTIVE_DESKTOP
, IDR_THEME_FRAME_INACTIVE_DESKTOP
,
185 "theme_frame_inactive" },
186 { PRS_THEME_FRAME_INCOGNITO_DESKTOP
, IDR_THEME_FRAME_INCOGNITO_DESKTOP
,
187 "theme_frame_incognito" },
188 { PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
,
189 IDR_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
,
190 "theme_frame_incognito_inactive" },
191 { PRS_THEME_TOOLBAR_DESKTOP
, IDR_THEME_TOOLBAR_DESKTOP
,
193 { PRS_THEME_TAB_BACKGROUND_DESKTOP
, IDR_THEME_TAB_BACKGROUND_DESKTOP
,
194 "theme_tab_background" },
195 { PRS_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
,
196 IDR_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
,
197 "theme_tab_background_incognito" },
199 const size_t kPersistingImagesDesktopAuraLength
=
200 arraysize(kPersistingImagesDesktopAura
);
203 int GetPersistentIDByNameHelper(const std::string
& key
,
204 const PersistingImagesTable
* image_table
,
205 size_t image_table_size
) {
206 for (size_t i
= 0; i
< image_table_size
; ++i
) {
207 if (image_table
[i
].key
&& LowerCaseEqualsASCII(key
, image_table
[i
].key
)) {
208 return image_table
[i
].persistent_id
;
214 int GetPersistentIDByName(const std::string
& key
) {
215 return GetPersistentIDByNameHelper(key
,
217 kPersistingImagesLength
);
220 int GetPersistentIDByIDR(int idr
) {
221 static std::map
<int,int>* lookup_table
= new std::map
<int,int>();
222 if (lookup_table
->empty()) {
223 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
) {
224 int idr
= kPersistingImages
[i
].idr_id
;
225 int prs_id
= kPersistingImages
[i
].persistent_id
;
226 (*lookup_table
)[idr
] = prs_id
;
228 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
229 for (size_t i
= 0; i
< kPersistingImagesDesktopAuraLength
; ++i
) {
230 int idr
= kPersistingImagesDesktopAura
[i
].idr_id
;
231 int prs_id
= kPersistingImagesDesktopAura
[i
].persistent_id
;
232 (*lookup_table
)[idr
] = prs_id
;
236 std::map
<int,int>::iterator it
= lookup_table
->find(idr
);
237 return (it
== lookup_table
->end()) ? -1 : it
->second
;
240 // Returns the maximum persistent id.
241 int GetMaxPersistentId() {
242 static int max_prs_id
= -1;
243 if (max_prs_id
== -1) {
244 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
) {
245 if (kPersistingImages
[i
].persistent_id
> max_prs_id
)
246 max_prs_id
= kPersistingImages
[i
].persistent_id
;
248 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
249 for (size_t i
= 0; i
< kPersistingImagesDesktopAuraLength
; ++i
) {
250 if (kPersistingImagesDesktopAura
[i
].persistent_id
> max_prs_id
)
251 max_prs_id
= kPersistingImagesDesktopAura
[i
].persistent_id
;
258 // Returns true if the scales in |input| match those in |expected|.
259 // The order must match as the index is used in determining the raw id.
260 bool InputScalesValid(const base::StringPiece
& input
,
261 const std::vector
<ui::ScaleFactor
>& expected
) {
262 size_t scales_size
= static_cast<size_t>(input
.size() / sizeof(float));
263 if (scales_size
!= expected
.size())
265 scoped_ptr
<float[]> scales(new float[scales_size
]);
266 // Do a memcpy to avoid misaligned memory access.
267 memcpy(scales
.get(), input
.data(), input
.size());
268 for (size_t index
= 0; index
< scales_size
; ++index
) {
269 if (scales
[index
] != ui::GetScaleForScaleFactor(expected
[index
]))
275 // Returns |scale_factors| as a string to be written to disk.
276 std::string
GetScaleFactorsAsString(
277 const std::vector
<ui::ScaleFactor
>& scale_factors
) {
278 scoped_ptr
<float[]> scales(new float[scale_factors
.size()]);
279 for (size_t i
= 0; i
< scale_factors
.size(); ++i
)
280 scales
[i
] = ui::GetScaleForScaleFactor(scale_factors
[i
]);
281 std::string out_string
= std::string(
282 reinterpret_cast<const char*>(scales
.get()),
283 scale_factors
.size() * sizeof(float));
287 struct StringToIntTable
{
288 const char* const key
;
289 ThemeProperties::OverwritableByUserThemeProperty id
;
292 // Strings used by themes to identify tints in the JSON.
293 StringToIntTable kTintTable
[] = {
294 { "buttons", ThemeProperties::TINT_BUTTONS
},
295 { "frame", ThemeProperties::TINT_FRAME
},
296 { "frame_inactive", ThemeProperties::TINT_FRAME_INACTIVE
},
297 { "frame_incognito", ThemeProperties::TINT_FRAME_INCOGNITO
},
298 { "frame_incognito_inactive",
299 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
300 { "background_tab", ThemeProperties::TINT_BACKGROUND_TAB
},
302 const size_t kTintTableLength
= arraysize(kTintTable
);
304 // Strings used by themes to identify colors in the JSON.
305 StringToIntTable kColorTable
[] = {
306 { "frame", ThemeProperties::COLOR_FRAME
},
307 { "frame_inactive", ThemeProperties::COLOR_FRAME_INACTIVE
},
308 { "frame_incognito", ThemeProperties::COLOR_FRAME_INCOGNITO
},
309 { "frame_incognito_inactive",
310 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
},
311 { "toolbar", ThemeProperties::COLOR_TOOLBAR
},
312 { "tab_text", ThemeProperties::COLOR_TAB_TEXT
},
313 { "tab_background_text", ThemeProperties::COLOR_BACKGROUND_TAB_TEXT
},
314 { "bookmark_text", ThemeProperties::COLOR_BOOKMARK_TEXT
},
315 { "ntp_background", ThemeProperties::COLOR_NTP_BACKGROUND
},
316 { "ntp_text", ThemeProperties::COLOR_NTP_TEXT
},
317 { "ntp_link", ThemeProperties::COLOR_NTP_LINK
},
318 { "ntp_link_underline", ThemeProperties::COLOR_NTP_LINK_UNDERLINE
},
319 { "ntp_header", ThemeProperties::COLOR_NTP_HEADER
},
320 { "ntp_section", ThemeProperties::COLOR_NTP_SECTION
},
321 { "ntp_section_text", ThemeProperties::COLOR_NTP_SECTION_TEXT
},
322 { "ntp_section_link", ThemeProperties::COLOR_NTP_SECTION_LINK
},
323 { "ntp_section_link_underline",
324 ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
},
325 { "button_background", ThemeProperties::COLOR_BUTTON_BACKGROUND
},
327 const size_t kColorTableLength
= arraysize(kColorTable
);
329 // Strings used by themes to identify display properties keys in JSON.
330 StringToIntTable kDisplayProperties
[] = {
331 { "ntp_background_alignment",
332 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
},
333 { "ntp_background_repeat", ThemeProperties::NTP_BACKGROUND_TILING
},
334 { "ntp_logo_alternate", ThemeProperties::NTP_LOGO_ALTERNATE
},
336 const size_t kDisplayPropertiesSize
= arraysize(kDisplayProperties
);
338 int GetIntForString(const std::string
& key
,
339 StringToIntTable
* table
,
340 size_t table_length
) {
341 for (size_t i
= 0; i
< table_length
; ++i
) {
342 if (LowerCaseEqualsASCII(key
, table
[i
].key
)) {
350 struct IntToIntTable
{
355 // Mapping used in CreateFrameImages() to associate frame images with the
356 // tint ID that should maybe be applied to it.
357 IntToIntTable kFrameTintMap
[] = {
358 { PRS_THEME_FRAME
, ThemeProperties::TINT_FRAME
},
359 { PRS_THEME_FRAME_INACTIVE
, ThemeProperties::TINT_FRAME_INACTIVE
},
360 { PRS_THEME_FRAME_OVERLAY
, ThemeProperties::TINT_FRAME
},
361 { PRS_THEME_FRAME_OVERLAY_INACTIVE
,
362 ThemeProperties::TINT_FRAME_INACTIVE
},
363 { PRS_THEME_FRAME_INCOGNITO
, ThemeProperties::TINT_FRAME_INCOGNITO
},
364 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
,
365 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
366 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
367 { PRS_THEME_FRAME_DESKTOP
, ThemeProperties::TINT_FRAME
},
368 { PRS_THEME_FRAME_INACTIVE_DESKTOP
, ThemeProperties::TINT_FRAME_INACTIVE
},
369 { PRS_THEME_FRAME_INCOGNITO_DESKTOP
, ThemeProperties::TINT_FRAME_INCOGNITO
},
370 { PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
,
371 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
375 // Mapping used in GenerateTabBackgroundImages() to associate what frame image
376 // goes with which tab background.
377 IntToIntTable kTabBackgroundMap
[] = {
378 { PRS_THEME_TAB_BACKGROUND
, PRS_THEME_FRAME
},
379 { PRS_THEME_TAB_BACKGROUND_INCOGNITO
, PRS_THEME_FRAME_INCOGNITO
},
380 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
381 { PRS_THEME_TAB_BACKGROUND_DESKTOP
, PRS_THEME_FRAME_DESKTOP
},
382 { PRS_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
,
383 PRS_THEME_FRAME_INCOGNITO_DESKTOP
},
390 // The maximum useful height of the image at |prs_id|.
393 // Whether cropping the image at |prs_id| should be skipped on OSes which
394 // have a frame border to the left and right of the web contents.
395 // This should be true for images which can be used to decorate the border to
396 // the left and the right of the web contents.
397 bool skip_if_frame_border
;
400 // The images which should be cropped before being saved to the data pack. The
401 // maximum heights are meant to be conservative as to give room for the UI to
402 // change without the maximum heights having to be modified.
403 // |kThemePackVersion| must be incremented if any of the maximum heights below
405 struct CropEntry kImagesToCrop
[] = {
406 { PRS_THEME_FRAME
, 120, true },
407 { PRS_THEME_FRAME_INACTIVE
, 120, true },
408 { PRS_THEME_FRAME_INCOGNITO
, 120, true },
409 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
, 120, true },
410 { PRS_THEME_FRAME_OVERLAY
, 120, true },
411 { PRS_THEME_FRAME_OVERLAY_INACTIVE
, 120, true },
412 { PRS_THEME_TOOLBAR
, 200, false },
413 { PRS_THEME_BUTTON_BACKGROUND
, 60, false },
414 { PRS_THEME_WINDOW_CONTROL_BACKGROUND
, 50, false },
415 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
416 { PRS_THEME_TOOLBAR_DESKTOP
, 200, false }
421 // A list of images that don't need tinting or any other modification and can
422 // be byte-copied directly into the finished DataPack. This should contain the
423 // persistent IDs for all themeable image IDs that aren't in kFrameTintMap,
424 // kTabBackgroundMap or kImagesToCrop.
425 const int kPreloadIDs
[] = {
426 PRS_THEME_NTP_BACKGROUND
,
427 PRS_THEME_NTP_ATTRIBUTION
,
430 // Returns true if this OS uses a browser frame which has a non zero width to
431 // the left and the right of the web contents.
432 bool HasFrameBorder() {
433 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
440 // Returns a piece of memory with the contents of the file |path|.
441 base::RefCountedMemory
* ReadFileData(const base::FilePath
& path
) {
443 base::File
file(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
444 if (file
.IsValid()) {
445 int64 length
= file
.GetLength();
446 if (length
> 0 && length
< INT_MAX
) {
447 int size
= static_cast<int>(length
);
448 std::vector
<unsigned char> raw_data
;
449 raw_data
.resize(size
);
450 char* data
= reinterpret_cast<char*>(&(raw_data
.front()));
451 if (file
.ReadAtCurrentPos(data
, size
) == length
)
452 return base::RefCountedBytes::TakeVector(&raw_data
);
460 // Shifts an image's HSL values. The caller is responsible for deleting
461 // the returned image.
462 gfx::Image
CreateHSLShiftedImage(const gfx::Image
& image
,
463 const color_utils::HSL
& hsl_shift
) {
464 const gfx::ImageSkia
* src_image
= image
.ToImageSkia();
465 return gfx::Image(gfx::ImageSkiaOperations::CreateHSLShiftedImage(
466 *src_image
, hsl_shift
));
469 // Computes a bitmap at one scale from a bitmap at a different scale.
470 SkBitmap
CreateLowQualityResizedBitmap(const SkBitmap
& source_bitmap
,
471 ui::ScaleFactor source_scale_factor
,
472 ui::ScaleFactor desired_scale_factor
) {
473 gfx::Size scaled_size
= gfx::ToCeiledSize(
474 gfx::ScaleSize(gfx::Size(source_bitmap
.width(),
475 source_bitmap
.height()),
476 ui::GetScaleForScaleFactor(desired_scale_factor
) /
477 ui::GetScaleForScaleFactor(source_scale_factor
)));
478 SkBitmap scaled_bitmap
;
479 scaled_bitmap
.allocN32Pixels(scaled_size
.width(), scaled_size
.height());
480 scaled_bitmap
.eraseARGB(0, 0, 0, 0);
481 SkCanvas
canvas(scaled_bitmap
);
482 SkRect scaled_bounds
= RectToSkRect(gfx::Rect(scaled_size
));
483 // Note(oshima): The following scaling code doesn't work with
485 canvas
.drawBitmapRect(source_bitmap
, NULL
, scaled_bounds
);
486 return scaled_bitmap
;
489 // A ImageSkiaSource that scales 100P image to the target scale factor
490 // if the ImageSkiaRep for the target scale factor isn't available.
491 class ThemeImageSource
: public gfx::ImageSkiaSource
{
493 explicit ThemeImageSource(const gfx::ImageSkia
& source
) : source_(source
) {
495 ~ThemeImageSource() override
{}
497 gfx::ImageSkiaRep
GetImageForScale(float scale
) override
{
498 if (source_
.HasRepresentation(scale
))
499 return source_
.GetRepresentation(scale
);
500 const gfx::ImageSkiaRep
& rep_100p
= source_
.GetRepresentation(1.0f
);
501 SkBitmap scaled_bitmap
= CreateLowQualityResizedBitmap(
502 rep_100p
.sk_bitmap(),
503 ui::SCALE_FACTOR_100P
,
504 ui::GetSupportedScaleFactor(scale
));
505 return gfx::ImageSkiaRep(scaled_bitmap
, scale
);
509 const gfx::ImageSkia source_
;
511 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource
);
514 // An ImageSkiaSource that delays decoding PNG data into bitmaps until
515 // needed. Missing data for a scale factor is computed by scaling data for an
516 // available scale factor. Computed bitmaps are stored for future look up.
517 class ThemeImagePngSource
: public gfx::ImageSkiaSource
{
519 typedef std::map
<ui::ScaleFactor
,
520 scoped_refptr
<base::RefCountedMemory
> > PngMap
;
522 explicit ThemeImagePngSource(const PngMap
& png_map
) : png_map_(png_map
) {}
524 ~ThemeImagePngSource() override
{}
527 gfx::ImageSkiaRep
GetImageForScale(float scale
) override
{
528 ui::ScaleFactor scale_factor
= ui::GetSupportedScaleFactor(scale
);
529 // Look up the bitmap for |scale factor| in the bitmap map. If found
531 BitmapMap::const_iterator exact_bitmap_it
= bitmap_map_
.find(scale_factor
);
532 if (exact_bitmap_it
!= bitmap_map_
.end())
533 return gfx::ImageSkiaRep(exact_bitmap_it
->second
, scale
);
535 // Look up the raw PNG data for |scale_factor| in the png map. If found,
536 // decode it, store the result in the bitmap map and return it.
537 PngMap::const_iterator exact_png_it
= png_map_
.find(scale_factor
);
538 if (exact_png_it
!= png_map_
.end()) {
540 if (!gfx::PNGCodec::Decode(exact_png_it
->second
->front(),
541 exact_png_it
->second
->size(),
544 return gfx::ImageSkiaRep();
546 bitmap_map_
[scale_factor
] = bitmap
;
547 return gfx::ImageSkiaRep(bitmap
, scale
);
550 // Find an available PNG for another scale factor. We want to use the
551 // highest available scale factor.
552 PngMap::const_iterator available_png_it
= png_map_
.end();
553 for (PngMap::const_iterator png_it
= png_map_
.begin();
554 png_it
!= png_map_
.end(); ++png_it
) {
555 if (available_png_it
== png_map_
.end() ||
556 ui::GetScaleForScaleFactor(png_it
->first
) >
557 ui::GetScaleForScaleFactor(available_png_it
->first
)) {
558 available_png_it
= png_it
;
561 if (available_png_it
== png_map_
.end())
562 return gfx::ImageSkiaRep();
563 ui::ScaleFactor available_scale_factor
= available_png_it
->first
;
565 // Look up the bitmap for |available_scale_factor| in the bitmap map.
566 // If not found, decode the corresponging png data, store the result
567 // in the bitmap map.
568 BitmapMap::const_iterator available_bitmap_it
=
569 bitmap_map_
.find(available_scale_factor
);
570 if (available_bitmap_it
== bitmap_map_
.end()) {
571 SkBitmap available_bitmap
;
572 if (!gfx::PNGCodec::Decode(available_png_it
->second
->front(),
573 available_png_it
->second
->size(),
574 &available_bitmap
)) {
576 return gfx::ImageSkiaRep();
578 bitmap_map_
[available_scale_factor
] = available_bitmap
;
579 available_bitmap_it
= bitmap_map_
.find(available_scale_factor
);
582 // Scale the available bitmap to the desired scale factor, store the result
583 // in the bitmap map and return it.
584 SkBitmap scaled_bitmap
= CreateLowQualityResizedBitmap(
585 available_bitmap_it
->second
,
586 available_scale_factor
,
588 bitmap_map_
[scale_factor
] = scaled_bitmap
;
589 return gfx::ImageSkiaRep(scaled_bitmap
, scale
);
594 typedef std::map
<ui::ScaleFactor
, SkBitmap
> BitmapMap
;
595 BitmapMap bitmap_map_
;
597 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource
);
600 class TabBackgroundImageSource
: public gfx::CanvasImageSource
{
602 TabBackgroundImageSource(const gfx::ImageSkia
& image_to_tint
,
603 const gfx::ImageSkia
& overlay
,
604 const color_utils::HSL
& hsl_shift
,
606 : gfx::CanvasImageSource(image_to_tint
.size(), false),
607 image_to_tint_(image_to_tint
),
609 hsl_shift_(hsl_shift
),
610 vertical_offset_(vertical_offset
) {
613 ~TabBackgroundImageSource() override
{}
615 // Overridden from CanvasImageSource:
616 void Draw(gfx::Canvas
* canvas
) override
{
617 gfx::ImageSkia bg_tint
=
618 gfx::ImageSkiaOperations::CreateHSLShiftedImage(image_to_tint_
,
620 canvas
->TileImageInt(bg_tint
, 0, vertical_offset_
, 0, 0,
621 size().width(), size().height());
623 // If they've provided a custom image, overlay it.
624 if (!overlay_
.isNull()) {
625 canvas
->TileImageInt(overlay_
, 0, 0, size().width(),
631 const gfx::ImageSkia image_to_tint_
;
632 const gfx::ImageSkia overlay_
;
633 const color_utils::HSL hsl_shift_
;
634 const int vertical_offset_
;
636 DISALLOW_COPY_AND_ASSIGN(TabBackgroundImageSource
);
641 BrowserThemePack::~BrowserThemePack() {
642 if (!data_pack_
.get()) {
646 delete [] display_properties_
;
647 delete [] source_images_
;
652 scoped_refptr
<BrowserThemePack
> BrowserThemePack::BuildFromExtension(
653 const Extension
* extension
) {
654 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
656 DCHECK(extension
->is_theme());
658 scoped_refptr
<BrowserThemePack
> pack(new BrowserThemePack
);
659 pack
->BuildHeader(extension
);
660 pack
->BuildTintsFromJSON(extensions::ThemeInfo::GetTints(extension
));
661 pack
->BuildColorsFromJSON(extensions::ThemeInfo::GetColors(extension
));
662 pack
->BuildDisplayPropertiesFromJSON(
663 extensions::ThemeInfo::GetDisplayProperties(extension
));
665 // Builds the images. (Image building is dependent on tints).
666 FilePathMap file_paths
;
667 pack
->ParseImageNamesFromJSON(
668 extensions::ThemeInfo::GetImages(extension
),
671 pack
->BuildSourceImagesArray(file_paths
);
673 if (!pack
->LoadRawBitmapsTo(file_paths
, &pack
->images_on_ui_thread_
))
676 pack
->CreateImages(&pack
->images_on_ui_thread_
);
678 // Make sure the |images_on_file_thread_| has bitmaps for supported
679 // scale factors before passing to FILE thread.
680 pack
->images_on_file_thread_
= pack
->images_on_ui_thread_
;
681 for (ImageCache::iterator it
= pack
->images_on_file_thread_
.begin();
682 it
!= pack
->images_on_file_thread_
.end(); ++it
) {
683 gfx::ImageSkia
* image_skia
=
684 const_cast<gfx::ImageSkia
*>(it
->second
.ToImageSkia());
685 image_skia
->MakeThreadSafe();
688 // Set ThemeImageSource on |images_on_ui_thread_| to resample the source
689 // image if a caller of BrowserThemePack::GetImageNamed() requests an
690 // ImageSkiaRep for a scale factor not specified by the theme author.
691 // Callers of BrowserThemePack::GetImageNamed() to be able to retrieve
692 // ImageSkiaReps for all supported scale factors.
693 for (ImageCache::iterator it
= pack
->images_on_ui_thread_
.begin();
694 it
!= pack
->images_on_ui_thread_
.end(); ++it
) {
695 const gfx::ImageSkia source_image_skia
= it
->second
.AsImageSkia();
696 ThemeImageSource
* source
= new ThemeImageSource(source_image_skia
);
697 // image_skia takes ownership of source.
698 gfx::ImageSkia
image_skia(source
, source_image_skia
.size());
699 it
->second
= gfx::Image(image_skia
);
702 // Generate raw images (for new-tab-page attribution and background) for
703 // any missing scale from an available scale image.
704 for (size_t i
= 0; i
< arraysize(kPreloadIDs
); ++i
) {
705 pack
->GenerateRawImageForAllSupportedScales(kPreloadIDs
[i
]);
708 // The BrowserThemePack is now in a consistent state.
713 scoped_refptr
<BrowserThemePack
> BrowserThemePack::BuildFromDataPack(
714 const base::FilePath
& path
, const std::string
& expected_id
) {
715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
716 // Allow IO on UI thread due to deep-seated theme design issues.
717 // (see http://crbug.com/80206)
718 base::ThreadRestrictions::ScopedAllowIO allow_io
;
719 scoped_refptr
<BrowserThemePack
> pack(new BrowserThemePack
);
720 // Scale factor parameter is moot as data pack has image resources for all
721 // supported scale factors.
722 pack
->data_pack_
.reset(
723 new ui::DataPack(ui::SCALE_FACTOR_NONE
));
725 if (!pack
->data_pack_
->LoadFromPath(path
)) {
726 LOG(ERROR
) << "Failed to load theme data pack.";
730 base::StringPiece pointer
;
731 if (!pack
->data_pack_
->GetStringPiece(kHeaderID
, &pointer
))
733 pack
->header_
= reinterpret_cast<BrowserThemePackHeader
*>(const_cast<char*>(
736 if (pack
->header_
->version
!= kThemePackVersion
) {
737 DLOG(ERROR
) << "BuildFromDataPack failure! Version mismatch!";
740 // TODO(erg): Check endianess once DataPack works on the other endian.
741 std::string
theme_id(reinterpret_cast<char*>(pack
->header_
->theme_id
),
742 crx_file::id_util::kIdSize
);
743 std::string truncated_id
= expected_id
.substr(0, crx_file::id_util::kIdSize
);
744 if (theme_id
!= truncated_id
) {
745 DLOG(ERROR
) << "Wrong id: " << theme_id
<< " vs " << expected_id
;
749 if (!pack
->data_pack_
->GetStringPiece(kTintsID
, &pointer
))
751 pack
->tints_
= reinterpret_cast<TintEntry
*>(const_cast<char*>(
754 if (!pack
->data_pack_
->GetStringPiece(kColorsID
, &pointer
))
757 reinterpret_cast<ColorPair
*>(const_cast<char*>(pointer
.data()));
759 if (!pack
->data_pack_
->GetStringPiece(kDisplayPropertiesID
, &pointer
))
761 pack
->display_properties_
= reinterpret_cast<DisplayPropertyPair
*>(
762 const_cast<char*>(pointer
.data()));
764 if (!pack
->data_pack_
->GetStringPiece(kSourceImagesID
, &pointer
))
766 pack
->source_images_
= reinterpret_cast<int*>(
767 const_cast<char*>(pointer
.data()));
769 if (!pack
->data_pack_
->GetStringPiece(kScaleFactorsID
, &pointer
))
772 if (!InputScalesValid(pointer
, pack
->scale_factors_
)) {
773 DLOG(ERROR
) << "BuildFromDataPack failure! The pack scale factors differ "
774 << "from those supported by platform.";
781 bool BrowserThemePack::IsPersistentImageID(int id
) {
782 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
)
783 if (kPersistingImages
[i
].idr_id
== id
)
786 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
787 for (size_t i
= 0; i
< kPersistingImagesDesktopAuraLength
; ++i
)
788 if (kPersistingImagesDesktopAura
[i
].idr_id
== id
)
795 bool BrowserThemePack::WriteToDisk(const base::FilePath
& path
) const {
796 // Add resources for each of the property arrays.
797 RawDataForWriting resources
;
798 resources
[kHeaderID
] = base::StringPiece(
799 reinterpret_cast<const char*>(header_
), sizeof(BrowserThemePackHeader
));
800 resources
[kTintsID
] = base::StringPiece(
801 reinterpret_cast<const char*>(tints_
),
802 sizeof(TintEntry
[kTintTableLength
]));
803 resources
[kColorsID
] = base::StringPiece(
804 reinterpret_cast<const char*>(colors_
),
805 sizeof(ColorPair
[kColorTableLength
]));
806 resources
[kDisplayPropertiesID
] = base::StringPiece(
807 reinterpret_cast<const char*>(display_properties_
),
808 sizeof(DisplayPropertyPair
[kDisplayPropertiesSize
]));
810 int source_count
= 1;
811 int* end
= source_images_
;
812 for (; *end
!= -1 ; end
++)
814 resources
[kSourceImagesID
] = base::StringPiece(
815 reinterpret_cast<const char*>(source_images_
),
816 source_count
* sizeof(*source_images_
));
818 // Store results of GetScaleFactorsAsString() in std::string as
819 // base::StringPiece does not copy data in constructor.
820 std::string scale_factors_string
= GetScaleFactorsAsString(scale_factors_
);
821 resources
[kScaleFactorsID
] = scale_factors_string
;
823 AddRawImagesTo(image_memory_
, &resources
);
825 RawImages reencoded_images
;
826 RepackImages(images_on_file_thread_
, &reencoded_images
);
827 AddRawImagesTo(reencoded_images
, &resources
);
829 return ui::DataPack::WritePack(path
, resources
, ui::DataPack::BINARY
);
832 bool BrowserThemePack::GetTint(int id
, color_utils::HSL
* hsl
) const {
834 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
835 if (tints_
[i
].id
== id
) {
836 hsl
->h
= tints_
[i
].h
;
837 hsl
->s
= tints_
[i
].s
;
838 hsl
->l
= tints_
[i
].l
;
847 bool BrowserThemePack::GetColor(int id
, SkColor
* color
) const {
849 for (size_t i
= 0; i
< kColorTableLength
; ++i
) {
850 if (colors_
[i
].id
== id
) {
851 *color
= colors_
[i
].color
;
860 bool BrowserThemePack::GetDisplayProperty(int id
, int* result
) const {
861 if (display_properties_
) {
862 for (size_t i
= 0; i
< kDisplayPropertiesSize
; ++i
) {
863 if (display_properties_
[i
].id
== id
) {
864 *result
= display_properties_
[i
].property
;
873 gfx::Image
BrowserThemePack::GetImageNamed(int idr_id
) {
874 int prs_id
= GetPersistentIDByIDR(idr_id
);
878 // Check if the image is cached.
879 ImageCache::const_iterator image_iter
= images_on_ui_thread_
.find(prs_id
);
880 if (image_iter
!= images_on_ui_thread_
.end())
881 return image_iter
->second
;
883 ThemeImagePngSource::PngMap png_map
;
884 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
885 scoped_refptr
<base::RefCountedMemory
> memory
=
886 GetRawData(idr_id
, scale_factors_
[i
]);
888 png_map
[scale_factors_
[i
]] = memory
;
890 if (!png_map
.empty()) {
891 gfx::ImageSkia
image_skia(new ThemeImagePngSource(png_map
), 1.0f
);
892 // |image_skia| takes ownership of ThemeImagePngSource.
893 gfx::Image ret
= gfx::Image(image_skia
);
894 images_on_ui_thread_
[prs_id
] = ret
;
901 base::RefCountedMemory
* BrowserThemePack::GetRawData(
903 ui::ScaleFactor scale_factor
) const {
904 base::RefCountedMemory
* memory
= NULL
;
905 int prs_id
= GetPersistentIDByIDR(idr_id
);
906 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factor
);
909 if (data_pack_
.get()) {
910 memory
= data_pack_
->GetStaticMemory(raw_id
);
912 RawImages::const_iterator it
= image_memory_
.find(raw_id
);
913 if (it
!= image_memory_
.end()) {
914 memory
= it
->second
.get();
922 bool BrowserThemePack::HasCustomImage(int idr_id
) const {
923 int prs_id
= GetPersistentIDByIDR(idr_id
);
927 int* img
= source_images_
;
928 for (; *img
!= -1; ++img
) {
938 BrowserThemePack::BrowserThemePack()
939 : CustomThemeSupplier(EXTENSION
),
943 display_properties_(NULL
),
944 source_images_(NULL
) {
945 scale_factors_
= ui::GetSupportedScaleFactors();
946 // On Windows HiDPI SCALE_FACTOR_100P may not be supported by default.
947 if (std::find(scale_factors_
.begin(), scale_factors_
.end(),
948 ui::SCALE_FACTOR_100P
) == scale_factors_
.end()) {
949 scale_factors_
.push_back(ui::SCALE_FACTOR_100P
);
953 void BrowserThemePack::BuildHeader(const Extension
* extension
) {
954 header_
= new BrowserThemePackHeader
;
955 header_
->version
= kThemePackVersion
;
957 // TODO(erg): Need to make this endian safe on other computers. Prerequisite
958 // is that ui::DataPack removes this same check.
959 #if defined(__BYTE_ORDER)
961 static_assert(__BYTE_ORDER
== __LITTLE_ENDIAN
,
962 "datapack assumes little endian");
963 #elif defined(__BIG_ENDIAN__)
965 #error DataPack assumes little endian
967 header_
->little_endian
= 1;
969 const std::string
& id
= extension
->id();
970 memcpy(header_
->theme_id
, id
.c_str(), crx_file::id_util::kIdSize
);
973 void BrowserThemePack::BuildTintsFromJSON(
974 const base::DictionaryValue
* tints_value
) {
975 tints_
= new TintEntry
[kTintTableLength
];
976 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
986 // Parse the incoming data from |tints_value| into an intermediary structure.
987 std::map
<int, color_utils::HSL
> temp_tints
;
988 for (base::DictionaryValue::Iterator
iter(*tints_value
); !iter
.IsAtEnd();
990 const base::ListValue
* tint_list
;
991 if (iter
.value().GetAsList(&tint_list
) &&
992 (tint_list
->GetSize() == 3)) {
993 color_utils::HSL hsl
= { -1, -1, -1 };
995 if (tint_list
->GetDouble(0, &hsl
.h
) &&
996 tint_list
->GetDouble(1, &hsl
.s
) &&
997 tint_list
->GetDouble(2, &hsl
.l
)) {
998 MakeHSLShiftValid(&hsl
);
999 int id
= GetIntForString(iter
.key(), kTintTable
, kTintTableLength
);
1001 temp_tints
[id
] = hsl
;
1007 // Copy data from the intermediary data structure to the array.
1009 for (std::map
<int, color_utils::HSL
>::const_iterator it
=
1011 it
!= temp_tints
.end() && count
< kTintTableLength
;
1013 tints_
[count
].id
= it
->first
;
1014 tints_
[count
].h
= it
->second
.h
;
1015 tints_
[count
].s
= it
->second
.s
;
1016 tints_
[count
].l
= it
->second
.l
;
1020 void BrowserThemePack::BuildColorsFromJSON(
1021 const base::DictionaryValue
* colors_value
) {
1022 colors_
= new ColorPair
[kColorTableLength
];
1023 for (size_t i
= 0; i
< kColorTableLength
; ++i
) {
1025 colors_
[i
].color
= SkColorSetRGB(0, 0, 0);
1028 std::map
<int, SkColor
> temp_colors
;
1030 ReadColorsFromJSON(colors_value
, &temp_colors
);
1031 GenerateMissingColors(&temp_colors
);
1033 // Copy data from the intermediary data structure to the array.
1035 for (std::map
<int, SkColor
>::const_iterator it
= temp_colors
.begin();
1036 it
!= temp_colors
.end() && count
< kColorTableLength
; ++it
, ++count
) {
1037 colors_
[count
].id
= it
->first
;
1038 colors_
[count
].color
= it
->second
;
1042 void BrowserThemePack::ReadColorsFromJSON(
1043 const base::DictionaryValue
* colors_value
,
1044 std::map
<int, SkColor
>* temp_colors
) {
1045 // Parse the incoming data from |colors_value| into an intermediary structure.
1046 for (base::DictionaryValue::Iterator
iter(*colors_value
); !iter
.IsAtEnd();
1048 const base::ListValue
* color_list
;
1049 if (iter
.value().GetAsList(&color_list
) &&
1050 ((color_list
->GetSize() == 3) || (color_list
->GetSize() == 4))) {
1051 SkColor color
= SK_ColorWHITE
;
1053 if (color_list
->GetInteger(0, &r
) &&
1054 color_list
->GetInteger(1, &g
) &&
1055 color_list
->GetInteger(2, &b
)) {
1056 if (color_list
->GetSize() == 4) {
1059 if (color_list
->GetDouble(3, &alpha
)) {
1060 color
= SkColorSetARGB(static_cast<int>(alpha
* 255), r
, g
, b
);
1061 } else if (color_list
->GetInteger(3, &alpha_int
) &&
1062 (alpha_int
== 0 || alpha_int
== 1)) {
1063 color
= SkColorSetARGB(alpha_int
? 255 : 0, r
, g
, b
);
1065 // Invalid entry for part 4.
1069 color
= SkColorSetRGB(r
, g
, b
);
1072 int id
= GetIntForString(iter
.key(), kColorTable
, kColorTableLength
);
1074 (*temp_colors
)[id
] = color
;
1081 void BrowserThemePack::GenerateMissingColors(
1082 std::map
<int, SkColor
>* colors
) {
1083 // Generate link colors, if missing. (See GetColor()).
1084 if (!colors
->count(ThemeProperties::COLOR_NTP_HEADER
) &&
1085 colors
->count(ThemeProperties::COLOR_NTP_SECTION
)) {
1086 (*colors
)[ThemeProperties::COLOR_NTP_HEADER
] =
1087 (*colors
)[ThemeProperties::COLOR_NTP_SECTION
];
1090 if (!colors
->count(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
) &&
1091 colors
->count(ThemeProperties::COLOR_NTP_SECTION_LINK
)) {
1092 SkColor color_section_link
=
1093 (*colors
)[ThemeProperties::COLOR_NTP_SECTION_LINK
];
1094 (*colors
)[ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
] =
1095 SkColorSetA(color_section_link
, SkColorGetA(color_section_link
) / 3);
1098 if (!colors
->count(ThemeProperties::COLOR_NTP_LINK_UNDERLINE
) &&
1099 colors
->count(ThemeProperties::COLOR_NTP_LINK
)) {
1100 SkColor color_link
= (*colors
)[ThemeProperties::COLOR_NTP_LINK
];
1101 (*colors
)[ThemeProperties::COLOR_NTP_LINK_UNDERLINE
] =
1102 SkColorSetA(color_link
, SkColorGetA(color_link
) / 3);
1105 // Generate frame colors, if missing. (See GenerateFrameColors()).
1107 std::map
<int, SkColor
>::const_iterator it
=
1108 colors
->find(ThemeProperties::COLOR_FRAME
);
1109 if (it
!= colors
->end()) {
1112 frame
= ThemeProperties::GetDefaultColor(
1113 ThemeProperties::COLOR_FRAME
);
1116 if (!colors
->count(ThemeProperties::COLOR_FRAME
)) {
1117 (*colors
)[ThemeProperties::COLOR_FRAME
] =
1118 HSLShift(frame
, GetTintInternal(ThemeProperties::TINT_FRAME
));
1120 if (!colors
->count(ThemeProperties::COLOR_FRAME_INACTIVE
)) {
1121 (*colors
)[ThemeProperties::COLOR_FRAME_INACTIVE
] =
1122 HSLShift(frame
, GetTintInternal(
1123 ThemeProperties::TINT_FRAME_INACTIVE
));
1125 if (!colors
->count(ThemeProperties::COLOR_FRAME_INCOGNITO
)) {
1126 (*colors
)[ThemeProperties::COLOR_FRAME_INCOGNITO
] =
1127 HSLShift(frame
, GetTintInternal(
1128 ThemeProperties::TINT_FRAME_INCOGNITO
));
1130 if (!colors
->count(ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
)) {
1131 (*colors
)[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
] =
1132 HSLShift(frame
, GetTintInternal(
1133 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
));
1137 void BrowserThemePack::BuildDisplayPropertiesFromJSON(
1138 const base::DictionaryValue
* display_properties_value
) {
1139 display_properties_
= new DisplayPropertyPair
[kDisplayPropertiesSize
];
1140 for (size_t i
= 0; i
< kDisplayPropertiesSize
; ++i
) {
1141 display_properties_
[i
].id
= -1;
1142 display_properties_
[i
].property
= 0;
1145 if (!display_properties_value
)
1148 std::map
<int, int> temp_properties
;
1149 for (base::DictionaryValue::Iterator
iter(*display_properties_value
);
1150 !iter
.IsAtEnd(); iter
.Advance()) {
1151 int property_id
= GetIntForString(iter
.key(), kDisplayProperties
,
1152 kDisplayPropertiesSize
);
1153 switch (property_id
) {
1154 case ThemeProperties::NTP_BACKGROUND_ALIGNMENT
: {
1156 if (iter
.value().GetAsString(&val
)) {
1157 temp_properties
[ThemeProperties::NTP_BACKGROUND_ALIGNMENT
] =
1158 ThemeProperties::StringToAlignment(val
);
1162 case ThemeProperties::NTP_BACKGROUND_TILING
: {
1164 if (iter
.value().GetAsString(&val
)) {
1165 temp_properties
[ThemeProperties::NTP_BACKGROUND_TILING
] =
1166 ThemeProperties::StringToTiling(val
);
1170 case ThemeProperties::NTP_LOGO_ALTERNATE
: {
1172 if (iter
.value().GetAsInteger(&val
))
1173 temp_properties
[ThemeProperties::NTP_LOGO_ALTERNATE
] = val
;
1179 // Copy data from the intermediary data structure to the array.
1181 for (std::map
<int, int>::const_iterator it
= temp_properties
.begin();
1182 it
!= temp_properties
.end() && count
< kDisplayPropertiesSize
;
1184 display_properties_
[count
].id
= it
->first
;
1185 display_properties_
[count
].property
= it
->second
;
1189 void BrowserThemePack::ParseImageNamesFromJSON(
1190 const base::DictionaryValue
* images_value
,
1191 const base::FilePath
& images_path
,
1192 FilePathMap
* file_paths
) const {
1196 for (base::DictionaryValue::Iterator
iter(*images_value
); !iter
.IsAtEnd();
1198 if (iter
.value().IsType(base::Value::TYPE_DICTIONARY
)) {
1199 const base::DictionaryValue
* inner_value
= NULL
;
1200 if (iter
.value().GetAsDictionary(&inner_value
)) {
1201 for (base::DictionaryValue::Iterator
inner_iter(*inner_value
);
1202 !inner_iter
.IsAtEnd();
1203 inner_iter
.Advance()) {
1205 ui::ScaleFactor scale_factor
= ui::SCALE_FACTOR_NONE
;
1206 if (GetScaleFactorFromManifestKey(inner_iter
.key(), &scale_factor
) &&
1207 inner_iter
.value().IsType(base::Value::TYPE_STRING
) &&
1208 inner_iter
.value().GetAsString(&name
)) {
1209 AddFileAtScaleToMap(iter
.key(),
1211 images_path
.AppendASCII(name
),
1216 } else if (iter
.value().IsType(base::Value::TYPE_STRING
)) {
1218 if (iter
.value().GetAsString(&name
)) {
1219 AddFileAtScaleToMap(iter
.key(),
1220 ui::SCALE_FACTOR_100P
,
1221 images_path
.AppendASCII(name
),
1228 void BrowserThemePack::AddFileAtScaleToMap(const std::string
& image_name
,
1229 ui::ScaleFactor scale_factor
,
1230 const base::FilePath
& image_path
,
1231 FilePathMap
* file_paths
) const {
1232 int id
= GetPersistentIDByName(image_name
);
1234 (*file_paths
)[id
][scale_factor
] = image_path
;
1235 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1236 id
= GetPersistentIDByNameHelper(image_name
,
1237 kPersistingImagesDesktopAura
,
1238 kPersistingImagesDesktopAuraLength
);
1240 (*file_paths
)[id
][scale_factor
] = image_path
;
1244 void BrowserThemePack::BuildSourceImagesArray(const FilePathMap
& file_paths
) {
1245 std::vector
<int> ids
;
1246 for (FilePathMap::const_iterator it
= file_paths
.begin();
1247 it
!= file_paths
.end(); ++it
) {
1248 ids
.push_back(it
->first
);
1251 source_images_
= new int[ids
.size() + 1];
1252 std::copy(ids
.begin(), ids
.end(), source_images_
);
1253 source_images_
[ids
.size()] = -1;
1256 bool BrowserThemePack::LoadRawBitmapsTo(
1257 const FilePathMap
& file_paths
,
1258 ImageCache
* image_cache
) {
1259 // Themes should be loaded on the file thread, not the UI thread.
1260 // http://crbug.com/61838
1261 base::ThreadRestrictions::ScopedAllowIO allow_io
;
1263 for (FilePathMap::const_iterator it
= file_paths
.begin();
1264 it
!= file_paths
.end(); ++it
) {
1265 int prs_id
= it
->first
;
1266 // Some images need to go directly into |image_memory_|. No modification is
1267 // necessary or desirable.
1268 bool is_copyable
= false;
1269 for (size_t i
= 0; i
< arraysize(kPreloadIDs
); ++i
) {
1270 if (kPreloadIDs
[i
] == prs_id
) {
1275 gfx::ImageSkia image_skia
;
1276 for (int pass
= 0; pass
< 2; ++pass
) {
1277 // Two passes: In the first pass, we process only scale factor
1278 // 100% and in the second pass all other scale factors. We
1279 // process scale factor 100% first because the first image added
1280 // in image_skia.AddRepresentation() determines the DIP size for
1281 // all representations.
1282 for (ScaleFactorToFileMap::const_iterator s2f
= it
->second
.begin();
1283 s2f
!= it
->second
.end(); ++s2f
) {
1284 ui::ScaleFactor scale_factor
= s2f
->first
;
1285 if ((pass
== 0 && scale_factor
!= ui::SCALE_FACTOR_100P
) ||
1286 (pass
== 1 && scale_factor
== ui::SCALE_FACTOR_100P
)) {
1289 scoped_refptr
<base::RefCountedMemory
> raw_data(
1290 ReadFileData(s2f
->second
));
1291 if (!raw_data
.get() || !raw_data
->size()) {
1292 LOG(ERROR
) << "Could not load theme image"
1293 << " prs_id=" << prs_id
1294 << " scale_factor_enum=" << scale_factor
1295 << " file=" << s2f
->second
.value()
1296 << (raw_data
.get() ? " (zero size)" : " (read error)");
1300 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factor
);
1301 image_memory_
[raw_id
] = raw_data
;
1304 if (gfx::PNGCodec::Decode(raw_data
->front(), raw_data
->size(),
1306 image_skia
.AddRepresentation(
1307 gfx::ImageSkiaRep(bitmap
,
1308 ui::GetScaleForScaleFactor(scale_factor
)));
1310 NOTREACHED() << "Unable to decode theme image resource "
1316 if (!is_copyable
&& !image_skia
.isNull())
1317 (*image_cache
)[prs_id
] = gfx::Image(image_skia
);
1323 void BrowserThemePack::CreateImages(ImageCache
* images
) const {
1325 CreateFrameImages(images
);
1326 CreateTintedButtons(GetTintInternal(ThemeProperties::TINT_BUTTONS
), images
);
1327 CreateTabBackgroundImages(images
);
1330 void BrowserThemePack::CropImages(ImageCache
* images
) const {
1331 bool has_frame_border
= HasFrameBorder();
1332 for (size_t i
= 0; i
< arraysize(kImagesToCrop
); ++i
) {
1333 if (has_frame_border
&& kImagesToCrop
[i
].skip_if_frame_border
)
1336 int prs_id
= kImagesToCrop
[i
].prs_id
;
1337 ImageCache::iterator it
= images
->find(prs_id
);
1338 if (it
== images
->end())
1341 int crop_height
= kImagesToCrop
[i
].max_height
;
1342 gfx::ImageSkia image_skia
= it
->second
.AsImageSkia();
1343 (*images
)[prs_id
] = gfx::Image(gfx::ImageSkiaOperations::ExtractSubset(
1344 image_skia
, gfx::Rect(0, 0, image_skia
.width(), crop_height
)));
1348 void BrowserThemePack::CreateFrameImages(ImageCache
* images
) const {
1349 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
1351 // Create all the output images in a separate cache and move them back into
1352 // the input images because there can be name collisions.
1353 ImageCache temp_output
;
1355 for (size_t i
= 0; i
< arraysize(kFrameTintMap
); ++i
) {
1356 int prs_id
= kFrameTintMap
[i
].key
;
1358 // If there's no frame image provided for the specified id, then load
1359 // the default provided frame. If that's not provided, skip this whole
1360 // thing and just use the default images.
1361 int prs_base_id
= 0;
1363 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1364 if (prs_id
== PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
) {
1365 prs_base_id
= images
->count(PRS_THEME_FRAME_INCOGNITO_DESKTOP
) ?
1366 PRS_THEME_FRAME_INCOGNITO_DESKTOP
: PRS_THEME_FRAME_DESKTOP
;
1367 } else if (prs_id
== PRS_THEME_FRAME_INACTIVE_DESKTOP
) {
1368 prs_base_id
= PRS_THEME_FRAME_DESKTOP
;
1369 } else if (prs_id
== PRS_THEME_FRAME_INCOGNITO_DESKTOP
&&
1370 !images
->count(PRS_THEME_FRAME_INCOGNITO_DESKTOP
)) {
1371 prs_base_id
= PRS_THEME_FRAME_DESKTOP
;
1375 if (prs_id
== PRS_THEME_FRAME_INCOGNITO_INACTIVE
) {
1376 prs_base_id
= images
->count(PRS_THEME_FRAME_INCOGNITO
) ?
1377 PRS_THEME_FRAME_INCOGNITO
: PRS_THEME_FRAME
;
1378 } else if (prs_id
== PRS_THEME_FRAME_OVERLAY_INACTIVE
) {
1379 prs_base_id
= PRS_THEME_FRAME_OVERLAY
;
1380 } else if (prs_id
== PRS_THEME_FRAME_INACTIVE
) {
1381 prs_base_id
= PRS_THEME_FRAME
;
1382 } else if (prs_id
== PRS_THEME_FRAME_INCOGNITO
&&
1383 !images
->count(PRS_THEME_FRAME_INCOGNITO
)) {
1384 prs_base_id
= PRS_THEME_FRAME
;
1386 prs_base_id
= prs_id
;
1389 if (images
->count(prs_id
)) {
1390 frame
= (*images
)[prs_id
];
1391 } else if (prs_base_id
!= prs_id
&& images
->count(prs_base_id
)) {
1392 frame
= (*images
)[prs_base_id
];
1393 } else if (prs_base_id
== PRS_THEME_FRAME_OVERLAY
) {
1394 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1395 if (images
->count(PRS_THEME_FRAME_DESKTOP
)) {
1397 if (images
->count(PRS_THEME_FRAME
)) {
1399 // If there is no theme overlay, don't tint the default frame,
1400 // because it will overwrite the custom frame image when we cache and
1401 // reload from disk.
1402 frame
= gfx::Image();
1405 // If the theme doesn't specify an image, then apply the tint to
1406 // the default frame.
1407 frame
= rb
.GetImageNamed(IDR_THEME_FRAME
);
1408 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1409 if (prs_id
>= PRS_THEME_FRAME_DESKTOP
&&
1410 prs_id
<= PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
) {
1411 frame
= rb
.GetImageNamed(IDR_THEME_FRAME_DESKTOP
);
1415 if (!frame
.IsEmpty()) {
1416 temp_output
[prs_id
] = CreateHSLShiftedImage(
1417 frame
, GetTintInternal(kFrameTintMap
[i
].value
));
1420 MergeImageCaches(temp_output
, images
);
1423 void BrowserThemePack::CreateTintedButtons(
1424 const color_utils::HSL
& button_tint
,
1425 ImageCache
* processed_images
) const {
1426 if (button_tint
.h
!= -1 || button_tint
.s
!= -1 || button_tint
.l
!= -1) {
1427 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
1428 const std::set
<int>& idr_ids
=
1429 ThemeProperties::GetTintableToolbarButtons();
1430 for (std::set
<int>::const_iterator it
= idr_ids
.begin();
1431 it
!= idr_ids
.end(); ++it
) {
1432 int prs_id
= GetPersistentIDByIDR(*it
);
1435 // Fetch the image by IDR...
1436 gfx::Image
& button
= rb
.GetImageNamed(*it
);
1438 // but save a version with the persistent ID.
1439 (*processed_images
)[prs_id
] =
1440 CreateHSLShiftedImage(button
, button_tint
);
1445 void BrowserThemePack::CreateTabBackgroundImages(ImageCache
* images
) const {
1446 ImageCache temp_output
;
1447 for (size_t i
= 0; i
< arraysize(kTabBackgroundMap
); ++i
) {
1448 int prs_id
= kTabBackgroundMap
[i
].key
;
1449 int prs_base_id
= kTabBackgroundMap
[i
].value
;
1451 // We only need to generate the background tab images if we were provided
1452 // with a PRS_THEME_FRAME.
1453 ImageCache::const_iterator it
= images
->find(prs_base_id
);
1454 if (it
!= images
->end()) {
1455 gfx::ImageSkia image_to_tint
= (it
->second
).AsImageSkia();
1456 color_utils::HSL hsl_shift
= GetTintInternal(
1457 ThemeProperties::TINT_BACKGROUND_TAB
);
1458 int vertical_offset
= images
->count(prs_id
)
1459 ? kRestoredTabVerticalOffset
: 0;
1461 gfx::ImageSkia overlay
;
1462 ImageCache::const_iterator overlay_it
= images
->find(prs_id
);
1463 if (overlay_it
!= images
->end())
1464 overlay
= overlay_it
->second
.AsImageSkia();
1466 gfx::ImageSkiaSource
* source
= new TabBackgroundImageSource(
1467 image_to_tint
, overlay
, hsl_shift
, vertical_offset
);
1468 // ImageSkia takes ownership of |source|.
1469 temp_output
[prs_id
] = gfx::Image(gfx::ImageSkia(source
,
1470 image_to_tint
.size()));
1473 MergeImageCaches(temp_output
, images
);
1476 void BrowserThemePack::RepackImages(const ImageCache
& images
,
1477 RawImages
* reencoded_images
) const {
1478 for (ImageCache::const_iterator it
= images
.begin();
1479 it
!= images
.end(); ++it
) {
1480 gfx::ImageSkia image_skia
= *it
->second
.ToImageSkia();
1482 typedef std::vector
<gfx::ImageSkiaRep
> ImageSkiaReps
;
1483 ImageSkiaReps image_reps
= image_skia
.image_reps();
1484 if (image_reps
.empty()) {
1485 NOTREACHED() << "No image reps for resource " << it
->first
<< ".";
1487 for (ImageSkiaReps::iterator rep_it
= image_reps
.begin();
1488 rep_it
!= image_reps
.end(); ++rep_it
) {
1489 std::vector
<unsigned char> bitmap_data
;
1490 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it
->sk_bitmap(), false,
1492 NOTREACHED() << "Image file for resource " << it
->first
1493 << " could not be encoded.";
1495 int raw_id
= GetRawIDByPersistentID(
1497 ui::GetSupportedScaleFactor(rep_it
->scale()));
1498 (*reencoded_images
)[raw_id
] =
1499 base::RefCountedBytes::TakeVector(&bitmap_data
);
1504 void BrowserThemePack::MergeImageCaches(
1505 const ImageCache
& source
, ImageCache
* destination
) const {
1506 for (ImageCache::const_iterator it
= source
.begin(); it
!= source
.end();
1508 (*destination
)[it
->first
] = it
->second
;
1512 void BrowserThemePack::AddRawImagesTo(const RawImages
& images
,
1513 RawDataForWriting
* out
) const {
1514 for (RawImages::const_iterator it
= images
.begin(); it
!= images
.end();
1516 (*out
)[it
->first
] = base::StringPiece(
1517 it
->second
->front_as
<char>(), it
->second
->size());
1521 color_utils::HSL
BrowserThemePack::GetTintInternal(int id
) const {
1523 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
1524 if (tints_
[i
].id
== id
) {
1525 color_utils::HSL hsl
;
1526 hsl
.h
= tints_
[i
].h
;
1527 hsl
.s
= tints_
[i
].s
;
1528 hsl
.l
= tints_
[i
].l
;
1534 return ThemeProperties::GetDefaultTint(id
);
1537 int BrowserThemePack::GetRawIDByPersistentID(
1539 ui::ScaleFactor scale_factor
) const {
1543 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1544 if (scale_factors_
[i
] == scale_factor
)
1545 return ((GetMaxPersistentId() + 1) * i
) + prs_id
;
1550 bool BrowserThemePack::GetScaleFactorFromManifestKey(
1551 const std::string
& key
,
1552 ui::ScaleFactor
* scale_factor
) const {
1554 if (base::StringToInt(key
, &percent
)) {
1555 float scale
= static_cast<float>(percent
) / 100.0f
;
1556 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1557 if (fabs(ui::GetScaleForScaleFactor(scale_factors_
[i
]) - scale
)
1559 *scale_factor
= scale_factors_
[i
];
1567 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id
) {
1568 // Compute (by scaling) bitmaps for |prs_id| for any scale factors
1569 // for which the theme author did not provide a bitmap. We compute
1570 // the bitmaps using the highest scale factor that theme author
1572 // Note: We use only supported scale factors. For example, if scale
1573 // factor 2x is supported by the current system, but 1.8x is not and
1574 // if the theme author did not provide an image for 2x but one for
1575 // 1.8x, we will not use the 1.8x image here. Here we will only use
1576 // images provided for scale factors supported by the current system.
1578 // See if any image is missing. If not, we're done.
1579 bool image_missing
= false;
1580 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1581 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1582 if (image_memory_
.find(raw_id
) == image_memory_
.end()) {
1583 image_missing
= true;
1590 // Find available scale factor with highest scale.
1591 ui::ScaleFactor available_scale_factor
= ui::SCALE_FACTOR_NONE
;
1592 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1593 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1594 if ((available_scale_factor
== ui::SCALE_FACTOR_NONE
||
1595 (ui::GetScaleForScaleFactor(scale_factors_
[i
]) >
1596 ui::GetScaleForScaleFactor(available_scale_factor
))) &&
1597 image_memory_
.find(raw_id
) != image_memory_
.end()) {
1598 available_scale_factor
= scale_factors_
[i
];
1601 // If no scale factor is available, we're done.
1602 if (available_scale_factor
== ui::SCALE_FACTOR_NONE
)
1605 // Get bitmap for the available scale factor.
1606 int available_raw_id
= GetRawIDByPersistentID(prs_id
, available_scale_factor
);
1607 RawImages::const_iterator it
= image_memory_
.find(available_raw_id
);
1608 SkBitmap available_bitmap
;
1609 if (!gfx::PNGCodec::Decode(it
->second
->front(),
1611 &available_bitmap
)) {
1612 NOTREACHED() << "Unable to decode theme image for prs_id="
1613 << prs_id
<< " for scale_factor=" << available_scale_factor
;
1617 // Fill in all missing scale factors by scaling the available bitmap.
1618 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1619 int scaled_raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1620 if (image_memory_
.find(scaled_raw_id
) != image_memory_
.end())
1622 SkBitmap scaled_bitmap
=
1623 CreateLowQualityResizedBitmap(available_bitmap
,
1624 available_scale_factor
,
1626 std::vector
<unsigned char> bitmap_data
;
1627 if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap
,
1630 NOTREACHED() << "Unable to encode theme image for prs_id="
1631 << prs_id
<< " for scale_factor=" << scale_factors_
[i
];
1634 image_memory_
[scaled_raw_id
] =
1635 base::RefCountedBytes::TakeVector(&bitmap_data
);