Commit 43def17e authored by rdsmith's avatar rdsmith Committed by Commit bot

Naming and refactoring changes to SdchDictionaryFetcher.

Suggested from review in CL https://codereview.chromium.org/864923002/.

BUG=None
R=mmenke@chromium.org

Review URL: https://codereview.chromium.org/877443002

Cr-Commit-Position: refs/heads/master@{#314076}
parent 8a9227c8
...@@ -13,18 +13,35 @@ ...@@ -13,18 +13,35 @@
#include "base/thread_task_runner_handle.h" #include "base/thread_task_runner_handle.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/base/net_log.h"
#include "net/base/sdch_net_log_params.h" #include "net/base/sdch_net_log_params.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_status.h" #include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_throttler_manager.h" #include "net/url_request/url_request_throttler_manager.h"
namespace net {
namespace { namespace {
const int kBufferSize = 4096; const int kBufferSize = 4096;
} // namespace // Map the bytes_read result from a read attempt and a URLRequest's
// status into a single net return value.
int GetReadResult(int bytes_read, const URLRequest* request) {
int rv = request->status().error();
if (request->status().is_success() && bytes_read < 0) {
rv = ERR_FAILED;
request->net_log().AddEventWithNetErrorCode(
NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv);
}
namespace net { if (rv == OK)
rv = bytes_read;
return rv;
}
} // namespace
SdchDictionaryFetcher::SdchDictionaryFetcher( SdchDictionaryFetcher::SdchDictionaryFetcher(
URLRequestContext* context, URLRequestContext* context,
...@@ -65,7 +82,7 @@ void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { ...@@ -65,7 +82,7 @@ void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
if (next_state_ != STATE_NONE) if (next_state_ != STATE_NONE)
return; return;
next_state_ = STATE_IDLE; next_state_ = STATE_SEND_REQUEST;
// There are no callbacks to user code from the dictionary fetcher, // There are no callbacks to user code from the dictionary fetcher,
// and Schedule() is only called from user code, so this call to DoLoop() // and Schedule() is only called from user code, so this call to DoLoop()
...@@ -95,16 +112,8 @@ void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { ...@@ -95,16 +112,8 @@ void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK_EQ(request, current_request_.get()); DCHECK_EQ(request, current_request_.get());
DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
DCHECK(!in_loop_);
// The response has started, so the stream can be read from.
next_state_ = STATE_REQUEST_READING;
// If this function was synchronously called, the containing
// state machine loop will handle the state transition. Otherwise,
// restart the state machine loop.
if (in_loop_)
return;
DoLoop(request->status().error()); DoLoop(request->status().error());
} }
...@@ -118,21 +127,10 @@ void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, ...@@ -118,21 +127,10 @@ void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK_EQ(request, current_request_.get()); DCHECK_EQ(request, current_request_.get());
DCHECK_EQ(next_state_, STATE_REQUEST_READING); DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE);
DCHECK(!in_loop_);
// No state transition is required in this function; the
// completion of the request is detected in DoRead().
if (request->status().is_success())
dictionary_.append(buffer_->data(), bytes_read);
// If this function was synchronously called, the containing
// state machine loop will handle the state transition. Otherwise,
// restart the state machine loop.
if (in_loop_)
return;
DoLoop(request->status().error()); DoLoop(GetReadResult(bytes_read, current_request_.get()));
} }
int SdchDictionaryFetcher::DoLoop(int rv) { int SdchDictionaryFetcher::DoLoop(int rv) {
...@@ -143,14 +141,17 @@ int SdchDictionaryFetcher::DoLoop(int rv) { ...@@ -143,14 +141,17 @@ int SdchDictionaryFetcher::DoLoop(int rv) {
State state = next_state_; State state = next_state_;
next_state_ = STATE_NONE; next_state_ = STATE_NONE;
switch (state) { switch (state) {
case STATE_IDLE: case STATE_SEND_REQUEST:
rv = DoDispatchRequest(rv); rv = DoSendRequest(rv);
break;
case STATE_SEND_REQUEST_COMPLETE:
rv = DoSendRequestComplete(rv);
break; break;
case STATE_REQUEST_STARTED: case STATE_READ_BODY:
rv = DoRequestStarted(rv); rv = DoReadBody(rv);
break; break;
case STATE_REQUEST_READING: case STATE_READ_BODY_COMPLETE:
rv = DoRead(rv); rv = DoReadBodyComplete(rv);
break; break;
case STATE_REQUEST_COMPLETE: case STATE_REQUEST_COMPLETE:
rv = DoCompleteRequest(rv); rv = DoCompleteRequest(rv);
...@@ -163,7 +164,7 @@ int SdchDictionaryFetcher::DoLoop(int rv) { ...@@ -163,7 +164,7 @@ int SdchDictionaryFetcher::DoLoop(int rv) {
return rv; return rv;
} }
int SdchDictionaryFetcher::DoDispatchRequest(int rv) { int SdchDictionaryFetcher::DoSendRequest(int rv) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
// |rv| is ignored, as the result from the previous request doesn't // |rv| is ignored, as the result from the previous request doesn't
...@@ -174,6 +175,8 @@ int SdchDictionaryFetcher::DoDispatchRequest(int rv) { ...@@ -174,6 +175,8 @@ int SdchDictionaryFetcher::DoDispatchRequest(int rv) {
return OK; return OK;
} }
next_state_ = STATE_SEND_REQUEST_COMPLETE;
current_request_ = current_request_ =
context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL);
current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES |
...@@ -181,82 +184,88 @@ int SdchDictionaryFetcher::DoDispatchRequest(int rv) { ...@@ -181,82 +184,88 @@ int SdchDictionaryFetcher::DoDispatchRequest(int rv) {
buffer_ = new IOBuffer(kBufferSize); buffer_ = new IOBuffer(kBufferSize);
fetch_queue_.pop(); fetch_queue_.pop();
next_state_ = STATE_REQUEST_STARTED;
current_request_->Start(); current_request_->Start();
current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);
return OK; return ERR_IO_PENDING;
} }
int SdchDictionaryFetcher::DoRequestStarted(int rv) { int SdchDictionaryFetcher::DoSendRequestComplete(int rv) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
DCHECK_EQ(rv, OK); // Can only come straight from above function.
// If there's been an error, abort the current request.
// The transition to STATE_REQUEST_READING occurs in the if (rv != OK) {
// OnResponseStarted() callback triggered by URLRequest::Start() current_request_.reset();
// (called in DoDispatchRequest(), above). If that callback did not buffer_ = NULL;
// occur synchronously, this routine is executed; it returns ERR_IO_PENDING, next_state_ = STATE_SEND_REQUEST;
// indicating to the controlling loop that no further work should be done
// until the callback occurs (which will re-invoke DoLoop()). return OK;
next_state_ = STATE_REQUEST_STARTED; }
return ERR_IO_PENDING;
next_state_ = STATE_READ_BODY;
return OK;
} }
int SdchDictionaryFetcher::DoRead(int rv) { int SdchDictionaryFetcher::DoReadBody(int rv) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
// If there's been an error, abort the current request. // If there's been an error, abort the current request.
if (rv != OK) { if (rv != OK) {
current_request_.reset(); current_request_.reset();
buffer_ = NULL; buffer_ = NULL;
next_state_ = STATE_IDLE; next_state_ = STATE_SEND_REQUEST;
return OK; return OK;
} }
next_state_ = STATE_REQUEST_READING; next_state_ = STATE_READ_BODY_COMPLETE;
int bytes_read = 0; int bytes_read = 0;
current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); current_request_->Read(buffer_.get(), kBufferSize, &bytes_read);
if (current_request_->status().is_io_pending()) if (current_request_->status().is_io_pending())
return ERR_IO_PENDING; return ERR_IO_PENDING;
if (bytes_read < 0 || !current_request_->status().is_success()) { return GetReadResult(bytes_read, current_request_.get());
if (current_request_->status().error() != OK) }
return current_request_->status().error();
int SdchDictionaryFetcher::DoReadBodyComplete(int rv) {
// An error with request status of OK should not happen, DCHECK(CalledOnValidThread());
// but there's enough machinery underneath URLRequest::Read()
// that this routine checks for that case. // An error; abort the current request.
net::Error error = if (rv < 0) {
current_request_->status().status() == URLRequestStatus::CANCELED ? current_request_.reset();
ERR_ABORTED : ERR_FAILED; buffer_ = NULL;
current_request_->net_log().AddEventWithNetErrorCode( next_state_ = STATE_SEND_REQUEST;
NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, error); return OK;
return error;
} }
if (bytes_read == 0) DCHECK(current_request_->status().is_success());
next_state_ = STATE_REQUEST_COMPLETE;
else
dictionary_.append(buffer_->data(), bytes_read);
// Data; append to the dictionary and look for more data.
if (rv > 0) {
dictionary_.append(buffer_->data(), rv);
next_state_ = STATE_READ_BODY;
return OK;
}
// End of file; complete the request.
next_state_ = STATE_REQUEST_COMPLETE;
return OK; return OK;
} }
int SdchDictionaryFetcher::DoCompleteRequest(int rv) { int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
DCHECK(CalledOnValidThread()); DCHECK(CalledOnValidThread());
// If the dictionary was successfully fetched, add it to the manager. // DoReadBodyComplete() only transitions to this state
if (rv == OK) { // on success.
dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), DCHECK_EQ(OK, rv);
current_request_->net_log());
}
dictionary_fetched_callback_.Run(dictionary_, current_request_->url(),
current_request_->net_log());
current_request_.reset(); current_request_.reset();
buffer_ = NULL; buffer_ = NULL;
dictionary_.clear(); dictionary_.clear();
next_state_ = STATE_IDLE; next_state_ = STATE_SEND_REQUEST;
return OK; return OK;
} }
......
...@@ -15,15 +15,19 @@ ...@@ -15,15 +15,19 @@
#include <set> #include <set>
#include <string> #include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h" #include "base/threading/non_thread_safe.h"
#include "net/base/sdch_manager.h" #include "net/base/sdch_manager.h"
#include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "url/gurl.h"
namespace net { namespace net {
class BoundNetLog;
class URLRequest; class URLRequest;
class URLRequestThrottlerEntryInterface; class URLRequestThrottlerEntryInterface;
...@@ -61,17 +65,19 @@ class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, ...@@ -61,17 +65,19 @@ class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate,
private: private:
enum State { enum State {
STATE_NONE, STATE_NONE,
STATE_IDLE, STATE_SEND_REQUEST,
STATE_REQUEST_STARTED, STATE_SEND_REQUEST_COMPLETE,
STATE_REQUEST_READING, STATE_READ_BODY,
STATE_READ_BODY_COMPLETE,
STATE_REQUEST_COMPLETE, STATE_REQUEST_COMPLETE,
}; };
// State machine implementation. // State machine implementation.
int DoLoop(int rv); int DoLoop(int rv);
int DoDispatchRequest(int rv); int DoSendRequest(int rv);
int DoRequestStarted(int rv); int DoSendRequestComplete(int rv);
int DoRead(int rv); int DoReadBody(int rv);
int DoReadBodyComplete(int rv);
int DoCompleteRequest(int rv); int DoCompleteRequest(int rv);
State next_state_; State next_state_;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "net/url_request/url_request_interceptor.h" #include "net/url_request/url_request_interceptor.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace net { namespace net {
......
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