Commit 8e5b4ae5 authored by Josh Karlin's avatar Josh Karlin Committed by Commit Bot

[AdTagging] Track resources loaded from memory cache

Requests served from memory cache don't have the is_ad bit in their newly
minted ResourceRequests. The bit is needed by the AdTracker class that
receives a probe on WillSendRequest. This CL copies the bit over to the
new request and tests it.

Bug: 807640
Change-Id: I9656446be52eb19159a84c26cbdf47905a0e1989
Reviewed-on: https://chromium-review.googlesource.com/980700Reviewed-by: default avatarNate Chapin <japhet@chromium.org>
Commit-Queue: Josh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#546166}
parent b1216c18
......@@ -384,6 +384,9 @@ void ResourceFetcher::DidLoadResourceFromMemoryCache(
resource_request.SetFrameType(original_resource_request.GetFrameType());
resource_request.SetRequestContext(
original_resource_request.GetRequestContext());
if (original_resource_request.IsAdResource())
resource_request.SetIsAdResource();
Context().DispatchDidLoadResourceFromMemoryCache(identifier, resource_request,
resource->GetResponse());
Context().DispatchWillSendRequest(
......
......@@ -56,6 +56,7 @@
#include "platform/testing/weburl_loader_mock_factory_impl.h"
#include "platform/weborigin/KURL.h"
#include "platform/wtf/Allocator.h"
#include "platform/wtf/Optional.h"
#include "public/platform/Platform.h"
#include "public/platform/WebURLLoader.h"
#include "public/platform/WebURLLoaderMockFactory.h"
......@@ -148,6 +149,39 @@ TEST_F(ResourceFetcherTest, UseExistingResource) {
EXPECT_EQ(resource, new_resource);
}
// Verify that the ad bit is copied to WillSendRequest's request when the
// response is served from the memory cache.
TEST_F(ResourceFetcherTest, WillSendRequestAdBit) {
// Add a resource to the memory cache.
scoped_refptr<const SecurityOrigin> source_origin =
SecurityOrigin::CreateUnique();
Context()->SetSecurityOrigin(source_origin);
KURL url("http://127.0.0.1:8000/foo.html");
Resource* resource = RawResource::CreateForTest(url, Resource::kRaw);
AddResourceToMemoryCache(resource, source_origin);
ResourceResponse response(url);
response.SetHTTPStatusCode(200);
response.SetHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600");
resource->ResponseReceived(response, nullptr);
resource->FinishForTest();
// Fetch the cached resource. The request to DispatchWillSendRequest should
// preserve the ad bit.
ResourceFetcher* fetcher = ResourceFetcher::Create(Context());
ResourceRequest resource_request(url);
resource_request.SetIsAdResource();
resource_request.SetRequestContext(WebURLRequest::kRequestContextInternal);
FetchParameters fetch_params(resource_request);
platform_->GetURLLoaderMockFactory()->RegisterURL(url, WebURLResponse(), "");
Resource* new_resource = RawResource::Fetch(fetch_params, fetcher, nullptr);
EXPECT_EQ(resource, new_resource);
WTF::Optional<ResourceRequest> new_request =
Context()->RequestFromWillSendRequest();
EXPECT_TRUE(new_request.has_value());
EXPECT_TRUE(new_request.value().IsAdResource());
}
TEST_F(ResourceFetcherTest, Vary) {
scoped_refptr<const SecurityOrigin> source_origin =
SecurityOrigin::CreateUnique();
......
......@@ -12,6 +12,7 @@
#include "platform/loader/fetch/ResourceTimingInfo.h"
#include "platform/scheduler/test/fake_frame_scheduler.h"
#include "platform/scheduler/test/fake_task_runner.h"
#include "platform/wtf/Optional.h"
#include "public/platform/Platform.h"
#include "public/platform/WebURLLoaderFactory.h"
......@@ -49,7 +50,20 @@ class MockFetchContext : public FetchContext {
security_origin_ = security_origin;
}
// The last ResourceRequest passed to DispatchWillSendRequest.
WTF::Optional<ResourceRequest> RequestFromWillSendRequest() const {
return will_send_request_;
}
// FetchContext:
void DispatchWillSendRequest(
unsigned long identifier,
ResourceRequest& request,
const ResourceResponse& redirect_response,
Resource::Type,
const FetchInitiatorInfo& = FetchInitiatorInfo()) override {
will_send_request_ = request;
}
bool AllowImage(bool images_enabled, const KURL&) const override {
return true;
}
......@@ -144,6 +158,7 @@ class MockFetchContext : public FetchContext {
std::unique_ptr<WebURLLoaderFactory> url_loader_factory_;
bool complete_;
long long transfer_size_;
WTF::Optional<ResourceRequest> will_send_request_;
};
} // namespace blink
......
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