summaryrefslogtreecommitdiff
path: root/chromium/components/password_manager/core/browser/statistics_table.h
blob: d53c849106e98b951236d1c6ff542f22cc1f8c8f (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
// 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.

#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_STATISTICS_TABLE_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_STATISTICS_TABLE_H_

#include <memory>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/time/time.h"
#include "url/gurl.h"

namespace sql {
class Connection;
}

namespace password_manager {

// The statistics containing user interactions with a site.
struct InteractionsStats {
  InteractionsStats();

  // The domain of the site.
  GURL origin_domain;

  // The value of the username.
  base::string16 username_value;

  // Number of times the user dismissed the bubble.
  int dismissal_count = 0;

  // The date when the row was updated.
  base::Time update_time;
};

bool operator==(const InteractionsStats& lhs, const InteractionsStats& rhs);

// Returns an element from |stats| with |username| or nullptr if not found.
InteractionsStats* FindStatsByUsername(
    const std::vector<std::unique_ptr<InteractionsStats>>& stats,
    const base::string16& username);

// Represents the 'stats' table in the Login Database.
class StatisticsTable {
 public:
  StatisticsTable();
  ~StatisticsTable();

  // Initializes |db_|.
  void Init(sql::Connection* db);

  // Creates the statistics table if it doesn't exist.
  bool CreateTableIfNecessary();

  // Migrates this table to |version|. The current version should be less than
  // |version|. Returns false if there was migration work to do and it failed,
  // true otherwise.
  bool MigrateToVersion(int version);

  // Adds or replaces the statistics about |stats.origin_domain| and
  // |stats.username_value|.
  bool AddRow(const InteractionsStats& stats);

  // Removes the statistics for |domain|. Returns true if the SQL completed
  // successfully.
  bool RemoveRow(const GURL& domain);

  // Returns the statistics for |domain| if it exists.
  std::vector<std::unique_ptr<InteractionsStats>> GetRows(const GURL& domain);

  // Removes the statistics between the dates. Returns true if the SQL completed
  // successfully.
  bool RemoveStatsBetween(base::Time delete_begin, base::Time delete_end);

 private:
  sql::Connection* db_;

  DISALLOW_COPY_AND_ASSIGN(StatisticsTable);
};

}  // namespace password_manager

#endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_STATISTICS_TABLE_H_