File: one_shot_background_sync_service_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,522 bytes parent folder | download | duplicates (9)
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
// 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/one_shot_background_sync_service_impl.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_sync/background_sync_context_impl.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/browser_thread.h"

namespace content {

OneShotBackgroundSyncServiceImpl::OneShotBackgroundSyncServiceImpl(
    BackgroundSyncContextImpl* background_sync_context,
    const url::Origin& origin,
    RenderProcessHost* render_process_host,
    mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService> receiver)
    : background_sync_context_(background_sync_context),
      origin_(origin),
      receiver_(this, std::move(receiver)) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(background_sync_context_);

  registration_helper_ = std::make_unique<BackgroundSyncRegistrationHelper>(
      background_sync_context_, render_process_host);

  receiver_.set_disconnect_handler(base::BindOnce(
      &OneShotBackgroundSyncServiceImpl::OnMojoDisconnect,
      base::Unretained(this) /* the channel is owned by |this| */));
}

OneShotBackgroundSyncServiceImpl::~OneShotBackgroundSyncServiceImpl() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

void OneShotBackgroundSyncServiceImpl::OnMojoDisconnect() {
  background_sync_context_->OneShotSyncServiceHadConnectionError(this);
  // |this| is now deleted.
}

void OneShotBackgroundSyncServiceImpl::Register(
    blink::mojom::SyncRegistrationOptionsPtr options,
    int64_t sw_registration_id,
    RegisterCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(options);

  if (options->min_interval != -1) {
    registration_helper_->NotifyInvalidOptionsProvided(std::move(callback));
    return;
  }

  if (!registration_helper_->ValidateSWRegistrationID(sw_registration_id,
                                                      origin_)) {
    std::move(callback).Run(blink::mojom::BackgroundSyncError::STORAGE,
                            /* registrations= */ nullptr);
    return;
  }

  registration_helper_->Register(std::move(options), sw_registration_id,
                                 std::move(callback));
}

void OneShotBackgroundSyncServiceImpl::DidResolveRegistration(
    blink::mojom::BackgroundSyncRegistrationInfoPtr registration_info) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  registration_helper_->DidResolveRegistration(std::move(registration_info));
}

void OneShotBackgroundSyncServiceImpl::GetRegistrations(
    int64_t sw_registration_id,
    GetRegistrationsCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (!registration_helper_->ValidateSWRegistrationID(sw_registration_id,
                                                      origin_)) {
    std::move(callback).Run(blink::mojom::BackgroundSyncError::STORAGE,
                            /* options= */ {});
    return;
  }

  BackgroundSyncManager* background_sync_manager =
      background_sync_context_->background_sync_manager();
  DCHECK(background_sync_manager);

  background_sync_manager->GetOneShotSyncRegistrations(
      sw_registration_id,
      base::BindOnce(
          &BackgroundSyncRegistrationHelper::OnGetRegistrationsResult,
          registration_helper_->GetWeakPtr(), std::move(callback)));
}

}  // namespace content