1 // Copyright 2017 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 THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_BRIDGE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_BRIDGE_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom-blink.h"
12 #include "third_party/blink/renderer/modules/service_worker/service_worker_registration.h"
13 #include "third_party/blink/renderer/platform/heap/handle.h"
14 #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
15 #include "third_party/blink/renderer/platform/supplementable.h"
16 #include "third_party/blink/renderer/platform/wtf/functional.h"
17 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
18 #include "third_party/blink/renderer/platform/wtf/vector.h"
19 
20 namespace blink {
21 
22 class BackgroundFetchRegistration;
23 
24 // The bridge is responsible for establishing and maintaining the Mojo
25 // connection to the BackgroundFetchService. It's keyed on an active Service
26 // Worker Registration.
27 class BackgroundFetchBridge final
28     : public GarbageCollected<BackgroundFetchBridge>,
29       public Supplement<ServiceWorkerRegistration> {
30  public:
31   static const char kSupplementName[];
32 
33   using GetDeveloperIdsCallback =
34       base::OnceCallback<void(mojom::blink::BackgroundFetchError,
35                               const Vector<String>&)>;
36   using RegistrationCallback =
37       base::OnceCallback<void(mojom::blink::BackgroundFetchError,
38                               BackgroundFetchRegistration*)>;
39   using GetIconDisplaySizeCallback = base::OnceCallback<void(const gfx::Size&)>;
40 
41   static BackgroundFetchBridge* From(ServiceWorkerRegistration* registration);
42 
43   explicit BackgroundFetchBridge(ServiceWorkerRegistration& registration);
44   virtual ~BackgroundFetchBridge();
45   void Trace(Visitor* visitor) const override;
46 
47   // Creates a new Background Fetch registration identified by |developer_id|
48   // for the sequence of |requests|. The |callback| will be invoked when the
49   // registration has been created.
50   void Fetch(const String& developer_id,
51              Vector<mojom::blink::FetchAPIRequestPtr> requests,
52              mojom::blink::BackgroundFetchOptionsPtr options,
53              const SkBitmap& icon,
54              mojom::blink::BackgroundFetchUkmDataPtr ukm_data,
55              RegistrationCallback callback);
56 
57   // Gets the size of the icon to be displayed in Background Fetch UI.
58   void GetIconDisplaySize(GetIconDisplaySizeCallback callback);
59 
60   // Gets the Background Fetch registration for the given |developer_id|. Will
61   // invoke the |callback| with the Background Fetch registration, which may be
62   // a nullptr if the |developer_id| does not exist, when the Mojo call has
63   // completed.
64   void GetRegistration(const String& developer_id,
65                        RegistrationCallback callback);
66 
67   // Gets the sequence of ids for active Background Fetch registrations. Will
68   // invoke the |callback| with the |developers_id|s when the Mojo call has
69   // completed.
70   void GetDeveloperIds(GetDeveloperIdsCallback callback);
71 
72  private:
73   // Returns an initialized BackgroundFetchService*. A connection will be
74   // established after the first call to this method.
75   mojom::blink::BackgroundFetchService* GetService();
76 
77   void DidGetRegistration(
78       RegistrationCallback callback,
79       mojom::blink::BackgroundFetchError error,
80       mojom::blink::BackgroundFetchRegistrationPtr registration_ptr);
81 
82   HeapMojoRemote<mojom::blink::BackgroundFetchService>
83       background_fetch_service_;
84 
85   DISALLOW_COPY_AND_ASSIGN(BackgroundFetchBridge);
86 };
87 
88 }  // namespace blink
89 
90 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_BRIDGE_H_
91