Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | |
|
James Hawkins
2012/02/05 05:21:51
nit: <string> include goes after the web_intents_t
Greg Billock
2012/02/07 00:45:06
Done.
| |
| 5 #include "chrome/browser/webdata/web_intents_table.h" | 6 #include "chrome/browser/webdata/web_intents_table.h" |
| 6 | 7 |
| 8 #include "base/i18n/case_conversion.h" | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | |
| 8 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/intents/default_intent_service.h" | |
| 9 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/mime_util.h" | |
| 10 #include "sql/statement.h" | 15 #include "sql/statement.h" |
| 11 | 16 |
| 12 using webkit_glue::WebIntentServiceData; | 17 using webkit_glue::WebIntentServiceData; |
| 13 | 18 |
| 14 namespace { | 19 namespace { |
| 15 | 20 |
| 16 bool ExtractIntents(sql::Statement* s, | 21 bool ExtractIntents(sql::Statement* s, |
| 17 std::vector<WebIntentServiceData>* services) { | 22 std::vector<WebIntentServiceData>* services) { |
| 18 DCHECK(s); | 23 DCHECK(s); |
| 19 if (!s->is_valid()) | 24 if (!s->is_valid()) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 41 | 46 |
| 42 WebIntentsTable::WebIntentsTable(sql::Connection* db, | 47 WebIntentsTable::WebIntentsTable(sql::Connection* db, |
| 43 sql::MetaTable* meta_table) | 48 sql::MetaTable* meta_table) |
| 44 : WebDatabaseTable(db, meta_table) { | 49 : WebDatabaseTable(db, meta_table) { |
| 45 } | 50 } |
| 46 | 51 |
| 47 WebIntentsTable::~WebIntentsTable() { | 52 WebIntentsTable::~WebIntentsTable() { |
| 48 } | 53 } |
| 49 | 54 |
| 50 bool WebIntentsTable::Init() { | 55 bool WebIntentsTable::Init() { |
| 51 if (db_->DoesTableExist("web_intents")) | 56 if (!db_->DoesTableExist("web_intents")) { |
| 52 return true; | 57 if (!db_->Execute("CREATE TABLE web_intents (" |
| 58 "service_url LONGVARCHAR," | |
| 59 "action VARCHAR," | |
| 60 "type VARCHAR," | |
| 61 "title LONGVARCHAR," | |
| 62 "disposition VARCHAR," | |
| 63 "UNIQUE (service_url, action, type))")) { | |
| 64 return false; | |
| 65 } | |
| 66 } | |
| 53 | 67 |
| 54 if (!db_->Execute("CREATE TABLE web_intents (" | 68 if (!db_->DoesTableExist("web_intents_defaults")) { |
| 55 "service_url LONGVARCHAR," | 69 if (!db_->Execute("CREATE TABLE web_intents_defaults (" |
| 56 "action VARCHAR," | 70 "action VARCHAR," |
| 57 "type VARCHAR," | 71 "type VARCHAR," |
| 58 "title VARCHAR," | 72 "url_prefix LONGVARCHAR," |
| 59 "disposition VARCHAR," | 73 "user_date INTEGER," |
| 60 "UNIQUE (service_url, action, type))")) { | 74 "suppression INTEGER," |
| 75 "service_url LONGVARCHAR," | |
| 76 "extension_url LONGVARCHAR," | |
| 77 "UNIQUE (action, type, url_prefix))")) { | |
| 78 return false; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_index " | |
| 83 "ON web_intents (action)")) | |
| 84 return false; | |
| 85 | |
| 86 if (!db_->Execute("CREATE INDEX IF NOT EXISTS web_intents_default_index " | |
| 87 "ON web_intents_defaults (action)")) { | |
| 61 return false; | 88 return false; |
| 62 } | 89 } |
| 63 | 90 |
| 64 if (!db_->Execute("CREATE INDEX web_intents_index ON web_intents (action)")) | |
| 65 return false; | |
| 66 | |
| 67 return true; | 91 return true; |
| 68 } | 92 } |
| 69 | 93 |
| 70 // TODO(jhawkins): Figure out Sync story. | 94 // TODO(jhawkins): Figure out Sync story. |
| 71 bool WebIntentsTable::IsSyncable() { | 95 bool WebIntentsTable::IsSyncable() { |
| 72 return false; | 96 return false; |
| 73 } | 97 } |
| 74 | 98 |
| 75 bool WebIntentsTable::GetWebIntentServices( | 99 bool WebIntentsTable::GetWebIntentServices( |
| 76 const string16& action, | 100 const string16& action, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 const WebIntentServiceData& service) { | 158 const WebIntentServiceData& service) { |
| 135 sql::Statement s(db_->GetUniqueStatement( | 159 sql::Statement s(db_->GetUniqueStatement( |
| 136 "DELETE FROM web_intents " | 160 "DELETE FROM web_intents " |
| 137 "WHERE service_url = ? AND action = ? AND type = ?")); | 161 "WHERE service_url = ? AND action = ? AND type = ?")); |
| 138 s.BindString(0, service.service_url.spec()); | 162 s.BindString(0, service.service_url.spec()); |
| 139 s.BindString16(1, service.action); | 163 s.BindString16(1, service.action); |
| 140 s.BindString16(2, service.type); | 164 s.BindString16(2, service.type); |
| 141 | 165 |
| 142 return s.Run(); | 166 return s.Run(); |
| 143 } | 167 } |
| 168 | |
| 169 bool TypeMatches(const string16 type_pattern, | |
|
James Hawkins
2012/02/05 05:21:51
Document the method.
Greg Billock
2012/02/07 00:45:06
Done.
| |
| 170 const string16 type_match) { | |
| 171 // Strip MIME parameters, lowercase, then check for match. | |
| 172 std::string pattern = UTF16ToUTF8(base::i18n::ToLower(type_pattern)); | |
| 173 pattern = pattern.substr(0, pattern.find(';')); | |
| 174 std::string mime = UTF16ToUTF8(base::i18n::ToLower(type_match)); | |
| 175 mime = mime.substr(0, mime.find(';')); | |
| 176 | |
| 177 // TODO(gbillock): put in MIME parameter matching here. | |
|
groby-ooo-7-16
2012/02/06 18:34:53
We currently handle MIME type matching in WebInten
Greg Billock
2012/02/07 00:45:06
That sounds good, especially if we already have st
| |
| 178 | |
| 179 return net::MatchesMimeType(pattern, mime); | |
|
groby-ooo-7-16
2012/02/06 18:34:53
Does not work with wild cards on one hand side of
Greg Billock
2012/02/07 00:45:06
Yeah, this assumes that the MIME type in the query
| |
| 180 } | |
| 181 | |
| 182 bool UrlMatches(const std::string& url_prefix, | |
|
groby-ooo-7-16
2012/02/06 18:34:53
GURLs use string16 (since URLs can be unicode)
Greg Billock
2012/02/07 00:45:06
GURL::spec() is UTF8, as are all DB entries where
groby-ooo-7-16
2012/02/08 00:48:45
Yes, but StartsWithASCII doesn't compare UTF8 :)
Greg Billock
2012/02/08 19:11:36
:-) True enough. For hostnames, it ought to work f
| |
| 183 const std::string& url) { | |
| 184 return StartsWithASCII(url, url_prefix, true); | |
| 185 } | |
| 186 | |
| 187 bool WebIntentsTable::GetDefaultServices( | |
| 188 const string16& action, | |
| 189 const string16& type, | |
| 190 const std::string& client_url, | |
| 191 std::vector<DefaultIntentService>* default_services) { | |
| 192 sql::Statement s(db_->GetUniqueStatement( | |
| 193 "SELECT action, type, url_prefix, user_date, suppression, " | |
| 194 "service_url, extension_url FROM web_intents_defaults " | |
| 195 "WHERE action=?")); | |
| 196 s.BindString16(0, action); | |
| 197 | |
| 198 while (s.Step()) { | |
| 199 if (TypeMatches(s.ColumnString16(1), type) && | |
| 200 UrlMatches(s.ColumnString(2), client_url)) { | |
| 201 DefaultIntentService entry; | |
| 202 entry.action = s.ColumnString16(0); | |
| 203 entry.type = s.ColumnString16(1); | |
| 204 entry.url_prefix = s.ColumnString(2); | |
| 205 entry.user_date = s.ColumnInt(3); | |
| 206 entry.suppression = s.ColumnInt(4); | |
| 207 entry.service_url = s.ColumnString(5); | |
| 208 entry.extension_url = s.ColumnString(6); | |
| 209 default_services->push_back(entry); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 return s.Succeeded(); | |
| 214 } | |
| 215 | |
| 216 bool WebIntentsTable::SetDefaultService( | |
| 217 const DefaultIntentService& default_service) { | |
| 218 sql::Statement s(db_->GetUniqueStatement( | |
| 219 "INSERT OR REPLACE INTO web_intents_defaults " | |
| 220 "(action, type, url_prefix, user_date, suppression," | |
| 221 " service_url, extension_url) " | |
| 222 "VALUES (?, ?, ?, ?, ?, ?, ?)")); | |
| 223 s.BindString16(0, default_service.action); | |
| 224 s.BindString16(1, default_service.type); | |
| 225 s.BindString(2, default_service.url_prefix); | |
| 226 s.BindInt(3, default_service.user_date); | |
| 227 s.BindInt(4, default_service.suppression); | |
| 228 s.BindString(5, default_service.service_url); | |
| 229 s.BindString(6, default_service.extension_url); | |
| 230 | |
| 231 return s.Run(); | |
| 232 } | |
| OLD | NEW |