OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browser_theme_provider.h" | 5 #include "chrome/browser/browser_theme_provider.h" |
6 | 6 |
7 #include "app/gfx/codec/png_codec.h" | 7 #include "app/gfx/codec/png_codec.h" |
8 #include "app/gfx/skbitmap_operations.h" | 8 #include "app/gfx/skbitmap_operations.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 // SaveThemeBitmap describing the process by which an original image is | 440 // SaveThemeBitmap describing the process by which an original image is |
441 // tagged. | 441 // tagged. |
442 ImageMap::const_iterator images_iter = images_.find(id); | 442 ImageMap::const_iterator images_iter = images_.find(id); |
443 ResourceNameMap::const_iterator names_iter = resource_names_.find(id); | 443 ResourceNameMap::const_iterator names_iter = resource_names_.find(id); |
444 if ((images_iter == images_.end()) || (names_iter == resource_names_.end())) | 444 if ((images_iter == images_.end()) || (names_iter == resource_names_.end())) |
445 return false; | 445 return false; |
446 return !EndsWith(UTF8ToWide(images_iter->second), | 446 return !EndsWith(UTF8ToWide(images_iter->second), |
447 UTF8ToWide(names_iter->second), false); | 447 UTF8ToWide(names_iter->second), false); |
448 } | 448 } |
449 | 449 |
450 bool BrowserThemeProvider::GetRawData( | 450 RefCountedMemory* BrowserThemeProvider::GetRawData(int id) const { |
451 int id, | |
452 std::vector<unsigned char>* raw_data) const { | |
453 // Check to see whether we should substitute some images. | 451 // Check to see whether we should substitute some images. |
454 int ntp_alternate; | 452 int ntp_alternate; |
455 GetDisplayProperty(NTP_LOGO_ALTERNATE, &ntp_alternate); | 453 GetDisplayProperty(NTP_LOGO_ALTERNATE, &ntp_alternate); |
456 if (id == IDR_PRODUCT_LOGO && ntp_alternate != 0) | 454 if (id == IDR_PRODUCT_LOGO && ntp_alternate != 0) |
457 id = IDR_PRODUCT_LOGO_WHITE; | 455 id = IDR_PRODUCT_LOGO_WHITE; |
458 | 456 |
459 RawDataMap::const_iterator data_iter = raw_data_.find(id); | 457 RawDataMap::const_iterator data_iter = raw_data_.find(id); |
460 if (data_iter != raw_data_.end()) { | 458 if (data_iter != raw_data_.end()) { |
461 *raw_data = data_iter->second; | 459 return data_iter->second; |
462 return true; | |
463 } | 460 } |
brettw
2009/10/16 23:03:12
You should also remove the {} for this conditional
| |
464 | 461 |
465 if (!ReadThemeFileData(id, raw_data) && | 462 RefCountedMemory* data = ReadThemeFileData(id); |
466 !rb_.LoadImageResourceBytes(id, raw_data)) | 463 if (!data) |
467 return false; | 464 data = rb_.LoadImageResourceBytes(id); |
465 if (!data) | |
466 return NULL; | |
468 | 467 |
469 raw_data_[id] = *raw_data; | 468 raw_data_[id] = data; |
470 return true; | 469 return data; |
471 } | 470 } |
472 | 471 |
473 void BrowserThemeProvider::SetTheme(Extension* extension) { | 472 void BrowserThemeProvider::SetTheme(Extension* extension) { |
474 // Clear our image cache. | 473 // Clear our image cache. |
475 ClearCaches(); | 474 ClearCaches(); |
476 | 475 |
477 DCHECK(extension); | 476 DCHECK(extension); |
478 DCHECK(extension->IsTheme()); | 477 DCHECK(extension->IsTheme()); |
479 SetImageData(extension->GetThemeImages(), | 478 SetImageData(extension->GetThemeImages(), |
480 extension->path()); | 479 extension->path()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 ClearAllThemeData(); | 514 ClearAllThemeData(); |
516 NotifyThemeChanged(); | 515 NotifyThemeChanged(); |
517 UserMetrics::RecordAction(L"Themes_Reset", profile_); | 516 UserMetrics::RecordAction(L"Themes_Reset", profile_); |
518 } | 517 } |
519 | 518 |
520 std::string BrowserThemeProvider::GetThemeID() const { | 519 std::string BrowserThemeProvider::GetThemeID() const { |
521 std::wstring id = profile_->GetPrefs()->GetString(prefs::kCurrentThemeID); | 520 std::wstring id = profile_->GetPrefs()->GetString(prefs::kCurrentThemeID); |
522 return WideToUTF8(id); | 521 return WideToUTF8(id); |
523 } | 522 } |
524 | 523 |
525 bool BrowserThemeProvider::ReadThemeFileData( | 524 RefCountedMemory* BrowserThemeProvider::ReadThemeFileData(int id) const { |
526 int id, std::vector<unsigned char>* raw_data) const { | |
527 ImageMap::const_iterator images_iter = images_.find(id); | 525 ImageMap::const_iterator images_iter = images_.find(id); |
528 if (images_iter != images_.end()) { | 526 if (images_iter != images_.end()) { |
529 // First check to see if we have a registered theme extension and whether | 527 // First check to see if we have a registered theme extension and whether |
530 // it can handle this resource. | 528 // it can handle this resource. |
531 #if defined(OS_WIN) | 529 #if defined(OS_WIN) |
532 FilePath path = FilePath(UTF8ToWide(images_iter->second)); | 530 FilePath path = FilePath(UTF8ToWide(images_iter->second)); |
533 #else | 531 #else |
534 FilePath path = FilePath(images_iter->second); | 532 FilePath path = FilePath(images_iter->second); |
535 #endif | 533 #endif |
536 if (!path.empty()) { | 534 if (!path.empty()) { |
537 net::FileStream file; | 535 net::FileStream file; |
538 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 536 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
539 if (file.Open(path, flags) == net::OK) { | 537 if (file.Open(path, flags) == net::OK) { |
540 int64 avail = file.Available(); | 538 int64 avail = file.Available(); |
541 if (avail > 0 && avail < INT_MAX) { | 539 if (avail > 0 && avail < INT_MAX) { |
542 size_t size = static_cast<size_t>(avail); | 540 size_t size = static_cast<size_t>(avail); |
543 raw_data->resize(size); | 541 std::vector<unsigned char> raw_data; |
544 char* data = reinterpret_cast<char*>(&(raw_data->front())); | 542 raw_data.resize(size); |
543 char* data = reinterpret_cast<char*>(&(raw_data.front())); | |
545 if (file.ReadUntilComplete(data, size) == avail) | 544 if (file.ReadUntilComplete(data, size) == avail) |
546 return true; | 545 return RefCountedBytes::TakeVector(&raw_data); |
547 } | 546 } |
548 } | 547 } |
549 } | 548 } |
550 } | 549 } |
551 | 550 |
552 return false; | 551 return NULL; |
553 } | 552 } |
554 | 553 |
555 // static | 554 // static |
556 std::string BrowserThemeProvider::AlignmentToString(int alignment) { | 555 std::string BrowserThemeProvider::AlignmentToString(int alignment) { |
557 // Convert from an AlignmentProperty back into a string. | 556 // Convert from an AlignmentProperty back into a string. |
558 std::string vertical_string; | 557 std::string vertical_string; |
559 std::string horizontal_string; | 558 std::string horizontal_string; |
560 | 559 |
561 if (alignment & BrowserThemeProvider::ALIGN_TOP) | 560 if (alignment & BrowserThemeProvider::ALIGN_TOP) |
562 vertical_string = kAlignmentTop; | 561 vertical_string = kAlignmentTop; |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
796 Source<BrowserThemeProvider>(this), | 795 Source<BrowserThemeProvider>(this), |
797 NotificationService::NoDetails()); | 796 NotificationService::NoDetails()); |
798 } | 797 } |
799 | 798 |
800 SkBitmap* BrowserThemeProvider::LoadThemeBitmap(int id) const { | 799 SkBitmap* BrowserThemeProvider::LoadThemeBitmap(int id) const { |
801 DCHECK(CalledOnValidThread()); | 800 DCHECK(CalledOnValidThread()); |
802 | 801 |
803 if (!themeable_images.count(id)) | 802 if (!themeable_images.count(id)) |
804 return NULL; | 803 return NULL; |
805 | 804 |
806 // Attempt to find the image in our theme bundle. | 805 scoped_refptr<RefCountedMemory> raw_data; |
807 std::vector<unsigned char> raw_data, png_data; | 806 |
808 if (ReadThemeFileData(id, &raw_data)) { | 807 // We special case images related to the NTP so we first try raw data. Why? |
808 // Because the DOMUI stuff uses that interface and otherwise we would be | |
809 // loading big images twice. Ouch. So either we prime the cache for when | |
810 // DOMUIThemeSource requests our image, or we take advantage of the already | |
811 // loaded data, saving a trip to disk. | |
brettw
2009/10/16 23:03:12
In person, I asked for some clarification of the c
| |
812 if (id == IDR_THEME_NTP_BACKGROUND) | |
813 raw_data = GetRawData(id); | |
814 | |
815 if (!raw_data) | |
816 raw_data = ReadThemeFileData(id); | |
817 | |
818 if (raw_data) { | |
819 std::vector<unsigned char> png_data; | |
820 | |
809 // Decode the PNG. | 821 // Decode the PNG. |
810 int image_width = 0; | 822 int image_width = 0; |
811 int image_height = 0; | 823 int image_height = 0; |
812 | 824 |
813 if (!gfx::PNGCodec::Decode(&raw_data.front(), raw_data.size(), | 825 if (!gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), |
814 gfx::PNGCodec::FORMAT_BGRA, &png_data, | 826 gfx::PNGCodec::FORMAT_BGRA, &png_data, |
815 &image_width, &image_height)) { | 827 &image_width, &image_height)) { |
816 NOTREACHED() << "Unable to decode theme image resource " << id; | 828 NOTREACHED() << "Unable to decode theme image resource " << id; |
817 return NULL; | 829 return NULL; |
818 } | 830 } |
819 | 831 |
820 return gfx::PNGCodec::CreateSkBitmapFromBGRAFormat(png_data, | 832 return gfx::PNGCodec::CreateSkBitmapFromBGRAFormat(png_data, |
821 image_width, | 833 image_width, |
822 image_height); | 834 image_height); |
823 } else { | 835 } else { |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1294 } | 1306 } |
1295 | 1307 |
1296 bool BrowserThemeProvider::ShouldTintFrames() const { | 1308 bool BrowserThemeProvider::ShouldTintFrames() const { |
1297 return (HasCustomImage(IDR_THEME_FRAME) || | 1309 return (HasCustomImage(IDR_THEME_FRAME) || |
1298 tints_.count(GetTintKey(TINT_BACKGROUND_TAB)) || | 1310 tints_.count(GetTintKey(TINT_BACKGROUND_TAB)) || |
1299 tints_.count(GetTintKey(TINT_FRAME)) || | 1311 tints_.count(GetTintKey(TINT_FRAME)) || |
1300 tints_.count(GetTintKey(TINT_FRAME_INACTIVE)) || | 1312 tints_.count(GetTintKey(TINT_FRAME_INACTIVE)) || |
1301 tints_.count(GetTintKey(TINT_FRAME_INCOGNITO)) || | 1313 tints_.count(GetTintKey(TINT_FRAME_INCOGNITO)) || |
1302 tints_.count(GetTintKey(TINT_FRAME_INCOGNITO_INACTIVE))); | 1314 tints_.count(GetTintKey(TINT_FRAME_INCOGNITO_INACTIVE))); |
1303 } | 1315 } |
OLD | NEW |