Chromium Code Reviews| 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( | |
| 56 db, | |
| 57 kTableName, | |
| 58 kTableBasicFields, | |
| 59 kTableContentFields, | |
| 60 (sizeof(kTableContentFields)/(sizeof(char*)))); | |
|
Matt Perry
2013/02/12 22:01:26
ARRAYSIZE
| |
| 61 return initialized; | |
| 62 } | |
| 63 | |
| 64 void DOMAction::Record(sql::Connection* db) { | |
| 43 std::string sql_str = "INSERT INTO " + std::string(kTableName) + | 65 std::string sql_str = "INSERT INTO " + std::string(kTableName) + |
| 44 " (extension_id, time, url_action_type, url, url_title, tech_message," | 66 " (extension_id, time, url_action_type, url, url_title, api_call, args," |
| 45 " extra) VALUES (?,?,?,?,?,?,?)"; | 67 " extra) VALUES (?,?,?,?,?,?,?,?)"; |
| 46 sql::Statement statement(db->GetCachedStatement( | 68 sql::Statement statement(db->GetCachedStatement( |
| 47 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 69 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 48 statement.BindString(0, extension_id_); | 70 statement.BindString(0, extension_id_); |
| 49 statement.BindInt64(1, time_.ToInternalValue()); | 71 statement.BindInt64(1, time_.ToInternalValue()); |
| 50 statement.BindString(2, VerbAsString()); | 72 statement.BindString(2, VerbAsString()); |
| 51 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); | 73 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); |
| 52 statement.BindString16(4, url_title_); | 74 statement.BindString16(4, url_title_); |
| 53 statement.BindString(5, technical_message_); | 75 statement.BindString(5, api_call_); |
| 54 statement.BindString(6, extra_); | 76 statement.BindString(6, args_); |
| 55 if (!statement.Run()) | 77 statement.BindString(7, "sdf"); |
| 78 if (!statement.Run()) { | |
| 56 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 79 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 80 LOG(ERROR) << "extension_id: " << extension_id_ << "; verb: " << | |
| 81 VerbAsString() << "; url: " << | |
| 82 history::URLDatabase::GURLToDatabaseURL(url_) << "; title: " << url_title_ | |
| 83 << "; api_call: " << api_call_ << "; args: " << args_; | |
| 84 } | |
| 57 } | 85 } |
| 58 | 86 |
| 59 std::string UrlAction::PrettyPrintFori18n() { | 87 std::string DOMAction::PrettyPrintFori18n() { |
| 60 // TODO(felt): implement this for real when the UI is redesigned. | 88 // TODO(felt): implement this for real when the UI is redesigned. |
| 61 return PrettyPrintForDebug(); | 89 return PrettyPrintForDebug(); |
| 62 } | 90 } |
| 63 | 91 |
| 64 std::string UrlAction::PrettyPrintForDebug() { | 92 std::string DOMAction::PrettyPrintForDebug() { |
| 65 // TODO(felt): implement this for real when the UI is redesigned. | 93 // TODO(felt): implement this for real when the UI is redesigned. |
| 66 return "Injected scripts (" + technical_message_ + ") onto " | 94 if (verb_ == INSERTED) |
| 95 return "Injected scripts (" + args_ + ") onto " | |
| 67 + std::string(url_.spec()); | 96 + std::string(url_.spec()); |
| 97 else | |
| 98 return "DOM API CALL: " + api_call_ + ", ARGS: " + args_; | |
| 68 } | 99 } |
| 69 | 100 |
| 70 std::string UrlAction::VerbAsString() const { | 101 std::string DOMAction::VerbAsString() const { |
| 71 switch (verb_) { | 102 switch (verb_) { |
| 72 case MODIFIED: | 103 case MODIFIED: |
| 73 return "MODIFIED"; | 104 return "MODIFIED"; |
| 74 case READ: | 105 case READ: |
| 75 return "READ"; | 106 return "READ"; |
| 76 case INSERTED: | 107 case INSERTED: |
| 77 return "INSERTED"; | 108 return "INSERTED"; |
| 78 case XHR: | 109 case XHR: |
| 79 return "XHR"; | 110 return "XHR"; |
| 80 default: | 111 default: |
| 81 NOTREACHED(); | 112 NOTREACHED(); |
| 82 return NULL; | 113 return NULL; |
| 83 } | 114 } |
| 84 } | 115 } |
| 85 | 116 |
| 86 UrlAction::UrlActionType UrlAction::StringAsUrlActionType( | 117 DOMAction::DOMActionType DOMAction::StringAsDOMActionType( |
| 87 const std::string& str) { | 118 const std::string& str) { |
| 88 if (str == "MODIFIED") { | 119 if (str == "MODIFIED") { |
| 89 return MODIFIED; | 120 return MODIFIED; |
| 90 } else if (str == "READ") { | 121 } else if (str == "READ") { |
| 91 return READ; | 122 return READ; |
| 92 } else if (str == "INSERTED") { | 123 } else if (str == "INSERTED") { |
| 93 return INSERTED; | 124 return INSERTED; |
| 94 } else if (str == "XHR") { | 125 } else if (str == "XHR") { |
| 95 return XHR; | 126 return XHR; |
| 96 } else { | 127 } else { |
| 97 NOTREACHED(); | 128 NOTREACHED(); |
| 98 return MODIFIED; // this should never happen! | 129 return MODIFIED; // this should never happen! |
| 99 } | 130 } |
| 100 } | 131 } |
| 101 | 132 |
| 102 } // namespace extensions | 133 } // namespace extensions |
| 103 | 134 |
| OLD | NEW |