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/image/canvas_image_source.h"
30 #include "ui/gfx/image/image.h"
31 #include "ui/gfx/image/image_skia.h"
32 #include "ui/gfx/image/image_skia_operations.h"
33 #include "ui/gfx/screen.h"
34 #include "ui/gfx/size_conversions.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.
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
!= NULL
&&
208 base::strcasecmp(key
.c_str(), image_table
[i
].key
) == 0) {
209 return image_table
[i
].persistent_id
;
215 int GetPersistentIDByName(const std::string
& key
) {
216 return GetPersistentIDByNameHelper(key
,
218 kPersistingImagesLength
);
221 int GetPersistentIDByIDR(int idr
) {
222 static std::map
<int,int>* lookup_table
= new std::map
<int,int>();
223 if (lookup_table
->empty()) {
224 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
) {
225 int idr
= kPersistingImages
[i
].idr_id
;
226 int prs_id
= kPersistingImages
[i
].persistent_id
;
227 (*lookup_table
)[idr
] = prs_id
;
229 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
230 for (size_t i
= 0; i
< kPersistingImagesDesktopAuraLength
; ++i
) {
231 int idr
= kPersistingImagesDesktopAura
[i
].idr_id
;
232 int prs_id
= kPersistingImagesDesktopAura
[i
].persistent_id
;
233 (*lookup_table
)[idr
] = prs_id
;
237 std::map
<int,int>::iterator it
= lookup_table
->find(idr
);
238 return (it
== lookup_table
->end()) ? -1 : it
->second
;
241 // Returns the maximum persistent id.
242 int GetMaxPersistentId() {
243 static int max_prs_id
= -1;
244 if (max_prs_id
== -1) {
245 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
) {
246 if (kPersistingImages
[i
].persistent_id
> max_prs_id
)
247 max_prs_id
= kPersistingImages
[i
].persistent_id
;
249 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
250 for (size_t i
= 0; i
< kPersistingImagesDesktopAuraLength
; ++i
) {
251 if (kPersistingImagesDesktopAura
[i
].persistent_id
> max_prs_id
)
252 max_prs_id
= kPersistingImagesDesktopAura
[i
].persistent_id
;
259 // Returns true if the scales in |input| match those in |expected|.
260 // The order must match as the index is used in determining the raw id.
261 bool InputScalesValid(const base::StringPiece
& input
,
262 const std::vector
<ui::ScaleFactor
>& expected
) {
263 size_t scales_size
= static_cast<size_t>(input
.size() / sizeof(float));
264 if (scales_size
!= expected
.size())
266 scoped_ptr
<float[]> scales(new float[scales_size
]);
267 // Do a memcpy to avoid misaligned memory access.
268 memcpy(scales
.get(), input
.data(), input
.size());
269 for (size_t index
= 0; index
< scales_size
; ++index
) {
270 if (scales
[index
] != ui::GetScaleForScaleFactor(expected
[index
]))
276 // Returns |scale_factors| as a string to be written to disk.
277 std::string
GetScaleFactorsAsString(
278 const std::vector
<ui::ScaleFactor
>& scale_factors
) {
279 scoped_ptr
<float[]> scales(new float[scale_factors
.size()]);
280 for (size_t i
= 0; i
< scale_factors
.size(); ++i
)
281 scales
[i
] = ui::GetScaleForScaleFactor(scale_factors
[i
]);
282 std::string out_string
= std::string(
283 reinterpret_cast<const char*>(scales
.get()),
284 scale_factors
.size() * sizeof(float));
288 struct StringToIntTable
{
290 ThemeProperties::OverwritableByUserThemeProperty id
;
293 // Strings used by themes to identify tints in the JSON.
294 StringToIntTable kTintTable
[] = {
295 { "buttons", ThemeProperties::TINT_BUTTONS
},
296 { "frame", ThemeProperties::TINT_FRAME
},
297 { "frame_inactive", ThemeProperties::TINT_FRAME_INACTIVE
},
298 { "frame_incognito", ThemeProperties::TINT_FRAME_INCOGNITO
},
299 { "frame_incognito_inactive",
300 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
301 { "background_tab", ThemeProperties::TINT_BACKGROUND_TAB
},
303 const size_t kTintTableLength
= arraysize(kTintTable
);
305 // Strings used by themes to identify colors in the JSON.
306 StringToIntTable kColorTable
[] = {
307 { "frame", ThemeProperties::COLOR_FRAME
},
308 { "frame_inactive", ThemeProperties::COLOR_FRAME_INACTIVE
},
309 { "frame_incognito", ThemeProperties::COLOR_FRAME_INCOGNITO
},
310 { "frame_incognito_inactive",
311 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
},
312 { "toolbar", ThemeProperties::COLOR_TOOLBAR
},
313 { "tab_text", ThemeProperties::COLOR_TAB_TEXT
},
314 { "tab_background_text", ThemeProperties::COLOR_BACKGROUND_TAB_TEXT
},
315 { "bookmark_text", ThemeProperties::COLOR_BOOKMARK_TEXT
},
316 { "ntp_background", ThemeProperties::COLOR_NTP_BACKGROUND
},
317 { "ntp_text", ThemeProperties::COLOR_NTP_TEXT
},
318 { "ntp_link", ThemeProperties::COLOR_NTP_LINK
},
319 { "ntp_link_underline", ThemeProperties::COLOR_NTP_LINK_UNDERLINE
},
320 { "ntp_header", ThemeProperties::COLOR_NTP_HEADER
},
321 { "ntp_section", ThemeProperties::COLOR_NTP_SECTION
},
322 { "ntp_section_text", ThemeProperties::COLOR_NTP_SECTION_TEXT
},
323 { "ntp_section_link", ThemeProperties::COLOR_NTP_SECTION_LINK
},
324 { "ntp_section_link_underline",
325 ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
},
326 { "button_background", ThemeProperties::COLOR_BUTTON_BACKGROUND
},
328 const size_t kColorTableLength
= arraysize(kColorTable
);
330 // Strings used by themes to identify display properties keys in JSON.
331 StringToIntTable kDisplayProperties
[] = {
332 { "ntp_background_alignment",
333 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
},
334 { "ntp_background_repeat", ThemeProperties::NTP_BACKGROUND_TILING
},
335 { "ntp_logo_alternate", ThemeProperties::NTP_LOGO_ALTERNATE
},
337 const size_t kDisplayPropertiesSize
= arraysize(kDisplayProperties
);
339 int GetIntForString(const std::string
& key
,
340 StringToIntTable
* table
,
341 size_t table_length
) {
342 for (size_t i
= 0; i
< table_length
; ++i
) {
343 if (base::strcasecmp(key
.c_str(), table
[i
].key
) == 0) {
351 struct IntToIntTable
{
356 // Mapping used in CreateFrameImages() to associate frame images with the
357 // tint ID that should maybe be applied to it.
358 IntToIntTable kFrameTintMap
[] = {
359 { PRS_THEME_FRAME
, ThemeProperties::TINT_FRAME
},
360 { PRS_THEME_FRAME_INACTIVE
, ThemeProperties::TINT_FRAME_INACTIVE
},
361 { PRS_THEME_FRAME_OVERLAY
, ThemeProperties::TINT_FRAME
},
362 { PRS_THEME_FRAME_OVERLAY_INACTIVE
,
363 ThemeProperties::TINT_FRAME_INACTIVE
},
364 { PRS_THEME_FRAME_INCOGNITO
, ThemeProperties::TINT_FRAME_INCOGNITO
},
365 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
,
366 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
367 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
368 { PRS_THEME_FRAME_DESKTOP
, ThemeProperties::TINT_FRAME
},
369 { PRS_THEME_FRAME_INACTIVE_DESKTOP
, ThemeProperties::TINT_FRAME_INACTIVE
},
370 { PRS_THEME_FRAME_INCOGNITO_DESKTOP
, ThemeProperties::TINT_FRAME_INCOGNITO
},
371 { PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
,
372 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
},
376 // Mapping used in GenerateTabBackgroundImages() to associate what frame image
377 // goes with which tab background.
378 IntToIntTable kTabBackgroundMap
[] = {
379 { PRS_THEME_TAB_BACKGROUND
, PRS_THEME_FRAME
},
380 { PRS_THEME_TAB_BACKGROUND_INCOGNITO
, PRS_THEME_FRAME_INCOGNITO
},
381 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
382 { PRS_THEME_TAB_BACKGROUND_DESKTOP
, PRS_THEME_FRAME_DESKTOP
},
383 { PRS_THEME_TAB_BACKGROUND_INCOGNITO_DESKTOP
,
384 PRS_THEME_FRAME_INCOGNITO_DESKTOP
},
391 // The maximum useful height of the image at |prs_id|.
394 // Whether cropping the image at |prs_id| should be skipped on OSes which
395 // have a frame border to the left and right of the web contents.
396 // This should be true for images which can be used to decorate the border to
397 // the left and the right of the web contents.
398 bool skip_if_frame_border
;
401 // The images which should be cropped before being saved to the data pack. The
402 // maximum heights are meant to be conservative as to give room for the UI to
403 // change without the maximum heights having to be modified.
404 // |kThemePackVersion| must be incremented if any of the maximum heights below
406 struct CropEntry kImagesToCrop
[] = {
407 { PRS_THEME_FRAME
, 120, true },
408 { PRS_THEME_FRAME_INACTIVE
, 120, true },
409 { PRS_THEME_FRAME_INCOGNITO
, 120, true },
410 { PRS_THEME_FRAME_INCOGNITO_INACTIVE
, 120, true },
411 { PRS_THEME_FRAME_OVERLAY
, 120, true },
412 { PRS_THEME_FRAME_OVERLAY_INACTIVE
, 120, true },
413 { PRS_THEME_TOOLBAR
, 200, false },
414 { PRS_THEME_BUTTON_BACKGROUND
, 60, false },
415 { PRS_THEME_WINDOW_CONTROL_BACKGROUND
, 50, false },
416 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
417 { PRS_THEME_TOOLBAR_DESKTOP
, 200, false }
422 // A list of images that don't need tinting or any other modification and can
423 // be byte-copied directly into the finished DataPack. This should contain the
424 // persistent IDs for all themeable image IDs that aren't in kFrameTintMap,
425 // kTabBackgroundMap or kImagesToCrop.
426 const int kPreloadIDs
[] = {
427 PRS_THEME_NTP_BACKGROUND
,
428 PRS_THEME_NTP_ATTRIBUTION
,
431 // Returns true if this OS uses a browser frame which has a non zero width to
432 // the left and the right of the web contents.
433 bool HasFrameBorder() {
434 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
441 // Returns a piece of memory with the contents of the file |path|.
442 base::RefCountedMemory
* ReadFileData(const base::FilePath
& path
) {
444 base::File
file(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
445 if (file
.IsValid()) {
446 int64 length
= file
.GetLength();
447 if (length
> 0 && length
< INT_MAX
) {
448 int size
= static_cast<int>(length
);
449 std::vector
<unsigned char> raw_data
;
450 raw_data
.resize(size
);
451 char* data
= reinterpret_cast<char*>(&(raw_data
.front()));
452 if (file
.ReadAtCurrentPos(data
, size
) == length
)
453 return base::RefCountedBytes::TakeVector(&raw_data
);
461 // Shifts an image's HSL values. The caller is responsible for deleting
462 // the returned image.
463 gfx::Image
CreateHSLShiftedImage(const gfx::Image
& image
,
464 const color_utils::HSL
& hsl_shift
) {
465 const gfx::ImageSkia
* src_image
= image
.ToImageSkia();
466 return gfx::Image(gfx::ImageSkiaOperations::CreateHSLShiftedImage(
467 *src_image
, hsl_shift
));
470 // Computes a bitmap at one scale from a bitmap at a different scale.
471 SkBitmap
CreateLowQualityResizedBitmap(const SkBitmap
& source_bitmap
,
472 ui::ScaleFactor source_scale_factor
,
473 ui::ScaleFactor desired_scale_factor
) {
474 gfx::Size scaled_size
= gfx::ToCeiledSize(
475 gfx::ScaleSize(gfx::Size(source_bitmap
.width(),
476 source_bitmap
.height()),
477 ui::GetScaleForScaleFactor(desired_scale_factor
) /
478 ui::GetScaleForScaleFactor(source_scale_factor
)));
479 SkBitmap scaled_bitmap
;
480 scaled_bitmap
.allocN32Pixels(scaled_size
.width(), scaled_size
.height());
481 scaled_bitmap
.eraseARGB(0, 0, 0, 0);
482 SkCanvas
canvas(scaled_bitmap
);
483 SkRect scaled_bounds
= RectToSkRect(gfx::Rect(scaled_size
));
484 // Note(oshima): The following scaling code doesn't work with
486 canvas
.drawBitmapRect(source_bitmap
, NULL
, scaled_bounds
);
487 return scaled_bitmap
;
490 // A ImageSkiaSource that scales 100P image to the target scale factor
491 // if the ImageSkiaRep for the target scale factor isn't available.
492 class ThemeImageSource
: public gfx::ImageSkiaSource
{
494 explicit ThemeImageSource(const gfx::ImageSkia
& source
) : source_(source
) {
496 virtual ~ThemeImageSource() {}
498 virtual gfx::ImageSkiaRep
GetImageForScale(float scale
) override
{
499 if (source_
.HasRepresentation(scale
))
500 return source_
.GetRepresentation(scale
);
501 const gfx::ImageSkiaRep
& rep_100p
= source_
.GetRepresentation(1.0f
);
502 SkBitmap scaled_bitmap
= CreateLowQualityResizedBitmap(
503 rep_100p
.sk_bitmap(),
504 ui::SCALE_FACTOR_100P
,
505 ui::GetSupportedScaleFactor(scale
));
506 return gfx::ImageSkiaRep(scaled_bitmap
, scale
);
510 const gfx::ImageSkia source_
;
512 DISALLOW_COPY_AND_ASSIGN(ThemeImageSource
);
515 // An ImageSkiaSource that delays decoding PNG data into bitmaps until
516 // needed. Missing data for a scale factor is computed by scaling data for an
517 // available scale factor. Computed bitmaps are stored for future look up.
518 class ThemeImagePngSource
: public gfx::ImageSkiaSource
{
520 typedef std::map
<ui::ScaleFactor
,
521 scoped_refptr
<base::RefCountedMemory
> > PngMap
;
523 explicit ThemeImagePngSource(const PngMap
& png_map
) : png_map_(png_map
) {}
525 virtual ~ThemeImagePngSource() {}
528 virtual gfx::ImageSkiaRep
GetImageForScale(float scale
) override
{
529 ui::ScaleFactor scale_factor
= ui::GetSupportedScaleFactor(scale
);
530 // Look up the bitmap for |scale factor| in the bitmap map. If found
532 BitmapMap::const_iterator exact_bitmap_it
= bitmap_map_
.find(scale_factor
);
533 if (exact_bitmap_it
!= bitmap_map_
.end())
534 return gfx::ImageSkiaRep(exact_bitmap_it
->second
, scale
);
536 // Look up the raw PNG data for |scale_factor| in the png map. If found,
537 // decode it, store the result in the bitmap map and return it.
538 PngMap::const_iterator exact_png_it
= png_map_
.find(scale_factor
);
539 if (exact_png_it
!= png_map_
.end()) {
541 if (!gfx::PNGCodec::Decode(exact_png_it
->second
->front(),
542 exact_png_it
->second
->size(),
545 return gfx::ImageSkiaRep();
547 bitmap_map_
[scale_factor
] = bitmap
;
548 return gfx::ImageSkiaRep(bitmap
, scale
);
551 // Find an available PNG for another scale factor. We want to use the
552 // highest available scale factor.
553 PngMap::const_iterator available_png_it
= png_map_
.end();
554 for (PngMap::const_iterator png_it
= png_map_
.begin();
555 png_it
!= png_map_
.end(); ++png_it
) {
556 if (available_png_it
== png_map_
.end() ||
557 ui::GetScaleForScaleFactor(png_it
->first
) >
558 ui::GetScaleForScaleFactor(available_png_it
->first
)) {
559 available_png_it
= png_it
;
562 if (available_png_it
== png_map_
.end())
563 return gfx::ImageSkiaRep();
564 ui::ScaleFactor available_scale_factor
= available_png_it
->first
;
566 // Look up the bitmap for |available_scale_factor| in the bitmap map.
567 // If not found, decode the corresponging png data, store the result
568 // in the bitmap map.
569 BitmapMap::const_iterator available_bitmap_it
=
570 bitmap_map_
.find(available_scale_factor
);
571 if (available_bitmap_it
== bitmap_map_
.end()) {
572 SkBitmap available_bitmap
;
573 if (!gfx::PNGCodec::Decode(available_png_it
->second
->front(),
574 available_png_it
->second
->size(),
575 &available_bitmap
)) {
577 return gfx::ImageSkiaRep();
579 bitmap_map_
[available_scale_factor
] = available_bitmap
;
580 available_bitmap_it
= bitmap_map_
.find(available_scale_factor
);
583 // Scale the available bitmap to the desired scale factor, store the result
584 // in the bitmap map and return it.
585 SkBitmap scaled_bitmap
= CreateLowQualityResizedBitmap(
586 available_bitmap_it
->second
,
587 available_scale_factor
,
589 bitmap_map_
[scale_factor
] = scaled_bitmap
;
590 return gfx::ImageSkiaRep(scaled_bitmap
, scale
);
595 typedef std::map
<ui::ScaleFactor
, SkBitmap
> BitmapMap
;
596 BitmapMap bitmap_map_
;
598 DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource
);
601 class TabBackgroundImageSource
: public gfx::CanvasImageSource
{
603 TabBackgroundImageSource(const gfx::ImageSkia
& image_to_tint
,
604 const gfx::ImageSkia
& overlay
,
605 const color_utils::HSL
& hsl_shift
,
607 : gfx::CanvasImageSource(image_to_tint
.size(), false),
608 image_to_tint_(image_to_tint
),
610 hsl_shift_(hsl_shift
),
611 vertical_offset_(vertical_offset
) {
614 virtual ~TabBackgroundImageSource() {
617 // Overridden from CanvasImageSource:
618 virtual void Draw(gfx::Canvas
* canvas
) override
{
619 gfx::ImageSkia bg_tint
=
620 gfx::ImageSkiaOperations::CreateHSLShiftedImage(image_to_tint_
,
622 canvas
->TileImageInt(bg_tint
, 0, vertical_offset_
, 0, 0,
623 size().width(), size().height());
625 // If they've provided a custom image, overlay it.
626 if (!overlay_
.isNull()) {
627 canvas
->TileImageInt(overlay_
, 0, 0, size().width(),
633 const gfx::ImageSkia image_to_tint_
;
634 const gfx::ImageSkia overlay_
;
635 const color_utils::HSL hsl_shift_
;
636 const int vertical_offset_
;
638 DISALLOW_COPY_AND_ASSIGN(TabBackgroundImageSource
);
643 BrowserThemePack::~BrowserThemePack() {
644 if (!data_pack_
.get()) {
648 delete [] display_properties_
;
649 delete [] source_images_
;
654 scoped_refptr
<BrowserThemePack
> BrowserThemePack::BuildFromExtension(
655 const Extension
* extension
) {
656 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
658 DCHECK(extension
->is_theme());
660 scoped_refptr
<BrowserThemePack
> pack(new BrowserThemePack
);
661 pack
->BuildHeader(extension
);
662 pack
->BuildTintsFromJSON(extensions::ThemeInfo::GetTints(extension
));
663 pack
->BuildColorsFromJSON(extensions::ThemeInfo::GetColors(extension
));
664 pack
->BuildDisplayPropertiesFromJSON(
665 extensions::ThemeInfo::GetDisplayProperties(extension
));
667 // Builds the images. (Image building is dependent on tints).
668 FilePathMap file_paths
;
669 pack
->ParseImageNamesFromJSON(
670 extensions::ThemeInfo::GetImages(extension
),
673 pack
->BuildSourceImagesArray(file_paths
);
675 if (!pack
->LoadRawBitmapsTo(file_paths
, &pack
->images_on_ui_thread_
))
678 pack
->CreateImages(&pack
->images_on_ui_thread_
);
680 // Make sure the |images_on_file_thread_| has bitmaps for supported
681 // scale factors before passing to FILE thread.
682 pack
->images_on_file_thread_
= pack
->images_on_ui_thread_
;
683 for (ImageCache::iterator it
= pack
->images_on_file_thread_
.begin();
684 it
!= pack
->images_on_file_thread_
.end(); ++it
) {
685 gfx::ImageSkia
* image_skia
=
686 const_cast<gfx::ImageSkia
*>(it
->second
.ToImageSkia());
687 image_skia
->MakeThreadSafe();
690 // Set ThemeImageSource on |images_on_ui_thread_| to resample the source
691 // image if a caller of BrowserThemePack::GetImageNamed() requests an
692 // ImageSkiaRep for a scale factor not specified by the theme author.
693 // Callers of BrowserThemePack::GetImageNamed() to be able to retrieve
694 // ImageSkiaReps for all supported scale factors.
695 for (ImageCache::iterator it
= pack
->images_on_ui_thread_
.begin();
696 it
!= pack
->images_on_ui_thread_
.end(); ++it
) {
697 const gfx::ImageSkia source_image_skia
= it
->second
.AsImageSkia();
698 ThemeImageSource
* source
= new ThemeImageSource(source_image_skia
);
699 // image_skia takes ownership of source.
700 gfx::ImageSkia
image_skia(source
, source_image_skia
.size());
701 it
->second
= gfx::Image(image_skia
);
704 // Generate raw images (for new-tab-page attribution and background) for
705 // any missing scale from an available scale image.
706 for (size_t i
= 0; i
< arraysize(kPreloadIDs
); ++i
) {
707 pack
->GenerateRawImageForAllSupportedScales(kPreloadIDs
[i
]);
710 // The BrowserThemePack is now in a consistent state.
715 scoped_refptr
<BrowserThemePack
> BrowserThemePack::BuildFromDataPack(
716 const base::FilePath
& path
, const std::string
& expected_id
) {
717 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
718 // Allow IO on UI thread due to deep-seated theme design issues.
719 // (see http://crbug.com/80206)
720 base::ThreadRestrictions::ScopedAllowIO allow_io
;
721 scoped_refptr
<BrowserThemePack
> pack(new BrowserThemePack
);
722 // Scale factor parameter is moot as data pack has image resources for all
723 // supported scale factors.
724 pack
->data_pack_
.reset(
725 new ui::DataPack(ui::SCALE_FACTOR_NONE
));
727 if (!pack
->data_pack_
->LoadFromPath(path
)) {
728 LOG(ERROR
) << "Failed to load theme data pack.";
732 base::StringPiece pointer
;
733 if (!pack
->data_pack_
->GetStringPiece(kHeaderID
, &pointer
))
735 pack
->header_
= reinterpret_cast<BrowserThemePackHeader
*>(const_cast<char*>(
738 if (pack
->header_
->version
!= kThemePackVersion
) {
739 DLOG(ERROR
) << "BuildFromDataPack failure! Version mismatch!";
742 // TODO(erg): Check endianess once DataPack works on the other endian.
743 std::string
theme_id(reinterpret_cast<char*>(pack
->header_
->theme_id
),
744 crx_file::id_util::kIdSize
);
745 std::string truncated_id
= expected_id
.substr(0, crx_file::id_util::kIdSize
);
746 if (theme_id
!= truncated_id
) {
747 DLOG(ERROR
) << "Wrong id: " << theme_id
<< " vs " << expected_id
;
751 if (!pack
->data_pack_
->GetStringPiece(kTintsID
, &pointer
))
753 pack
->tints_
= reinterpret_cast<TintEntry
*>(const_cast<char*>(
756 if (!pack
->data_pack_
->GetStringPiece(kColorsID
, &pointer
))
759 reinterpret_cast<ColorPair
*>(const_cast<char*>(pointer
.data()));
761 if (!pack
->data_pack_
->GetStringPiece(kDisplayPropertiesID
, &pointer
))
763 pack
->display_properties_
= reinterpret_cast<DisplayPropertyPair
*>(
764 const_cast<char*>(pointer
.data()));
766 if (!pack
->data_pack_
->GetStringPiece(kSourceImagesID
, &pointer
))
768 pack
->source_images_
= reinterpret_cast<int*>(
769 const_cast<char*>(pointer
.data()));
771 if (!pack
->data_pack_
->GetStringPiece(kScaleFactorsID
, &pointer
))
774 if (!InputScalesValid(pointer
, pack
->scale_factors_
)) {
775 DLOG(ERROR
) << "BuildFromDataPack failure! The pack scale factors differ "
776 << "from those supported by platform.";
783 void BrowserThemePack::GetThemeableImageIDRs(std::set
<int>* result
) {
788 for (size_t i
= 0; i
< kPersistingImagesLength
; ++i
)
789 result
->insert(kPersistingImages
[i
].idr_id
);
791 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
792 for (size_t i
= 0; i
< kPersistingImagesDesktopAuraLength
; ++i
)
793 result
->insert(kPersistingImagesDesktopAura
[i
].idr_id
);
797 bool BrowserThemePack::WriteToDisk(const base::FilePath
& path
) const {
798 // Add resources for each of the property arrays.
799 RawDataForWriting resources
;
800 resources
[kHeaderID
] = base::StringPiece(
801 reinterpret_cast<const char*>(header_
), sizeof(BrowserThemePackHeader
));
802 resources
[kTintsID
] = base::StringPiece(
803 reinterpret_cast<const char*>(tints_
),
804 sizeof(TintEntry
[kTintTableLength
]));
805 resources
[kColorsID
] = base::StringPiece(
806 reinterpret_cast<const char*>(colors_
),
807 sizeof(ColorPair
[kColorTableLength
]));
808 resources
[kDisplayPropertiesID
] = base::StringPiece(
809 reinterpret_cast<const char*>(display_properties_
),
810 sizeof(DisplayPropertyPair
[kDisplayPropertiesSize
]));
812 int source_count
= 1;
813 int* end
= source_images_
;
814 for (; *end
!= -1 ; end
++)
816 resources
[kSourceImagesID
] = base::StringPiece(
817 reinterpret_cast<const char*>(source_images_
),
818 source_count
* sizeof(*source_images_
));
820 // Store results of GetScaleFactorsAsString() in std::string as
821 // base::StringPiece does not copy data in constructor.
822 std::string scale_factors_string
= GetScaleFactorsAsString(scale_factors_
);
823 resources
[kScaleFactorsID
] = scale_factors_string
;
825 AddRawImagesTo(image_memory_
, &resources
);
827 RawImages reencoded_images
;
828 RepackImages(images_on_file_thread_
, &reencoded_images
);
829 AddRawImagesTo(reencoded_images
, &resources
);
831 return ui::DataPack::WritePack(path
, resources
, ui::DataPack::BINARY
);
834 bool BrowserThemePack::GetTint(int id
, color_utils::HSL
* hsl
) const {
836 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
837 if (tints_
[i
].id
== id
) {
838 hsl
->h
= tints_
[i
].h
;
839 hsl
->s
= tints_
[i
].s
;
840 hsl
->l
= tints_
[i
].l
;
849 bool BrowserThemePack::GetColor(int id
, SkColor
* color
) const {
851 for (size_t i
= 0; i
< kColorTableLength
; ++i
) {
852 if (colors_
[i
].id
== id
) {
853 *color
= colors_
[i
].color
;
862 bool BrowserThemePack::GetDisplayProperty(int id
, int* result
) const {
863 if (display_properties_
) {
864 for (size_t i
= 0; i
< kDisplayPropertiesSize
; ++i
) {
865 if (display_properties_
[i
].id
== id
) {
866 *result
= display_properties_
[i
].property
;
875 gfx::Image
BrowserThemePack::GetImageNamed(int idr_id
) {
876 int prs_id
= GetPersistentIDByIDR(idr_id
);
880 // Check if the image is cached.
881 ImageCache::const_iterator image_iter
= images_on_ui_thread_
.find(prs_id
);
882 if (image_iter
!= images_on_ui_thread_
.end())
883 return image_iter
->second
;
885 ThemeImagePngSource::PngMap png_map
;
886 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
887 scoped_refptr
<base::RefCountedMemory
> memory
=
888 GetRawData(idr_id
, scale_factors_
[i
]);
890 png_map
[scale_factors_
[i
]] = memory
;
892 if (!png_map
.empty()) {
893 gfx::ImageSkia
image_skia(new ThemeImagePngSource(png_map
), 1.0f
);
894 // |image_skia| takes ownership of ThemeImagePngSource.
895 gfx::Image ret
= gfx::Image(image_skia
);
896 images_on_ui_thread_
[prs_id
] = ret
;
903 base::RefCountedMemory
* BrowserThemePack::GetRawData(
905 ui::ScaleFactor scale_factor
) const {
906 base::RefCountedMemory
* memory
= NULL
;
907 int prs_id
= GetPersistentIDByIDR(idr_id
);
908 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factor
);
911 if (data_pack_
.get()) {
912 memory
= data_pack_
->GetStaticMemory(raw_id
);
914 RawImages::const_iterator it
= image_memory_
.find(raw_id
);
915 if (it
!= image_memory_
.end()) {
916 memory
= it
->second
.get();
924 bool BrowserThemePack::HasCustomImage(int idr_id
) const {
925 int prs_id
= GetPersistentIDByIDR(idr_id
);
929 int* img
= source_images_
;
930 for (; *img
!= -1; ++img
) {
940 BrowserThemePack::BrowserThemePack()
941 : CustomThemeSupplier(EXTENSION
),
945 display_properties_(NULL
),
946 source_images_(NULL
) {
947 scale_factors_
= ui::GetSupportedScaleFactors();
948 // On Windows HiDPI SCALE_FACTOR_100P may not be supported by default.
949 if (std::find(scale_factors_
.begin(), scale_factors_
.end(),
950 ui::SCALE_FACTOR_100P
) == scale_factors_
.end()) {
951 scale_factors_
.push_back(ui::SCALE_FACTOR_100P
);
955 void BrowserThemePack::BuildHeader(const Extension
* extension
) {
956 header_
= new BrowserThemePackHeader
;
957 header_
->version
= kThemePackVersion
;
959 // TODO(erg): Need to make this endian safe on other computers. Prerequisite
960 // is that ui::DataPack removes this same check.
961 #if defined(__BYTE_ORDER)
963 COMPILE_ASSERT(__BYTE_ORDER
== __LITTLE_ENDIAN
,
964 datapack_assumes_little_endian
);
965 #elif defined(__BIG_ENDIAN__)
967 #error DataPack assumes little endian
969 header_
->little_endian
= 1;
971 const std::string
& id
= extension
->id();
972 memcpy(header_
->theme_id
, id
.c_str(), crx_file::id_util::kIdSize
);
975 void BrowserThemePack::BuildTintsFromJSON(
976 const base::DictionaryValue
* tints_value
) {
977 tints_
= new TintEntry
[kTintTableLength
];
978 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
988 // Parse the incoming data from |tints_value| into an intermediary structure.
989 std::map
<int, color_utils::HSL
> temp_tints
;
990 for (base::DictionaryValue::Iterator
iter(*tints_value
); !iter
.IsAtEnd();
992 const base::ListValue
* tint_list
;
993 if (iter
.value().GetAsList(&tint_list
) &&
994 (tint_list
->GetSize() == 3)) {
995 color_utils::HSL hsl
= { -1, -1, -1 };
997 if (tint_list
->GetDouble(0, &hsl
.h
) &&
998 tint_list
->GetDouble(1, &hsl
.s
) &&
999 tint_list
->GetDouble(2, &hsl
.l
)) {
1001 int id
= GetIntForString(iter
.key(), kTintTable
, kTintTableLength
);
1003 temp_tints
[id
] = hsl
;
1009 // Copy data from the intermediary data structure to the array.
1011 for (std::map
<int, color_utils::HSL
>::const_iterator it
=
1013 it
!= temp_tints
.end() && count
< kTintTableLength
;
1015 tints_
[count
].id
= it
->first
;
1016 tints_
[count
].h
= it
->second
.h
;
1017 tints_
[count
].s
= it
->second
.s
;
1018 tints_
[count
].l
= it
->second
.l
;
1022 void BrowserThemePack::BuildColorsFromJSON(
1023 const base::DictionaryValue
* colors_value
) {
1024 colors_
= new ColorPair
[kColorTableLength
];
1025 for (size_t i
= 0; i
< kColorTableLength
; ++i
) {
1027 colors_
[i
].color
= SkColorSetRGB(0, 0, 0);
1030 std::map
<int, SkColor
> temp_colors
;
1032 ReadColorsFromJSON(colors_value
, &temp_colors
);
1033 GenerateMissingColors(&temp_colors
);
1035 // Copy data from the intermediary data structure to the array.
1037 for (std::map
<int, SkColor
>::const_iterator it
= temp_colors
.begin();
1038 it
!= temp_colors
.end() && count
< kColorTableLength
; ++it
, ++count
) {
1039 colors_
[count
].id
= it
->first
;
1040 colors_
[count
].color
= it
->second
;
1044 void BrowserThemePack::ReadColorsFromJSON(
1045 const base::DictionaryValue
* colors_value
,
1046 std::map
<int, SkColor
>* temp_colors
) {
1047 // Parse the incoming data from |colors_value| into an intermediary structure.
1048 for (base::DictionaryValue::Iterator
iter(*colors_value
); !iter
.IsAtEnd();
1050 const base::ListValue
* color_list
;
1051 if (iter
.value().GetAsList(&color_list
) &&
1052 ((color_list
->GetSize() == 3) || (color_list
->GetSize() == 4))) {
1053 SkColor color
= SK_ColorWHITE
;
1055 if (color_list
->GetInteger(0, &r
) &&
1056 color_list
->GetInteger(1, &g
) &&
1057 color_list
->GetInteger(2, &b
)) {
1058 if (color_list
->GetSize() == 4) {
1061 if (color_list
->GetDouble(3, &alpha
)) {
1062 color
= SkColorSetARGB(static_cast<int>(alpha
* 255), r
, g
, b
);
1063 } else if (color_list
->GetInteger(3, &alpha_int
) &&
1064 (alpha_int
== 0 || alpha_int
== 1)) {
1065 color
= SkColorSetARGB(alpha_int
? 255 : 0, r
, g
, b
);
1067 // Invalid entry for part 4.
1071 color
= SkColorSetRGB(r
, g
, b
);
1074 int id
= GetIntForString(iter
.key(), kColorTable
, kColorTableLength
);
1076 (*temp_colors
)[id
] = color
;
1083 void BrowserThemePack::GenerateMissingColors(
1084 std::map
<int, SkColor
>* colors
) {
1085 // Generate link colors, if missing. (See GetColor()).
1086 if (!colors
->count(ThemeProperties::COLOR_NTP_HEADER
) &&
1087 colors
->count(ThemeProperties::COLOR_NTP_SECTION
)) {
1088 (*colors
)[ThemeProperties::COLOR_NTP_HEADER
] =
1089 (*colors
)[ThemeProperties::COLOR_NTP_SECTION
];
1092 if (!colors
->count(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
) &&
1093 colors
->count(ThemeProperties::COLOR_NTP_SECTION_LINK
)) {
1094 SkColor color_section_link
=
1095 (*colors
)[ThemeProperties::COLOR_NTP_SECTION_LINK
];
1096 (*colors
)[ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE
] =
1097 SkColorSetA(color_section_link
, SkColorGetA(color_section_link
) / 3);
1100 if (!colors
->count(ThemeProperties::COLOR_NTP_LINK_UNDERLINE
) &&
1101 colors
->count(ThemeProperties::COLOR_NTP_LINK
)) {
1102 SkColor color_link
= (*colors
)[ThemeProperties::COLOR_NTP_LINK
];
1103 (*colors
)[ThemeProperties::COLOR_NTP_LINK_UNDERLINE
] =
1104 SkColorSetA(color_link
, SkColorGetA(color_link
) / 3);
1107 // Generate frame colors, if missing. (See GenerateFrameColors()).
1109 std::map
<int, SkColor
>::const_iterator it
=
1110 colors
->find(ThemeProperties::COLOR_FRAME
);
1111 if (it
!= colors
->end()) {
1114 frame
= ThemeProperties::GetDefaultColor(
1115 ThemeProperties::COLOR_FRAME
);
1118 if (!colors
->count(ThemeProperties::COLOR_FRAME
)) {
1119 (*colors
)[ThemeProperties::COLOR_FRAME
] =
1120 HSLShift(frame
, GetTintInternal(ThemeProperties::TINT_FRAME
));
1122 if (!colors
->count(ThemeProperties::COLOR_FRAME_INACTIVE
)) {
1123 (*colors
)[ThemeProperties::COLOR_FRAME_INACTIVE
] =
1124 HSLShift(frame
, GetTintInternal(
1125 ThemeProperties::TINT_FRAME_INACTIVE
));
1127 if (!colors
->count(ThemeProperties::COLOR_FRAME_INCOGNITO
)) {
1128 (*colors
)[ThemeProperties::COLOR_FRAME_INCOGNITO
] =
1129 HSLShift(frame
, GetTintInternal(
1130 ThemeProperties::TINT_FRAME_INCOGNITO
));
1132 if (!colors
->count(ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
)) {
1133 (*colors
)[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE
] =
1134 HSLShift(frame
, GetTintInternal(
1135 ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE
));
1139 void BrowserThemePack::BuildDisplayPropertiesFromJSON(
1140 const base::DictionaryValue
* display_properties_value
) {
1141 display_properties_
= new DisplayPropertyPair
[kDisplayPropertiesSize
];
1142 for (size_t i
= 0; i
< kDisplayPropertiesSize
; ++i
) {
1143 display_properties_
[i
].id
= -1;
1144 display_properties_
[i
].property
= 0;
1147 if (!display_properties_value
)
1150 std::map
<int, int> temp_properties
;
1151 for (base::DictionaryValue::Iterator
iter(*display_properties_value
);
1152 !iter
.IsAtEnd(); iter
.Advance()) {
1153 int property_id
= GetIntForString(iter
.key(), kDisplayProperties
,
1154 kDisplayPropertiesSize
);
1155 switch (property_id
) {
1156 case ThemeProperties::NTP_BACKGROUND_ALIGNMENT
: {
1158 if (iter
.value().GetAsString(&val
)) {
1159 temp_properties
[ThemeProperties::NTP_BACKGROUND_ALIGNMENT
] =
1160 ThemeProperties::StringToAlignment(val
);
1164 case ThemeProperties::NTP_BACKGROUND_TILING
: {
1166 if (iter
.value().GetAsString(&val
)) {
1167 temp_properties
[ThemeProperties::NTP_BACKGROUND_TILING
] =
1168 ThemeProperties::StringToTiling(val
);
1172 case ThemeProperties::NTP_LOGO_ALTERNATE
: {
1174 if (iter
.value().GetAsInteger(&val
))
1175 temp_properties
[ThemeProperties::NTP_LOGO_ALTERNATE
] = val
;
1181 // Copy data from the intermediary data structure to the array.
1183 for (std::map
<int, int>::const_iterator it
= temp_properties
.begin();
1184 it
!= temp_properties
.end() && count
< kDisplayPropertiesSize
;
1186 display_properties_
[count
].id
= it
->first
;
1187 display_properties_
[count
].property
= it
->second
;
1191 void BrowserThemePack::ParseImageNamesFromJSON(
1192 const base::DictionaryValue
* images_value
,
1193 const base::FilePath
& images_path
,
1194 FilePathMap
* file_paths
) const {
1198 for (base::DictionaryValue::Iterator
iter(*images_value
); !iter
.IsAtEnd();
1200 if (iter
.value().IsType(base::Value::TYPE_DICTIONARY
)) {
1201 const base::DictionaryValue
* inner_value
= NULL
;
1202 if (iter
.value().GetAsDictionary(&inner_value
)) {
1203 for (base::DictionaryValue::Iterator
inner_iter(*inner_value
);
1204 !inner_iter
.IsAtEnd();
1205 inner_iter
.Advance()) {
1207 ui::ScaleFactor scale_factor
= ui::SCALE_FACTOR_NONE
;
1208 if (GetScaleFactorFromManifestKey(inner_iter
.key(), &scale_factor
) &&
1209 inner_iter
.value().IsType(base::Value::TYPE_STRING
) &&
1210 inner_iter
.value().GetAsString(&name
)) {
1211 AddFileAtScaleToMap(iter
.key(),
1213 images_path
.AppendASCII(name
),
1218 } else if (iter
.value().IsType(base::Value::TYPE_STRING
)) {
1220 if (iter
.value().GetAsString(&name
)) {
1221 AddFileAtScaleToMap(iter
.key(),
1222 ui::SCALE_FACTOR_100P
,
1223 images_path
.AppendASCII(name
),
1230 void BrowserThemePack::AddFileAtScaleToMap(const std::string
& image_name
,
1231 ui::ScaleFactor scale_factor
,
1232 const base::FilePath
& image_path
,
1233 FilePathMap
* file_paths
) const {
1234 int id
= GetPersistentIDByName(image_name
);
1236 (*file_paths
)[id
][scale_factor
] = image_path
;
1237 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1238 id
= GetPersistentIDByNameHelper(image_name
,
1239 kPersistingImagesDesktopAura
,
1240 kPersistingImagesDesktopAuraLength
);
1242 (*file_paths
)[id
][scale_factor
] = image_path
;
1246 void BrowserThemePack::BuildSourceImagesArray(const FilePathMap
& file_paths
) {
1247 std::vector
<int> ids
;
1248 for (FilePathMap::const_iterator it
= file_paths
.begin();
1249 it
!= file_paths
.end(); ++it
) {
1250 ids
.push_back(it
->first
);
1253 source_images_
= new int[ids
.size() + 1];
1254 std::copy(ids
.begin(), ids
.end(), source_images_
);
1255 source_images_
[ids
.size()] = -1;
1258 bool BrowserThemePack::LoadRawBitmapsTo(
1259 const FilePathMap
& file_paths
,
1260 ImageCache
* image_cache
) {
1261 // Themes should be loaded on the file thread, not the UI thread.
1262 // http://crbug.com/61838
1263 base::ThreadRestrictions::ScopedAllowIO allow_io
;
1265 for (FilePathMap::const_iterator it
= file_paths
.begin();
1266 it
!= file_paths
.end(); ++it
) {
1267 int prs_id
= it
->first
;
1268 // Some images need to go directly into |image_memory_|. No modification is
1269 // necessary or desirable.
1270 bool is_copyable
= false;
1271 for (size_t i
= 0; i
< arraysize(kPreloadIDs
); ++i
) {
1272 if (kPreloadIDs
[i
] == prs_id
) {
1277 gfx::ImageSkia image_skia
;
1278 for (int pass
= 0; pass
< 2; ++pass
) {
1279 // Two passes: In the first pass, we process only scale factor
1280 // 100% and in the second pass all other scale factors. We
1281 // process scale factor 100% first because the first image added
1282 // in image_skia.AddRepresentation() determines the DIP size for
1283 // all representations.
1284 for (ScaleFactorToFileMap::const_iterator s2f
= it
->second
.begin();
1285 s2f
!= it
->second
.end(); ++s2f
) {
1286 ui::ScaleFactor scale_factor
= s2f
->first
;
1287 if ((pass
== 0 && scale_factor
!= ui::SCALE_FACTOR_100P
) ||
1288 (pass
== 1 && scale_factor
== ui::SCALE_FACTOR_100P
)) {
1291 scoped_refptr
<base::RefCountedMemory
> raw_data(
1292 ReadFileData(s2f
->second
));
1293 if (!raw_data
.get() || !raw_data
->size()) {
1294 LOG(ERROR
) << "Could not load theme image"
1295 << " prs_id=" << prs_id
1296 << " scale_factor_enum=" << scale_factor
1297 << " file=" << s2f
->second
.value()
1298 << (raw_data
.get() ? " (zero size)" : " (read error)");
1302 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factor
);
1303 image_memory_
[raw_id
] = raw_data
;
1306 if (gfx::PNGCodec::Decode(raw_data
->front(), raw_data
->size(),
1308 image_skia
.AddRepresentation(
1309 gfx::ImageSkiaRep(bitmap
,
1310 ui::GetScaleForScaleFactor(scale_factor
)));
1312 NOTREACHED() << "Unable to decode theme image resource "
1318 if (!is_copyable
&& !image_skia
.isNull())
1319 (*image_cache
)[prs_id
] = gfx::Image(image_skia
);
1325 void BrowserThemePack::CreateImages(ImageCache
* images
) const {
1327 CreateFrameImages(images
);
1328 CreateTintedButtons(GetTintInternal(ThemeProperties::TINT_BUTTONS
), images
);
1329 CreateTabBackgroundImages(images
);
1332 void BrowserThemePack::CropImages(ImageCache
* images
) const {
1333 bool has_frame_border
= HasFrameBorder();
1334 for (size_t i
= 0; i
< arraysize(kImagesToCrop
); ++i
) {
1335 if (has_frame_border
&& kImagesToCrop
[i
].skip_if_frame_border
)
1338 int prs_id
= kImagesToCrop
[i
].prs_id
;
1339 ImageCache::iterator it
= images
->find(prs_id
);
1340 if (it
== images
->end())
1343 int crop_height
= kImagesToCrop
[i
].max_height
;
1344 gfx::ImageSkia image_skia
= it
->second
.AsImageSkia();
1345 (*images
)[prs_id
] = gfx::Image(gfx::ImageSkiaOperations::ExtractSubset(
1346 image_skia
, gfx::Rect(0, 0, image_skia
.width(), crop_height
)));
1350 void BrowserThemePack::CreateFrameImages(ImageCache
* images
) const {
1351 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
1353 // Create all the output images in a separate cache and move them back into
1354 // the input images because there can be name collisions.
1355 ImageCache temp_output
;
1357 for (size_t i
= 0; i
< arraysize(kFrameTintMap
); ++i
) {
1358 int prs_id
= kFrameTintMap
[i
].key
;
1360 // If there's no frame image provided for the specified id, then load
1361 // the default provided frame. If that's not provided, skip this whole
1362 // thing and just use the default images.
1363 int prs_base_id
= 0;
1365 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1366 if (prs_id
== PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
) {
1367 prs_base_id
= images
->count(PRS_THEME_FRAME_INCOGNITO_DESKTOP
) ?
1368 PRS_THEME_FRAME_INCOGNITO_DESKTOP
: PRS_THEME_FRAME_DESKTOP
;
1369 } else if (prs_id
== PRS_THEME_FRAME_INACTIVE_DESKTOP
) {
1370 prs_base_id
= PRS_THEME_FRAME_DESKTOP
;
1371 } else if (prs_id
== PRS_THEME_FRAME_INCOGNITO_DESKTOP
&&
1372 !images
->count(PRS_THEME_FRAME_INCOGNITO_DESKTOP
)) {
1373 prs_base_id
= PRS_THEME_FRAME_DESKTOP
;
1377 if (prs_id
== PRS_THEME_FRAME_INCOGNITO_INACTIVE
) {
1378 prs_base_id
= images
->count(PRS_THEME_FRAME_INCOGNITO
) ?
1379 PRS_THEME_FRAME_INCOGNITO
: PRS_THEME_FRAME
;
1380 } else if (prs_id
== PRS_THEME_FRAME_OVERLAY_INACTIVE
) {
1381 prs_base_id
= PRS_THEME_FRAME_OVERLAY
;
1382 } else if (prs_id
== PRS_THEME_FRAME_INACTIVE
) {
1383 prs_base_id
= PRS_THEME_FRAME
;
1384 } else if (prs_id
== PRS_THEME_FRAME_INCOGNITO
&&
1385 !images
->count(PRS_THEME_FRAME_INCOGNITO
)) {
1386 prs_base_id
= PRS_THEME_FRAME
;
1388 prs_base_id
= prs_id
;
1391 if (images
->count(prs_id
)) {
1392 frame
= (*images
)[prs_id
];
1393 } else if (prs_base_id
!= prs_id
&& images
->count(prs_base_id
)) {
1394 frame
= (*images
)[prs_base_id
];
1395 } else if (prs_base_id
== PRS_THEME_FRAME_OVERLAY
) {
1396 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1397 if (images
->count(PRS_THEME_FRAME_DESKTOP
)) {
1399 if (images
->count(PRS_THEME_FRAME
)) {
1401 // If there is no theme overlay, don't tint the default frame,
1402 // because it will overwrite the custom frame image when we cache and
1403 // reload from disk.
1404 frame
= gfx::Image();
1407 // If the theme doesn't specify an image, then apply the tint to
1408 // the default frame.
1409 frame
= rb
.GetImageNamed(IDR_THEME_FRAME
);
1410 #if defined(USE_ASH) && !defined(OS_CHROMEOS)
1411 if (prs_id
>= PRS_THEME_FRAME_DESKTOP
&&
1412 prs_id
<= PRS_THEME_FRAME_INCOGNITO_INACTIVE_DESKTOP
) {
1413 frame
= rb
.GetImageNamed(IDR_THEME_FRAME_DESKTOP
);
1417 if (!frame
.IsEmpty()) {
1418 temp_output
[prs_id
] = CreateHSLShiftedImage(
1419 frame
, GetTintInternal(kFrameTintMap
[i
].value
));
1422 MergeImageCaches(temp_output
, images
);
1425 void BrowserThemePack::CreateTintedButtons(
1426 const color_utils::HSL
& button_tint
,
1427 ImageCache
* processed_images
) const {
1428 if (button_tint
.h
!= -1 || button_tint
.s
!= -1 || button_tint
.l
!= -1) {
1429 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
1430 const std::set
<int>& idr_ids
=
1431 ThemeProperties::GetTintableToolbarButtons();
1432 for (std::set
<int>::const_iterator it
= idr_ids
.begin();
1433 it
!= idr_ids
.end(); ++it
) {
1434 int prs_id
= GetPersistentIDByIDR(*it
);
1437 // Fetch the image by IDR...
1438 gfx::Image
& button
= rb
.GetImageNamed(*it
);
1440 // but save a version with the persistent ID.
1441 (*processed_images
)[prs_id
] =
1442 CreateHSLShiftedImage(button
, button_tint
);
1447 void BrowserThemePack::CreateTabBackgroundImages(ImageCache
* images
) const {
1448 ImageCache temp_output
;
1449 for (size_t i
= 0; i
< arraysize(kTabBackgroundMap
); ++i
) {
1450 int prs_id
= kTabBackgroundMap
[i
].key
;
1451 int prs_base_id
= kTabBackgroundMap
[i
].value
;
1453 // We only need to generate the background tab images if we were provided
1454 // with a PRS_THEME_FRAME.
1455 ImageCache::const_iterator it
= images
->find(prs_base_id
);
1456 if (it
!= images
->end()) {
1457 gfx::ImageSkia image_to_tint
= (it
->second
).AsImageSkia();
1458 color_utils::HSL hsl_shift
= GetTintInternal(
1459 ThemeProperties::TINT_BACKGROUND_TAB
);
1460 int vertical_offset
= images
->count(prs_id
)
1461 ? kRestoredTabVerticalOffset
: 0;
1463 gfx::ImageSkia overlay
;
1464 ImageCache::const_iterator overlay_it
= images
->find(prs_id
);
1465 if (overlay_it
!= images
->end())
1466 overlay
= overlay_it
->second
.AsImageSkia();
1468 gfx::ImageSkiaSource
* source
= new TabBackgroundImageSource(
1469 image_to_tint
, overlay
, hsl_shift
, vertical_offset
);
1470 // ImageSkia takes ownership of |source|.
1471 temp_output
[prs_id
] = gfx::Image(gfx::ImageSkia(source
,
1472 image_to_tint
.size()));
1475 MergeImageCaches(temp_output
, images
);
1478 void BrowserThemePack::RepackImages(const ImageCache
& images
,
1479 RawImages
* reencoded_images
) const {
1480 for (ImageCache::const_iterator it
= images
.begin();
1481 it
!= images
.end(); ++it
) {
1482 gfx::ImageSkia image_skia
= *it
->second
.ToImageSkia();
1484 typedef std::vector
<gfx::ImageSkiaRep
> ImageSkiaReps
;
1485 ImageSkiaReps image_reps
= image_skia
.image_reps();
1486 if (image_reps
.empty()) {
1487 NOTREACHED() << "No image reps for resource " << it
->first
<< ".";
1489 for (ImageSkiaReps::iterator rep_it
= image_reps
.begin();
1490 rep_it
!= image_reps
.end(); ++rep_it
) {
1491 std::vector
<unsigned char> bitmap_data
;
1492 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it
->sk_bitmap(), false,
1494 NOTREACHED() << "Image file for resource " << it
->first
1495 << " could not be encoded.";
1497 int raw_id
= GetRawIDByPersistentID(
1499 ui::GetSupportedScaleFactor(rep_it
->scale()));
1500 (*reencoded_images
)[raw_id
] =
1501 base::RefCountedBytes::TakeVector(&bitmap_data
);
1506 void BrowserThemePack::MergeImageCaches(
1507 const ImageCache
& source
, ImageCache
* destination
) const {
1508 for (ImageCache::const_iterator it
= source
.begin(); it
!= source
.end();
1510 (*destination
)[it
->first
] = it
->second
;
1514 void BrowserThemePack::AddRawImagesTo(const RawImages
& images
,
1515 RawDataForWriting
* out
) const {
1516 for (RawImages::const_iterator it
= images
.begin(); it
!= images
.end();
1518 (*out
)[it
->first
] = base::StringPiece(
1519 it
->second
->front_as
<char>(), it
->second
->size());
1523 color_utils::HSL
BrowserThemePack::GetTintInternal(int id
) const {
1525 for (size_t i
= 0; i
< kTintTableLength
; ++i
) {
1526 if (tints_
[i
].id
== id
) {
1527 color_utils::HSL hsl
;
1528 hsl
.h
= tints_
[i
].h
;
1529 hsl
.s
= tints_
[i
].s
;
1530 hsl
.l
= tints_
[i
].l
;
1536 return ThemeProperties::GetDefaultTint(id
);
1539 int BrowserThemePack::GetRawIDByPersistentID(
1541 ui::ScaleFactor scale_factor
) const {
1545 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1546 if (scale_factors_
[i
] == scale_factor
)
1547 return ((GetMaxPersistentId() + 1) * i
) + prs_id
;
1552 bool BrowserThemePack::GetScaleFactorFromManifestKey(
1553 const std::string
& key
,
1554 ui::ScaleFactor
* scale_factor
) const {
1556 if (base::StringToInt(key
, &percent
)) {
1557 float scale
= static_cast<float>(percent
) / 100.0f
;
1558 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1559 if (fabs(ui::GetScaleForScaleFactor(scale_factors_
[i
]) - scale
)
1561 *scale_factor
= scale_factors_
[i
];
1569 void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id
) {
1570 // Compute (by scaling) bitmaps for |prs_id| for any scale factors
1571 // for which the theme author did not provide a bitmap. We compute
1572 // the bitmaps using the highest scale factor that theme author
1574 // Note: We use only supported scale factors. For example, if scale
1575 // factor 2x is supported by the current system, but 1.8x is not and
1576 // if the theme author did not provide an image for 2x but one for
1577 // 1.8x, we will not use the 1.8x image here. Here we will only use
1578 // images provided for scale factors supported by the current system.
1580 // See if any image is missing. If not, we're done.
1581 bool image_missing
= false;
1582 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1583 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1584 if (image_memory_
.find(raw_id
) == image_memory_
.end()) {
1585 image_missing
= true;
1592 // Find available scale factor with highest scale.
1593 ui::ScaleFactor available_scale_factor
= ui::SCALE_FACTOR_NONE
;
1594 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1595 int raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1596 if ((available_scale_factor
== ui::SCALE_FACTOR_NONE
||
1597 (ui::GetScaleForScaleFactor(scale_factors_
[i
]) >
1598 ui::GetScaleForScaleFactor(available_scale_factor
))) &&
1599 image_memory_
.find(raw_id
) != image_memory_
.end()) {
1600 available_scale_factor
= scale_factors_
[i
];
1603 // If no scale factor is available, we're done.
1604 if (available_scale_factor
== ui::SCALE_FACTOR_NONE
)
1607 // Get bitmap for the available scale factor.
1608 int available_raw_id
= GetRawIDByPersistentID(prs_id
, available_scale_factor
);
1609 RawImages::const_iterator it
= image_memory_
.find(available_raw_id
);
1610 SkBitmap available_bitmap
;
1611 if (!gfx::PNGCodec::Decode(it
->second
->front(),
1613 &available_bitmap
)) {
1614 NOTREACHED() << "Unable to decode theme image for prs_id="
1615 << prs_id
<< " for scale_factor=" << available_scale_factor
;
1619 // Fill in all missing scale factors by scaling the available bitmap.
1620 for (size_t i
= 0; i
< scale_factors_
.size(); ++i
) {
1621 int scaled_raw_id
= GetRawIDByPersistentID(prs_id
, scale_factors_
[i
]);
1622 if (image_memory_
.find(scaled_raw_id
) != image_memory_
.end())
1624 SkBitmap scaled_bitmap
=
1625 CreateLowQualityResizedBitmap(available_bitmap
,
1626 available_scale_factor
,
1628 std::vector
<unsigned char> bitmap_data
;
1629 if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap
,
1632 NOTREACHED() << "Unable to encode theme image for prs_id="
1633 << prs_id
<< " for scale_factor=" << scale_factors_
[i
];
1636 image_memory_
[scaled_raw_id
] =
1637 base::RefCountedBytes::TakeVector(&bitmap_data
);