Exemplo n.º 1
0
void ResourceLoader::requestSynchronously(const ResourceRequest& request) {
  // downloadToFile is not supported for synchronous requests.
  DCHECK(!request.downloadToFile());
  DCHECK(m_loader);
  DCHECK_EQ(request.priority(), ResourceLoadPriorityHighest);

  WrappedResourceRequest requestIn(request);
  WebURLResponse responseOut;
  WebURLError errorOut;
  WebData dataOut;
  int64_t encodedDataLength = WebURLLoaderClient::kUnknownEncodedDataLength;
  int64_t encodedBodyLength = 0;
  m_loader->loadSynchronously(requestIn, responseOut, errorOut, dataOut,
                              encodedDataLength, encodedBodyLength);

  // A message dispatched while synchronously fetching the resource
  // can bring about the cancellation of this load.
  if (!m_loader)
    return;
  if (errorOut.reason) {
    didFail(errorOut, encodedDataLength, encodedBodyLength);
    return;
  }
  didReceiveResponse(responseOut);
  if (!m_loader)
    return;
  DCHECK_GE(responseOut.toResourceResponse().encodedBodyLength(), 0);

  // Follow the async case convention of not calling didReceiveData or
  // appending data to m_resource if the response body is empty. Copying the
  // empty buffer is a noop in most cases, but is destructive in the case of
  // a 304, where it will overwrite the cached data we should be reusing.
  if (dataOut.size()) {
    m_fetcher->didReceiveData(m_resource.get(), dataOut.data(), dataOut.size());
    m_resource->setResourceBuffer(dataOut);
  }
  didFinishLoading(monotonicallyIncreasingTime(), encodedDataLength,
                   encodedBodyLength);
}
Exemplo n.º 2
0
TEST(ResourceRequestTest, CrossThreadResourceRequestData) {
  ResourceRequest original;
  original.setURL(KURL(ParsedURLString, "http://www.example.com/test.htm"));
  original.setCachePolicy(WebCachePolicy::UseProtocolCachePolicy);
  original.setTimeoutInterval(10);
  original.setFirstPartyForCookies(
      KURL(ParsedURLString, "http://www.example.com/first_party.htm"));
  original.setRequestorOrigin(SecurityOrigin::create(
      KURL(ParsedURLString, "http://www.example.com/first_party.htm")));
  original.setHTTPMethod(HTTPNames::GET);
  original.setHTTPHeaderField(AtomicString("Foo"), AtomicString("Bar"));
  original.setHTTPHeaderField(AtomicString("Piyo"), AtomicString("Fuga"));
  original.setPriority(ResourceLoadPriorityLow, 20);

  RefPtr<EncodedFormData> originalBody(EncodedFormData::create("Test Body"));
  original.setHTTPBody(originalBody);
  original.setAllowStoredCredentials(false);
  original.setReportUploadProgress(false);
  original.setHasUserGesture(false);
  original.setDownloadToFile(false);
  original.setSkipServiceWorker(WebURLRequest::SkipServiceWorker::None);
  original.setFetchRequestMode(WebURLRequest::FetchRequestModeCORS);
  original.setFetchCredentialsMode(
      WebURLRequest::FetchCredentialsModeSameOrigin);
  original.setRequestorID(30);
  original.setRequestorProcessID(40);
  original.setAppCacheHostID(50);
  original.setRequestContext(WebURLRequest::RequestContextAudio);
  original.setFrameType(WebURLRequest::FrameTypeNested);
  original.setHTTPReferrer(
      Referrer("http://www.example.com/referrer.htm", ReferrerPolicyDefault));

  EXPECT_STREQ("http://www.example.com/test.htm",
               original.url().getString().utf8().data());
  EXPECT_EQ(WebCachePolicy::UseProtocolCachePolicy, original.getCachePolicy());
  EXPECT_EQ(10, original.timeoutInterval());
  EXPECT_STREQ("http://www.example.com/first_party.htm",
               original.firstPartyForCookies().getString().utf8().data());
  EXPECT_STREQ("www.example.com",
               original.requestorOrigin()->host().utf8().data());
  EXPECT_STREQ("GET", original.httpMethod().utf8().data());
  EXPECT_STREQ("Bar", original.httpHeaderFields().get("Foo").utf8().data());
  EXPECT_STREQ("Fuga", original.httpHeaderFields().get("Piyo").utf8().data());
  EXPECT_EQ(ResourceLoadPriorityLow, original.priority());
  EXPECT_STREQ("Test Body",
               original.httpBody()->flattenToString().utf8().data());
  EXPECT_FALSE(original.allowStoredCredentials());
  EXPECT_FALSE(original.reportUploadProgress());
  EXPECT_FALSE(original.hasUserGesture());
  EXPECT_FALSE(original.downloadToFile());
  EXPECT_EQ(WebURLRequest::SkipServiceWorker::None,
            original.skipServiceWorker());
  EXPECT_EQ(WebURLRequest::FetchRequestModeCORS, original.fetchRequestMode());
  EXPECT_EQ(WebURLRequest::FetchCredentialsModeSameOrigin,
            original.fetchCredentialsMode());
  EXPECT_EQ(30, original.requestorID());
  EXPECT_EQ(40, original.requestorProcessID());
  EXPECT_EQ(50, original.appCacheHostID());
  EXPECT_EQ(WebURLRequest::RequestContextAudio, original.requestContext());
  EXPECT_EQ(WebURLRequest::FrameTypeNested, original.frameType());
  EXPECT_STREQ("http://www.example.com/referrer.htm",
               original.httpReferrer().utf8().data());
  EXPECT_EQ(ReferrerPolicyDefault, original.getReferrerPolicy());

  std::unique_ptr<CrossThreadResourceRequestData> data1(original.copyData());
  ResourceRequest copy1(data1.get());

  EXPECT_STREQ("http://www.example.com/test.htm",
               copy1.url().getString().utf8().data());
  EXPECT_EQ(WebCachePolicy::UseProtocolCachePolicy, copy1.getCachePolicy());
  EXPECT_EQ(10, copy1.timeoutInterval());
  EXPECT_STREQ("http://www.example.com/first_party.htm",
               copy1.firstPartyForCookies().getString().utf8().data());
  EXPECT_STREQ("www.example.com",
               copy1.requestorOrigin()->host().utf8().data());
  EXPECT_STREQ("GET", copy1.httpMethod().utf8().data());
  EXPECT_STREQ("Bar", copy1.httpHeaderFields().get("Foo").utf8().data());
  EXPECT_EQ(ResourceLoadPriorityLow, copy1.priority());
  EXPECT_STREQ("Test Body", copy1.httpBody()->flattenToString().utf8().data());
  EXPECT_FALSE(copy1.allowStoredCredentials());
  EXPECT_FALSE(copy1.reportUploadProgress());
  EXPECT_FALSE(copy1.hasUserGesture());
  EXPECT_FALSE(copy1.downloadToFile());
  EXPECT_EQ(WebURLRequest::SkipServiceWorker::None, copy1.skipServiceWorker());
  EXPECT_EQ(WebURLRequest::FetchRequestModeCORS, copy1.fetchRequestMode());
  EXPECT_EQ(WebURLRequest::FetchCredentialsModeSameOrigin,
            copy1.fetchCredentialsMode());
  EXPECT_EQ(30, copy1.requestorID());
  EXPECT_EQ(40, copy1.requestorProcessID());
  EXPECT_EQ(50, copy1.appCacheHostID());
  EXPECT_EQ(WebURLRequest::RequestContextAudio, copy1.requestContext());
  EXPECT_EQ(WebURLRequest::FrameTypeNested, copy1.frameType());
  EXPECT_STREQ("http://www.example.com/referrer.htm",
               copy1.httpReferrer().utf8().data());
  EXPECT_EQ(ReferrerPolicyDefault, copy1.getReferrerPolicy());

  copy1.setAllowStoredCredentials(true);
  copy1.setReportUploadProgress(true);
  copy1.setHasUserGesture(true);
  copy1.setDownloadToFile(true);
  copy1.setSkipServiceWorker(WebURLRequest::SkipServiceWorker::All);
  copy1.setFetchRequestMode(WebURLRequest::FetchRequestModeNoCORS);
  copy1.setFetchCredentialsMode(WebURLRequest::FetchCredentialsModeInclude);

  std::unique_ptr<CrossThreadResourceRequestData> data2(copy1.copyData());
  ResourceRequest copy2(data2.get());
  EXPECT_TRUE(copy2.allowStoredCredentials());
  EXPECT_TRUE(copy2.reportUploadProgress());
  EXPECT_TRUE(copy2.hasUserGesture());
  EXPECT_TRUE(copy2.downloadToFile());
  EXPECT_EQ(WebURLRequest::SkipServiceWorker::All, copy2.skipServiceWorker());
  EXPECT_EQ(WebURLRequest::FetchRequestModeNoCORS, copy1.fetchRequestMode());
  EXPECT_EQ(WebURLRequest::FetchCredentialsModeInclude,
            copy1.fetchCredentialsMode());
}
TEST(ResourceRequestTest, CrossThreadResourceRequestData)
{
    ResourceRequest original;
    original.setURL(KURL(ParsedURLString, "http://www.example.com/test.htm"));
    original.setCachePolicy(UseProtocolCachePolicy);
    original.setTimeoutInterval(10);
    original.setFirstPartyForCookies(KURL(ParsedURLString, "http://www.example.com/first_party.htm"));
    original.setHTTPMethod(AtomicString("GET", AtomicString::ConstructFromLiteral));
    original.setHTTPHeaderField(AtomicString("Foo"), AtomicString("Bar"));
    original.setHTTPHeaderField(AtomicString("Piyo"), AtomicString("Fuga"));
    original.setPriority(ResourceLoadPriorityLow, 20);

    RefPtr<FormData> originalBody(FormData::create("Test Body"));
    original.setHTTPBody(originalBody);
    original.setAllowStoredCredentials(false);
    original.setReportUploadProgress(false);
    original.setHasUserGesture(false);
    original.setDownloadToFile(false);
    original.setSkipServiceWorker(false);
    original.setFetchRequestMode(WebURLRequest::FetchRequestModeCORS);
    original.setFetchCredentialsMode(WebURLRequest::FetchCredentialsModeSameOrigin);
    original.setRequestorID(30);
    original.setRequestorProcessID(40);
    original.setAppCacheHostID(50);
    original.setRequestContext(WebURLRequest::RequestContextAudio);
    original.setFrameType(WebURLRequest::FrameTypeNested);
    original.setHTTPReferrer(Referrer("http://www.example.com/referrer.htm", ReferrerPolicyDefault));

    EXPECT_STREQ("http://www.example.com/test.htm", original.url().string().utf8().data());
    EXPECT_EQ(UseProtocolCachePolicy, original.cachePolicy());
    EXPECT_EQ(10, original.timeoutInterval());
    EXPECT_STREQ("http://www.example.com/first_party.htm", original.firstPartyForCookies().string().utf8().data());
    EXPECT_STREQ("GET", original.httpMethod().utf8().data());
    EXPECT_STREQ("Bar", original.httpHeaderFields().get("Foo").utf8().data());
    EXPECT_STREQ("Fuga", original.httpHeaderFields().get("Piyo").utf8().data());
    EXPECT_EQ(ResourceLoadPriorityLow, original.priority());
    EXPECT_STREQ("Test Body", original.httpBody()->flattenToString().utf8().data());
    EXPECT_FALSE(original.allowStoredCredentials());
    EXPECT_FALSE(original.reportUploadProgress());
    EXPECT_FALSE(original.hasUserGesture());
    EXPECT_FALSE(original.downloadToFile());
    EXPECT_FALSE(original.skipServiceWorker());
    EXPECT_EQ(WebURLRequest::FetchRequestModeCORS, original.fetchRequestMode());
    EXPECT_EQ(WebURLRequest::FetchCredentialsModeSameOrigin, original.fetchCredentialsMode());
    EXPECT_EQ(30, original.requestorID());
    EXPECT_EQ(40, original.requestorProcessID());
    EXPECT_EQ(50, original.appCacheHostID());
    EXPECT_EQ(WebURLRequest::RequestContextAudio, original.requestContext());
    EXPECT_EQ(WebURLRequest::FrameTypeNested, original.frameType());
    EXPECT_STREQ("http://www.example.com/referrer.htm", original.httpReferrer().utf8().data());
    EXPECT_EQ(ReferrerPolicyDefault, original.referrerPolicy());

    OwnPtr<CrossThreadResourceRequestData> data1(original.copyData());
    OwnPtr<ResourceRequest> copy1(ResourceRequest::adopt(data1.release()));

    EXPECT_STREQ("http://www.example.com/test.htm", copy1->url().string().utf8().data());
    EXPECT_EQ(UseProtocolCachePolicy, copy1->cachePolicy());
    EXPECT_EQ(10, copy1->timeoutInterval());
    EXPECT_STREQ("http://www.example.com/first_party.htm", copy1->firstPartyForCookies().string().utf8().data());
    EXPECT_STREQ("GET", copy1->httpMethod().utf8().data());
    EXPECT_STREQ("Bar", copy1->httpHeaderFields().get("Foo").utf8().data());
    EXPECT_EQ(ResourceLoadPriorityLow, copy1->priority());
    EXPECT_STREQ("Test Body", copy1->httpBody()->flattenToString().utf8().data());
    EXPECT_FALSE(copy1->allowStoredCredentials());
    EXPECT_FALSE(copy1->reportUploadProgress());
    EXPECT_FALSE(copy1->hasUserGesture());
    EXPECT_FALSE(copy1->downloadToFile());
    EXPECT_FALSE(copy1->skipServiceWorker());
    EXPECT_EQ(WebURLRequest::FetchRequestModeCORS, copy1->fetchRequestMode());
    EXPECT_EQ(WebURLRequest::FetchCredentialsModeSameOrigin, copy1->fetchCredentialsMode());
    EXPECT_EQ(30, copy1->requestorID());
    EXPECT_EQ(40, copy1->requestorProcessID());
    EXPECT_EQ(50, copy1->appCacheHostID());
    EXPECT_EQ(WebURLRequest::RequestContextAudio, copy1->requestContext());
    EXPECT_EQ(WebURLRequest::FrameTypeNested, copy1->frameType());
    EXPECT_STREQ("http://www.example.com/referrer.htm", copy1->httpReferrer().utf8().data());
    EXPECT_EQ(ReferrerPolicyDefault, copy1->referrerPolicy());

    copy1->setAllowStoredCredentials(true);
    copy1->setReportUploadProgress(true);
    copy1->setHasUserGesture(true);
    copy1->setDownloadToFile(true);
    copy1->setSkipServiceWorker(true);
    copy1->setFetchRequestMode(WebURLRequest::FetchRequestModeNoCORS);
    copy1->setFetchCredentialsMode(WebURLRequest::FetchCredentialsModeInclude);

    OwnPtr<CrossThreadResourceRequestData> data2(copy1->copyData());
    OwnPtr<ResourceRequest> copy2(ResourceRequest::adopt(data2.release()));
    EXPECT_TRUE(copy2->allowStoredCredentials());
    EXPECT_TRUE(copy2->reportUploadProgress());
    EXPECT_TRUE(copy2->hasUserGesture());
    EXPECT_TRUE(copy2->downloadToFile());
    EXPECT_TRUE(copy2->skipServiceWorker());
    EXPECT_EQ(WebURLRequest::FetchRequestModeNoCORS, copy1->fetchRequestMode());
    EXPECT_EQ(WebURLRequest::FetchCredentialsModeInclude, copy1->fetchCredentialsMode());
}