1 // Copyright 2020 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 COMPONENTS_QUERY_TILES_INTERNAL_TILE_SERVICE_SCHEDULER_IMPL_H_
6 #define COMPONENTS_QUERY_TILES_INTERNAL_TILE_SERVICE_SCHEDULER_IMPL_H_
7 
8 #include "base/time/clock.h"
9 #include "base/time/tick_clock.h"
10 #include "components/query_tiles/internal/log_source.h"
11 #include "components/query_tiles/internal/tile_config.h"
12 #include "components/query_tiles/internal/tile_service_scheduler.h"
13 #include "components/query_tiles/tile_service_prefs.h"
14 #include "net/base/backoff_entry_serializer.h"
15 
16 class PrefService;
17 
18 namespace query_tiles {
19 
20 // An implementation of TileServiceScheduler interface and LogSource interface.
21 class TileServiceSchedulerImpl : public TileServiceScheduler, public LogSource {
22  public:
23   TileServiceSchedulerImpl(
24       background_task::BackgroundTaskScheduler* scheduler,
25       PrefService* prefs,
26       base::Clock* clock,
27       const base::TickClock* tick_clock,
28       std::unique_ptr<net::BackoffEntry::Policy> backoff_policy,
29       LogSink* log_sink);
30 
31   ~TileServiceSchedulerImpl() override;
32 
33  private:
34   // TileServiceScheduler implementation.
35   void CancelTask() override;
36   void OnFetchStarted() override;
37   void OnFetchCompleted(TileInfoRequestStatus status) override;
38   void OnTileManagerInitialized(TileGroupStatus status) override;
39   void OnDbPurged(TileGroupStatus status) override;
40   void OnGroupDataSaved(TileGroupStatus status) override;
41   void SetDelegate(Delegate* delegate) override;
42 
43   // LogSource implementation.
44   TileInfoRequestStatus GetFetcherStatus() override;
45   TileGroupStatus GetGroupStatus() override;
46   TileGroup* GetTileGroup() override;
47 
48   void ScheduleTask(bool is_init_schedule);
49   std::unique_ptr<net::BackoffEntry> GetBackoff();
50   void AddBackoff();
51   void ResetBackoff();
52   void MaximizeBackoff();
53   int64_t GetDelaysFromBackoff();
54   void GetInstantTaskWindow(int64_t* start_time_ms,
55                             int64_t* end_time_ms,
56                             bool is_init_schedule);
57   void GetTaskWindow(int64_t* start_time_ms,
58                      int64_t* end_time_ms,
59                      bool is_init_schedule);
60   void UpdateBackoff(net::BackoffEntry* backoff);
61   void MarkFirstRunScheduled();
62   void MarkFirstRunFinished();
63 
64   // Returns true if the initial task has been scheduled because no tiles in
65   // db(kickoff condition), but still waiting to be completed at the designated
66   // window. Returns false either the first task is not scheduled yet or it is
67   // already finished.
68   bool IsDuringFirstFlow();
69 
70   // Ping the log sink to update.
71   void PingLogSink();
72 
73   // Native Background Scheduler instance.
74   background_task::BackgroundTaskScheduler* scheduler_;
75 
76   PrefService* prefs_;
77 
78   // Clock object to get current time.
79   base::Clock* clock_;
80 
81   // TickClock used for building backoff entry.
82   const base::TickClock* tick_clock_;
83 
84   // Backoff policy used for reschdule.
85   std::unique_ptr<net::BackoffEntry::Policy> backoff_policy_;
86 
87   // Flag to indicate if currently have a suspend status to avoid overwriting if
88   // already scheduled a suspend task during this lifecycle.
89   bool is_suspend_;
90 
91   // Delegate instance.
92   Delegate* delegate_;
93 
94   // Internal fetcher status.
95   TileInfoRequestStatus fetcher_status_;
96 
97   // Internal group status.
98   TileGroupStatus group_status_;
99 
100   // LogSink instance.
101   LogSink* log_sink_;
102 };
103 
104 }  // namespace query_tiles
105 
106 #endif  // COMPONENTS_QUERY_TILES_INTERNAL_TILE_SERVICE_SCHEDULER_H_
107