Commit 54bc028e authored by Sam McNally's avatar Sam McNally Committed by Commit Bot

Remove origins from the BackgroundFetchService interface messages.

Move registration of the BackgroundFetchService interface from
RenderProcessHost to RendererInterfaceBinders so its implementation can
receive a browser-tracked origin for its renderer-process client.

Add support for move-only types to base::AutoReset. Use it in the
BackgroundFetchServiceImpl test to swap in a service instance with a
different origin for the part of the test that previously passed a
different origin to the mojo interface.

Bug: 779444
Change-Id: I68737625cb5cd0a2843eeb46643aad3b4f852d8c
Reviewed-on: https://chromium-review.googlesource.com/788641Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Sam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523661}
parent 2b77b931
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef BASE_AUTO_RESET_H_ #ifndef BASE_AUTO_RESET_H_
#define BASE_AUTO_RESET_H_ #define BASE_AUTO_RESET_H_
#include <utility>
#include "base/macros.h" #include "base/macros.h"
// base::AutoReset<> is useful for setting a variable to a new value only within // base::AutoReset<> is useful for setting a variable to a new value only within
...@@ -23,11 +25,11 @@ class AutoReset { ...@@ -23,11 +25,11 @@ class AutoReset {
public: public:
AutoReset(T* scoped_variable, T new_value) AutoReset(T* scoped_variable, T new_value)
: scoped_variable_(scoped_variable), : scoped_variable_(scoped_variable),
original_value_(*scoped_variable) { original_value_(std::move(*scoped_variable)) {
*scoped_variable_ = new_value; *scoped_variable_ = std::move(new_value);
} }
~AutoReset() { *scoped_variable_ = original_value_; } ~AutoReset() { *scoped_variable_ = std::move(original_value_); }
private: private:
T* scoped_variable_; T* scoped_variable_;
......
...@@ -10,10 +10,11 @@ ...@@ -10,10 +10,11 @@
#include "content/browser/background_fetch/background_fetch_context.h" #include "content/browser/background_fetch/background_fetch_context.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h" #include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/bad_message.h" #include "content/browser/bad_message.h"
#include "content/browser/storage_partition_impl.h"
#include "content/common/service_worker/service_worker_types.h" #include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/strong_binding.h" #include "mojo/public/cpp/bindings/strong_binding.h"
#include "url/origin.h"
namespace content { namespace content {
...@@ -29,21 +30,37 @@ constexpr size_t kMaxTitleLength = 1024 * 1024; ...@@ -29,21 +30,37 @@ constexpr size_t kMaxTitleLength = 1024 * 1024;
// static // static
void BackgroundFetchServiceImpl::Create( void BackgroundFetchServiceImpl::Create(
int render_process_id, blink::mojom::BackgroundFetchServiceRequest request,
RenderProcessHost* render_process_host,
const url::Origin& origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(
BackgroundFetchServiceImpl::CreateOnIoThread,
WrapRefCounted(static_cast<StoragePartitionImpl*>(
render_process_host->GetStoragePartition())
->GetBackgroundFetchContext()),
origin, std::move(request)));
}
// static
void BackgroundFetchServiceImpl::CreateOnIoThread(
scoped_refptr<BackgroundFetchContext> background_fetch_context, scoped_refptr<BackgroundFetchContext> background_fetch_context,
url::Origin origin,
blink::mojom::BackgroundFetchServiceRequest request) { blink::mojom::BackgroundFetchServiceRequest request) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
mojo::MakeStrongBinding( mojo::MakeStrongBinding(
std::make_unique<BackgroundFetchServiceImpl>( std::make_unique<BackgroundFetchServiceImpl>(
render_process_id, std::move(background_fetch_context)), std::move(background_fetch_context), std::move(origin)),
std::move(request)); std::move(request));
} }
BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( BackgroundFetchServiceImpl::BackgroundFetchServiceImpl(
int render_process_id, scoped_refptr<BackgroundFetchContext> background_fetch_context,
scoped_refptr<BackgroundFetchContext> background_fetch_context) url::Origin origin)
: render_process_id_(render_process_id), : background_fetch_context_(std::move(background_fetch_context)),
background_fetch_context_(std::move(background_fetch_context)) { origin_(std::move(origin)) {
DCHECK(background_fetch_context_); DCHECK(background_fetch_context_);
} }
...@@ -53,7 +70,6 @@ BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() { ...@@ -53,7 +70,6 @@ BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() {
void BackgroundFetchServiceImpl::Fetch( void BackgroundFetchServiceImpl::Fetch(
int64_t service_worker_registration_id, int64_t service_worker_registration_id,
const url::Origin& origin,
const std::string& developer_id, const std::string& developer_id,
const std::vector<ServiceWorkerFetchRequest>& requests, const std::vector<ServiceWorkerFetchRequest>& requests,
const BackgroundFetchOptions& options, const BackgroundFetchOptions& options,
...@@ -76,7 +92,7 @@ void BackgroundFetchServiceImpl::Fetch( ...@@ -76,7 +92,7 @@ void BackgroundFetchServiceImpl::Fetch(
// New |unique_id|, since this is a new Background Fetch registration. This is // New |unique_id|, since this is a new Background Fetch registration. This is
// the only place new |unique_id|s should be created outside of tests. // the only place new |unique_id|s should be created outside of tests.
BackgroundFetchRegistrationId registration_id(service_worker_registration_id, BackgroundFetchRegistrationId registration_id(service_worker_registration_id,
origin, developer_id, origin_, developer_id,
base::GenerateGUID()); base::GenerateGUID());
background_fetch_context_->StartFetch(registration_id, requests, options, background_fetch_context_->StartFetch(registration_id, requests, options,
...@@ -97,7 +113,6 @@ void BackgroundFetchServiceImpl::UpdateUI(const std::string& unique_id, ...@@ -97,7 +113,6 @@ void BackgroundFetchServiceImpl::UpdateUI(const std::string& unique_id,
} }
void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id, void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id,
const url::Origin& origin,
const std::string& developer_id, const std::string& developer_id,
const std::string& unique_id, const std::string& unique_id,
AbortCallback callback) { AbortCallback callback) {
...@@ -109,14 +124,13 @@ void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id, ...@@ -109,14 +124,13 @@ void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id,
} }
background_fetch_context_->Abort( background_fetch_context_->Abort(
BackgroundFetchRegistrationId(service_worker_registration_id, origin, BackgroundFetchRegistrationId(service_worker_registration_id, origin_,
developer_id, unique_id), developer_id, unique_id),
std::move(callback)); std::move(callback));
} }
void BackgroundFetchServiceImpl::GetRegistration( void BackgroundFetchServiceImpl::GetRegistration(
int64_t service_worker_registration_id, int64_t service_worker_registration_id,
const url::Origin& origin,
const std::string& developer_id, const std::string& developer_id,
GetRegistrationCallback callback) { GetRegistrationCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -128,17 +142,16 @@ void BackgroundFetchServiceImpl::GetRegistration( ...@@ -128,17 +142,16 @@ void BackgroundFetchServiceImpl::GetRegistration(
} }
background_fetch_context_->GetRegistration(service_worker_registration_id, background_fetch_context_->GetRegistration(service_worker_registration_id,
origin, developer_id, origin_, developer_id,
std::move(callback)); std::move(callback));
} }
void BackgroundFetchServiceImpl::GetDeveloperIds( void BackgroundFetchServiceImpl::GetDeveloperIds(
int64_t service_worker_registration_id, int64_t service_worker_registration_id,
const url::Origin& origin,
GetDeveloperIdsCallback callback) { GetDeveloperIdsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
background_fetch_context_->GetDeveloperIdsForServiceWorker( background_fetch_context_->GetDeveloperIdsForServiceWorker(
service_worker_registration_id, origin, std::move(callback)); service_worker_registration_id, origin_, std::move(callback));
} }
void BackgroundFetchServiceImpl::AddRegistrationObserver( void BackgroundFetchServiceImpl::AddRegistrationObserver(
...@@ -155,8 +168,7 @@ void BackgroundFetchServiceImpl::AddRegistrationObserver( ...@@ -155,8 +168,7 @@ void BackgroundFetchServiceImpl::AddRegistrationObserver(
bool BackgroundFetchServiceImpl::ValidateDeveloperId( bool BackgroundFetchServiceImpl::ValidateDeveloperId(
const std::string& developer_id) { const std::string& developer_id) {
if (developer_id.empty() || developer_id.size() > kMaxDeveloperIdLength) { if (developer_id.empty() || developer_id.size() > kMaxDeveloperIdLength) {
bad_message::ReceivedBadMessage(render_process_id_, mojo::ReportBadMessage("Invalid developer_id");
bad_message::BFSI_INVALID_DEVELOPER_ID);
return false; return false;
} }
...@@ -166,8 +178,7 @@ bool BackgroundFetchServiceImpl::ValidateDeveloperId( ...@@ -166,8 +178,7 @@ bool BackgroundFetchServiceImpl::ValidateDeveloperId(
bool BackgroundFetchServiceImpl::ValidateUniqueId( bool BackgroundFetchServiceImpl::ValidateUniqueId(
const std::string& unique_id) { const std::string& unique_id) {
if (!base::IsValidGUIDOutputString(unique_id)) { if (!base::IsValidGUIDOutputString(unique_id)) {
bad_message::ReceivedBadMessage(render_process_id_, mojo::ReportBadMessage("Invalid unique_id");
bad_message::BFSI_INVALID_UNIQUE_ID);
return false; return false;
} }
...@@ -177,8 +188,7 @@ bool BackgroundFetchServiceImpl::ValidateUniqueId( ...@@ -177,8 +188,7 @@ bool BackgroundFetchServiceImpl::ValidateUniqueId(
bool BackgroundFetchServiceImpl::ValidateRequests( bool BackgroundFetchServiceImpl::ValidateRequests(
const std::vector<ServiceWorkerFetchRequest>& requests) { const std::vector<ServiceWorkerFetchRequest>& requests) {
if (requests.empty()) { if (requests.empty()) {
bad_message::ReceivedBadMessage(render_process_id_, mojo::ReportBadMessage("Invalid requests");
bad_message::BFSI_INVALID_REQUESTS);
return false; return false;
} }
...@@ -187,8 +197,7 @@ bool BackgroundFetchServiceImpl::ValidateRequests( ...@@ -187,8 +197,7 @@ bool BackgroundFetchServiceImpl::ValidateRequests(
bool BackgroundFetchServiceImpl::ValidateTitle(const std::string& title) { bool BackgroundFetchServiceImpl::ValidateTitle(const std::string& title) {
if (title.empty() || title.size() > kMaxTitleLength) { if (title.empty() || title.size() > kMaxTitleLength) {
bad_message::ReceivedBadMessage(render_process_id_, mojo::ReportBadMessage("Invalid title");
bad_message::BFSI_INVALID_TITLE);
return false; return false;
} }
......
...@@ -14,14 +14,12 @@ ...@@ -14,14 +14,12 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h" #include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h"
#include "url/origin.h"
namespace url {
class Origin;
}
namespace content { namespace content {
class BackgroundFetchContext; class BackgroundFetchContext;
class RenderProcessHost;
struct BackgroundFetchOptions; struct BackgroundFetchOptions;
struct ServiceWorkerFetchRequest; struct ServiceWorkerFetchRequest;
...@@ -29,18 +27,16 @@ class CONTENT_EXPORT BackgroundFetchServiceImpl ...@@ -29,18 +27,16 @@ class CONTENT_EXPORT BackgroundFetchServiceImpl
: public blink::mojom::BackgroundFetchService { : public blink::mojom::BackgroundFetchService {
public: public:
BackgroundFetchServiceImpl( BackgroundFetchServiceImpl(
int render_process_id, scoped_refptr<BackgroundFetchContext> background_fetch_context,
scoped_refptr<BackgroundFetchContext> background_fetch_context); url::Origin origin);
~BackgroundFetchServiceImpl() override; ~BackgroundFetchServiceImpl() override;
static void Create( static void Create(blink::mojom::BackgroundFetchServiceRequest request,
int render_process_id, RenderProcessHost* render_process_host,
scoped_refptr<BackgroundFetchContext> background_fetch_context, const url::Origin& origin);
blink::mojom::BackgroundFetchServiceRequest request);
// blink::mojom::BackgroundFetchService implementation. // blink::mojom::BackgroundFetchService implementation.
void Fetch(int64_t service_worker_registration_id, void Fetch(int64_t service_worker_registration_id,
const url::Origin& origin,
const std::string& developer_id, const std::string& developer_id,
const std::vector<ServiceWorkerFetchRequest>& requests, const std::vector<ServiceWorkerFetchRequest>& requests,
const BackgroundFetchOptions& options, const BackgroundFetchOptions& options,
...@@ -49,22 +45,24 @@ class CONTENT_EXPORT BackgroundFetchServiceImpl ...@@ -49,22 +45,24 @@ class CONTENT_EXPORT BackgroundFetchServiceImpl
const std::string& title, const std::string& title,
UpdateUICallback callback) override; UpdateUICallback callback) override;
void Abort(int64_t service_worker_registration_id, void Abort(int64_t service_worker_registration_id,
const url::Origin& origin,
const std::string& developer_id, const std::string& developer_id,
const std::string& unique_id, const std::string& unique_id,
AbortCallback callback) override; AbortCallback callback) override;
void GetRegistration(int64_t service_worker_registration_id, void GetRegistration(int64_t service_worker_registration_id,
const url::Origin& origin,
const std::string& developer_id, const std::string& developer_id,
GetRegistrationCallback callback) override; GetRegistrationCallback callback) override;
void GetDeveloperIds(int64_t service_worker_registration_id, void GetDeveloperIds(int64_t service_worker_registration_id,
const url::Origin& origin,
GetDeveloperIdsCallback callback) override; GetDeveloperIdsCallback callback) override;
void AddRegistrationObserver( void AddRegistrationObserver(
const std::string& unique_id, const std::string& unique_id,
blink::mojom::BackgroundFetchRegistrationObserverPtr observer) override; blink::mojom::BackgroundFetchRegistrationObserverPtr observer) override;
private: private:
static void CreateOnIoThread(
scoped_refptr<BackgroundFetchContext> background_fetch_context,
url::Origin origin,
blink::mojom::BackgroundFetchServiceRequest request);
// Validates and returns whether the |developer_id|, |unique_id|, |requests| // Validates and returns whether the |developer_id|, |unique_id|, |requests|
// and |title| respectively have valid values. The renderer will be flagged // and |title| respectively have valid values. The renderer will be flagged
// for having sent a bad message if the values are invalid. // for having sent a bad message if the values are invalid.
...@@ -74,12 +72,11 @@ class CONTENT_EXPORT BackgroundFetchServiceImpl ...@@ -74,12 +72,11 @@ class CONTENT_EXPORT BackgroundFetchServiceImpl
WARN_UNUSED_RESULT; WARN_UNUSED_RESULT;
bool ValidateTitle(const std::string& title) WARN_UNUSED_RESULT; bool ValidateTitle(const std::string& title) WARN_UNUSED_RESULT;
// Id of the renderer process that this service has been created for.
int render_process_id_;
// The Background Fetch context on which operations will be dispatched. // The Background Fetch context on which operations will be dispatched.
scoped_refptr<BackgroundFetchContext> background_fetch_context_; scoped_refptr<BackgroundFetchContext> background_fetch_context_;
const url::Origin origin_;
DISALLOW_COPY_AND_ASSIGN(BackgroundFetchServiceImpl); DISALLOW_COPY_AND_ASSIGN(BackgroundFetchServiceImpl);
}; };
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "base/auto_reset.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/run_loop.h" #include "base/run_loop.h"
...@@ -18,6 +19,8 @@ ...@@ -18,6 +19,8 @@
#include "content/browser/service_worker/service_worker_context_wrapper.h" #include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h" #include "content/browser/storage_partition_impl.h"
#include "content/common/service_worker/service_worker_types.h" #include "content/common/service_worker/service_worker_types.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/public/cpp/bindings/message.h"
#include "services/network/public/interfaces/fetch_api.mojom.h" #include "services/network/public/interfaces/fetch_api.mojom.h"
namespace content { namespace content {
...@@ -38,11 +41,52 @@ IconDefinition CreateIcon(std::string src, ...@@ -38,11 +41,52 @@ IconDefinition CreateIcon(std::string src,
return icon; return icon;
} }
class BadMessageObserver {
public:
BadMessageObserver()
: dummy_message_(0, 0, 0, 0, nullptr), context_(&dummy_message_) {
mojo::edk::SetDefaultProcessErrorCallback(base::BindRepeating(
&BadMessageObserver::ReportBadMessage, base::Unretained(this)));
}
~BadMessageObserver() {
mojo::edk::SetDefaultProcessErrorCallback(
mojo::edk::ProcessErrorCallback());
}
const std::string& last_error() const { return last_error_; }
private:
void ReportBadMessage(const std::string& error) { last_error_ = error; }
mojo::Message dummy_message_;
mojo::internal::MessageDispatchContext context_;
std::string last_error_;
DISALLOW_COPY_AND_ASSIGN(BadMessageObserver);
};
class BackgroundFetchServiceTest : public BackgroundFetchTestBase { class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
public: public:
BackgroundFetchServiceTest() = default; BackgroundFetchServiceTest() = default;
~BackgroundFetchServiceTest() override = default; ~BackgroundFetchServiceTest() override = default;
class ScopedCustomBackgroundFetchService {
public:
ScopedCustomBackgroundFetchService(BackgroundFetchServiceTest* test,
const url::Origin& origin)
: scoped_service_(
&test->service_,
std::make_unique<BackgroundFetchServiceImpl>(test->context_,
origin)) {}
private:
base::AutoReset<std::unique_ptr<BackgroundFetchServiceImpl>>
scoped_service_;
DISALLOW_COPY_AND_ASSIGN(ScopedCustomBackgroundFetchService);
};
// Synchronous wrapper for BackgroundFetchServiceImpl::Fetch(). // Synchronous wrapper for BackgroundFetchServiceImpl::Fetch().
BackgroundFetchRegistrationId Fetch( BackgroundFetchRegistrationId Fetch(
int64_t service_worker_registration_id, int64_t service_worker_registration_id,
...@@ -56,8 +100,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase { ...@@ -56,8 +100,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
base::RunLoop run_loop; base::RunLoop run_loop;
service_->Fetch( service_->Fetch(
service_worker_registration_id, origin(), developer_id, requests, service_worker_registration_id, developer_id, requests, options,
options,
base::BindOnce(&BackgroundFetchServiceTest::DidGetRegistration, base::BindOnce(&BackgroundFetchServiceTest::DidGetRegistration,
base::Unretained(this), run_loop.QuitClosure(), base::Unretained(this), run_loop.QuitClosure(),
out_error, out_registration)); out_error, out_registration));
...@@ -94,8 +137,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase { ...@@ -94,8 +137,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
DCHECK(out_error); DCHECK(out_error);
base::RunLoop run_loop; base::RunLoop run_loop;
service_->Abort(service_worker_registration_id, origin(), developer_id, service_->Abort(service_worker_registration_id, developer_id, unique_id,
unique_id,
base::BindOnce(&BackgroundFetchServiceTest::DidGetError, base::BindOnce(&BackgroundFetchServiceTest::DidGetError,
base::Unretained(this), base::Unretained(this),
run_loop.QuitClosure(), out_error)); run_loop.QuitClosure(), out_error));
...@@ -113,7 +155,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase { ...@@ -113,7 +155,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
base::RunLoop run_loop; base::RunLoop run_loop;
service_->GetRegistration( service_->GetRegistration(
service_worker_registration_id, origin(), developer_id, service_worker_registration_id, developer_id,
base::BindOnce(&BackgroundFetchServiceTest::DidGetRegistration, base::BindOnce(&BackgroundFetchServiceTest::DidGetRegistration,
base::Unretained(this), run_loop.QuitClosure(), base::Unretained(this), run_loop.QuitClosure(),
out_error, out_registration)); out_error, out_registration));
...@@ -123,7 +165,6 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase { ...@@ -123,7 +165,6 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
// Synchronous wrapper for BackgroundFetchServiceImpl::GetDeveloperIds(). // Synchronous wrapper for BackgroundFetchServiceImpl::GetDeveloperIds().
void GetDeveloperIds(int64_t service_worker_registration_id, void GetDeveloperIds(int64_t service_worker_registration_id,
const url::Origin& origin,
blink::mojom::BackgroundFetchError* out_error, blink::mojom::BackgroundFetchError* out_error,
std::vector<std::string>* out_developer_ids) { std::vector<std::string>* out_developer_ids) {
DCHECK(out_error); DCHECK(out_error);
...@@ -131,7 +172,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase { ...@@ -131,7 +172,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
base::RunLoop run_loop; base::RunLoop run_loop;
service_->GetDeveloperIds( service_->GetDeveloperIds(
service_worker_registration_id, origin, service_worker_registration_id,
base::BindOnce(&BackgroundFetchServiceTest::DidGetDeveloperIds, base::BindOnce(&BackgroundFetchServiceTest::DidGetDeveloperIds,
base::Unretained(this), run_loop.QuitClosure(), base::Unretained(this), run_loop.QuitClosure(),
out_error, out_developer_ids)); out_error, out_developer_ids));
...@@ -147,8 +188,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase { ...@@ -147,8 +188,7 @@ class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
browser_context(), browser_context(),
base::WrapRefCounted(embedded_worker_test_helper()->context_wrapper())); base::WrapRefCounted(embedded_worker_test_helper()->context_wrapper()));
service_ = std::make_unique<BackgroundFetchServiceImpl>( service_ = std::make_unique<BackgroundFetchServiceImpl>(context_, origin());
0 /* render_process_id */, context_);
} }
void TearDown() override { void TearDown() override {
...@@ -210,6 +250,7 @@ TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) { ...@@ -210,6 +250,7 @@ TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) {
// The |developer_id| must be a non-empty string. // The |developer_id| must be a non-empty string.
{ {
BadMessageObserver bad_message_observer;
std::vector<ServiceWorkerFetchRequest> requests; std::vector<ServiceWorkerFetchRequest> requests;
requests.emplace_back(); // empty, but valid requests.emplace_back(); // empty, but valid
...@@ -219,10 +260,12 @@ TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) { ...@@ -219,10 +260,12 @@ TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) {
Fetch(42 /* service_worker_registration_id */, "" /* developer_id */, Fetch(42 /* service_worker_registration_id */, "" /* developer_id */,
requests, options, &error, &registration); requests, options, &error, &registration);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
EXPECT_EQ("Invalid developer_id", bad_message_observer.last_error());
} }
// At least a single ServiceWorkerFetchRequest must be given. // At least a single ServiceWorkerFetchRequest must be given.
{ {
BadMessageObserver bad_message_observer;
std::vector<ServiceWorkerFetchRequest> requests; std::vector<ServiceWorkerFetchRequest> requests;
// |requests| has deliberately been left empty. // |requests| has deliberately been left empty.
...@@ -232,6 +275,7 @@ TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) { ...@@ -232,6 +275,7 @@ TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) {
Fetch(42 /* service_worker_registration_id */, kExampleDeveloperId, Fetch(42 /* service_worker_registration_id */, kExampleDeveloperId,
requests, options, &error, &registration); requests, options, &error, &registration);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
EXPECT_EQ("Invalid requests", bad_message_observer.last_error());
} }
} }
...@@ -598,10 +642,12 @@ TEST_F(BackgroundFetchServiceTest, AbortInvalidDeveloperIdArgument) { ...@@ -598,10 +642,12 @@ TEST_F(BackgroundFetchServiceTest, AbortInvalidDeveloperIdArgument) {
// return INVALID_ARGUMENT when an invalid |developer_id| is sent over the // return INVALID_ARGUMENT when an invalid |developer_id| is sent over the
// Mojo channel. // Mojo channel.
BadMessageObserver bad_message_observer;
blink::mojom::BackgroundFetchError error; blink::mojom::BackgroundFetchError error;
Abort(42 /* service_worker_registration_id */, "" /* developer_id */, Abort(42 /* service_worker_registration_id */, "" /* developer_id */,
kExampleUniqueId, &error); kExampleUniqueId, &error);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
EXPECT_EQ("Invalid developer_id", bad_message_observer.last_error());
} }
TEST_F(BackgroundFetchServiceTest, AbortInvalidUniqueIdArgument) { TEST_F(BackgroundFetchServiceTest, AbortInvalidUniqueIdArgument) {
...@@ -609,10 +655,12 @@ TEST_F(BackgroundFetchServiceTest, AbortInvalidUniqueIdArgument) { ...@@ -609,10 +655,12 @@ TEST_F(BackgroundFetchServiceTest, AbortInvalidUniqueIdArgument) {
// return INVALID_ARGUMENT when an invalid |unique_id| is sent over the Mojo // return INVALID_ARGUMENT when an invalid |unique_id| is sent over the Mojo
// channel. // channel.
BadMessageObserver bad_message_observer;
blink::mojom::BackgroundFetchError error; blink::mojom::BackgroundFetchError error;
Abort(42 /* service_worker_registration_id */, kExampleDeveloperId, Abort(42 /* service_worker_registration_id */, kExampleDeveloperId,
"not a GUID" /* unique_id */, &error); "not a GUID" /* unique_id */, &error);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
EXPECT_EQ("Invalid unique_id", bad_message_observer.last_error());
} }
TEST_F(BackgroundFetchServiceTest, AbortUnknownUniqueId) { TEST_F(BackgroundFetchServiceTest, AbortUnknownUniqueId) {
...@@ -805,8 +853,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) { ...@@ -805,8 +853,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) {
blink::mojom::BackgroundFetchError error; blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids; std::vector<std::string> developer_ids;
GetDeveloperIds(service_worker_registration_id, origin(), &error, GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
&developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 0u); ASSERT_EQ(developer_ids.size(), 0u);
...@@ -827,8 +874,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) { ...@@ -827,8 +874,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) {
blink::mojom::BackgroundFetchError error; blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids; std::vector<std::string> developer_ids;
GetDeveloperIds(service_worker_registration_id, origin(), &error, GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
&developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 1u); ASSERT_EQ(developer_ids.size(), 1u);
...@@ -850,8 +896,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) { ...@@ -850,8 +896,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) {
blink::mojom::BackgroundFetchError error; blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids; std::vector<std::string> developer_ids;
GetDeveloperIds(service_worker_registration_id, origin(), &error, GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
&developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 2u); ASSERT_EQ(developer_ids.size(), 2u);
...@@ -866,12 +911,12 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) { ...@@ -866,12 +911,12 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) {
// Verify that using the wrong origin does not return developer ids even if // Verify that using the wrong origin does not return developer ids even if
// the service worker registration is correct. // the service worker registration is correct.
{ {
ScopedCustomBackgroundFetchService scoped_bogus_url_service(
this, url::Origin::Create(GURL("https://www.bogus-origin.com")));
blink::mojom::BackgroundFetchError error; blink::mojom::BackgroundFetchError error;
std::vector<std::string> developer_ids; std::vector<std::string> developer_ids;
GetDeveloperIds(service_worker_registration_id, GetDeveloperIds(service_worker_registration_id, &error, &developer_ids);
url::Origin::Create(GURL("https://www.bogus-origin.com")),
&error, &developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
ASSERT_EQ(developer_ids.size(), 0u); ASSERT_EQ(developer_ids.size(), 0u);
...@@ -886,7 +931,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) { ...@@ -886,7 +931,7 @@ TEST_F(BackgroundFetchServiceTest, GetDeveloperIds) {
int64_t bogus_service_worker_registration_id = int64_t bogus_service_worker_registration_id =
service_worker_registration_id + 1; service_worker_registration_id + 1;
GetDeveloperIds(bogus_service_worker_registration_id, origin(), &error, GetDeveloperIds(bogus_service_worker_registration_id, &error,
&developer_ids); &developer_ids);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
......
...@@ -1888,11 +1888,6 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() { ...@@ -1888,11 +1888,6 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() {
base::Bind(&PushMessagingManager::BindRequest, base::Bind(&PushMessagingManager::BindRequest,
base::Unretained(push_messaging_manager_.get()))); base::Unretained(push_messaging_manager_.get())));
registry->AddInterface(
base::Bind(&BackgroundFetchServiceImpl::Create, GetID(),
base::WrapRefCounted(
storage_partition_impl_->GetBackgroundFetchContext())));
if (gpu_client_) { if (gpu_client_) {
// |gpu_client_| outlives the registry, because its destruction is posted to // |gpu_client_| outlives the registry, because its destruction is posted to
// IO thread from the destructor of |this|. // IO thread from the destructor of |this|.
......
...@@ -140,6 +140,8 @@ void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() { ...@@ -140,6 +140,8 @@ void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
->GetPlatformNotificationContext() ->GetPlatformNotificationContext()
->CreateService(host->GetID(), origin, std::move(request)); ->CreateService(host->GetID(), origin, std::move(request));
})); }));
parameterized_binder_registry_.AddInterface(
base::BindRepeating(&BackgroundFetchServiceImpl::Create));
} }
RendererInterfaceBinders& GetRendererInterfaceBinders() { RendererInterfaceBinders& GetRendererInterfaceBinders() {
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
"ui::mojom::Gpu" "ui::mojom::Gpu"
], ],
"renderer": [ "renderer": [
"blink::mojom::BackgroundFetchService",
"blink::mojom::BackgroundSyncService", "blink::mojom::BackgroundSyncService",
"blink::mojom::BlobRegistry", "blink::mojom::BlobRegistry",
"blink::mojom::BroadcastChannelProvider", "blink::mojom::BroadcastChannelProvider",
...@@ -124,6 +123,7 @@ ...@@ -124,6 +123,7 @@
// impossible this week. Remove once sky/ken fix this. // impossible this week. Remove once sky/ken fix this.
"autofill::mojom::AutofillDriver", "autofill::mojom::AutofillDriver",
"autofill::mojom::PasswordManagerDriver", "autofill::mojom::PasswordManagerDriver",
"blink::mojom::BackgroundFetchService",
"blink::mojom::DedicatedWorkerFactory", "blink::mojom::DedicatedWorkerFactory",
"blink::mojom::LockManager", "blink::mojom::LockManager",
"blink::mojom::GeolocationService", "blink::mojom::GeolocationService",
...@@ -194,6 +194,7 @@ ...@@ -194,6 +194,7 @@
"navigation:service_worker": { "navigation:service_worker": {
"provides": { "provides": {
"renderer": [ "renderer": [
"blink::mojom::BackgroundFetchService",
"blink::mojom::LockManager", "blink::mojom::LockManager",
"blink::mojom::NotificationService", "blink::mojom::NotificationService",
"blink::mojom::PermissionService", "blink::mojom::PermissionService",
......
...@@ -9,10 +9,9 @@ ...@@ -9,10 +9,9 @@
#include "modules/background_fetch/BackgroundFetchRegistration.h" #include "modules/background_fetch/BackgroundFetchRegistration.h"
#include "modules/background_fetch/BackgroundFetchTypeConverters.h" #include "modules/background_fetch/BackgroundFetchTypeConverters.h"
#include "modules/background_fetch/IconDefinition.h" #include "modules/background_fetch/IconDefinition.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/Platform.h"
#include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
#include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h"
#include "services/service_manager/public/cpp/interface_provider.h"
namespace blink { namespace blink {
...@@ -50,9 +49,8 @@ void BackgroundFetchBridge::Fetch(const String& developer_id, ...@@ -50,9 +49,8 @@ void BackgroundFetchBridge::Fetch(const String& developer_id,
const BackgroundFetchOptions& options, const BackgroundFetchOptions& options,
RegistrationCallback callback) { RegistrationCallback callback) {
GetService()->Fetch( GetService()->Fetch(
GetSupplementable()->WebRegistration()->RegistrationId(), GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
GetSecurityOrigin(), developer_id, std::move(requests), std::move(requests), mojom::blink::BackgroundFetchOptions::From(options),
mojom::blink::BackgroundFetchOptions::From(options),
WTF::Bind(&BackgroundFetchBridge::DidGetRegistration, WTF::Bind(&BackgroundFetchBridge::DidGetRegistration,
WrapPersistent(this), WTF::Passed(std::move(callback)))); WrapPersistent(this), WTF::Passed(std::move(callback))));
} }
...@@ -61,8 +59,7 @@ void BackgroundFetchBridge::Abort(const String& developer_id, ...@@ -61,8 +59,7 @@ void BackgroundFetchBridge::Abort(const String& developer_id,
const String& unique_id, const String& unique_id,
AbortCallback callback) { AbortCallback callback) {
GetService()->Abort(GetSupplementable()->WebRegistration()->RegistrationId(), GetService()->Abort(GetSupplementable()->WebRegistration()->RegistrationId(),
GetSecurityOrigin(), developer_id, unique_id, developer_id, unique_id, std::move(callback));
std::move(callback));
} }
void BackgroundFetchBridge::UpdateUI(const String& developer_id, void BackgroundFetchBridge::UpdateUI(const String& developer_id,
...@@ -75,8 +72,7 @@ void BackgroundFetchBridge::UpdateUI(const String& developer_id, ...@@ -75,8 +72,7 @@ void BackgroundFetchBridge::UpdateUI(const String& developer_id,
void BackgroundFetchBridge::GetRegistration(const String& developer_id, void BackgroundFetchBridge::GetRegistration(const String& developer_id,
RegistrationCallback callback) { RegistrationCallback callback) {
GetService()->GetRegistration( GetService()->GetRegistration(
GetSupplementable()->WebRegistration()->RegistrationId(), GetSupplementable()->WebRegistration()->RegistrationId(), developer_id,
GetSecurityOrigin(), developer_id,
WTF::Bind(&BackgroundFetchBridge::DidGetRegistration, WTF::Bind(&BackgroundFetchBridge::DidGetRegistration,
WrapPersistent(this), WTF::Passed(std::move(callback)))); WrapPersistent(this), WTF::Passed(std::move(callback))));
} }
...@@ -99,7 +95,7 @@ void BackgroundFetchBridge::DidGetRegistration( ...@@ -99,7 +95,7 @@ void BackgroundFetchBridge::DidGetRegistration(
void BackgroundFetchBridge::GetDeveloperIds(GetDeveloperIdsCallback callback) { void BackgroundFetchBridge::GetDeveloperIds(GetDeveloperIdsCallback callback) {
GetService()->GetDeveloperIds( GetService()->GetDeveloperIds(
GetSupplementable()->WebRegistration()->RegistrationId(), GetSupplementable()->WebRegistration()->RegistrationId(),
GetSecurityOrigin(), std::move(callback)); std::move(callback));
} }
void BackgroundFetchBridge::AddRegistrationObserver( void BackgroundFetchBridge::AddRegistrationObserver(
...@@ -108,16 +104,16 @@ void BackgroundFetchBridge::AddRegistrationObserver( ...@@ -108,16 +104,16 @@ void BackgroundFetchBridge::AddRegistrationObserver(
GetService()->AddRegistrationObserver(unique_id, std::move(observer)); GetService()->AddRegistrationObserver(unique_id, std::move(observer));
} }
const SecurityOrigin* BackgroundFetchBridge::GetSecurityOrigin() { mojom::blink::BackgroundFetchService* BackgroundFetchBridge::GetService() {
return GetSupplementable()->GetExecutionContext()->GetSecurityOrigin();
}
mojom::blink::BackgroundFetchServicePtr& BackgroundFetchBridge::GetService() {
if (!background_fetch_service_) { if (!background_fetch_service_) {
Platform::Current()->GetInterfaceProvider()->GetInterface( auto request = mojo::MakeRequest(&background_fetch_service_);
mojo::MakeRequest(&background_fetch_service_)); if (auto* interface_provider = GetSupplementable()
->GetExecutionContext()
->GetInterfaceProvider()) {
interface_provider->GetInterface(std::move(request));
}
} }
return background_fetch_service_; return background_fetch_service_.get();
} }
} // namespace blink } // namespace blink
...@@ -89,13 +89,9 @@ class BackgroundFetchBridge final ...@@ -89,13 +89,9 @@ class BackgroundFetchBridge final
private: private:
explicit BackgroundFetchBridge(ServiceWorkerRegistration&); explicit BackgroundFetchBridge(ServiceWorkerRegistration&);
// Returns the security origin for the Service Worker registration this bridge // Returns an initialized BackgroundFetchService*. A connection will be
// is servicing, which is to be included in the Mojo calls.
const SecurityOrigin* GetSecurityOrigin();
// Returns an initialized BackgroundFetchServicePtr. A connection will be
// established after the first call to this method. // established after the first call to this method.
mojom::blink::BackgroundFetchServicePtr& GetService(); mojom::blink::BackgroundFetchService* GetService();
void DidGetRegistration(RegistrationCallback, void DidGetRegistration(RegistrationCallback,
mojom::blink::BackgroundFetchError, mojom::blink::BackgroundFetchError,
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
module blink.mojom; module blink.mojom;
import "third_party/WebKit/public/platform/modules/fetch/fetch_api_request.mojom"; import "third_party/WebKit/public/platform/modules/fetch/fetch_api_request.mojom";
import "url/mojo/origin.mojom";
enum BackgroundFetchError { enum BackgroundFetchError {
NONE, NONE,
...@@ -64,7 +63,6 @@ interface BackgroundFetchService { ...@@ -64,7 +63,6 @@ interface BackgroundFetchService {
// Creates a new Background Fetch registration identified to the developer by // Creates a new Background Fetch registration identified to the developer by
// |developer_id|, with the given |options| for the sequence of |requests|. // |developer_id|, with the given |options| for the sequence of |requests|.
Fetch(int64 service_worker_registration_id, Fetch(int64 service_worker_registration_id,
url.mojom.Origin origin,
string developer_id, string developer_id,
array<FetchAPIRequest> requests, array<FetchAPIRequest> requests,
BackgroundFetchOptions options) BackgroundFetchOptions options)
...@@ -80,21 +78,19 @@ interface BackgroundFetchService { ...@@ -80,21 +78,19 @@ interface BackgroundFetchService {
// the developer's |developer_id|. Fails if the registration had already // the developer's |developer_id|. Fails if the registration had already
// completed/failed/aborted. // completed/failed/aborted.
Abort(int64 service_worker_registration_id, Abort(int64 service_worker_registration_id,
url.mojom.Origin origin,
string developer_id, string developer_id,
string unique_id) string unique_id)
=> (BackgroundFetchError error); => (BackgroundFetchError error);
// Gets the active Background Fetch registration identified by |developer_id|. // Gets the active Background Fetch registration identified by |developer_id|.
GetRegistration(int64 service_worker_registration_id, GetRegistration(int64 service_worker_registration_id,
url.mojom.Origin origin,
string developer_id) string developer_id)
=> (BackgroundFetchError error, => (BackgroundFetchError error,
BackgroundFetchRegistration? registration); BackgroundFetchRegistration? registration);
// Gets the sequence of |developer_id|s for active Background Fetch // Gets the sequence of |developer_id|s for active Background Fetch
// registrations given the |service_worker_registration_id|. // registrations given the |service_worker_registration_id|.
GetDeveloperIds(int64 service_worker_registration_id, url.mojom.Origin origin) GetDeveloperIds(int64 service_worker_registration_id)
=> (BackgroundFetchError error, => (BackgroundFetchError error,
array<string> developer_ids); array<string> developer_ids);
......
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