| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IOS_CHROME_BROWSER_READING_LIST_URL_DOWNLOADER_H_ | 5 #ifndef IOS_CHROME_BROWSER_READING_LIST_URL_DOWNLOADER_H_ |
| 6 #define IOS_CHROME_BROWSER_READING_LIST_URL_DOWNLOADER_H_ | 6 #define IOS_CHROME_BROWSER_READING_LIST_URL_DOWNLOADER_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // This class downloads and deletes offline versions of URLs using DOM distiller | 24 // This class downloads and deletes offline versions of URLs using DOM distiller |
| 25 // to fetch the page and simplify it. Only one item is downloaded or deleted at | 25 // to fetch the page and simplify it. Only one item is downloaded or deleted at |
| 26 // a time using a queue of tasks that are handled sequentially. Items (page + | 26 // a time using a queue of tasks that are handled sequentially. Items (page + |
| 27 // images) are saved to individual folders within an offline folder, using md5 | 27 // images) are saved to individual folders within an offline folder, using md5 |
| 28 // hashing to create unique file names. When a deletion is requested, all | 28 // hashing to create unique file names. When a deletion is requested, all |
| 29 // previous downloads for that URL are cancelled as they would be deleted. | 29 // previous downloads for that URL are cancelled as they would be deleted. |
| 30 class URLDownloader { | 30 class URLDownloader { |
| 31 friend class MockURLDownloader; | 31 friend class MockURLDownloader; |
| 32 | 32 |
| 33 public: | 33 public: |
| 34 // A completion callback that takes a GURL and a bool indicating | 34 // And enum indicating different download outcomes. |
| 35 // success and returns void. | 35 enum SuccessState { |
| 36 DOWNLOAD_SUCCESS, |
| 37 DOWNLOAD_EXISTS, |
| 38 ERROR_RETRY, |
| 39 ERROR_PERMANENT |
| 40 }; |
| 41 |
| 42 // A completion callback that takes a GURL and a bool indicating the |
| 43 // outcome and returns void. |
| 36 using SuccessCompletion = base::Callback<void(const GURL&, bool)>; | 44 using SuccessCompletion = base::Callback<void(const GURL&, bool)>; |
| 37 | 45 |
| 46 // A download completion callback that takes, in order, the GURL that was |
| 47 // downloaded, a SuccessState indicating the outcome of the download, the |
| 48 // offline GURL for the download, and the title of the url, and returns void. |
| 49 // The offline GURL and title should not be used in case of failure. |
| 50 using DownloadCompletion = base::Callback< |
| 51 void(const GURL&, SuccessState, const GURL&, const std::string&)>; |
| 52 |
| 38 // Create a URL downloader with completion callbacks for downloads and | 53 // Create a URL downloader with completion callbacks for downloads and |
| 39 // deletions. | 54 // deletions. The completion callbacks will be called with the original url |
| 55 // and a boolean indicating success. For downloads, if distillation was |
| 56 // successful, it will also include the distilled url and extracted title. |
| 40 URLDownloader(dom_distiller::DomDistillerService* distiller_service, | 57 URLDownloader(dom_distiller::DomDistillerService* distiller_service, |
| 41 PrefService* prefs, | 58 PrefService* prefs, |
| 42 base::FilePath chrome_profile_path, | 59 base::FilePath chrome_profile_path, |
| 43 const SuccessCompletion& download_completion, | 60 const DownloadCompletion& download_completion, |
| 44 const SuccessCompletion& delete_completion); | 61 const SuccessCompletion& delete_completion); |
| 45 virtual ~URLDownloader(); | 62 virtual ~URLDownloader(); |
| 46 | 63 |
| 47 // Asynchronously download an offline version of the URL. | 64 // Asynchronously download an offline version of the URL. |
| 48 void DownloadOfflineURL(const GURL& url); | 65 void DownloadOfflineURL(const GURL& url); |
| 49 | 66 |
| 50 // Asynchronously remove the offline version of the URL if it exists. | 67 // Asynchronously remove the offline version of the URL if it exists. |
| 51 void RemoveOfflineURL(const GURL& url); | 68 void RemoveOfflineURL(const GURL& url); |
| 52 | 69 |
| 53 private: | 70 private: |
| 54 enum TaskType { DELETE, DOWNLOAD }; | 71 enum TaskType { DELETE, DOWNLOAD }; |
| 55 using Task = std::pair<TaskType, GURL>; | 72 using Task = std::pair<TaskType, GURL>; |
| 56 | 73 |
| 57 // Calls callback with true if an offline file exists for this URL. | 74 // Calls callback with true if an offline file exists for this URL. |
| 58 void OfflineURLExists(const GURL& url, base::Callback<void(bool)> callback); | 75 void OfflineURLExists(const GURL& url, base::Callback<void(bool)> callback); |
| 59 // Handles the next task in the queue, if no task is currently being handled. | 76 // Handles the next task in the queue, if no task is currently being handled. |
| 60 void HandleNextTask(); | 77 void HandleNextTask(); |
| 61 // Callback for completed (or failed) download, handles calling | 78 // Callback for completed (or failed) download, handles calling |
| 62 // downloadCompletion and starting the next task. | 79 // downloadCompletion and starting the next task. |
| 63 void DownloadCompletionHandler(const GURL& url, bool success); | 80 void DownloadCompletionHandler(const GURL& url, |
| 81 const std::string& title, |
| 82 SuccessState success); |
| 64 // Callback for completed (or failed) deletion, handles calling | 83 // Callback for completed (or failed) deletion, handles calling |
| 65 // deleteCompletion and starting the next task. | 84 // deleteCompletion and starting the next task. |
| 66 void DeleteCompletionHandler(const GURL& url, bool success); | 85 void DeleteCompletionHandler(const GURL& url, bool success); |
| 67 // The path of the directory where offline URLs are saved. | 86 // The path of the directory where offline URLs are saved. |
| 68 base::FilePath OfflineDirectoryPath(); | 87 base::FilePath OfflineDirectoryPath(); |
| 69 // The path of the directory where a specific URL is saved offline. Contains | 88 // The path of the directory where a specific URL is saved offline. Contains |
| 70 // the page and supporting files (images). | 89 // the page and supporting files (images). |
| 71 base::FilePath OfflineURLDirectoryPath(const GURL& url); | 90 base::FilePath OfflineURLDirectoryPath(const GURL& url); |
| 72 // The path of the offline webpage for the URL. The file may not exist. | 91 // The path of the offline webpage for the URL. The file may not exist. |
| 73 base::FilePath OfflineURLPagePath(const GURL& url); | 92 base::FilePath OfflineURLPagePath(const GURL& url); |
| 74 // Creates the offline directory for |url|. Returns true if successful or if | 93 // Creates the offline directory for |url|. Returns true if successful or if |
| 75 // the directory already exists. | 94 // the directory already exists. |
| 76 bool CreateOfflineURLDirectory(const GURL& url); | 95 bool CreateOfflineURLDirectory(const GURL& url); |
| 77 // Saves the |data| for image at |imageURL| to disk, for main URL |url|; | 96 // Saves the |data| for image at |imageURL| to disk, for main URL |url|; |
| 78 // returns path of saved file. | 97 // puts path of saved file in |path| and returns whether save was successful. |
| 79 std::string SaveImage(const GURL& url, | 98 bool SaveImage(const GURL& url, |
| 80 const GURL& imageURL, | 99 const GURL& imageURL, |
| 81 const std::string& data); | 100 const std::string& data, |
| 101 base::FilePath& path); |
| 82 // Saves images in |images| array to disk and replaces references in |html| to | 102 // Saves images in |images| array to disk and replaces references in |html| to |
| 83 // local path. Returns updated html. | 103 // local path. Returns updated html. |
| 84 std::string SaveAndReplaceImagesInHTML( | 104 std::string SaveAndReplaceImagesInHTML( |
| 85 const GURL& url, | 105 const GURL& url, |
| 86 const std::string& html, | 106 const std::string& html, |
| 87 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& | 107 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& |
| 88 images); | 108 images); |
| 89 // Saves |html| to disk in the correct location for |url|; returns success. | 109 // Saves |html| to disk in the correct location for |url|; returns success. |
| 90 bool SaveHTMLForURL(std::string html, const GURL& url); | 110 bool SaveHTMLForURL(std::string html, const GURL& url); |
| 91 // Downloads |url|, depending on |offlineURLExists| state. | 111 // Downloads |url|, depending on |offlineURLExists| state. |
| 92 virtual void DownloadURL(GURL url, bool offlineURLExists); | 112 virtual void DownloadURL(GURL url, bool offlineURLExists); |
| 93 // Saves distilled html to disk, including saving images and main file. | 113 // Saves distilled html to disk, including saving images and main file. |
| 94 bool SaveDistilledHTML( | 114 SuccessState SaveDistilledHTML( |
| 95 const GURL& url, | 115 const GURL& url, |
| 96 std::vector<dom_distiller::DistillerViewerInterface::ImageInfo> images, | 116 std::vector<dom_distiller::DistillerViewerInterface::ImageInfo> images, |
| 97 std::string html); | 117 std::string html); |
| 98 // Callback for distillation completion. | 118 // Callback for distillation completion. |
| 99 void DistillerCallback( | 119 void DistillerCallback( |
| 100 const GURL& pageURL, | 120 const GURL& pageURL, |
| 101 const std::string& html, | 121 const std::string& html, |
| 102 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& | 122 const std::vector<dom_distiller::DistillerViewerInterface::ImageInfo>& |
| 103 images); | 123 images, |
| 124 const std::string& title); |
| 104 | 125 |
| 105 dom_distiller::DomDistillerService* distiller_service_; | 126 dom_distiller::DomDistillerService* distiller_service_; |
| 106 PrefService* pref_service_; | 127 PrefService* pref_service_; |
| 107 const SuccessCompletion download_completion_; | 128 const DownloadCompletion download_completion_; |
| 108 const SuccessCompletion delete_completion_; | 129 const SuccessCompletion delete_completion_; |
| 109 std::deque<Task> tasks_; | 130 std::deque<Task> tasks_; |
| 110 bool working_; | 131 bool working_; |
| 111 base::FilePath base_directory_; | 132 base::FilePath base_directory_; |
| 112 std::unique_ptr<dom_distiller::DistillerViewerInterface> distiller_; | 133 std::unique_ptr<dom_distiller::DistillerViewerInterface> distiller_; |
| 113 base::CancelableTaskTracker task_tracker_; | 134 base::CancelableTaskTracker task_tracker_; |
| 114 | 135 |
| 115 DISALLOW_COPY_AND_ASSIGN(URLDownloader); | 136 DISALLOW_COPY_AND_ASSIGN(URLDownloader); |
| 116 }; | 137 }; |
| 117 | 138 |
| 118 #endif // IOS_CHROME_BROWSER_READING_LIST_URL_DOWNLOADER_H_ | 139 #endif // IOS_CHROME_BROWSER_READING_LIST_URL_DOWNLOADER_H_ |
| OLD | NEW |