Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(904)

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_download_service.cc

Issue 2650753003: [ReadingList iOS] Cleanup Offline directory on startup (Closed)
Patch Set: cleanup Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "ios/chrome/browser/reading_list/reading_list_download_service.h" 5 #include "ios/chrome/browser/reading_list/reading_list_download_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_enumerator.h"
10 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
11 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
12 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
13 #include "components/reading_list/ios/offline_url_utils.h" 15 #include "components/reading_list/ios/offline_url_utils.h"
14 #include "components/reading_list/ios/reading_list_entry.h" 16 #include "components/reading_list/ios/reading_list_entry.h"
15 #include "components/reading_list/ios/reading_list_model.h" 17 #include "components/reading_list/ios/reading_list_model.h"
16 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h" 18 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h"
17 #include "ios/web/public/web_thread.h" 19 #include "ios/web/public/web_thread.h"
18 20
19 namespace { 21 namespace {
20 // Status of the download when it ends, for UMA report. 22 // Status of the download when it ends, for UMA report.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 return reading_list::OfflineRootDirectoryPath(chrome_profile_path_); 77 return reading_list::OfflineRootDirectoryPath(chrome_profile_path_);
76 } 78 }
77 79
78 void ReadingListDownloadService::Shutdown() { 80 void ReadingListDownloadService::Shutdown() {
79 reading_list_model_->RemoveObserver(this); 81 reading_list_model_->RemoveObserver(this);
80 } 82 }
81 83
82 void ReadingListDownloadService::ReadingListModelLoaded( 84 void ReadingListDownloadService::ReadingListModelLoaded(
83 const ReadingListModel* model) { 85 const ReadingListModel* model) {
84 DCHECK_EQ(reading_list_model_, model); 86 DCHECK_EQ(reading_list_model_, model);
85 DownloadAllEntries(); 87 DownloadAllEntries();
gambard 2017/01/25 09:02:07 Don't you need to update this?
Olivier 2017/01/25 12:25:30 mmm, yes, I think it would work better if this is
86 } 88 }
87 89
88 void ReadingListDownloadService::ReadingListWillRemoveEntry( 90 void ReadingListDownloadService::ReadingListWillRemoveEntry(
89 const ReadingListModel* model, 91 const ReadingListModel* model,
90 const GURL& url) { 92 const GURL& url) {
91 DCHECK_EQ(reading_list_model_, model); 93 DCHECK_EQ(reading_list_model_, model);
92 DCHECK(model->GetEntryByURL(url)); 94 DCHECK(model->GetEntryByURL(url));
93 RemoveDownloadedEntry(url); 95 RemoveDownloadedEntry(url);
94 } 96 }
95 97
(...skipping 14 matching lines...) Expand all
110 112
111 void ReadingListDownloadService::ProcessNewEntry(const GURL& url) { 113 void ReadingListDownloadService::ProcessNewEntry(const GURL& url) {
112 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); 114 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
113 if (!entry || entry->IsRead()) { 115 if (!entry || entry->IsRead()) {
114 url_downloader_->CancelDownloadOfflineURL(url); 116 url_downloader_->CancelDownloadOfflineURL(url);
115 } else { 117 } else {
116 ScheduleDownloadEntry(url); 118 ScheduleDownloadEntry(url);
117 } 119 }
118 } 120 }
119 121
120 void ReadingListDownloadService::DownloadAllEntries() { 122 void ReadingListDownloadService::SyncWithModel() {
121 DCHECK(reading_list_model_->loaded()); 123 DCHECK(reading_list_model_->loaded());
124 std::set<std::string> processed_directories;
125 std::set<GURL> unprocessed_entries;
122 for (const auto& url : reading_list_model_->Keys()) { 126 for (const auto& url : reading_list_model_->Keys()) {
127 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
128 if (entry->DistilledState() == ReadingListEntry::PROCESSED) {
jif 2017/01/25 09:15:51 You know the drill. Can you do a: switch (entry->
Olivier 2017/01/25 12:25:30 Done.
129 std::string directory_name = reading_list::OfflineURLDirectoryID(url);
130 processed_directories.insert(directory_name);
jif 2017/01/25 09:15:51 can you do: processed_directories.insert(reading_
Olivier 2017/01/25 12:25:30 Done.
131 } else if (entry->DistilledState() != ReadingListEntry::ERROR) {
132 unprocessed_entries.insert(url);
133 }
134 }
135 web::WebThread::PostTaskAndReply(
136 web::WebThread::FILE, FROM_HERE,
137 base::Bind(&ReadingListDownloadService::CleanUpFiles,
138 base::Unretained(this), processed_directories),
139 base::Bind(&ReadingListDownloadService::DownloadUnprocessedEntries,
140 base::Unretained(this), unprocessed_entries));
141 }
142
143 void ReadingListDownloadService::CleanUpFiles(
144 const std::set<std::string>& processed_directories) {
145 base::FileEnumerator file_enumerator(OfflineRoot(), false,
146 base::FileEnumerator::DIRECTORIES);
147 for (base::FilePath sub_directory = file_enumerator.Next();
148 !sub_directory.empty(); sub_directory = file_enumerator.Next()) {
149 std::string directory_name = sub_directory.BaseName().value();
150 if (!processed_directories.count(directory_name)) {
151 base::DeleteFile(sub_directory, true);
152 }
153 }
154 }
155
156 void ReadingListDownloadService::DownloadUnprocessedEntries(
157 const std::set<GURL>& unprocessed_entries) {
158 for (const GURL& url : unprocessed_entries) {
123 this->ScheduleDownloadEntry(url); 159 this->ScheduleDownloadEntry(url);
124 } 160 }
125 } 161 }
126 162
127 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) { 163 void ReadingListDownloadService::ScheduleDownloadEntry(const GURL& url) {
128 DCHECK(reading_list_model_->loaded()); 164 DCHECK(reading_list_model_->loaded());
129 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url); 165 const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
130 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR || 166 if (!entry || entry->DistilledState() == ReadingListEntry::ERROR ||
131 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead()) 167 entry->DistilledState() == ReadingListEntry::PROCESSED || entry->IsRead())
132 return; 168 return;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 for (auto& url : url_to_download_cellular_) { 281 for (auto& url : url_to_download_cellular_) {
246 ScheduleDownloadEntry(url); 282 ScheduleDownloadEntry(url);
247 } 283 }
248 } 284 }
249 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) { 285 if (type == net::NetworkChangeNotifier::CONNECTION_WIFI) {
250 for (auto& url : url_to_download_wifi_) { 286 for (auto& url : url_to_download_wifi_) {
251 ScheduleDownloadEntry(url); 287 ScheduleDownloadEntry(url);
252 } 288 }
253 } 289 }
254 } 290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698