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