Implement exponential back-off mechanism.

Contributed by [email protected], original review http://codereview.chromium.org/4194001/

Implement exponential back-off mechanism. Enforce it at the URLRequestHttpJob level for all outgoing HTTP requests.

The reason why to make this change is that we need back-off logic at a lower enough level to manage all outgoing HTTP traffic, so that the browser won't cause any DDoS attack.

This change:
1) patches http://codereview.chromium.org/2487001/show, which is the exponential back-off implementation.
2) resolves conflicts with URLFetcher, by removing its own back-off logic:
-- removes url_fetcher_protect.{h,cc};
-- integrates the sliding window mechanism of URLFetcherProtectEntry into RequestThrottlerEntry.
3) resolves conflicts with CloudPrintURLFetcher.
4) makes unit tests of CloudPrintURLFetcher, URLFetcher and URLRequest work.

BUG=none
TEST=pass all existing tests and also the newly-added request_throttler_unittest.cc

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67375 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc
index f00f490..826f635 100644
--- a/net/url_request/url_request_http_job.cc
+++ b/net/url_request/url_request_http_job.cc
@@ -33,6 +33,8 @@
 #include "net/url_request/url_request_context.h"
 #include "net/url_request/url_request_error_job.h"
 #include "net/url_request/url_request_redirect_job.h"
+#include "net/url_request/url_request_throttler_header_adapter.h"
+#include "net/url_request/url_request_throttler_manager.h"
 
 static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
 
@@ -91,6 +93,8 @@
           this, &URLRequestHttpJob::OnReadCompleted)),
       read_in_progress_(false),
       transaction_(NULL),
+      throttling_entry_(net::URLRequestThrottlerManager::GetInstance()->
+          RegisterRequestUrl(request->url())),
       sdch_dictionary_advertised_(false),
       sdch_test_activated_(false),
       sdch_test_control_(false),
@@ -569,6 +573,12 @@
   // also need this info.
   is_cached_content_ = response_info_->was_cached;
 
+  if (!is_cached_content_) {
+    net::URLRequestThrottlerHeaderAdapter response_adapter(
+        response_info_->headers);
+    throttling_entry_->UpdateWithResponse(&response_adapter);
+  }
+
   ProcessStrictTransportSecurityHeader();
 
   if (SdchManager::Global() &&
@@ -618,6 +628,7 @@
   // with auth provided by username_ and password_.
 
   int rv;
+
   if (transaction_.get()) {
     rv = transaction_->RestartWithAuth(username_, password_, &start_callback_);
     username_.clear();
@@ -629,8 +640,13 @@
     rv = request_->context()->http_transaction_factory()->CreateTransaction(
         &transaction_);
     if (rv == net::OK) {
-      rv = transaction_->Start(
-          &request_info_, &start_callback_, request_->net_log());
+      if (!throttling_entry_->IsDuringExponentialBackoff()) {
+        rv = transaction_->Start(
+            &request_info_, &start_callback_, request_->net_log());
+      } else {
+        // Special error code for the exponential back-off module.
+        rv = net::ERR_TEMPORARILY_THROTTLED;
+      }
       // Make sure the context is alive for the duration of the
       // transaction.
       context_ = request_->context();