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_READING_LIST_DOWNLOAD_SERVICE_H_ | 5 #ifndef IOS_CHROME_BROWSER_READING_LIST_READING_LIST_DOWNLOAD_SERVICE_H_ |
6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_DOWNLOAD_SERVICE_H_ | 6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_DOWNLOAD_SERVICE_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "components/keyed_service/core/keyed_service.h" | 9 #include "components/keyed_service/core/keyed_service.h" |
10 #include "ios/chrome/browser/reading_list/reading_list_model_observer.h" | 10 #include "ios/chrome/browser/reading_list/reading_list_model_observer.h" |
11 #include "ios/chrome/browser/reading_list/url_downloader.h" | 11 #include "ios/chrome/browser/reading_list/url_downloader.h" |
| 12 #include "net/base/network_change_notifier.h" |
12 | 13 |
13 class GURL; | 14 class GURL; |
14 class PrefService; | 15 class PrefService; |
15 class ReadingListModel; | 16 class ReadingListModel; |
16 namespace base { | 17 namespace base { |
17 class FilePath; | 18 class FilePath; |
18 } | 19 } |
19 | 20 |
20 namespace dom_distiller { | 21 namespace dom_distiller { |
21 class DomDistillerService; | 22 class DomDistillerService; |
22 } | 23 } |
23 | 24 |
24 // Observes the reading list and downloads offline versions of its articles. | 25 // Observes the reading list and downloads offline versions of its articles. |
25 // Any calls made to DownloadAllEntries/DownloadEntry before the model is | 26 // Any calls made to DownloadAllEntries/DownloadEntry before the model is |
26 // loaded will be ignored. When the model is loaded, DownloadAllEntries will be | 27 // loaded will be ignored. When the model is loaded, DownloadAllEntries will be |
27 // called automatically. | 28 // called automatically. |
28 class ReadingListDownloadService : public KeyedService, | 29 class ReadingListDownloadService |
29 public ReadingListModelObserver { | 30 : public KeyedService, |
| 31 public ReadingListModelObserver, |
| 32 public net::NetworkChangeNotifier::ConnectionTypeObserver { |
30 public: | 33 public: |
31 ReadingListDownloadService( | 34 ReadingListDownloadService( |
32 ReadingListModel* reading_list_model, | 35 ReadingListModel* reading_list_model, |
33 dom_distiller::DomDistillerService* distiller_service, | 36 dom_distiller::DomDistillerService* distiller_service, |
34 PrefService* prefs, | 37 PrefService* prefs, |
35 base::FilePath chrome_profile_path); | 38 base::FilePath chrome_profile_path); |
36 ~ReadingListDownloadService() override; | 39 ~ReadingListDownloadService() override; |
37 | 40 |
38 // Initializes the reading list download service. | 41 // Initializes the reading list download service. |
39 void Initialize(); | 42 void Initialize(); |
40 | 43 |
41 // KeyedService implementation. | 44 // KeyedService implementation. |
42 void Shutdown() override; | 45 void Shutdown() override; |
43 | 46 |
44 // ReadingListModelObserver implementation | 47 // ReadingListModelObserver implementation |
45 void ReadingListModelLoaded(const ReadingListModel* model) override; | 48 void ReadingListModelLoaded(const ReadingListModel* model) override; |
46 void ReadingListWillRemoveReadEntry(const ReadingListModel* model, | 49 void ReadingListWillRemoveReadEntry(const ReadingListModel* model, |
47 size_t index) override; | 50 size_t index) override; |
48 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model, | 51 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model, |
49 size_t index) override; | 52 size_t index) override; |
50 void ReadingListWillAddUnreadEntry(const ReadingListModel* model, | 53 void ReadingListWillAddUnreadEntry(const ReadingListModel* model, |
51 const ReadingListEntry& entry) override; | 54 const ReadingListEntry& entry) override; |
52 void ReadingListWillAddReadEntry(const ReadingListModel* model, | 55 void ReadingListWillAddReadEntry(const ReadingListModel* model, |
53 const ReadingListEntry& entry) override; | 56 const ReadingListEntry& entry) override; |
54 | 57 |
55 private: | 58 private: |
56 // Tries to save offline versions of all entries in the reading list that are | 59 // Tries to save offline versions of all entries in the reading list that are |
57 // not yet saved. Must only be called after reading list model is loaded. | 60 // not yet saved. Must only be called after reading list model is loaded. |
58 void DownloadAllEntries(); | 61 void DownloadAllEntries(); |
| 62 // Schedule a download of an offline version of the reading list entry, |
| 63 // according to the delay of the entry. Must only be called after reading list |
| 64 // model is loaded. |
| 65 void ScheduleDownloadEntry(const ReadingListEntry& entry); |
| 66 // Tries to save an offline version of the reading list entry corresponding to |
| 67 // the |url| if it is not yet saved. Must only be called after reading list |
| 68 // model is loaded. |
| 69 void DownloadEntryFromURL(const GURL& url); |
| 70 // Schedule a download of an offline version of the reading list entry |
| 71 // associated to this |url|, according to the delay of the entry. Must only be |
| 72 // called after reading list model is loaded. |
| 73 void ScheduleDownloadEntryFromURL(const GURL& url); |
59 // Tries to save an offline version of the reading list entry if it is not yet | 74 // Tries to save an offline version of the reading list entry if it is not yet |
60 // saved. Must only be called after reading list model is loaded. | 75 // saved. Must only be called after reading list model is loaded. |
61 void DownloadEntry(const ReadingListEntry& entry); | 76 void DownloadEntry(const ReadingListEntry& entry); |
62 // Removes the offline version of the reading list entry if it exists. Must | 77 // Removes the offline version of the reading list entry if it exists. Must |
63 // only be called after reading list model is loaded. | 78 // only be called after reading list model is loaded. |
64 void RemoveDownloadedEntry(const ReadingListEntry& entry); | 79 void RemoveDownloadedEntry(const ReadingListEntry& entry); |
65 // Callback for entry download. | 80 // Callback for entry download. |
66 void OnDownloadEnd(const GURL& url, | 81 void OnDownloadEnd(const GURL& url, |
67 URLDownloader::SuccessState success, | 82 URLDownloader::SuccessState success, |
68 const GURL& distilled_url, | 83 const GURL& distilled_url, |
69 const std::string& title); | 84 const std::string& title); |
70 | 85 |
71 // Callback for entry deletion. | 86 // Callback for entry deletion. |
72 void OnDeleteEnd(const GURL& url, bool success); | 87 void OnDeleteEnd(const GURL& url, bool success); |
73 | 88 |
| 89 // net::NetworkChangeNotifier::ConnectionTypeObserver: |
| 90 void OnConnectionTypeChanged( |
| 91 net::NetworkChangeNotifier::ConnectionType type) override; |
| 92 |
74 ReadingListModel* reading_list_model_; | 93 ReadingListModel* reading_list_model_; |
75 std::unique_ptr<URLDownloader> url_downloader_; | 94 std::unique_ptr<URLDownloader> url_downloader_; |
| 95 std::vector<GURL> url_to_download_cellular_; |
| 96 std::vector<GURL> url_to_download_wifi_; |
| 97 bool had_connection_; |
| 98 |
| 99 base::WeakPtrFactory<ReadingListDownloadService> weak_ptr_factory_; |
76 | 100 |
77 DISALLOW_COPY_AND_ASSIGN(ReadingListDownloadService); | 101 DISALLOW_COPY_AND_ASSIGN(ReadingListDownloadService); |
78 }; | 102 }; |
79 | 103 |
80 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_DOWNLOAD_SERVICE_H_ | 104 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_DOWNLOAD_SERVICE_H_ |
OLD | NEW |