Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 // exists. | 49 // exists. |
| 50 g_object_ref(pixbuf); | 50 g_object_ref(pixbuf); |
| 51 return pixbuf; | 51 return pixbuf; |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 | 54 |
| 55 FilePath GetResourcesPakFilePath(const std::string& pak_name) { | 55 FilePath GetResourcesPakFilePath(const std::string& pak_name) { |
| 56 FilePath path; | 56 FilePath path; |
| 57 if (PathService::Get(base::DIR_MODULE, &path)) | 57 if (PathService::Get(base::DIR_MODULE, &path)) |
| 58 return path.AppendASCII(pak_name.c_str()); | 58 return path.AppendASCII(pak_name.c_str()); |
| 59 return FilePath(); | 59 |
| 60 // Return just the name of the pack file. | |
| 61 return FilePath(pak_name.c_str()); | |
| 60 } | 62 } |
| 61 | 63 |
| 62 } // namespace | 64 } // namespace |
| 63 | 65 |
| 64 void ResourceBundle::LoadCommonResources() { | 66 void ResourceBundle::LoadCommonResources() { |
| 65 AddDataPack(GetResourcesPakFilePath("chrome.pak"), | 67 AddDataPack(GetResourcesPakFilePath("chrome.pak"), |
| 66 ResourceHandle::kScaleFactor100x); | 68 ResourceHandle::kScaleFactor100x); |
| 67 AddDataPack(GetResourcesPakFilePath("theme_resources_standard.pak"), | 69 AddDataPack(GetResourcesPakFilePath("theme_resources_standard.pak"), |
| 68 ResourceHandle::kScaleFactor100x); | 70 ResourceHandle::kScaleFactor100x); |
| 69 AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), | 71 AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), |
| 70 ResourceHandle::kScaleFactor100x); | 72 ResourceHandle::kScaleFactor100x); |
| 71 } | 73 } |
| 72 | 74 |
| 73 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { | 75 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { |
|
Robert Sesek
2012/05/03 19:53:57
const?
Marshall
2012/05/04 14:41:03
Done.
| |
| 74 // Use the negative |resource_id| for the key for BIDI-aware images. | 76 // Use the negative |resource_id| for the key for BIDI-aware images. |
| 75 int key = rtl == RTL_ENABLED ? -resource_id : resource_id; | 77 int key = rtl == RTL_ENABLED ? -resource_id : resource_id; |
| 76 | 78 |
| 77 // Check to see if the image is already in the cache. | 79 // Check to see if the image is already in the cache. |
| 78 { | 80 { |
| 79 base::AutoLock lock_scope(*images_and_fonts_lock_); | 81 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 80 ImageMap::const_iterator found = images_.find(key); | 82 ImageMap::const_iterator found = images_.find(key); |
| 81 if (found != images_.end()) | 83 if (found != images_.end()) |
| 82 return *found->second; | 84 return *found->second; |
| 83 } | 85 } |
| 84 | 86 |
| 85 scoped_refptr<base::RefCountedStaticMemory> data( | 87 gfx::Image image; |
| 86 LoadDataResourceBytes(resource_id)); | 88 if (delegate_) |
| 87 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl == RTL_ENABLED); | 89 image = delegate_->GetNativeImageNamed(resource_id, rtl); |
| 88 | 90 |
| 89 // The load was successful, so cache the image. | 91 if (image.IsEmpty()) { |
| 90 if (pixbuf) { | 92 scoped_refptr<base::RefCountedStaticMemory> data( |
| 91 base::AutoLock lock_scope(*images_and_fonts_lock_); | 93 LoadDataResourceBytes(resource_id)); |
| 94 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl == RTL_ENABLED); | |
| 92 | 95 |
| 93 // Another thread raced the load and has already cached the image. | 96 if (!pixbuf) { |
| 94 if (images_.count(key)) { | 97 LOG(WARNING) << "Unable to load pixbuf with id " << resource_id; |
| 95 g_object_unref(pixbuf); | 98 NOTREACHED(); // Want to assert in debug mode. |
| 96 return *images_[key]; | 99 return *GetEmptyImage(); |
| 97 } | 100 } |
| 98 | 101 |
| 99 gfx::Image* image = new gfx::Image(pixbuf); // Takes ownership. | 102 image = gfx::Image(pixbuf); // Takes ownership. |
| 100 images_[key] = image; | |
| 101 return *image; | |
| 102 } | 103 } |
| 103 | 104 |
| 104 LOG(WARNING) << "Unable to pixbuf with id " << resource_id; | 105 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 105 NOTREACHED(); // Want to assert in debug mode. | 106 |
| 106 return *GetEmptyImage(); | 107 // Another thread raced the load and has already cached the image. |
| 108 if (images_.count(key)) | |
| 109 return images_[key]; | |
| 110 | |
| 111 images_[key] = image; | |
| 112 return images_[key]; | |
| 107 } | 113 } |
| 108 | 114 |
| 109 } // namespace ui | 115 } // namespace ui |
| OLD | NEW |