Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 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 "components/offline_pages/offline_page_model.h" | 5 #include "components/offline_pages/offline_page_model_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 34 // be deleted or re-ordered and new ones should be added to the end. | 34 // be deleted or re-ordered and new ones should be added to the end. |
| 35 enum ClearAllStatus { | 35 enum ClearAllStatus { |
| 36 CLEAR_ALL_SUCCEEDED, | 36 CLEAR_ALL_SUCCEEDED, |
| 37 STORE_RESET_FAILED, | 37 STORE_RESET_FAILED, |
| 38 STORE_RELOAD_FAILED, | 38 STORE_RELOAD_FAILED, |
| 39 | 39 |
| 40 // NOTE: always keep this entry at the end. | 40 // NOTE: always keep this entry at the end. |
| 41 CLEAR_ALL_STATUS_COUNT | 41 CLEAR_ALL_STATUS_COUNT |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 int64_t GenerateOfflineId() { | |
| 45 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | |
| 46 } | |
| 47 | |
| 44 // The maximum histogram size for the metrics that measure time between views of | 48 // The maximum histogram size for the metrics that measure time between views of |
| 45 // a given page. | 49 // a given page. |
| 46 const base::TimeDelta kMaxOpenedPageHistogramBucket = | 50 const base::TimeDelta kMaxOpenedPageHistogramBucket = |
| 47 base::TimeDelta::FromDays(90); | 51 base::TimeDelta::FromDays(90); |
| 48 | 52 |
| 49 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 53 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
| 50 SavePageResult result; | 54 SavePageResult result; |
| 51 switch (archiver_result) { | 55 switch (archiver_result) { |
| 52 case ArchiverResult::SUCCESSFULLY_CREATED: | 56 case ArchiverResult::SUCCESSFULLY_CREATED: |
| 53 result = SavePageResult::SUCCESS; | 57 result = SavePageResult::SUCCESS; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 (storage_stats.total_archives_size + storage_stats.free_disk_space) * | 121 (storage_stats.total_archives_size + storage_stats.free_disk_space) * |
| 118 100); | 122 100); |
| 119 UMA_HISTOGRAM_PERCENTAGE( | 123 UMA_HISTOGRAM_PERCENTAGE( |
| 120 "OfflinePages.DeletePage.TotalPageSizeAsPercentageOfFreeSpace", | 124 "OfflinePages.DeletePage.TotalPageSizeAsPercentageOfFreeSpace", |
| 121 percentage_of_free); | 125 percentage_of_free); |
| 122 } | 126 } |
| 123 } | 127 } |
| 124 | 128 |
| 125 } // namespace | 129 } // namespace |
| 126 | 130 |
| 127 // static | 131 // protected |
| 128 bool OfflinePageModel::CanSavePage(const GURL& url) { | 132 OfflinePageModelImpl::OfflinePageModelImpl() |
| 129 return url.SchemeIsHTTPOrHTTPS(); | 133 : OfflinePageModel(), is_loaded_(false), weak_ptr_factory_(this) {} |
| 130 } | |
| 131 | 134 |
| 132 // protected | 135 OfflinePageModelImpl::OfflinePageModelImpl( |
| 133 OfflinePageModel::OfflinePageModel() | |
| 134 : is_loaded_(false), weak_ptr_factory_(this) {} | |
| 135 | |
| 136 OfflinePageModel::OfflinePageModel( | |
| 137 std::unique_ptr<OfflinePageMetadataStore> store, | 136 std::unique_ptr<OfflinePageMetadataStore> store, |
| 138 const base::FilePath& archives_dir, | 137 const base::FilePath& archives_dir, |
| 139 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 138 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 140 : store_(std::move(store)), | 139 : store_(std::move(store)), |
| 141 archives_dir_(archives_dir), | 140 archives_dir_(archives_dir), |
| 142 is_loaded_(false), | 141 is_loaded_(false), |
| 143 policy_controller_(new ClientPolicyController()), | 142 policy_controller_(new ClientPolicyController()), |
| 144 archive_manager_(new ArchiveManager(archives_dir, task_runner)), | 143 archive_manager_(new ArchiveManager(archives_dir, task_runner)), |
| 145 weak_ptr_factory_(this) { | 144 weak_ptr_factory_(this) { |
| 146 archive_manager_->EnsureArchivesDirCreated( | 145 archive_manager_->EnsureArchivesDirCreated( |
| 147 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, | 146 base::Bind(&OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone, |
| 148 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); | 147 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); |
| 149 } | 148 } |
| 150 | 149 |
| 151 OfflinePageModel::~OfflinePageModel() { | 150 OfflinePageModelImpl::~OfflinePageModelImpl() {} |
| 152 } | |
| 153 | 151 |
| 154 void OfflinePageModel::AddObserver(Observer* observer) { | 152 void OfflinePageModelImpl::AddObserver(Observer* observer) { |
| 155 observers_.AddObserver(observer); | 153 observers_.AddObserver(observer); |
| 156 } | 154 } |
| 157 | 155 |
| 158 void OfflinePageModel::RemoveObserver(Observer* observer) { | 156 void OfflinePageModelImpl::RemoveObserver(Observer* observer) { |
| 159 observers_.RemoveObserver(observer); | 157 observers_.RemoveObserver(observer); |
| 160 } | 158 } |
| 161 | 159 |
| 162 void OfflinePageModel::SavePage(const GURL& url, | 160 void OfflinePageModelImpl::SavePage( |
| 163 const ClientId& client_id, | 161 const GURL& url, |
| 164 std::unique_ptr<OfflinePageArchiver> archiver, | 162 const ClientId& client_id, |
| 165 const SavePageCallback& callback) { | 163 std::unique_ptr<OfflinePageArchiver> archiver, |
| 164 const SavePageCallback& callback) { | |
| 166 DCHECK(is_loaded_); | 165 DCHECK(is_loaded_); |
| 167 | 166 |
| 168 // Skip saving the page that is not intended to be saved, like local file | 167 // Skip saving the page that is not intended to be saved, like local file |
| 169 // page. | 168 // page. |
| 170 if (url.is_valid() && !CanSavePage(url)) { | 169 if (url.is_valid() && !CanSavePage(url)) { |
| 171 InformSavePageDone(callback, SavePageResult::SKIPPED, client_id, | 170 InformSavePageDone(callback, SavePageResult::SKIPPED, client_id, |
| 172 kInvalidOfflineId); | 171 kInvalidOfflineId); |
| 173 return; | 172 return; |
| 174 } | 173 } |
| 175 | 174 |
| 176 // The web contents is not available if archiver is not created and passed. | 175 // The web contents is not available if archiver is not created and passed. |
| 177 if (!archiver.get()) { | 176 if (!archiver.get()) { |
| 178 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, client_id, | 177 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, client_id, |
| 179 kInvalidOfflineId); | 178 kInvalidOfflineId); |
| 180 return; | 179 return; |
| 181 } | 180 } |
| 182 | 181 |
| 183 int64_t offline_id = GenerateOfflineId(); | 182 int64_t offline_id = GenerateOfflineId(); |
| 184 | 183 |
| 185 archiver->CreateArchive( | 184 archiver->CreateArchive( |
| 186 archives_dir_, | 185 archives_dir_, offline_id, |
| 187 offline_id, | 186 base::Bind(&OfflinePageModelImpl::OnCreateArchiveDone, |
| 188 base::Bind(&OfflinePageModel::OnCreateArchiveDone, | 187 weak_ptr_factory_.GetWeakPtr(), url, offline_id, client_id, |
| 189 weak_ptr_factory_.GetWeakPtr(), url, offline_id, | 188 base::Time::Now(), callback)); |
| 190 client_id, base::Time::Now(), callback)); | |
| 191 pending_archivers_.push_back(std::move(archiver)); | 189 pending_archivers_.push_back(std::move(archiver)); |
| 192 } | 190 } |
| 193 | 191 |
| 194 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) { | 192 void OfflinePageModelImpl::MarkPageAccessed(int64_t offline_id) { |
| 195 DCHECK(is_loaded_); | 193 DCHECK(is_loaded_); |
| 196 auto iter = offline_pages_.find(offline_id); | 194 auto iter = offline_pages_.find(offline_id); |
| 197 if (iter == offline_pages_.end()) | 195 if (iter == offline_pages_.end()) |
| 198 return; | 196 return; |
| 199 | 197 |
| 200 // Make a copy of the cached item and update it. The cached item should only | 198 // Make a copy of the cached item and update it. The cached item should only |
| 201 // be updated upon the successful store operation. | 199 // be updated upon the successful store operation. |
| 202 OfflinePageItem offline_page_item = iter->second; | 200 OfflinePageItem offline_page_item = iter->second; |
| 203 | 201 |
| 204 base::Time now = base::Time::Now(); | 202 base::Time now = base::Time::Now(); |
| 205 base::TimeDelta time_since_last_accessed = | 203 base::TimeDelta time_since_last_accessed = |
| 206 now - offline_page_item.last_access_time; | 204 now - offline_page_item.last_access_time; |
| 207 | 205 |
| 208 // When the access account is still zero, the page is opened for the first | 206 // When the access account is still zero, the page is opened for the first |
| 209 // time since its creation. | 207 // time since its creation. |
| 210 UMA_HISTOGRAM_CUSTOM_COUNTS( | 208 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 211 AddHistogramSuffix( | 209 AddHistogramSuffix(offline_page_item.client_id, |
|
dewittj
2016/05/26 16:38:43
I'd avoid reformatting code that is unchanged.
dougarnett
2016/05/26 20:50:34
Wasn't my intention - fallout of moving to new fil
| |
| 212 offline_page_item.client_id, | 210 (offline_page_item.access_count == 0) |
| 213 (offline_page_item.access_count == 0) ? | 211 ? "OfflinePages.FirstOpenSinceCreated" |
| 214 "OfflinePages.FirstOpenSinceCreated" : | 212 : "OfflinePages.OpenSinceLastOpen") |
| 215 "OfflinePages.OpenSinceLastOpen").c_str(), | 213 .c_str(), |
| 216 time_since_last_accessed.InMinutes(), 1, | 214 time_since_last_accessed.InMinutes(), 1, |
| 217 kMaxOpenedPageHistogramBucket.InMinutes(), 50); | 215 kMaxOpenedPageHistogramBucket.InMinutes(), 50); |
| 218 | 216 |
| 219 offline_page_item.last_access_time = now; | 217 offline_page_item.last_access_time = now; |
| 220 offline_page_item.access_count++; | 218 offline_page_item.access_count++; |
| 221 | 219 |
| 222 store_->AddOrUpdateOfflinePage( | 220 store_->AddOrUpdateOfflinePage( |
| 223 offline_page_item, | 221 offline_page_item, |
| 224 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, | 222 base::Bind(&OfflinePageModelImpl::OnMarkPageAccesseDone, |
| 225 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 223 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
| 226 } | 224 } |
| 227 | 225 |
| 228 void OfflinePageModel::DeletePagesByOfflineId( | 226 void OfflinePageModelImpl::DeletePagesByOfflineId( |
| 229 const std::vector<int64_t>& offline_ids, | 227 const std::vector<int64_t>& offline_ids, |
| 230 const DeletePageCallback& callback) { | 228 const DeletePageCallback& callback) { |
| 231 if (!is_loaded_) { | 229 if (!is_loaded_) { |
| 232 delayed_tasks_.push_back( | 230 delayed_tasks_.push_back( |
| 233 base::Bind(&OfflinePageModel::DoDeletePagesByOfflineId, | 231 base::Bind(&OfflinePageModelImpl::DoDeletePagesByOfflineId, |
| 234 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 232 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 235 | 233 |
| 236 return; | 234 return; |
| 237 } | 235 } |
| 238 DoDeletePagesByOfflineId(offline_ids, callback); | 236 DoDeletePagesByOfflineId(offline_ids, callback); |
| 239 } | 237 } |
| 240 | 238 |
| 241 void OfflinePageModel::DoDeletePagesByOfflineId( | 239 void OfflinePageModelImpl::DoDeletePagesByOfflineId( |
| 242 const std::vector<int64_t>& offline_ids, | 240 const std::vector<int64_t>& offline_ids, |
| 243 const DeletePageCallback& callback) { | 241 const DeletePageCallback& callback) { |
| 244 DCHECK(is_loaded_); | 242 DCHECK(is_loaded_); |
| 245 | 243 |
| 246 std::vector<base::FilePath> paths_to_delete; | 244 std::vector<base::FilePath> paths_to_delete; |
| 247 for (const auto& offline_id : offline_ids) { | 245 for (const auto& offline_id : offline_ids) { |
| 248 auto iter = offline_pages_.find(offline_id); | 246 auto iter = offline_pages_.find(offline_id); |
| 249 if (iter != offline_pages_.end()) { | 247 if (iter != offline_pages_.end()) { |
| 250 paths_to_delete.push_back(iter->second.file_path); | 248 paths_to_delete.push_back(iter->second.file_path); |
| 251 } | 249 } |
| 252 } | 250 } |
| 253 | 251 |
| 254 if (paths_to_delete.empty()) { | 252 if (paths_to_delete.empty()) { |
| 255 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | 253 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); |
| 256 return; | 254 return; |
| 257 } | 255 } |
| 258 | 256 |
| 259 archive_manager_->DeleteMultipleArchives( | 257 archive_manager_->DeleteMultipleArchives( |
| 260 paths_to_delete, | 258 paths_to_delete, |
| 261 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 259 base::Bind(&OfflinePageModelImpl::OnDeleteArchiveFilesDone, |
| 262 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 260 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 263 } | 261 } |
| 264 | 262 |
| 265 void OfflinePageModel::ClearAll(const base::Closure& callback) { | 263 void OfflinePageModelImpl::ClearAll(const base::Closure& callback) { |
| 266 DCHECK(is_loaded_); | 264 DCHECK(is_loaded_); |
| 267 | 265 |
| 268 std::vector<int64_t> offline_ids; | 266 std::vector<int64_t> offline_ids; |
| 269 for (const auto& id_page_pair : offline_pages_) | 267 for (const auto& id_page_pair : offline_pages_) |
| 270 offline_ids.push_back(id_page_pair.first); | 268 offline_ids.push_back(id_page_pair.first); |
| 271 DeletePagesByOfflineId( | 269 DeletePagesByOfflineId( |
| 272 offline_ids, | 270 offline_ids, |
| 273 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | 271 base::Bind(&OfflinePageModelImpl::OnRemoveAllFilesDoneForClearAll, |
| 274 weak_ptr_factory_.GetWeakPtr(), callback)); | 272 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 275 } | 273 } |
| 276 | 274 |
| 277 void OfflinePageModel::DeletePagesByURLPredicate( | 275 void OfflinePageModelImpl::DeletePagesByURLPredicate( |
| 278 const UrlPredicate& predicate, | 276 const UrlPredicate& predicate, |
| 279 const DeletePageCallback& callback) { | 277 const DeletePageCallback& callback) { |
| 280 if (!is_loaded_) { | 278 if (!is_loaded_) { |
| 281 delayed_tasks_.push_back( | 279 delayed_tasks_.push_back( |
| 282 base::Bind(&OfflinePageModel::DoDeletePagesByURLPredicate, | 280 base::Bind(&OfflinePageModelImpl::DoDeletePagesByURLPredicate, |
| 283 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); | 281 weak_ptr_factory_.GetWeakPtr(), predicate, callback)); |
| 284 | 282 |
| 285 return; | 283 return; |
| 286 } | 284 } |
| 287 DoDeletePagesByURLPredicate(predicate, callback); | 285 DoDeletePagesByURLPredicate(predicate, callback); |
| 288 } | 286 } |
| 289 | 287 |
| 290 void OfflinePageModel::DoDeletePagesByURLPredicate( | 288 void OfflinePageModelImpl::DoDeletePagesByURLPredicate( |
| 291 const UrlPredicate& predicate, | 289 const UrlPredicate& predicate, |
| 292 const DeletePageCallback& callback) { | 290 const DeletePageCallback& callback) { |
| 293 DCHECK(is_loaded_); | 291 DCHECK(is_loaded_); |
| 294 | 292 |
| 295 std::vector<int64_t> offline_ids; | 293 std::vector<int64_t> offline_ids; |
| 296 for (const auto& id_page_pair : offline_pages_) { | 294 for (const auto& id_page_pair : offline_pages_) { |
| 297 if (predicate.Run(id_page_pair.second.url)) | 295 if (predicate.Run(id_page_pair.second.url)) |
| 298 offline_ids.push_back(id_page_pair.first); | 296 offline_ids.push_back(id_page_pair.first); |
| 299 } | 297 } |
| 300 DoDeletePagesByOfflineId(offline_ids, callback); | 298 DoDeletePagesByOfflineId(offline_ids, callback); |
| 301 } | 299 } |
| 302 | 300 |
| 303 void OfflinePageModel::HasPages(const std::string& name_space, | 301 void OfflinePageModelImpl::HasPages(const std::string& name_space, |
| 304 const HasPagesCallback& callback) { | 302 const HasPagesCallback& callback) { |
| 305 RunWhenLoaded(base::Bind(&OfflinePageModel::HasPagesAfterLoadDone, | 303 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::HasPagesAfterLoadDone, |
| 306 weak_ptr_factory_.GetWeakPtr(), name_space, | 304 weak_ptr_factory_.GetWeakPtr(), name_space, |
| 307 callback)); | 305 callback)); |
| 308 } | 306 } |
| 309 | 307 |
| 310 void OfflinePageModel::HasPagesAfterLoadDone( | 308 void OfflinePageModelImpl::HasPagesAfterLoadDone( |
| 311 const std::string& name_space, | 309 const std::string& name_space, |
| 312 const HasPagesCallback& callback) const { | 310 const HasPagesCallback& callback) const { |
| 313 DCHECK(is_loaded_); | 311 DCHECK(is_loaded_); |
| 314 | 312 |
| 315 bool has_pages = false; | 313 bool has_pages = false; |
| 316 | 314 |
| 317 for (const auto& id_page_pair : offline_pages_) { | 315 for (const auto& id_page_pair : offline_pages_) { |
| 318 if (id_page_pair.second.client_id.name_space == name_space) { | 316 if (id_page_pair.second.client_id.name_space == name_space) { |
| 319 has_pages = true; | 317 has_pages = true; |
| 320 break; | 318 break; |
| 321 } | 319 } |
| 322 } | 320 } |
| 323 | 321 |
| 324 callback.Run(has_pages); | 322 callback.Run(has_pages); |
| 325 } | 323 } |
| 326 | 324 |
| 327 void OfflinePageModel::CheckPagesExistOffline( | 325 void OfflinePageModelImpl::CheckPagesExistOffline( |
| 328 const std::set<GURL>& urls, | 326 const std::set<GURL>& urls, |
| 329 const CheckPagesExistOfflineCallback& callback) { | 327 const CheckPagesExistOfflineCallback& callback) { |
| 330 RunWhenLoaded( | 328 RunWhenLoaded( |
| 331 base::Bind(&OfflinePageModel::CheckPagesExistOfflineAfterLoadDone, | 329 base::Bind(&OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone, |
| 332 weak_ptr_factory_.GetWeakPtr(), urls, callback)); | 330 weak_ptr_factory_.GetWeakPtr(), urls, callback)); |
| 333 } | 331 } |
| 334 | 332 |
| 335 void OfflinePageModel::CheckPagesExistOfflineAfterLoadDone( | 333 void OfflinePageModelImpl::CheckPagesExistOfflineAfterLoadDone( |
| 336 const std::set<GURL>& urls, | 334 const std::set<GURL>& urls, |
| 337 const CheckPagesExistOfflineCallback& callback) { | 335 const CheckPagesExistOfflineCallback& callback) { |
| 338 DCHECK(is_loaded_); | 336 DCHECK(is_loaded_); |
| 339 CheckPagesExistOfflineResult result; | 337 CheckPagesExistOfflineResult result; |
| 340 for (const auto& id_page_pair : offline_pages_) { | 338 for (const auto& id_page_pair : offline_pages_) { |
| 341 auto iter = urls.find(id_page_pair.second.url); | 339 auto iter = urls.find(id_page_pair.second.url); |
| 342 if (iter != urls.end()) | 340 if (iter != urls.end()) |
| 343 result.insert(*iter); | 341 result.insert(*iter); |
| 344 } | 342 } |
| 345 callback.Run(result); | 343 callback.Run(result); |
| 346 } | 344 } |
| 347 | 345 |
| 348 void OfflinePageModel::GetAllPages( | 346 void OfflinePageModelImpl::GetAllPages( |
| 349 const MultipleOfflinePageItemCallback& callback) { | 347 const MultipleOfflinePageItemCallback& callback) { |
| 350 RunWhenLoaded(base::Bind(&OfflinePageModel::GetAllPagesAfterLoadDone, | 348 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::GetAllPagesAfterLoadDone, |
| 351 weak_ptr_factory_.GetWeakPtr(), callback)); | 349 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 352 } | 350 } |
| 353 | 351 |
| 354 void OfflinePageModel::GetAllPagesAfterLoadDone( | 352 void OfflinePageModelImpl::GetAllPagesAfterLoadDone( |
| 355 const MultipleOfflinePageItemCallback& callback) { | 353 const MultipleOfflinePageItemCallback& callback) { |
| 356 DCHECK(is_loaded_); | 354 DCHECK(is_loaded_); |
| 357 | 355 |
| 358 MultipleOfflinePageItemResult offline_pages; | 356 MultipleOfflinePageItemResult offline_pages; |
| 359 for (const auto& id_page_pair : offline_pages_) | 357 for (const auto& id_page_pair : offline_pages_) |
| 360 offline_pages.push_back(id_page_pair.second); | 358 offline_pages.push_back(id_page_pair.second); |
| 361 | 359 |
| 362 callback.Run(offline_pages); | 360 callback.Run(offline_pages); |
| 363 } | 361 } |
| 364 | 362 |
| 365 void OfflinePageModel::GetOfflineIdsForClientId( | 363 void OfflinePageModelImpl::GetOfflineIdsForClientId( |
| 366 const ClientId& client_id, | 364 const ClientId& client_id, |
| 367 const MultipleOfflineIdCallback& callback) { | 365 const MultipleOfflineIdCallback& callback) { |
| 368 RunWhenLoaded( | 366 RunWhenLoaded( |
| 369 base::Bind(&OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone, | 367 base::Bind(&OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone, |
| 370 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); | 368 weak_ptr_factory_.GetWeakPtr(), client_id, callback)); |
| 371 } | 369 } |
| 372 | 370 |
| 373 void OfflinePageModel::GetOfflineIdsForClientIdWhenLoadDone( | 371 void OfflinePageModelImpl::GetOfflineIdsForClientIdWhenLoadDone( |
| 374 const ClientId& client_id, | 372 const ClientId& client_id, |
| 375 const MultipleOfflineIdCallback& callback) const { | 373 const MultipleOfflineIdCallback& callback) const { |
| 376 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); | 374 callback.Run(MaybeGetOfflineIdsForClientId(client_id)); |
| 377 } | 375 } |
| 378 | 376 |
| 379 const std::vector<int64_t> OfflinePageModel::MaybeGetOfflineIdsForClientId( | 377 const std::vector<int64_t> OfflinePageModelImpl::MaybeGetOfflineIdsForClientId( |
| 380 const ClientId& client_id) const { | 378 const ClientId& client_id) const { |
| 381 DCHECK(is_loaded_); | 379 DCHECK(is_loaded_); |
| 382 std::vector<int64_t> results; | 380 std::vector<int64_t> results; |
| 383 | 381 |
| 384 // We want only all pages, including those marked for deletion. | 382 // We want only all pages, including those marked for deletion. |
| 385 // TODO(bburns): actually use an index rather than linear scan. | 383 // TODO(bburns): actually use an index rather than linear scan. |
| 386 for (const auto& id_page_pair : offline_pages_) { | 384 for (const auto& id_page_pair : offline_pages_) { |
| 387 if (id_page_pair.second.client_id == client_id) | 385 if (id_page_pair.second.client_id == client_id) |
| 388 results.push_back(id_page_pair.second.offline_id); | 386 results.push_back(id_page_pair.second.offline_id); |
| 389 } | 387 } |
| 390 return results; | 388 return results; |
| 391 } | 389 } |
| 392 | 390 |
| 393 void OfflinePageModel::GetPageByOfflineId( | 391 void OfflinePageModelImpl::GetPageByOfflineId( |
| 394 int64_t offline_id, | 392 int64_t offline_id, |
| 395 const SingleOfflinePageItemCallback& callback) { | 393 const SingleOfflinePageItemCallback& callback) { |
| 396 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPageByOfflineIdWhenLoadDone, | 394 RunWhenLoaded( |
| 397 weak_ptr_factory_.GetWeakPtr(), offline_id, | 395 base::Bind(&OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone, |
| 398 callback)); | 396 weak_ptr_factory_.GetWeakPtr(), offline_id, callback)); |
| 399 } | 397 } |
| 400 | 398 |
| 401 void OfflinePageModel::GetPageByOfflineIdWhenLoadDone( | 399 void OfflinePageModelImpl::GetPageByOfflineIdWhenLoadDone( |
| 402 int64_t offline_id, | 400 int64_t offline_id, |
| 403 const SingleOfflinePageItemCallback& callback) const { | 401 const SingleOfflinePageItemCallback& callback) const { |
| 404 SingleOfflinePageItemResult result; | 402 SingleOfflinePageItemResult result; |
| 405 const OfflinePageItem* match = MaybeGetPageByOfflineId(offline_id); | 403 const OfflinePageItem* match = MaybeGetPageByOfflineId(offline_id); |
| 406 if (match != nullptr) | 404 if (match != nullptr) |
| 407 result = *match; | 405 result = *match; |
| 408 callback.Run(result); | 406 callback.Run(result); |
| 409 } | 407 } |
| 410 | 408 |
| 411 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOfflineId( | 409 const OfflinePageItem* OfflinePageModelImpl::MaybeGetPageByOfflineId( |
| 412 int64_t offline_id) const { | 410 int64_t offline_id) const { |
| 413 const auto iter = offline_pages_.find(offline_id); | 411 const auto iter = offline_pages_.find(offline_id); |
| 414 return iter != offline_pages_.end() ? &(iter->second) : nullptr; | 412 return iter != offline_pages_.end() ? &(iter->second) : nullptr; |
| 415 } | 413 } |
| 416 | 414 |
| 417 void OfflinePageModel::GetPageByOfflineURL( | 415 void OfflinePageModelImpl::GetPageByOfflineURL( |
| 418 const GURL& offline_url, | 416 const GURL& offline_url, |
| 419 const SingleOfflinePageItemCallback& callback) { | 417 const SingleOfflinePageItemCallback& callback) { |
| 420 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPageByOfflineURLWhenLoadDone, | 418 RunWhenLoaded( |
| 421 weak_ptr_factory_.GetWeakPtr(), offline_url, | 419 base::Bind(&OfflinePageModelImpl::GetPageByOfflineURLWhenLoadDone, |
| 422 callback)); | 420 weak_ptr_factory_.GetWeakPtr(), offline_url, callback)); |
| 423 } | 421 } |
| 424 | 422 |
| 425 void OfflinePageModel::GetPageByOfflineURLWhenLoadDone( | 423 void OfflinePageModelImpl::GetPageByOfflineURLWhenLoadDone( |
| 426 const GURL& offline_url, | 424 const GURL& offline_url, |
| 427 const SingleOfflinePageItemCallback& callback) const { | 425 const SingleOfflinePageItemCallback& callback) const { |
| 428 base::Optional<OfflinePageItem> result; | 426 base::Optional<OfflinePageItem> result; |
| 429 | 427 |
| 430 for (const auto& id_page_pair : offline_pages_) { | 428 for (const auto& id_page_pair : offline_pages_) { |
| 431 if (id_page_pair.second.GetOfflineURL() == offline_url) { | 429 if (id_page_pair.second.GetOfflineURL() == offline_url) { |
| 432 callback.Run(base::make_optional(id_page_pair.second)); | 430 callback.Run(base::make_optional(id_page_pair.second)); |
| 433 return; | 431 return; |
| 434 } | 432 } |
| 435 } | 433 } |
| 436 | 434 |
| 437 callback.Run(base::nullopt); | 435 callback.Run(base::nullopt); |
| 438 } | 436 } |
| 439 | 437 |
| 440 const OfflinePageItem* OfflinePageModel::MaybeGetPageByOfflineURL( | 438 const OfflinePageItem* OfflinePageModelImpl::MaybeGetPageByOfflineURL( |
| 441 const GURL& offline_url) const { | 439 const GURL& offline_url) const { |
| 442 for (const auto& id_page_pair : offline_pages_) { | 440 for (const auto& id_page_pair : offline_pages_) { |
| 443 if (id_page_pair.second.GetOfflineURL() == offline_url) | 441 if (id_page_pair.second.GetOfflineURL() == offline_url) |
| 444 return &(id_page_pair.second); | 442 return &(id_page_pair.second); |
| 445 } | 443 } |
| 446 return nullptr; | 444 return nullptr; |
| 447 } | 445 } |
| 448 | 446 |
| 449 void OfflinePageModel::GetBestPageForOnlineURL( | 447 void OfflinePageModelImpl::GetBestPageForOnlineURL( |
| 450 const GURL& online_url, | 448 const GURL& online_url, |
| 451 const SingleOfflinePageItemCallback callback) { | 449 const SingleOfflinePageItemCallback callback) { |
| 452 RunWhenLoaded( | 450 RunWhenLoaded( |
| 453 base::Bind(&OfflinePageModel::GetBestPageForOnlineURLWhenLoadDone, | 451 base::Bind(&OfflinePageModelImpl::GetBestPageForOnlineURLWhenLoadDone, |
| 454 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); | 452 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); |
| 455 } | 453 } |
| 456 | 454 |
| 457 void OfflinePageModel::GetBestPageForOnlineURLWhenLoadDone( | 455 void OfflinePageModelImpl::GetBestPageForOnlineURLWhenLoadDone( |
| 458 const GURL& online_url, | 456 const GURL& online_url, |
| 459 const SingleOfflinePageItemCallback& callback) const { | 457 const SingleOfflinePageItemCallback& callback) const { |
| 460 SingleOfflinePageItemResult result; | 458 SingleOfflinePageItemResult result; |
| 461 | 459 |
| 462 const OfflinePageItem* best_page = MaybeGetBestPageForOnlineURL(online_url); | 460 const OfflinePageItem* best_page = MaybeGetBestPageForOnlineURL(online_url); |
| 463 if (best_page != nullptr) | 461 if (best_page != nullptr) |
| 464 result = *best_page; | 462 result = *best_page; |
| 465 | 463 |
| 466 callback.Run(result); | 464 callback.Run(result); |
| 467 } | 465 } |
| 468 | 466 |
| 469 void OfflinePageModel::GetPagesByOnlineURL( | 467 void OfflinePageModelImpl::GetPagesByOnlineURL( |
| 470 const GURL& online_url, | 468 const GURL& online_url, |
| 471 const MultipleOfflinePageItemCallback& callback) { | 469 const MultipleOfflinePageItemCallback& callback) { |
| 472 RunWhenLoaded(base::Bind(&OfflinePageModel::GetPagesByOnlineURLWhenLoadDone, | 470 RunWhenLoaded( |
| 473 weak_ptr_factory_.GetWeakPtr(), online_url, | 471 base::Bind(&OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone, |
| 474 callback)); | 472 weak_ptr_factory_.GetWeakPtr(), online_url, callback)); |
| 475 } | 473 } |
| 476 | 474 |
| 477 void OfflinePageModel::GetPagesByOnlineURLWhenLoadDone( | 475 void OfflinePageModelImpl::GetPagesByOnlineURLWhenLoadDone( |
| 478 const GURL& online_url, | 476 const GURL& online_url, |
| 479 const MultipleOfflinePageItemCallback& callback) const { | 477 const MultipleOfflinePageItemCallback& callback) const { |
| 480 std::vector<OfflinePageItem> result; | 478 std::vector<OfflinePageItem> result; |
| 481 | 479 |
| 482 for (const auto& id_page_pair : offline_pages_) { | 480 for (const auto& id_page_pair : offline_pages_) { |
| 483 if (id_page_pair.second.url == online_url) | 481 if (id_page_pair.second.url == online_url) |
| 484 result.push_back(id_page_pair.second); | 482 result.push_back(id_page_pair.second); |
| 485 } | 483 } |
| 486 | 484 |
| 487 callback.Run(result); | 485 callback.Run(result); |
| 488 } | 486 } |
| 489 | 487 |
| 490 const OfflinePageItem* OfflinePageModel::MaybeGetBestPageForOnlineURL( | 488 const OfflinePageItem* OfflinePageModelImpl::MaybeGetBestPageForOnlineURL( |
| 491 const GURL& online_url) const { | 489 const GURL& online_url) const { |
| 492 const OfflinePageItem* result = nullptr; | 490 const OfflinePageItem* result = nullptr; |
| 493 for (const auto& id_page_pair : offline_pages_) { | 491 for (const auto& id_page_pair : offline_pages_) { |
| 494 if (id_page_pair.second.url == online_url) { | 492 if (id_page_pair.second.url == online_url) { |
| 495 if (!result || id_page_pair.second.creation_time > result->creation_time) | 493 if (!result || id_page_pair.second.creation_time > result->creation_time) |
| 496 result = &(id_page_pair.second); | 494 result = &(id_page_pair.second); |
| 497 } | 495 } |
| 498 } | 496 } |
| 499 return result; | 497 return result; |
| 500 } | 498 } |
| 501 | 499 |
| 502 void OfflinePageModel::CheckForExternalFileDeletion() { | 500 void OfflinePageModelImpl::CheckForExternalFileDeletion() { |
| 503 DCHECK(is_loaded_); | 501 DCHECK(is_loaded_); |
| 504 | 502 |
| 505 archive_manager_->GetAllArchives( | 503 archive_manager_->GetAllArchives( |
| 506 base::Bind(&OfflinePageModel::ScanForMissingArchiveFiles, | 504 base::Bind(&OfflinePageModelImpl::ScanForMissingArchiveFiles, |
| 507 weak_ptr_factory_.GetWeakPtr())); | 505 weak_ptr_factory_.GetWeakPtr())); |
| 508 } | 506 } |
| 509 | 507 |
| 510 void OfflinePageModel::ExpirePages(const std::vector<int64_t>& offline_ids, | 508 void OfflinePageModelImpl::ExpirePages(const std::vector<int64_t>& offline_ids, |
| 511 const base::Time& expiration_time) { | 509 const base::Time& expiration_time) { |
| 512 for (int64_t offline_id : offline_ids) { | 510 for (int64_t offline_id : offline_ids) { |
| 513 auto iter = offline_pages_.find(offline_id); | 511 auto iter = offline_pages_.find(offline_id); |
| 514 if (iter == offline_pages_.end()) | 512 if (iter == offline_pages_.end()) |
| 515 continue; | 513 continue; |
| 516 | 514 |
| 517 OfflinePageItem offline_page = iter->second; | 515 OfflinePageItem offline_page = iter->second; |
| 518 offline_page.expiration_time = expiration_time; | 516 offline_page.expiration_time = expiration_time; |
| 519 | 517 |
| 520 store_->AddOrUpdateOfflinePage( | 518 store_->AddOrUpdateOfflinePage( |
| 521 offline_page, base::Bind(&OfflinePageModel::OnExpirePageDone, | 519 offline_page, base::Bind(&OfflinePageModelImpl::OnExpirePageDone, |
| 522 weak_ptr_factory_.GetWeakPtr(), offline_id, | 520 weak_ptr_factory_.GetWeakPtr(), offline_id, |
| 523 expiration_time)); | 521 expiration_time)); |
| 524 } | 522 } |
| 525 } | 523 } |
| 526 | 524 |
| 527 void OfflinePageModel::OnExpirePageDone(int64_t offline_id, | 525 void OfflinePageModelImpl::OnExpirePageDone(int64_t offline_id, |
| 528 const base::Time& expiration_time, | 526 const base::Time& expiration_time, |
| 529 bool success) { | 527 bool success) { |
| 530 // TODO(romax): Report UMA about successful expiration. | 528 // TODO(romax): Report UMA about successful expiration. |
| 531 if (success) { | 529 if (success) { |
| 532 auto iter = offline_pages_.find(offline_id); | 530 auto iter = offline_pages_.find(offline_id); |
| 533 if (iter != offline_pages_.end()) | 531 if (iter != offline_pages_.end()) |
| 534 iter->second.expiration_time = expiration_time; | 532 iter->second.expiration_time = expiration_time; |
| 535 } | 533 } |
| 536 } | 534 } |
| 537 | 535 |
| 538 ClientPolicyController* OfflinePageModel::GetPolicyController() { | 536 ClientPolicyController* OfflinePageModelImpl::GetPolicyController() { |
| 539 return policy_controller_.get(); | 537 return policy_controller_.get(); |
| 540 } | 538 } |
| 541 | 539 |
| 542 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 540 OfflinePageMetadataStore* OfflinePageModelImpl::GetStoreForTesting() { |
| 543 return store_.get(); | 541 return store_.get(); |
| 544 } | 542 } |
| 545 | 543 |
| 546 OfflinePageStorageManager* OfflinePageModel::GetStorageManager() { | 544 OfflinePageStorageManager* OfflinePageModelImpl::GetStorageManager() { |
| 547 return storage_manager_.get(); | 545 return storage_manager_.get(); |
| 548 } | 546 } |
| 549 | 547 |
| 550 bool OfflinePageModel::is_loaded() const { | 548 bool OfflinePageModelImpl::is_loaded() const { |
| 551 return is_loaded_; | 549 return is_loaded_; |
| 552 } | 550 } |
| 553 | 551 |
| 554 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, | 552 void OfflinePageModelImpl::OnCreateArchiveDone(const GURL& requested_url, |
| 555 int64_t offline_id, | 553 int64_t offline_id, |
| 556 const ClientId& client_id, | 554 const ClientId& client_id, |
| 557 const base::Time& start_time, | 555 const base::Time& start_time, |
| 558 const SavePageCallback& callback, | 556 const SavePageCallback& callback, |
| 559 OfflinePageArchiver* archiver, | 557 OfflinePageArchiver* archiver, |
| 560 ArchiverResult archiver_result, | 558 ArchiverResult archiver_result, |
| 561 const GURL& url, | 559 const GURL& url, |
| 562 const base::FilePath& file_path, | 560 const base::FilePath& file_path, |
| 563 int64_t file_size) { | 561 int64_t file_size) { |
| 564 if (requested_url != url) { | 562 if (requested_url != url) { |
| 565 DVLOG(1) << "Saved URL does not match requested URL."; | 563 DVLOG(1) << "Saved URL does not match requested URL."; |
| 566 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 564 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 567 // deleted from here, once archiver has the right functionality. | 565 // deleted from here, once archiver has the right functionality. |
| 568 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, | 566 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, |
| 569 client_id, offline_id); | 567 client_id, offline_id); |
| 570 DeletePendingArchiver(archiver); | 568 DeletePendingArchiver(archiver); |
| 571 return; | 569 return; |
| 572 } | 570 } |
| 573 | 571 |
| 574 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 572 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 575 SavePageResult result = ToSavePageResult(archiver_result); | 573 SavePageResult result = ToSavePageResult(archiver_result); |
| 576 InformSavePageDone(callback, result, client_id, offline_id); | 574 InformSavePageDone(callback, result, client_id, offline_id); |
| 577 DeletePendingArchiver(archiver); | 575 DeletePendingArchiver(archiver); |
| 578 return; | 576 return; |
| 579 } | 577 } |
| 580 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, | 578 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, |
| 581 file_size, start_time); | 579 file_size, start_time); |
| 582 store_->AddOrUpdateOfflinePage( | 580 store_->AddOrUpdateOfflinePage( |
| 583 offline_page_item, | 581 offline_page_item, base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, |
| 584 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, | 582 weak_ptr_factory_.GetWeakPtr(), archiver, |
| 585 weak_ptr_factory_.GetWeakPtr(), archiver, callback, | 583 callback, offline_page_item)); |
| 586 offline_page_item)); | |
| 587 } | 584 } |
| 588 | 585 |
| 589 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, | 586 void OfflinePageModelImpl::OnAddOfflinePageDone( |
| 590 const SavePageCallback& callback, | 587 OfflinePageArchiver* archiver, |
| 591 const OfflinePageItem& offline_page, | 588 const SavePageCallback& callback, |
| 592 bool success) { | 589 const OfflinePageItem& offline_page, |
| 590 bool success) { | |
| 593 SavePageResult result; | 591 SavePageResult result; |
| 594 if (success) { | 592 if (success) { |
| 595 offline_pages_[offline_page.offline_id] = offline_page; | 593 offline_pages_[offline_page.offline_id] = offline_page; |
| 596 result = SavePageResult::SUCCESS; | 594 result = SavePageResult::SUCCESS; |
| 597 UMA_HISTOGRAM_TIMES( | 595 UMA_HISTOGRAM_TIMES( |
| 598 AddHistogramSuffix( | 596 AddHistogramSuffix(offline_page.client_id, "OfflinePages.SavePageTime") |
| 599 offline_page.client_id, "OfflinePages.SavePageTime").c_str(), | 597 .c_str(), |
| 600 base::Time::Now() - offline_page.creation_time); | 598 base::Time::Now() - offline_page.creation_time); |
| 601 // 50 buckets capped between 1Kb and 10Mb. | 599 // 50 buckets capped between 1Kb and 10Mb. |
| 602 UMA_HISTOGRAM_COUNTS_10000(AddHistogramSuffix( | 600 UMA_HISTOGRAM_COUNTS_10000( |
| 603 offline_page.client_id, "OfflinePages.PageSize").c_str(), | 601 AddHistogramSuffix(offline_page.client_id, "OfflinePages.PageSize") |
|
dewittj
2016/05/26 16:38:43
Avoid reformatting unchanged code
dougarnett
2016/05/26 20:50:34
Acknowledged.
| |
| 602 .c_str(), | |
| 604 offline_page.file_size / 1024); | 603 offline_page.file_size / 1024); |
| 605 } else { | 604 } else { |
| 606 result = SavePageResult::STORE_FAILURE; | 605 result = SavePageResult::STORE_FAILURE; |
| 607 } | 606 } |
| 608 InformSavePageDone(callback, result, offline_page.client_id, | 607 InformSavePageDone(callback, result, offline_page.client_id, |
| 609 offline_page.offline_id); | 608 offline_page.offline_id); |
| 610 DeletePendingArchiver(archiver); | 609 DeletePendingArchiver(archiver); |
| 611 | 610 |
| 612 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 611 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 613 } | 612 } |
| 614 | 613 |
| 615 void OfflinePageModel::OnMarkPageAccesseDone( | 614 void OfflinePageModelImpl::OnMarkPageAccesseDone( |
| 616 const OfflinePageItem& offline_page_item, bool success) { | 615 const OfflinePageItem& offline_page_item, |
| 616 bool success) { | |
| 617 // Update the item in the cache only upon success. | 617 // Update the item in the cache only upon success. |
| 618 if (success) | 618 if (success) |
| 619 offline_pages_[offline_page_item.offline_id] = offline_page_item; | 619 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 620 | 620 |
| 621 // No need to fire OfflinePageModelChanged event since updating access info | 621 // No need to fire OfflinePageModelChanged event since updating access info |
| 622 // should not have any impact to the UI. | 622 // should not have any impact to the UI. |
| 623 } | 623 } |
| 624 | 624 |
| 625 void OfflinePageModel::OnEnsureArchivesDirCreatedDone( | 625 void OfflinePageModelImpl::OnEnsureArchivesDirCreatedDone( |
| 626 const base::TimeTicks& start_time) { | 626 const base::TimeTicks& start_time) { |
| 627 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ArchiveDirCreationTime", | 627 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ArchiveDirCreationTime", |
| 628 base::TimeTicks::Now() - start_time); | 628 base::TimeTicks::Now() - start_time); |
| 629 | 629 |
| 630 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, | 630 store_->Load(base::Bind(&OfflinePageModelImpl::OnLoadDone, |
| 631 weak_ptr_factory_.GetWeakPtr(), start_time)); | 631 weak_ptr_factory_.GetWeakPtr(), start_time)); |
| 632 } | 632 } |
| 633 | 633 |
| 634 void OfflinePageModel::OnLoadDone( | 634 void OfflinePageModelImpl::OnLoadDone( |
| 635 const base::TimeTicks& start_time, | 635 const base::TimeTicks& start_time, |
| 636 OfflinePageMetadataStore::LoadStatus load_status, | 636 OfflinePageMetadataStore::LoadStatus load_status, |
| 637 const std::vector<OfflinePageItem>& offline_pages) { | 637 const std::vector<OfflinePageItem>& offline_pages) { |
| 638 DCHECK(!is_loaded_); | 638 DCHECK(!is_loaded_); |
| 639 is_loaded_ = true; | 639 is_loaded_ = true; |
| 640 | 640 |
| 641 // TODO(jianli): rebuild the store upon failure. | 641 // TODO(jianli): rebuild the store upon failure. |
| 642 | 642 |
| 643 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) | 643 if (load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED) |
| 644 CacheLoadedData(offline_pages); | 644 CacheLoadedData(offline_pages); |
| 645 | 645 |
| 646 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ConstructionToLoadedEventTime", | 646 UMA_HISTOGRAM_TIMES("OfflinePages.Model.ConstructionToLoadedEventTime", |
| 647 base::TimeTicks::Now() - start_time); | 647 base::TimeTicks::Now() - start_time); |
| 648 | 648 |
| 649 // Create Storage Manager. | 649 // Create Storage Manager. |
| 650 storage_manager_.reset(new OfflinePageStorageManager( | 650 storage_manager_.reset(new OfflinePageStorageManager( |
| 651 this, GetPolicyController(), archive_manager_.get())); | 651 this, GetPolicyController(), archive_manager_.get())); |
| 652 | 652 |
| 653 // Run all the delayed tasks. | 653 // Run all the delayed tasks. |
| 654 for (const auto& delayed_task : delayed_tasks_) | 654 for (const auto& delayed_task : delayed_tasks_) |
| 655 delayed_task.Run(); | 655 delayed_task.Run(); |
| 656 delayed_tasks_.clear(); | 656 delayed_tasks_.clear(); |
| 657 | 657 |
| 658 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 658 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
| 659 | 659 |
| 660 CheckForExternalFileDeletion(); | 660 CheckForExternalFileDeletion(); |
| 661 } | 661 } |
| 662 | 662 |
| 663 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 663 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 664 SavePageResult result, | 664 SavePageResult result, |
| 665 const ClientId& client_id, | 665 const ClientId& client_id, |
| 666 int64_t offline_id) { | 666 int64_t offline_id) { |
| 667 UMA_HISTOGRAM_ENUMERATION( | 667 UMA_HISTOGRAM_ENUMERATION( |
| 668 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), | 668 AddHistogramSuffix(client_id, "OfflinePages.SavePageResult").c_str(), |
| 669 static_cast<int>(result), | 669 static_cast<int>(result), static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 670 static_cast<int>(SavePageResult::RESULT_COUNT)); | |
| 671 archive_manager_->GetStorageStats( | 670 archive_manager_->GetStorageStats( |
| 672 base::Bind(&ReportStorageHistogramsAfterSave)); | 671 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 673 callback.Run(result, offline_id); | 672 callback.Run(result, offline_id); |
| 674 } | 673 } |
| 675 | 674 |
| 676 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 675 void OfflinePageModelImpl::DeletePendingArchiver( |
| 677 pending_archivers_.erase(std::find( | 676 OfflinePageArchiver* archiver) { |
| 678 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 677 pending_archivers_.erase(std::find(pending_archivers_.begin(), |
| 678 pending_archivers_.end(), archiver)); | |
| 679 } | 679 } |
| 680 | 680 |
| 681 void OfflinePageModel::OnDeleteArchiveFilesDone( | 681 void OfflinePageModelImpl::OnDeleteArchiveFilesDone( |
| 682 const std::vector<int64_t>& offline_ids, | 682 const std::vector<int64_t>& offline_ids, |
| 683 const DeletePageCallback& callback, | 683 const DeletePageCallback& callback, |
| 684 bool success) { | 684 bool success) { |
| 685 if (!success) { | 685 if (!success) { |
| 686 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); | 686 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); |
| 687 return; | 687 return; |
| 688 } | 688 } |
| 689 | 689 |
| 690 store_->RemoveOfflinePages( | 690 store_->RemoveOfflinePages( |
| 691 offline_ids, | 691 offline_ids, |
| 692 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 692 base::Bind(&OfflinePageModelImpl::OnRemoveOfflinePagesDone, |
| 693 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); | 693 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 694 } | 694 } |
| 695 | 695 |
| 696 void OfflinePageModel::OnRemoveOfflinePagesDone( | 696 void OfflinePageModelImpl::OnRemoveOfflinePagesDone( |
| 697 const std::vector<int64_t>& offline_ids, | 697 const std::vector<int64_t>& offline_ids, |
| 698 const DeletePageCallback& callback, | 698 const DeletePageCallback& callback, |
| 699 bool success) { | 699 bool success) { |
| 700 // Delete the offline page from the in memory cache regardless of success in | 700 // Delete the offline page from the in memory cache regardless of success in |
| 701 // store. | 701 // store. |
| 702 base::Time now = base::Time::Now(); | 702 base::Time now = base::Time::Now(); |
| 703 int64_t total_size = 0; | 703 int64_t total_size = 0; |
| 704 for (int64_t offline_id : offline_ids) { | 704 for (int64_t offline_id : offline_ids) { |
| 705 auto iter = offline_pages_.find(offline_id); | 705 auto iter = offline_pages_.find(offline_id); |
| 706 if (iter == offline_pages_.end()) | 706 if (iter == offline_pages_.end()) |
| 707 continue; | 707 continue; |
| 708 total_size += iter->second.file_size; | 708 total_size += iter->second.file_size; |
| 709 ClientId client_id = iter->second.client_id; | 709 ClientId client_id = iter->second.client_id; |
| 710 UMA_HISTOGRAM_CUSTOM_COUNTS( | 710 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 711 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime").c_str(), | 711 AddHistogramSuffix(client_id, "OfflinePages.PageLifetime").c_str(), |
| 712 (now - iter->second.creation_time).InMinutes(), | 712 (now - iter->second.creation_time).InMinutes(), 1, |
| 713 1, | 713 base::TimeDelta::FromDays(365).InMinutes(), 100); |
| 714 base::TimeDelta::FromDays(365).InMinutes(), | |
| 715 100); | |
| 716 UMA_HISTOGRAM_CUSTOM_COUNTS( | 714 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 717 AddHistogramSuffix( | 715 AddHistogramSuffix(client_id, |
|
dewittj
2016/05/26 16:38:43
Same, this code hasn't changed, so don't reformat
dougarnett
2016/05/26 20:50:34
Acknowledged.
| |
| 718 client_id, "OfflinePages.DeletePage.TimeSinceLastOpen").c_str(), | 716 "OfflinePages.DeletePage.TimeSinceLastOpen") |
| 719 (now - iter->second.last_access_time).InMinutes(), | 717 .c_str(), |
| 720 1, | 718 (now - iter->second.last_access_time).InMinutes(), 1, |
| 721 base::TimeDelta::FromDays(365).InMinutes(), | 719 base::TimeDelta::FromDays(365).InMinutes(), 100); |
| 722 100); | |
| 723 UMA_HISTOGRAM_CUSTOM_COUNTS( | 720 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 724 AddHistogramSuffix( | 721 AddHistogramSuffix(client_id, |
| 725 client_id, "OfflinePages.DeletePage.LastOpenToCreated").c_str(), | 722 "OfflinePages.DeletePage.LastOpenToCreated") |
| 726 (iter->second.last_access_time - iter->second.creation_time). | 723 .c_str(), |
| 727 InMinutes(), | 724 (iter->second.last_access_time - iter->second.creation_time) |
| 728 1, | 725 .InMinutes(), |
| 729 base::TimeDelta::FromDays(365).InMinutes(), | 726 1, base::TimeDelta::FromDays(365).InMinutes(), 100); |
| 730 100); | |
| 731 UMA_HISTOGRAM_MEMORY_KB( | 727 UMA_HISTOGRAM_MEMORY_KB( |
| 732 AddHistogramSuffix( | 728 AddHistogramSuffix(client_id, "OfflinePages.DeletePage.PageSize") |
| 733 client_id, "OfflinePages.DeletePage.PageSize").c_str(), | 729 .c_str(), |
| 734 iter->second.file_size / 1024); | 730 iter->second.file_size / 1024); |
| 735 UMA_HISTOGRAM_COUNTS( | 731 UMA_HISTOGRAM_COUNTS( |
| 736 AddHistogramSuffix( | 732 AddHistogramSuffix(client_id, "OfflinePages.DeletePage.AccessCount") |
| 737 client_id, "OfflinePages.DeletePage.AccessCount").c_str(), | 733 .c_str(), |
| 738 iter->second.access_count); | 734 iter->second.access_count); |
| 739 FOR_EACH_OBSERVER( | 735 FOR_EACH_OBSERVER( |
| 740 Observer, observers_, | 736 Observer, observers_, |
| 741 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); | 737 OfflinePageDeleted(iter->second.offline_id, iter->second.client_id)); |
| 742 offline_pages_.erase(iter); | 738 offline_pages_.erase(iter); |
| 743 } | 739 } |
| 744 if (offline_ids.size() > 1) { | 740 if (offline_ids.size() > 1) { |
| 745 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", | 741 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", |
| 746 static_cast<int32_t>(offline_ids.size())); | 742 static_cast<int32_t>(offline_ids.size())); |
| 747 UMA_HISTOGRAM_MEMORY_KB( | 743 UMA_HISTOGRAM_MEMORY_KB("OfflinePages.BatchDelete.TotalPageSize", |
| 748 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | 744 total_size / 1024); |
| 749 } | 745 } |
| 750 // Deleting multiple pages always succeeds when it gets to this point. | 746 // Deleting multiple pages always succeeds when it gets to this point. |
| 751 InformDeletePageDone(callback, (success || offline_ids.size() > 1) | 747 InformDeletePageDone(callback, (success || offline_ids.size() > 1) |
| 752 ? DeletePageResult::SUCCESS | 748 ? DeletePageResult::SUCCESS |
| 753 : DeletePageResult::STORE_FAILURE); | 749 : DeletePageResult::STORE_FAILURE); |
| 754 } | 750 } |
| 755 | 751 |
| 756 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 752 void OfflinePageModelImpl::InformDeletePageDone( |
| 757 DeletePageResult result) { | 753 const DeletePageCallback& callback, |
| 758 UMA_HISTOGRAM_ENUMERATION( | 754 DeletePageResult result) { |
| 759 "OfflinePages.DeletePageResult", | 755 UMA_HISTOGRAM_ENUMERATION("OfflinePages.DeletePageResult", |
| 760 static_cast<int>(result), | 756 static_cast<int>(result), |
| 761 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 757 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 762 archive_manager_->GetStorageStats( | 758 archive_manager_->GetStorageStats( |
| 763 base::Bind(&ReportStorageHistogramsAfterDelete)); | 759 base::Bind(&ReportStorageHistogramsAfterDelete)); |
| 764 if (!callback.is_null()) | 760 if (!callback.is_null()) |
| 765 callback.Run(result); | 761 callback.Run(result); |
| 766 } | 762 } |
| 767 | 763 |
| 768 void OfflinePageModel::ScanForMissingArchiveFiles( | 764 void OfflinePageModelImpl::ScanForMissingArchiveFiles( |
| 769 const std::set<base::FilePath>& archive_paths) { | 765 const std::set<base::FilePath>& archive_paths) { |
| 770 std::vector<int64_t> ids_of_pages_missing_archive_file; | 766 std::vector<int64_t> ids_of_pages_missing_archive_file; |
| 771 std::vector<std::pair<int64_t, ClientId>> offline_client_id_pairs; | 767 std::vector<std::pair<int64_t, ClientId>> offline_client_id_pairs; |
| 772 for (const auto& id_page_pair : offline_pages_) { | 768 for (const auto& id_page_pair : offline_pages_) { |
| 773 if (archive_paths.count(id_page_pair.second.file_path) == 0UL) { | 769 if (archive_paths.count(id_page_pair.second.file_path) == 0UL) { |
| 774 ids_of_pages_missing_archive_file.push_back(id_page_pair.first); | 770 ids_of_pages_missing_archive_file.push_back(id_page_pair.first); |
| 775 offline_client_id_pairs.push_back( | 771 offline_client_id_pairs.push_back( |
| 776 std::make_pair(id_page_pair.first, id_page_pair.second.client_id)); | 772 std::make_pair(id_page_pair.first, id_page_pair.second.client_id)); |
| 777 } | 773 } |
| 778 } | 774 } |
| 779 | 775 |
| 780 // No offline pages missing archive files, we can bail out. | 776 // No offline pages missing archive files, we can bail out. |
| 781 if (ids_of_pages_missing_archive_file.empty()) | 777 if (ids_of_pages_missing_archive_file.empty()) |
| 782 return; | 778 return; |
| 783 | 779 |
| 784 DeletePageCallback remove_pages_done_callback( | 780 DeletePageCallback remove_pages_done_callback(base::Bind( |
| 785 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone, | 781 &OfflinePageModelImpl::OnRemoveOfflinePagesMissingArchiveFileDone, |
| 786 weak_ptr_factory_.GetWeakPtr(), offline_client_id_pairs)); | 782 weak_ptr_factory_.GetWeakPtr(), offline_client_id_pairs)); |
| 787 | 783 |
| 788 store_->RemoveOfflinePages( | 784 store_->RemoveOfflinePages( |
| 789 ids_of_pages_missing_archive_file, | 785 ids_of_pages_missing_archive_file, |
| 790 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 786 base::Bind(&OfflinePageModelImpl::OnRemoveOfflinePagesDone, |
| 791 weak_ptr_factory_.GetWeakPtr(), | 787 weak_ptr_factory_.GetWeakPtr(), |
| 792 ids_of_pages_missing_archive_file, | 788 ids_of_pages_missing_archive_file, |
| 793 remove_pages_done_callback)); | 789 remove_pages_done_callback)); |
| 794 } | 790 } |
| 795 | 791 |
| 796 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 792 void OfflinePageModelImpl::OnRemoveOfflinePagesMissingArchiveFileDone( |
| 797 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs, | 793 const std::vector<std::pair<int64_t, ClientId>>& offline_client_id_pairs, |
| 798 DeletePageResult /* result */) { | 794 DeletePageResult /* result */) { |
| 799 for (const auto& id_pair : offline_client_id_pairs) { | 795 for (const auto& id_pair : offline_client_id_pairs) { |
| 800 FOR_EACH_OBSERVER(Observer, observers_, | 796 FOR_EACH_OBSERVER(Observer, observers_, |
| 801 OfflinePageDeleted(id_pair.first, id_pair.second)); | 797 OfflinePageDeleted(id_pair.first, id_pair.second)); |
| 802 } | 798 } |
| 803 } | 799 } |
| 804 | 800 |
| 805 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | 801 void OfflinePageModelImpl::OnRemoveAllFilesDoneForClearAll( |
| 806 const base::Closure& callback, | 802 const base::Closure& callback, |
| 807 DeletePageResult result) { | 803 DeletePageResult result) { |
| 808 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | 804 store_->Reset(base::Bind(&OfflinePageModelImpl::OnResetStoreDoneForClearAll, |
| 809 weak_ptr_factory_.GetWeakPtr(), | 805 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 810 callback)); | |
| 811 } | 806 } |
| 812 | 807 |
| 813 void OfflinePageModel::OnResetStoreDoneForClearAll( | 808 void OfflinePageModelImpl::OnResetStoreDoneForClearAll( |
| 814 const base::Closure& callback, bool success) { | 809 const base::Closure& callback, |
| 810 bool success) { | |
| 815 DCHECK(success); | 811 DCHECK(success); |
| 816 if (!success) { | 812 if (!success) { |
| 817 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus2", | 813 UMA_HISTOGRAM_ENUMERATION("OfflinePages.ClearAllStatus2", |
| 818 STORE_RESET_FAILED, | 814 STORE_RESET_FAILED, CLEAR_ALL_STATUS_COUNT); |
| 819 CLEAR_ALL_STATUS_COUNT); | |
| 820 } | 815 } |
| 821 | 816 |
| 822 offline_pages_.clear(); | 817 offline_pages_.clear(); |
| 823 store_->Load(base::Bind(&OfflinePageModel::OnReloadStoreDoneForClearAll, | 818 store_->Load(base::Bind(&OfflinePageModelImpl::OnReloadStoreDoneForClearAll, |
| 824 weak_ptr_factory_.GetWeakPtr(), | 819 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 825 callback)); | |
| 826 } | 820 } |
| 827 | 821 |
| 828 void OfflinePageModel::OnReloadStoreDoneForClearAll( | 822 void OfflinePageModelImpl::OnReloadStoreDoneForClearAll( |
| 829 const base::Closure& callback, | 823 const base::Closure& callback, |
| 830 OfflinePageMetadataStore::LoadStatus load_status, | 824 OfflinePageMetadataStore::LoadStatus load_status, |
| 831 const std::vector<OfflinePageItem>& offline_pages) { | 825 const std::vector<OfflinePageItem>& offline_pages) { |
| 832 DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); | 826 DCHECK_EQ(OfflinePageMetadataStore::LOAD_SUCCEEDED, load_status); |
| 833 UMA_HISTOGRAM_ENUMERATION( | 827 UMA_HISTOGRAM_ENUMERATION( |
| 834 "OfflinePages.ClearAllStatus2", | 828 "OfflinePages.ClearAllStatus2", |
| 835 load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED ? | 829 load_status == OfflinePageMetadataStore::LOAD_SUCCEEDED |
| 836 CLEAR_ALL_SUCCEEDED : STORE_RELOAD_FAILED, | 830 ? CLEAR_ALL_SUCCEEDED |
| 831 : STORE_RELOAD_FAILED, | |
| 837 CLEAR_ALL_STATUS_COUNT); | 832 CLEAR_ALL_STATUS_COUNT); |
| 838 | 833 |
| 839 CacheLoadedData(offline_pages); | 834 CacheLoadedData(offline_pages); |
| 840 callback.Run(); | 835 callback.Run(); |
| 841 } | 836 } |
| 842 | 837 |
| 843 void OfflinePageModel::CacheLoadedData( | 838 void OfflinePageModelImpl::CacheLoadedData( |
| 844 const std::vector<OfflinePageItem>& offline_pages) { | 839 const std::vector<OfflinePageItem>& offline_pages) { |
| 845 offline_pages_.clear(); | 840 offline_pages_.clear(); |
| 846 for (const auto& offline_page : offline_pages) | 841 for (const auto& offline_page : offline_pages) |
| 847 offline_pages_[offline_page.offline_id] = offline_page; | 842 offline_pages_[offline_page.offline_id] = offline_page; |
| 848 } | 843 } |
| 849 | 844 |
| 850 int64_t OfflinePageModel::GenerateOfflineId() { | 845 void OfflinePageModelImpl::RunWhenLoaded(const base::Closure& task) { |
| 851 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; | |
| 852 } | |
| 853 | |
| 854 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { | |
| 855 if (!is_loaded_) { | 846 if (!is_loaded_) { |
| 856 delayed_tasks_.push_back(task); | 847 delayed_tasks_.push_back(task); |
| 857 return; | 848 return; |
| 858 } | 849 } |
| 859 | 850 |
| 860 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 851 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 861 } | 852 } |
| 862 | 853 |
| 863 } // namespace offline_pages | 854 } // namespace offline_pages |
| OLD | NEW |