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

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

Issue 1976163004: [Offline Pages] Adds Rappor metrics measuring what domains are saved & viewed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@no-store
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/memory/weak_ptr.h"
fgorski 2016/05/13 17:49:18 this is already in .h file.
14 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
15 #include "base/optional.h" 16 #include "base/optional.h"
16 #include "base/rand_util.h" 17 #include "base/rand_util.h"
17 #include "base/sequenced_task_runner.h" 18 #include "base/sequenced_task_runner.h"
18 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
19 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 #include "components/offline_pages/client_policy_controller.h" 22 #include "components/offline_pages/client_policy_controller.h"
22 #include "components/offline_pages/offline_page_item.h" 23 #include "components/offline_pages/offline_page_item.h"
23 #include "components/offline_pages/offline_page_storage_manager.h" 24 #include "components/offline_pages/offline_page_storage_manager.h"
24 #include "components/offline_pages/proto/offline_pages.pb.h" 25 #include "components/offline_pages/proto/offline_pages.pb.h"
26 #include "components/rappor/rappor_service.h"
27 #include "components/rappor/rappor_utils.h"
25 #include "url/gurl.h" 28 #include "url/gurl.h"
26 29
27 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; 30 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult;
28 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; 31 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
29 32
30 namespace offline_pages { 33 namespace offline_pages {
31 34
32 namespace { 35 namespace {
33 36
34 // This enum is used in an UMA histogram. Hence the entries here shouldn't 37 // This enum is used in an UMA histogram. Hence the entries here shouldn't
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 base::DirectoryExists(id_path.second)) { 101 base::DirectoryExists(id_path.second)) {
99 ids_of_pages_missing_archive_file->push_back(id_path.first); 102 ids_of_pages_missing_archive_file->push_back(id_path.first);
100 } 103 }
101 } 104 }
102 } 105 }
103 106
104 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) { 107 void EnsureArchivesDirCreated(const base::FilePath& archives_dir) {
105 CHECK(base::CreateDirectory(archives_dir)); 108 CHECK(base::CreateDirectory(archives_dir));
106 } 109 }
107 110
111 // Record a sample for the OfflinePages.SavedPage rappor metric.
112 void SampleOfflinePageURL(
113 const GURL& url,
114 const base::WeakPtr<rappor::RapporService>& rappor_service) {
115 if (rappor_service && url.is_valid()) {
116 rappor::SampleDomainAndRegistryFromGURL(rappor_service.get(),
117 "OfflinePages.SavedPage", url);
118 }
119 }
120
108 } // namespace 121 } // namespace
109 122
110 // static 123 // static
111 bool OfflinePageModel::CanSavePage(const GURL& url) { 124 bool OfflinePageModel::CanSavePage(const GURL& url) {
112 return url.SchemeIsHTTPOrHTTPS(); 125 return url.SchemeIsHTTPOrHTTPS();
113 } 126 }
114 127
115 // protected 128 // protected
116 OfflinePageModel::OfflinePageModel() 129 OfflinePageModel::OfflinePageModel()
117 : is_loaded_(false), weak_ptr_factory_(this) {} 130 : is_loaded_(false), weak_ptr_factory_(this) {}
118 131
119 OfflinePageModel::OfflinePageModel( 132 OfflinePageModel::OfflinePageModel(
120 std::unique_ptr<OfflinePageMetadataStore> store, 133 std::unique_ptr<OfflinePageMetadataStore> store,
121 const base::FilePath& archives_dir, 134 const base::FilePath& archives_dir,
135 base::WeakPtr<rappor::RapporService> rappor_service,
122 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 136 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
123 : store_(std::move(store)), 137 : store_(std::move(store)),
124 archives_dir_(archives_dir), 138 archives_dir_(archives_dir),
125 is_loaded_(false), 139 is_loaded_(false),
126 task_runner_(task_runner), 140 task_runner_(task_runner),
127 policy_controller_(new ClientPolicyController()), 141 policy_controller_(new ClientPolicyController()),
128 storage_manager_(new OfflinePageStorageManager(this)), 142 storage_manager_(new OfflinePageStorageManager(this)),
143 rappor_service_(rappor_service),
129 weak_ptr_factory_(this) { 144 weak_ptr_factory_(this) {
130 task_runner_->PostTaskAndReply( 145 task_runner_->PostTaskAndReply(
131 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archives_dir_), 146 FROM_HERE, base::Bind(EnsureArchivesDirCreated, archives_dir_),
132 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone, 147 base::Bind(&OfflinePageModel::OnEnsureArchivesDirCreatedDone,
133 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now())); 148 weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));
134 } 149 }
135 150
136 OfflinePageModel::~OfflinePageModel() { 151 OfflinePageModel::~OfflinePageModel() {
137 } 152 }
138 153
(...skipping 13 matching lines...) Expand all
152 167
153 // Skip saving the page that is not intended to be saved, like local file 168 // Skip saving the page that is not intended to be saved, like local file
154 // page. 169 // page.
155 if (!CanSavePage(url)) { 170 if (!CanSavePage(url)) {
156 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId); 171 InformSavePageDone(callback, SavePageResult::SKIPPED, kInvalidOfflineId);
157 return; 172 return;
158 } 173 }
159 174
160 DCHECK(archiver.get()); 175 DCHECK(archiver.get());
161 176
177 SampleOfflinePageURL(url, rappor_service_);
178
162 int64_t offline_id = GenerateOfflineId(); 179 int64_t offline_id = GenerateOfflineId();
163 180
164 archiver->CreateArchive( 181 archiver->CreateArchive(
165 archives_dir_, 182 archives_dir_,
166 offline_id, 183 offline_id,
167 base::Bind(&OfflinePageModel::OnCreateArchiveDone, 184 base::Bind(&OfflinePageModel::OnCreateArchiveDone,
168 weak_ptr_factory_.GetWeakPtr(), url, offline_id, 185 weak_ptr_factory_.GetWeakPtr(), url, offline_id,
169 client_id, base::Time::Now(), callback)); 186 client_id, base::Time::Now(), callback));
170 pending_archivers_.push_back(std::move(archiver)); 187 pending_archivers_.push_back(std::move(archiver));
171 } 188 }
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) { 826 void OfflinePageModel::RunWhenLoaded(const base::Closure& task) {
810 if (!is_loaded_) { 827 if (!is_loaded_) {
811 delayed_tasks_.push_back(task); 828 delayed_tasks_.push_back(task);
812 return; 829 return;
813 } 830 }
814 831
815 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 832 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
816 } 833 }
817 834
818 } // namespace offline_pages 835 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698