Skip to content

Commit

Permalink
Revert 93365 - it broke on Chrome OS
Browse files Browse the repository at this point in the history
Added RefCountedString, as this is what many RefCountedMemory users seem to want
Made data member of RefCountedBytes private, as per style guide
Changed base64 APIs to accept StringPiece, as it's sometimes better and never worse than string.

BUG=None
TEST=All existing tests pass. Extended  ref_counted_memory_unittests

Review URL: http://codereview.chromium.org/7397021

TBR=joth@chromium.org
Review URL: http://codereview.chromium.org/7471033

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93367 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
joth@chromium.org committed Jul 21, 2011
1 parent dc20613 commit 289f5a7
Show file tree
Hide file tree
Showing 35 changed files with 190 additions and 209 deletions.
1 change: 0 additions & 1 deletion base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
'md5_unittest.cc',
'memory/linked_ptr_unittest.cc',
'memory/mru_cache_unittest.cc',
'memory/ref_counted_memory_unittest.cc',
'memory/ref_counted_unittest.cc',
'memory/scoped_ptr_unittest.cc',
'memory/scoped_vector_unittest.cc',
Expand Down
4 changes: 2 additions & 2 deletions base/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace base {

bool Base64Encode(const StringPiece& input, std::string* output) {
bool Base64Encode(const std::string& input, std::string* output) {
std::string temp;
temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte

Expand All @@ -23,7 +23,7 @@ bool Base64Encode(const StringPiece& input, std::string* output) {
return true;
}

bool Base64Decode(const StringPiece& input, std::string* output) {
bool Base64Decode(const std::string& input, std::string* output) {
std::string temp;
temp.resize(modp_b64_decode_len(input.size()));

Expand Down
5 changes: 2 additions & 3 deletions base/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
#include <string>

#include "base/base_api.h"
#include "base/string_piece.h"

namespace base {

// Encodes the input string in base64. Returns true if successful and false
// otherwise. The output string is only modified if successful.
BASE_API bool Base64Encode(const StringPiece& input, std::string* output);
BASE_API bool Base64Encode(const std::string& input, std::string* output);

// Decodes the base64 input string. Returns true if successful and false
// otherwise. The output string is only modified if successful.
BASE_API bool Base64Decode(const StringPiece& input, std::string* output);
BASE_API bool Base64Decode(const std::string& input, std::string* output);

} // namespace base

Expand Down
34 changes: 4 additions & 30 deletions base/memory/ref_counted_memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "base/memory/ref_counted_memory.h"

#include "base/logging.h"

RefCountedMemory::RefCountedMemory() {
}

Expand All @@ -24,49 +22,25 @@ RefCountedBytes::RefCountedBytes() {
}

RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
: data_(initializer) {
: data(initializer) {
}

RefCountedBytes* RefCountedBytes::TakeVector(
std::vector<unsigned char>* to_destroy) {
RefCountedBytes* bytes = new RefCountedBytes;
bytes->data_.swap(*to_destroy);
bytes->data.swap(*to_destroy);
return bytes;
}

const unsigned char* RefCountedBytes::front() const {
// STL will assert if we do front() on an empty vector, but calling code
// expects a NULL.
return size() ? &data_.front() : NULL;
return size() ? &data.front() : NULL;
}

size_t RefCountedBytes::size() const {
return data_.size();
return data.size();
}

RefCountedBytes::~RefCountedBytes() {
}

namespace base {

RefCountedString::RefCountedString() {}

RefCountedString::~RefCountedString() {}

// static
RefCountedString* RefCountedString::TakeString(std::string* to_destroy) {
RefCountedString* self = new RefCountedString;
to_destroy->swap(self->data_);
return self;
}

const unsigned char* RefCountedString::front() const {
return data_.empty() ? NULL :
reinterpret_cast<const unsigned char*>(data_.data());
}

size_t RefCountedString::size() const {
return data_.size();
}

} // namespace base
51 changes: 8 additions & 43 deletions base/memory/ref_counted_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
#define BASE_MEMORY_REF_COUNTED_MEMORY_H_
#pragma once

#include <string>
#include <vector>

#include "base/base_api.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"

// TODO(erg): The contents of this file should be in a namespace. This would
Expand Down Expand Up @@ -42,9 +40,9 @@ class BASE_API RefCountedStaticMemory : public RefCountedMemory {
RefCountedStaticMemory()
: data_(NULL), length_(0) {}
RefCountedStaticMemory(const unsigned char* data, size_t length)
: data_(length ? data : NULL), length_(length) {}
: data_(data), length_(length) {}

// Overridden from RefCountedMemory:
// Overriden from RefCountedMemory:
virtual const unsigned char* front() const;
virtual size_t size() const;

Expand All @@ -69,51 +67,18 @@ class BASE_API RefCountedBytes : public RefCountedMemory {
// vector.)
static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);

// Overridden from RefCountedMemory:
virtual const unsigned char* front() const OVERRIDE;
virtual size_t size() const OVERRIDE;
// Overriden from RefCountedMemory:
virtual const unsigned char* front() const;
virtual size_t size() const;

const std::vector<unsigned char>& data() const { return data_; }
std::vector<unsigned char>& data() { return data_; }
std::vector<unsigned char> data;

private:
protected:
friend class base::RefCountedThreadSafe<RefCountedBytes>;
virtual ~RefCountedBytes();

std::vector<unsigned char> data_;

DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
};

namespace base {

// An implementation of RefCountedMemory, where the bytes are stored in an STL
// string. Use this if your data naturally arrives in that format.
class BASE_API RefCountedString : public RefCountedMemory {
public:
RefCountedString();

// Constructs a RefCountedString object by performing a swap. (To non
// destructively build a RefCountedString, use the default constructor and
// copy into object->data()).
static RefCountedString* TakeString(std::string* to_destroy);

// Overridden from RefCountedMemory:
virtual const unsigned char* front() const OVERRIDE;
virtual size_t size() const OVERRIDE;

const std::string& data() const { return data_; }
std::string& data() { return data_; }

private:
friend class base::RefCountedThreadSafe<RefCountedString>;
virtual ~RefCountedString();

std::string data_;

DISALLOW_COPY_AND_ASSIGN(RefCountedString);
DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
};

} // namespace base

#endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_
45 changes: 0 additions & 45 deletions base/memory/ref_counted_memory_unittest.cc

This file was deleted.

6 changes: 4 additions & 2 deletions chrome/browser/browser_about_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1421,8 +1421,10 @@ void AboutSource::StartDataRequest(const std::string& path,
}

void AboutSource::FinishDataRequest(const std::string& html, int request_id) {
std::string html_copy(html);
SendResponse(request_id, base::RefCountedString::TakeString(&html_copy));
scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
html_bytes->data.resize(html.size());
std::copy(html.begin(), html.end(), html_bytes->data.begin());
SendResponse(request_id, html_bytes);
}

std::string AboutSource::GetMimeType(const std::string& path) const {
Expand Down
6 changes: 5 additions & 1 deletion chrome/browser/browser_signin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/browser_signin.h"

#include <algorithm>
#include <string>
#include <vector>

Expand Down Expand Up @@ -67,7 +68,10 @@ void BrowserSigninResourcesSource::StartDataRequest(const std::string& path,
response = jstemplate_builder::GetI18nTemplateHtml(html, &dict);
}

SendResponse(request_id, base::RefCountedString::TakeString(&response));
scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
html_bytes->data.resize(response.size());
std::copy(response.begin(), response.end(), html_bytes->data.begin());
SendResponse(request_id, html_bytes);
}

class BrowserSigninHtml : public HtmlDialogUIDelegate,
Expand Down
14 changes: 8 additions & 6 deletions chrome/browser/extensions/extension_tabs_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "base/base64.h"
#include "base/memory/ref_counted_memory.h"
#include "base/stl_util.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
Expand Down Expand Up @@ -1264,7 +1263,7 @@ void CaptureVisibleTabFunction::Observe(int type,
// and call SendResponse().
void CaptureVisibleTabFunction::SendResultFromBitmap(
const SkBitmap& screen_capture) {
std::vector<unsigned char> data;
scoped_refptr<RefCountedBytes> image_data(new RefCountedBytes);
SkAutoLockPixels screen_capture_lock(screen_capture);
bool encoded = false;
std::string mime_type;
Expand All @@ -1277,14 +1276,14 @@ void CaptureVisibleTabFunction::SendResultFromBitmap(
screen_capture.height(),
static_cast<int>(screen_capture.rowBytes()),
image_quality_,
&data);
&image_data->data);
mime_type = keys::kMimeTypeJpeg;
break;
case FORMAT_PNG:
encoded = gfx::PNGCodec::EncodeBGRASkBitmap(
screen_capture,
true, // Discard transparency.
&data);
&image_data->data);
mime_type = keys::kMimeTypePng;
break;
default:
Expand All @@ -1298,8 +1297,11 @@ void CaptureVisibleTabFunction::SendResultFromBitmap(
}

std::string base64_result;
base::StringPiece stream_as_string(
reinterpret_cast<const char*>(vector_as_array(&data)), data.size());
std::string stream_as_string;
stream_as_string.resize(image_data->data.size());
memcpy(&stream_as_string[0],
reinterpret_cast<const char*>(&image_data->data[0]),
image_data->data.size());

base::Base64Encode(stream_as_string, &base64_result);
base64_result.insert(0, base::StringPrintf("data:%s;base64,",
Expand Down
10 changes: 5 additions & 5 deletions chrome/browser/favicon/favicon_handler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
history_handler->favicon_data_.expired = false;
history_handler->favicon_data_.icon_url = icon_url;
scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
FillBitmap(kFaviconSize, kFaviconSize, &data->data());
FillBitmap(kFaviconSize, kFaviconSize, &data->data);
history_handler->favicon_data_.image_data = data;

// Send history response.
Expand Down Expand Up @@ -442,7 +442,7 @@ TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
history_handler->favicon_data_.expired = false;
history_handler->favicon_data_.icon_url = icon_url;
scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
FillBitmap(kFaviconSize, kFaviconSize, &data->data());
FillBitmap(kFaviconSize, kFaviconSize, &data->data);
history_handler->favicon_data_.image_data = data;

// Send history response.
Expand Down Expand Up @@ -529,7 +529,7 @@ TEST_F(FaviconHandlerTest, UpdateFavicon) {
history_handler->favicon_data_.expired = false;
history_handler->favicon_data_.icon_url = icon_url;
scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
FillBitmap(kFaviconSize, kFaviconSize, &data->data());
FillBitmap(kFaviconSize, kFaviconSize, &data->data);
history_handler->favicon_data_.image_data = data;

// Send history response.
Expand Down Expand Up @@ -671,7 +671,7 @@ TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
history_handler->favicon_data_.expired = true;
history_handler->favicon_data_.icon_url = new_icon_url;
scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
FillBitmap(kFaviconSize, kFaviconSize, &data->data());
FillBitmap(kFaviconSize, kFaviconSize, &data->data);
history_handler->favicon_data_.image_data = data;
history_handler->InvokeCallback();

Expand Down Expand Up @@ -800,7 +800,7 @@ TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
handler->favicon_data_.icon_type = history::TOUCH_ICON;
handler->favicon_data_.icon_url = latest_icon_url;
scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
FillBitmap(kFaviconSize, kFaviconSize, &data->data());
FillBitmap(kFaviconSize, kFaviconSize, &data->data);
handler->favicon_data_.image_data = data;

handler->InvokeCallback();
Expand Down
Loading

0 comments on commit 289f5a7

Please sign in to comment.