OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 #include <string> | |
6 #include "base/logging.h" | |
7 #include "chrome/browser/extensions/activity_database.h" | |
8 #include "chrome/browser/history/url_database.h" | |
9 #include "sql/transaction.h" | |
10 | |
11 #if defined(OS_MACOSX) | |
12 #include "base/mac/mac_util.h" | |
13 #endif | |
14 | |
15 namespace { | |
16 | |
17 void LogStatementFailure(const std::string& sql_str) { | |
18 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | |
19 } | |
20 | |
21 } // namespace | |
22 | |
23 namespace extensions { | |
24 | |
25 ActivityDatabase::ActivityDatabase() { | |
26 initialized_ = false; | |
Eric Dingle
2012/12/13 20:37:34
You should initialize this in the initialization l
felt
2012/12/15 02:51:52
Done.
| |
27 } | |
28 | |
29 ActivityDatabase::~ActivityDatabase() { | |
30 Close(); // Safe to call Close() even if Open() never happened. | |
31 } | |
32 | |
33 sql::InitStatus ActivityDatabase::Init( | |
34 const FilePath& db_name, | |
35 sql::ErrorDelegate* error_delegate) { | |
36 db_.set_error_delegate(error_delegate); | |
37 db_.set_page_size(4096); | |
38 db_.set_cache_size(32); | |
39 | |
40 if (!db_.Open(db_name)) { | |
41 LOG(ERROR) << db_.GetErrorMessage(); | |
42 return MarkInitFailure(); | |
43 } | |
44 | |
45 // Wrap the initialization in a transaction so that the db doesn't | |
46 // get corrupted if init fails/crashes. | |
47 sql::Transaction committer(&db_); | |
48 if (!committer.Begin()) | |
49 return MarkInitFailure(); | |
50 | |
51 #if defined(OS_MACOSX) | |
52 // Exclude the database from backups. | |
53 base::mac::SetFileBackupExclusion(db_name); | |
54 #endif | |
55 | |
56 db_.Preload(); | |
57 | |
58 // Create the UrlAction database. | |
59 if (!db_.DoesTableExist(url_table_name_)) { | |
Eric Dingle
2012/12/13 20:37:34
This logic might be worth extracting to a helper m
felt
2012/12/15 02:51:52
Done.
| |
60 std::string url_table_creator = "CREATE TABLE " + | |
Eric Dingle
2012/12/13 20:37:34
Replace these concatenations with StrCat(...). Her
felt
2012/12/15 02:51:52
Done.
| |
61 std::string(url_table_name_) + " " + | |
62 std::string(url_table_structure_); | |
63 if (!db_.Execute(url_table_creator.c_str())) | |
64 return MarkInitFailure(); | |
65 } | |
66 | |
67 // Create the ManagerAction database. | |
68 if (!db_.DoesTableExist(manager_table_name_)) { | |
69 std::string manager_table_creator = "CREATE TABLE " + | |
70 std::string(manager_table_name_) + " " + | |
71 std::string(manager_table_structure_); | |
72 if (!db_.Execute(manager_table_creator.c_str())) | |
73 return MarkInitFailure(); | |
74 } | |
75 | |
76 // Create the BlockedAction database. | |
77 if (!db_.DoesTableExist(blocked_table_name_)) { | |
78 std::string blocked_table_creator = "CREATE TABLE " + | |
79 std::string(blocked_table_name_) + " " + | |
80 std::string(blocked_table_structure_); | |
81 if (!db_.Execute(blocked_table_creator.c_str())) | |
82 return MarkInitFailure(); | |
83 } | |
84 | |
85 sql::InitStatus stat = committer.Commit() ? sql::INIT_OK : MarkInitFailure(); | |
86 if (stat == sql::INIT_OK) | |
87 initialized_ = true; | |
88 return stat; | |
89 } | |
90 | |
91 sql::InitStatus ActivityDatabase::MarkInitFailure() { | |
Eric Dingle
2012/12/13 20:37:34
Not sure that this method is needed. initialized_
felt
2012/12/15 02:51:52
Done.
| |
92 initialized_ = false; | |
93 return sql::INIT_FAILURE; | |
94 } | |
95 | |
96 // The table structure is defined in url_actions.h. | |
97 void ActivityDatabase::RecordUrlAction(scoped_refptr<UrlAction> action) { | |
98 if (!initialized_) | |
99 return; | |
100 std::string sql_str = "INSERT INTO " + std::string(url_table_name_) + | |
101 " (extension_id, time, url_action_type, url, url_title, api_call)" | |
102 " VALUES (?,?,?,?,?,?)"; | |
103 sql::Statement statement(db_.GetCachedStatement( | |
104 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | |
105 statement.BindString(0, action->extension_id()); | |
106 statement.BindInt64(1, action->time().ToInternalValue()); | |
107 statement.BindString(2, action->VerbAsString()); | |
108 statement.BindString(3, | |
109 history::URLDatabase::GURLToDatabaseURL(action->url())); | |
110 statement.BindString16(4, action->url_title()); | |
111 statement.BindString(5, action->script()); | |
112 if (!statement.Run()) | |
113 LogStatementFailure(sql_str); | |
114 } | |
115 | |
116 // The table structure is defined in manager_actions.h. | |
117 void ActivityDatabase::RecordManagerAction( | |
118 scoped_refptr<ManagerAction> action) { | |
119 if (!initialized_) | |
120 return; | |
121 std::string sql_str = "INSERT INTO " + std::string(manager_table_name_) + | |
122 " (extension_id, time, manager_action_type, target_type, api_call)" | |
123 " VALUES (?,?,?,?,?)"; | |
124 sql::Statement statement(db_.GetCachedStatement( | |
125 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | |
126 statement.BindString(0, action->extension_id()); | |
127 statement.BindInt64(1, action->time().ToInternalValue()); | |
128 statement.BindString(2, action->VerbAsString()); | |
129 statement.BindString(3, action->TargetAsString()); | |
130 statement.BindString(4, action->api_call()); | |
131 if (!statement.Run()) | |
132 LogStatementFailure(sql_str); | |
133 } | |
134 | |
135 // The table structure is defined in blocked_actions.h | |
136 void ActivityDatabase::RecordBlockedAction( | |
137 scoped_refptr<BlockedAction> action) { | |
138 if (!initialized_) | |
139 return; | |
140 std::string sql_str = "INSERT INTO " + std::string(blocked_table_name_) + | |
141 " (extension_id, time, blocked_action, reason)" | |
142 " VALUES (?,?,?,?)"; | |
143 sql::Statement statement(db_.GetCachedStatement( | |
144 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | |
145 statement.BindString(0, action->extension_id()); | |
146 statement.BindInt64(1, action->time().ToInternalValue()); | |
147 statement.BindString(2, action->blocked_action()); | |
148 statement.BindString(3, action->reason()); | |
149 if (!statement.Run()) | |
150 LogStatementFailure(sql_str); | |
151 } | |
152 | |
153 void ActivityDatabase::BeginTransaction() { | |
154 db_.BeginTransaction(); | |
155 } | |
156 | |
157 void ActivityDatabase::CommitTransaction() { | |
158 db_.CommitTransaction(); | |
159 } | |
160 | |
161 void ActivityDatabase::RollbackTransaction() { | |
162 db_.RollbackTransaction(); | |
163 } | |
164 | |
165 bool ActivityDatabase::Raze() { | |
166 return db_.Raze(); | |
167 } | |
168 | |
169 void ActivityDatabase::Close() { | |
170 db_.Close(); | |
171 } | |
172 | |
173 void ActivityDatabase::KillDatabase() { | |
174 db_.RollbackTransaction(); | |
175 db_.Raze(); | |
176 db_.Close(); | |
177 } | |
178 | |
179 } // namespace | |
180 | |
OLD | NEW |