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
  123
  124
  125
  126
  127
  128

content / browser / background_sync / background_sync_proxy.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_proxy.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/task_traits.h"
#include "content/browser/background_sync/background_sync_scheduler.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/background_sync_context.h"
#include "content/public/browser/background_sync_controller.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"

namespace content {

BackgroundSyncProxy::BackgroundSyncProxy(
    scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
    : service_worker_context_(std::move(service_worker_context)) {
  // This class lives on the UI thread. Check explicitly that it is on the UI
  // thread in the constructor, and use the `sequence_checker_` in
  // each method to check that it is on a single sequence (the UI thread).
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(service_worker_context_);
}

BackgroundSyncProxy::~BackgroundSyncProxy() = default;

void BackgroundSyncProxy::ScheduleDelayedProcessing(
    blink::mojom::BackgroundSyncType sync_type,
    base::TimeDelta delay,
    base::OnceClosure delayed_task) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Schedule Chrome wakeup.
  if (!browser_context())
    return;

  auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context());
  DCHECK(scheduler);
  DCHECK(delay != base::TimeDelta::Max());

  scheduler->ScheduleDelayedProcessing(
      service_worker_context_->storage_partition(), sync_type, delay,
      std::move(delayed_task));
}

void BackgroundSyncProxy::CancelDelayedProcessing(
    blink::mojom::BackgroundSyncType sync_type) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!browser_context())
    return;

  auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context());
  DCHECK(scheduler);

  scheduler->CancelDelayedProcessing(
      service_worker_context_->storage_partition(), sync_type);
}

void BackgroundSyncProxy::SendSuspendedPeriodicSyncOrigins(
    std::set<url::Origin> suspended_origins) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!browser_context())
    return;

  auto* controller = browser_context()->GetBackgroundSyncController();
  DCHECK(controller);

  controller->NoteSuspendedPeriodicSyncOrigins(std::move(suspended_origins));
}

void BackgroundSyncProxy::SendRegisteredPeriodicSyncOrigins(
    std::set<url::Origin> registered_origins) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!browser_context())
    return;

  auto* controller = browser_context()->GetBackgroundSyncController();
  DCHECK(controller);

  controller->NoteRegisteredPeriodicSyncOrigins(std::move(registered_origins));
}

void BackgroundSyncProxy::AddToTrackedOrigins(url::Origin origin) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  if (!browser_context())
    return;

  auto* controller = browser_context()->GetBackgroundSyncController();
  DCHECK(controller);

  controller->AddToTrackedOrigins(origin);
}

void BackgroundSyncProxy::RemoveFromTrackedOrigins(url::Origin origin) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!browser_context())
    return;

  auto* controller = browser_context()->GetBackgroundSyncController();
  DCHECK(controller);

  controller->RemoveFromTrackedOrigins(origin);
}

BrowserContext* BackgroundSyncProxy::browser_context() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!service_worker_context_)
    return nullptr;

  StoragePartitionImpl* storage_partition_impl =
      service_worker_context_->storage_partition();
  if (!storage_partition_impl)  // may be null in tests
    return nullptr;

  return storage_partition_impl->browser_context();
}

}  // namespace content