Commit 5e7e2953 authored by Dan Elphick's avatar Dan Elphick Committed by Commit Bot

Factor out a DelegateProxy from BackgroundFetch

On the way to creating a BackgroundFetchDelegate, factor out the
thread-crossing and Core part of BackgroundFetchJobController into
BackgroundFetchDelegateProxy. Ultimately this will have very little business
logic but that will come in a follow-up.

TBR=avi@chromium.org

Change-Id: Iaa4cc9491a1758b8cd3c64a31cc41407c701ebaa
Reviewed-on: https://chromium-review.googlesource.com/571801
Commit-Queue: Dan Elphick <delphick@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarJohn Mellor <johnme@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487464}
parent 2d1d3d0a
...@@ -370,6 +370,8 @@ source_set("browser") { ...@@ -370,6 +370,8 @@ source_set("browser") {
"background_fetch/background_fetch_cross_origin_filter.h", "background_fetch/background_fetch_cross_origin_filter.h",
"background_fetch/background_fetch_data_manager.cc", "background_fetch/background_fetch_data_manager.cc",
"background_fetch/background_fetch_data_manager.h", "background_fetch/background_fetch_data_manager.h",
"background_fetch/background_fetch_delegate_proxy.cc",
"background_fetch/background_fetch_delegate_proxy.h",
"background_fetch/background_fetch_event_dispatcher.cc", "background_fetch/background_fetch_event_dispatcher.cc",
"background_fetch/background_fetch_event_dispatcher.h", "background_fetch/background_fetch_event_dispatcher.h",
"background_fetch/background_fetch_job_controller.cc", "background_fetch/background_fetch_job_controller.cc",
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "content/browser/service_worker/service_worker_context_wrapper.h" #include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/blob_handle.h" #include "content/public/browser/blob_handle.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "url/origin.h" #include "url/origin.h"
...@@ -144,39 +143,12 @@ void BackgroundFetchContext::CreateController( ...@@ -144,39 +143,12 @@ void BackgroundFetchContext::CreateController(
const BackgroundFetchOptions& options) { const BackgroundFetchOptions& options) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("background_fetch_context", R"(
semantics {
sender: "Background Fetch API"
description:
"The Background Fetch API enables developers to upload or download "
"files on behalf of the user. Such fetches will yield a user "
"visible notification to inform the user of the operation, through "
"which it can be suspended, resumed and/or cancelled. The "
"developer retains control of the file once the fetch is "
"completed, similar to XMLHttpRequest and other mechanisms for "
"fetching resources using JavaScript."
trigger:
"When the website uses the Background Fetch API to request "
"fetching a file and/or a list of files. This is a Web Platform "
"API for which no express user permission is required."
data:
"The request headers and data as set by the website's developer."
destination: WEBSITE
}
policy {
cookies_allowed: true
cookies_store: "user"
setting: "This feature cannot be disabled in settings."
policy_exception_justification: "Not implemented."
})");
std::unique_ptr<BackgroundFetchJobController> controller = std::unique_ptr<BackgroundFetchJobController> controller =
base::MakeUnique<BackgroundFetchJobController>( base::MakeUnique<BackgroundFetchJobController>(
registration_id, options, data_manager_.get(), browser_context_, registration_id, options, data_manager_.get(), browser_context_,
request_context_getter_, request_context_getter_,
base::BindOnce(&BackgroundFetchContext::DidCompleteJob, base::BindOnce(&BackgroundFetchContext::DidCompleteJob,
weak_factory_.GetWeakPtr()), weak_factory_.GetWeakPtr()));
traffic_annotation);
// TODO(peter): We should actually be able to use Background Fetch in layout // TODO(peter): We should actually be able to use Background Fetch in layout
// tests. That requires a download manager and a request context. // tests. That requires a download manager and a request context.
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/public/browser/browser_thread.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
class URLRequestContextGetter;
}
namespace content {
class BackgroundFetchJobController;
class BrowserContext;
// Proxy class for passing messages between BackgroundFetchJobController on the
// IO thread to and BackgroundFetchDelegate on the UI thread.
// TODO(delphick): Create BackgroundFetchDelegate.
class BackgroundFetchDelegateProxy {
public:
BackgroundFetchDelegateProxy(
BackgroundFetchJobController* job_controller,
const BackgroundFetchRegistrationId& registration_id,
BrowserContext* browser_context,
scoped_refptr<net::URLRequestContextGetter> request_context);
~BackgroundFetchDelegateProxy();
// Requests that the download manager start fetching |request|.
// Should only be called from the BackgroundFetchJobController (on the IO
// thread).
void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request);
// Updates the representation of this Background Fetch in the user interface
// to match the given |title|.
// Should only be called from the BackgroundFetchJobController (on the IO
// thread).
void UpdateUI(const std::string& title);
// Immediately aborts this Background Fetch by request of the developer.
// Should only be called from the BackgroundFetchJobController (on the IO
// thread).
void Abort();
private:
class Core;
// Called when the download manager has started the given |request|. The
// |download_item| continues to be owned by the download system. The
// |interrupt_reason| will indicate when a request could not be started.
// Should only be called from the BackgroundFetchDelegate (on the IO thread).
void DidStartRequest(scoped_refptr<BackgroundFetchRequestInfo> request,
const std::string& download_guid);
// Called when the given |request| has been completed.
// Should only be called from the BackgroundFetchDelegate (on the IO thread).
void DidCompleteRequest(scoped_refptr<BackgroundFetchRequestInfo> request);
// Parent job controller that owns |this|.
BackgroundFetchJobController* const job_controller_;
std::unique_ptr<Core, BrowserThread::DeleteOnUIThread> ui_core_;
base::WeakPtr<Core> ui_core_ptr_;
// Traffic annotation for network request.
const net::NetworkTrafficAnnotationTag traffic_annotation_;
base::WeakPtrFactory<BackgroundFetchDelegateProxy> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BackgroundFetchDelegateProxy);
};
} // namespace content
#endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_
...@@ -12,12 +12,12 @@ ...@@ -12,12 +12,12 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "content/browser/background_fetch/background_fetch_delegate_proxy.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h" #include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/background_fetch/background_fetch_request_info.h" #include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/common/background_fetch/background_fetch_types.h" #include "content/common/background_fetch/background_fetch_types.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net { namespace net {
class URLRequestContextGetter; class URLRequestContextGetter;
...@@ -44,8 +44,7 @@ class CONTENT_EXPORT BackgroundFetchJobController { ...@@ -44,8 +44,7 @@ class CONTENT_EXPORT BackgroundFetchJobController {
BackgroundFetchDataManager* data_manager, BackgroundFetchDataManager* data_manager,
BrowserContext* browser_context, BrowserContext* browser_context,
scoped_refptr<net::URLRequestContextGetter> request_context, scoped_refptr<net::URLRequestContextGetter> request_context,
CompletedCallback completed_callback, CompletedCallback completed_callback);
const net::NetworkTrafficAnnotationTag& traffic_annotation);
~BackgroundFetchJobController(); ~BackgroundFetchJobController();
// Starts fetching the first few requests. The controller will continue to // Starts fetching the first few requests. The controller will continue to
...@@ -70,12 +69,6 @@ class CONTENT_EXPORT BackgroundFetchJobController { ...@@ -70,12 +69,6 @@ class CONTENT_EXPORT BackgroundFetchJobController {
// Returns the options with which this job is fetching data. // Returns the options with which this job is fetching data.
const BackgroundFetchOptions& options() const { return options_; } const BackgroundFetchOptions& options() const { return options_; }
private:
class Core;
// Requests the download manager to start fetching |request|.
void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request);
// Called when the given |request| has started fetching, after having been // Called when the given |request| has started fetching, after having been
// assigned the |download_guid| by the download system. // assigned the |download_guid| by the download system.
void DidStartRequest(scoped_refptr<BackgroundFetchRequestInfo> request, void DidStartRequest(scoped_refptr<BackgroundFetchRequestInfo> request,
...@@ -84,6 +77,10 @@ class CONTENT_EXPORT BackgroundFetchJobController { ...@@ -84,6 +77,10 @@ class CONTENT_EXPORT BackgroundFetchJobController {
// Called when the given |request| has been completed. // Called when the given |request| has been completed.
void DidCompleteRequest(scoped_refptr<BackgroundFetchRequestInfo> request); void DidCompleteRequest(scoped_refptr<BackgroundFetchRequestInfo> request);
private:
// Requests the download manager to start fetching |request|.
void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request);
// Called when a completed download has been marked as such in DataManager. // Called when a completed download has been marked as such in DataManager.
void DidMarkRequestCompleted(bool has_pending_or_active_requests); void DidMarkRequestCompleted(bool has_pending_or_active_requests);
...@@ -96,20 +93,17 @@ class CONTENT_EXPORT BackgroundFetchJobController { ...@@ -96,20 +93,17 @@ class CONTENT_EXPORT BackgroundFetchJobController {
// The current state of this Job Controller. // The current state of this Job Controller.
State state_ = State::INITIALIZED; State state_ = State::INITIALIZED;
// Inner core of this job controller which lives on the UI thread.
std::unique_ptr<Core, BrowserThread::DeleteOnUIThread> ui_core_;
base::WeakPtr<Core> ui_core_ptr_;
// The DataManager's lifetime is controlled by the BackgroundFetchContext and // The DataManager's lifetime is controlled by the BackgroundFetchContext and
// will be kept alive until after the JobController is destroyed. // will be kept alive until after the JobController is destroyed.
BackgroundFetchDataManager* data_manager_; BackgroundFetchDataManager* data_manager_;
// Proxy for interacting with the BackgroundFetchDelegate across thread
// boundaries.
BackgroundFetchDelegateProxy delegate_proxy_;
// Callback for when all fetches have been completed. // Callback for when all fetches have been completed.
CompletedCallback completed_callback_; CompletedCallback completed_callback_;
// Traffic annotation for network request.
const net::NetworkTrafficAnnotationTag traffic_annotation_;
base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_; base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobController); DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobController);
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/test/fake_download_item.h" #include "content/public/test/fake_download_item.h"
#include "content/public/test/mock_download_manager.h" #include "content/public/test/mock_download_manager.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -87,8 +86,7 @@ class BackgroundFetchJobControllerTest : public BackgroundFetchTestBase { ...@@ -87,8 +86,7 @@ class BackgroundFetchJobControllerTest : public BackgroundFetchTestBase {
browser_context(), browser_context(),
make_scoped_refptr(storage_partition->GetURLRequestContext()), make_scoped_refptr(storage_partition->GetURLRequestContext()),
base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob,
base::Unretained(this)), base::Unretained(this)));
TRAFFIC_ANNOTATION_FOR_TESTS);
} }
protected: protected:
......
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