1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
content / browser / background_sync / background_sync_registration_helper.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_sync/background_sync_registration_helper.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_sync/background_sync_context_impl.h"
#include "content/browser/background_sync/background_sync_manager.h"
#include "content/browser/background_sync/background_sync_status.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "url/origin.h"
namespace content {
BackgroundSyncRegistrationHelper::BackgroundSyncRegistrationHelper(
BackgroundSyncContextImpl* background_sync_context,
RenderProcessHost* render_process_host)
: background_sync_context_(background_sync_context),
render_process_host_id_(render_process_host->GetID()) {
DCHECK(background_sync_context_);
}
BackgroundSyncRegistrationHelper::~BackgroundSyncRegistrationHelper() = default;
bool BackgroundSyncRegistrationHelper::ValidateSWRegistrationID(
int64_t sw_registration_id,
const url::Origin& origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
scoped_refptr<ServiceWorkerRegistration> service_worker_registration =
background_sync_manager->service_worker_context()->GetLiveRegistration(
sw_registration_id);
return service_worker_registration &&
service_worker_registration->key().origin().IsSameOriginWith(origin);
}
void BackgroundSyncRegistrationHelper::Register(
blink::mojom::SyncRegistrationOptionsPtr options,
int64_t sw_registration_id,
RegisterCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
background_sync_manager->Register(
sw_registration_id, render_process_host_id_, *options,
base::BindOnce(&BackgroundSyncRegistrationHelper::OnRegisterResult,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void BackgroundSyncRegistrationHelper::DidResolveRegistration(
blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
background_sync_manager->DidResolveRegistration(std::move(registration_info));
}
void BackgroundSyncRegistrationHelper::OnRegisterResult(
RegisterCallback callback,
BackgroundSyncStatus status,
std::unique_ptr<BackgroundSyncRegistration> result) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// TODO(crbug.com/40614176): Use blink::mojom::BackgroundSyncError
// directly.
if (status != BACKGROUND_SYNC_STATUS_OK) {
std::move(callback).Run(
static_cast<blink::mojom::BackgroundSyncError>(status),
/* options= */ nullptr);
return;
}
DCHECK(result);
std::move(callback).Run(
static_cast<blink::mojom::BackgroundSyncError>(status),
result->options()->Clone());
}
void BackgroundSyncRegistrationHelper::NotifyInvalidOptionsProvided(
RegisterCallback callback) const {
mojo::ReportBadMessage(
"BackgroundSyncRegistrationHelper: Invalid options passed.");
std::move(callback).Run(blink::mojom::BackgroundSyncError::NOT_ALLOWED,
/* options= */ nullptr);
}
void BackgroundSyncRegistrationHelper::OnGetRegistrationsResult(
GetRegistrationsCallback callback,
BackgroundSyncStatus status,
std::vector<std::unique_ptr<BackgroundSyncRegistration>>
result_registrations) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::vector<blink::mojom::SyncRegistrationOptionsPtr> mojo_registrations;
mojo_registrations.reserve(result_registrations.size());
for (const auto& registration : result_registrations)
mojo_registrations.push_back(registration->options()->Clone());
std::move(callback).Run(
static_cast<blink::mojom::BackgroundSyncError>(status),
std::move(mojo_registrations));
}
base::WeakPtr<BackgroundSyncRegistrationHelper>
BackgroundSyncRegistrationHelper::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace content