15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/history_provider.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/bookmarks/bookmark_model_factory.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/history_service.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_service_factory.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/history/in_memory_url_index_types.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/bookmarks/browser/bookmark_model.h"
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_input.h"
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_match.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/url_util.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(done_);
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile_);
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.deletable);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryService* const history_service =
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Delete the underlying URL along with all its visits from the history DB.
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The resulting HISTORY_URLS_DELETED notification will also cause all caches
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // and indices to drop any data they might have stored pertaining to the URL.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(history_service);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(match.destination_url.is_valid());
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  history_service->DeleteURL(match.destination_url);
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeleteMatchFromMatches(match);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
40a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// static
41a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochbool HistoryProvider::PreventInlineAutocomplete(
42a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const AutocompleteInput& input) {
43a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return input.prevent_inline_autocomplete() ||
44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      (!input.text().empty() &&
45a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch       IsWhitespace(input.text()[input.text().length() - 1]));
46a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
47a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
48116680a4aac90f2aa7413d9095a592090648e557Ben MurdochHistoryProvider::HistoryProvider(Profile* profile,
49a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                 AutocompleteProvider::Type type)
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : AutocompleteProvider(type),
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      profile_(profile) {
52a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
53a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HistoryProvider::~HistoryProvider() {}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool found = false;
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (i->destination_url == match.destination_url && i->type == match.type) {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      found = true;
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      if (i->is_history_what_you_typed_match ||
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          (bookmark_model &&
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)           bookmark_model->IsBookmarked(i->destination_url))) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // We can't get rid of What-You-Typed or Bookmarked matches,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // but we can make them look like they have no backing data.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        i->deletable = false;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        i->description.clear();
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        i->description_class.clear();
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        matches_.erase(i);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(found) << "Asked to delete a URL that isn't in our set of matches";
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)ACMatchClassifications HistoryProvider::SpansFromTermMatch(
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const history::TermMatches& matches,
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    size_t text_length,
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    bool is_url) {
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ACMatchClassification::Style url_style =
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      is_url ? ACMatchClassification::URL : ACMatchClassification::NONE;
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ACMatchClassifications spans;
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (matches.empty()) {
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (text_length)
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      spans.push_back(ACMatchClassification(0, url_style));
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return spans;
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (matches[0].offset)
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    spans.push_back(ACMatchClassification(0, url_style));
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  size_t match_count = matches.size();
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 0; i < match_count;) {
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    size_t offset = matches[i].offset;
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    spans.push_back(ACMatchClassification(offset,
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        ACMatchClassification::MATCH | url_style));
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Skip all adjacent matches.
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    do {
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      offset += matches[i].length;
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      ++i;
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } while ((i < match_count) && (offset == matches[i].offset));
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (offset < text_length)
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      spans.push_back(ACMatchClassification(offset, url_style));
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return spans;
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
110