summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profiles/profile_statistics.cc
blob: 8cfa7e72cc44215d11b093dc1b6603b3ada4ed66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/profiles/profile_statistics.h"

#include "base/bind.h"
#include "base/macros.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_statistics_aggregator.h"

ProfileStatistics::ProfileStatistics(Profile* profile)
    : profile_(profile), aggregator_(nullptr), weak_ptr_factory_(this) {
}

ProfileStatistics::~ProfileStatistics() {
}

void ProfileStatistics::GatherStatistics(
    const profiles::ProfileStatisticsCallback& callback) {
  // IsValidProfile() can be false in unit tests.
  if (!g_browser_process->profile_manager()->IsValidProfile(profile_))
    return;
  DCHECK(!profile_->IsOffTheRecord() && !profile_->IsSystemProfile());

  if (HasAggregator()) {
    GetAggregator()->AddCallbackAndStartAggregator(callback);
  } else {
    // The statistics task may outlive ProfileStatistics in unit tests, so a
    // weak pointer is used for the callback.
    scoped_refptr<ProfileStatisticsAggregator> aggregator =
        new ProfileStatisticsAggregator(
                profile_,
                callback,
                base::Bind(&ProfileStatistics::DeregisterAggregator,
                           weak_ptr_factory_.GetWeakPtr()));
    RegisterAggregator(aggregator.get());
  }
}

bool ProfileStatistics::HasAggregator() const {
  return aggregator_ != nullptr;
}

ProfileStatisticsAggregator* ProfileStatistics::GetAggregator() const {
  return aggregator_;
}

void ProfileStatistics::RegisterAggregator(
    ProfileStatisticsAggregator* aggregator) {
  aggregator_ = aggregator;
}

void ProfileStatistics::DeregisterAggregator() {
  aggregator_ = nullptr;
}

// static
profiles::ProfileCategoryStats
    ProfileStatistics::GetProfileStatisticsFromAttributesStorage(
        const base::FilePath& profile_path) {
  ProfileAttributesEntry* entry = nullptr;
  bool has_entry = g_browser_process->profile_manager()->
      GetProfileAttributesStorage().
      GetProfileAttributesWithPath(profile_path, &entry);

  profiles::ProfileCategoryStats stats;
  profiles::ProfileCategoryStat stat;

  stat.category = profiles::kProfileStatisticsBrowsingHistory;
  stat.success = has_entry ? entry->HasStatsBrowsingHistory() : false;
  stat.count = stat.success ? entry->GetStatsBrowsingHistory() : 0;
  stats.push_back(stat);

  stat.category = profiles::kProfileStatisticsPasswords;
  stat.success = has_entry ? entry->HasStatsPasswords() : false;
  stat.count = stat.success ? entry->GetStatsPasswords() : 0;
  stats.push_back(stat);

  stat.category = profiles::kProfileStatisticsBookmarks;
  stat.success = has_entry ? entry->HasStatsBookmarks() : false;
  stat.count = stat.success ? entry->GetStatsBookmarks() : 0;
  stats.push_back(stat);

  stat.category = profiles::kProfileStatisticsSettings;
  stat.success = has_entry ? entry->HasStatsSettings() : false;
  stat.count = stat.success ? entry->GetStatsSettings() : 0;
  stats.push_back(stat);

  return stats;
}

// static
void ProfileStatistics::SetProfileStatisticsToAttributesStorage(
    const base::FilePath& profile_path,
    const std::string& category,
    int count) {
  // |profile_manager()| may return a null pointer.
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  if (!profile_manager)
    return;

  ProfileAttributesEntry* entry = nullptr;
  if (!profile_manager->GetProfileAttributesStorage().
          GetProfileAttributesWithPath(profile_path, &entry)) {
    // It is possible to have the profile attributes entry absent, e.g. the
    // profile is scheduled for deletion during the async statistics task.
    return;
  }

  if (category == profiles::kProfileStatisticsBrowsingHistory) {
    entry->SetStatsBrowsingHistory(count);
  } else if (category == profiles::kProfileStatisticsPasswords) {
    entry->SetStatsPasswords(count);
  } else if (category == profiles::kProfileStatisticsBookmarks) {
    entry->SetStatsBookmarks(count);
  } else if (category == profiles::kProfileStatisticsSettings) {
    entry->SetStatsSettings(count);
  } else {
    NOTREACHED();
  }
}