File: omnibox_result.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (165 lines) | stat: -rw-r--r-- 6,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/app_list/search/omnibox_result.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete_controller.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
#include "chrome/browser/ui/app_list/search/search_util.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/omnibox/autocomplete_match_type.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
#include "url/url_canon.h"

namespace app_list {

namespace {

// The Omnibox keyword for Google search. This is correct even if the user is
// using an international domain (such as google.com.au).
const char kGoogleSearchKeyword[] = "google.com";

int ACMatchStyleToTagStyle(int styles) {
  int tag_styles = 0;
  if (styles & ACMatchClassification::URL)
    tag_styles |= SearchResult::Tag::URL;
  if (styles & ACMatchClassification::MATCH)
    tag_styles |= SearchResult::Tag::MATCH;
  if (styles & ACMatchClassification::DIM)
    tag_styles |= SearchResult::Tag::DIM;

  return tag_styles;
}

// Translates ACMatchClassifications into SearchResult tags.
void ACMatchClassificationsToTags(
    const base::string16& text,
    const ACMatchClassifications& text_classes,
    SearchResult::Tags* tags) {
  int tag_styles = SearchResult::Tag::NONE;
  size_t tag_start = 0;

  for (size_t i = 0; i < text_classes.size(); ++i) {
    const ACMatchClassification& text_class = text_classes[i];

    // Closes current tag.
    if (tag_styles != SearchResult::Tag::NONE) {
      tags->push_back(SearchResult::Tag(
          tag_styles, tag_start, text_class.offset));
      tag_styles = SearchResult::Tag::NONE;
    }

    if (text_class.style == ACMatchClassification::NONE)
      continue;

    tag_start = text_class.offset;
    tag_styles = ACMatchStyleToTagStyle(text_class.style);
  }

  if (tag_styles != SearchResult::Tag::NONE) {
    tags->push_back(SearchResult::Tag(
        tag_styles, tag_start, text.length()));
  }
}

// Converts a Google Search URL into a spoken feedback URL, by adding query
// parameters. |search_url| must be a Google Search URL.
GURL MakeGoogleSearchSpokenFeedbackUrl(const GURL& search_url) {
  std::string query = search_url.query();
  query += "&gs_ivs=1";
  GURL::Replacements replacements;
  replacements.SetQueryStr(query);
  return search_url.ReplaceComponents(replacements);
}

}  // namespace

OmniboxResult::OmniboxResult(Profile* profile,
                             AppListControllerDelegate* list_controller,
                             AutocompleteController* autocomplete_controller,
                             bool is_voice_query,
                             const AutocompleteMatch& match)
    : profile_(profile),
      list_controller_(list_controller),
      autocomplete_controller_(autocomplete_controller),
      is_voice_query_(is_voice_query),
      match_(match) {
  if (match_.search_terms_args && autocomplete_controller_) {
    match_.search_terms_args->from_app_list = true;
    autocomplete_controller_->UpdateMatchDestinationURL(
        *match_.search_terms_args, &match_);
  }
  set_id(match_.destination_url.spec());

  // Derive relevance from omnibox relevance and normalize it to [0, 1].
  // The magic number 1500 is the highest score of an omnibox result.
  // See comments in autocomplete_provider.h.
  set_relevance(match_.relevance / 1500.0);

  UpdateIcon();
  UpdateTitleAndDetails();

  // The raw "what you typed" search results should be promoted and
  // automatically selected by voice queries. If a "history" result exactly
  // matches what you typed, then the omnibox will not produce a "what you
  // typed" result; therefore, we must also flag "history" results as voice
  // results if they exactly match the query.
  if (match_.type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
      (match_.type == AutocompleteMatchType::SEARCH_HISTORY &&
       match_.search_terms_args &&
       match_.contents == match_.search_terms_args->original_query)) {
    set_voice_result(true);
  }
}

OmniboxResult::~OmniboxResult() {
}

void OmniboxResult::Open(int event_flags) {
  RecordHistogram(OMNIBOX_SEARCH_RESULT);
  GURL url = match_.destination_url;
  if (is_voice_query_ &&
      base::UTF16ToUTF8(match_.keyword) == kGoogleSearchKeyword) {
    url = MakeGoogleSearchSpokenFeedbackUrl(url);
  }
  list_controller_->OpenURL(profile_, url, match_.transition,
                            ui::DispositionFromEventFlags(event_flags));
}

scoped_ptr<SearchResult> OmniboxResult::Duplicate() {
  return scoped_ptr<SearchResult>(new OmniboxResult(profile_, list_controller_,
                                                    autocomplete_controller_,
                                                    is_voice_query_, match_));
}

void OmniboxResult::UpdateIcon() {
  BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_);
  bool is_bookmarked =
      bookmark_model && bookmark_model->IsBookmarked(match_.destination_url);
  int resource_id = is_bookmarked ? IDR_OMNIBOX_STAR
                                  : AutocompleteMatch::TypeToIcon(match_.type);
  SetIcon(
      *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id));
}

void OmniboxResult::UpdateTitleAndDetails() {
  set_title(match_.contents);
  SearchResult::Tags title_tags;
  ACMatchClassificationsToTags(match_.contents, match_.contents_class,
                               &title_tags);
  set_title_tags(title_tags);

  set_details(match_.description);
  SearchResult::Tags details_tags;
  ACMatchClassificationsToTags(match_.description, match_.description_class,
                               &details_tags);
  set_details_tags(details_tags);
}

}  // namespace app_list