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_REGISTRATION_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_REGISTRATION_H_
7 
8 #include "mojo/public/cpp/bindings/remote.h"
9 #include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom-blink.h"
10 #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
11 #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
12 #include "third_party/blink/renderer/core/dom/events/event_target.h"
13 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
14 #include "third_party/blink/renderer/platform/heap/garbage_collected.h"
15 #include "third_party/blink/renderer/platform/heap/handle.h"
16 #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
17 #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
18 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
19 
20 namespace blink {
21 
22 class BackgroundFetchRecord;
23 class CacheQueryOptions;
24 class ExceptionState;
25 class ScriptPromiseResolver;
26 class ScriptState;
27 class ServiceWorkerRegistration;
28 class RequestOrUSVString;
29 
30 // Represents an individual Background Fetch registration. Gives developers
31 // access to its properties, options, and enables them to abort the fetch.
32 class BackgroundFetchRegistration final
33     : public EventTargetWithInlineData,
34       public ActiveScriptWrappable<BackgroundFetchRegistration>,
35       public blink::mojom::blink::BackgroundFetchRegistrationObserver {
36   DEFINE_WRAPPERTYPEINFO();
37 
38  public:
39   BackgroundFetchRegistration(
40       ServiceWorkerRegistration* service_worker_registration,
41       mojom::blink::BackgroundFetchRegistrationPtr registration);
42 
43   ~BackgroundFetchRegistration() override;
44 
45   // BackgroundFetchRegistrationObserver implementation.
46   void OnProgress(uint64_t upload_total,
47                   uint64_t uploaded,
48                   uint64_t download_total,
49                   uint64_t downloaded,
50                   mojom::BackgroundFetchResult result,
51                   mojom::BackgroundFetchFailureReason failure_reason) override;
52   void OnRecordsUnavailable() override;
53 
54   // Called when the |request| is complete. |response| points to the response
55   // received, if any.
56   void OnRequestCompleted(mojom::blink::FetchAPIRequestPtr request,
57                           mojom::blink::FetchAPIResponsePtr response) override;
58 
59   // Web Exposed attribute defined in the IDL file. Corresponds to the
60   // |developer_id| used elsewhere in the codebase.
61   String id() const;
62   ScriptPromise match(ScriptState* script_state,
63                       const RequestOrUSVString& request,
64                       const CacheQueryOptions* options,
65                       ExceptionState& exception_state);
66   ScriptPromise matchAll(ScriptState* scrip_state,
67                          ExceptionState& exception_state);
68   ScriptPromise matchAll(ScriptState* script_state,
69                          const RequestOrUSVString& request,
70                          const CacheQueryOptions* options,
71                          ExceptionState& exception_state);
72 
73   uint64_t uploadTotal() const;
74   uint64_t uploaded() const;
75   uint64_t downloadTotal() const;
76   uint64_t downloaded() const;
77   bool recordsAvailable() const;
78   const String result() const;
79   const String failureReason() const;
80 
81   DEFINE_ATTRIBUTE_EVENT_LISTENER(progress, kProgress)
82 
83   ScriptPromise abort(ScriptState* script_state);
84 
85   // EventTargetWithInlineData implementation.
86   const AtomicString& InterfaceName() const override;
87   ExecutionContext* GetExecutionContext() const override;
88 
89   void Trace(Visitor* visitor) const override;
90 
91   // Keeps the object alive until there are non-zero number of |observers_|.
92   bool HasPendingActivity() const final;
93 
94   void UpdateUI(
95       const String& in_title,
96       const SkBitmap& in_icon,
97       mojom::blink::BackgroundFetchRegistrationService::UpdateUICallback
98           callback);
99 
100  private:
101   void DidAbort(ScriptPromiseResolver* resolver,
102                 mojom::blink::BackgroundFetchError error);
103   ScriptPromise MatchImpl(
104       ScriptState* script_state,
105       base::Optional<RequestOrUSVString> request,
106       mojom::blink::CacheQueryOptionsPtr cache_query_options,
107       ExceptionState& exception_state,
108       bool match_all);
109   void DidGetMatchingRequests(
110       ScriptPromiseResolver* resolver,
111       bool return_all,
112       Vector<mojom::blink::BackgroundFetchSettledFetchPtr> settled_fetches);
113 
114   // Updates the |record| with a |response|, if one is available, else marks
115   // the |record|'s request as aborted or failed.
116   void UpdateRecord(BackgroundFetchRecord* record,
117                     mojom::blink::FetchAPIResponsePtr& response);
118 
119   bool IsAborted();
120 
121   Member<ServiceWorkerRegistration> registration_;
122 
123   // Corresponds to IDL 'id' attribute. Not unique - an active registration can
124   // have the same |developer_id_| as one or more inactive registrations.
125   String developer_id_;
126 
127   uint64_t upload_total_;
128   uint64_t uploaded_;
129   uint64_t download_total_;
130   uint64_t downloaded_;
131   bool records_available_ = true;
132   mojom::BackgroundFetchResult result_;
133   mojom::BackgroundFetchFailureReason failure_reason_;
134   HeapVector<Member<BackgroundFetchRecord>> observers_;
135 
136   mojo::Remote<mojom::blink::BackgroundFetchRegistrationService>
137       registration_service_;
138 
139   HeapMojoReceiver<blink::mojom::blink::BackgroundFetchRegistrationObserver,
140                    BackgroundFetchRegistration,
141                    HeapMojoWrapperMode::kWithoutContextObserver>
142       observer_receiver_;
143 };
144 
145 }  // namespace blink
146 
147 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_REGISTRATION_H_
148