OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_DATABASE_H_ | |
6 #define CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_DATABASE_H_ | |
7 #pragma once | |
8 | |
9 #include <ostream> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/file_path.h" | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/string16.h" | |
17 #include "base/synchronization/cancellation_flag.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "sql/connection.h" | |
20 | |
21 class Profile; | |
22 | |
23 // This manages the network action predictor table within the SQLite database | |
24 // passed in to the constructor. It expects the following scheme: | |
25 // | |
26 // network_action_predictor | |
27 // id A unique id. | |
28 // user_text What the user typed. | |
29 // url The URL of the entry. | |
30 // number_of_hits Number of times the entry was shown to the user and | |
31 // selected. | |
32 // number_of_misses Number of times the entry was shown to the user but not | |
33 // selected. | |
34 // | |
35 // Ref-counted as it is created and destroyed on a different thread to the DB | |
36 // thread that is required for all methods performing database access. | |
37 class NetworkActionPredictorDatabase | |
38 : public base::RefCountedThreadSafe<NetworkActionPredictorDatabase> { | |
39 public: | |
40 struct Row { | |
41 // TODO(dominich): Make this 64-bit integer as an optimization. This | |
42 // requires some investigation into how to make sure the id is unique for | |
43 // each user_text/url pair. | |
44 // http://crbug.com/102020 | |
45 typedef std::string Id; | |
46 | |
47 Row(); | |
48 | |
49 // Only used by unit tests. | |
50 Row(const Id& id, | |
51 const string16& user_text, | |
52 const GURL& url, | |
53 int number_of_hits, | |
54 int number_of_misses); | |
55 | |
56 Id id; | |
57 string16 user_text; | |
58 GURL url; | |
59 int number_of_hits; | |
60 int number_of_misses; | |
61 }; | |
62 | |
63 explicit NetworkActionPredictorDatabase(Profile* profile); | |
64 | |
65 // Opens the database file from the profile path. Separated from the | |
66 // constructor to ease construction/destruction of this object on one thread | |
67 // but database access on the DB thread. | |
68 void Initialize(); | |
69 | |
70 void GetRow(const Row::Id& id, Row* row); | |
71 void GetAllRows(std::vector<Row>* row_buffer); | |
72 | |
73 void AddRow(const Row& row); | |
74 void UpdateRow(const Row& row); | |
75 void DeleteRow(const Row::Id& id); | |
76 void DeleteRows(const std::vector<Row::Id>& id_list); | |
77 void DeleteAllRows(); | |
78 | |
79 // For batching database operations. | |
80 void BeginTransaction(); | |
81 void CommitTransaction(); | |
82 | |
83 void OnPredictorDestroyed(); | |
84 | |
85 private: | |
86 friend class NetworkActionPredictorDatabaseTest; | |
87 friend class base::RefCountedThreadSafe<NetworkActionPredictorDatabase>; | |
88 virtual ~NetworkActionPredictorDatabase(); | |
89 | |
90 void CreateTable(); | |
91 | |
92 // TODO(dominich): Consider adding this table to one of the history databases. | |
93 // In memory is currently used, but adding to the on-disk visits database | |
94 // would allow DeleteOldEntries to be cheaper through use of a join. | |
95 FilePath db_path_; | |
96 sql::Connection db_; | |
97 | |
98 // Set when the NetworkActionPredictor is destroyed so we can cancel any | |
99 // posted database requests. | |
100 base::CancellationFlag canceled_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictorDatabase); | |
103 }; | |
104 | |
105 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_DATABASE_H_ | |
OLD | NEW |