Commit a341a03c authored by peter's avatar peter Committed by Commit bot

Some minor fixes for Background Fetch

- Initialize the URL request context getter on the IO thread.
- Initialize the response type to Default to match a DCHECK.
- Don't double initialize the fetches in BackgroundFetchedEvent

BUG=692579

Review-Url: https://codereview.chromium.org/2806713002
Cr-Commit-Position: refs/heads/master@{#462868}
parent d93ff442
......@@ -44,14 +44,18 @@ BackgroundFetchContext::BackgroundFetchContext(
event_dispatcher_(base::MakeUnique<BackgroundFetchEventDispatcher>(
std::move(service_worker_context))) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
request_context_ =
make_scoped_refptr(storage_partition->GetURLRequestContext());
}
BackgroundFetchContext::~BackgroundFetchContext() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
void BackgroundFetchContext::InitializeOnIOThread(
scoped_refptr<net::URLRequestContextGetter> request_context_getter) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
request_context_getter_ = request_context_getter;
}
void BackgroundFetchContext::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
......@@ -146,12 +150,12 @@ void BackgroundFetchContext::CreateController(
std::unique_ptr<BackgroundFetchJobController> controller =
base::MakeUnique<BackgroundFetchJobController>(
registration_id, options, data_manager_.get(), browser_context_,
request_context_,
request_context_getter_,
base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this));
// TODO(peter): We should actually be able to use Background Fetch in layout
// tests. That requires a download manager and a request context.
if (request_context_) {
if (request_context_getter_) {
// Start fetching the |initial_requests| immediately. At some point in the
// future we may want a more elaborate scheduling mechanism here.
controller->Start(std::move(initial_requests));
......
......@@ -51,6 +51,11 @@ class CONTENT_EXPORT BackgroundFetchContext
StoragePartitionImpl* storage_partition,
scoped_refptr<ServiceWorkerContextWrapper> context);
// Finishes initializing the Background Fetch context on the IO thread by
// setting the |request_context_getter|.
void InitializeOnIOThread(
scoped_refptr<net::URLRequestContextGetter> request_context_getter);
// Shutdown must be called before deleting this. Call on the UI thread.
void Shutdown();
......@@ -120,7 +125,7 @@ class CONTENT_EXPORT BackgroundFetchContext
// |this| is owned, indirectly, by the BrowserContext.
BrowserContext* browser_context_;
scoped_refptr<net::URLRequestContextGetter> request_context_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
std::unique_ptr<BackgroundFetchDataManager> data_manager_;
std::unique_ptr<BackgroundFetchEventDispatcher> event_dispatcher_;
......
......@@ -16,6 +16,7 @@
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_item.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerResponseType.h"
namespace content {
......@@ -208,7 +209,8 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
settled_fetch.response.url_list = request->GetURLChain();
// TODO: settled_fetch.response.status_code
// TODO: settled_fetch.response.status_text
// TODO: settled_fetch.response.response_type
settled_fetch.response.response_type =
blink::WebServiceWorkerResponseTypeDefault;
// TODO: settled_fetch.response.headers
if (request->GetFileSize() > 0) {
......
......@@ -128,6 +128,9 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
context_ = new BackgroundFetchContext(
browser_context(), storage_partition,
make_scoped_refptr(embedded_worker_test_helper()->context_wrapper()));
context_->InitializeOnIOThread(
make_scoped_refptr(storage_partition->GetURLRequestContext()));
service_ = base::MakeUnique<BackgroundFetchServiceImpl>(
0 /* render_process_id */, context_);
}
......@@ -369,7 +372,7 @@ TEST_F(BackgroundFetchServiceTest, FetchSuccessEventDispatch) {
EXPECT_EQ(fetches[i].response.status_code, 0);
EXPECT_TRUE(fetches[i].response.status_text.empty());
EXPECT_EQ(fetches[i].response.response_type,
blink::WebServiceWorkerResponseTypeOpaque);
blink::WebServiceWorkerResponseTypeDefault);
EXPECT_TRUE(fetches[i].response.headers.empty());
EXPECT_EQ(fetches[i].response.error,
blink::WebServiceWorkerResponseErrorUnknown);
......
......@@ -25,6 +25,7 @@
#include "build/build_config.h"
#include "content/browser/appcache/appcache_interceptor.h"
#include "content/browser/appcache/chrome_appcache_service.h"
#include "content/browser/background_fetch/background_fetch_context.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/fileapi/browser_file_system_helper.h"
#include "content/browser/loader/resource_request_info_impl.h"
......@@ -600,6 +601,12 @@ void StoragePartitionImplMap::PostCreateInitialization(
partition->GetServiceWorkerContext(),
browser_context_->GetResourceContext()));
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&BackgroundFetchContext::InitializeOnIOThread,
partition->GetBackgroundFetchContext(),
base::RetainedRef(partition->GetURLRequestContext())));
// We do not call InitializeURLRequestContext() for media contexts because,
// other than the HTTP cache, the media contexts share the same backing
// objects as their associated "normal" request context. Thus, the previous
......
......@@ -31,7 +31,6 @@ BackgroundFetchedEvent::BackgroundFetchedEvent(
WaitUntilObserver* observer,
ServiceWorkerRegistration* registration)
: BackgroundFetchEvent(type, initializer, observer),
m_fetches(initializer.fetches()),
m_registration(registration) {
m_fetches.reserveInitialCapacity(fetches.size());
for (const WebBackgroundFetchSettledFetch& fetch : fetches) {
......
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