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

Side by Side Diff: components/offline_pages/offline_page_model.cc

Issue 1928393003: Using separate directories based on LifetimeType. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
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/rand_util.h" 15 #include "base/rand_util.h"
16 #include "base/sequenced_task_runner.h" 16 #include "base/sequenced_task_runner.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/thread_task_runner_handle.h" 18 #include "base/thread_task_runner_handle.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "components/offline_pages/client_policy_controller.h"
20 #include "components/offline_pages/offline_page_item.h" 21 #include "components/offline_pages/offline_page_item.h"
21 #include "components/offline_pages/proto/offline_pages.pb.h" 22 #include "components/offline_pages/proto/offline_pages.pb.h"
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 24
24 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; 25 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult;
25 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; 26 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
27 using ArchiveDirs = offline_pages::OfflinePageModel::ArchiveDirs;
26 28
27 namespace offline_pages { 29 namespace offline_pages {
28 30
29 namespace { 31 namespace {
30 32
31 // This enum is used in an UMA histogram. Hence the entries here shouldn't 33 // This enum is used in an UMA histogram. Hence the entries here shouldn't
32 // 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.
33 enum ClearAllStatus { 35 enum ClearAllStatus {
34 CLEAR_ALL_SUCCEEDED, 36 CLEAR_ALL_SUCCEEDED,
35 STORE_RESET_FAILED, 37 STORE_RESET_FAILED,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 DCHECK(ids_of_pages_missing_archive_file); 90 DCHECK(ids_of_pages_missing_archive_file);
89 91
90 for (const auto& id_path : id_path_pairs) { 92 for (const auto& id_path : id_path_pairs) {
91 if (!base::PathExists(id_path.second) || 93 if (!base::PathExists(id_path.second) ||
92 base::DirectoryExists(id_path.second)) { 94 base::DirectoryExists(id_path.second)) {
93 ids_of_pages_missing_archive_file->push_back(id_path.first); 95 ids_of_pages_missing_archive_file->push_back(id_path.first);
94 } 96 }
95 } 97 }
96 } 98 }
97 99
98 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) { 100 void EnsureArchivesDirCreated(const ArchiveDirs& archive_dirs) {
99 CHECK(base::CreateDirectory(archives_dir)); 101 for (const auto& iter : archive_dirs) {
102 CHECK(base::CreateDirectory(iter.second));
103 }
100 } 104 }
101 105
102 } // namespace 106 } // namespace
103 107
104 // static 108 // static
105 bool OfflinePageModel::CanSavePage(const GURL& url) { 109 bool OfflinePageModel::CanSavePage(const GURL& url) {
106 return url.SchemeIsHTTPOrHTTPS(); 110 return url.SchemeIsHTTPOrHTTPS();
107 } 111 }
108 112
109 OfflinePageModel::OfflinePageModel( 113 OfflinePageModel::OfflinePageModel(
110 std::unique_ptr<OfflinePageMetadataStore> store, 114 std::unique_ptr<OfflinePageMetadataStore> store,
111 const base::FilePath& archives_dir, 115 const ArchiveDirs& archive_dirs,
112 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 116 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
113 : store_(std::move(store)), 117 : store_(std::move(store)),
114 archives_dir_(archives_dir), 118 archive_dirs_(archive_dirs),
115 is_loaded_(false), 119 is_loaded_(false),
116 task_runner_(task_runner), 120 task_runner_(task_runner),
117 weak_ptr_factory_(this) { 121 weak_ptr_factory_(this) {
118 task_runner_->PostTaskAndReply( 122 task_runner_->PostTaskAndReply(
119 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archives_dir_), 123 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archive_dirs_),
120 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, 124 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone,
121 weak_ptr_factory_.GetWeakPtr())); 125 weak_ptr_factory_.GetWeakPtr()));
122 } 126 }
123 127
124 OfflinePageModel::~OfflinePageModel() { 128 OfflinePageModel::~OfflinePageModel() {
125 } 129 }
126 130
127 void OfflinePageModel::AddObserver(Observer* observer) { 131 void OfflinePageModel::AddObserver(Observer* observer) {
128 observers_.AddObserver(observer); 132 observers_.AddObserver(observer);
129 } 133 }
(...skipping 13 matching lines...) Expand all
143 if (!CanSavePage(url)) { 147 if (!CanSavePage(url)) {
144 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId); 148 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId);
145 return; 149 return;
146 } 150 }
147 151
148 DCHECK(archiver.get()); 152 DCHECK(archiver.get());
149 153
150 int64_t offline_id = GenerateOfflineId(); 154 int64_t offline_id = GenerateOfflineId();
151 155
152 archiver->CreateArchive( 156 archiver->CreateArchive(
153 archives_dir_, 157 GetArchivePath(client_id),
154 offline_id, 158 offline_id,
155 base::Bind(&OfflinePageModel::OnCreateArchiveDone, 159 base::Bind(&OfflinePageModel::OnCreateArchiveDone,
156 weak_ptr_factory_.GetWeakPtr(), url, offline_id, 160 weak_ptr_factory_.GetWeakPtr(), url, offline_id,
157 client_id, base::Time::Now(), callback)); 161 client_id, base::Time::Now(), callback));
158 pending_archivers_.push_back(std::move(archiver)); 162 pending_archivers_.push_back(std::move(archiver));
159 } 163 }
160 164
161 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) { 165 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) {
162 DCHECK(is_loaded_); 166 DCHECK(is_loaded_);
163 auto iter = offline_pages_.find(offline_id); 167 auto iter = offline_pages_.find(offline_id);
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 const std::vector<OfflinePageItem>& offline_pages) { 712 const std::vector<OfflinePageItem>& offline_pages) {
709 offline_pages_.clear(); 713 offline_pages_.clear();
710 for (const auto& offline_page : offline_pages) 714 for (const auto& offline_page : offline_pages)
711 offline_pages_[offline_page.offline_id] = offline_page; 715 offline_pages_[offline_page.offline_id] = offline_page;
712 } 716 }
713 717
714 int64_t OfflinePageModel::GenerateOfflineId() { 718 int64_t OfflinePageModel::GenerateOfflineId() {
715 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; 719 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1;
716 } 720 }
717 721
722 const base::FilePath& OfflinePageModel::GetArchivePath(
723 const ClientId& client_id) {
724 OfflinePageClientPolicy policy =
725 ClientPolicyController::GetInstance().GetPolicy(client_id.name_space);
726 return archive_dirs_.at(policy.lifetime_policy.type);
727 }
728
718 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { 729 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) {
719 if (!is_loaded_) { 730 if (!is_loaded_) {
720 delayed_tasks_.push_back(task); 731 delayed_tasks_.push_back(task);
721 return; 732 return;
722 } 733 }
723 734
724 task_runner_->PostTask(FROM_HERE, task); 735 task_runner_->PostTask(FROM_HERE, task);
725 } 736 }
726 737
727 } // namespace offline_pages 738 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.h ('k') | components/offline_pages/offline_page_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698