offline_page_archive_publisher.h 2.8 KB

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