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