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_FETCH_STORAGE_GET_REQUEST_BLOB_TASK_H_
6 #define CONTENT_BROWSER_BACKGROUND_FETCH_STORAGE_GET_REQUEST_BLOB_TASK_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/memory/scoped_refptr.h"
10 #include "content/browser/background_fetch/background_fetch_request_info.h"
11 #include "content/browser/background_fetch/storage/database_task.h"
12 #include "content/browser/cache_storage/cache_storage_cache.h"
13 #include "storage/browser/blob/blob_data_handle.h"
14 #include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
15 
16 namespace content {
17 namespace background_fetch {
18 
19 class GetRequestBlobTask : public DatabaseTask {
20  public:
21   using GetRequestBlobCallback =
22       base::OnceCallback<void(blink::mojom::BackgroundFetchError,
23                               blink::mojom::SerializedBlobPtr)>;
24 
25   GetRequestBlobTask(
26       DatabaseTaskHost* host,
27       const BackgroundFetchRegistrationId& registration_id,
28       const scoped_refptr<BackgroundFetchRequestInfo>& request_info,
29       GetRequestBlobCallback callback);
30 
31   ~GetRequestBlobTask() override;
32 
33   // DatabaseTask implementation:
34   void Start() override;
35 
36  private:
37   void DidOpenCache(int64_t trace_id,
38                     CacheStorageCacheHandle handle,
39                     blink::mojom::CacheStorageError error);
40 
41   void DidMatchRequest(CacheStorageCacheHandle handle,
42                        int64_t trace_id,
43                        blink::mojom::CacheStorageError error,
44                        std::vector<CacheStorageCache::CacheEntry> entries);
45 
46   void FinishWithError(blink::mojom::BackgroundFetchError error) override;
47 
48   std::string HistogramName() const override;
49 
50   BackgroundFetchRegistrationId registration_id_;
51   scoped_refptr<BackgroundFetchRequestInfo> request_info_;
52   GetRequestBlobCallback callback_;
53 
54   blink::mojom::SerializedBlobPtr blob_;
55 
56   base::WeakPtrFactory<GetRequestBlobTask> weak_factory_{
57       this};  // Keep as last.
58 
59   DISALLOW_COPY_AND_ASSIGN(GetRequestBlobTask);
60 };
61 
62 }  // namespace background_fetch
63 }  // namespace content
64 
65 #endif  // CONTENT_BROWSER_BACKGROUND_FETCH_STORAGE_GET_REQUEST_BLOB_TASK_H_
66