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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/json/json_string_value_serializer.h" | |
| 6 #include "base/logging.h" | 7 #include "base/logging.h" |
| 7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/activity_log/dom_actions.h" | 10 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
| 11 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | |
| 10 #include "chrome/browser/history/url_database.h" | 12 #include "chrome/browser/history/url_database.h" |
| 11 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 13 | 15 |
| 14 using content::BrowserThread; | 16 using content::BrowserThread; |
| 15 | 17 |
| 16 namespace extensions { | 18 namespace extensions { |
| 17 | 19 |
| 18 using api::activity_log_private::ExtensionActivity; | 20 using api::activity_log_private::ExtensionActivity; |
| 19 using api::activity_log_private::DomActivityDetail; | 21 using api::activity_log_private::DomActivityDetail; |
| 20 using api::activity_log_private::ChromeActivityDetail; | 22 using api::activity_log_private::ChromeActivityDetail; |
| 21 using api::activity_log_private::BlockedChromeActivityDetail; | 23 using api::activity_log_private::BlockedChromeActivityDetail; |
| 22 | 24 |
| 23 const char* DOMAction::kTableName = "activitylog_urls"; | |
| 24 const char* DOMAction::kTableContentFields[] = | |
| 25 {"url_action_type", "url_tld", "url_path", "url_title", "api_call", | |
| 26 "args", "extra"}; | |
| 27 const char* DOMAction::kTableFieldTypes[] = | |
| 28 {"INTEGER", "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR", | |
| 29 "LONGVARCHAR", "LONGVARCHAR"}; | |
| 30 | |
| 31 DOMAction::DOMAction(const std::string& extension_id, | 25 DOMAction::DOMAction(const std::string& extension_id, |
| 32 const base::Time& time, | 26 const base::Time& time, |
| 33 const DomActionType::Type verb, | 27 const DomActionType::Type verb, |
| 34 const GURL& url, | 28 const GURL& url, |
| 35 const string16& url_title, | 29 const string16& url_title, |
| 36 const std::string& api_call, | 30 const std::string& api_call, |
| 37 const std::string& args, | 31 const std::string& args, |
| 38 const std::string& extra) | 32 const std::string& extra) |
| 39 : Action(extension_id, time, ExtensionActivity::ACTIVITY_TYPE_DOM), | 33 : Action(extension_id, time, ExtensionActivity::ACTIVITY_TYPE_DOM), |
| 40 verb_(verb), | 34 verb_(verb), |
| 41 url_(url), | 35 url_(url), |
| 42 url_title_(url_title), | 36 url_title_(url_title), |
| 43 api_call_(api_call), | 37 api_call_(api_call), |
| 44 args_(args), | 38 args_(args), |
| 45 extra_(extra) { } | 39 extra_(extra) { } |
| 46 | 40 |
| 47 DOMAction::DOMAction(const sql::Statement& s) | |
| 48 : Action(s.ColumnString(0), | |
| 49 base::Time::FromInternalValue(s.ColumnInt64(1)), | |
| 50 ExtensionActivity::ACTIVITY_TYPE_DOM), | |
| 51 verb_(static_cast<DomActionType::Type>(s.ColumnInt(2))), | |
| 52 url_(GURL(s.ColumnString(3)+ s.ColumnString(4))), | |
| 53 url_title_(s.ColumnString16(5)), | |
| 54 api_call_(s.ColumnString(6)), | |
| 55 args_(s.ColumnString(7)), | |
| 56 extra_(s.ColumnString(8)) { } | |
| 57 | |
| 58 DOMAction::~DOMAction() { | 41 DOMAction::~DOMAction() { |
| 59 } | 42 } |
| 60 | 43 |
| 61 scoped_ptr<ExtensionActivity> DOMAction::ConvertToExtensionActivity() { | 44 scoped_ptr<ExtensionActivity> DOMAction::ConvertToExtensionActivity() { |
| 62 scoped_ptr<ExtensionActivity> formatted_activity; | 45 scoped_ptr<ExtensionActivity> formatted_activity; |
| 63 formatted_activity.reset(new ExtensionActivity); | 46 formatted_activity.reset(new ExtensionActivity); |
| 64 formatted_activity->extension_id.reset( | 47 formatted_activity->extension_id.reset( |
| 65 new std::string(extension_id())); | 48 new std::string(extension_id())); |
| 66 formatted_activity->activity_type = activity_type(); | 49 formatted_activity->activity_type = activity_type(); |
| 67 formatted_activity->time.reset(new double(time().ToJsTime())); | 50 formatted_activity->time.reset(new double(time().ToJsTime())); |
| 68 DomActivityDetail* details = new DomActivityDetail; | 51 DomActivityDetail* details = new DomActivityDetail; |
| 69 details->dom_activity_type = DomActivityDetail::ParseDomActivityType( | 52 details->dom_activity_type = DomActivityDetail::ParseDomActivityType( |
| 70 VerbAsString()); | 53 VerbAsString()); |
| 71 details->url.reset(new std::string(url_.spec())); | 54 details->url.reset(new std::string(url_.spec())); |
| 72 details->url_title.reset(new std::string(base::UTF16ToUTF8(url_title_))); | 55 details->url_title.reset(new std::string(base::UTF16ToUTF8(url_title_))); |
| 73 details->api_call.reset(new std::string(api_call_)); | 56 details->api_call.reset(new std::string(api_call_)); |
| 74 details->args.reset(new std::string(args_)); | 57 details->args.reset(new std::string(args_)); |
| 75 details->extra.reset(new std::string(extra_)); | 58 details->extra.reset(new std::string(extra_)); |
| 76 formatted_activity->dom_activity_detail.reset(details); | 59 formatted_activity->dom_activity_detail.reset(details); |
| 77 return formatted_activity.Pass(); | 60 return formatted_activity.Pass(); |
| 78 } | 61 } |
| 79 | 62 |
| 80 // static | |
| 81 bool DOMAction::InitializeTable(sql::Connection* db) { | |
| 82 // The original table schema was different than the existing one. | |
| 83 // Sqlite doesn't let you delete or modify existing columns, so we drop it. | |
| 84 // The old version can be identified because it had a field named | |
| 85 // tech_message. Any data loss incurred here doesn't matter since these | |
| 86 // fields existed before we started using the AL for anything. | |
| 87 if (db->DoesColumnExist(kTableName, "tech_message")) { | |
| 88 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 89 if (!db->Execute(drop_table.c_str())) | |
| 90 return false; | |
| 91 } | |
| 92 // The url field is now broken into two parts - url_tld and url_path. | |
| 93 // ulr_tld contains the scheme, host, and port of the url and | |
| 94 // url_path contains the path. | |
| 95 if (db->DoesColumnExist(kTableName, "url")) { | |
| 96 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 97 if (!db->Execute(drop_table.c_str())) | |
| 98 return false; | |
| 99 } | |
| 100 // We also now use INTEGER instead of VARCHAR for url_action_type. | |
| 101 if (db->DoesColumnExist(kTableName, "url_action_type")) { | |
| 102 std::string select = base::StringPrintf( | |
| 103 "SELECT url_action_type FROM %s ORDER BY rowid LIMIT 1", kTableName); | |
| 104 sql::Statement statement(db->GetUniqueStatement(select.c_str())); | |
| 105 if (statement.DeclaredColumnType(0) != sql::COLUMN_TYPE_INTEGER) { | |
| 106 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 107 if (!db->Execute(drop_table.c_str())) | |
| 108 return false; | |
| 109 } | |
| 110 } | |
| 111 // Now initialize the table. | |
| 112 bool initialized = InitializeTableInternal(db, | |
| 113 kTableName, | |
| 114 kTableContentFields, | |
| 115 kTableFieldTypes, | |
| 116 arraysize(kTableContentFields)); | |
| 117 return initialized; | |
| 118 } | |
| 119 | |
| 120 bool DOMAction::Record(sql::Connection* db) { | 63 bool DOMAction::Record(sql::Connection* db) { |
| 121 std::string sql_str = "INSERT INTO " + std::string(kTableName) + | 64 std::string sql_str = "INSERT INTO " + |
| 122 " (extension_id, time, url_action_type, url_tld, url_path, url_title," | 65 std::string(FullStreamUIPolicy::kTableName) + |
| 123 " api_call, args, extra) VALUES (?,?,?,?,?,?,?,?,?)"; | 66 " (extension_id, time, action_type, api_name, args, " |
| 67 "page_url, arg_url, other) VALUES (?,?,?,?,?,?,?,?)"; | |
| 124 sql::Statement statement(db->GetCachedStatement( | 68 sql::Statement statement(db->GetCachedStatement( |
| 125 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 69 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 126 std::string url_tld; | |
| 127 std::string url_path; | |
| 128 statement.BindString(0, extension_id()); | 70 statement.BindString(0, extension_id()); |
| 129 statement.BindInt64(1, time().ToInternalValue()); | 71 statement.BindInt64(1, time().ToInternalValue()); |
| 130 statement.BindInt(2, static_cast<int>(verb_)); | 72 if (verb_ == DomActionType::INSERTED) |
| 131 url_tld = url_.GetOrigin().spec(); | 73 statement.BindInt(2, static_cast<int>(Action::ACTION_CONTENT_SCRIPT)); |
| 132 // delete the extra "/" | 74 else |
| 133 if ((url_tld.size() > 0) && (url_tld[url_tld.size()-1] == '/')) | 75 statement.BindInt(2, static_cast<int>(Action::ACTION_DOM_ACCESS)); |
| 134 url_tld.erase(url_tld.size()-1); | 76 statement.BindString(3, api_call_); |
| 135 statement.BindString(3, url_tld); | 77 |
| 136 // If running in activity testing mode, store the parameters as well. | 78 ListValue args_list; |
| 79 args_list.AppendString(args_); | |
| 80 std::string args_as_text; | |
| 81 JSONStringValueSerializer serializer(&args_as_text); | |
| 82 serializer.SerializeAndOmitBinaryValues(args_list); | |
| 83 statement.BindString(4, args_as_text); | |
| 84 | |
| 85 // If running in activity testing mode, store the URL parameters as well. | |
| 86 GURL database_url; | |
| 87 database_url = url_; | |
|
Matt Perry
2013/07/17 22:13:19
remove this line
mvrable
2013/07/17 22:56:35
Done.
| |
| 137 if ((CommandLine::ForCurrentProcess()->HasSwitch( | 88 if ((CommandLine::ForCurrentProcess()->HasSwitch( |
| 138 switches::kEnableExtensionActivityLogTesting)) && (url_.has_query())) | 89 switches::kEnableExtensionActivityLogTesting))) { |
| 139 url_path = url_.path()+"?"+url_.query(); | 90 database_url = url_; |
| 91 } else { | |
| 92 url_canon::Replacements<char> sanitize; | |
| 93 sanitize.ClearQuery(); | |
| 94 sanitize.ClearRef(); | |
| 95 database_url = url_.ReplaceComponents(sanitize); | |
| 96 } | |
| 97 statement.BindString(5, database_url.spec()); | |
| 98 | |
| 99 if (verb_ == DomActionType::INSERTED) | |
| 100 statement.BindString(6, args_); | |
| 140 else | 101 else |
| 141 url_path = url_.path(); | 102 statement.BindNull(6); |
| 142 statement.BindString(4, url_path); | 103 |
| 143 statement.BindString16(5, url_title_); | 104 DictionaryValue other; |
| 144 statement.BindString(6, api_call_); | 105 other.SetString("extra", extra_); |
| 145 statement.BindString(7, args_); | 106 other.SetString("page_title", url_title_); |
| 146 statement.BindString(8, extra_); | 107 other.SetInteger("dom_verb", static_cast<int>(verb_)); |
| 108 std::string other_string; | |
| 109 JSONStringValueSerializer other_serializer(&other_string); | |
| 110 other_serializer.SerializeAndOmitBinaryValues(other); | |
| 111 statement.BindString(7, other_string); | |
| 112 | |
| 147 if (!statement.Run()) { | 113 if (!statement.Run()) { |
| 148 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 114 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 149 statement.Clear(); | 115 statement.Clear(); |
| 150 return false; | 116 return false; |
| 151 } | 117 } |
| 152 return true; | 118 return true; |
| 153 } | 119 } |
| 154 | 120 |
| 155 std::string DOMAction::PrintForDebug() { | 121 std::string DOMAction::PrintForDebug() { |
| 156 if (verb_ == DomActionType::INSERTED) | 122 if (verb_ == DomActionType::INSERTED) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 177 return "webrequest"; | 143 return "webrequest"; |
| 178 case DomActionType::MODIFIED: // legacy | 144 case DomActionType::MODIFIED: // legacy |
| 179 return "modified"; | 145 return "modified"; |
| 180 default: | 146 default: |
| 181 NOTREACHED(); | 147 NOTREACHED(); |
| 182 return NULL; | 148 return NULL; |
| 183 } | 149 } |
| 184 } | 150 } |
| 185 | 151 |
| 186 } // namespace extensions | 152 } // namespace extensions |
| OLD | NEW |