1 // Copyright 2018 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_REGISTRATION_TASK_H_
6 #define CONTENT_BROWSER_BACKGROUND_FETCH_STORAGE_GET_REGISTRATION_TASK_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/callback_forward.h"
12 #include "content/browser/background_fetch/background_fetch.pb.h"
13 #include "content/browser/background_fetch/background_fetch_registration_id.h"
14 #include "content/browser/background_fetch/storage/database_task.h"
15 #include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
16 #include "url/origin.h"
17 
18 namespace content {
19 namespace background_fetch {
20 
21 // Gets an active Background Fetch metadata entry from the database.
22 class GetRegistrationTask : public DatabaseTask {
23  public:
24   using GetRegistrationCallback = base::OnceCallback<void(
25       blink::mojom::BackgroundFetchError,
26       BackgroundFetchRegistrationId,
27       blink::mojom::BackgroundFetchRegistrationDataPtr)>;
28 
29   GetRegistrationTask(DatabaseTaskHost* host,
30                       int64_t service_worker_registration_id,
31                       const url::Origin& origin,
32                       const std::string& developer_id,
33                       GetRegistrationCallback callback);
34 
35   ~GetRegistrationTask() override;
36 
37   // DatabaseTask implementation:
38   void Start() override;
39 
40  private:
41   void DidGetMetadata(
42       blink::mojom::BackgroundFetchError error,
43       std::unique_ptr<proto::BackgroundFetchMetadata> metadata_proto);
44 
45   void FinishWithError(blink::mojom::BackgroundFetchError error) override;
46 
47   std::string HistogramName() const override;
48 
49   int64_t service_worker_registration_id_;
50   url::Origin origin_;
51   std::string developer_id_;
52 
53   GetRegistrationCallback callback_;
54 
55   std::unique_ptr<proto::BackgroundFetchMetadata> metadata_proto_;
56 
57   base::WeakPtrFactory<GetRegistrationTask> weak_factory_{
58       this};  // Keep as last.
59 
60   DISALLOW_COPY_AND_ASSIGN(GetRegistrationTask);
61 };
62 
63 }  // namespace background_fetch
64 }  // namespace content
65 
66 #endif  // CONTENT_BROWSER_BACKGROUND_FETCH_STORAGE_GET_REGISTRATION_TASK_H_
67