| OLD | NEW |
| 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/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" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "components/offline_pages/client_policy_controller.h" | 21 #include "components/offline_pages/client_policy_controller.h" |
| 22 #include "components/offline_pages/offline_page_item.h" | 22 #include "components/offline_pages/offline_page_item.h" |
| 23 #include "components/offline_pages/offline_page_storage_manager.h" | 23 #include "components/offline_pages/offline_page_storage_manager.h" |
| 24 #include "components/offline_pages/proto/offline_pages.pb.h" | 24 #include "components/offline_pages/proto/offline_pages.pb.h" |
| 25 #include "components/rappor/rappor_service.h" |
| 26 #include "components/rappor/rappor_utils.h" |
| 25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 26 | 28 |
| 27 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 29 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
| 28 | 30 |
| 29 namespace offline_pages { | 31 namespace offline_pages { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 // This enum is used in an UMA histogram. Hence the entries here shouldn't | 35 // This enum is used in an UMA histogram. Hence the entries here shouldn't |
| 34 // be deleted or re-ordered and new ones should be added to the end. | 36 // be deleted or re-ordered and new ones should be added to the end. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 base::DirectoryExists(id_path.second)) { | 99 base::DirectoryExists(id_path.second)) { |
| 98 ids_of_pages_missing_archive_file->push_back(id_path.first); | 100 ids_of_pages_missing_archive_file->push_back(id_path.first); |
| 99 } | 101 } |
| 100 } | 102 } |
| 101 } | 103 } |
| 102 | 104 |
| 103 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) { | 105 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) { |
| 104 CHECK(base::CreateDirectory(archives_dir)); | 106 CHECK(base::CreateDirectory(archives_dir)); |
| 105 } | 107 } |
| 106 | 108 |
| 109 // Record a sample for the OfflinePages.SavedPage rappor metric. |
| 110 void SampleOfflinePageURL( |
| 111 const GURL& url, |
| 112 const base::WeakPtr<rappor::RapporService>& rappor_service) { |
| 113 if (rappor_service && url.is_valid()) { |
| 114 rappor::SampleDomainAndRegistryFromGURL( |
| 115 rappor_service.get(), "OfflinePages.PageWasSavedForOffline", url); |
| 116 } |
| 117 } |
| 118 |
| 107 } // namespace | 119 } // namespace |
| 108 | 120 |
| 109 // static | 121 // static |
| 110 bool OfflinePageModel::CanSavePage(const GURL& url) { | 122 bool OfflinePageModel::CanSavePage(const GURL& url) { |
| 111 return url.SchemeIsHTTPOrHTTPS(); | 123 return url.SchemeIsHTTPOrHTTPS(); |
| 112 } | 124 } |
| 113 | 125 |
| 114 // protected | 126 // protected |
| 115 OfflinePageModel::OfflinePageModel() | 127 OfflinePageModel::OfflinePageModel() |
| 116 : is_loaded_(false), weak_ptr_factory_(this) {} | 128 : is_loaded_(false), weak_ptr_factory_(this) {} |
| 117 | 129 |
| 118 OfflinePageModel::OfflinePageModel( | 130 OfflinePageModel::OfflinePageModel( |
| 119 std::unique_ptr<OfflinePageMetadataStore> store, | 131 std::unique_ptr<OfflinePageMetadataStore> store, |
| 120 const base::FilePath& archives_dir, | 132 const base::FilePath& archives_dir, |
| 133 base::WeakPtr<rappor::RapporService> rappor_service, |
| 121 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 134 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 122 : store_(std::move(store)), | 135 : store_(std::move(store)), |
| 123 archives_dir_(archives_dir), | 136 archives_dir_(archives_dir), |
| 124 is_loaded_(false), | 137 is_loaded_(false), |
| 125 task_runner_(task_runner), | 138 task_runner_(task_runner), |
| 126 policy_controller_(new ClientPolicyController()), | 139 policy_controller_(new ClientPolicyController()), |
| 140 rappor_service_(rappor_service), |
| 127 weak_ptr_factory_(this) { | 141 weak_ptr_factory_(this) { |
| 128 task_runner_->PostTaskAndReply( | 142 task_runner_->PostTaskAndReply( |
| 129 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archives_dir_), | 143 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archives_dir_), |
| 130 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, | 144 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, |
| 131 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); | 145 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); |
| 132 } | 146 } |
| 133 | 147 |
| 134 OfflinePageModel::~OfflinePageModel() { | 148 OfflinePageModel::~OfflinePageModel() { |
| 135 } | 149 } |
| 136 | 150 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 150 | 164 |
| 151 // Skip saving the page that is not intended to be saved, like local file | 165 // Skip saving the page that is not intended to be saved, like local file |
| 152 // page. | 166 // page. |
| 153 if (!CanSavePage(url)) { | 167 if (!CanSavePage(url)) { |
| 154 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId); | 168 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId); |
| 155 return; | 169 return; |
| 156 } | 170 } |
| 157 | 171 |
| 158 DCHECK(archiver.get()); | 172 DCHECK(archiver.get()); |
| 159 | 173 |
| 174 SampleOfflinePageURL(url, rappor_service_); |
| 175 |
| 160 int64_t offline_id = GenerateOfflineId(); | 176 int64_t offline_id = GenerateOfflineId(); |
| 161 | 177 |
| 162 archiver->CreateArchive( | 178 archiver->CreateArchive( |
| 163 archives_dir_, | 179 archives_dir_, |
| 164 offline_id, | 180 offline_id, |
| 165 base::Bind(&OfflinePageModel::OnCreateArchiveDone, | 181 base::Bind(&OfflinePageModel::OnCreateArchiveDone, |
| 166 weak_ptr_factory_.GetWeakPtr(), url, offline_id, | 182 weak_ptr_factory_.GetWeakPtr(), url, offline_id, |
| 167 client_id, base::Time::Now(), callback)); | 183 client_id, base::Time::Now(), callback)); |
| 168 pending_archivers_.push_back(std::move(archiver)); | 184 pending_archivers_.push_back(std::move(archiver)); |
| 169 } | 185 } |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { | 850 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { |
| 835 if (!is_loaded_) { | 851 if (!is_loaded_) { |
| 836 delayed_tasks_.push_back(task); | 852 delayed_tasks_.push_back(task); |
| 837 return; | 853 return; |
| 838 } | 854 } |
| 839 | 855 |
| 840 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 856 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 841 } | 857 } |
| 842 | 858 |
| 843 } // namespace offline_pages | 859 } // namespace offline_pages |
| OLD | NEW |