OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/stringprintf.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/dom_actions.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 |
| 11 using content::BrowserThread; |
| 12 |
| 13 namespace extensions { |
| 14 |
| 15 const char* DOMAction::kTableName = "activitylog_urls"; |
| 16 const char* DOMAction::kTableBasicFields = |
| 17 "extension_id LONGVARCHAR NOT NULL, " |
| 18 "time INTEGER NOT NULL"; |
| 19 const char* DOMAction::kTableContentFields[] = |
| 20 {"url_action_type", "url", "url_title", "api_call", "args", "extra"}; |
| 21 |
| 22 DOMAction::DOMAction(const std::string& extension_id, |
| 23 const base::Time& time, |
| 24 const DOMActionType verb, |
| 25 const GURL& url, |
| 26 const string16& url_title, |
| 27 const std::string& api_call, |
| 28 const std::string& args, |
| 29 const std::string& extra) |
| 30 : extension_id_(extension_id), |
| 31 time_(time), |
| 32 verb_(verb), |
| 33 url_(url), |
| 34 url_title_(url_title), |
| 35 api_call_(api_call), |
| 36 args_(args), |
| 37 extra_(extra) { } |
| 38 |
| 39 DOMAction::~DOMAction() { |
| 40 } |
| 41 |
| 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) { |
| 64 std::string sql_str = "INSERT INTO " + std::string(kTableName) + |
| 65 " (extension_id, time, url_action_type, url, url_title, api_call, args," |
| 66 " extra) VALUES (?,?,?,?,?,?,?,?)"; |
| 67 sql::Statement statement(db->GetCachedStatement( |
| 68 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 69 statement.BindString(0, extension_id_); |
| 70 statement.BindInt64(1, time_.ToInternalValue()); |
| 71 statement.BindString(2, VerbAsString()); |
| 72 statement.BindString(3, history::URLDatabase::GURLToDatabaseURL(url_)); |
| 73 statement.BindString16(4, url_title_); |
| 74 statement.BindString(5, api_call_); |
| 75 statement.BindString(6, args_); |
| 76 statement.BindString(7, "sdf"); |
| 77 if (!statement.Run()) { |
| 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 } |
| 84 } |
| 85 |
| 86 std::string DOMAction::PrettyPrintFori18n() { |
| 87 // TODO(felt): implement this for real when the UI is redesigned. |
| 88 return PrettyPrintForDebug(); |
| 89 } |
| 90 |
| 91 std::string DOMAction::PrettyPrintForDebug() { |
| 92 // TODO(felt): implement this for real when the UI is redesigned. |
| 93 if (verb_ == INSERTED) |
| 94 return "Injected scripts (" + args_ + ") onto " |
| 95 + std::string(url_.spec()); |
| 96 else |
| 97 return "DOM API CALL: " + api_call_ + ", ARGS: " + args_; |
| 98 } |
| 99 |
| 100 std::string DOMAction::VerbAsString() const { |
| 101 switch (verb_) { |
| 102 case MODIFIED: |
| 103 return "MODIFIED"; |
| 104 case READ: |
| 105 return "READ"; |
| 106 case INSERTED: |
| 107 return "INSERTED"; |
| 108 case XHR: |
| 109 return "XHR"; |
| 110 default: |
| 111 NOTREACHED(); |
| 112 return NULL; |
| 113 } |
| 114 } |
| 115 |
| 116 DOMAction::DOMActionType DOMAction::StringAsDOMActionType( |
| 117 const std::string& str) { |
| 118 if (str == "MODIFIED") { |
| 119 return MODIFIED; |
| 120 } else if (str == "READ") { |
| 121 return READ; |
| 122 } else if (str == "INSERTED") { |
| 123 return INSERTED; |
| 124 } else if (str == "XHR") { |
| 125 return XHR; |
| 126 } else { |
| 127 NOTREACHED(); |
| 128 return MODIFIED; // this should never happen! |
| 129 } |
| 130 } |
| 131 |
| 132 } // namespace extensions |
| 133 |
OLD | NEW |