browser_theme_pack.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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.
4
5#include "chrome/browser/themes/browser_theme_pack.h"
6
7#include <limits>
8
9#include "base/memory/ref_counted_memory.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/stl_util.h"
12#include "base/strings/string_number_conversions.h"
13#include "base/strings/string_util.h"
14#include "base/strings/utf_string_conversions.h"
15#include "base/threading/sequenced_worker_pool.h"
16#include "base/threading/thread_restrictions.h"
17#include "base/values.h"
18#include "chrome/browser/themes/theme_properties.h"
19#include "chrome/common/extensions/manifest_handlers/theme_handler.h"
20#include "content/public/browser/browser_thread.h"
21#include "extensions/common/id_util.h"
22#include "grit/theme_resources.h"
23#include "grit/ui_resources.h"
24#include "net/base/file_stream.h"
25#include "net/base/net_errors.h"
26#include "third_party/skia/include/core/SkCanvas.h"
27#include "ui/base/resource/data_pack.h"
28#include "ui/base/resource/resource_bundle.h"
29#include "ui/gfx/canvas.h"
30#include "ui/gfx/codec/png_codec.h"
31#include "ui/gfx/image/canvas_image_source.h"
32#include "ui/gfx/image/image.h"
33#include "ui/gfx/image/image_skia.h"
34#include "ui/gfx/image/image_skia_operations.h"
35#include "ui/gfx/screen.h"
36#include "ui/gfx/size_conversions.h"
37#include "ui/gfx/skia_util.h"
38
39using content::BrowserThread;
40using extensions::Extension;
41
42namespace {
43
44// Version number of the current theme pack. We just throw out and rebuild
45// theme packs that aren't int-equal to this. Increment this number if you
46// change default theme assets.
47const int kThemePackVersion = 31;
48
49// IDs that are in the DataPack won't clash with the positive integer
50// uint16. kHeaderID should always have the maximum value because we want the
51// "header" to be written last. That way we can detect whether the pack was
52// successfully written and ignore and regenerate if it was only partially
53// written (i.e. chrome crashed on a different thread while writing the pack).
54const int kMaxID = 0x0000FFFF;  // Max unsigned 16-bit int.
55const int kHeaderID = kMaxID - 1;
56const int kTintsID = kMaxID - 2;
57const int kColorsID = kMaxID - 3;
58const int kDisplayPropertiesID = kMaxID - 4;
59const int kSourceImagesID = kMaxID - 5;
60const int kScaleFactorsID = kMaxID - 6;
61
62// The sum of kFrameBorderThickness and kNonClientRestoredExtraThickness from
63// OpaqueBrowserFrameView.
64const int kRestoredTabVerticalOffset = 15;
65
66// Persistent constants for the main images that we need. These have the same
67// names as their IDR_* counterparts but these values will always stay the
68// same.
69const int PRS_THEME_FRAME = 1;
70const int PRS_THEME_FRAME_INACTIVE = 2;
71const int PRS_THEME_FRAME_INCOGNITO = 3;
72const int PRS_THEME_FRAME_INCOGNITO_INACTIVE = 4;
73const int PRS_THEME_TOOLBAR = 5;
74const int PRS_THEME_TAB_BACKGROUND = 6;
75const int PRS_THEME_TAB_BACKGROUND_INCOGNITO = 7;
76const int PRS_THEME_TAB_BACKGROUND_V = 8;
77const int PRS_THEME_NTP_BACKGROUND = 9;
78const int PRS_THEME_FRAME_OVERLAY = 10;
79const int PRS_THEME_FRAME_OVERLAY_INACTIVE = 11;
80const int PRS_THEME_BUTTON_BACKGROUND = 12;
81const int PRS_THEME_NTP_ATTRIBUTION = 13;
82const int PRS_THEME_WINDOW_CONTROL_BACKGROUND = 14;
83
84struct PersistingImagesTable {
85  // A non-changing integer ID meant to be saved in theme packs. This ID must
86  // not change between versions of chrome.
87  int persistent_id;
88
89  // The IDR that depends on the whims of GRIT and therefore changes whenever
90  // someone adds a new resource.
91  int idr_id;
92
93  // String to check for when parsing theme manifests or NULL if this isn't
94  // supposed to be changeable by the user.
95  const char* key;
96};
97
98// IDR_* resource names change whenever new resources are added; use persistent
99// IDs when storing to a cached pack.
100PersistingImagesTable kPersistingImages[] = {
101  { PRS_THEME_FRAME, IDR_THEME_FRAME,
102    "theme_frame" },
103  { PRS_THEME_FRAME_INACTIVE, IDR_THEME_FRAME_INACTIVE,
104    "theme_frame_inactive" },
105  { PRS_THEME_FRAME_INCOGNITO, IDR_THEME_FRAME_INCOGNITO,
106    "theme_frame_incognito" },
107  { PRS_THEME_FRAME_INCOGNITO_INACTIVE, IDR_THEME_FRAME_INCOGNITO_INACTIVE,
108    "theme_frame_incognito_inactive" },
109  { PRS_THEME_TOOLBAR, IDR_THEME_TOOLBAR,
110    "theme_toolbar" },
111  { PRS_THEME_TAB_BACKGROUND, IDR_THEME_TAB_BACKGROUND,
112    "theme_tab_background" },
113  { PRS_THEME_TAB_BACKGROUND_INCOGNITO, IDR_THEME_TAB_BACKGROUND_INCOGNITO,
114    "theme_tab_background_incognito" },
115  { PRS_THEME_TAB_BACKGROUND_V, IDR_THEME_TAB_BACKGROUND_V,
116    "theme_tab_background_v"},
117  { PRS_THEME_NTP_BACKGROUND, IDR_THEME_NTP_BACKGROUND,
118    "theme_ntp_background" },
119  { PRS_THEME_FRAME_OVERLAY, IDR_THEME_FRAME_OVERLAY,
120    "theme_frame_overlay" },
121  { PRS_THEME_FRAME_OVERLAY_INACTIVE, IDR_THEME_FRAME_OVERLAY_INACTIVE,
122    "theme_frame_overlay_inactive" },
123  { PRS_THEME_BUTTON_BACKGROUND, IDR_THEME_BUTTON_BACKGROUND,
124    "theme_button_background" },
125  { PRS_THEME_NTP_ATTRIBUTION, IDR_THEME_NTP_ATTRIBUTION,
126    "theme_ntp_attribution" },
127  { PRS_THEME_WINDOW_CONTROL_BACKGROUND, IDR_THEME_WINDOW_CONTROL_BACKGROUND,
128    "theme_window_control_background"},
129
130  // The rest of these entries have no key because they can't be overridden
131  // from the json manifest.
132  { 15, IDR_BACK, NULL },
133  { 16, IDR_BACK_D, NULL },
134  { 17, IDR_BACK_H, NULL },
135  { 18, IDR_BACK_P, NULL },
136  { 19, IDR_FORWARD, NULL },
137  { 20, IDR_FORWARD_D, NULL },
138  { 21, IDR_FORWARD_H, NULL },
139  { 22, IDR_FORWARD_P, NULL },
140  { 23, IDR_HOME, NULL },
141  { 24, IDR_HOME_H, NULL },
142  { 25, IDR_HOME_P, NULL },
143  { 26, IDR_RELOAD, NULL },
144  { 27, IDR_RELOAD_H, NULL },
145  { 28, IDR_RELOAD_P, NULL },
146  { 29, IDR_STOP, NULL },
147  { 30, IDR_STOP_D, NULL },
148  { 31, IDR_STOP_H, NULL },
149  { 32, IDR_STOP_P, NULL },
150  { 33, IDR_BROWSER_ACTIONS_OVERFLOW, NULL },
151  { 34, IDR_BROWSER_ACTIONS_OVERFLOW_H, NULL },
152  { 35, IDR_BROWSER_ACTIONS_OVERFLOW_P, NULL },
153  { 36, IDR_TOOLS, NULL },
154  { 37, IDR_TOOLS_H, NULL },
155  { 38, IDR_TOOLS_P, NULL },
156  { 39, IDR_MENU_DROPARROW, NULL },
157  { 40, IDR_THROBBER, NULL },
158  { 41, IDR_THROBBER_WAITING, NULL },
159  { 42, IDR_THROBBER_LIGHT, NULL },
160  { 43, IDR_TOOLBAR_BEZEL_HOVER, NULL },
161  { 44, IDR_TOOLBAR_BEZEL_PRESSED, NULL },
162  { 45, IDR_TOOLS_BAR, NULL },
163};
164const size_t kPersistingImagesLength = arraysize(kPersistingImages);
165
166#if defined(OS_WIN) && defined(USE_AURA)
167// Persistent theme ids for Windows AURA.
168const int PRS_THEME_FRAME_WIN = 100;
169const int PRS_THEME_FRAME_INACTIVE_WIN = 101;
170const int PRS_THEME_FRAME_INCOGNITO_WIN = 102;
171const int PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN = 103;
172const int PRS_THEME_TOOLBAR_WIN = 104;
173const int PRS_THEME_TAB_BACKGROUND_WIN = 105;
174const int PRS_THEME_TAB_BACKGROUND_INCOGNITO_WIN = 106;
175
176// Persistent theme to resource id mapping for Windows AURA.
177PersistingImagesTable kPersistingImagesWinDesktopAura[] = {
178  { PRS_THEME_FRAME_WIN, IDR_THEME_FRAME_WIN,
179    "theme_frame" },
180  { PRS_THEME_FRAME_INACTIVE_WIN, IDR_THEME_FRAME_INACTIVE_WIN,
181    "theme_frame_inactive" },
182  { PRS_THEME_FRAME_INCOGNITO_WIN, IDR_THEME_FRAME_INCOGNITO_WIN,
183    "theme_frame_incognito" },
184  { PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN,
185    IDR_THEME_FRAME_INCOGNITO_INACTIVE_WIN,
186    "theme_frame_incognito_inactive" },
187  { PRS_THEME_TOOLBAR_WIN, IDR_THEME_TOOLBAR_WIN,
188    "theme_toolbar" },
189  { PRS_THEME_TAB_BACKGROUND_WIN, IDR_THEME_TAB_BACKGROUND_WIN,
190    "theme_tab_background" },
191  { PRS_THEME_TAB_BACKGROUND_INCOGNITO_WIN,
192    IDR_THEME_TAB_BACKGROUND_INCOGNITO_WIN,
193    "theme_tab_background_incognito" },
194};
195const size_t kPersistingImagesWinDesktopAuraLength =
196    arraysize(kPersistingImagesWinDesktopAura);
197#endif
198
199int GetPersistentIDByNameHelper(const std::string& key,
200                                const PersistingImagesTable* image_table,
201                                size_t image_table_size) {
202  for (size_t i = 0; i < image_table_size; ++i) {
203    if (image_table[i].key != NULL &&
204        base::strcasecmp(key.c_str(), image_table[i].key) == 0) {
205      return image_table[i].persistent_id;
206    }
207  }
208  return -1;
209}
210
211int GetPersistentIDByName(const std::string& key) {
212  return GetPersistentIDByNameHelper(key,
213                                     kPersistingImages,
214                                     kPersistingImagesLength);
215}
216
217int GetPersistentIDByIDR(int idr) {
218  static std::map<int,int>* lookup_table = new std::map<int,int>();
219  if (lookup_table->empty()) {
220    for (size_t i = 0; i < kPersistingImagesLength; ++i) {
221      int idr = kPersistingImages[i].idr_id;
222      int prs_id = kPersistingImages[i].persistent_id;
223      (*lookup_table)[idr] = prs_id;
224    }
225#if defined(OS_WIN) && defined(USE_AURA)
226    for (size_t i = 0; i < kPersistingImagesWinDesktopAuraLength; ++i) {
227      int idr = kPersistingImagesWinDesktopAura[i].idr_id;
228      int prs_id = kPersistingImagesWinDesktopAura[i].persistent_id;
229      (*lookup_table)[idr] = prs_id;
230    }
231#endif
232  }
233  std::map<int,int>::iterator it = lookup_table->find(idr);
234  return (it == lookup_table->end()) ? -1 : it->second;
235}
236
237// Returns true if the scales in |input| match those in |expected|.
238// The order must match as the index is used in determining the raw id.
239bool InputScalesValid(const base::StringPiece& input,
240                      const std::vector<ui::ScaleFactor>& expected) {
241  size_t scales_size = static_cast<size_t>(input.size() / sizeof(float));
242  if (scales_size != expected.size())
243    return false;
244  scoped_ptr<float[]> scales(new float[scales_size]);
245  // Do a memcpy to avoid misaligned memory access.
246  memcpy(scales.get(), input.data(), input.size());
247  for (size_t index = 0; index < scales_size; ++index) {
248    if (scales[index] != ui::GetScaleFactorScale(expected[index]))
249      return false;
250  }
251  return true;
252}
253
254// Returns |scale_factors| as a string to be written to disk.
255std::string GetScaleFactorsAsString(
256    const std::vector<ui::ScaleFactor>& scale_factors) {
257  scoped_ptr<float[]> scales(new float[scale_factors.size()]);
258  for (size_t i = 0; i < scale_factors.size(); ++i)
259    scales[i] = ui::GetScaleFactorScale(scale_factors[i]);
260  std::string out_string = std::string(
261      reinterpret_cast<const char*>(scales.get()),
262      scale_factors.size() * sizeof(float));
263  return out_string;
264}
265
266struct StringToIntTable {
267  const char* key;
268  ThemeProperties::OverwritableByUserThemeProperty id;
269};
270
271// Strings used by themes to identify tints in the JSON.
272StringToIntTable kTintTable[] = {
273  { "buttons", ThemeProperties::TINT_BUTTONS },
274  { "frame", ThemeProperties::TINT_FRAME },
275  { "frame_inactive", ThemeProperties::TINT_FRAME_INACTIVE },
276  { "frame_incognito", ThemeProperties::TINT_FRAME_INCOGNITO },
277  { "frame_incognito_inactive",
278    ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE },
279  { "background_tab", ThemeProperties::TINT_BACKGROUND_TAB },
280};
281const size_t kTintTableLength = arraysize(kTintTable);
282
283// Strings used by themes to identify colors in the JSON.
284StringToIntTable kColorTable[] = {
285  { "frame", ThemeProperties::COLOR_FRAME },
286  { "frame_inactive", ThemeProperties::COLOR_FRAME_INACTIVE },
287  { "frame_incognito", ThemeProperties::COLOR_FRAME_INCOGNITO },
288  { "frame_incognito_inactive",
289    ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE },
290  { "toolbar", ThemeProperties::COLOR_TOOLBAR },
291  { "tab_text", ThemeProperties::COLOR_TAB_TEXT },
292  { "tab_background_text", ThemeProperties::COLOR_BACKGROUND_TAB_TEXT },
293  { "bookmark_text", ThemeProperties::COLOR_BOOKMARK_TEXT },
294  { "ntp_background", ThemeProperties::COLOR_NTP_BACKGROUND },
295  { "ntp_text", ThemeProperties::COLOR_NTP_TEXT },
296  { "ntp_link", ThemeProperties::COLOR_NTP_LINK },
297  { "ntp_link_underline", ThemeProperties::COLOR_NTP_LINK_UNDERLINE },
298  { "ntp_header", ThemeProperties::COLOR_NTP_HEADER },
299  { "ntp_section", ThemeProperties::COLOR_NTP_SECTION },
300  { "ntp_section_text", ThemeProperties::COLOR_NTP_SECTION_TEXT },
301  { "ntp_section_link", ThemeProperties::COLOR_NTP_SECTION_LINK },
302  { "ntp_section_link_underline",
303    ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE },
304  { "button_background", ThemeProperties::COLOR_BUTTON_BACKGROUND },
305};
306const size_t kColorTableLength = arraysize(kColorTable);
307
308// Strings used by themes to identify display properties keys in JSON.
309StringToIntTable kDisplayProperties[] = {
310  { "ntp_background_alignment",
311    ThemeProperties::NTP_BACKGROUND_ALIGNMENT },
312  { "ntp_background_repeat", ThemeProperties::NTP_BACKGROUND_TILING },
313  { "ntp_logo_alternate", ThemeProperties::NTP_LOGO_ALTERNATE },
314};
315const size_t kDisplayPropertiesSize = arraysize(kDisplayProperties);
316
317int GetIntForString(const std::string& key,
318                    StringToIntTable* table,
319                    size_t table_length) {
320  for (size_t i = 0; i < table_length; ++i) {
321    if (base::strcasecmp(key.c_str(), table[i].key) == 0) {
322      return table[i].id;
323    }
324  }
325
326  return -1;
327}
328
329struct IntToIntTable {
330  int key;
331  int value;
332};
333
334// Mapping used in CreateFrameImages() to associate frame images with the
335// tint ID that should maybe be applied to it.
336IntToIntTable kFrameTintMap[] = {
337  { PRS_THEME_FRAME, ThemeProperties::TINT_FRAME },
338  { PRS_THEME_FRAME_INACTIVE, ThemeProperties::TINT_FRAME_INACTIVE },
339  { PRS_THEME_FRAME_OVERLAY, ThemeProperties::TINT_FRAME },
340  { PRS_THEME_FRAME_OVERLAY_INACTIVE,
341    ThemeProperties::TINT_FRAME_INACTIVE },
342  { PRS_THEME_FRAME_INCOGNITO, ThemeProperties::TINT_FRAME_INCOGNITO },
343  { PRS_THEME_FRAME_INCOGNITO_INACTIVE,
344    ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE },
345#if defined(OS_WIN) && defined(USE_AURA)
346  { PRS_THEME_FRAME_WIN, ThemeProperties::TINT_FRAME },
347  { PRS_THEME_FRAME_INACTIVE_WIN, ThemeProperties::TINT_FRAME_INACTIVE },
348  { PRS_THEME_FRAME_INCOGNITO_WIN, ThemeProperties::TINT_FRAME_INCOGNITO },
349  { PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN,
350    ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE },
351#endif
352};
353
354// Mapping used in GenerateTabBackgroundImages() to associate what frame image
355// goes with which tab background.
356IntToIntTable kTabBackgroundMap[] = {
357  { PRS_THEME_TAB_BACKGROUND, PRS_THEME_FRAME },
358  { PRS_THEME_TAB_BACKGROUND_INCOGNITO, PRS_THEME_FRAME_INCOGNITO },
359#if defined(OS_WIN) && defined(USE_AURA)
360  { PRS_THEME_TAB_BACKGROUND_WIN, PRS_THEME_FRAME_WIN },
361  { PRS_THEME_TAB_BACKGROUND_INCOGNITO_WIN, PRS_THEME_FRAME_INCOGNITO_WIN },
362#endif
363};
364
365struct CropEntry {
366  int prs_id;
367
368  // The maximum useful height of the image at |prs_id|.
369  int max_height;
370
371  // Whether cropping the image at |prs_id| should be skipped on OSes which
372  // have a frame border to the left and right of the web contents.
373  // This should be true for images which can be used to decorate the border to
374  // the left and the right of the web contents.
375  bool skip_if_frame_border;
376};
377
378// The images which should be cropped before being saved to the data pack. The
379// maximum heights are meant to be conservative as to give room for the UI to
380// change without the maximum heights having to be modified.
381// |kThemePackVersion| must be incremented if any of the maximum heights below
382// are modified.
383struct CropEntry kImagesToCrop[] = {
384  { PRS_THEME_FRAME, 120, true },
385  { PRS_THEME_FRAME_INACTIVE, 120, true },
386  { PRS_THEME_FRAME_INCOGNITO, 120, true },
387  { PRS_THEME_FRAME_INCOGNITO_INACTIVE, 120, true },
388  { PRS_THEME_FRAME_OVERLAY, 120, true },
389  { PRS_THEME_FRAME_OVERLAY_INACTIVE, 120, true },
390  { PRS_THEME_TOOLBAR, 200, false },
391  { PRS_THEME_BUTTON_BACKGROUND, 60, false },
392  { PRS_THEME_WINDOW_CONTROL_BACKGROUND, 50, false },
393#if defined(OS_WIN) && defined(USE_AURA)
394  { PRS_THEME_TOOLBAR_WIN, 200, false }
395#endif
396};
397
398
399// A list of images that don't need tinting or any other modification and can
400// be byte-copied directly into the finished DataPack. This should contain the
401// persistent IDs for all themeable image IDs that aren't in kFrameTintMap,
402// kTabBackgroundMap or kImagesToCrop.
403const int kPreloadIDs[] = {
404  PRS_THEME_NTP_BACKGROUND,
405  PRS_THEME_NTP_ATTRIBUTION,
406};
407
408// Returns true if this OS uses a browser frame which has a non zero width to
409// the left and the right of the web contents.
410bool HasFrameBorder() {
411#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
412  return false;
413#else
414  return true;
415#endif
416}
417
418// Returns a piece of memory with the contents of the file |path|.
419base::RefCountedMemory* ReadFileData(const base::FilePath& path) {
420  if (!path.empty()) {
421    net::FileStream file(NULL);
422    int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
423    if (file.OpenSync(path, flags) == net::OK) {
424      int64 avail = file.Available();
425      if (avail > 0 && avail < INT_MAX) {
426        size_t size = static_cast<size_t>(avail);
427        std::vector<unsigned char> raw_data;
428        raw_data.resize(size);
429        char* data = reinterpret_cast<char*>(&(raw_data.front()));
430        if (file.ReadUntilComplete(data, size) == avail)
431          return base::RefCountedBytes::TakeVector(&raw_data);
432      }
433    }
434  }
435
436  return NULL;
437}
438
439// Shifts an image's HSL values. The caller is responsible for deleting
440// the returned image.
441gfx::Image CreateHSLShiftedImage(const gfx::Image& image,
442                                 const color_utils::HSL& hsl_shift) {
443  const gfx::ImageSkia* src_image = image.ToImageSkia();
444  return gfx::Image(gfx::ImageSkiaOperations::CreateHSLShiftedImage(
445      *src_image, hsl_shift));
446}
447
448// Computes a bitmap at one scale from a bitmap at a different scale.
449SkBitmap CreateLowQualityResizedBitmap(const SkBitmap& source_bitmap,
450                                       ui::ScaleFactor source_scale_factor,
451                                       ui::ScaleFactor desired_scale_factor) {
452  gfx::Size scaled_size = gfx::ToCeiledSize(
453      gfx::ScaleSize(gfx::Size(source_bitmap.width(),
454                               source_bitmap.height()),
455                     ui::GetScaleFactorScale(desired_scale_factor) /
456                     ui::GetScaleFactorScale(source_scale_factor)));
457  SkBitmap scaled_bitmap;
458  scaled_bitmap.setConfig(SkBitmap::kARGB_8888_Config,
459                          scaled_size.width(),
460                          scaled_size.height());
461  if (!scaled_bitmap.allocPixels())
462    SK_CRASH();
463  scaled_bitmap.eraseARGB(0, 0, 0, 0);
464  SkCanvas canvas(scaled_bitmap);
465  SkRect scaled_bounds = RectToSkRect(gfx::Rect(scaled_size));
466  // Note(oshima): The following scaling code doesn't work with
467  // a mask image.
468  canvas.drawBitmapRect(source_bitmap, NULL, scaled_bounds);
469  return scaled_bitmap;
470}
471
472// A ImageSkiaSource that scales 100P image to the target scale factor
473// if the ImageSkiaRep for the target scale factor isn't available.
474class ThemeImageSource: public gfx::ImageSkiaSource {
475 public:
476  explicit ThemeImageSource(const gfx::ImageSkia& source) : source_(source) {
477  }
478  virtual ~ThemeImageSource() {}
479
480  virtual gfx::ImageSkiaRep GetImageForScale(
481      ui::ScaleFactor scale_factor) OVERRIDE {
482    if (source_.HasRepresentation(scale_factor))
483      return source_.GetRepresentation(scale_factor);
484    const gfx::ImageSkiaRep& rep_100p =
485        source_.GetRepresentation(ui::SCALE_FACTOR_100P);
486    SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
487        rep_100p.sk_bitmap(),
488        ui::SCALE_FACTOR_100P,
489        scale_factor);
490    return gfx::ImageSkiaRep(scaled_bitmap, scale_factor);
491  }
492
493 private:
494  const gfx::ImageSkia source_;
495
496  DISALLOW_COPY_AND_ASSIGN(ThemeImageSource);
497};
498
499// An ImageSkiaSource that delays decoding PNG data into bitmaps until
500// needed. Missing data for a scale factor is computed by scaling data for an
501// available scale factor. Computed bitmaps are stored for future look up.
502class ThemeImagePngSource : public gfx::ImageSkiaSource {
503 public:
504  typedef std::map<ui::ScaleFactor,
505                   scoped_refptr<base::RefCountedMemory> > PngMap;
506
507  explicit ThemeImagePngSource(const PngMap& png_map) : png_map_(png_map) {}
508
509  virtual ~ThemeImagePngSource() {}
510
511 private:
512  virtual gfx::ImageSkiaRep GetImageForScale(
513      ui::ScaleFactor scale_factor) OVERRIDE {
514    // Look up the bitmap for |scale factor| in the bitmap map. If found
515    // return it.
516    BitmapMap::const_iterator exact_bitmap_it = bitmap_map_.find(scale_factor);
517    if (exact_bitmap_it != bitmap_map_.end())
518      return gfx::ImageSkiaRep(exact_bitmap_it->second, scale_factor);
519
520    // Look up the raw PNG data for |scale_factor| in the png map. If found,
521    // decode it, store the result in the bitmap map and return it.
522    PngMap::const_iterator exact_png_it = png_map_.find(scale_factor);
523    if (exact_png_it != png_map_.end()) {
524      SkBitmap bitmap;
525      if (!gfx::PNGCodec::Decode(exact_png_it->second->front(),
526                                 exact_png_it->second->size(),
527                                 &bitmap)) {
528        NOTREACHED();
529        return gfx::ImageSkiaRep();
530      }
531      bitmap_map_[scale_factor] = bitmap;
532      return gfx::ImageSkiaRep(bitmap, scale_factor);
533    }
534
535    // Find an available PNG for another scale factor. We want to use the
536    // highest available scale factor.
537    PngMap::const_iterator available_png_it = png_map_.end();
538    for (PngMap::const_iterator png_it = png_map_.begin();
539         png_it != png_map_.end(); ++png_it) {
540      if (available_png_it == png_map_.end() ||
541          ui::GetScaleFactorScale(png_it->first) >
542          ui::GetScaleFactorScale(available_png_it->first)) {
543        available_png_it = png_it;
544      }
545    }
546    if (available_png_it == png_map_.end())
547      return gfx::ImageSkiaRep();
548    ui::ScaleFactor available_scale_factor = available_png_it->first;
549
550    // Look up the bitmap for |available_scale_factor| in the bitmap map.
551    // If not found, decode the corresponging png data, store the result
552    // in the bitmap map.
553    BitmapMap::const_iterator available_bitmap_it =
554        bitmap_map_.find(available_scale_factor);
555    if (available_bitmap_it == bitmap_map_.end()) {
556      SkBitmap available_bitmap;
557      if (!gfx::PNGCodec::Decode(available_png_it->second->front(),
558                                 available_png_it->second->size(),
559                                 &available_bitmap)) {
560        NOTREACHED();
561        return gfx::ImageSkiaRep();
562      }
563      bitmap_map_[available_scale_factor] = available_bitmap;
564      available_bitmap_it = bitmap_map_.find(available_scale_factor);
565    }
566
567    // Scale the available bitmap to the desired scale factor, store the result
568    // in the bitmap map and return it.
569    SkBitmap scaled_bitmap = CreateLowQualityResizedBitmap(
570        available_bitmap_it->second,
571        available_scale_factor,
572        scale_factor);
573    bitmap_map_[scale_factor] = scaled_bitmap;
574    return gfx::ImageSkiaRep(scaled_bitmap, scale_factor);
575  }
576
577  PngMap png_map_;
578
579  typedef std::map<ui::ScaleFactor, SkBitmap> BitmapMap;
580  BitmapMap bitmap_map_;
581
582  DISALLOW_COPY_AND_ASSIGN(ThemeImagePngSource);
583};
584
585class TabBackgroundImageSource: public gfx::CanvasImageSource {
586 public:
587  TabBackgroundImageSource(const gfx::ImageSkia& image_to_tint,
588                           const gfx::ImageSkia& overlay,
589                           const color_utils::HSL& hsl_shift,
590                           int vertical_offset)
591      : gfx::CanvasImageSource(image_to_tint.size(), false),
592        image_to_tint_(image_to_tint),
593        overlay_(overlay),
594        hsl_shift_(hsl_shift),
595        vertical_offset_(vertical_offset) {
596  }
597
598  virtual ~TabBackgroundImageSource() {
599  }
600
601  // Overridden from CanvasImageSource:
602  virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
603    gfx::ImageSkia bg_tint =
604        gfx::ImageSkiaOperations::CreateHSLShiftedImage(image_to_tint_,
605            hsl_shift_);
606    canvas->TileImageInt(bg_tint, 0, vertical_offset_, 0, 0,
607        size().width(), size().height());
608
609    // If they've provided a custom image, overlay it.
610    if (!overlay_.isNull()) {
611      canvas->TileImageInt(overlay_, 0, 0, size().width(),
612                           overlay_.height());
613    }
614  }
615
616 private:
617  const gfx::ImageSkia image_to_tint_;
618  const gfx::ImageSkia overlay_;
619  const color_utils::HSL hsl_shift_;
620  const int vertical_offset_;
621
622  DISALLOW_COPY_AND_ASSIGN(TabBackgroundImageSource);
623};
624
625}  // namespace
626
627BrowserThemePack::~BrowserThemePack() {
628  if (!data_pack_.get()) {
629    delete header_;
630    delete [] tints_;
631    delete [] colors_;
632    delete [] display_properties_;
633    delete [] source_images_;
634  }
635}
636
637// static
638scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromExtension(
639    const Extension* extension) {
640  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
641  DCHECK(extension);
642  DCHECK(extension->is_theme());
643
644  scoped_refptr<BrowserThemePack> pack(new BrowserThemePack);
645  pack->BuildHeader(extension);
646  pack->BuildTintsFromJSON(extensions::ThemeInfo::GetTints(extension));
647  pack->BuildColorsFromJSON(extensions::ThemeInfo::GetColors(extension));
648  pack->BuildDisplayPropertiesFromJSON(
649      extensions::ThemeInfo::GetDisplayProperties(extension));
650
651  // Builds the images. (Image building is dependent on tints).
652  FilePathMap file_paths;
653  pack->ParseImageNamesFromJSON(
654      extensions::ThemeInfo::GetImages(extension),
655      extension->path(),
656      &file_paths);
657  pack->BuildSourceImagesArray(file_paths);
658
659  if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_on_ui_thread_))
660    return NULL;
661
662  pack->CreateImages(&pack->images_on_ui_thread_);
663
664  // Make sure the |images_on_file_thread_| has bitmaps for supported
665  // scale factors before passing to FILE thread.
666  pack->images_on_file_thread_ = pack->images_on_ui_thread_;
667  for (ImageCache::iterator it = pack->images_on_file_thread_.begin();
668       it != pack->images_on_file_thread_.end(); ++it) {
669    gfx::ImageSkia* image_skia =
670        const_cast<gfx::ImageSkia*>(it->second.ToImageSkia());
671    image_skia->MakeThreadSafe();
672  }
673
674  // Set ThemeImageSource on |images_on_ui_thread_| to resample the source
675  // image if a caller of BrowserThemePack::GetImageNamed() requests an
676  // ImageSkiaRep for a scale factor not specified by the theme author.
677  // Callers of BrowserThemePack::GetImageNamed() to be able to retrieve
678  // ImageSkiaReps for all supported scale factors.
679  for (ImageCache::iterator it = pack->images_on_ui_thread_.begin();
680       it != pack->images_on_ui_thread_.end(); ++it) {
681    const gfx::ImageSkia source_image_skia = it->second.AsImageSkia();
682    ThemeImageSource* source = new ThemeImageSource(source_image_skia);
683    // image_skia takes ownership of source.
684    gfx::ImageSkia image_skia(source, source_image_skia.size());
685    it->second = gfx::Image(image_skia);
686  }
687
688  // Generate raw images (for new-tab-page attribution and background) for
689  // any missing scale from an available scale image.
690  for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) {
691    pack->GenerateRawImageForAllSupportedScales(kPreloadIDs[i]);
692  }
693
694  // The BrowserThemePack is now in a consistent state.
695  return pack;
696}
697
698// static
699scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack(
700    const base::FilePath& path, const std::string& expected_id) {
701  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
702  // Allow IO on UI thread due to deep-seated theme design issues.
703  // (see http://crbug.com/80206)
704  base::ThreadRestrictions::ScopedAllowIO allow_io;
705  scoped_refptr<BrowserThemePack> pack(new BrowserThemePack);
706  // Scale factor parameter is moot as data pack has image resources for all
707  // supported scale factors.
708  pack->data_pack_.reset(
709      new ui::DataPack(ui::SCALE_FACTOR_NONE));
710
711  if (!pack->data_pack_->LoadFromPath(path)) {
712    LOG(ERROR) << "Failed to load theme data pack.";
713    return NULL;
714  }
715
716  base::StringPiece pointer;
717  if (!pack->data_pack_->GetStringPiece(kHeaderID, &pointer))
718    return NULL;
719  pack->header_ = reinterpret_cast<BrowserThemePackHeader*>(const_cast<char*>(
720      pointer.data()));
721
722  if (pack->header_->version != kThemePackVersion) {
723    DLOG(ERROR) << "BuildFromDataPack failure! Version mismatch!";
724    return NULL;
725  }
726  // TODO(erg): Check endianess once DataPack works on the other endian.
727  std::string theme_id(reinterpret_cast<char*>(pack->header_->theme_id),
728                       extensions::id_util::kIdSize);
729  std::string truncated_id =
730      expected_id.substr(0, extensions::id_util::kIdSize);
731  if (theme_id != truncated_id) {
732    DLOG(ERROR) << "Wrong id: " << theme_id << " vs " << expected_id;
733    return NULL;
734  }
735
736  if (!pack->data_pack_->GetStringPiece(kTintsID, &pointer))
737    return NULL;
738  pack->tints_ = reinterpret_cast<TintEntry*>(const_cast<char*>(
739      pointer.data()));
740
741  if (!pack->data_pack_->GetStringPiece(kColorsID, &pointer))
742    return NULL;
743  pack->colors_ =
744      reinterpret_cast<ColorPair*>(const_cast<char*>(pointer.data()));
745
746  if (!pack->data_pack_->GetStringPiece(kDisplayPropertiesID, &pointer))
747    return NULL;
748  pack->display_properties_ = reinterpret_cast<DisplayPropertyPair*>(
749      const_cast<char*>(pointer.data()));
750
751  if (!pack->data_pack_->GetStringPiece(kSourceImagesID, &pointer))
752    return NULL;
753  pack->source_images_ = reinterpret_cast<int*>(
754      const_cast<char*>(pointer.data()));
755
756  if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer))
757    return NULL;
758
759  if (!InputScalesValid(pointer, pack->scale_factors_)) {
760    DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ "
761                << "from those supported by platform.";
762  }
763  return pack;
764}
765
766// static
767void BrowserThemePack::GetThemeableImageIDRs(std::set<int>* result) {
768  if (!result)
769    return;
770
771  result->clear();
772  for (size_t i = 0; i < kPersistingImagesLength; ++i)
773    result->insert(kPersistingImages[i].idr_id);
774
775#if defined(OS_WIN) && defined(USE_AURA)
776  for (size_t i = 0; i < kPersistingImagesWinDesktopAuraLength; ++i)
777    result->insert(kPersistingImagesWinDesktopAura[i].idr_id);
778#endif
779}
780
781bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const {
782  // Add resources for each of the property arrays.
783  RawDataForWriting resources;
784  resources[kHeaderID] = base::StringPiece(
785      reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader));
786  resources[kTintsID] = base::StringPiece(
787      reinterpret_cast<const char*>(tints_),
788      sizeof(TintEntry[kTintTableLength]));
789  resources[kColorsID] = base::StringPiece(
790      reinterpret_cast<const char*>(colors_),
791      sizeof(ColorPair[kColorTableLength]));
792  resources[kDisplayPropertiesID] = base::StringPiece(
793      reinterpret_cast<const char*>(display_properties_),
794      sizeof(DisplayPropertyPair[kDisplayPropertiesSize]));
795
796  int source_count = 1;
797  int* end = source_images_;
798  for (; *end != -1 ; end++)
799    source_count++;
800  resources[kSourceImagesID] = base::StringPiece(
801      reinterpret_cast<const char*>(source_images_),
802      source_count * sizeof(*source_images_));
803
804  // Store results of GetScaleFactorsAsString() in std::string as
805  // base::StringPiece does not copy data in constructor.
806  std::string scale_factors_string = GetScaleFactorsAsString(scale_factors_);
807  resources[kScaleFactorsID] = scale_factors_string;
808
809  AddRawImagesTo(image_memory_, &resources);
810
811  RawImages reencoded_images;
812  RepackImages(images_on_file_thread_, &reencoded_images);
813  AddRawImagesTo(reencoded_images, &resources);
814
815  return ui::DataPack::WritePack(path, resources, ui::DataPack::BINARY);
816}
817
818bool BrowserThemePack::GetTint(int id, color_utils::HSL* hsl) const {
819  if (tints_) {
820    for (size_t i = 0; i < kTintTableLength; ++i) {
821      if (tints_[i].id == id) {
822        hsl->h = tints_[i].h;
823        hsl->s = tints_[i].s;
824        hsl->l = tints_[i].l;
825        return true;
826      }
827    }
828  }
829
830  return false;
831}
832
833bool BrowserThemePack::GetColor(int id, SkColor* color) const {
834  if (colors_) {
835    for (size_t i = 0; i < kColorTableLength; ++i) {
836      if (colors_[i].id == id) {
837        *color = colors_[i].color;
838        return true;
839      }
840    }
841  }
842
843  return false;
844}
845
846bool BrowserThemePack::GetDisplayProperty(int id, int* result) const {
847  if (display_properties_) {
848    for (size_t i = 0; i < kDisplayPropertiesSize; ++i) {
849      if (display_properties_[i].id == id) {
850        *result = display_properties_[i].property;
851        return true;
852      }
853    }
854  }
855
856  return false;
857}
858
859gfx::Image BrowserThemePack::GetImageNamed(int idr_id) {
860  int prs_id = GetPersistentIDByIDR(idr_id);
861  if (prs_id == -1)
862    return gfx::Image();
863
864  // Check if the image is cached.
865  ImageCache::const_iterator image_iter = images_on_ui_thread_.find(prs_id);
866  if (image_iter != images_on_ui_thread_.end())
867    return image_iter->second;
868
869  ThemeImagePngSource::PngMap png_map;
870  for (size_t i = 0; i < scale_factors_.size(); ++i) {
871    scoped_refptr<base::RefCountedMemory> memory =
872        GetRawData(idr_id, scale_factors_[i]);
873    if (memory.get())
874      png_map[scale_factors_[i]] = memory;
875  }
876  if (!png_map.empty()) {
877    gfx::ImageSkia image_skia(new ThemeImagePngSource(png_map),
878                              ui::SCALE_FACTOR_100P);
879    // |image_skia| takes ownership of ThemeImagePngSource.
880    gfx::Image ret = gfx::Image(image_skia);
881    images_on_ui_thread_[prs_id] = ret;
882    return ret;
883  }
884
885  return gfx::Image();
886}
887
888base::RefCountedMemory* BrowserThemePack::GetRawData(
889    int idr_id,
890    ui::ScaleFactor scale_factor) const {
891  base::RefCountedMemory* memory = NULL;
892  int prs_id = GetPersistentIDByIDR(idr_id);
893  int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
894
895  if (raw_id != -1) {
896    if (data_pack_.get()) {
897      memory = data_pack_->GetStaticMemory(raw_id);
898    } else {
899      RawImages::const_iterator it = image_memory_.find(raw_id);
900      if (it != image_memory_.end()) {
901        memory = it->second.get();
902      }
903    }
904  }
905
906  return memory;
907}
908
909bool BrowserThemePack::HasCustomImage(int idr_id) const {
910  int prs_id = GetPersistentIDByIDR(idr_id);
911  if (prs_id == -1)
912    return false;
913
914  int* img = source_images_;
915  for (; *img != -1; ++img) {
916    if (*img == prs_id)
917      return true;
918  }
919
920  return false;
921}
922
923// private:
924
925BrowserThemePack::BrowserThemePack()
926    : CustomThemeSupplier(EXTENSION),
927      header_(NULL),
928      tints_(NULL),
929      colors_(NULL),
930      display_properties_(NULL),
931      source_images_(NULL) {
932  scale_factors_ = ui::GetSupportedScaleFactors();
933}
934
935void BrowserThemePack::BuildHeader(const Extension* extension) {
936  header_ = new BrowserThemePackHeader;
937  header_->version = kThemePackVersion;
938
939  // TODO(erg): Need to make this endian safe on other computers. Prerequisite
940  // is that ui::DataPack removes this same check.
941#if defined(__BYTE_ORDER)
942  // Linux check
943  COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN,
944                 datapack_assumes_little_endian);
945#elif defined(__BIG_ENDIAN__)
946  // Mac check
947  #error DataPack assumes little endian
948#endif
949  header_->little_endian = 1;
950
951  const std::string& id = extension->id();
952  memcpy(header_->theme_id, id.c_str(), extensions::id_util::kIdSize);
953}
954
955void BrowserThemePack::BuildTintsFromJSON(
956    const base::DictionaryValue* tints_value) {
957  tints_ = new TintEntry[kTintTableLength];
958  for (size_t i = 0; i < kTintTableLength; ++i) {
959    tints_[i].id = -1;
960    tints_[i].h = -1;
961    tints_[i].s = -1;
962    tints_[i].l = -1;
963  }
964
965  if (!tints_value)
966    return;
967
968  // Parse the incoming data from |tints_value| into an intermediary structure.
969  std::map<int, color_utils::HSL> temp_tints;
970  for (DictionaryValue::Iterator iter(*tints_value); !iter.IsAtEnd();
971       iter.Advance()) {
972    const ListValue* tint_list;
973    if (iter.value().GetAsList(&tint_list) &&
974        (tint_list->GetSize() == 3)) {
975      color_utils::HSL hsl = { -1, -1, -1 };
976
977      if (tint_list->GetDouble(0, &hsl.h) &&
978          tint_list->GetDouble(1, &hsl.s) &&
979          tint_list->GetDouble(2, &hsl.l)) {
980        int id = GetIntForString(iter.key(), kTintTable, kTintTableLength);
981        if (id != -1) {
982          temp_tints[id] = hsl;
983        }
984      }
985    }
986  }
987
988  // Copy data from the intermediary data structure to the array.
989  size_t count = 0;
990  for (std::map<int, color_utils::HSL>::const_iterator it =
991           temp_tints.begin();
992       it != temp_tints.end() && count < kTintTableLength;
993       ++it, ++count) {
994    tints_[count].id = it->first;
995    tints_[count].h = it->second.h;
996    tints_[count].s = it->second.s;
997    tints_[count].l = it->second.l;
998  }
999}
1000
1001void BrowserThemePack::BuildColorsFromJSON(
1002    const base::DictionaryValue* colors_value) {
1003  colors_ = new ColorPair[kColorTableLength];
1004  for (size_t i = 0; i < kColorTableLength; ++i) {
1005    colors_[i].id = -1;
1006    colors_[i].color = SkColorSetRGB(0, 0, 0);
1007  }
1008
1009  std::map<int, SkColor> temp_colors;
1010  if (colors_value)
1011    ReadColorsFromJSON(colors_value, &temp_colors);
1012  GenerateMissingColors(&temp_colors);
1013
1014  // Copy data from the intermediary data structure to the array.
1015  size_t count = 0;
1016  for (std::map<int, SkColor>::const_iterator it = temp_colors.begin();
1017       it != temp_colors.end() && count < kColorTableLength; ++it, ++count) {
1018    colors_[count].id = it->first;
1019    colors_[count].color = it->second;
1020  }
1021}
1022
1023void BrowserThemePack::ReadColorsFromJSON(
1024    const base::DictionaryValue* colors_value,
1025    std::map<int, SkColor>* temp_colors) {
1026  // Parse the incoming data from |colors_value| into an intermediary structure.
1027  for (DictionaryValue::Iterator iter(*colors_value); !iter.IsAtEnd();
1028       iter.Advance()) {
1029    const ListValue* color_list;
1030    if (iter.value().GetAsList(&color_list) &&
1031        ((color_list->GetSize() == 3) || (color_list->GetSize() == 4))) {
1032      SkColor color = SK_ColorWHITE;
1033      int r, g, b;
1034      if (color_list->GetInteger(0, &r) &&
1035          color_list->GetInteger(1, &g) &&
1036          color_list->GetInteger(2, &b)) {
1037        if (color_list->GetSize() == 4) {
1038          double alpha;
1039          int alpha_int;
1040          if (color_list->GetDouble(3, &alpha)) {
1041            color = SkColorSetARGB(static_cast<int>(alpha * 255), r, g, b);
1042          } else if (color_list->GetInteger(3, &alpha_int) &&
1043                     (alpha_int == 0 || alpha_int == 1)) {
1044            color = SkColorSetARGB(alpha_int ? 255 : 0, r, g, b);
1045          } else {
1046            // Invalid entry for part 4.
1047            continue;
1048          }
1049        } else {
1050          color = SkColorSetRGB(r, g, b);
1051        }
1052
1053        int id = GetIntForString(iter.key(), kColorTable, kColorTableLength);
1054        if (id != -1) {
1055          (*temp_colors)[id] = color;
1056        }
1057      }
1058    }
1059  }
1060}
1061
1062void BrowserThemePack::GenerateMissingColors(
1063    std::map<int, SkColor>* colors) {
1064  // Generate link colors, if missing. (See GetColor()).
1065  if (!colors->count(ThemeProperties::COLOR_NTP_HEADER) &&
1066      colors->count(ThemeProperties::COLOR_NTP_SECTION)) {
1067    (*colors)[ThemeProperties::COLOR_NTP_HEADER] =
1068        (*colors)[ThemeProperties::COLOR_NTP_SECTION];
1069  }
1070
1071  if (!colors->count(ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE) &&
1072      colors->count(ThemeProperties::COLOR_NTP_SECTION_LINK)) {
1073    SkColor color_section_link =
1074        (*colors)[ThemeProperties::COLOR_NTP_SECTION_LINK];
1075    (*colors)[ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE] =
1076        SkColorSetA(color_section_link, SkColorGetA(color_section_link) / 3);
1077  }
1078
1079  if (!colors->count(ThemeProperties::COLOR_NTP_LINK_UNDERLINE) &&
1080      colors->count(ThemeProperties::COLOR_NTP_LINK)) {
1081    SkColor color_link = (*colors)[ThemeProperties::COLOR_NTP_LINK];
1082    (*colors)[ThemeProperties::COLOR_NTP_LINK_UNDERLINE] =
1083        SkColorSetA(color_link, SkColorGetA(color_link) / 3);
1084  }
1085
1086  // Generate frame colors, if missing. (See GenerateFrameColors()).
1087  SkColor frame;
1088  std::map<int, SkColor>::const_iterator it =
1089      colors->find(ThemeProperties::COLOR_FRAME);
1090  if (it != colors->end()) {
1091    frame = it->second;
1092  } else {
1093    frame = ThemeProperties::GetDefaultColor(
1094        ThemeProperties::COLOR_FRAME);
1095  }
1096
1097  if (!colors->count(ThemeProperties::COLOR_FRAME)) {
1098    (*colors)[ThemeProperties::COLOR_FRAME] =
1099        HSLShift(frame, GetTintInternal(ThemeProperties::TINT_FRAME));
1100  }
1101  if (!colors->count(ThemeProperties::COLOR_FRAME_INACTIVE)) {
1102    (*colors)[ThemeProperties::COLOR_FRAME_INACTIVE] =
1103        HSLShift(frame, GetTintInternal(
1104            ThemeProperties::TINT_FRAME_INACTIVE));
1105  }
1106  if (!colors->count(ThemeProperties::COLOR_FRAME_INCOGNITO)) {
1107    (*colors)[ThemeProperties::COLOR_FRAME_INCOGNITO] =
1108        HSLShift(frame, GetTintInternal(
1109            ThemeProperties::TINT_FRAME_INCOGNITO));
1110  }
1111  if (!colors->count(ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE)) {
1112    (*colors)[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE] =
1113        HSLShift(frame, GetTintInternal(
1114            ThemeProperties::TINT_FRAME_INCOGNITO_INACTIVE));
1115  }
1116}
1117
1118void BrowserThemePack::BuildDisplayPropertiesFromJSON(
1119    const base::DictionaryValue* display_properties_value) {
1120  display_properties_ = new DisplayPropertyPair[kDisplayPropertiesSize];
1121  for (size_t i = 0; i < kDisplayPropertiesSize; ++i) {
1122    display_properties_[i].id = -1;
1123    display_properties_[i].property = 0;
1124  }
1125
1126  if (!display_properties_value)
1127    return;
1128
1129  std::map<int, int> temp_properties;
1130  for (DictionaryValue::Iterator iter(*display_properties_value);
1131       !iter.IsAtEnd(); iter.Advance()) {
1132    int property_id = GetIntForString(iter.key(), kDisplayProperties,
1133                                      kDisplayPropertiesSize);
1134    switch (property_id) {
1135      case ThemeProperties::NTP_BACKGROUND_ALIGNMENT: {
1136        std::string val;
1137        if (iter.value().GetAsString(&val)) {
1138          temp_properties[ThemeProperties::NTP_BACKGROUND_ALIGNMENT] =
1139              ThemeProperties::StringToAlignment(val);
1140        }
1141        break;
1142      }
1143      case ThemeProperties::NTP_BACKGROUND_TILING: {
1144        std::string val;
1145        if (iter.value().GetAsString(&val)) {
1146          temp_properties[ThemeProperties::NTP_BACKGROUND_TILING] =
1147              ThemeProperties::StringToTiling(val);
1148        }
1149        break;
1150      }
1151      case ThemeProperties::NTP_LOGO_ALTERNATE: {
1152        int val = 0;
1153        if (iter.value().GetAsInteger(&val))
1154          temp_properties[ThemeProperties::NTP_LOGO_ALTERNATE] = val;
1155        break;
1156      }
1157    }
1158  }
1159
1160  // Copy data from the intermediary data structure to the array.
1161  size_t count = 0;
1162  for (std::map<int, int>::const_iterator it = temp_properties.begin();
1163       it != temp_properties.end() && count < kDisplayPropertiesSize;
1164       ++it, ++count) {
1165    display_properties_[count].id = it->first;
1166    display_properties_[count].property = it->second;
1167  }
1168}
1169
1170void BrowserThemePack::ParseImageNamesFromJSON(
1171    const base::DictionaryValue* images_value,
1172    const base::FilePath& images_path,
1173    FilePathMap* file_paths) const {
1174  if (!images_value)
1175    return;
1176
1177  for (DictionaryValue::Iterator iter(*images_value); !iter.IsAtEnd();
1178       iter.Advance()) {
1179    if (iter.value().IsType(Value::TYPE_DICTIONARY)) {
1180      const DictionaryValue* inner_value = NULL;
1181      if (iter.value().GetAsDictionary(&inner_value)) {
1182        for (DictionaryValue::Iterator inner_iter(*inner_value);
1183             !inner_iter.IsAtEnd();
1184             inner_iter.Advance()) {
1185          std::string name;
1186          ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE;
1187          if (GetScaleFactorFromManifestKey(inner_iter.key(), &scale_factor) &&
1188              inner_iter.value().IsType(Value::TYPE_STRING) &&
1189              inner_iter.value().GetAsString(&name)) {
1190            AddFileAtScaleToMap(iter.key(),
1191                                scale_factor,
1192                                images_path.AppendASCII(name),
1193                                file_paths);
1194          }
1195        }
1196      }
1197    } else if (iter.value().IsType(Value::TYPE_STRING)) {
1198      std::string name;
1199      if (iter.value().GetAsString(&name)) {
1200        AddFileAtScaleToMap(iter.key(),
1201                            ui::SCALE_FACTOR_100P,
1202                            images_path.AppendASCII(name),
1203                            file_paths);
1204      }
1205    }
1206  }
1207}
1208
1209void BrowserThemePack::AddFileAtScaleToMap(const std::string& image_name,
1210                                           ui::ScaleFactor scale_factor,
1211                                           const base::FilePath& image_path,
1212                                           FilePathMap* file_paths) const {
1213  int id = GetPersistentIDByName(image_name);
1214  if (id != -1)
1215    (*file_paths)[id][scale_factor] = image_path;
1216#if defined(OS_WIN) && defined(USE_AURA)
1217  id = GetPersistentIDByNameHelper(image_name,
1218                                   kPersistingImagesWinDesktopAura,
1219                                   kPersistingImagesWinDesktopAuraLength);
1220  if (id != -1)
1221    (*file_paths)[id][scale_factor] = image_path;
1222#endif
1223}
1224
1225void BrowserThemePack::BuildSourceImagesArray(const FilePathMap& file_paths) {
1226  std::vector<int> ids;
1227  for (FilePathMap::const_iterator it = file_paths.begin();
1228       it != file_paths.end(); ++it) {
1229    ids.push_back(it->first);
1230  }
1231
1232  source_images_ = new int[ids.size() + 1];
1233  std::copy(ids.begin(), ids.end(), source_images_);
1234  source_images_[ids.size()] = -1;
1235}
1236
1237bool BrowserThemePack::LoadRawBitmapsTo(
1238    const FilePathMap& file_paths,
1239    ImageCache* image_cache) {
1240  // Themes should be loaded on the file thread, not the UI thread.
1241  // http://crbug.com/61838
1242  base::ThreadRestrictions::ScopedAllowIO allow_io;
1243
1244  for (FilePathMap::const_iterator it = file_paths.begin();
1245       it != file_paths.end(); ++it) {
1246    int prs_id = it->first;
1247    // Some images need to go directly into |image_memory_|. No modification is
1248    // necessary or desirable.
1249    bool is_copyable = false;
1250    for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) {
1251      if (kPreloadIDs[i] == prs_id) {
1252        is_copyable = true;
1253        break;
1254      }
1255    }
1256    gfx::ImageSkia image_skia;
1257    for (int pass = 0; pass < 2; ++pass) {
1258      // Two passes: In the first pass, we process only scale factor
1259      // 100% and in the second pass all other scale factors. We
1260      // process scale factor 100% first because the first image added
1261      // in image_skia.AddRepresentation() determines the DIP size for
1262      // all representations.
1263      for (ScaleFactorToFileMap::const_iterator s2f = it->second.begin();
1264           s2f != it->second.end(); ++s2f) {
1265        ui::ScaleFactor scale_factor = s2f->first;
1266        if ((pass == 0 && scale_factor != ui::SCALE_FACTOR_100P) ||
1267            (pass == 1 && scale_factor == ui::SCALE_FACTOR_100P)) {
1268          continue;
1269        }
1270        scoped_refptr<base::RefCountedMemory> raw_data(
1271            ReadFileData(s2f->second));
1272        if (!raw_data.get() || !raw_data->size()) {
1273          LOG(ERROR) << "Could not load theme image"
1274                     << " prs_id=" << prs_id
1275                     << " scale_factor_enum=" << scale_factor
1276                     << " file=" << s2f->second.value()
1277                     << (raw_data.get() ? " (zero size)" : " (read error)");
1278          return false;
1279        }
1280        if (is_copyable) {
1281          int raw_id = GetRawIDByPersistentID(prs_id, scale_factor);
1282          image_memory_[raw_id] = raw_data;
1283        } else {
1284          SkBitmap bitmap;
1285          if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(),
1286                                    &bitmap)) {
1287            image_skia.AddRepresentation(
1288                gfx::ImageSkiaRep(bitmap, scale_factor));
1289          } else {
1290            NOTREACHED() << "Unable to decode theme image resource "
1291                         << it->first;
1292          }
1293        }
1294      }
1295    }
1296    if (!is_copyable && !image_skia.isNull())
1297      (*image_cache)[prs_id] = gfx::Image(image_skia);
1298  }
1299
1300  return true;
1301}
1302
1303void BrowserThemePack::CreateImages(ImageCache* images) const {
1304  CropImages(images);
1305  CreateFrameImages(images);
1306  CreateTintedButtons(GetTintInternal(ThemeProperties::TINT_BUTTONS), images);
1307  CreateTabBackgroundImages(images);
1308}
1309
1310void BrowserThemePack::CropImages(ImageCache* images) const {
1311  bool has_frame_border = HasFrameBorder();
1312  for (size_t i = 0; i < arraysize(kImagesToCrop); ++i) {
1313    if (has_frame_border && kImagesToCrop[i].skip_if_frame_border)
1314      continue;
1315
1316    int prs_id = kImagesToCrop[i].prs_id;
1317    ImageCache::iterator it = images->find(prs_id);
1318    if (it == images->end())
1319      continue;
1320
1321    int crop_height = kImagesToCrop[i].max_height;
1322    gfx::ImageSkia image_skia = it->second.AsImageSkia();
1323    (*images)[prs_id] = gfx::Image(gfx::ImageSkiaOperations::ExtractSubset(
1324        image_skia, gfx::Rect(0, 0, image_skia.width(), crop_height)));
1325  }
1326}
1327
1328void BrowserThemePack::CreateFrameImages(ImageCache* images) const {
1329  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
1330
1331  // Create all the output images in a separate cache and move them back into
1332  // the input images because there can be name collisions.
1333  ImageCache temp_output;
1334
1335  for (size_t i = 0; i < arraysize(kFrameTintMap); ++i) {
1336    int prs_id = kFrameTintMap[i].key;
1337    gfx::Image frame;
1338    // If there's no frame image provided for the specified id, then load
1339    // the default provided frame. If that's not provided, skip this whole
1340    // thing and just use the default images.
1341    int prs_base_id = 0;
1342
1343#if defined(OS_WIN) && defined(USE_AURA)
1344    if (prs_id == PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN) {
1345      prs_base_id = images->count(PRS_THEME_FRAME_INCOGNITO_WIN) ?
1346                    PRS_THEME_FRAME_INCOGNITO_WIN : PRS_THEME_FRAME_WIN;
1347    } else if (prs_id == PRS_THEME_FRAME_INACTIVE_WIN) {
1348      prs_base_id = PRS_THEME_FRAME_WIN;
1349    } else if (prs_id == PRS_THEME_FRAME_INCOGNITO_WIN &&
1350                !images->count(PRS_THEME_FRAME_INCOGNITO_WIN)) {
1351      prs_base_id = PRS_THEME_FRAME_WIN;
1352    }
1353#endif
1354    if (!prs_base_id) {
1355      if (prs_id == PRS_THEME_FRAME_INCOGNITO_INACTIVE) {
1356        prs_base_id = images->count(PRS_THEME_FRAME_INCOGNITO) ?
1357                      PRS_THEME_FRAME_INCOGNITO : PRS_THEME_FRAME;
1358      } else if (prs_id == PRS_THEME_FRAME_OVERLAY_INACTIVE) {
1359        prs_base_id = PRS_THEME_FRAME_OVERLAY;
1360      } else if (prs_id == PRS_THEME_FRAME_INACTIVE) {
1361        prs_base_id = PRS_THEME_FRAME;
1362      } else if (prs_id == PRS_THEME_FRAME_INCOGNITO &&
1363                 !images->count(PRS_THEME_FRAME_INCOGNITO)) {
1364        prs_base_id = PRS_THEME_FRAME;
1365      } else {
1366        prs_base_id = prs_id;
1367      }
1368    }
1369    if (images->count(prs_id)) {
1370      frame = (*images)[prs_id];
1371    } else if (prs_base_id != prs_id && images->count(prs_base_id)) {
1372      frame = (*images)[prs_base_id];
1373    } else if (prs_base_id == PRS_THEME_FRAME_OVERLAY) {
1374#if defined(OS_WIN) && defined(USE_AURA)
1375      if (images->count(PRS_THEME_FRAME_WIN)) {
1376#else
1377      if (images->count(PRS_THEME_FRAME)) {
1378#endif
1379        // If there is no theme overlay, don't tint the default frame,
1380        // because it will overwrite the custom frame image when we cache and
1381        // reload from disk.
1382        frame = gfx::Image();
1383      }
1384    } else {
1385      // If the theme doesn't specify an image, then apply the tint to
1386      // the default frame.
1387      frame = rb.GetImageNamed(IDR_THEME_FRAME);
1388#if defined(OS_WIN) && defined(USE_AURA)
1389      if (prs_id >= PRS_THEME_FRAME_WIN &&
1390          prs_id <= PRS_THEME_FRAME_INCOGNITO_INACTIVE_WIN) {
1391        frame = rb.GetImageNamed(IDR_THEME_FRAME_WIN);
1392      }
1393#endif
1394    }
1395    if (!frame.IsEmpty()) {
1396      temp_output[prs_id] = CreateHSLShiftedImage(
1397          frame, GetTintInternal(kFrameTintMap[i].value));
1398    }
1399  }
1400  MergeImageCaches(temp_output, images);
1401}
1402
1403void BrowserThemePack::CreateTintedButtons(
1404    const color_utils::HSL& button_tint,
1405    ImageCache* processed_images) const {
1406  if (button_tint.h != -1 || button_tint.s != -1 || button_tint.l != -1) {
1407    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
1408    const std::set<int>& idr_ids =
1409        ThemeProperties::GetTintableToolbarButtons();
1410    for (std::set<int>::const_iterator it = idr_ids.begin();
1411         it != idr_ids.end(); ++it) {
1412      int prs_id = GetPersistentIDByIDR(*it);
1413      DCHECK(prs_id > 0);
1414
1415      // Fetch the image by IDR...
1416      gfx::Image& button = rb.GetImageNamed(*it);
1417
1418      // but save a version with the persistent ID.
1419      (*processed_images)[prs_id] =
1420          CreateHSLShiftedImage(button, button_tint);
1421    }
1422  }
1423}
1424
1425void BrowserThemePack::CreateTabBackgroundImages(ImageCache* images) const {
1426  ImageCache temp_output;
1427  for (size_t i = 0; i < arraysize(kTabBackgroundMap); ++i) {
1428    int prs_id = kTabBackgroundMap[i].key;
1429    int prs_base_id = kTabBackgroundMap[i].value;
1430
1431    // We only need to generate the background tab images if we were provided
1432    // with a PRS_THEME_FRAME.
1433    ImageCache::const_iterator it = images->find(prs_base_id);
1434    if (it != images->end()) {
1435      gfx::ImageSkia image_to_tint = (it->second).AsImageSkia();
1436      color_utils::HSL hsl_shift = GetTintInternal(
1437          ThemeProperties::TINT_BACKGROUND_TAB);
1438      int vertical_offset = images->count(prs_id)
1439                            ? kRestoredTabVerticalOffset : 0;
1440
1441      gfx::ImageSkia overlay;
1442      ImageCache::const_iterator overlay_it = images->find(prs_id);
1443      if (overlay_it != images->end())
1444        overlay = overlay_it->second.AsImageSkia();
1445
1446      gfx::ImageSkiaSource* source = new TabBackgroundImageSource(
1447          image_to_tint, overlay, hsl_shift, vertical_offset);
1448      // ImageSkia takes ownership of |source|.
1449      temp_output[prs_id] = gfx::Image(gfx::ImageSkia(source,
1450          image_to_tint.size()));
1451    }
1452  }
1453  MergeImageCaches(temp_output, images);
1454}
1455
1456void BrowserThemePack::RepackImages(const ImageCache& images,
1457                                    RawImages* reencoded_images) const {
1458  typedef std::vector<ui::ScaleFactor> ScaleFactors;
1459  for (ImageCache::const_iterator it = images.begin();
1460       it != images.end(); ++it) {
1461    gfx::ImageSkia image_skia = *it->second.ToImageSkia();
1462
1463    typedef std::vector<gfx::ImageSkiaRep> ImageSkiaReps;
1464    ImageSkiaReps image_reps = image_skia.image_reps();
1465    if (image_reps.empty()) {
1466      NOTREACHED() << "No image reps for resource " << it->first << ".";
1467    }
1468    for (ImageSkiaReps::iterator rep_it = image_reps.begin();
1469         rep_it != image_reps.end(); ++rep_it) {
1470      std::vector<unsigned char> bitmap_data;
1471      if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false,
1472                                             &bitmap_data)) {
1473        NOTREACHED() << "Image file for resource " << it->first
1474                     << " could not be encoded.";
1475      }
1476      int raw_id = GetRawIDByPersistentID(it->first, rep_it->scale_factor());
1477      (*reencoded_images)[raw_id] =
1478          base::RefCountedBytes::TakeVector(&bitmap_data);
1479    }
1480  }
1481}
1482
1483void BrowserThemePack::MergeImageCaches(
1484    const ImageCache& source, ImageCache* destination) const {
1485  for (ImageCache::const_iterator it = source.begin(); it != source.end();
1486       ++it) {
1487    (*destination)[it->first] = it->second;
1488  }
1489}
1490
1491void BrowserThemePack::AddRawImagesTo(const RawImages& images,
1492                                      RawDataForWriting* out) const {
1493  for (RawImages::const_iterator it = images.begin(); it != images.end();
1494       ++it) {
1495    (*out)[it->first] = base::StringPiece(
1496        reinterpret_cast<const char*>(it->second->front()), it->second->size());
1497  }
1498}
1499
1500color_utils::HSL BrowserThemePack::GetTintInternal(int id) const {
1501  if (tints_) {
1502    for (size_t i = 0; i < kTintTableLength; ++i) {
1503      if (tints_[i].id == id) {
1504        color_utils::HSL hsl;
1505        hsl.h = tints_[i].h;
1506        hsl.s = tints_[i].s;
1507        hsl.l = tints_[i].l;
1508        return hsl;
1509      }
1510    }
1511  }
1512
1513  return ThemeProperties::GetDefaultTint(id);
1514}
1515
1516int BrowserThemePack::GetRawIDByPersistentID(
1517    int prs_id,
1518    ui::ScaleFactor scale_factor) const {
1519  if (prs_id < 0)
1520    return -1;
1521
1522  for (size_t i = 0; i < scale_factors_.size(); ++i) {
1523    if (scale_factors_[i] == scale_factor)
1524      return static_cast<int>(kPersistingImagesLength * i) + prs_id;
1525  }
1526  return -1;
1527}
1528
1529bool BrowserThemePack::GetScaleFactorFromManifestKey(
1530    const std::string& key,
1531    ui::ScaleFactor* scale_factor) const {
1532  int percent = 0;
1533  if (base::StringToInt(key, &percent)) {
1534    float scale = static_cast<float>(percent) / 100.0f;
1535    for (size_t i = 0; i < scale_factors_.size(); ++i) {
1536      if (fabs(ui::GetScaleFactorScale(scale_factors_[i]) - scale) < 0.001) {
1537        *scale_factor = scale_factors_[i];
1538        return true;
1539      }
1540    }
1541  }
1542  return false;
1543}
1544
1545void BrowserThemePack::GenerateRawImageForAllSupportedScales(int prs_id) {
1546  // Compute (by scaling) bitmaps for |prs_id| for any scale factors
1547  // for which the theme author did not provide a bitmap. We compute
1548  // the bitmaps using the highest scale factor that theme author
1549  // provided.
1550  // Note: We use only supported scale factors. For example, if scale
1551  // factor 2x is supported by the current system, but 1.8x is not and
1552  // if the theme author did not provide an image for 2x but one for
1553  // 1.8x, we will not use the 1.8x image here. Here we will only use
1554  // images provided for scale factors supported by the current system.
1555
1556  // See if any image is missing. If not, we're done.
1557  bool image_missing = false;
1558  for (size_t i = 0; i < scale_factors_.size(); ++i) {
1559    int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1560    if (image_memory_.find(raw_id) == image_memory_.end()) {
1561      image_missing = true;
1562      break;
1563    }
1564  }
1565  if (!image_missing)
1566    return;
1567
1568  // Find available scale factor with highest scale.
1569  ui::ScaleFactor available_scale_factor = ui::SCALE_FACTOR_NONE;
1570  for (size_t i = 0; i < scale_factors_.size(); ++i) {
1571    int raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1572    if ((available_scale_factor == ui::SCALE_FACTOR_NONE ||
1573         (ui::GetScaleFactorScale(scale_factors_[i]) >
1574          ui::GetScaleFactorScale(available_scale_factor))) &&
1575        image_memory_.find(raw_id) != image_memory_.end()) {
1576      available_scale_factor = scale_factors_[i];
1577    }
1578  }
1579  // If no scale factor is available, we're done.
1580  if (available_scale_factor == ui::SCALE_FACTOR_NONE)
1581    return;
1582
1583  // Get bitmap for the available scale factor.
1584  int available_raw_id = GetRawIDByPersistentID(prs_id, available_scale_factor);
1585  RawImages::const_iterator it = image_memory_.find(available_raw_id);
1586  SkBitmap available_bitmap;
1587  if (!gfx::PNGCodec::Decode(it->second->front(),
1588                             it->second->size(),
1589                             &available_bitmap)) {
1590    NOTREACHED() << "Unable to decode theme image for prs_id="
1591                 << prs_id << " for scale_factor=" << available_scale_factor;
1592    return;
1593  }
1594
1595  // Fill in all missing scale factors by scaling the available bitmap.
1596  for (size_t i = 0; i < scale_factors_.size(); ++i) {
1597    int scaled_raw_id = GetRawIDByPersistentID(prs_id, scale_factors_[i]);
1598    if (image_memory_.find(scaled_raw_id) != image_memory_.end())
1599      continue;
1600    SkBitmap scaled_bitmap =
1601        CreateLowQualityResizedBitmap(available_bitmap,
1602                                      available_scale_factor,
1603                                      scale_factors_[i]);
1604    std::vector<unsigned char> bitmap_data;
1605    if (!gfx::PNGCodec::EncodeBGRASkBitmap(scaled_bitmap,
1606                                           false,
1607                                           &bitmap_data)) {
1608      NOTREACHED() << "Unable to encode theme image for prs_id="
1609                   << prs_id << " for scale_factor=" << scale_factors_[i];
1610      break;
1611    }
1612    image_memory_[scaled_raw_id] =
1613        base::RefCountedBytes::TakeVector(&bitmap_data);
1614  }
1615}
1616