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 COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_ARCHIVE_PUBLISHER_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_ARCHIVE_PUBLISHER_H_
7 
8 #include <cstdint>
9 #include <string>
10 
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "components/offline_pages/core/offline_page_item.h"
14 #include "components/offline_pages/core/offline_page_types.h"
15 
16 namespace base {
17 class SequencedTaskRunner;
18 }  // namespace base
19 
20 namespace offline_pages {
21 
22 // These constants are used to set offline_page_item.download_id when no
23 // download ID is available.
24 const int64_t kArchiveNotPublished = 0LL;
25 const int64_t kArchivePublishedWithoutDownloadId = -1LL;
26 
27 // Identifies one published archive. Before Android Q, a published archive is
28 // assigned a download ID; on Q and later, a published archive is assigned a
29 // content URI.
30 struct PublishedArchiveId {
31   PublishedArchiveId() = default;
PublishedArchiveIdPublishedArchiveId32   PublishedArchiveId(int64_t download_id, const base::FilePath& new_file_path)
33       : download_id(download_id), new_file_path(new_file_path) {}
34   bool operator==(const PublishedArchiveId& other) const {
35     return download_id == other.download_id &&
36            new_file_path == other.new_file_path;
37   }
38 
39   // Identifier returned by Android DownloadManager when present, or
40   // kArchivePublishedWithoutDownloadManager otherwise. Set to
41   // kArchiveNotPublished if publishing failed.
42   int64_t download_id = kArchiveNotPublished;
43 
44   // The published archive's path or content URI; empty if publishing failed.
45   base::FilePath new_file_path;
46 };
47 
48 // The result of publishing an offline page to Downloads.
49 struct PublishArchiveResult {
50   SavePageResult move_result;
51   PublishedArchiveId id;
52 
53   static PublishArchiveResult Failure(SavePageResult save_page_result);
54 };
55 
56 // Interface of a class responsible for publishing offline page archives to
57 // downloads.
58 class OfflinePageArchivePublisher {
59  public:
60   using PublishArchiveDoneCallback =
61       base::OnceCallback<void(const OfflinePageItem& /* offline_page */,
62                               PublishArchiveResult /* archive_result */)>;
63 
~OfflinePageArchivePublisher()64   virtual ~OfflinePageArchivePublisher() {}
65 
66   // Publishes the page on a background thread, then returns to the
67   // OfflinePageModelTaskified's done callback.
68   virtual void PublishArchive(
69       const OfflinePageItem& offline_page,
70       const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
71       PublishArchiveDoneCallback publish_done_callback) const = 0;
72 
73   // Removes  archives from downloads.
74   virtual void UnpublishArchives(
75       const std::vector<PublishedArchiveId>& archive_ids) const = 0;
76 };
77 
78 }  // namespace offline_pages
79 
80 #endif  // COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_ARCHIVE_PUBLISHER_H_
81