Index: chrome/browser/profiles/profile_statistics.cc |
diff --git a/chrome/browser/profiles/profile_statistics.cc b/chrome/browser/profiles/profile_statistics.cc |
index 961e4ccf527a81cd8588a63fe0a2cb5bcbc89b69..93bb1ede32a26081e5c580e7d80adc5c74d74085 100644 |
--- a/chrome/browser/profiles/profile_statistics.cc |
+++ b/chrome/browser/profiles/profile_statistics.cc |
@@ -4,6 +4,7 @@ |
#include "chrome/browser/profiles/profile_statistics.h" |
+#include <set> |
#include "base/bind.h" |
#include "base/memory/ref_counted.h" |
#include "base/prefs/pref_service.h" |
@@ -18,11 +19,14 @@ |
#include "chrome/browser/profiles/profile_info_cache.h" |
#include "chrome/browser/profiles/profile_manager.h" |
#include "components/bookmarks/browser/bookmark_model.h" |
+#include "components/bookmarks/browser/bookmark_model_observer.h" |
#include "components/history/core/browser/history_service.h" |
#include "components/password_manager/core/browser/password_store.h" |
#include "components/password_manager/core/browser/password_store_consumer.h" |
#include "content/public/browser/browser_thread.h" |
+using bookmarks::BookmarkModel; |
Mike Lerman
2015/11/26 15:06:25
ugh; please try not to use using statements.
lwchkg
2015/12/02 15:33:34
Acknowledged.
|
+using bookmarks::BookmarkNode; |
using content::BrowserThread; |
namespace { |
@@ -32,7 +36,7 @@ struct ProfileStatValue { |
bool success; // false means the statistics failed to load |
}; |
-int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
+int CountBookmarksFromNode(const BookmarkNode* node) { |
int count = 0; |
if (node->is_url()) { |
++count; |
@@ -45,18 +49,19 @@ int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
class ProfileStatisticsAggregator |
: public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { |
- // This class collects statistical information about the profile and returns |
+ // This class is used internally by GetProfileStatistics and |
+ // StoreProfileStatisticsToCache. |
+ // |
+ // The class collects statistical information about the profile and returns |
// the information via a callback function. Currently bookmarks, history, |
// logins and preferences are counted. |
// |
// The class is RefCounted because this is needed for CancelableTaskTracker |
// to function properly. Once all tasks are run (or cancelled) the instance is |
// automatically destructed. |
- // |
- // The class is used internally by GetProfileStatistics function. |
public: |
- explicit ProfileStatisticsAggregator(Profile* profile, |
+ ProfileStatisticsAggregator(Profile* profile, |
const profiles::ProfileStatisticsCallback& callback, |
base::CancelableTaskTracker* tracker); |
@@ -78,7 +83,8 @@ class ProfileStatisticsAggregator |
void StatisticsCallbackHistory(history::HistoryCountResult result); |
// Bookmark counting. |
- ProfileStatValue CountBookmarks() const; |
+ void PrepareCountBookmarks(); |
Mike Lerman
2015/11/26 15:06:25
Perhaps call this WaitOrCountBookmarks()? The meth
lwchkg
2015/12/02 15:33:34
Good idea. Thanks!
|
+ void CountBookmarks(BookmarkModel* bookmark_model); |
// Preference counting. |
ProfileStatValue CountPrefs() const; |
@@ -91,6 +97,52 @@ class ProfileStatisticsAggregator |
const profiles::ProfileStatisticsCallback callback_; |
base::CancelableTaskTracker* tracker_; |
+ scoped_ptr<base::CancelableTaskTracker> default_tracker_; |
+ |
+ // Bookmark counting |
+ class BookmarkModelHelper |
+ : public bookmarks::BookmarkModelObserver { |
+ public: |
+ explicit BookmarkModelHelper(ProfileStatisticsAggregator* parent) |
+ : parent_(parent) {} |
+ |
+ void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) |
+ override { |
+ // Remove observer before release, otherwise it may become a dangling |
+ // reference. |
+ model->RemoveObserver(this); |
+ parent_->CountBookmarks(model); |
+ parent_->Release(); |
+ } |
+ |
+ void BookmarkNodeMoved(BookmarkModel* model, const BookmarkNode* old_parent, |
+ int old_index, const BookmarkNode* new_parent, |
+ int new_index) override {} |
+ |
+ void BookmarkNodeAdded(BookmarkModel* model, const BookmarkNode* parent, |
+ int index) override {} |
+ |
+ void BookmarkNodeRemoved(BookmarkModel* model, const BookmarkNode* parent, |
+ int old_index, const BookmarkNode* node, |
+ const std::set<GURL>& no_longer_bookmarked) override {} |
+ |
+ void BookmarkNodeChanged(BookmarkModel* model, const BookmarkNode* node) |
+ override {} |
+ |
+ void BookmarkNodeFaviconChanged(BookmarkModel* model, |
+ const BookmarkNode* node) override {} |
+ |
+ void BookmarkNodeChildrenReordered(BookmarkModel* model, |
+ const BookmarkNode* node) override {} |
+ |
+ void BookmarkAllUserNodesRemoved(BookmarkModel* model, |
+ const std::set<GURL>& removed_urls) override {} |
+ |
+ |
+ private: |
+ ProfileStatisticsAggregator* parent_ = nullptr; |
+ }; |
+ scoped_ptr<BookmarkModelHelper> bookmark_model_helper_; |
// Password counting. |
class PasswordStoreConsumerHelper |
@@ -123,17 +175,21 @@ ProfileStatisticsAggregator::ProfileStatisticsAggregator( |
callback_(callback), |
tracker_(tracker), |
password_store_consumer_helper_(this) { |
+ if (!tracker_) { |
+ default_tracker_.reset(new base::CancelableTaskTracker); |
+ tracker_ = default_tracker_.get(); |
+ } |
Init(); |
} |
void ProfileStatisticsAggregator::Init() { |
+ DCHECK(profile_); |
+ |
// Initiate bookmark counting (async). Post to UI thread. |
- tracker_->PostTaskAndReplyWithResult( |
+ tracker_->PostTask( |
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
- FROM_HERE, |
- base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this), |
- base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
- this, profiles::kProfileStatisticsBookmarks)); |
+ FROM_HERE, |
+ base::Bind(&ProfileStatisticsAggregator::PrepareCountBookmarks, this)); |
// Initiate history counting (async). |
history::HistoryService* history_service = |
@@ -164,10 +220,10 @@ void ProfileStatisticsAggregator::Init() { |
// Initiate preference counting (async). Post to UI thread. |
tracker_->PostTaskAndReplyWithResult( |
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
- FROM_HERE, |
- base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), |
- base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
- this, profiles::kProfileStatisticsSettings)); |
+ FROM_HERE, |
+ base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), |
+ base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
+ this, profiles::kProfileStatisticsSettings)); |
} |
void ProfileStatisticsAggregator::StatisticsCallback( |
@@ -177,7 +233,11 @@ void ProfileStatisticsAggregator::StatisticsCallback( |
datum.count = result.count; |
datum.success = result.success; |
profile_category_stats_.push_back(datum); |
- callback_.Run(profile_category_stats_); |
+ if (!callback_.is_null()) |
+ callback_.Run(profile_category_stats_); |
+ |
+ profiles::SetProfileStatisticsInCache(profile_->GetPath(), datum.category, |
+ result.count); |
} |
void ProfileStatisticsAggregator::StatisticsCallbackSuccess( |
@@ -205,21 +265,30 @@ void ProfileStatisticsAggregator::StatisticsCallbackHistory( |
result_converted); |
} |
-ProfileStatValue ProfileStatisticsAggregator::CountBookmarks() const { |
- bookmarks::BookmarkModel* bookmark_model = |
+void ProfileStatisticsAggregator::CountBookmarks( |
+ BookmarkModel* bookmark_model) { |
+ int count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) + |
+ CountBookmarksFromNode(bookmark_model->other_node()) + |
+ CountBookmarksFromNode(bookmark_model->mobile_node()); |
+ |
+ StatisticsCallbackSuccess(profiles::kProfileStatisticsBookmarks, count); |
+} |
+ |
+void ProfileStatisticsAggregator::PrepareCountBookmarks() { |
+ BookmarkModel* bookmark_model = |
BookmarkModelFactory::GetForProfileIfExists(profile_); |
- ProfileStatValue result; |
if (bookmark_model) { |
- result.count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) + |
- CountBookmarksFromNode(bookmark_model->other_node()) + |
- CountBookmarksFromNode(bookmark_model->mobile_node()); |
- result.success = true; |
+ if (bookmark_model->loaded()) { |
+ CountBookmarks(bookmark_model); |
+ } else { |
+ AddRef(); |
+ bookmark_model_helper_.reset(new BookmarkModelHelper(this)); |
+ bookmark_model->AddObserver(bookmark_model_helper_.get()); |
+ } |
} else { |
- result.count = 0; |
- result.success = false; |
+ StatisticsCallbackFailure(profiles::kProfileStatisticsBookmarks); |
} |
Mike Lerman
2015/11/26 15:06:25
if there's no bookmark_model, what's the appropria
lwchkg
2015/12/02 15:33:34
The statistics should fail. That's what Statistics
|
- return result; |
} |
ProfileStatValue ProfileStatisticsAggregator::CountPrefs() const { |
@@ -268,6 +337,10 @@ void GetProfileStatistics(Profile* profile, |
new ProfileStatisticsAggregator(profile, callback, tracker); |
} |
+void StoreProfileStatisticsToCache(Profile* profile) { |
+ GetProfileStatistics(profile, profiles::ProfileStatisticsCallback(), nullptr); |
+} |
+ |
ProfileCategoryStats GetProfileStatisticsFromCache( |
const base::FilePath& profile_path) { |
ProfileInfoCache& profile_info_cache = |