Commit f9df5354 authored by skerner@chromium.org's avatar skerner@chromium.org

Move response_container_ into the URLFetcher::Core.

It is accessed from the io thread after the fetcher is destroyed on the ui thread.

BUG=none
TEST=Reliability bots.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86340 0039d316-1c4b-4281-b951-d872f2087c98
parent e348af70
...@@ -35,18 +35,18 @@ void TestURLFetcher::set_status(const net::URLRequestStatus& status) { ...@@ -35,18 +35,18 @@ void TestURLFetcher::set_status(const net::URLRequestStatus& status) {
} }
void TestURLFetcher::SetResponseString(const std::string& response) { void TestURLFetcher::SetResponseString(const std::string& response) {
response_destination_ = STRING; SetResponseDestinationForTesting(STRING);
fake_response_string_ = response; fake_response_string_ = response;
} }
void TestURLFetcher::SetResponseFilePath(const FilePath& path) { void TestURLFetcher::SetResponseFilePath(const FilePath& path) {
response_destination_ = TEMP_FILE; SetResponseDestinationForTesting(TEMP_FILE);
fake_response_file_path_ = path; fake_response_file_path_ = path;
} }
bool TestURLFetcher::GetResponseAsString( bool TestURLFetcher::GetResponseAsString(
std::string* out_response_string) const { std::string* out_response_string) const {
if (response_destination_ != STRING) if (GetResponseDestinationForTesting() != STRING)
return false; return false;
*out_response_string = fake_response_string_; *out_response_string = fake_response_string_;
...@@ -55,7 +55,7 @@ bool TestURLFetcher::GetResponseAsString( ...@@ -55,7 +55,7 @@ bool TestURLFetcher::GetResponseAsString(
bool TestURLFetcher::GetResponseAsFilePath( bool TestURLFetcher::GetResponseAsFilePath(
bool take_ownership, FilePath* out_response_path) const { bool take_ownership, FilePath* out_response_path) const {
if (response_destination_ != TEMP_FILE) if (GetResponseDestinationForTesting() != TEMP_FILE)
return false; return false;
*out_response_path = fake_response_file_path_; *out_response_path = fake_response_file_path_;
......
...@@ -192,7 +192,7 @@ class URLFetcher::Core ...@@ -192,7 +192,7 @@ class URLFetcher::Core
void AppendChunkToUpload(const std::string& data, bool is_last_chunk); void AppendChunkToUpload(const std::string& data, bool is_last_chunk);
// Store the response bytes in |buffer_| in the container indicated by // Store the response bytes in |buffer_| in the container indicated by
// |fetcher_->response_destination_|. Return true if the write has been // |response_destination_|. Return true if the write has been
// done, and another read can overwrite |buffer_|. If this function // done, and another read can overwrite |buffer_|. If this function
// returns false, it will post a task that will read more bytes once the // returns false, it will post a task that will read more bytes once the
// write is complete. // write is complete.
...@@ -265,6 +265,9 @@ class URLFetcher::Core ...@@ -265,6 +265,9 @@ class URLFetcher::Core
// writing, and destruction of that file. // writing, and destruction of that file.
scoped_ptr<TempFileWriter> temp_file_writer_; scoped_ptr<TempFileWriter> temp_file_writer_;
// Where should responses be saved?
ResponseDestinationType response_destination_;
static base::LazyInstance<Registry> g_registry; static base::LazyInstance<Registry> g_registry;
friend class URLFetcher; friend class URLFetcher;
...@@ -466,8 +469,6 @@ void URLFetcher::Delegate::OnURLFetchComplete(const URLFetcher* source) { ...@@ -466,8 +469,6 @@ void URLFetcher::Delegate::OnURLFetchComplete(const URLFetcher* source) {
// parameter list to OnURLFetchComplete(). If a user asked to save // parameter list to OnURLFetchComplete(). If a user asked to save
// the response to a file, they must use the new parameter list, // the response to a file, they must use the new parameter list,
// in which case we can not get here. // in which case we can not get here.
CHECK(source->response_destination_ == STRING);
// To avoid updating all callers, thunk to the old prototype for now. // To avoid updating all callers, thunk to the old prototype for now.
OnURLFetchComplete(source, OnURLFetchComplete(source,
source->url(), source->url(),
...@@ -486,8 +487,7 @@ URLFetcher::URLFetcher(const GURL& url, ...@@ -486,8 +487,7 @@ URLFetcher::URLFetcher(const GURL& url,
: ALLOW_THIS_IN_INITIALIZER_LIST( : ALLOW_THIS_IN_INITIALIZER_LIST(
core_(new Core(this, url, request_type, d))), core_(new Core(this, url, request_type, d))),
automatically_retry_on_5xx_(true), automatically_retry_on_5xx_(true),
max_retries_(0), max_retries_(0) {
response_destination_(STRING) {
} }
URLFetcher::~URLFetcher() { URLFetcher::~URLFetcher() {
...@@ -517,7 +517,8 @@ URLFetcher::Core::Core(URLFetcher* fetcher, ...@@ -517,7 +517,8 @@ URLFetcher::Core::Core(URLFetcher* fetcher,
buffer_(new net::IOBuffer(kBufferSize)), buffer_(new net::IOBuffer(kBufferSize)),
is_chunked_upload_(false), is_chunked_upload_(false),
num_retries_(0), num_retries_(0),
was_cancelled_(false) { was_cancelled_(false),
response_destination_(STRING) {
} }
URLFetcher::Core::~Core() { URLFetcher::Core::~Core() {
...@@ -532,7 +533,7 @@ void URLFetcher::Core::Start() { ...@@ -532,7 +533,7 @@ void URLFetcher::Core::Start() {
io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy(); io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy();
CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy"; CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy";
switch (fetcher_->response_destination_) { switch (response_destination_) {
case STRING: case STRING:
io_message_loop_proxy_->PostTask( io_message_loop_proxy_->PostTask(
FROM_HERE, FROM_HERE,
...@@ -614,7 +615,7 @@ void URLFetcher::Core::AppendChunkToUpload(const std::string& content, ...@@ -614,7 +615,7 @@ void URLFetcher::Core::AppendChunkToUpload(const std::string& content,
// be done later. // be done later.
bool URLFetcher::Core::WriteBuffer(int num_bytes) { bool URLFetcher::Core::WriteBuffer(int num_bytes) {
bool write_complete = false; bool write_complete = false;
switch (fetcher_->response_destination_) { switch (response_destination_) {
case STRING: case STRING:
data_.append(buffer_->data(), num_bytes); data_.append(buffer_->data(), num_bytes);
write_complete = true; write_complete = true;
...@@ -913,7 +914,7 @@ void URLFetcher::set_automatically_retry_on_5xx(bool retry) { ...@@ -913,7 +914,7 @@ void URLFetcher::set_automatically_retry_on_5xx(bool retry) {
void URLFetcher::SaveResponseToTemporaryFile( void URLFetcher::SaveResponseToTemporaryFile(
scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) {
core_->file_message_loop_proxy_ = file_message_loop_proxy; core_->file_message_loop_proxy_ = file_message_loop_proxy;
response_destination_ = TEMP_FILE; core_->response_destination_ = TEMP_FILE;
} }
net::HttpResponseHeaders* URLFetcher::response_headers() const { net::HttpResponseHeaders* URLFetcher::response_headers() const {
...@@ -972,7 +973,7 @@ void URLFetcher::ReceivedContentWasMalformed() { ...@@ -972,7 +973,7 @@ void URLFetcher::ReceivedContentWasMalformed() {
} }
bool URLFetcher::GetResponseAsString(std::string* out_response_string) const { bool URLFetcher::GetResponseAsString(std::string* out_response_string) const {
if (response_destination_ != STRING) if (core_->response_destination_ != STRING)
return false; return false;
*out_response_string = core_->data_; *out_response_string = core_->data_;
...@@ -980,13 +981,24 @@ bool URLFetcher::GetResponseAsString(std::string* out_response_string) const { ...@@ -980,13 +981,24 @@ bool URLFetcher::GetResponseAsString(std::string* out_response_string) const {
} }
const std::string& URLFetcher::GetResponseStringRef() const { const std::string& URLFetcher::GetResponseStringRef() const {
CHECK(response_destination_ == STRING); CHECK(core_->response_destination_ == STRING);
return core_->data_; return core_->data_;
} }
void URLFetcher::SetResponseDestinationForTesting(
ResponseDestinationType value) {
core_->response_destination_ = value;
}
URLFetcher::ResponseDestinationType
URLFetcher::GetResponseDestinationForTesting() const {
return core_->response_destination_;
}
bool URLFetcher::GetResponseAsFilePath(bool take_ownership, bool URLFetcher::GetResponseAsFilePath(bool take_ownership,
FilePath* out_response_path) const { FilePath* out_response_path) const {
if (response_destination_ != TEMP_FILE || !core_->temp_file_writer_.get()) if (core_->response_destination_ != TEMP_FILE ||
!core_->temp_file_writer_.get())
return false; return false;
*out_response_path = core_->temp_file_writer_->temp_file(); *out_response_path = core_->temp_file_writer_->temp_file();
......
...@@ -263,6 +263,12 @@ class URLFetcher { ...@@ -263,6 +263,12 @@ class URLFetcher {
static void CancelAll(); static void CancelAll();
protected: protected:
// How should the response be stored?
enum ResponseDestinationType {
STRING, // Default: In a std::string
TEMP_FILE // Write to a temp file
};
// Returns the delegate. // Returns the delegate.
Delegate* delegate() const; Delegate* delegate() const;
...@@ -275,16 +281,13 @@ class URLFetcher { ...@@ -275,16 +281,13 @@ class URLFetcher {
// of crbug.com/83592 . // of crbug.com/83592 .
const std::string& GetResponseStringRef() const; const std::string& GetResponseStringRef() const;
void SetResponseDestinationForTesting(ResponseDestinationType);
ResponseDestinationType GetResponseDestinationForTesting() const;
private: private:
friend class URLFetcherTest; friend class URLFetcherTest;
friend class TestURLFetcher; friend class TestURLFetcher;
// How should the response be stored?
enum ResponseDestinationType {
STRING, // Default: In a std::string
TEMP_FILE // Write to a temp file
};
// Only used by URLFetcherTest, returns the number of URLFetcher::Core objects // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects
// actively running. // actively running.
static int GetNumFetcherCores(); static int GetNumFetcherCores();
...@@ -304,9 +307,6 @@ class URLFetcher { ...@@ -304,9 +307,6 @@ class URLFetcher {
// Maximum retries allowed. // Maximum retries allowed.
int max_retries_; int max_retries_;
// Where should responses be saved?
ResponseDestinationType response_destination_;
static bool g_interception_enabled; static bool g_interception_enabled;
DISALLOW_COPY_AND_ASSIGN(URLFetcher); DISALLOW_COPY_AND_ASSIGN(URLFetcher);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment