| 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 "chrome/browser/history/url_database.h" | 5 #include "chrome/browser/history/url_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 enumerator->statement_.Assign(GetDB().GetUniqueStatement( | 284 enumerator->statement_.Assign(GetDB().GetUniqueStatement( |
| 285 "SELECT url, favicon_id FROM urls WHERE favicon_id <> 0")); | 285 "SELECT url, favicon_id FROM urls WHERE favicon_id <> 0")); |
| 286 if (!enumerator->statement_) { | 286 if (!enumerator->statement_) { |
| 287 NOTREACHED() << GetDB().GetErrorMessage(); | 287 NOTREACHED() << GetDB().GetErrorMessage(); |
| 288 return false; | 288 return false; |
| 289 } | 289 } |
| 290 enumerator->initialized_ = true; | 290 enumerator->initialized_ = true; |
| 291 return true; | 291 return true; |
| 292 } | 292 } |
| 293 | 293 |
| 294 bool URLDatabase::AutocompleteForPrefix(const string16& prefix, | 294 bool URLDatabase::AutocompleteForPrefix(const std::string& prefix, |
| 295 size_t max_results, | 295 size_t max_results, |
| 296 bool typed_only, | 296 bool typed_only, |
| 297 std::vector<history::URLRow>* results) { | 297 std::vector<history::URLRow>* results) { |
| 298 // NOTE: this query originally sorted by starred as the second parameter. But | 298 // NOTE: this query originally sorted by starred as the second parameter. But |
| 299 // as bookmarks is no longer part of the db we no longer include the order | 299 // as bookmarks is no longer part of the db we no longer include the order |
| 300 // by clause. | 300 // by clause. |
| 301 results->clear(); | 301 results->clear(); |
| 302 const char* sql; | 302 const char* sql; |
| 303 int line; | 303 int line; |
| 304 if (typed_only) { | 304 if (typed_only) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 316 } | 316 } |
| 317 sql::Statement statement( | 317 sql::Statement statement( |
| 318 GetDB().GetCachedStatement(sql::StatementID(__FILE__, line), sql)); | 318 GetDB().GetCachedStatement(sql::StatementID(__FILE__, line), sql)); |
| 319 if (!statement) | 319 if (!statement) |
| 320 return false; | 320 return false; |
| 321 | 321 |
| 322 // We will find all strings between "prefix" and this string, which is prefix | 322 // We will find all strings between "prefix" and this string, which is prefix |
| 323 // followed by the maximum character size. Use 8-bit strings for everything | 323 // followed by the maximum character size. Use 8-bit strings for everything |
| 324 // so we can be sure sqlite is comparing everything in 8-bit mode. Otherwise, | 324 // so we can be sure sqlite is comparing everything in 8-bit mode. Otherwise, |
| 325 // it will have to convert strings either to UTF-8 or UTF-16 for comparison. | 325 // it will have to convert strings either to UTF-8 or UTF-16 for comparison. |
| 326 std::string prefix_utf8(UTF16ToUTF8(prefix)); | 326 std::string end_query(prefix); |
| 327 std::string end_query(prefix_utf8); | |
| 328 end_query.push_back(std::numeric_limits<unsigned char>::max()); | 327 end_query.push_back(std::numeric_limits<unsigned char>::max()); |
| 329 | 328 |
| 330 statement.BindString(0, prefix_utf8); | 329 statement.BindString(0, prefix); |
| 331 statement.BindString(1, end_query); | 330 statement.BindString(1, end_query); |
| 332 statement.BindInt(2, static_cast<int>(max_results)); | 331 statement.BindInt(2, static_cast<int>(max_results)); |
| 333 | 332 |
| 334 while (statement.Step()) { | 333 while (statement.Step()) { |
| 335 history::URLRow info; | 334 history::URLRow info; |
| 336 FillURLRow(statement, &info); | 335 FillURLRow(statement, &info); |
| 337 if (info.url().is_valid()) | 336 if (info.url().is_valid()) |
| 338 results->push_back(info); | 337 results->push_back(info); |
| 339 } | 338 } |
| 340 return !results->empty(); | 339 return !results->empty(); |
| 341 } | 340 } |
| 342 | 341 |
| 342 bool URLDatabase::IsTypedHost(const std::string& host) { |
| 343 const char* schemes[] = { |
| 344 chrome::kHttpScheme, |
| 345 chrome::kHttpsScheme, |
| 346 chrome::kFtpScheme |
| 347 }; |
| 348 std::vector<history::URLRow> dummy; |
| 349 for (size_t i = 0; i < arraysize(schemes); ++i) { |
| 350 std::string scheme_and_host(schemes[i]); |
| 351 scheme_and_host += chrome::kStandardSchemeSeparator + host; |
| 352 if (AutocompleteForPrefix(scheme_and_host + '/', 1, true, &dummy) || |
| 353 AutocompleteForPrefix(scheme_and_host + ':', 1, true, &dummy)) |
| 354 return true; |
| 355 } |
| 356 return false; |
| 357 } |
| 358 |
| 343 bool URLDatabase::FindShortestURLFromBase(const std::string& base, | 359 bool URLDatabase::FindShortestURLFromBase(const std::string& base, |
| 344 const std::string& url, | 360 const std::string& url, |
| 345 int min_visits, | 361 int min_visits, |
| 346 int min_typed, | 362 int min_typed, |
| 347 bool allow_base, | 363 bool allow_base, |
| 348 history::URLRow* info) { | 364 history::URLRow* info) { |
| 349 // Select URLs that start with |base| and are prefixes of |url|. All parts | 365 // Select URLs that start with |base| and are prefixes of |url|. All parts |
| 350 // of this query except the substr() call can be done using the index. We | 366 // of this query except the substr() call can be done using the index. We |
| 351 // could do this query with a couple of LIKE or GLOB statements as well, but | 367 // could do this query with a couple of LIKE or GLOB statements as well, but |
| 352 // those wouldn't use the index, and would run into problems with "wildcard" | 368 // those wouldn't use the index, and would run into problems with "wildcard" |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 return GetDB().Execute(sql.c_str()); | 569 return GetDB().Execute(sql.c_str()); |
| 554 } | 570 } |
| 555 | 571 |
| 556 void URLDatabase::CreateMainURLIndex() { | 572 void URLDatabase::CreateMainURLIndex() { |
| 557 // Index over URLs so we can quickly look up based on URL. Ignore errors as | 573 // Index over URLs so we can quickly look up based on URL. Ignore errors as |
| 558 // this likely already exists (and the same below). | 574 // this likely already exists (and the same below). |
| 559 GetDB().Execute("CREATE INDEX urls_url_index ON urls (url)"); | 575 GetDB().Execute("CREATE INDEX urls_url_index ON urls (url)"); |
| 560 } | 576 } |
| 561 | 577 |
| 562 } // namespace history | 578 } // namespace history |
| OLD | NEW |