1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_PUBLIC_BROWSER_BACKGROUND_SYNC_CONTEXT_H_
6 #define CONTENT_PUBLIC_BROWSER_BACKGROUND_SYNC_CONTEXT_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/macros.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
13 #include "url/origin.h"
14 
15 #if defined(OS_ANDROID)
16 #include "base/android/jni_android.h"
17 #include "base/android/scoped_java_ref.h"
18 #endif
19 
20 namespace content {
21 
22 class BrowserContext;
23 class StoragePartition;
24 
25 // One instance of this exists per StoragePartition, and services multiple child
26 // processes/origins. It contains the context for processing Background Sync
27 // registrations, and delegates most of this processing to owned instances of
28 // other components.
29 class CONTENT_EXPORT BackgroundSyncContext {
30  public:
31   // Gets the soonest time delta from now, when the browser should be woken up
32   // to fire Background Sync events of |sync_type|, across all storage
33   // partitions in |browser_context|, and invokes |callback| with it.
34   static void GetSoonestWakeupDeltaAcrossPartitions(
35       blink::mojom::BackgroundSyncType sync_type,
36       BrowserContext* browser_context,
37       base::OnceCallback<void(base::TimeDelta)> callback);
38 
39 #if defined(OS_ANDROID)
40   // Processes pending Background Sync registrations of |sync_type| for all the
41   // storage partitions in |browser_context|, and then runs  the |j_runnable|
42   // when done.
43   static void FireBackgroundSyncEventsAcrossPartitions(
44       BrowserContext* browser_context,
45       blink::mojom::BackgroundSyncType sync_type,
46       const base::android::JavaParamRef<jobject>& j_runnable);
47 #endif
48 
49   BackgroundSyncContext() = default;
50 
51   // Process any pending Background Sync registrations.
52   // This involves firing any sync events ready to be fired, and optionally
53   // scheduling a job to wake up the browser when the next event needs to be
54   // fired.
55   virtual void FireBackgroundSyncEvents(
56       blink::mojom::BackgroundSyncType sync_type,
57       base::OnceClosure done_closure) = 0;
58 
59   // Gets the soonest time delta from now, when the browser should be woken up
60   // to fire any Background Sync events. Calls |callback| with this value.
61   virtual void GetSoonestWakeupDelta(
62       blink::mojom::BackgroundSyncType sync_type,
63       base::Time last_browser_wakeup_for_periodic_sync,
64       base::OnceCallback<void(base::TimeDelta)> callback) = 0;
65 
66   // Revives any suspended periodic Background Sync registrations for |origin|.
67   virtual void RevivePeriodicBackgroundSyncRegistrations(
68       url::Origin origin) = 0;
69 
70   // Unregisters any periodic Background Sync registrations for |origin|.
71   virtual void UnregisterPeriodicSyncForOrigin(url::Origin origin) = 0;
72 
73  protected:
74   virtual ~BackgroundSyncContext() = default;
75 
76   DISALLOW_COPY_AND_ASSIGN(BackgroundSyncContext);
77 };
78 
79 }  // namespace content
80 
81 #endif  // CONTENT_PUBLIC_BROWSER_BACKGROUND_SYNC_CONTEXT_H_
82