Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Unified Diff: net/base/sdch_dictionary_fetcher.cc

Issue 495523003: Change SDCHDictionaryFetcher to use URLRequest instead of URLFetcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Results of self-review. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« net/base/sdch_dictionary_fetcher.h ('K') | « net/base/sdch_dictionary_fetcher.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/sdch_dictionary_fetcher.cc
diff --git a/net/base/sdch_dictionary_fetcher.cc b/net/base/sdch_dictionary_fetcher.cc
index f0a417e925954eebcd972793f9c72fccbe50d8bd..43137044a0ea76e60482452c981a7b9fca3069e1 100644
--- a/net/base/sdch_dictionary_fetcher.cc
+++ b/net/base/sdch_dictionary_fetcher.cc
@@ -8,9 +8,15 @@
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "net/base/load_flags.h"
-#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
+#include "net/url_request/url_request_throttler_manager.h"
+
+namespace {
+
+const int kBufferSize = 4096;
+
+} // namespace
namespace net {
@@ -18,9 +24,8 @@ SdchDictionaryFetcher::SdchDictionaryFetcher(
SdchManager* manager,
scoped_refptr<URLRequestContextGetter> context)
: manager_(manager),
- weak_factory_(this),
- task_is_pending_(false),
- context_(context) {
+ context_(context),
+ weak_factory_(this) {
DCHECK(CalledOnValidThread());
DCHECK(manager);
}
@@ -47,7 +52,7 @@ void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
}
attempted_load_.insert(dictionary_url);
fetch_queue_.push(dictionary_url);
- ScheduleDelayedRun();
+ DispatchRun();
}
void SdchDictionaryFetcher::Cancel() {
@@ -57,49 +62,115 @@ void SdchDictionaryFetcher::Cancel() {
fetch_queue_.pop();
attempted_load_.clear();
weak_factory_.InvalidateWeakPtrs();
- current_fetch_.reset(NULL);
+ current_request_.reset(NULL);
+ buffer_ = NULL;
+ dictionary_ = "";
}
-void SdchDictionaryFetcher::ScheduleDelayedRun() {
- if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_)
- return;
- base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
- base::Bind(&SdchDictionaryFetcher::StartFetching,
- weak_factory_.GetWeakPtr()),
- base::TimeDelta::FromMilliseconds(kMsDelayFromRequestTillDownload));
- task_is_pending_ = true;
+void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
+ DCHECK(CalledOnValidThread());
+ DCHECK_EQ(request, current_request_.get());
+
+ int bytes_read = 0;
+ request->Read(buffer_.get(), kBufferSize, &bytes_read);
+ OnReadCompleted(request, bytes_read);
+}
+
+void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
+ int bytes_read) {
+ DCHECK(CalledOnValidThread());
+ DCHECK_EQ(request, current_request_.get());
+
+ URLRequestThrottlerManager* throttler_manager =
+ request->context()->throttler_manager();
+ if (throttler_manager) {
+ url_throttler_entry_ =
+ throttler_manager->RegisterRequestUrl(request->url());
+ }
+
+ do {
+ if (!current_request_->status().is_success() || bytes_read <= 0)
+ break;
+
+ dictionary_ += std::string(buffer_->data(), bytes_read);
+ } while (current_request_->Read(buffer_.get(), kBufferSize, &bytes_read));
+
+ const URLRequestStatus status = current_request_->status();
+
+ if (!status.is_io_pending()) {
+ if (status.is_success())
+ manager_->AddSdchDictionary(dictionary_, current_request_->url());
+
+ current_request_.reset();
+ buffer_ = NULL;
+ dictionary_ = "";
+
+ DispatchRun();
+ }
}
-void SdchDictionaryFetcher::StartFetching() {
+void SdchDictionaryFetcher::DispatchRun() {
DCHECK(CalledOnValidThread());
- DCHECK(task_is_pending_);
- task_is_pending_ = false;
+ DCHECK(context_.get());
- // Handle losing the race against Cancel().
- if (fetch_queue_.empty())
+ if (fetch_queue_.empty() || current_request_.get())
return;
+ int64 delay = 0LL;
Ryan Sleevi 2014/08/20 18:27:55 portability warning: Don't assume int64 is LL Use
Randy Smith (Not in Mondays) 2014/08/20 19:21:50 Done, but this code was copied from URLFetcherCore
+ if (original_url_throttler_entry_.get() == NULL) {
Ryan Sleevi 2014/08/20 18:27:55 Use boolean testing if (!original_url_throttler_e
Randy Smith (Not in Mondays) 2014/08/20 19:21:49 Done, here and in url_fetcher_core.cc.
+ URLRequestThrottlerManager* manager =
+ context_->GetURLRequestContext()->throttler_manager();
+ if (manager) {
+ original_url_throttler_entry_ =
+ manager->RegisterRequestUrl(fetch_queue_.front());
+ }
+ }
+ if (original_url_throttler_entry_.get() != NULL) {
Ryan Sleevi 2014/08/20 18:27:55 ditto
Randy Smith (Not in Mondays) 2014/08/20 19:21:49 Done.
+ delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest(
+ GetBackoffReleaseTime());
+ }
+
+ if (delay == 0) {
+ StartURLRequest();
+ } else {
+ base::MessageLoop::current()->PostDelayedTask(
Ryan Sleevi 2014/08/20 18:27:55 Don't assume a MessageLoop base::ThreadTaskRunner
Randy Smith (Not in Mondays) 2014/08/20 19:21:49 Done.
+ FROM_HERE, base::Bind(&SdchDictionaryFetcher::StartURLRequest,
+ weak_factory_.GetWeakPtr()),
+ base::TimeDelta::FromMilliseconds(delay));
+ }
Ryan Sleevi 2014/08/20 18:27:55 Short-circuit and structure for the early return?
Randy Smith (Not in Mondays) 2014/08/20 19:21:49 Done.
+}
+
+void SdchDictionaryFetcher::StartURLRequest() {
+ DCHECK(CalledOnValidThread());
DCHECK(context_.get());
- current_fetch_.reset(URLFetcher::Create(
- fetch_queue_.front(), URLFetcher::GET, this));
+ DCHECK(!current_request_.get());
+
+ current_request_ = context_->GetURLRequestContext()->CreateRequest(
+ fetch_queue_.front(), IDLE, this, NULL);
+ buffer_ = new IOBuffer(kBufferSize);
fetch_queue_.pop();
- current_fetch_->SetRequestContext(context_.get());
- current_fetch_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES |
- LOAD_DO_NOT_SAVE_COOKIES);
- current_fetch_->Start();
+
+ current_request_->Start();
}
-void SdchDictionaryFetcher::OnURLFetchComplete(
- const URLFetcher* source) {
+base::TimeTicks SdchDictionaryFetcher::GetBackoffReleaseTime() {
DCHECK(CalledOnValidThread());
- if ((200 == source->GetResponseCode()) &&
- (source->GetStatus().status() == URLRequestStatus::SUCCESS)) {
- std::string data;
- source->GetResponseAsString(&data);
- manager_->AddSdchDictionary(data, source->GetURL());
+
+ if (original_url_throttler_entry_.get()) {
+ base::TimeTicks original_url_backoff =
+ original_url_throttler_entry_->GetExponentialBackoffReleaseTime();
+ base::TimeTicks destination_url_backoff;
+ if (url_throttler_entry_.get() != NULL &&
+ original_url_throttler_entry_.get() != url_throttler_entry_.get()) {
+ destination_url_backoff =
+ url_throttler_entry_->GetExponentialBackoffReleaseTime();
+ }
+
+ return original_url_backoff > destination_url_backoff ?
+ original_url_backoff : destination_url_backoff;
+ } else {
+ return base::TimeTicks();
Ryan Sleevi 2014/08/20 18:27:55 Rewrite this with short-circuits if (!original_ur
Randy Smith (Not in Mondays) 2014/08/20 19:21:49 Done, here and in url_fetcher_core.cc.
}
- current_fetch_.reset(NULL);
- ScheduleDelayedRun();
}
} // namespace net
« net/base/sdch_dictionary_fetcher.h ('K') | « net/base/sdch_dictionary_fetcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698