Reland "AppCache: Add padding to cross-origin responses."

This is a reland of 85b389caa7d725cdd31f59e9a2b79ff54804b7b7

Initialized CacheRecord::padding_size to 0.

Original change's description:
> AppCache: Add padding to cross-origin responses.
>
> Bug: 918293
> Change-Id: I4f16640f06feac009d6bbbb624951da6d2669f6c
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1488059
> Commit-Queue: Staphany Park <staphany@chromium.org>
> Reviewed-by: Victor Costan <pwnall@chromium.org>
> Reviewed-by: Marijn Kruisselbrink <mek@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#644624}

Bug: 918293
Change-Id: Ie1d3f99c7e8a854d33255a4d66243da2ce16441c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1539906
Reviewed-by: Victor Costan <pwnall@chromium.org>
Commit-Queue: Staphany Park <staphany@chromium.org>
Cr-Commit-Position: refs/heads/master@{#644719}
void AppCacheUpdateJob::HandleUrlFetchCompleted(URLFetcher* fetcher,
                                                int net_error) {
  DCHECK(internal_state_ == DOWNLOADING);

  UpdateURLLoaderRequest* request = fetcher->request();
  const GURL& url = request->GetURL();
  pending_url_fetches_.erase(url);
  NotifyAllProgress(url);
  ++url_fetches_completed_;

  int response_code = net_error == net::OK ? request->GetResponseCode()
                                           : fetcher->redirect_response_code();

  AppCacheEntry& entry = url_file_list_.find(url)->second;

  if (response_code / 100 == 2) {
     DCHECK(fetcher->response_writer());
     entry.set_response_id(fetcher->response_writer()->response_id());
    entry.SetResponseAndPaddingSizes(
        fetcher->response_writer()->amount_written(),
        ComputeAppCacheResponsePadding(url, manifest_url_));
     if (!inprogress_cache_->AddOrModifyEntry(url, entry))
       duplicate_response_ids_.push_back(entry.response_id());
 

  } else {
    VLOG(1) << "Request error: " << net_error
            << " response code: " << response_code;
    if (entry.IsExplicit() || entry.IsFallback() || entry.IsIntercept()) {
       if (response_code == 304 && fetcher->existing_entry().has_response_id()) {
         entry.set_response_id(fetcher->existing_entry().response_id());
        entry.SetResponseAndPaddingSizes(
            fetcher->existing_entry().response_size(),
            fetcher->existing_entry().padding_size());
         inprogress_cache_->AddOrModifyEntry(url, entry);
       } else {
         const char kFormatString[] = "Resource fetch failed (%d) %s";
        std::string message = FormatUrlErrorMessage(
            kFormatString, url, fetcher->result(), response_code);
        ResultType result = fetcher->result();
        bool is_cross_origin = url.GetOrigin() != manifest_url_.GetOrigin();
        switch (result) {
          case DISKCACHE_ERROR:
            HandleCacheFailure(
                blink::mojom::AppCacheErrorDetails(
                    message,
                    blink::mojom::AppCacheErrorReason::APPCACHE_UNKNOWN_ERROR,
                    GURL(), 0, is_cross_origin),
                result, url);
            break;
          case NETWORK_ERROR:
            HandleCacheFailure(
                blink::mojom::AppCacheErrorDetails(
                    message,
                    blink::mojom::AppCacheErrorReason::APPCACHE_RESOURCE_ERROR,
                    url, 0, is_cross_origin),
                result, url);
            break;
          default:
            HandleCacheFailure(
                blink::mojom::AppCacheErrorDetails(
                    message,
                    blink::mojom::AppCacheErrorReason::APPCACHE_RESOURCE_ERROR,
                    url, response_code, is_cross_origin),
                result, url);
            break;
        }
        return;
      }
    } else if (response_code == 404 || response_code == 410) {
    } else if (update_type_ == UPGRADE_ATTEMPT &&
               fetcher->existing_entry().has_response_id()) {
       entry.set_response_id(fetcher->existing_entry().response_id());
      entry.SetResponseAndPaddingSizes(
          fetcher->existing_entry().response_size(),
          fetcher->existing_entry().padding_size());
       inprogress_cache_->AddOrModifyEntry(url, entry);
     }
   }

  DCHECK(internal_state_ != CACHE_FAILURE);
  FetchUrls();
  MaybeCompleteUpdate();
}

void AppCacheUpdateJob::HandleUrlFetchCompleted(URLFetcher* fetcher,
                                                int net_error) {
  DCHECK(internal_state_ == DOWNLOADING);

  UpdateURLLoaderRequest* request = fetcher->request();
  const GURL& url = request->GetURL();
  pending_url_fetches_.erase(url);
  NotifyAllProgress(url);
  ++url_fetches_completed_;

  int response_code = net_error == net::OK ? request->GetResponseCode()
                                           : fetcher->redirect_response_code();

  AppCacheEntry& entry = url_file_list_.find(url)->second;

  if (response_code / 100 == 2) {
     DCHECK(fetcher->response_writer());
     entry.set_response_id(fetcher->response_writer()->response_id());
    entry.set_response_size(fetcher->response_writer()->amount_written());
     if (!inprogress_cache_->AddOrModifyEntry(url, entry))
       duplicate_response_ids_.push_back(entry.response_id());
 

  } else {
    VLOG(1) << "Request error: " << net_error
            << " response code: " << response_code;
    if (entry.IsExplicit() || entry.IsFallback() || entry.IsIntercept()) {
       if (response_code == 304 && fetcher->existing_entry().has_response_id()) {
         entry.set_response_id(fetcher->existing_entry().response_id());
        entry.set_response_size(fetcher->existing_entry().response_size());
         inprogress_cache_->AddOrModifyEntry(url, entry);
       } else {
         const char kFormatString[] = "Resource fetch failed (%d) %s";
        std::string message = FormatUrlErrorMessage(
            kFormatString, url, fetcher->result(), response_code);
        ResultType result = fetcher->result();
        bool is_cross_origin = url.GetOrigin() != manifest_url_.GetOrigin();
        switch (result) {
          case DISKCACHE_ERROR:
            HandleCacheFailure(
                blink::mojom::AppCacheErrorDetails(
                    message,
                    blink::mojom::AppCacheErrorReason::APPCACHE_UNKNOWN_ERROR,
                    GURL(), 0, is_cross_origin),
                result, url);
            break;
          case NETWORK_ERROR:
            HandleCacheFailure(
                blink::mojom::AppCacheErrorDetails(
                    message,
                    blink::mojom::AppCacheErrorReason::APPCACHE_RESOURCE_ERROR,
                    url, 0, is_cross_origin),
                result, url);
            break;
          default:
            HandleCacheFailure(
                blink::mojom::AppCacheErrorDetails(
                    message,
                    blink::mojom::AppCacheErrorReason::APPCACHE_RESOURCE_ERROR,
                    url, response_code, is_cross_origin),
                result, url);
            break;
        }
        return;
      }
    } else if (response_code == 404 || response_code == 410) {
    } else if (update_type_ == UPGRADE_ATTEMPT &&
               fetcher->existing_entry().has_response_id()) {
       entry.set_response_id(fetcher->existing_entry().response_id());
      entry.set_response_size(fetcher->existing_entry().response_size());
       inprogress_cache_->AddOrModifyEntry(url, entry);
     }
   }

  DCHECK(internal_state_ != CACHE_FAILURE);
  FetchUrls();
  MaybeCompleteUpdate();
}
