| 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/stringprintf.h" |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/activity_log/dom_actions.h" | 8 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
| 9 #include "chrome/browser/history/url_database.h" | 9 #include "chrome/browser/history/url_database.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 | 11 |
| 12 using content::BrowserThread; | 12 using content::BrowserThread; |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 const char* DOMAction::kTableName = "activitylog_urls"; | 16 const char* DOMAction::kTableName = "activitylog_urls"; |
| 17 const char* DOMAction::kTableContentFields[] = | 17 const char* DOMAction::kTableContentFields[] = |
| 18 {"url_action_type", "url", "url_title", "api_call", "args", "extra"}; | 18 {"url_action_type", "url", "url_title", "api_call", "args", "extra"}; |
| 19 const char* DOMAction::kTableFieldTypes[] = |
| 20 {"INTEGER", "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR", |
| 21 "LONGVARCHAR"}; |
| 19 | 22 |
| 20 DOMAction::DOMAction(const std::string& extension_id, | 23 DOMAction::DOMAction(const std::string& extension_id, |
| 21 const base::Time& time, | 24 const base::Time& time, |
| 22 const DOMActionType verb, | 25 const DOMActionType verb, |
| 23 const GURL& url, | 26 const GURL& url, |
| 24 const string16& url_title, | 27 const string16& url_title, |
| 25 const std::string& api_call, | 28 const std::string& api_call, |
| 26 const std::string& args, | 29 const std::string& args, |
| 27 const std::string& extra) | 30 const std::string& extra) |
| 28 : Action(extension_id, time), | 31 : Action(extension_id, time), |
| 29 verb_(verb), | 32 verb_(verb), |
| 30 url_(url), | 33 url_(url), |
| 31 url_title_(url_title), | 34 url_title_(url_title), |
| 32 api_call_(api_call), | 35 api_call_(api_call), |
| 33 args_(args), | 36 args_(args), |
| 34 extra_(extra) { } | 37 extra_(extra) { } |
| 35 | 38 |
| 36 DOMAction::DOMAction(const sql::Statement& s) | 39 DOMAction::DOMAction(const sql::Statement& s) |
| 37 : Action(s.ColumnString(0), | 40 : Action(s.ColumnString(0), |
| 38 base::Time::FromInternalValue(s.ColumnInt64(1))), | 41 base::Time::FromInternalValue(s.ColumnInt64(1))), |
| 39 verb_(StringAsDOMActionType(s.ColumnString(2))), | 42 verb_(static_cast<DOMActionType>(s.ColumnInt(2))), |
| 40 url_(GURL(s.ColumnString(3))), | 43 url_(GURL(s.ColumnString(3))), |
| 41 url_title_(s.ColumnString16(4)), | 44 url_title_(s.ColumnString16(4)), |
| 42 api_call_(s.ColumnString(5)), | 45 api_call_(s.ColumnString(5)), |
| 43 args_(s.ColumnString(6)), | 46 args_(s.ColumnString(6)), |
| 44 extra_(s.ColumnString(7)) { } | 47 extra_(s.ColumnString(7)) { } |
| 45 | 48 |
| 46 DOMAction::~DOMAction() { | 49 DOMAction::~DOMAction() { |
| 47 } | 50 } |
| 48 | 51 |
| 49 // static | 52 // static |
| 50 bool DOMAction::InitializeTable(sql::Connection* db) { | 53 bool DOMAction::InitializeTable(sql::Connection* db) { |
| 51 // The original table schema was different than the existing one. | 54 // The original table schema was different than the existing one. |
| 52 // Sqlite doesn't let you delete or modify existing columns, so we drop it. | 55 // Sqlite doesn't let you delete or modify existing columns, so we drop it. |
| 53 // The old version can be identified because it had a field named | 56 // The old version can be identified because it had a field named |
| 54 // tech_message. Any data loss incurred here doesn't matter since these | 57 // tech_message. Any data loss incurred here doesn't matter since these |
| 55 // fields existed before we started using the AL for anything. | 58 // fields existed before we started using the AL for anything. |
| 56 if (db->DoesColumnExist(kTableName, "tech_message")) { | 59 if (db->DoesColumnExist(kTableName, "tech_message")) { |
| 57 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | 60 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); |
| 58 if (!db->Execute(drop_table.c_str())) | 61 if (!db->Execute(drop_table.c_str())) |
| 59 return false; | 62 return false; |
| 60 } | 63 } |
| 64 // We also now use INTEGER instead of VARCHAR for url_action_type. |
| 65 if (db->DoesColumnExist(kTableName, "url_action_type")) { |
| 66 std::string select = base::StringPrintf( |
| 67 "SELECT url_action_type FROM %s ORDER BY rowid LIMIT 1", kTableName); |
| 68 sql::Statement statement(db->GetUniqueStatement(select.c_str())); |
| 69 if (statement.DeclaredColumnType(0) != sql::COLUMN_TYPE_INTEGER) { |
| 70 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); |
| 71 if (!db->Execute(drop_table.c_str())) |
| 72 return false; |
| 73 } |
| 74 } |
| 61 // Now initialize the table. | 75 // Now initialize the table. |
| 62 bool initialized = InitializeTableInternal(db, | 76 bool initialized = InitializeTableInternal(db, |
| 63 kTableName, | 77 kTableName, |
| 64 kTableContentFields, | 78 kTableContentFields, |
| 79 kTableFieldTypes, |
| 65 arraysize(kTableContentFields)); | 80 arraysize(kTableContentFields)); |
| 66 return initialized; | 81 return initialized; |
| 67 } | 82 } |
| 68 | 83 |
| 69 void DOMAction::Record(sql::Connection* db) { | 84 void DOMAction::Record(sql::Connection* db) { |
| 70 std::string sql_str = "INSERT INTO " + std::string(kTableName) + | 85 std::string sql_str = "INSERT INTO " + std::string(kTableName) + |
| 71 " (extension_id, time, url_action_type, url, url_title, api_call, args," | 86 " (extension_id, time, url_action_type, url, url_title, api_call, args," |
| 72 " extra) VALUES (?,?,?,?,?,?,?,?)"; | 87 " extra) VALUES (?,?,?,?,?,?,?,?)"; |
| 73 sql::Statement statement(db->GetCachedStatement( | 88 sql::Statement statement(db->GetCachedStatement( |
| 74 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 89 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 75 statement.BindString(0, extension_id()); | 90 statement.BindString(0, extension_id()); |
| 76 statement.BindInt64(1, time().ToInternalValue()); | 91 statement.BindInt64(1, time().ToInternalValue()); |
| 77 statement.BindString(2, VerbAsString()); | 92 statement.BindInt(2, static_cast<int>(verb_)); |
| 78 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); | 93 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); |
| 79 statement.BindString16(4, url_title_); | 94 statement.BindString16(4, url_title_); |
| 80 statement.BindString(5, api_call_); | 95 statement.BindString(5, api_call_); |
| 81 statement.BindString(6, args_); | 96 statement.BindString(6, args_); |
| 82 statement.BindString(7, extra_); | 97 statement.BindString(7, extra_); |
| 83 if (!statement.Run()) | 98 if (!statement.Run()) |
| 84 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 99 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 85 } | 100 } |
| 86 | 101 |
| 87 std::string DOMAction::PrintForDebug() { | 102 std::string DOMAction::PrintForDebug() { |
| 88 if (verb_ == INSERTED) | 103 if (verb_ == INSERTED) |
| 89 return "Injected scripts (" + args_ + ") onto " | 104 return "Injected scripts (" + args_ + ") onto " |
| 90 + std::string(url_.spec()); | 105 + std::string(url_.spec()); |
| 91 else | 106 else |
| 92 return "DOM API CALL: " + api_call_ + ", ARGS: " + args_; | 107 return "DOM API CALL: " + api_call_ + ", ARGS: " + args_ + ", VERB: " |
| 108 + VerbAsString(); |
| 93 } | 109 } |
| 94 | 110 |
| 95 std::string DOMAction::VerbAsString() const { | 111 std::string DOMAction::VerbAsString() const { |
| 96 switch (verb_) { | 112 switch (verb_) { |
| 97 case GETTER: | 113 case GETTER: |
| 98 return "GETTER"; | 114 return "GETTER"; |
| 99 case SETTER: | 115 case SETTER: |
| 100 return "SETTER"; | 116 return "SETTER"; |
| 101 case METHOD: | 117 case METHOD: |
| 102 return "METHOD"; | 118 return "METHOD"; |
| 103 case INSERTED: | 119 case INSERTED: |
| 104 return "INSERTED"; | 120 return "INSERTED"; |
| 105 case XHR: | 121 case XHR: |
| 106 return "XHR"; | 122 return "XHR"; |
| 107 case WEBREQUEST: | 123 case WEBREQUEST: |
| 108 return "WEBREQUEST"; | 124 return "WEBREQUEST"; |
| 109 case MODIFIED: // legacy | 125 case MODIFIED: // legacy |
| 110 return "MODIFIED"; | 126 return "MODIFIED"; |
| 111 default: | 127 default: |
| 112 NOTREACHED(); | 128 NOTREACHED(); |
| 113 return NULL; | 129 return NULL; |
| 114 } | 130 } |
| 115 } | 131 } |
| 116 | 132 |
| 117 DOMAction::DOMActionType DOMAction::StringAsDOMActionType( | |
| 118 const std::string& str) { | |
| 119 if (str == "GETTER") { | |
| 120 return GETTER; | |
| 121 } else if (str == "SETTER") { | |
| 122 return SETTER; | |
| 123 } else if (str == "METHOD") { | |
| 124 return METHOD; | |
| 125 } else if (str == "INSERTED") { | |
| 126 return INSERTED; | |
| 127 } else if (str == "XHR") { | |
| 128 return XHR; | |
| 129 } else if (str == "WEBREQUEST") { | |
| 130 return WEBREQUEST; | |
| 131 } else if (str == "MODIFIED") { // legacy | |
| 132 return MODIFIED; | |
| 133 } else if (str == "READ") { // legacy | |
| 134 return GETTER; | |
| 135 } else { | |
| 136 NOTREACHED(); | |
| 137 return MODIFIED; // this should never happen! | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 } // namespace extensions | 133 } // namespace extensions |
| OLD | NEW |