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 "chrome/browser/profiles/profile_statistics.h" | 5 #include "chrome/browser/profiles/profile_statistics.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
14 #include "chrome/browser/password_manager/password_store_factory.h" | 15 #include "chrome/browser/password_manager/password_store_factory.h" |
| 16 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 17 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 18 #include "chrome/browser/profiles/profile_info_cache.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
16 #include "components/history/core/browser/history_service.h" | 21 #include "components/history/core/browser/history_service.h" |
17 #include "components/password_manager/core/browser/password_store.h" | 22 #include "components/password_manager/core/browser/password_store.h" |
18 #include "components/password_manager/core/browser/password_store_consumer.h" | 23 #include "components/password_manager/core/browser/password_store_consumer.h" |
19 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
20 | 25 |
21 using content::BrowserThread; | 26 using content::BrowserThread; |
22 | 27 |
23 namespace { | 28 namespace { |
24 | 29 |
25 struct ProfileStatValue { | 30 struct ProfileStatValue { |
26 int count; | 31 int count; |
27 bool success; // false means the statistics failed to load | 32 bool success; // false means the statistics failed to load |
28 }; | 33 }; |
29 | 34 |
30 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { | 35 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
31 int count = 0; | 36 int count = 0; |
32 if (node->is_url()) { | 37 if (node->is_url()) { |
33 ++count; | 38 ++count; |
34 } else { | 39 } else { |
35 for (int i = 0; i < node->child_count(); ++i) | 40 for (int i = 0; i < node->child_count(); ++i) |
36 count += CountBookmarksFromNode(node->GetChild(i)); | 41 count += CountBookmarksFromNode(node->GetChild(i)); |
37 } | 42 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 const char kProfileStatisticsBookmarks[] = "Bookmarks"; | 261 const char kProfileStatisticsBookmarks[] = "Bookmarks"; |
257 const char kProfileStatisticsSettings[] = "Settings"; | 262 const char kProfileStatisticsSettings[] = "Settings"; |
258 | 263 |
259 void GetProfileStatistics(Profile* profile, | 264 void GetProfileStatistics(Profile* profile, |
260 const ProfileStatisticsCallback& callback, | 265 const ProfileStatisticsCallback& callback, |
261 base::CancelableTaskTracker* tracker) { | 266 base::CancelableTaskTracker* tracker) { |
262 scoped_refptr<ProfileStatisticsAggregator> aggregator = | 267 scoped_refptr<ProfileStatisticsAggregator> aggregator = |
263 new ProfileStatisticsAggregator(profile, callback, tracker); | 268 new ProfileStatisticsAggregator(profile, callback, tracker); |
264 } | 269 } |
265 | 270 |
| 271 ProfileCategoryStats GetProfileStatisticsFromCache( |
| 272 const base::FilePath& profile_path) { |
| 273 ProfileInfoCache& profile_info_cache = |
| 274 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 275 ProfileAttributesEntry* entry = nullptr; |
| 276 bool has_entry = profile_info_cache. |
| 277 GetProfileAttributesWithPath(profile_path, &entry); |
| 278 |
| 279 ProfileCategoryStats stats; |
| 280 ProfileCategoryStat stat; |
| 281 |
| 282 stat.category = kProfileStatisticsBrowsingHistory; |
| 283 stat.success = has_entry ? entry->HasStatsBrowsingHistory() : false; |
| 284 stat.count = stat.success ? entry->GetStatsBrowsingHistory() : 0; |
| 285 stats.push_back(stat); |
| 286 |
| 287 stat.category = kProfileStatisticsPasswords; |
| 288 stat.success = has_entry ? entry->HasStatsPasswords() : false; |
| 289 stat.count = stat.success ? entry->GetStatsPasswords() : 0; |
| 290 stats.push_back(stat); |
| 291 |
| 292 stat.category = kProfileStatisticsBookmarks; |
| 293 stat.success = has_entry ? entry->HasStatsBookmarks() : false; |
| 294 stat.count = stat.success ? entry->GetStatsBookmarks() : 0; |
| 295 stats.push_back(stat); |
| 296 |
| 297 stat.category = kProfileStatisticsSettings; |
| 298 stat.success = has_entry ? entry->HasStatsSettings() : false; |
| 299 stat.count = stat.success ? entry->GetStatsSettings() : 0; |
| 300 stats.push_back(stat); |
| 301 |
| 302 return stats; |
| 303 } |
| 304 |
| 305 void SetProfileStatisticsInCache(const base::FilePath& profile_path, |
| 306 const std::string& category, int count) { |
| 307 // If local_state() is null, profile_manager() will seg-fault. |
| 308 if (!g_browser_process || !g_browser_process->local_state()) |
| 309 return; |
| 310 |
| 311 ProfileInfoCache& profile_info_cache = |
| 312 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 313 ProfileAttributesEntry* entry = nullptr; |
| 314 if (!profile_info_cache.GetProfileAttributesWithPath(profile_path, &entry)) |
| 315 return; |
| 316 |
| 317 if (category == kProfileStatisticsBrowsingHistory) { |
| 318 entry->SetStatsBrowsingHistory(count); |
| 319 } else if (category == kProfileStatisticsPasswords) { |
| 320 entry->SetStatsPasswords(count); |
| 321 } else if (category == kProfileStatisticsBookmarks) { |
| 322 entry->SetStatsBookmarks(count); |
| 323 } else if (category == kProfileStatisticsSettings) { |
| 324 entry->SetStatsSettings(count); |
| 325 } else { |
| 326 NOTREACHED(); |
| 327 } |
| 328 } |
| 329 |
266 } // namespace profiles | 330 } // namespace profiles |
OLD | NEW |