OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/stringprintf.h" |
6 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
7 #include "chrome/browser/extensions/url_actions.h" | 8 #include "chrome/browser/extensions/dom_actions.h" |
8 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
9 | 10 |
10 using content::BrowserThread; | 11 using content::BrowserThread; |
11 | 12 |
12 namespace extensions { | 13 namespace extensions { |
13 | 14 |
14 const char* UrlAction::kTableName = "activitylog_urls"; | 15 const char* DOMAction::kTableName = "activitylog_urls"; |
15 const char* UrlAction::kTableStructure = "(" | 16 const char* DOMAction::kTableBasicFields = |
16 "extension_id LONGVARCHAR NOT NULL, " | 17 "extension_id LONGVARCHAR NOT NULL, " |
17 "time INTEGER NOT NULL, " | 18 "time INTEGER NOT NULL"; |
18 "url_action_type LONGVARCHAR NOT NULL, " | 19 const char* DOMAction::kTableContentFields[] = |
19 "url LONGVARCHAR NOT NULL, " | 20 {"url_action_type", "url", "url_title", "api_call", "args", "extra"}; |
20 "url_title LONGVARCHAR, " | |
21 "tech_message LONGVARCHAR NOT NULL, " | |
22 "extra LONGCHAR VAR NOT NULL)"; | |
23 | 21 |
24 UrlAction::UrlAction(const std::string& extension_id, | 22 DOMAction::DOMAction(const std::string& extension_id, |
25 const base::Time& time, | 23 const base::Time& time, |
26 const UrlActionType verb, | 24 const DOMActionType verb, |
27 const GURL& url, | 25 const GURL& url, |
28 const string16& url_title, | 26 const string16& url_title, |
29 const std::string& tech_message, | 27 const std::string& api_call, |
| 28 const std::string& args, |
30 const std::string& extra) | 29 const std::string& extra) |
31 : extension_id_(extension_id), | 30 : extension_id_(extension_id), |
32 time_(time), | 31 time_(time), |
33 verb_(verb), | 32 verb_(verb), |
34 url_(url), | 33 url_(url), |
35 url_title_(url_title), | 34 url_title_(url_title), |
36 technical_message_(tech_message), | 35 api_call_(api_call), |
| 36 args_(args), |
37 extra_(extra) { } | 37 extra_(extra) { } |
38 | 38 |
39 UrlAction::~UrlAction() { | 39 DOMAction::~DOMAction() { |
40 } | 40 } |
41 | 41 |
42 void UrlAction::Record(sql::Connection* db) { | 42 // static |
| 43 bool DOMAction::InitializeTable(sql::Connection* db) { |
| 44 // The original table schema was different than the existing one. |
| 45 // Sqlite doesn't let you delete or modify existing columns, so we drop it. |
| 46 // The old version can be identified because it had a field named |
| 47 // tech_message. Any data loss incurred here doesn't matter since these |
| 48 // fields existed before we started using the AL for anything. |
| 49 if (db->DoesColumnExist(kTableName, "tech_message")) { |
| 50 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); |
| 51 if (!db->Execute(drop_table.c_str())) |
| 52 return false; |
| 53 } |
| 54 // Now initialize the table. |
| 55 bool initialized = InitializeTableInternal(db, |
| 56 kTableName, |
| 57 kTableBasicFields, |
| 58 kTableContentFields, |
| 59 arraysize(kTableContentFields)); |
| 60 return initialized; |
| 61 } |
| 62 |
| 63 void DOMAction::Record(sql::Connection* db) { |
43 std::string sql_str = "INSERT INTO " + std::string(kTableName) + | 64 std::string sql_str = "INSERT INTO " + std::string(kTableName) + |
44 " (extension_id, time, url_action_type, url, url_title, tech_message," | 65 " (extension_id, time, url_action_type, url, url_title, api_call, args," |
45 " extra) VALUES (?,?,?,?,?,?,?)"; | 66 " extra) VALUES (?,?,?,?,?,?,?,?)"; |
46 sql::Statement statement(db->GetCachedStatement( | 67 sql::Statement statement(db->GetCachedStatement( |
47 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 68 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
48 statement.BindString(0, extension_id_); | 69 statement.BindString(0, extension_id_); |
49 statement.BindInt64(1, time_.ToInternalValue()); | 70 statement.BindInt64(1, time_.ToInternalValue()); |
50 statement.BindString(2, VerbAsString()); | 71 statement.BindString(2, VerbAsString()); |
51 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); | 72 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); |
52 statement.BindString16(4, url_title_); | 73 statement.BindString16(4, url_title_); |
53 statement.BindString(5, technical_message_); | 74 statement.BindString(5, api_call_); |
54 statement.BindString(6, extra_); | 75 statement.BindString(6, args_); |
55 if (!statement.Run()) | 76 statement.BindString(7, "sdf"); |
| 77 if (!statement.Run()) { |
56 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 78 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 79 LOG(ERROR) << "extension_id: " << extension_id_ << "; verb: " << |
| 80 VerbAsString() << "; url: " << |
| 81 history::URLDatabase::GURLToDatabaseURL(url_) << "; title: " << url_title_ |
| 82 << "; api_call: " << api_call_ << "; args: " << args_; |
| 83 } |
57 } | 84 } |
58 | 85 |
59 std::string UrlAction::PrettyPrintFori18n() { | 86 std::string DOMAction::PrettyPrintFori18n() { |
60 // TODO(felt): implement this for real when the UI is redesigned. | 87 // TODO(felt): implement this for real when the UI is redesigned. |
61 return PrettyPrintForDebug(); | 88 return PrettyPrintForDebug(); |
62 } | 89 } |
63 | 90 |
64 std::string UrlAction::PrettyPrintForDebug() { | 91 std::string DOMAction::PrettyPrintForDebug() { |
65 // TODO(felt): implement this for real when the UI is redesigned. | 92 // TODO(felt): implement this for real when the UI is redesigned. |
66 return "Injected scripts (" + technical_message_ + ") onto " | 93 if (verb_ == INSERTED) |
| 94 return "Injected scripts (" + args_ + ") onto " |
67 + std::string(url_.spec()); | 95 + std::string(url_.spec()); |
| 96 else |
| 97 return "DOM API CALL: " + api_call_ + ", ARGS: " + args_; |
68 } | 98 } |
69 | 99 |
70 std::string UrlAction::VerbAsString() const { | 100 std::string DOMAction::VerbAsString() const { |
71 switch (verb_) { | 101 switch (verb_) { |
72 case MODIFIED: | 102 case MODIFIED: |
73 return "MODIFIED"; | 103 return "MODIFIED"; |
74 case READ: | 104 case READ: |
75 return "READ"; | 105 return "READ"; |
76 case INSERTED: | 106 case INSERTED: |
77 return "INSERTED"; | 107 return "INSERTED"; |
78 case XHR: | 108 case XHR: |
79 return "XHR"; | 109 return "XHR"; |
80 default: | 110 default: |
81 NOTREACHED(); | 111 NOTREACHED(); |
82 return NULL; | 112 return NULL; |
83 } | 113 } |
84 } | 114 } |
85 | 115 |
86 UrlAction::UrlActionType UrlAction::StringAsUrlActionType( | 116 DOMAction::DOMActionType DOMAction::StringAsDOMActionType( |
87 const std::string& str) { | 117 const std::string& str) { |
88 if (str == "MODIFIED") { | 118 if (str == "MODIFIED") { |
89 return MODIFIED; | 119 return MODIFIED; |
90 } else if (str == "READ") { | 120 } else if (str == "READ") { |
91 return READ; | 121 return READ; |
92 } else if (str == "INSERTED") { | 122 } else if (str == "INSERTED") { |
93 return INSERTED; | 123 return INSERTED; |
94 } else if (str == "XHR") { | 124 } else if (str == "XHR") { |
95 return XHR; | 125 return XHR; |
96 } else { | 126 } else { |
97 NOTREACHED(); | 127 NOTREACHED(); |
98 return MODIFIED; // this should never happen! | 128 return MODIFIED; // this should never happen! |
99 } | 129 } |
100 } | 130 } |
101 | 131 |
102 } // namespace extensions | 132 } // namespace extensions |
103 | 133 |
OLD | NEW |