diff options
35 files changed, 244 insertions, 160 deletions
diff --git a/app/resource_bundle.cc b/app/resource_bundle.cc index a108660..7261dfd 100644 --- a/app/resource_bundle.cc +++ b/app/resource_bundle.cc @@ -69,20 +69,20 @@ void ResourceBundle::FreeImages() { /* static */ SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { - std::vector<unsigned char> raw_data, png_data; - bool success = false; + std::vector<unsigned char> png_data; - if (!success) - success = LoadResourceBytes(data_handle, resource_id, &raw_data); - if (!success) + scoped_refptr<RefCountedMemory> memory( + LoadResourceBytes(data_handle, resource_id)); + if (!memory) return NULL; // Decode the PNG. int image_width; int image_height; - if (!gfx::PNGCodec::Decode(&raw_data.front(), raw_data.size(), - gfx::PNGCodec::FORMAT_BGRA, - &png_data, &image_width, &image_height)) { + if (!gfx::PNGCodec::Decode( + memory->front(), memory->size(), + gfx::PNGCodec::FORMAT_BGRA, + &png_data, &image_width, &image_height)) { NOTREACHED() << "Unable to decode image resource " << resource_id; return NULL; } @@ -96,14 +96,14 @@ std::string ResourceBundle::GetDataResource(int resource_id) { return GetRawDataResource(resource_id).as_string(); } -bool ResourceBundle::LoadImageResourceBytes(int resource_id, - std::vector<unsigned char>* bytes) { - return LoadResourceBytes(theme_data_, resource_id, bytes); +RefCountedStaticMemory* ResourceBundle::LoadImageResourceBytes( + int resource_id) { + return LoadResourceBytes(theme_data_, resource_id); } -bool ResourceBundle::LoadDataResourceBytes(int resource_id, - std::vector<unsigned char>* bytes) { - return LoadResourceBytes(resources_data_, resource_id, bytes); +RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( + int resource_id) { + return LoadResourceBytes(resources_data_, resource_id); } SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { diff --git a/app/resource_bundle.h b/app/resource_bundle.h index 23f39c5..f2fdb58 100644 --- a/app/resource_bundle.h +++ b/app/resource_bundle.h @@ -17,6 +17,7 @@ #include "base/basictypes.h" #include "base/file_path.h" #include "base/lock.h" +#include "base/ref_counted_memory.h" #include "base/scoped_ptr.h" #include "base/string16.h" @@ -90,14 +91,12 @@ class ResourceBundle { // Loads the raw bytes of an image resource into |bytes|, // without doing any processing or interpretation of // the resource. Returns whether we successfully read the resource. - bool LoadImageResourceBytes(int resource_id, - std::vector<unsigned char>* bytes); + RefCountedStaticMemory* LoadImageResourceBytes(int resource_id); // Loads the raw bytes of a data resource into |bytes|, // without doing any processing or interpretation of // the resource. Returns whether we successfully read the resource. - bool LoadDataResourceBytes(int resource_id, - std::vector<unsigned char>* bytes); + RefCountedStaticMemory* LoadDataResourceBytes(int resource_id); // Return the contents of a file in a string given the resource id. // This will copy the data from the resource and return it as a string. @@ -185,12 +184,12 @@ class ResourceBundle { // string if no locale data files are found. FilePath GetLocaleFilePath(const std::wstring& pref_locale); - // Loads the raw bytes of a resource from |module| into |bytes|, - // without doing any processing or interpretation of - // the resource. Returns whether we successfully read the resource. - static bool LoadResourceBytes(DataHandle module, - int resource_id, - std::vector<unsigned char>* bytes); + // Returns a handle to bytes from the resource |module|, without doing any + // processing or interpretation of the resource. Returns whether we + // successfully read the resource. Caller does not own the data returned + // through this method and must not modify the data pointed to by |bytes|. + static RefCountedStaticMemory* LoadResourceBytes(DataHandle module, + int resource_id); // Creates and returns a new SkBitmap given the data file to look in and the // resource id. It's up to the caller to free the returned bitmap when diff --git a/app/resource_bundle_linux.cc b/app/resource_bundle_linux.cc index 911cc03..929a77f 100644 --- a/app/resource_bundle_linux.cc +++ b/app/resource_bundle_linux.cc @@ -25,10 +25,10 @@ namespace { // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned // has a ref count of 1 so the caller must call g_object_unref to free the // memory. -GdkPixbuf* LoadPixbuf(std::vector<unsigned char>& data, bool rtl_enabled) { +GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); bool ok = gdk_pixbuf_loader_write(loader.get(), - static_cast<guint8*>(data.data()), data.size(), NULL); + reinterpret_cast<const guint8*>(data->front()), data->size(), NULL); if (!ok) return NULL; // Calling gdk_pixbuf_loader_close forces the data to be parsed by the @@ -117,18 +117,16 @@ void ResourceBundle::LoadThemeResources() { DCHECK(success) << "failed to load theme data"; } -/* static */ -bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id, - std::vector<unsigned char>* bytes) { +// static +RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( + DataHandle module, int resource_id) { DCHECK(module); - base::StringPiece data; - if (!module->Get(resource_id, &data)) - return false; - - bytes->resize(data.length()); - memcpy(&(bytes->front()), data.data(), data.length()); + base::StringPiece bytes; + if (!module->Get(resource_id, &bytes)) + return NULL; - return true; + return new RefCountedStaticMemory( + reinterpret_cast<const unsigned char*>(bytes.data()), bytes.length()); } base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { @@ -176,9 +174,9 @@ GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { return found->second; } - std::vector<unsigned char> data; - LoadImageResourceBytes(resource_id, &data); - GdkPixbuf* pixbuf = LoadPixbuf(data, rtl_enabled); + scoped_refptr<RefCountedStaticMemory> data( + LoadImageResourceBytes(resource_id)); + GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); // We loaded successfully. Cache the pixbuf. if (pixbuf) { diff --git a/app/resource_bundle_mac.mm b/app/resource_bundle_mac.mm index 6696617..b306602 100644 --- a/app/resource_bundle_mac.mm +++ b/app/resource_bundle_mac.mm @@ -71,18 +71,16 @@ void ResourceBundle::LoadThemeResources() { DCHECK(theme_data_) << "failed to load theme.pak"; } -/* static */ -bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id, - std::vector<unsigned char>* bytes) { +// static +RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( + DataHandle module, int resource_id) { DCHECK(module); - base::StringPiece data; - if (!module->Get(resource_id, &data)) - return false; - - bytes->resize(data.length()); - memcpy(&(bytes->front()), data.data(), data.length()); + base::StringPiece bytes; + if (!module->Get(resource_id, &bytes)) + return NULL; - return true; + return new RefCountedStaticMemory( + reinterpret_cast<const unsigned char*>(bytes.data()), bytes.length()); } base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { diff --git a/app/resource_bundle_win.cc b/app/resource_bundle_win.cc index fc9e360..0c25c47 100644 --- a/app/resource_bundle_win.cc +++ b/app/resource_bundle_win.cc @@ -85,20 +85,17 @@ void ResourceBundle::LoadThemeResources() { DCHECK(theme_data_ != NULL) << "unable to load " << theme_data_path.value(); } -/* static */ -bool ResourceBundle::LoadResourceBytes( - DataHandle module, - int resource_id, - std::vector<unsigned char>* bytes) { +// static +RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( + DataHandle module, int resource_id) { void* data_ptr; size_t data_size; if (base::GetDataResourceFromModule(module, resource_id, &data_ptr, &data_size)) { - bytes->resize(data_size); - memcpy(&(bytes->front()), data_ptr, data_size); - return true; + return new RefCountedStaticMemory( + reinterpret_cast<const unsigned char*>(data_ptr), data_size); } else { - return false; + return NULL; } } diff --git a/app/theme_provider.h b/app/theme_provider.h index c3b8a9c..410e699 100644 --- a/app/theme_provider.h +++ b/app/theme_provider.h @@ -23,6 +23,7 @@ class NSImage; #endif // OS_* class Profile; +class RefCountedMemory; class SkBitmap; //////////////////////////////////////////////////////////////////////////////// @@ -60,10 +61,9 @@ class ThemeProvider { // doesn't provide a certain image, but custom themes might (badges, etc). virtual bool HasCustomImage(int id) const = 0; - // Reads the image data from the theme file into the specified vector. Returns - // true on success. - virtual bool GetRawData(int id, - std::vector<unsigned char>* raw_data) const = 0; + // Reads the image data from the theme file into the specified + // vector. Returns NULL on error. + virtual RefCountedMemory* GetRawData(int id) const = 0; #if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS) // Gets the GdkPixbuf with the specified |id|. Returns a pointer to a shared diff --git a/base/base.gyp b/base/base.gyp index c423484..2ca5cfb 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -238,6 +238,7 @@ 'rand_util_win.cc', 'ref_counted.cc', 'ref_counted.h', + 'ref_counted_memory.h', 'registry.cc', 'registry.h', 'resource_util.cc', diff --git a/base/ref_counted.h b/base/ref_counted.h index 70536b9..e04bb7c 100644 --- a/base/ref_counted.h +++ b/base/ref_counted.h @@ -62,8 +62,6 @@ class RefCountedThreadSafeBase { DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); }; - - } // namespace subtle // diff --git a/base/ref_counted_memory.h b/base/ref_counted_memory.h new file mode 100644 index 0000000..ef05abd --- /dev/null +++ b/base/ref_counted_memory.h @@ -0,0 +1,76 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_REF_COUNTED_MEMORY_H_ +#define BASE_REF_COUNTED_MEMORY_H_ + +#include <vector> + +#include "base/ref_counted.h" + +// TODO(erg): The contents of this file should be in a namespace. This would +// require touching >100 files in chrome/ though. + +// A generic interface to memory. This object is reference counted because one +// of its two subclasses own the data they carry, and we need to have +// heterogeneous containers of these two types of memory. +class RefCountedMemory : public base::RefCountedThreadSafe< RefCountedMemory > { + public: + virtual ~RefCountedMemory() {} + + // Retrieves a pointer to the beginning of the data we point to. + virtual const unsigned char* front() const = 0; + + // Size of the memory pointed to. + virtual size_t size() const = 0; +}; + +// An implementation of RefCountedMemory, where the ref counting does not +// matter. +class RefCountedStaticMemory : public RefCountedMemory { + public: + RefCountedStaticMemory() + : data_(NULL), length_(0) {} + RefCountedStaticMemory(const unsigned char* data, size_t length) + : data_(data), length_(length) {} + + virtual const unsigned char* front() const { return data_; } + virtual size_t size() const { return length_; } + + private: + const unsigned char* data_; + size_t length_; + + DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); +}; + +// An implementation of RefCountedMemory, where we own our the data in a +// vector. +class RefCountedBytes : public RefCountedMemory { + public: + // Constructs a RefCountedBytes object by performing a swap. (To non + // destructively build a RefCountedBytes, use the constructor that takes a + // vector.) + static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy) { + RefCountedBytes* bytes = new RefCountedBytes; + bytes->data.swap(*to_destroy); + return bytes; + } + + RefCountedBytes() {} + + // Constructs a RefCountedBytes object by _copying_ from |initializer|. + RefCountedBytes(const std::vector<unsigned char>& initializer) + : data(initializer) {} + + virtual const unsigned char* front() const { return &data.front(); } + virtual size_t size() const { return data.size(); } + + std::vector<unsigned char> data; + + private: + DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); +}; + +#endif // BASE_REF_COUNTED_MEMORY_H_ 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__ diff --git a/views/widget/default_theme_provider.h b/views/widget/default_theme_provider.h index 512b512..962427f 100644 --- a/views/widget/default_theme_provider.h +++ b/views/widget/default_theme_provider.h @@ -30,9 +30,7 @@ class DefaultThemeProvider : public ThemeProvider { virtual bool GetDisplayProperty(int id, int* result) const { return false; } virtual bool ShouldUseNativeFrame() const; virtual bool HasCustomImage(int id) const { return false; } - virtual bool GetRawData(int id, std::vector<unsigned char>* raw_data) const { - return false; - } + virtual RefCountedMemory* GetRawData(int id) const { return NULL; } private: DISALLOW_COPY_AND_ASSIGN(DefaultThemeProvider); |