Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/profiles/profile_statistics_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/prefs/pref_service.h" | |
| 9 #include "base/task_runner.h" | |
| 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
| 11 #include "chrome/browser/history/history_service_factory.h" | |
| 12 #include "chrome/browser/password_manager/password_store_factory.h" | |
| 13 #include "components/history/core/browser/history_service.h" | |
| 14 #include "components/password_manager/core/browser/password_store.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 using content::BrowserThread; | |
| 18 | |
| 19 namespace profiles { | |
| 20 | |
| 21 ProfileStatisticsService::ProfileStatisticsService | |
| 22 (Profile* profile, ProfileStatisticsCallback callback) : | |
| 23 profile_(profile), callback_(callback), weak_factory_(this) { | |
| 24 init_(); | |
| 25 } | |
| 26 | |
| 27 void ProfileStatisticsService::init_() { | |
| 28 // Initiate bookmark counting (async). | |
| 29 bookmarks::BookmarkModel* bookmark_model = | |
| 30 BookmarkModelFactory::GetForProfileIfExists(profile_); | |
| 31 | |
| 32 if (bookmark_model) { | |
| 33 // This task does not use a database, but | |
| 34 // DB thread is used because most other operations in this class use it. | |
| 35 tracker_.PostTaskAndReplyWithResult( | |
| 36 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), | |
|
Mike Lerman
2015/07/22 01:39:26
nit: indent 4 spaces whenever you line wrap.
lwchkg
2015/07/26 17:38:22
Acknowledged.
| |
| 37 FROM_HERE, | |
| 38 base::Bind(&ProfileStatisticsService::CountURLs_, | |
| 39 base::Unretained(bookmark_model)), | |
| 40 base::Bind(&ProfileStatisticsService::StatsticsCallback_, | |
| 41 weak_factory_.GetWeakPtr(), | |
| 42 "Bookmarks")); | |
| 43 } else { | |
| 44 StatsticsCallback_("Bookmarks", 0); | |
| 45 } | |
| 46 | |
| 47 // Initiate history counting (async). | |
| 48 history::HistoryService* history_service = | |
| 49 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | |
| 50 | |
| 51 if (history_service) { | |
| 52 history_service->CountURLs( | |
| 53 base::Bind(&ProfileStatisticsService::StatsticsCallback_, | |
| 54 weak_factory_.GetWeakPtr(), | |
| 55 "BrowsingHistory"), | |
| 56 &tracker_); | |
| 57 } else { | |
| 58 StatsticsCallback_("BrowsingHistory", 0); | |
| 59 } | |
| 60 | |
| 61 // Initiate stored password counting (async). | |
| 62 scoped_refptr<password_manager::PasswordStore> password_store = | |
| 63 PasswordStoreFactory::GetForProfile( | |
| 64 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
| 65 if (password_store) { | |
| 66 // Currently this task is not cancellable. | |
| 67 password_store->GetCountOfLogins( | |
| 68 base::Bind(&ProfileStatisticsService::StatsticsCallback_, | |
| 69 weak_factory_.GetWeakPtr(), | |
| 70 "Passwords")); | |
| 71 } | |
| 72 | |
| 73 // Initiate preference counting (async). Post to UI thread. | |
| 74 tracker_.PostTaskAndReplyWithResult( | |
| 75 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | |
| 76 FROM_HERE, | |
| 77 base::Bind(&ProfileStatisticsService::CountPrefs_, | |
| 78 base::Unretained(this)), | |
| 79 base::Bind(&ProfileStatisticsService::StatsticsCallback_, | |
| 80 weak_factory_.GetWeakPtr(), | |
| 81 "Settings")); | |
| 82 } | |
| 83 | |
| 84 void ProfileStatisticsService::StatsticsCallback_(std::string category, | |
| 85 int count) { | |
| 86 profile_stats_values_.push_back(std::make_pair(category, count)); | |
| 87 callback_.Run(profile_stats_values_); | |
| 88 } | |
| 89 | |
| 90 int ProfileStatisticsService::CountURLsFromNode_( | |
| 91 const bookmarks::BookmarkNode* node) { | |
| 92 int count = 0; | |
| 93 if (node->is_url()) { | |
| 94 ++count; | |
| 95 } else { | |
| 96 for (int i = 0; i < node->child_count(); ++i) | |
| 97 count += CountURLsFromNode_(node->GetChild(i)); | |
| 98 } | |
| 99 return count; | |
| 100 } | |
| 101 | |
| 102 int ProfileStatisticsService::CountURLs_( | |
| 103 const bookmarks::BookmarkModel* bookmark_model) { | |
| 104 if (bookmark_model) | |
|
Mike Lerman
2015/07/22 01:39:26
nit: use parenthesis anytime the statement block i
lwchkg
2015/07/26 17:38:22
Acknowledged.
| |
| 105 return CountURLsFromNode_(bookmark_model->bookmark_bar_node()) + | |
| 106 CountURLsFromNode_(bookmark_model->other_node()) + | |
| 107 CountURLsFromNode_(bookmark_model->mobile_node()); | |
| 108 else | |
| 109 return 0; | |
| 110 } | |
| 111 | |
| 112 int ProfileStatisticsService::CountPrefs_() const { | |
| 113 const PrefService* pref_service = profile_->GetPrefs(); | |
| 114 | |
| 115 if (!pref_service) | |
| 116 return 0; | |
| 117 | |
| 118 scoped_ptr<base::DictionaryValue> prefs = | |
| 119 pref_service->GetPreferenceValuesWithoutPathExpansion(); | |
| 120 | |
| 121 int count = 0; | |
| 122 for (base::DictionaryValue::Iterator it(*(prefs.get())); | |
| 123 !it.IsAtEnd(); it.Advance()) { | |
| 124 const PrefService::Preference* pref = pref_service-> | |
| 125 FindPreference(it.key()); | |
|
Mike Lerman
2015/07/22 01:39:26
indent only 4 spaces.
lwchkg
2015/07/26 17:38:22
Should I have "F" directly under "_"?
| |
| 126 // Skip all dictionaries (which must be empty by the function call above). | |
| 127 if (it.value().GetType() != base::Value::TYPE_DICTIONARY && | |
| 128 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) { | |
| 129 ++count; | |
| 130 } | |
| 131 } | |
| 132 return count; | |
| 133 } | |
| 134 | |
| 135 } // namespace profiles | |
| OLD | NEW |