Factor out RequestManager from BackgroundFetchDataManager
RequestManager is a small interface containing all the methods that
JobController calls on DataManager. These methods are all to do with
getting requests off the queue and updating their status. This means
that BackgroundFetchJobController now has no dependency on DataManager.
Also removes the final usage of DataManager in
BackgroundFetchServiceImpl by adding the method to
BackgroundFetchContext instead. As a result the data_manager() getter
can be removed from BackgroundFetchContext.
DataManager is now only used directly by the Context class and the
Database tasks in storage.
[email protected]
Bug: 757760
Change-Id: I959ae0855fe2b9d8c5951f676252404f61aa123c
Reviewed-on: https://chromium-review.googlesource.com/756746
Commit-Queue: Dan Elphick <[email protected]>
Reviewed-by: Anita Woodruff <[email protected]>
Reviewed-by: Peter Beverloo <[email protected]>
Cr-Commit-Position: refs/heads/master@{#514464}
diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
index 140d5d2..cc0e307 100644
--- a/content/browser/BUILD.gn
+++ b/content/browser/BUILD.gn
@@ -402,6 +402,7 @@
"background_fetch/background_fetch_registration_notifier.h",
"background_fetch/background_fetch_request_info.cc",
"background_fetch/background_fetch_request_info.h",
+ "background_fetch/background_fetch_request_manager.h",
"background_fetch/background_fetch_service_impl.cc",
"background_fetch/background_fetch_service_impl.h",
"background_fetch/storage/cleanup_task.cc",
diff --git a/content/browser/background_fetch/background_fetch_context.cc b/content/browser/background_fetch/background_fetch_context.cc
index 2e3f23d..cc1cb4b 100644
--- a/content/browser/background_fetch/background_fetch_context.cc
+++ b/content/browser/background_fetch/background_fetch_context.cc
@@ -69,6 +69,14 @@
weak_factory_.GetWeakPtr(), std::move(callback)));
}
+void BackgroundFetchContext::GetDeveloperIdsForServiceWorker(
+ int64_t service_worker_registration_id,
+ const url::Origin& origin,
+ blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback) {
+ data_manager_.GetDeveloperIdsForServiceWorker(service_worker_registration_id,
+ origin, std::move(callback));
+}
+
void BackgroundFetchContext::DidGetRegistration(
blink::mojom::BackgroundFetchService::GetRegistrationCallback callback,
blink::mojom::BackgroundFetchError error,
diff --git a/content/browser/background_fetch/background_fetch_context.h b/content/browser/background_fetch/background_fetch_context.h
index 148e4ad1..8692ef07 100644
--- a/content/browser/background_fetch/background_fetch_context.h
+++ b/content/browser/background_fetch/background_fetch_context.h
@@ -57,6 +57,13 @@
const std::string& developer_id,
blink::mojom::BackgroundFetchService::GetRegistrationCallback callback);
+ // Gets all the Background Fetch registration |developer_id|s for a Service
+ // Worker and invokes |callback| with that list.
+ void GetDeveloperIdsForServiceWorker(
+ int64_t service_worker_registration_id,
+ const url::Origin& origin,
+ blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback);
+
// Starts a Background Fetch for the |registration_id|. The |requests| will be
// asynchronously fetched. The |callback| will be invoked when the fetch has
// been registered, or an error occurred that prevents it from doing so.
@@ -86,8 +93,6 @@
const std::string& title,
blink::mojom::BackgroundFetchService::UpdateUICallback callback);
- BackgroundFetchDataManager& data_manager() { return data_manager_; }
-
private:
friend class base::DeleteHelper<BackgroundFetchContext>;
friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
diff --git a/content/browser/background_fetch/background_fetch_data_manager.cc b/content/browser/background_fetch/background_fetch_data_manager.cc
index c0b0293e..5eefc9f9 100644
--- a/content/browser/background_fetch/background_fetch_data_manager.cc
+++ b/content/browser/background_fetch/background_fetch_data_manager.cc
@@ -494,13 +494,16 @@
void BackgroundFetchDataManager::GetDeveloperIdsForServiceWorker(
int64_t service_worker_registration_id,
+ const url::Origin& origin,
blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
std::vector<std::string> developer_ids;
for (const auto& entry : active_registration_unique_ids_) {
- if (service_worker_registration_id == std::get<0>(entry.first))
+ if (service_worker_registration_id == std::get<0>(entry.first) &&
+ origin == std::get<1>(entry.first)) {
developer_ids.emplace_back(std::get<2>(entry.first));
+ }
}
std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE,
diff --git a/content/browser/background_fetch/background_fetch_data_manager.h b/content/browser/background_fetch/background_fetch_data_manager.h
index c4435074..c901b40 100644
--- a/content/browser/background_fetch/background_fetch_data_manager.h
+++ b/content/browser/background_fetch/background_fetch_data_manager.h
@@ -17,6 +17,7 @@
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h"
+#include "content/browser/background_fetch/background_fetch_request_manager.h"
#include "content/browser/background_fetch/storage/database_task.h"
#include "content/common/content_export.h"
#include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h"
@@ -45,12 +46,9 @@
// Worker code to remove a ServiceWorkerRegistration and all its keys).
//
// Storage schema is documented in storage/README.md
-class CONTENT_EXPORT BackgroundFetchDataManager {
+class CONTENT_EXPORT BackgroundFetchDataManager
+ : public BackgroundFetchRequestManager {
public:
- using NextRequestCallback =
- base::OnceCallback<void(scoped_refptr<BackgroundFetchRequestInfo>)>;
- using MarkedCompleteCallback =
- base::OnceCallback<void(bool /* has_pending_or_active_requests */)>;
using SettledFetchesCallback = base::OnceCallback<void(
blink::mojom::BackgroundFetchError,
bool /* background_fetch_succeeded */,
@@ -63,7 +61,7 @@
BackgroundFetchDataManager(
BrowserContext* browser_context,
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
- ~BackgroundFetchDataManager();
+ ~BackgroundFetchDataManager() override;
// Creates and stores a new registration with the given properties. Will
// invoke the |callback| when the registration has been created, which may
@@ -86,25 +84,6 @@
const std::string& title,
blink::mojom::BackgroundFetchService::UpdateUICallback callback);
- // Removes the next request, if any, from the pending requests queue, and
- // invokes the |callback| with that request, else a null request.
- void PopNextRequest(const BackgroundFetchRegistrationId& registration_id,
- NextRequestCallback callback);
-
- // Marks that the |request|, part of the Background Fetch identified by
- // |registration_id|, has been started as |download_guid|.
- void MarkRequestAsStarted(
- const BackgroundFetchRegistrationId& registration_id,
- BackgroundFetchRequestInfo* request,
- const std::string& download_guid);
-
- // Marks that the |request|, part of the Background Fetch identified by
- // |registration_id|, has completed.
- void MarkRequestAsComplete(
- const BackgroundFetchRegistrationId& registration_id,
- BackgroundFetchRequestInfo* request,
- MarkedCompleteCallback callback);
-
// Reads all settled fetches for the given |registration_id|. Both the Request
// and Response objects will be initialised based on the stored data. Will
// invoke the |callback| when the list of fetches has been compiled.
@@ -137,11 +116,24 @@
// Worker.
void GetDeveloperIdsForServiceWorker(
int64_t service_worker_registration_id,
+ const url::Origin& origin,
blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback);
int GetTotalNumberOfRequests(
const BackgroundFetchRegistrationId& registration_id) const;
+ // BackgroundFetchRequestManager implementation:
+ void PopNextRequest(const BackgroundFetchRegistrationId& registration_id,
+ NextRequestCallback callback) override;
+ void MarkRequestAsStarted(
+ const BackgroundFetchRegistrationId& registration_id,
+ BackgroundFetchRequestInfo* request,
+ const std::string& download_guid) override;
+ void MarkRequestAsComplete(
+ const BackgroundFetchRegistrationId& registration_id,
+ BackgroundFetchRequestInfo* request,
+ MarkedCompleteCallback callback) override;
+
private:
FRIEND_TEST_ALL_PREFIXES(BackgroundFetchDataManagerTest, Cleanup);
friend class BackgroundFetchDataManagerTest;
diff --git a/content/browser/background_fetch/background_fetch_job_controller.cc b/content/browser/background_fetch/background_fetch_job_controller.cc
index f2f0015..77d74f2 100644
--- a/content/browser/background_fetch/background_fetch_job_controller.cc
+++ b/content/browser/background_fetch/background_fetch_job_controller.cc
@@ -7,6 +7,7 @@
#include <utility>
#include "base/memory/ptr_util.h"
+#include "content/browser/background_fetch/background_fetch_request_manager.h"
#include "content/public/browser/browser_thread.h"
namespace content {
@@ -16,13 +17,13 @@
const BackgroundFetchRegistrationId& registration_id,
const BackgroundFetchOptions& options,
const BackgroundFetchRegistration& registration,
- BackgroundFetchDataManager* data_manager,
+ BackgroundFetchRequestManager* request_manager,
ProgressCallback progress_callback,
FinishedCallback finished_callback)
: registration_id_(registration_id),
options_(options),
complete_requests_downloaded_bytes_cache_(registration.downloaded),
- data_manager_(data_manager),
+ request_manager_(request_manager),
delegate_proxy_(delegate_proxy),
progress_callback_(std::move(progress_callback)),
finished_callback_(std::move(finished_callback)),
@@ -51,7 +52,7 @@
// TODO(crbug.com/741609): Enforce kMaximumBackgroundFetchParallelRequests
// globally and/or per origin rather than per fetch.
for (size_t i = 0; i < kMaximumBackgroundFetchParallelRequests; i++) {
- data_manager_->PopNextRequest(
+ request_manager_->PopNextRequest(
registration_id_,
base::BindOnce(&BackgroundFetchJobController::StartRequest,
weak_ptr_factory_.GetWeakPtr()));
@@ -77,8 +78,8 @@
const scoped_refptr<BackgroundFetchRequestInfo>& request,
const std::string& download_guid) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- data_manager_->MarkRequestAsStarted(registration_id_, request.get(),
- download_guid);
+ request_manager_->MarkRequestAsStarted(registration_id_, request.get(),
+ download_guid);
}
void BackgroundFetchJobController::DidUpdateRequest(
@@ -102,14 +103,13 @@
const std::string& download_guid) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- // This request is no longer in-progress, so the DataManager will take over
- // responsibility for storing its downloaded bytes, though still need a cache.
active_request_download_bytes_.erase(download_guid);
complete_requests_downloaded_bytes_cache_ += request->GetFileSize();
- // The DataManager must acknowledge that it stored the data and that there are
- // no more pending requests to avoid marking this job as completed too early.
- data_manager_->MarkRequestAsComplete(
+ // The RequestManager must acknowledge that it stored the data and that there
+ // are no more pending requests to avoid marking this job as completed too
+ // early.
+ request_manager_->MarkRequestAsComplete(
registration_id_, request.get(),
base::BindOnce(&BackgroundFetchJobController::DidMarkRequestCompleted,
weak_ptr_factory_.GetWeakPtr()));
@@ -122,7 +122,7 @@
// If not all requests have completed, start a pending request if there are
// any left, and bail.
if (has_pending_or_active_requests) {
- data_manager_->PopNextRequest(
+ request_manager_->PopNextRequest(
registration_id_,
base::BindOnce(&BackgroundFetchJobController::StartRequest,
weak_ptr_factory_.GetWeakPtr()));
diff --git a/content/browser/background_fetch/background_fetch_job_controller.h b/content/browser/background_fetch/background_fetch_job_controller.h
index 510b830..b0d7725 100644
--- a/content/browser/background_fetch/background_fetch_job_controller.h
+++ b/content/browser/background_fetch/background_fetch_job_controller.h
@@ -13,7 +13,6 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
-#include "content/browser/background_fetch/background_fetch_data_manager.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_request_info.h"
@@ -23,13 +22,15 @@
namespace content {
+class BackgroundFetchRequestManager;
+
// The JobController will be responsible for coordinating communication with the
-// DownloadManager. It will get requests from the DataManager and dispatch them
-// to the DownloadManager. It lives entirely on the IO thread.
+// DownloadManager. It will get requests from the RequestManager and dispatch
+// them to the DownloadService. It lives entirely on the IO thread.
//
// Lifetime: It is created lazily only once a Background Fetch registration
// starts downloading, and it is destroyed once no more communication with the
-// DownloadManager or Offline Items Collection is necessary (i.e. once the
+// DownloadService or Offline Items Collection is necessary (i.e. once the
// registration has been aborted, or once it has completed/failed and the
// waitUntil promise has been resolved so UpdateUI can no longer be called).
class CONTENT_EXPORT BackgroundFetchJobController final
@@ -47,7 +48,7 @@
const BackgroundFetchRegistrationId& registration_id,
const BackgroundFetchOptions& options,
const BackgroundFetchRegistration& registration,
- BackgroundFetchDataManager* data_manager,
+ BackgroundFetchRequestManager* request_manager,
ProgressCallback progress_callback,
FinishedCallback finished_callback);
~BackgroundFetchJobController() override;
@@ -122,9 +123,9 @@
// delivering progress events without having to read from the database.
uint64_t complete_requests_downloaded_bytes_cache_;
- // The DataManager's lifetime is controlled by the BackgroundFetchContext and
- // will be kept alive until after the JobController is destroyed.
- BackgroundFetchDataManager* data_manager_;
+ // The RequestManager's lifetime is controlled by the BackgroundFetchContext
+ // and will be kept alive until after the JobController is destroyed.
+ BackgroundFetchRequestManager* request_manager_;
// Proxy for interacting with the BackgroundFetchDelegate across thread
// boundaries. It is owned by the BackgroundFetchContext.
diff --git a/content/browser/background_fetch/background_fetch_request_manager.h b/content/browser/background_fetch/background_fetch_request_manager.h
new file mode 100644
index 0000000..f987104
--- /dev/null
+++ b/content/browser/background_fetch/background_fetch_request_manager.h
@@ -0,0 +1,53 @@
+// 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_REQUEST_MANAGER_H_
+#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_REQUEST_MANAGER_H_
+
+#include <string>
+
+#include "base/callback_forward.h"
+#include "base/memory/scoped_refptr.h"
+
+namespace content {
+
+class BackgroundFetchRegistrationId;
+class BackgroundFetchRequestInfo;
+
+// Interface for manager requests that are part of a Background Fetch.
+// Implementations maintain a queue of requests for each given
+// |BackgroundFetchRegistrationId| that may be backed by a database.
+class BackgroundFetchRequestManager {
+ public:
+ using NextRequestCallback =
+ base::OnceCallback<void(scoped_refptr<BackgroundFetchRequestInfo>)>;
+ using MarkedCompleteCallback =
+ base::OnceCallback<void(bool /* has_pending_or_active_requests */)>;
+
+ virtual ~BackgroundFetchRequestManager() {}
+
+ // Removes the next request, if any, from the pending requests queue, and
+ // invokes the |callback| with that request, else a null request.
+ virtual void PopNextRequest(
+ const BackgroundFetchRegistrationId& registration_id,
+ NextRequestCallback callback) = 0;
+
+ // Marks that the |request|, part of the Background Fetch identified by
+ // |registration_id|, has been started as |download_guid|.
+ virtual void MarkRequestAsStarted(
+ const BackgroundFetchRegistrationId& registration_id,
+ BackgroundFetchRequestInfo* request,
+ const std::string& download_guid) = 0;
+
+ // Marks that the |request|, part of the Background Fetch identified by
+ // |registration_id|, has completed.
+ virtual void MarkRequestAsComplete(
+ const BackgroundFetchRegistrationId& registration_id,
+ BackgroundFetchRequestInfo* request,
+ MarkedCompleteCallback callback) = 0;
+};
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_REQUEST_MANAGER_H_
diff --git a/content/browser/background_fetch/background_fetch_service_impl.cc b/content/browser/background_fetch/background_fetch_service_impl.cc
index 7328951..b4f9e6c 100644
--- a/content/browser/background_fetch/background_fetch_service_impl.cc
+++ b/content/browser/background_fetch/background_fetch_service_impl.cc
@@ -137,8 +137,8 @@
const url::Origin& origin,
GetDeveloperIdsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- background_fetch_context_->data_manager().GetDeveloperIdsForServiceWorker(
- service_worker_registration_id, std::move(callback));
+ background_fetch_context_->GetDeveloperIdsForServiceWorker(
+ service_worker_registration_id, origin, std::move(callback));
}
void BackgroundFetchServiceImpl::AddRegistrationObserver(
diff --git a/content/browser/background_fetch/background_fetch_service_unittest.cc b/content/browser/background_fetch/background_fetch_service_unittest.cc
index 2f8bf98..be45450 100644
--- a/content/browser/background_fetch/background_fetch_service_unittest.cc
+++ b/content/browser/background_fetch/background_fetch_service_unittest.cc
@@ -123,6 +123,7 @@
// Synchronous wrapper for BackgroundFetchServiceImpl::GetDeveloperIds().
void GetDeveloperIds(int64_t service_worker_registration_id,
+ const url::Origin& origin,
blink::mojom::BackgroundFetchError* out_error,
std::vector<std::string>* out_developer_ids) {
DCHECK(out_error);
@@ -130,7 +131,7 @@
base::RunLoop run_loop;
service_->GetDeveloperIds(
- service_worker_registration_id, origin(),
+ service_worker_registration_id, origin,
base::BindOnce(&BackgroundFetchServiceTest::DidGetDeveloperIds,
base::Unretained(this), run_loop.QuitClosure(),
out_error, out_developer_ids));
@@ -825,7 +826,8 @@
blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids;
- GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
+ GetDeveloperIds(service_worker_registration_id, origin(), &error,
+ &developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 0u);
@@ -846,7 +848,8 @@
blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids;
- GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
+ GetDeveloperIds(service_worker_registration_id, origin(), &error,
+ &developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 1u);
@@ -868,7 +871,8 @@
blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids;
- GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
+ GetDeveloperIds(service_worker_registration_id, origin(), &error,
+ &developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 2u);
@@ -879,6 +883,36 @@
EXPECT_TRUE(developer_ids[0] == kAlternativeDeveloperId ||
developer_ids[1] == kAlternativeDeveloperId);
}
+
+ // Verify that using the wrong origin does not return developer ids even if
+ // the service worker registration is correct.
+ {
+ blink::mojom::BackgroundFetchError error;
+ std::vector<std::string> developer_ids;
+
+ GetDeveloperIds(service_worker_registration_id,
+ url::Origin::Create(GURL("https://www.bogus-origin.com")),
+ &error, &developer_ids);
+ ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
+
+ ASSERT_EQ(developer_ids.size(), 0u);
+ }
+
+ // Verify that using the wrong service worker id does not return developer ids
+ // even if the origin is correct.
+ {
+ blink::mojom::BackgroundFetchError error;
+ std::vector<std::string> developer_ids;
+
+ int64_t bogus_service_worker_registration_id =
+ service_worker_registration_id + 1;
+
+ GetDeveloperIds(bogus_service_worker_registration_id, origin(), &error,
+ &developer_ids);
+ ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
+
+ ASSERT_EQ(developer_ids.size(), 0u);
+ }
}
} // namespace