1 // Copyright 2016 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_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_
6 #define CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/callback_forward.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/time/time.h"
16 #include "content/browser/background_sync/background_sync_manager.h"
17 #include "content/browser/service_worker/service_worker_registry.h"
18 
19 namespace url {
20 class Origin;
21 }  // namespace url
22 
23 namespace content {
24 
25 struct BackgroundSyncParameters;
26 class DevToolsBackgroundServicesContextImpl;
27 class ServiceWorkerContextWrapper;
28 class ServiceWorkerVersion;
29 
30 // A BackgroundSyncManager for use in unit tests. This class provides control
31 // over internal behavior and state, to allow tests to simulate various
32 // scenarios.  Examples include (but are not limited to):
33 //  - Delaying and corrupting the backend storage.
34 //  - Controlling the firing of service worker onsync events.
35 //  - Setting the network state
36 class TestBackgroundSyncManager : public BackgroundSyncManager {
37  public:
38   using DispatchSyncCallback =
39       base::RepeatingCallback<void(scoped_refptr<ServiceWorkerVersion>,
40                                    ServiceWorkerVersion::StatusCallback)>;
41 
42   TestBackgroundSyncManager(
43       scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
44       scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context);
45   ~TestBackgroundSyncManager() override;
46 
47   // Force a call to the internal Init() method.
48   void DoInit();
49 
50   // Resume any delayed backend operation.
51   void ResumeBackendOperation();
52 
53   // Clear the delayed task if it exists. Delayed tasks are tasks posted with a
54   // delay to trigger the BackgroundSyncManager to try firing its sync events
55   // again.
56   void ClearDelayedTask();
57 
58   // Determine whether backend operations succeed or fail due to
59   // corruption.
set_corrupt_backend(bool corrupt_backend)60   void set_corrupt_backend(bool corrupt_backend) {
61     corrupt_backend_ = corrupt_backend;
62   }
63 
64   // Delay backend operations until explicitly resumed by calling
65   // ResumeBackendOperation().
set_delay_backend(bool delay_backend)66   void set_delay_backend(bool delay_backend) { delay_backend_ = delay_backend; }
67 
68   // Set a callback for when the sync event is dispatched, so tests can observe
69   // when the event happens.
set_dispatch_sync_callback(const DispatchSyncCallback & callback)70   void set_dispatch_sync_callback(const DispatchSyncCallback& callback) {
71     dispatch_sync_callback_ = callback;
72   }
73 
74   // Set a callback for when the periodicSync event is dispatched, so tests can
75   // observe it.
set_dispatch_periodic_sync_callback(const DispatchSyncCallback & callback)76   void set_dispatch_periodic_sync_callback(
77       const DispatchSyncCallback& callback) {
78     dispatch_periodic_sync_callback_ = callback;
79   }
80 
81   // Sets the response to checks for a main frame for register attempts.
set_has_main_frame_window_client(bool value)82   void set_has_main_frame_window_client(bool value) {
83     has_main_frame_window_client_ = value;
84   }
85 
86   // Accessors to internal state
last_chance()87   bool last_chance() const { return last_chance_; }
background_sync_parameters()88   const BackgroundSyncParameters* background_sync_parameters() const {
89     return parameters_.get();
90   }
91 
92   void DispatchPeriodicSyncEvent(
93       const std::string& tag,
94       scoped_refptr<ServiceWorkerVersion> active_version,
95       ServiceWorkerVersion::StatusCallback callback) override;
96 
97   // Override to allow the test to cache the result.
98   base::TimeDelta GetSoonestWakeupDelta(
99       blink::mojom::BackgroundSyncType sync_type,
100       base::Time last_browser_wakeup_for_periodic_sync) override;
101 
102   // Override to do not fire any sync events when firing is disabled.
103   void FireReadyEvents(blink::mojom::BackgroundSyncType sync_type,
104                        bool reschedule,
105                        base::OnceClosure callback,
106                        std::unique_ptr<BackgroundSyncEventKeepAlive> keepalive =
107                            nullptr) override;
108 
SuspendFiringEvents()109   void SuspendFiringEvents() { dont_fire_sync_events_ = true; }
110 
ResumeFiringEvents()111   void ResumeFiringEvents() { dont_fire_sync_events_ = false; }
112 
113  protected:
114   // Override to allow delays to be injected by tests.
115   void StoreDataInBackend(
116       int64_t sw_registration_id,
117       const url::Origin& origin,
118       const std::string& key,
119       const std::string& data,
120       ServiceWorkerRegistry::StatusCallback callback) override;
121 
122   // Override to allow delays to be injected by tests.
123   void GetDataFromBackend(
124       const std::string& key,
125       ServiceWorkerRegistry::GetUserDataForAllRegistrationsCallback callback)
126       override;
127 
128   // Override to avoid actual dispatching of the event, just call the provided
129   // callback instead.
130   void DispatchSyncEvent(
131       const std::string& tag,
132       scoped_refptr<ServiceWorkerVersion> active_version,
133       bool last_chance,
134       ServiceWorkerVersion::StatusCallback callback) override;
135 
136   // Override to avoid actual check for main frame, instead return the value set
137   // by tests.
138   void HasMainFrameWindowClient(const url::Origin& origin,
139                                 BoolCallback callback) override;
140 
141  private:
142   // Callback to resume the StoreDataInBackend operation, after explicit
143   // delays injected by tests.
144   void StoreDataInBackendContinue(
145       int64_t sw_registration_id,
146       const url::Origin& origin,
147       const std::string& key,
148       const std::string& data,
149       ServiceWorkerRegistry::StatusCallback callback);
150 
151   // Callback to resume the GetDataFromBackend operation, after explicit delays
152   // injected by tests.
153   void GetDataFromBackendContinue(
154       const std::string& key,
155       ServiceWorkerRegistry::GetUserDataForAllRegistrationsCallback callback);
156 
157   bool corrupt_backend_ = false;
158   bool delay_backend_ = false;
159   bool has_main_frame_window_client_ = true;
160   bool last_chance_ = false;
161   bool dont_fire_sync_events_ = false;
162   base::OnceClosure continuation_;
163   DispatchSyncCallback dispatch_sync_callback_;
164   DispatchSyncCallback dispatch_periodic_sync_callback_;
165   base::TimeDelta soonest_one_shot_sync_wakeup_delta_;
166   base::TimeDelta soonest_periodic_sync_wakeup_delta_;
167 
168   DISALLOW_COPY_AND_ASSIGN(TestBackgroundSyncManager);
169 };
170 
171 }  // namespace content
172 
173 #endif  // CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_
174