OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/browser_theme_pack.h" |
| 6 |
| 7 #include <climits> |
| 8 |
| 9 #include "app/gfx/codec/png_codec.h" |
| 10 #include "app/gfx/skbitmap_operations.h" |
| 11 #include "base/data_pack.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/stl_util-inl.h" |
| 14 #include "base/string_util.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/browser/browser_theme_provider.h" |
| 17 #include "chrome/browser/chrome_thread.h" |
| 18 #include "chrome/browser/theme_resources_util.h" |
| 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "grit/app_resources.h" |
| 21 #include "grit/theme_resources.h" |
| 22 #include "net/base/file_stream.h" |
| 23 #include "net/base/net_errors.h" |
| 24 #include "third_party/skia/include/core/SkBitmap.h" |
| 25 #include "third_party/skia/include/core/SkCanvas.h" |
| 26 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Version number of the current theme pack. We just throw out and rebuild |
| 31 // theme packs that aren't int-equal to this. |
| 32 const int kThemePackVersion = 1; |
| 33 |
| 34 // IDs that are in the DataPack won't clash with the positive integer |
| 35 // int32_t. kHeaderID should always have the maximum value because we want the |
| 36 // "header" to be written last. That way we can detect whether the pack was |
| 37 // successfully written and ignore and regenerate if it was only partially |
| 38 // written (i.e. chrome crashed on a different thread while writing the pack). |
| 39 const int kHeaderID = UINT_MAX - 1; |
| 40 const int kTintsID = UINT_MAX - 2; |
| 41 const int kColorsID = UINT_MAX - 3; |
| 42 const int kDisplayPropertiesID = UINT_MAX - 4; |
| 43 |
| 44 // Static size of the tint/color/display property arrays that are mmapped. |
| 45 const int kTintArraySize = 6; |
| 46 const int kColorArraySize = 19; |
| 47 const int kDisplayPropertySize = 3; |
| 48 |
| 49 // The sum of kFrameBorderThickness and kNonClientRestoredExtraThickness from |
| 50 // OpaqueBrowserFrameView. |
| 51 const int kRestoredTabVerticalOffset = 15; |
| 52 |
| 53 |
| 54 struct StringToIntTable { |
| 55 const char* key; |
| 56 int id; |
| 57 }; |
| 58 |
| 59 // Strings used by themes to identify tints in the JSON. |
| 60 StringToIntTable kTintTable[] = { |
| 61 { "buttons", BrowserThemeProvider::TINT_BUTTONS }, |
| 62 { "frame", BrowserThemeProvider::TINT_FRAME }, |
| 63 { "frame_inactive", BrowserThemeProvider::TINT_FRAME_INACTIVE }, |
| 64 { "frame_incognito_inactive", |
| 65 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE }, |
| 66 { "background_tab", BrowserThemeProvider::TINT_BACKGROUND_TAB }, |
| 67 { NULL, 0 } |
| 68 }; |
| 69 |
| 70 // Strings used by themes to identify colors in the JSON. |
| 71 StringToIntTable kColorTable[] = { |
| 72 { "frame", BrowserThemeProvider::COLOR_FRAME }, |
| 73 { "frame_inactive", BrowserThemeProvider::COLOR_FRAME_INACTIVE }, |
| 74 { "frame_incognito", BrowserThemeProvider::COLOR_FRAME_INCOGNITO }, |
| 75 { "frame_incognito_inactive", |
| 76 BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE }, |
| 77 { "toolbar", BrowserThemeProvider::COLOR_TOOLBAR }, |
| 78 { "tab_text", BrowserThemeProvider::COLOR_TAB_TEXT }, |
| 79 { "tab_background_text", BrowserThemeProvider::COLOR_BACKGROUND_TAB_TEXT }, |
| 80 { "bookmark_text", BrowserThemeProvider::COLOR_BOOKMARK_TEXT }, |
| 81 { "ntp_background", BrowserThemeProvider::COLOR_NTP_BACKGROUND }, |
| 82 { "ntp_text", BrowserThemeProvider::COLOR_NTP_TEXT }, |
| 83 { "ntp_link", BrowserThemeProvider::COLOR_NTP_LINK }, |
| 84 { "ntp_link_underline", BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE }, |
| 85 { "ntp_header", BrowserThemeProvider::COLOR_NTP_HEADER }, |
| 86 { "ntp_section", BrowserThemeProvider::COLOR_NTP_SECTION }, |
| 87 { "ntp_section_text", BrowserThemeProvider::COLOR_NTP_SECTION_TEXT }, |
| 88 { "ntp_section_link", BrowserThemeProvider::COLOR_NTP_SECTION_LINK }, |
| 89 { "ntp_section_link_underline", |
| 90 BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE }, |
| 91 { "control_background", BrowserThemeProvider::COLOR_CONTROL_BACKGROUND }, |
| 92 { "button_background", BrowserThemeProvider::COLOR_BUTTON_BACKGROUND }, |
| 93 { NULL, 0 } |
| 94 }; |
| 95 |
| 96 // Strings used by themes to identify display properties keys in JSON. |
| 97 StringToIntTable kDisplayProperties[] = { |
| 98 { "ntp_background_alignment", |
| 99 BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT }, |
| 100 { "ntp_background_repeat", BrowserThemeProvider::NTP_BACKGROUND_TILING }, |
| 101 { "ntp_logo_alternate", BrowserThemeProvider::NTP_LOGO_ALTERNATE }, |
| 102 { NULL, 0 } |
| 103 }; |
| 104 |
| 105 // Strings used by the tiling values in JSON. |
| 106 StringToIntTable kTilingStrings[] = { |
| 107 { "no-repeat", BrowserThemeProvider::NO_REPEAT }, |
| 108 { "repeat-x", BrowserThemeProvider::REPEAT_X }, |
| 109 { "repeat-y", BrowserThemeProvider::REPEAT_Y }, |
| 110 { "repeat", BrowserThemeProvider::REPEAT }, |
| 111 { NULL, 0 } |
| 112 }; |
| 113 |
| 114 int GetIntForString(const std::string& key, StringToIntTable* table) { |
| 115 for (int i = 0; table[i].key != NULL; ++i) { |
| 116 if (base::strcasecmp(key.c_str(), table[i].key) == 0) { |
| 117 return table[i].id; |
| 118 } |
| 119 } |
| 120 |
| 121 return -1; |
| 122 } |
| 123 |
| 124 struct IntToIntTable { |
| 125 int key; |
| 126 int value; |
| 127 }; |
| 128 |
| 129 // Mapping used in GenerateFrameImages() to associate frame images with the |
| 130 // tint ID that should maybe be applied to it. |
| 131 IntToIntTable kFrameTintMap[] = { |
| 132 { IDR_THEME_FRAME, BrowserThemeProvider::TINT_FRAME }, |
| 133 { IDR_THEME_FRAME_INACTIVE, BrowserThemeProvider::TINT_FRAME_INACTIVE }, |
| 134 { IDR_THEME_FRAME_OVERLAY, BrowserThemeProvider::TINT_FRAME }, |
| 135 { IDR_THEME_FRAME_OVERLAY_INACTIVE, |
| 136 BrowserThemeProvider::TINT_FRAME_INACTIVE }, |
| 137 { IDR_THEME_FRAME_INCOGNITO, BrowserThemeProvider::TINT_FRAME_INCOGNITO }, |
| 138 { IDR_THEME_FRAME_INCOGNITO_INACTIVE, |
| 139 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE } |
| 140 }; |
| 141 |
| 142 // Mapping used in GenerateTabBackgroundImages() to associate what frame image |
| 143 // goes with which tab background. |
| 144 IntToIntTable kTabBackgroundMap[] = { |
| 145 { IDR_THEME_TAB_BACKGROUND, IDR_THEME_FRAME }, |
| 146 { IDR_THEME_TAB_BACKGROUND_INCOGNITO, IDR_THEME_FRAME_INCOGNITO } |
| 147 }; |
| 148 |
| 149 // A list of images that don't need tinting or any other modification and can |
| 150 // be byte-copied directly into the finished DataPack. This should contain all |
| 151 // themeable image IDs that aren't in kFrameTintMap or kTabBackgroundMap. |
| 152 const int kPreloadIDs[] = { |
| 153 IDR_THEME_TOOLBAR, |
| 154 IDR_THEME_NTP_BACKGROUND, |
| 155 IDR_THEME_BUTTON_BACKGROUND, |
| 156 IDR_THEME_NTP_ATTRIBUTION, |
| 157 IDR_THEME_WINDOW_CONTROL_BACKGROUND |
| 158 }; |
| 159 |
| 160 // The image resources that will be tinted by the 'button' tint value. |
| 161 const int kToolbarButtonIDs[] = { |
| 162 IDR_BACK, IDR_BACK_D, IDR_BACK_H, IDR_BACK_P, |
| 163 IDR_FORWARD, IDR_FORWARD_D, IDR_FORWARD_H, IDR_FORWARD_P, |
| 164 IDR_RELOAD, IDR_RELOAD_H, IDR_RELOAD_P, |
| 165 IDR_HOME, IDR_HOME_H, IDR_HOME_P, |
| 166 IDR_STAR, IDR_STAR_NOBORDER, IDR_STAR_NOBORDER_CENTER, IDR_STAR_D, IDR_STAR_H, |
| 167 IDR_STAR_P, |
| 168 IDR_STARRED, IDR_STARRED_NOBORDER, IDR_STARRED_NOBORDER_CENTER, IDR_STARRED_H, |
| 169 IDR_STARRED_P, |
| 170 IDR_GO, IDR_GO_NOBORDER, IDR_GO_NOBORDER_CENTER, IDR_GO_H, IDR_GO_P, |
| 171 IDR_STOP, IDR_STOP_NOBORDER, IDR_STOP_NOBORDER_CENTER, IDR_STOP_H, IDR_STOP_P, |
| 172 IDR_MENU_BOOKMARK, |
| 173 IDR_MENU_PAGE, IDR_MENU_PAGE_RTL, |
| 174 IDR_MENU_CHROME, IDR_MENU_CHROME_RTL, |
| 175 IDR_MENU_DROPARROW, |
| 176 IDR_THROBBER, IDR_THROBBER_WAITING, IDR_THROBBER_LIGHT, |
| 177 IDR_LOCATIONBG |
| 178 }; |
| 179 |
| 180 // Returns a piece of memory with the contents of the file |path|. |
| 181 RefCountedMemory* ReadFileData(const FilePath& path) { |
| 182 if (!path.empty()) { |
| 183 net::FileStream file; |
| 184 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| 185 if (file.Open(path, flags) == net::OK) { |
| 186 int64 avail = file.Available(); |
| 187 if (avail > 0 && avail < INT_MAX) { |
| 188 size_t size = static_cast<size_t>(avail); |
| 189 std::vector<unsigned char> raw_data; |
| 190 raw_data.resize(size); |
| 191 char* data = reinterpret_cast<char*>(&(raw_data.front())); |
| 192 if (file.ReadUntilComplete(data, size) == avail) |
| 193 return RefCountedBytes::TakeVector(&raw_data); |
| 194 } |
| 195 } |
| 196 } |
| 197 |
| 198 return NULL; |
| 199 } |
| 200 |
| 201 // Does error checking for invalid incoming data while trying to read an |
| 202 // floaing point value. |
| 203 bool ValidRealValue(ListValue* tint_list, int index, double* out) { |
| 204 if (tint_list->GetReal(index, out)) |
| 205 return true; |
| 206 |
| 207 int value = 0; |
| 208 if (tint_list->GetInteger(index, &value)) { |
| 209 *out = value; |
| 210 return true; |
| 211 } |
| 212 |
| 213 return false; |
| 214 } |
| 215 |
| 216 } // namespace |
| 217 |
| 218 BrowserThemePack::~BrowserThemePack() { |
| 219 if (!data_pack_.get()) { |
| 220 delete header_; |
| 221 delete [] tints_; |
| 222 delete [] colors_; |
| 223 delete [] display_properties_; |
| 224 } |
| 225 |
| 226 STLDeleteValues(&image_cache_); |
| 227 } |
| 228 |
| 229 // static |
| 230 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromExtension( |
| 231 Extension* extension) { |
| 232 DCHECK(extension); |
| 233 DCHECK(extension->IsTheme()); |
| 234 |
| 235 scoped_refptr<BrowserThemePack> pack = new BrowserThemePack; |
| 236 pack->BuildHeader(extension); |
| 237 pack->BuildTintsFromJSON(extension->GetThemeTints()); |
| 238 pack->BuildColorsFromJSON(extension->GetThemeColors()); |
| 239 pack->BuildDisplayPropertiesFromJSON(extension->GetThemeDisplayProperties()); |
| 240 |
| 241 // Builds the images. (Image building is dependent on tints). |
| 242 std::map<int, FilePath> file_paths; |
| 243 pack->ParseImageNamesFromJSON(extension->GetThemeImages(), |
| 244 extension->path(), |
| 245 &file_paths); |
| 246 |
| 247 pack->LoadRawBitmapsTo(file_paths, &pack->image_cache_); |
| 248 |
| 249 pack->GenerateFrameImages(&pack->image_cache_); |
| 250 |
| 251 #if !defined(OS_MACOSX) |
| 252 // OSX uses its own special buttons that are PDFs that do odd sorts of vector |
| 253 // graphics tricks. Other platforms use bitmaps and we must pre-tint them. |
| 254 pack->GenerateTintedButtons( |
| 255 pack->GetTintInternal(BrowserThemeProvider::TINT_BUTTONS), |
| 256 &pack->image_cache_); |
| 257 #endif |
| 258 |
| 259 pack->GenerateTabBackgroundImages(&pack->image_cache_); |
| 260 |
| 261 // Repack all the images from |image_cache_| into |image_memory_| for |
| 262 // writing to the data pack |
| 263 pack->RepackImageCacheToImageMemory(); |
| 264 |
| 265 // The BrowserThemePack is now in a consistent state. |
| 266 return pack; |
| 267 } |
| 268 |
| 269 // static |
| 270 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( |
| 271 FilePath path, const std::string& expected_id) { |
| 272 scoped_refptr<BrowserThemePack> pack = new BrowserThemePack; |
| 273 pack->data_pack_.reset(new base::DataPack); |
| 274 |
| 275 if (!pack->data_pack_->Load(path)) { |
| 276 LOG(ERROR) << "Failed to load theme data pack."; |
| 277 return NULL; |
| 278 } |
| 279 |
| 280 base::StringPiece pointer; |
| 281 if (!pack->data_pack_->GetStringPiece(kHeaderID, &pointer)) |
| 282 return NULL; |
| 283 pack->header_ = reinterpret_cast<BrowserThemePackHeader*>(const_cast<char*>( |
| 284 pointer.data())); |
| 285 |
| 286 if (pack->header_->version != kThemePackVersion) |
| 287 return NULL; |
| 288 // TODO(erg): Check endianess once DataPack works on the other endian. |
| 289 std::string theme_id(reinterpret_cast<char*>(pack->header_->theme_id), |
| 290 Extension::kIdSize); |
| 291 std::string truncated_id = expected_id.substr(0, Extension::kIdSize); |
| 292 if (theme_id != truncated_id) { |
| 293 DLOG(ERROR) << "Wrong id: " << theme_id << " vs " << expected_id; |
| 294 return NULL; |
| 295 } |
| 296 |
| 297 if (!pack->data_pack_->GetStringPiece(kTintsID, &pointer)) |
| 298 return NULL; |
| 299 pack->tints_ = reinterpret_cast<TintEntry*>(const_cast<char*>( |
| 300 pointer.data())); |
| 301 |
| 302 if (!pack->data_pack_->GetStringPiece(kColorsID, &pointer)) |
| 303 return NULL; |
| 304 pack->colors_ = |
| 305 reinterpret_cast<ColorPair*>(const_cast<char*>(pointer.data())); |
| 306 |
| 307 if (!pack->data_pack_->GetStringPiece(kDisplayPropertiesID, &pointer)) |
| 308 return NULL; |
| 309 pack->display_properties_ = reinterpret_cast<DisplayPropertyPair*>( |
| 310 const_cast<char*>(pointer.data())); |
| 311 |
| 312 return pack; |
| 313 } |
| 314 |
| 315 bool BrowserThemePack::WriteToDisk(FilePath path) const { |
| 316 std::map<uint32, base::StringPiece> resources; |
| 317 |
| 318 resources[kHeaderID] = base::StringPiece( |
| 319 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); |
| 320 resources[kTintsID] = base::StringPiece( |
| 321 reinterpret_cast<const char*>(tints_), sizeof(TintEntry[kTintArraySize])); |
| 322 resources[kColorsID] = base::StringPiece( |
| 323 reinterpret_cast<const char*>(colors_), |
| 324 sizeof(ColorPair[kColorArraySize])); |
| 325 resources[kDisplayPropertiesID] = base::StringPiece( |
| 326 reinterpret_cast<const char*>(display_properties_), |
| 327 sizeof(DisplayPropertyPair[kDisplayPropertySize])); |
| 328 |
| 329 for (RawImages::const_iterator it = image_memory_.begin(); |
| 330 it != image_memory_.end(); ++it) { |
| 331 resources[it->first] = base::StringPiece( |
| 332 reinterpret_cast<const char*>(it->second->front()), it->second->size()); |
| 333 } |
| 334 |
| 335 return base::DataPack::WritePack(path, resources); |
| 336 } |
| 337 |
| 338 bool BrowserThemePack::GetTint(int id, color_utils::HSL* hsl) const { |
| 339 if (tints_) { |
| 340 for (int i = 0; i < kTintArraySize; ++i) { |
| 341 if (tints_[i].id == id) { |
| 342 hsl->h = tints_[i].h; |
| 343 hsl->s = tints_[i].s; |
| 344 hsl->l = tints_[i].l; |
| 345 return true; |
| 346 } |
| 347 } |
| 348 } |
| 349 |
| 350 return false; |
| 351 } |
| 352 |
| 353 bool BrowserThemePack::GetColor(int id, SkColor* color) const { |
| 354 if (colors_) { |
| 355 for (int i = 0; i < kColorArraySize; ++i) { |
| 356 if (colors_[i].id == id) { |
| 357 *color = colors_[i].color; |
| 358 return true; |
| 359 } |
| 360 } |
| 361 } |
| 362 |
| 363 return false; |
| 364 } |
| 365 |
| 366 bool BrowserThemePack::GetDisplayProperty(int id, int* result) const { |
| 367 if (display_properties_) { |
| 368 for (int i = 0; i < kDisplayPropertySize; ++i) { |
| 369 if (display_properties_[i].id == id) { |
| 370 *result = display_properties_[i].property; |
| 371 return true; |
| 372 } |
| 373 } |
| 374 } |
| 375 |
| 376 return false; |
| 377 } |
| 378 |
| 379 SkBitmap* BrowserThemePack::GetBitmapNamed(int id) const { |
| 380 // Check our cache of prepared images, first. |
| 381 ImageCache::const_iterator image_iter = image_cache_.find(id); |
| 382 if (image_iter != image_cache_.end()) |
| 383 return image_iter->second; |
| 384 |
| 385 scoped_refptr<RefCountedMemory> memory; |
| 386 if (data_pack_.get()) { |
| 387 memory = data_pack_->GetStaticMemory(id); |
| 388 } else { |
| 389 RawImages::const_iterator it = image_memory_.find(id); |
| 390 if (it != image_memory_.end()) { |
| 391 memory = it->second; |
| 392 } |
| 393 } |
| 394 |
| 395 if (memory.get()) { |
| 396 // Decode the PNG. |
| 397 SkBitmap bitmap; |
| 398 if (!gfx::PNGCodec::Decode(memory->front(), memory->size(), |
| 399 &bitmap)) { |
| 400 NOTREACHED() << "Unable to decode theme image resource " << id |
| 401 << " from saved DataPack."; |
| 402 return NULL; |
| 403 } |
| 404 |
| 405 SkBitmap* ret = new SkBitmap(bitmap); |
| 406 image_cache_[id] = ret; |
| 407 |
| 408 return ret; |
| 409 } |
| 410 |
| 411 return NULL; |
| 412 } |
| 413 |
| 414 RefCountedMemory* BrowserThemePack::GetRawData(int id) const { |
| 415 RefCountedMemory* memory = NULL; |
| 416 |
| 417 if (data_pack_.get()) { |
| 418 memory = data_pack_->GetStaticMemory(id); |
| 419 } else { |
| 420 RawImages::const_iterator it = image_memory_.find(id); |
| 421 if (it != image_memory_.end()) { |
| 422 memory = it->second; |
| 423 } |
| 424 } |
| 425 |
| 426 return memory; |
| 427 } |
| 428 |
| 429 bool BrowserThemePack::HasCustomImage(int id) const { |
| 430 if (data_pack_.get()) { |
| 431 base::StringPiece ignored; |
| 432 return data_pack_->GetStringPiece(id, &ignored); |
| 433 } else { |
| 434 return image_memory_.count(id) > 0; |
| 435 } |
| 436 } |
| 437 |
| 438 // private: |
| 439 |
| 440 BrowserThemePack::BrowserThemePack() |
| 441 : header_(NULL), |
| 442 tints_(NULL), |
| 443 colors_(NULL), |
| 444 display_properties_(NULL) { |
| 445 } |
| 446 |
| 447 void BrowserThemePack::BuildHeader(Extension* extension) { |
| 448 header_ = new BrowserThemePackHeader; |
| 449 header_->version = kThemePackVersion; |
| 450 |
| 451 // TODO(erg): Need to make this endian safe on other computers. Prerequisite |
| 452 // is that base::DataPack removes this same check. |
| 453 #if defined(__BYTE_ORDER) |
| 454 // Linux check |
| 455 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, |
| 456 datapack_assumes_little_endian); |
| 457 #elif defined(__BIG_ENDIAN__) |
| 458 // Mac check |
| 459 #error DataPack assumes little endian |
| 460 #endif |
| 461 header_->little_endian = 1; |
| 462 |
| 463 const std::string& id = extension->id(); |
| 464 memcpy(header_->theme_id, id.c_str(), Extension::kIdSize); |
| 465 } |
| 466 |
| 467 void BrowserThemePack::BuildTintsFromJSON(DictionaryValue* tints_value) { |
| 468 tints_ = new TintEntry[kTintArraySize]; |
| 469 for (int i = 0; i < kTintArraySize; ++i) { |
| 470 tints_[i].id = -1; |
| 471 tints_[i].h = -1; |
| 472 tints_[i].s = -1; |
| 473 tints_[i].l = -1; |
| 474 } |
| 475 |
| 476 // Parse the incoming data from |tints_value| into an intermediary structure. |
| 477 std::map<int, color_utils::HSL> temp_tints; |
| 478 for (DictionaryValue::key_iterator iter(tints_value->begin_keys()); |
| 479 iter != tints_value->end_keys(); ++iter) { |
| 480 ListValue* tint_list; |
| 481 if (tints_value->GetList(*iter, &tint_list) && |
| 482 (tint_list->GetSize() == 3)) { |
| 483 color_utils::HSL hsl = { -1, -1, -1 }; |
| 484 |
| 485 if (ValidRealValue(tint_list, 0, &hsl.h) && |
| 486 ValidRealValue(tint_list, 1, &hsl.s) && |
| 487 ValidRealValue(tint_list, 2, &hsl.l)) { |
| 488 |
| 489 int id = GetIntForString(WideToUTF8(*iter), kTintTable); |
| 490 if (id != -1) { |
| 491 temp_tints[id] = hsl; |
| 492 } |
| 493 } |
| 494 } |
| 495 } |
| 496 |
| 497 // Copy data from the intermediary data structure to the array. |
| 498 int count = 0; |
| 499 for (std::map<int, color_utils::HSL>::const_iterator it = |
| 500 temp_tints.begin(); it != temp_tints.end() && count < kTintArraySize; |
| 501 ++it, ++count) { |
| 502 tints_[count].id = it->first; |
| 503 tints_[count].h = it->second.h; |
| 504 tints_[count].s = it->second.s; |
| 505 tints_[count].l = it->second.l; |
| 506 } |
| 507 } |
| 508 |
| 509 void BrowserThemePack::BuildColorsFromJSON(DictionaryValue* colors_value) { |
| 510 colors_ = new ColorPair[kColorArraySize]; |
| 511 for (int i = 0; i < kColorArraySize; ++i) { |
| 512 colors_[i].id = -1; |
| 513 colors_[i].color = SkColorSetRGB(0, 0, 0); |
| 514 } |
| 515 |
| 516 std::map<int, SkColor> temp_colors; |
| 517 ReadColorsFromJSON(colors_value, &temp_colors); |
| 518 GenerateMissingColors(&temp_colors); |
| 519 |
| 520 // Copy data from the intermediary data structure to the array. |
| 521 int count = 0; |
| 522 for (std::map<int, SkColor>::const_iterator it = temp_colors.begin(); |
| 523 it != temp_colors.end() && count < kColorArraySize; ++it, ++count) { |
| 524 colors_[count].id = it->first; |
| 525 colors_[count].color = it->second; |
| 526 } |
| 527 } |
| 528 |
| 529 void BrowserThemePack::ReadColorsFromJSON( |
| 530 DictionaryValue* colors_value, |
| 531 std::map<int, SkColor>* temp_colors) { |
| 532 // Parse the incoming data from |colors_value| into an intermediary structure. |
| 533 for (DictionaryValue::key_iterator iter(colors_value->begin_keys()); |
| 534 iter != colors_value->end_keys(); ++iter) { |
| 535 ListValue* color_list; |
| 536 if (colors_value->GetList(*iter, &color_list) && |
| 537 ((color_list->GetSize() == 3) || (color_list->GetSize() == 4))) { |
| 538 SkColor color = SK_ColorWHITE; |
| 539 int r, g, b; |
| 540 if (color_list->GetInteger(0, &r) && |
| 541 color_list->GetInteger(1, &g) && |
| 542 color_list->GetInteger(2, &b)) { |
| 543 if (color_list->GetSize() == 4) { |
| 544 double alpha; |
| 545 int alpha_int; |
| 546 if (color_list->GetReal(3, &alpha)) { |
| 547 color = SkColorSetARGB(static_cast<int>(alpha * 255), r, g, b); |
| 548 } else if (color_list->GetInteger(3, &alpha_int) && |
| 549 (alpha_int == 0 || alpha_int == 1)) { |
| 550 color = SkColorSetARGB(alpha_int ? 255 : 0, r, g, b); |
| 551 } else { |
| 552 // Invalid entry for part 4. |
| 553 continue; |
| 554 } |
| 555 } else { |
| 556 color = SkColorSetRGB(r, g, b); |
| 557 } |
| 558 |
| 559 int id = GetIntForString(WideToUTF8(*iter), kColorTable); |
| 560 if (id != -1) { |
| 561 (*temp_colors)[id] = color; |
| 562 } |
| 563 } |
| 564 } |
| 565 } |
| 566 } |
| 567 |
| 568 void BrowserThemePack::GenerateMissingColors( |
| 569 std::map<int, SkColor>* colors) { |
| 570 // Generate link colors, if missing. (See GetColor()). |
| 571 if (!colors->count(BrowserThemeProvider::COLOR_NTP_HEADER) && |
| 572 colors->count(BrowserThemeProvider::COLOR_NTP_SECTION)) { |
| 573 (*colors)[BrowserThemeProvider::COLOR_NTP_HEADER] = |
| 574 (*colors)[BrowserThemeProvider::COLOR_NTP_SECTION]; |
| 575 } |
| 576 |
| 577 if (!colors->count(BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE) && |
| 578 colors->count(BrowserThemeProvider::COLOR_NTP_SECTION_LINK)) { |
| 579 SkColor color_section_link = |
| 580 (*colors)[BrowserThemeProvider::COLOR_NTP_SECTION_LINK]; |
| 581 (*colors)[BrowserThemeProvider::COLOR_NTP_SECTION_LINK_UNDERLINE] = |
| 582 SkColorSetA(color_section_link, SkColorGetA(color_section_link) / 3); |
| 583 } |
| 584 |
| 585 if (!colors->count(BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE) && |
| 586 colors->count(BrowserThemeProvider::COLOR_NTP_LINK)) { |
| 587 SkColor color_link = (*colors)[BrowserThemeProvider::COLOR_NTP_LINK]; |
| 588 (*colors)[BrowserThemeProvider::COLOR_NTP_LINK_UNDERLINE] = |
| 589 SkColorSetA(color_link, SkColorGetA(color_link) / 3); |
| 590 } |
| 591 |
| 592 // Generate frame colors, if missing. (See GenerateFrameColors()). |
| 593 SkColor frame; |
| 594 std::map<int, SkColor>::const_iterator it = |
| 595 colors->find(BrowserThemeProvider::COLOR_FRAME); |
| 596 if (it != colors->end()) { |
| 597 frame = it->second; |
| 598 } else { |
| 599 frame = BrowserThemeProvider::GetDefaultColor( |
| 600 BrowserThemeProvider::COLOR_FRAME); |
| 601 } |
| 602 |
| 603 if (!colors->count(BrowserThemeProvider::COLOR_FRAME)) { |
| 604 (*colors)[BrowserThemeProvider::COLOR_FRAME] = |
| 605 HSLShift(frame, GetTintInternal(BrowserThemeProvider::TINT_FRAME)); |
| 606 } |
| 607 if (!colors->count(BrowserThemeProvider::COLOR_FRAME_INACTIVE)) { |
| 608 (*colors)[BrowserThemeProvider::COLOR_FRAME_INACTIVE] = |
| 609 HSLShift(frame, GetTintInternal( |
| 610 BrowserThemeProvider::TINT_FRAME_INACTIVE)); |
| 611 } |
| 612 if (!colors->count(BrowserThemeProvider::COLOR_FRAME_INCOGNITO)) { |
| 613 (*colors)[BrowserThemeProvider::COLOR_FRAME_INCOGNITO] = |
| 614 HSLShift(frame, GetTintInternal( |
| 615 BrowserThemeProvider::TINT_FRAME_INCOGNITO)); |
| 616 } |
| 617 if (!colors->count(BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE)) { |
| 618 (*colors)[BrowserThemeProvider::COLOR_FRAME_INCOGNITO_INACTIVE] = |
| 619 HSLShift(frame, GetTintInternal( |
| 620 BrowserThemeProvider::TINT_FRAME_INCOGNITO_INACTIVE)); |
| 621 } |
| 622 } |
| 623 |
| 624 void BrowserThemePack::BuildDisplayPropertiesFromJSON( |
| 625 DictionaryValue* display_properties_value) { |
| 626 display_properties_ = new DisplayPropertyPair[kDisplayPropertySize]; |
| 627 for (int i = 0; i < kDisplayPropertySize; ++i) { |
| 628 display_properties_[i].id = -1; |
| 629 display_properties_[i].property = 0; |
| 630 } |
| 631 |
| 632 std::map<int, int> temp_properties; |
| 633 for (DictionaryValue::key_iterator iter( |
| 634 display_properties_value->begin_keys()); |
| 635 iter != display_properties_value->end_keys(); ++iter) { |
| 636 int property_id = GetIntForString(WideToUTF8(*iter), kDisplayProperties); |
| 637 switch (property_id) { |
| 638 case BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT: { |
| 639 std::string val; |
| 640 if (display_properties_value->GetString(*iter, &val)) { |
| 641 temp_properties[BrowserThemeProvider::NTP_BACKGROUND_ALIGNMENT] = |
| 642 BrowserThemeProvider::StringToAlignment(val); |
| 643 } |
| 644 break; |
| 645 } |
| 646 case BrowserThemeProvider::NTP_BACKGROUND_TILING: { |
| 647 std::string val; |
| 648 if (display_properties_value->GetString(*iter, &val)) { |
| 649 temp_properties[BrowserThemeProvider::NTP_BACKGROUND_TILING] = |
| 650 GetIntForString(val, kTilingStrings); |
| 651 } |
| 652 break; |
| 653 } |
| 654 case BrowserThemeProvider::NTP_LOGO_ALTERNATE: { |
| 655 int val = 0; |
| 656 if (display_properties_value->GetInteger(*iter, &val)) |
| 657 temp_properties[BrowserThemeProvider::NTP_LOGO_ALTERNATE] = val; |
| 658 break; |
| 659 } |
| 660 } |
| 661 } |
| 662 |
| 663 // Copy data from the intermediary data structure to the array. |
| 664 int count = 0; |
| 665 for (std::map<int, int>::const_iterator it = temp_properties.begin(); |
| 666 it != temp_properties.end() && count < kDisplayPropertySize; |
| 667 ++it, ++count) { |
| 668 display_properties_[count].id = it->first; |
| 669 display_properties_[count].property = it->second; |
| 670 } |
| 671 } |
| 672 |
| 673 void BrowserThemePack::ParseImageNamesFromJSON( |
| 674 DictionaryValue* images_value, |
| 675 FilePath images_path, |
| 676 std::map<int, FilePath>* file_paths) const { |
| 677 for (DictionaryValue::key_iterator iter(images_value->begin_keys()); |
| 678 iter != images_value->end_keys(); ++iter) { |
| 679 std::string val; |
| 680 if (images_value->GetString(*iter, &val)) { |
| 681 int id = ThemeResourcesUtil::GetId(WideToUTF8(*iter)); |
| 682 if (id != -1) |
| 683 (*file_paths)[id] = images_path.AppendASCII(val); |
| 684 } |
| 685 } |
| 686 } |
| 687 |
| 688 void BrowserThemePack::LoadRawBitmapsTo( |
| 689 const std::map<int, FilePath>& file_paths, |
| 690 ImageCache* raw_bitmaps) { |
| 691 for (std::map<int, FilePath>::const_iterator it = file_paths.begin(); |
| 692 it != file_paths.end(); ++it) { |
| 693 scoped_refptr<RefCountedMemory> raw_data(ReadFileData(it->second)); |
| 694 int id = it->first; |
| 695 |
| 696 // Some images need to go directly into |image_memory_|. No modification is |
| 697 // necessary or desirable. |
| 698 bool is_copyable = false; |
| 699 for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) { |
| 700 if (kPreloadIDs[i] == id) { |
| 701 is_copyable = true; |
| 702 break; |
| 703 } |
| 704 } |
| 705 |
| 706 if (is_copyable) { |
| 707 image_memory_[id] = raw_data; |
| 708 } else if (raw_data.get() && raw_data->size()) { |
| 709 // Decode the PNG. |
| 710 SkBitmap bitmap; |
| 711 if (gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), |
| 712 &bitmap)) { |
| 713 (*raw_bitmaps)[it->first] = new SkBitmap(bitmap); |
| 714 } else { |
| 715 NOTREACHED() << "Unable to decode theme image resource " << it->first; |
| 716 } |
| 717 } |
| 718 } |
| 719 } |
| 720 |
| 721 void BrowserThemePack::GenerateFrameImages(ImageCache* bitmaps) const { |
| 722 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 723 |
| 724 // Create all the output bitmaps in a separate cache and move them back into |
| 725 // the input bitmaps because there can be name collisions. |
| 726 ImageCache temp_output; |
| 727 |
| 728 for (size_t i = 0; i < arraysize(kFrameTintMap); ++i) { |
| 729 int id = kFrameTintMap[i].key; |
| 730 scoped_ptr<SkBitmap> frame; |
| 731 // If there's no frame image provided for the specified id, then load |
| 732 // the default provided frame. If that's not provided, skip this whole |
| 733 // thing and just use the default images. |
| 734 int base_id; |
| 735 |
| 736 if (id == IDR_THEME_FRAME_INCOGNITO_INACTIVE) { |
| 737 base_id = bitmaps->count(IDR_THEME_FRAME_INCOGNITO) ? |
| 738 IDR_THEME_FRAME_INCOGNITO : IDR_THEME_FRAME; |
| 739 } else if (id == IDR_THEME_FRAME_OVERLAY_INACTIVE) { |
| 740 base_id = IDR_THEME_FRAME_OVERLAY; |
| 741 } else if (id == IDR_THEME_FRAME_INACTIVE) { |
| 742 base_id = IDR_THEME_FRAME; |
| 743 } else if (id == IDR_THEME_FRAME_INCOGNITO && |
| 744 !bitmaps->count(IDR_THEME_FRAME_INCOGNITO)) { |
| 745 base_id = IDR_THEME_FRAME; |
| 746 } else { |
| 747 base_id = id; |
| 748 } |
| 749 |
| 750 if (bitmaps->count(id)) { |
| 751 frame.reset(new SkBitmap(*(*bitmaps)[id])); |
| 752 } else if (base_id != id && bitmaps->count(base_id)) { |
| 753 frame.reset(new SkBitmap(*(*bitmaps)[base_id])); |
| 754 } else if (base_id == IDR_THEME_FRAME_OVERLAY && |
| 755 bitmaps->count(IDR_THEME_FRAME)) { |
| 756 // If there is no theme overlay, don't tint the default frame, |
| 757 // because it will overwrite the custom frame image when we cache and |
| 758 // reload from disk. |
| 759 frame.reset(NULL); |
| 760 } else { |
| 761 // If the theme doesn't specify an image, then apply the tint to |
| 762 // the default frame. |
| 763 frame.reset(new SkBitmap(*rb.GetBitmapNamed(IDR_THEME_FRAME))); |
| 764 } |
| 765 |
| 766 if (frame.get()) { |
| 767 temp_output[id] = new SkBitmap( |
| 768 SkBitmapOperations::CreateHSLShiftedBitmap( |
| 769 *frame, GetTintInternal(kFrameTintMap[i].value))); |
| 770 } |
| 771 } |
| 772 |
| 773 MergeImageCaches(temp_output, bitmaps); |
| 774 } |
| 775 |
| 776 void BrowserThemePack::GenerateTintedButtons( |
| 777 color_utils::HSL button_tint, |
| 778 ImageCache* processed_bitmaps) const { |
| 779 if (button_tint.h != -1 || button_tint.s != -1 || button_tint.l != -1) { |
| 780 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 781 for (size_t i = 0; i < arraysize(kToolbarButtonIDs); ++i) { |
| 782 scoped_ptr<SkBitmap> button( |
| 783 new SkBitmap(*rb.GetBitmapNamed(kToolbarButtonIDs[i]))); |
| 784 (*processed_bitmaps)[kToolbarButtonIDs[i]] = new SkBitmap( |
| 785 SkBitmapOperations::CreateHSLShiftedBitmap(*button, button_tint)); |
| 786 } |
| 787 } |
| 788 } |
| 789 |
| 790 void BrowserThemePack::GenerateTabBackgroundImages(ImageCache* bitmaps) const { |
| 791 ImageCache temp_output; |
| 792 for (size_t i = 0; i < arraysize(kTabBackgroundMap); ++i) { |
| 793 int id = kTabBackgroundMap[i].key; |
| 794 int base_id = kTabBackgroundMap[i].value; |
| 795 |
| 796 // We only need to generate the background tab images if we were provided |
| 797 // with a IDR_THEME_FRAME. |
| 798 ImageCache::const_iterator it = bitmaps->find(base_id); |
| 799 if (it != bitmaps->end()) { |
| 800 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap( |
| 801 *(it->second), GetTintInternal( |
| 802 BrowserThemeProvider::TINT_BACKGROUND_TAB)); |
| 803 int vertical_offset = bitmaps->count(id) ? kRestoredTabVerticalOffset : 0; |
| 804 SkBitmap* bg_tab = new SkBitmap(SkBitmapOperations::CreateTiledBitmap( |
| 805 bg_tint, 0, vertical_offset, bg_tint.width(), bg_tint.height())); |
| 806 |
| 807 // If they've provided a custom image, overlay it. |
| 808 ImageCache::const_iterator overlay_it = bitmaps->find(id); |
| 809 if (overlay_it != bitmaps->end()) { |
| 810 SkBitmap* overlay = overlay_it->second; |
| 811 SkCanvas canvas(*bg_tab); |
| 812 for (int x = 0; x < bg_tab->width(); x += overlay->width()) |
| 813 canvas.drawBitmap(*overlay, static_cast<SkScalar>(x), 0, NULL); |
| 814 } |
| 815 |
| 816 temp_output[id] = bg_tab; |
| 817 } |
| 818 } |
| 819 |
| 820 MergeImageCaches(temp_output, bitmaps); |
| 821 } |
| 822 |
| 823 void BrowserThemePack::RepackImageCacheToImageMemory() { |
| 824 // TODO(erg): This shouldn't be done on the main thread. This should be done |
| 825 // during the WriteToDisk() method, but will require some tricky locking. |
| 826 for (ImageCache::const_iterator it = image_cache_.begin(); |
| 827 it != image_cache_.end(); ++it) { |
| 828 std::vector<unsigned char> image_data; |
| 829 if (!gfx::PNGCodec::EncodeBGRASkBitmap(*(it->second), false, &image_data)) { |
| 830 NOTREACHED() << "Image file for resource " << it->first |
| 831 << " could not be encoded."; |
| 832 } else { |
| 833 image_memory_[it->first] = RefCountedBytes::TakeVector(&image_data); |
| 834 } |
| 835 } |
| 836 } |
| 837 |
| 838 void BrowserThemePack::MergeImageCaches( |
| 839 const ImageCache& source, ImageCache* destination) const { |
| 840 |
| 841 for (ImageCache::const_iterator it = source.begin(); it != source.end(); |
| 842 ++it) { |
| 843 ImageCache::const_iterator bitmap_it = destination->find(it->first); |
| 844 if (bitmap_it != destination->end()) |
| 845 delete bitmap_it->second; |
| 846 |
| 847 (*destination)[it->first] = it->second; |
| 848 } |
| 849 } |
| 850 |
| 851 color_utils::HSL BrowserThemePack::GetTintInternal(int id) const { |
| 852 if (tints_) { |
| 853 for (int i = 0; i < kTintArraySize; ++i) { |
| 854 if (tints_[i].id == id) { |
| 855 color_utils::HSL hsl; |
| 856 hsl.h = tints_[i].h; |
| 857 hsl.s = tints_[i].s; |
| 858 hsl.l = tints_[i].l; |
| 859 return hsl; |
| 860 } |
| 861 } |
| 862 } |
| 863 |
| 864 return BrowserThemeProvider::GetDefaultTint(id); |
| 865 } |
OLD | NEW |