history_provider.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright (c) 2012 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 "chrome/browser/autocomplete/history_provider.h"
6
7#include <string>
8
9#include "base/strings/string_util.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/browser/autocomplete/autocomplete_match.h"
12#include "chrome/browser/history/history_service.h"
13#include "chrome/browser/history/history_service_factory.h"
14#include "chrome/browser/history/in_memory_url_index_types.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/common/url_constants.h"
17#include "components/autocomplete/autocomplete_input.h"
18#include "url/url_util.h"
19
20void HistoryProvider::DeleteMatch(const AutocompleteMatch& match) {
21  DCHECK(done_);
22  DCHECK(profile_);
23  DCHECK(match.deletable);
24
25  HistoryService* const history_service =
26      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
27
28  // Delete the underlying URL along with all its visits from the history DB.
29  // The resulting HISTORY_URLS_DELETED notification will also cause all caches
30  // and indices to drop any data they might have stored pertaining to the URL.
31  DCHECK(history_service);
32  DCHECK(match.destination_url.is_valid());
33  history_service->DeleteURL(match.destination_url);
34
35  DeleteMatchFromMatches(match);
36}
37
38// static
39bool HistoryProvider::PreventInlineAutocomplete(
40    const AutocompleteInput& input) {
41  return input.prevent_inline_autocomplete() ||
42      (!input.text().empty() &&
43       IsWhitespace(input.text()[input.text().length() - 1]));
44}
45
46HistoryProvider::HistoryProvider(Profile* profile,
47                                 AutocompleteProvider::Type type)
48    : AutocompleteProvider(type),
49      profile_(profile) {
50}
51
52HistoryProvider::~HistoryProvider() {}
53
54void HistoryProvider::DeleteMatchFromMatches(const AutocompleteMatch& match) {
55  bool found = false;
56  for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
57    if (i->destination_url == match.destination_url && i->type == match.type) {
58      found = true;
59      if (i->is_history_what_you_typed_match || i->starred) {
60        // We can't get rid of What-You-Typed or Bookmarked matches,
61        // but we can make them look like they have no backing data.
62        i->deletable = false;
63        i->description.clear();
64        i->description_class.clear();
65      } else {
66        matches_.erase(i);
67      }
68      break;
69    }
70  }
71  DCHECK(found) << "Asked to delete a URL that isn't in our set of matches";
72}
73
74// static
75ACMatchClassifications HistoryProvider::SpansFromTermMatch(
76    const history::TermMatches& matches,
77    size_t text_length,
78    bool is_url) {
79  ACMatchClassification::Style url_style =
80      is_url ? ACMatchClassification::URL : ACMatchClassification::NONE;
81  ACMatchClassifications spans;
82  if (matches.empty()) {
83    if (text_length)
84      spans.push_back(ACMatchClassification(0, url_style));
85    return spans;
86  }
87  if (matches[0].offset)
88    spans.push_back(ACMatchClassification(0, url_style));
89  size_t match_count = matches.size();
90  for (size_t i = 0; i < match_count;) {
91    size_t offset = matches[i].offset;
92    spans.push_back(ACMatchClassification(offset,
93        ACMatchClassification::MATCH | url_style));
94    // Skip all adjacent matches.
95    do {
96      offset += matches[i].length;
97      ++i;
98    } while ((i < match_count) && (offset == matches[i].offset));
99    if (offset < text_length)
100      spans.push_back(ACMatchClassification(offset, url_style));
101  }
102
103  return spans;
104}
105