1 // Copyright 2014 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.
5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_DATABASE_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_DATABASE_H_
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "components/password_manager/core/browser/affiliation_utils.h"
22 namespace password_manager
{
24 // Stores equivalence classes of facets, i.e., facets that are affiliated with
25 // each other, in an SQLite database. See affiliation_utils.h for a more
26 // detailed definition of what this means.
28 // Under the assumption that there is most likely not much the caller can do in
29 // case of database errors, most methods silently ignore them. Nevertheless, the
30 // caller must plan ahead for this rare but non-negligible scenario, and expect
31 // that in odd cases basic database invariants will not hold.
32 class AffiliationDatabase
{
34 AffiliationDatabase();
35 ~AffiliationDatabase();
37 // Opens an existing database at |path|, or creates a new one if none exists,
38 // and returns true on success.
39 bool Init(const base::FilePath
& path
);
41 // Looks up the equivalence class containing |facet_uri|, and returns true if
42 // such a class is found, in which case it is also stored into |result|.
43 bool GetAffiliationsForFacet(const FacetURI
& facet_uri
,
44 AffiliatedFacetsWithUpdateTime
* result
) const;
46 // Retrieves all stored equivalence classes.
47 void GetAllAffiliations(
48 std::vector
<AffiliatedFacetsWithUpdateTime
>* results
) const;
50 // Removes the stored equivalence class, if any, containing |facet_uri|.
51 void DeleteAffiliationsForFacet(const FacetURI
& facet_uri
);
53 // Removes stored equivalence classes that were last updated before the
54 // |cutoff_threshold|.
55 void DeleteAffiliationsOlderThan(const base::Time
& cutoff_threshold
);
57 // Removes all records from all tables of the database.
58 void DeleteAllAffiliations();
60 // Stores the equivalence class defined by |affiliated_facets| to the DB and
61 // returns true unless it has a non-empty subset with a preexisting class, in
62 // which case no changes are made and the function returns false.
63 bool Store(const AffiliatedFacetsWithUpdateTime
& affiliated_facets
);
65 // Stores the equivalence class defined by |affiliated_facets| to the DB,
66 // database, and removes any other equivalence classes that are in conflict
67 // with |affiliated_facets|, i.e. those that are neither equal nor disjoint to
68 // it. Removed equivalence classes are stored into |removed_affiliations|.
69 void StoreAndRemoveConflicting(
70 const AffiliatedFacetsWithUpdateTime
& affiliated_facets
,
71 std::vector
<AffiliatedFacetsWithUpdateTime
>* removed_affiliations
);
73 // Deletes the database file at |path| along with all its auxiliary files. The
74 // database must be closed before calling this.
75 static void Delete(const base::FilePath
& path
);
78 // Creates any tables and indices that do not already exist in the database.
79 bool CreateTablesAndIndicesIfNeeded();
81 // Called when SQLite encounters an error.
82 void SQLErrorCallback(int error_number
, sql::Statement
* statement
);
84 // The SQL connection to the database.
85 scoped_ptr
<sql::Connection
> sql_connection_
;
87 DISALLOW_COPY_AND_ASSIGN(AffiliationDatabase
);
90 } // namespace password_manager
92 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_DATABASE_H_