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_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_SCHEDULER_H_
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_SCHEDULER_H_
7 
8 #include <map>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "build/build_config.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
19 
20 namespace content {
21 
22 class StoragePartitionImpl;
23 
24 // Key name on BrowserContext.
25 extern const char kBackgroundSyncSchedulerKey[];
26 
27 // This contains the logic to schedule delayed processing of (periodic)
28 // Background Sync registrations.
29 // It keeps track of all storage partitions, and the soonest time we should
30 // attempt to fire (periodic)sync events for it.
31 class CONTENT_EXPORT BackgroundSyncScheduler
32     : public base::RefCountedThreadSafe<BackgroundSyncScheduler,
33                                         BrowserThread::DeleteOnUIThread> {
34  public:
35   static BackgroundSyncScheduler* GetFor(BrowserContext* browser_context);
36 
37   BackgroundSyncScheduler();
38 
39   // Schedules delayed_processing for |sync_type| for |storage_partition|.
40   // On non-Android platforms, runs |delayed_task| after |delay| has passed.
41   // TODO(crbug.com/996166): Add logic to schedule browser wakeup on Android.
42   // Must be called on the UI thread.
43   virtual void ScheduleDelayedProcessing(
44       StoragePartitionImpl* storage_partition,
45       blink::mojom::BackgroundSyncType sync_type,
46       base::TimeDelta delay,
47       base::OnceClosure delayed_task);
48 
49   // Cancels delayed_processing for |sync_type| for |storage_partition|.
50   // Must be called on the UI thread.
51   virtual void CancelDelayedProcessing(
52       StoragePartitionImpl* storage_partition,
53       blink::mojom::BackgroundSyncType sync_type);
54 
55  private:
56   virtual ~BackgroundSyncScheduler();
57 
58   friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
59   friend class base::DeleteHelper<BackgroundSyncScheduler>;
60   friend class BackgroundSyncSchedulerTest;
61 
62   std::map<StoragePartitionImpl*, std::unique_ptr<base::OneShotTimer>>&
63   GetDelayedProcessingInfoMap(blink::mojom::BackgroundSyncType sync_type);
64   void RunDelayedTaskAndPruneInfoMap(blink::mojom::BackgroundSyncType sync_type,
65                                      StoragePartitionImpl* storage_partition,
66                                      base::OnceClosure delayed_task);
67 #if defined(OS_ANDROID)
68   void ScheduleOrCancelBrowserWakeupForSyncType(
69       blink::mojom::BackgroundSyncType sync_type,
70       StoragePartitionImpl* storage_partition);
71 #endif
72 
73   std::map<StoragePartitionImpl*, std::unique_ptr<base::OneShotTimer>>
74       delayed_processing_info_one_shot_;
75   std::map<StoragePartitionImpl*, std::unique_ptr<base::OneShotTimer>>
76       delayed_processing_info_periodic_;
77 
78   std::map<blink::mojom::BackgroundSyncType, base::TimeTicks>
79       scheduled_wakeup_time_{
80           {blink::mojom::BackgroundSyncType::ONE_SHOT, base::TimeTicks::Max()},
81           {blink::mojom::BackgroundSyncType::PERIODIC, base::TimeTicks::Max()},
82       };
83 
84   base::WeakPtrFactory<BackgroundSyncScheduler> weak_ptr_factory_{this};
85 
86   DISALLOW_COPY_AND_ASSIGN(BackgroundSyncScheduler);
87 };
88 
89 }  // namespace content
90 
91 #endif  // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_SCHEDULER_H_
92