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
  129
  130
  131
  132
  133
  134

content / public / browser / background_sync_controller.h [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_BROWSER_BACKGROUND_SYNC_CONTROLLER_H_
#define CONTENT_PUBLIC_BROWSER_BACKGROUND_SYNC_CONTROLLER_H_

#include <stdint.h>

#include <set>

#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/public/browser/background_sync_registration.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
#include "third_party/blink/public/mojom/background_sync/background_sync.mojom-shared.h"

namespace url {
class Origin;
}  // namespace url

namespace content {

struct BackgroundSyncParameters;

// An interface that the Background Sync API uses to access services from the
// embedder. Must only be used on the UI thread.
class CONTENT_EXPORT BackgroundSyncController {
 public:
  class BackgroundSyncEventKeepAlive {
   public:
    virtual ~BackgroundSyncEventKeepAlive() = default;

   protected:
    BackgroundSyncEventKeepAlive() = default;
  };

  virtual ~BackgroundSyncController() {}

  // This function allows the controller to alter the parameters used by
  // background sync. Note that disable can be overridden from false to true
  // but overrides from true to false will be ignored.
  virtual void GetParameterOverrides(BackgroundSyncParameters* parameters) {}

  // Notification that a service worker registration with origin |origin| just
  // registered a one-shot background sync event. Also includes information
  // about the registration.
  virtual void NotifyOneShotBackgroundSyncRegistered(const url::Origin& origin,
                                                     bool can_fire,
                                                     bool is_reregistered) {}

  // Notification that a service worker registration with origin |origin| just
  // registered a periodic background sync event. Also includes information
  // about the registration.
  virtual void NotifyPeriodicBackgroundSyncRegistered(const url::Origin& origin,
                                                      int min_interval,
                                                      bool is_reregistered) {}

  // Notification that a service worker registration with origin |origin| just
  // completed a one-shot background sync registration. Also include the
  // |status_code| the registration finished with, the number of attempts, and
  // the max allowed number of attempts.
  virtual void NotifyOneShotBackgroundSyncCompleted(
      const url::Origin& origin,
      blink::ServiceWorkerStatusCode status_code,
      int num_attempts,
      int max_attempts) {}

  // Notification that a service worker registration with origin |origin| just
  // completed a periodic background sync registration. Also include the
  // |status_code| the registration finished with, the number of attempts, and
  // the max allowed number of attempts.
  virtual void NotifyPeriodicBackgroundSyncCompleted(
      const url::Origin& origin,
      blink::ServiceWorkerStatusCode status_code,
      int num_attempts,
      int max_attempts) {}

  // Schedules a background task with delay |delay| to wake up the browser to
  // process Background Sync registrations of type |sync_type|.
  virtual void ScheduleBrowserWakeUpWithDelay(
      blink::mojom::BackgroundSyncType sync_type,
      base::TimeDelta delay) {}

  // Cancel the background task that wakes the browser up to process Background
  // Sync registrations of type |sync_type|.
  virtual void CancelBrowserWakeup(blink::mojom::BackgroundSyncType sync_type) {
  }

  // Calculates the delay after which the next sync event should be fired
  // for a BackgroundSync registration. The delay is based on the sync_type of
  // the |registration|, the |parameters| for the feature, the soonest time
  // a (periodic)sync event is scheduled to fire for this origin, and other
  // browser-specific considerations.
  virtual base::TimeDelta GetNextEventDelay(
      const BackgroundSyncRegistration& registration,
      content::BackgroundSyncParameters* parameters,
      base::TimeDelta time_till_soonest_scheduled_event_for_origin) = 0;

  // Keeps the browser alive to allow a one-shot Background Sync registration
  // to finish firing one sync event.
  virtual std::unique_ptr<BackgroundSyncEventKeepAlive>
  CreateBackgroundSyncEventKeepAlive() = 0;

  // Updates its internal list of origins for which we have suspended periodic
  // Background Sync registrations. This is compiled from each
  // BackgroundSyncManager when they are initialized. This list used to ignore
  // changes concerning origins we don't care about.
  virtual void NoteSuspendedPeriodicSyncOrigins(
      std::set<url::Origin> suspended_origins) = 0;

  // Updates its internal list of origins for which we have periodic Background
  // Sync registrations. This is compiled from each BackgroundSyncManager when
  // they are initialized and subsequently kept up to date. This list is used to
  // respond to changes in site settings affecting the feature.
  virtual void NoteRegisteredPeriodicSyncOrigins(
      std::set<url::Origin> registered_origins) = 0;

  // Adds |origin| to its internal list of origins for which we have periodic
  // Background Sync registrations. This list used to unregister periodic
  // Background Sync when the controller deems necessary, for instance, upon
  // revocation of permission.
  virtual void AddToTrackedOrigins(const url::Origin& origin) = 0;

  // Removes |origin| from its internal list of origins for which we have
  // periodic Background Sync registrations. This list used to unregister
  // periodic Background Sync when the controller deems necessary, for instance,
  //  upon revocation of permission.
  virtual void RemoveFromTrackedOrigins(const url::Origin& origin) = 0;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_BROWSER_BACKGROUND_SYNC_CONTROLLER_H_