diff options
author | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-19 17:04:46 +0000 |
---|---|---|
committer | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-19 17:04:46 +0000 |
commit | 6de2799a47cbc201d943b32e0ac62555dc45f662 (patch) | |
tree | 6c64342df90783c5f334d370d6f8cb08472fcd7f /chrome | |
parent | 5f1b13bc6952eb89149716930e654c9d989b9e13 (diff) | |
download | chromium_src-6de2799a47cbc201d943b32e0ac62555dc45f662.zip chromium_src-6de2799a47cbc201d943b32e0ac62555dc45f662.tar.gz chromium_src-6de2799a47cbc201d943b32e0ac62555dc45f662.tar.bz2 |
First fix to minimize copying of image data.
This is the first of multiple patches that clean up handling of memory
regarding images. Previously, the code did several memcpy()s or
equivalents. This change:
- Creates an abstract interface RefCountedMemory which provides access to the
front() of a memory range and the size() of it. It is a RefCountedThreadSafe.
- Adds a RefCountedStaticMemory class which isa RefCountedMemory.
- Pushes RefCountedBytes up into base/ from chrome/ and make it conform to
RefCountedMemory.
- Have ResourceBundle return RefCountedStaticMemory to the mmaped() or DLL
loaded resources instead of memcpy()ing them.
- General cleanups to minimize copies in constructing RefCountedBytes.
- Use the above consistent interface in the BrowserThemeProvider, along with
special casing the loading of the new tab page background.
This patch is mostly cleanups and there should only be a slight performance
gain if any. Most of the real speedups should come in subsequent patches.
BUG=http://crbug.com/24493
TEST=Slightly faster on Perf bot; does not introduce crashes.
Review URL: http://codereview.chromium.org/288005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29412 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
25 files changed, 112 insertions, 93 deletions
diff --git a/chrome/browser/browser_theme_provider.cc b/chrome/browser/browser_theme_provider.cc index f234d84..0adfa6b 100644 --- a/chrome/browser/browser_theme_provider.cc +++ b/chrome/browser/browser_theme_provider.cc @@ -447,9 +447,7 @@ bool BrowserThemeProvider::HasCustomImage(int id) const { UTF8ToWide(names_iter->second), false); } -bool BrowserThemeProvider::GetRawData( - int id, - std::vector<unsigned char>* raw_data) const { +RefCountedMemory* BrowserThemeProvider::GetRawData(int id) const { // Check to see whether we should substitute some images. int ntp_alternate; GetDisplayProperty(NTP_LOGO_ALTERNATE, &ntp_alternate); @@ -457,17 +455,17 @@ bool BrowserThemeProvider::GetRawData( id = IDR_PRODUCT_LOGO_WHITE; RawDataMap::const_iterator data_iter = raw_data_.find(id); - if (data_iter != raw_data_.end()) { - *raw_data = data_iter->second; - return true; - } + if (data_iter != raw_data_.end()) + return data_iter->second; - if (!ReadThemeFileData(id, raw_data) && - !rb_.LoadImageResourceBytes(id, raw_data)) - return false; + RefCountedMemory* data = ReadThemeFileData(id); + if (!data) + data = rb_.LoadImageResourceBytes(id); + if (!data) + return NULL; - raw_data_[id] = *raw_data; - return true; + raw_data_[id] = data; + return data; } void BrowserThemeProvider::SetTheme(Extension* extension) { @@ -522,8 +520,7 @@ std::string BrowserThemeProvider::GetThemeID() const { return WideToUTF8(id); } -bool BrowserThemeProvider::ReadThemeFileData( - int id, std::vector<unsigned char>* raw_data) const { +RefCountedMemory* BrowserThemeProvider::ReadThemeFileData(int id) const { ImageMap::const_iterator images_iter = images_.find(id); if (images_iter != images_.end()) { // First check to see if we have a registered theme extension and whether @@ -540,16 +537,17 @@ bool BrowserThemeProvider::ReadThemeFileData( int64 avail = file.Available(); if (avail > 0 && avail < INT_MAX) { size_t size = static_cast<size_t>(avail); - raw_data->resize(size); - char* data = reinterpret_cast<char*>(&(raw_data->front())); + std::vector<unsigned char> raw_data; + raw_data.resize(size); + char* data = reinterpret_cast<char*>(&(raw_data.front())); if (file.ReadUntilComplete(data, size) == avail) - return true; + return RefCountedBytes::TakeVector(&raw_data); } } } } - return false; + return NULL; } // static @@ -803,14 +801,30 @@ SkBitmap* BrowserThemeProvider::LoadThemeBitmap(int id) const { if (!themeable_images.count(id)) return NULL; - // Attempt to find the image in our theme bundle. - std::vector<unsigned char> raw_data, png_data; - if (ReadThemeFileData(id, &raw_data)) { + scoped_refptr<RefCountedMemory> raw_data; + + // We special case images related to the NTP so we first try raw data. Why? + // Because the DOMUI stuff uses that interface to return raw PNG data instead + // of the normal theme interface which returns SkBitmaps. GetRawData() also + // caches the PNG data so it opens new tab pages faster. If we didn't try and + // otherwise we would be loading big images twice, once through GetRawData() + // and once here. Ouch. So either we prime the GetRawData() cache for when + // DOMUIThemeSource requests our image, or we take advantage of the already + // loaded data, saving a trip to disk. + if (id == IDR_THEME_NTP_BACKGROUND) + raw_data = GetRawData(id); + + if (!raw_data) + raw_data = ReadThemeFileData(id); + + if (raw_data) { + std::vector<unsigned char> png_data; + // Decode the PNG. int image_width = 0; int image_height = 0; - if (!gfx::PNGCodec::Decode(&raw_data.front(), raw_data.size(), + if (!gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), gfx::PNGCodec::FORMAT_BGRA, &png_data, &image_width, &image_height)) { NOTREACHED() << "Unable to decode theme image resource " << id; diff --git a/chrome/browser/browser_theme_provider.h b/chrome/browser/browser_theme_provider.h index 02fec9b..f54c3ca 100644 --- a/chrome/browser/browser_theme_provider.h +++ b/chrome/browser/browser_theme_provider.h @@ -162,7 +162,7 @@ class BrowserThemeProvider : public NonThreadSafe, virtual bool GetDisplayProperty(int id, int* result) const; virtual bool ShouldUseNativeFrame() const; virtual bool HasCustomImage(int id) const; - virtual bool GetRawData(int id, std::vector<unsigned char>* raw_data) const; + virtual RefCountedMemory* GetRawData(int id) const; #if defined(OS_LINUX) virtual GdkPixbuf* GetPixbufNamed(int id) const; virtual GdkPixbuf* GetRTLEnabledPixbufNamed(int id) const; @@ -188,7 +188,7 @@ class BrowserThemeProvider : public NonThreadSafe, // Reads the image data from the theme file into the specified vector. Returns // true on success. - bool ReadThemeFileData(int id, std::vector<unsigned char>* raw_data) const; + RefCountedMemory* ReadThemeFileData(int id) const; // Convert a bitfield alignment into a string like "top left". Public so that // it can be used to generate CSS values. Takes a bitfield of AlignmentMasks. @@ -277,7 +277,7 @@ class BrowserThemeProvider : public NonThreadSafe, typedef std::map<const std::string, SkColor> ColorMap; typedef std::map<const std::string, color_utils::HSL> TintMap; typedef std::map<const std::string, int> DisplayPropertyMap; - typedef std::map<const int, std::vector<unsigned char> > RawDataMap; + typedef std::map<const int, scoped_refptr<RefCountedMemory> > RawDataMap; typedef std::map<const int, std::string> ResourceNameMap; // Returns the string key for the given tint |id| TINT_* enum value. diff --git a/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm b/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm index 37c61ae..a6414f4 100644 --- a/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm +++ b/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm @@ -33,7 +33,7 @@ class MockThemeProvider : public ThemeProvider { MOCK_CONST_METHOD2(GetDisplayProperty, bool(int, int*)); MOCK_CONST_METHOD0(ShouldUseNativeFrame, bool()); MOCK_CONST_METHOD1(HasCustomImage, bool(int)); - MOCK_CONST_METHOD2(GetRawData, bool(int, std::vector<unsigned char>*)); + MOCK_CONST_METHOD1(GetRawData, RefCountedMemory*(int)); // OSX stuff MOCK_CONST_METHOD1(GetNSImageNamed, NSImage*(int)); diff --git a/chrome/browser/dom_ui/chrome_url_data_manager.cc b/chrome/browser/dom_ui/chrome_url_data_manager.cc index be3c856..d937e7a 100644 --- a/chrome/browser/dom_ui/chrome_url_data_manager.cc +++ b/chrome/browser/dom_ui/chrome_url_data_manager.cc @@ -50,7 +50,7 @@ class URLRequestChromeJob : public URLRequestJob { // Called by ChromeURLDataManager to notify us that the data blob is ready // for us. - void DataAvailable(RefCountedBytes* bytes); + void DataAvailable(RefCountedMemory* bytes); void SetMimeType(const std::string& mime_type) { mime_type_ = mime_type; @@ -66,7 +66,7 @@ class URLRequestChromeJob : public URLRequestJob { void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); // The actual data we're serving. NULL until it's been fetched. - scoped_refptr<RefCountedBytes> data_; + scoped_refptr<RefCountedMemory> data_; // The current offset into the data that we're handing off to our // callers via the Read interfaces. int data_offset_; @@ -258,7 +258,7 @@ void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { void ChromeURLDataManager::DataAvailable( RequestID request_id, - scoped_refptr<RefCountedBytes> bytes) { + scoped_refptr<RefCountedMemory> bytes) { // Forward this data on to the pending URLRequest, if it exists. PendingRequestMap::iterator i = pending_requests_.find(request_id); if (i != pending_requests_.end()) { @@ -272,11 +272,11 @@ void ChromeURLDataManager::DataAvailable( void ChromeURLDataManager::DataSource::SendResponse( RequestID request_id, - RefCountedBytes* bytes) { + RefCountedMemory* bytes) { ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(FROM_HERE, NewRunnableMethod(&chrome_url_data_manager, &ChromeURLDataManager::DataAvailable, - request_id, scoped_refptr<RefCountedBytes>(bytes))); + request_id, scoped_refptr<RefCountedMemory>(bytes))); } MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( @@ -368,7 +368,7 @@ bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { return !mime_type_.empty(); } -void URLRequestChromeJob::DataAvailable(RefCountedBytes* bytes) { +void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { if (bytes) { // The request completed, and we have all the data. // Clear any IO pending status. @@ -406,11 +406,11 @@ bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size, void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read) { - int remaining = static_cast<int>(data_->data.size()) - data_offset_; + int remaining = static_cast<int>(data_->size()) - data_offset_; if (buf_size > remaining) buf_size = remaining; if (buf_size > 0) { - memcpy(buf->data(), &data_->data[0] + data_offset_, buf_size); + memcpy(buf->data(), data_->front() + data_offset_, buf_size); data_offset_ += buf_size; } *bytes_read = buf_size; diff --git a/chrome/browser/dom_ui/chrome_url_data_manager.h b/chrome/browser/dom_ui/chrome_url_data_manager.h index 22b1ee4..bb1a82c 100644 --- a/chrome/browser/dom_ui/chrome_url_data_manager.h +++ b/chrome/browser/dom_ui/chrome_url_data_manager.h @@ -9,7 +9,7 @@ #include <string> #include "base/task.h" -#include "chrome/common/ref_counted_util.h" +#include "base/ref_counted_memory.h" class DictionaryValue; class FilePath; @@ -58,7 +58,7 @@ class ChromeURLDataManager { // Report that a request has resulted in the data |bytes|. // If the request can't be satisfied, pass NULL for |bytes| to indicate // the request is over. - void SendResponse(int request_id, RefCountedBytes* bytes); + void SendResponse(int request_id, RefCountedMemory* bytes); // Returns the MessageLoop on which the DataSource wishes to have // StartDataRequest called to handle the request for |path|. If the @@ -131,7 +131,7 @@ class ChromeURLDataManager { // Sent by Request::SendResponse. void DataAvailable(RequestID request_id, - scoped_refptr<RefCountedBytes> bytes); + scoped_refptr<RefCountedMemory> bytes); // File sources of data, keyed by source name (e.g. "inspector"). typedef std::map<std::string, FilePath> FileSourceMap; diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc index cdfbd7f..ad98860 100644 --- a/chrome/browser/dom_ui/dom_ui_factory.cc +++ b/chrome/browser/dom_ui/dom_ui_factory.cc @@ -120,16 +120,16 @@ DOMUI* DOMUIFactory::CreateDOMUIForURL(TabContents* tab_contents, } // static -bool DOMUIFactory::GetFaviconResourceBytes(const GURL& page_url, - std::vector<unsigned char>* bytes) { +RefCountedMemory* DOMUIFactory::GetFaviconResourceBytes( + const GURL& page_url) { if (!HasDOMUIScheme(page_url)) - return false; + return NULL; if (page_url.host() == chrome::kChromeUIHistoryHost) - return HistoryUI::GetFaviconResourceBytes(bytes); + return HistoryUI::GetFaviconResourceBytes(); if (page_url.host() == chrome::kChromeUIDownloadsHost) - return DownloadsUI::GetFaviconResourceBytes(bytes); + return DownloadsUI::GetFaviconResourceBytes(); - return false; + return NULL; } diff --git a/chrome/browser/dom_ui/dom_ui_factory.h b/chrome/browser/dom_ui/dom_ui_factory.h index 705708a..33c32cb 100644 --- a/chrome/browser/dom_ui/dom_ui_factory.h +++ b/chrome/browser/dom_ui/dom_ui_factory.h @@ -9,6 +9,7 @@ class DOMUI; class GURL; +class RefCountedMemory; class TabContents; // An opaque identifier used to identify a DOMUI. This can only be compared to @@ -42,8 +43,7 @@ class DOMUIFactory { // Gets the data for the favicon for a DOMUI page. Returns false if the DOMUI // does not have a favicon. - static bool GetFaviconResourceBytes(const GURL& page_url, - std::vector<unsigned char>* bytes); + static RefCountedMemory* GetFaviconResourceBytes(const GURL& page_url); private: // Class is for scoping only. diff --git a/chrome/browser/dom_ui/dom_ui_favicon_source.cc b/chrome/browser/dom_ui/dom_ui_favicon_source.cc index d117b784..536e175 100644 --- a/chrome/browser/dom_ui/dom_ui_favicon_source.cc +++ b/chrome/browser/dom_ui/dom_ui_favicon_source.cc @@ -54,9 +54,9 @@ void DOMUIFavIconSource::OnFavIconDataAvailable( SendResponse(request_id, data); } else { if (!default_favicon_.get()) { - default_favicon_ = new RefCountedBytes; - ResourceBundle::GetSharedInstance().LoadImageResourceBytes( - IDR_DEFAULT_FAVICON, &default_favicon_->data); + default_favicon_ = + ResourceBundle::GetSharedInstance().LoadImageResourceBytes( + IDR_DEFAULT_FAVICON); } SendResponse(request_id, default_favicon_); diff --git a/chrome/browser/dom_ui/dom_ui_favicon_source.h b/chrome/browser/dom_ui/dom_ui_favicon_source.h index 5e53754..6556c39 100644 --- a/chrome/browser/dom_ui/dom_ui_favicon_source.h +++ b/chrome/browser/dom_ui/dom_ui_favicon_source.h @@ -45,7 +45,7 @@ class DOMUIFavIconSource : public ChromeURLDataManager::DataSource { // Raw PNG representation of the favicon to show when the favicon // database doesn't have a favicon for a webpage. - scoped_refptr<RefCountedBytes> default_favicon_; + scoped_refptr<RefCountedMemory> default_favicon_; DISALLOW_COPY_AND_ASSIGN(DOMUIFavIconSource); }; diff --git a/chrome/browser/dom_ui/dom_ui_theme_source.cc b/chrome/browser/dom_ui/dom_ui_theme_source.cc index dfe9128..50e40a2 100644 --- a/chrome/browser/dom_ui/dom_ui_theme_source.cc +++ b/chrome/browser/dom_ui/dom_ui_theme_source.cc @@ -90,7 +90,7 @@ std::string DOMUIThemeSource::GetMimeType(const std::string& path) const { return "image/png"; } -void DOMUIThemeSource::SendResponse(int request_id, RefCountedBytes* data) { +void DOMUIThemeSource::SendResponse(int request_id, RefCountedMemory* data) { ChromeURLDataManager::DataSource::SendResponse(request_id, data); } @@ -248,14 +248,8 @@ void DOMUIThemeSource::SendThemeBitmap(int request_id, int resource_id) { ThemeProvider* tp = profile_->GetThemeProvider(); DCHECK(tp); - std::vector<unsigned char> png_bytes; - if (tp->GetRawData(resource_id, &png_bytes)) { - scoped_refptr<RefCountedBytes> image_data = - new RefCountedBytes(png_bytes); - SendResponse(request_id, image_data); - } else { - SendResponse(request_id, NULL); - } + scoped_refptr<RefCountedMemory> image_data(tp->GetRawData(resource_id)); + SendResponse(request_id, image_data); } std::string DOMUIThemeSource::GetNewTabBackgroundCSS(bool bar_attached) { diff --git a/chrome/browser/dom_ui/dom_ui_theme_source.h b/chrome/browser/dom_ui/dom_ui_theme_source.h index fa90eba..65f83e7 100644 --- a/chrome/browser/dom_ui/dom_ui_theme_source.h +++ b/chrome/browser/dom_ui/dom_ui_theme_source.h @@ -22,7 +22,7 @@ class DOMUIThemeSource : public ChromeURLDataManager::DataSource { virtual void StartDataRequest(const std::string& path, int request_id); virtual std::string GetMimeType(const std::string& path) const; - virtual void SendResponse(int request_id, RefCountedBytes* data); + virtual void SendResponse(int request_id, RefCountedMemory* data); virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) const; diff --git a/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc b/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc index 653c2ef..f33bf27 100644 --- a/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc +++ b/chrome/browser/dom_ui/dom_ui_theme_source_unittest.cc @@ -19,8 +19,8 @@ class MockThemeSource : public DOMUIThemeSource { result_data_size_(0) { } - virtual void SendResponse(int request_id, RefCountedBytes* data) { - result_data_size_ = data ? data->data.size() : 0; + virtual void SendResponse(int request_id, RefCountedMemory* data) { + result_data_size_ = data ? data->size() : 0; result_request_id_ = request_id; } diff --git a/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc b/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc index acb8276..c455b26 100644 --- a/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc +++ b/chrome/browser/dom_ui/dom_ui_thumbnail_source.cc @@ -64,9 +64,9 @@ void DOMUIThumbnailSource::DoDataRequest(const std::string& path, } else { // Don't have the thumbnail so return the default thumbnail. if (!default_thumbnail_.get()) { - default_thumbnail_ = new RefCountedBytes; - ResourceBundle::GetSharedInstance().LoadImageResourceBytes( - IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); + default_thumbnail_ = + ResourceBundle::GetSharedInstance().LoadImageResourceBytes( + IDR_DEFAULT_THUMBNAIL); } SendResponse(request_id, default_thumbnail_); } @@ -83,9 +83,9 @@ void DOMUIThumbnailSource::OnThumbnailDataAvailable( SendResponse(request_id, data); } else { if (!default_thumbnail_.get()) { - default_thumbnail_ = new RefCountedBytes; - ResourceBundle::GetSharedInstance().LoadImageResourceBytes( - IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); + default_thumbnail_ = + ResourceBundle::GetSharedInstance().LoadImageResourceBytes( + IDR_DEFAULT_THUMBNAIL); } SendResponse(request_id, default_thumbnail_); diff --git a/chrome/browser/dom_ui/dom_ui_thumbnail_source.h b/chrome/browser/dom_ui/dom_ui_thumbnail_source.h index 6daf357..48f2879 100644 --- a/chrome/browser/dom_ui/dom_ui_thumbnail_source.h +++ b/chrome/browser/dom_ui/dom_ui_thumbnail_source.h @@ -53,7 +53,7 @@ class DOMUIThumbnailSource : public ChromeURLDataManager::DataSource, // Raw PNG representation of the thumbnail to show when the thumbnail // database doesn't have a thumbnail for a webpage. - scoped_refptr<RefCountedBytes> default_thumbnail_; + scoped_refptr<RefCountedMemory> default_thumbnail_; // Store requests when the ThumbnailStore isn't ready. When a notification is // received that it is ready, then serve these requests. diff --git a/chrome/browser/dom_ui/downloads_ui.cc b/chrome/browser/dom_ui/downloads_ui.cc index 52a26df..3de6f5a 100644 --- a/chrome/browser/dom_ui/downloads_ui.cc +++ b/chrome/browser/dom_ui/downloads_ui.cc @@ -139,7 +139,7 @@ DownloadsUI::DownloadsUI(TabContents* contents) : DOMUI(contents) { } // static -bool DownloadsUI::GetFaviconResourceBytes(std::vector<unsigned char>* bytes) { +RefCountedMemory* DownloadsUI::GetFaviconResourceBytes() { return ResourceBundle::GetSharedInstance(). - LoadImageResourceBytes(IDR_DOWNLOADS_FAVICON, bytes); + LoadImageResourceBytes(IDR_DOWNLOADS_FAVICON); } diff --git a/chrome/browser/dom_ui/downloads_ui.h b/chrome/browser/dom_ui/downloads_ui.h index c7ebcdb..cd3fbf1 100644 --- a/chrome/browser/dom_ui/downloads_ui.h +++ b/chrome/browser/dom_ui/downloads_ui.h @@ -9,11 +9,13 @@ #include "chrome/browser/dom_ui/dom_ui.h" +class RefCountedMemory; + class DownloadsUI : public DOMUI { public: explicit DownloadsUI(TabContents* contents); - static bool GetFaviconResourceBytes(std::vector<unsigned char>* bytes); + static RefCountedMemory* GetFaviconResourceBytes(); private: DISALLOW_COPY_AND_ASSIGN(DownloadsUI); diff --git a/chrome/browser/dom_ui/fileicon_source.cc b/chrome/browser/dom_ui/fileicon_source.cc index 247f8c5..7a423d0 100644 --- a/chrome/browser/dom_ui/fileicon_source.cc +++ b/chrome/browser/dom_ui/fileicon_source.cc @@ -39,10 +39,9 @@ void FileIconSource::StartDataRequest(const std::string& path, SkBitmap* icon = im->LookupIcon(escaped_filepath, IconLoader::NORMAL); if (icon) { - std::vector<unsigned char> png_bytes; - gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &png_bytes); + scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes; + gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data); - scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes(png_bytes); SendResponse(request_id, icon_data); } else { // Icon was not in cache, go fetch it slowly. @@ -62,10 +61,9 @@ void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, int request_id = cancelable_consumer_.GetClientData(im, handle); if (icon) { - std::vector<unsigned char> png_bytes; - gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &png_bytes); + scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes; + gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data); - scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes(png_bytes); SendResponse(request_id, icon_data); } else { // TODO(glen): send a dummy icon. diff --git a/chrome/browser/dom_ui/history_ui.cc b/chrome/browser/dom_ui/history_ui.cc index 17e6580..7c2b916 100644 --- a/chrome/browser/dom_ui/history_ui.cc +++ b/chrome/browser/dom_ui/history_ui.cc @@ -381,7 +381,7 @@ const GURL HistoryUI::GetHistoryURLWithSearchText(const std::wstring& text) { } // static -bool HistoryUI::GetFaviconResourceBytes(std::vector<unsigned char>* bytes) { +RefCountedMemory* HistoryUI::GetFaviconResourceBytes() { return ResourceBundle::GetSharedInstance(). - LoadImageResourceBytes(IDR_HISTORY_FAVICON, bytes); + LoadImageResourceBytes(IDR_HISTORY_FAVICON); } diff --git a/chrome/browser/dom_ui/history_ui.h b/chrome/browser/dom_ui/history_ui.h index 892b2ab..3dc51c4 100644 --- a/chrome/browser/dom_ui/history_ui.h +++ b/chrome/browser/dom_ui/history_ui.h @@ -95,7 +95,7 @@ class HistoryUI : public DOMUI { // Return the URL for a given search term. static const GURL GetHistoryURLWithSearchText(const std::wstring& text); - static bool GetFaviconResourceBytes(std::vector<unsigned char>* bytes); + static RefCountedMemory* GetFaviconResourceBytes(); private: DISALLOW_COPY_AND_ASSIGN(HistoryUI); diff --git a/chrome/browser/fav_icon_helper.h b/chrome/browser/fav_icon_helper.h index b2d53ee..d6d1867 100644 --- a/chrome/browser/fav_icon_helper.h +++ b/chrome/browser/fav_icon_helper.h @@ -9,6 +9,7 @@ #include <vector> #include "base/basictypes.h" +#include "base/ref_counted_memory.h" #include "base/scoped_ptr.h" #include "chrome/browser/cancelable_request.h" #include "chrome/browser/favicon_service.h" diff --git a/chrome/browser/favicon_service.cc b/chrome/browser/favicon_service.cc index 937bc08..4acf7dc 100644 --- a/chrome/browser/favicon_service.cc +++ b/chrome/browser/favicon_service.cc @@ -50,12 +50,24 @@ FaviconService::Handle FaviconService::GetFaviconForURL( AddRequest(request, consumer); FaviconService::Handle handle = request->handle(); if (page_url.SchemeIs(chrome::kChromeUIScheme)) { - std::vector<unsigned char> icon_bytes; + // TODO(erg): For now, we're cheating here. DOMUIFactory returns the new + // RefCountedMemory superclass, but consumers of favicon information are + // still all hardcoded to use RefCountedBytes. For now, just copy the + // favicon data in this case because the returned RefCountedMemory class is + // the statically allocated memory one; not the vector backed + // RefCountedBytes. scoped_refptr<RefCountedBytes> icon_data = NULL; - bool know_icon = DOMUIFactory::GetFaviconResourceBytes(page_url, - &icon_bytes); - if (know_icon) - icon_data = new RefCountedBytes(icon_bytes); + scoped_refptr<RefCountedMemory> static_memory( + DOMUIFactory::GetFaviconResourceBytes(page_url)); + bool know_icon = static_memory.get() != NULL; + + if (know_icon) { + std::vector<unsigned char> bytes; + bytes.insert(bytes.begin(), + static_memory->front(), + static_memory->front() + static_memory->size()); + icon_data = RefCountedBytes::TakeVector(&bytes); + } request->ForwardResultAsync(FaviconDataCallback::TupleType(handle, know_icon, icon_data, false, GURL())); diff --git a/chrome/browser/favicon_service.h b/chrome/browser/favicon_service.h index 327acc8..c48fc64 100644 --- a/chrome/browser/favicon_service.h +++ b/chrome/browser/favicon_service.h @@ -8,6 +8,7 @@ #include <vector> #include "base/ref_counted.h" +#include "base/ref_counted_memory.h" #include "base/task.h" #include "chrome/browser/cancelable_request.h" #include "chrome/browser/history/history_types.h" diff --git a/chrome/browser/history/top_sites.h b/chrome/browser/history/top_sites.h index 93fad64..4294578 100644 --- a/chrome/browser/history/top_sites.h +++ b/chrome/browser/history/top_sites.h @@ -13,8 +13,8 @@ #include "base/basictypes.h" #include "base/lock.h" #include "base/ref_counted.h" +#include "base/ref_counted_memory.h" #include "chrome/browser/history/history_types.h" -#include "chrome/common/ref_counted_util.h" #include "chrome/common/thumbnail_score.h" #include "googleurl/src/gurl.h" diff --git a/chrome/browser/sync/sync_setup_wizard.cc b/chrome/browser/sync/sync_setup_wizard.cc index 2027bdb..5c55730 100644 --- a/chrome/browser/sync/sync_setup_wizard.cc +++ b/chrome/browser/sync/sync_setup_wizard.cc @@ -42,9 +42,10 @@ void SyncResourcesSource::StartDataRequest(const std::string& path_raw, int request_id) { scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); if (path_raw == chrome::kSyncThrobberPath) { - ResourceBundle::GetSharedInstance().LoadImageResourceBytes(IDR_THROBBER, - &html_bytes->data); - SendResponse(request_id, html_bytes); + scoped_refptr<RefCountedMemory> throbber( + ResourceBundle::GetSharedInstance().LoadImageResourceBytes( + IDR_THROBBER)); + SendResponse(request_id, throbber); return; } diff --git a/chrome/common/ref_counted_util.h b/chrome/common/ref_counted_util.h index eb38869..801aa42 100644 --- a/chrome/common/ref_counted_util.h +++ b/chrome/common/ref_counted_util.h @@ -23,8 +23,4 @@ class RefCountedVector : DISALLOW_COPY_AND_ASSIGN(RefCountedVector<T>); }; -// RefCountedThreadSafeBytes represent a ref-counted blob of bytes. -// Useful for passing data between threads without copying. -typedef RefCountedVector<unsigned char> RefCountedBytes; - #endif // CHROME_COMMON_REF_COUNTED_UTIL_H__ |