Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view.cc
blobf66cff677cc2366ade5188bce373d1f8d2f4f154
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 // This file defines helper functions shared by the various implementations
6 // of OmniboxView.
8 #include "chrome/browser/ui/omnibox/omnibox_view.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
16 #include "chrome/browser/ui/toolbar/toolbar_model.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/omnibox/autocomplete_match.h"
19 #include "components/search_engines/template_url.h"
20 #include "components/search_engines/template_url_service.h"
21 #include "grit/components_scaled_resources.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/clipboard/clipboard.h"
24 #include "ui/base/l10n/l10n_util.h"
26 // static
27 base::string16 OmniboxView::StripJavascriptSchemas(const base::string16& text) {
28 const base::string16 kJsPrefix(
29 base::ASCIIToUTF16(url::kJavaScriptScheme) + base::ASCIIToUTF16(":"));
30 base::string16 out(text);
31 while (StartsWith(out, kJsPrefix, false)) {
32 base::TrimWhitespace(out.substr(kJsPrefix.length()), base::TRIM_LEADING,
33 &out);
35 return out;
38 // static
39 base::string16 OmniboxView::SanitizeTextForPaste(const base::string16& text) {
40 // Check for non-newline whitespace; if found, collapse whitespace runs down
41 // to single spaces.
42 // TODO(shess): It may also make sense to ignore leading or
43 // trailing whitespace when making this determination.
44 for (size_t i = 0; i < text.size(); ++i) {
45 if (IsWhitespace(text[i]) && text[i] != '\n' && text[i] != '\r') {
46 const base::string16 collapsed = base::CollapseWhitespace(text, false);
47 // If the user is pasting all-whitespace, paste a single space
48 // rather than nothing, since pasting nothing feels broken.
49 return collapsed.empty() ?
50 base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed);
54 // Otherwise, all whitespace is newlines; remove it entirely.
55 return StripJavascriptSchemas(base::CollapseWhitespace(text, true));
58 // static
59 base::string16 OmniboxView::GetClipboardText() {
60 // Try text format.
61 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
62 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
63 ui::CLIPBOARD_TYPE_COPY_PASTE)) {
64 base::string16 text;
65 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &text);
66 return SanitizeTextForPaste(text);
69 // Try bookmark format.
71 // It is tempting to try bookmark format first, but the URL we get out of a
72 // bookmark has been cannonicalized via GURL. This means if a user copies
73 // and pastes from the URL bar to itself, the text will get fixed up and
74 // cannonicalized, which is not what the user expects. By pasting in this
75 // order, we are sure to paste what the user copied.
76 if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
77 ui::CLIPBOARD_TYPE_COPY_PASTE)) {
78 std::string url_str;
79 clipboard->ReadBookmark(NULL, &url_str);
80 // pass resulting url string through GURL to normalize
81 GURL url(url_str);
82 if (url.is_valid())
83 return StripJavascriptSchemas(base::UTF8ToUTF16(url.spec()));
86 return base::string16();
89 OmniboxView::~OmniboxView() {
92 void OmniboxView::OpenMatch(const AutocompleteMatch& match,
93 WindowOpenDisposition disposition,
94 const GURL& alternate_nav_url,
95 const base::string16& pasted_text,
96 size_t selected_line) {
97 // Invalid URLs such as chrome://history can end up here.
98 if (!match.destination_url.is_valid() || !model_)
99 return;
100 model_->OpenMatch(
101 match, disposition, alternate_nav_url, pasted_text, selected_line);
102 OnMatchOpened(match, controller_->GetWebContents());
105 bool OmniboxView::IsEditingOrEmpty() const {
106 return (model_.get() && model_->user_input_in_progress()) ||
107 (GetOmniboxTextLength() == 0);
110 int OmniboxView::GetIcon() const {
111 if (!IsEditingOrEmpty())
112 return controller_->GetToolbarModel()->GetIcon();
113 int id = AutocompleteMatch::TypeToIcon(model_.get() ?
114 model_->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED);
115 return (id == IDR_OMNIBOX_HTTP) ? IDR_LOCATION_BAR_HTTP : id;
118 base::string16 OmniboxView::GetHintText() const {
119 // If the user is in keyword mode (the "Search <some site>:" chip is showing)
120 // then it doesn't make sense to show the "Search <default search engine>"
121 // hint text.
122 if (model_->is_keyword_selected())
123 return base::string16();
125 // Attempt to determine the default search provider and use that in the hint
126 // text.
127 TemplateURLService* template_url_service =
128 TemplateURLServiceFactory::GetForProfile(model_->profile());
129 if (template_url_service) {
130 TemplateURL* template_url =
131 template_url_service->GetDefaultSearchProvider();
132 if (template_url)
133 return l10n_util::GetStringFUTF16(
134 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER,
135 template_url->AdjustedShortNameForLocaleDirection());
138 // Otherwise return a hint based on there being no default search provider.
139 return l10n_util::GetStringUTF16(
140 IDS_OMNIBOX_EMPTY_HINT_NO_DEFAULT_SEARCH_PROVIDER);
143 void OmniboxView::SetUserText(const base::string16& text) {
144 SetUserText(text, text, true);
147 void OmniboxView::SetUserText(const base::string16& text,
148 const base::string16& display_text,
149 bool update_popup) {
150 if (model_.get())
151 model_->SetUserText(text);
152 SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup,
153 true);
156 void OmniboxView::ShowURL() {
157 SetFocus();
158 controller_->GetToolbarModel()->set_url_replacement_enabled(false);
159 model_->UpdatePermanentText();
160 RevertWithoutResettingSearchTermReplacement();
161 SelectAll(true);
164 void OmniboxView::HideURL() {
165 controller_->GetToolbarModel()->set_url_replacement_enabled(true);
166 model_->UpdatePermanentText();
167 RevertWithoutResettingSearchTermReplacement();
170 void OmniboxView::RevertAll() {
171 controller_->GetToolbarModel()->set_url_replacement_enabled(true);
172 RevertWithoutResettingSearchTermReplacement();
175 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
176 CloseOmniboxPopup();
177 if (model_.get())
178 model_->Revert();
179 TextChanged();
182 void OmniboxView::CloseOmniboxPopup() {
183 if (model_.get())
184 model_->StopAutocomplete();
187 bool OmniboxView::IsImeShowingPopup() const {
188 // Default to claiming that the IME is not showing a popup, since hiding the
189 // omnibox dropdown is a bad user experience when we don't know for sure that
190 // we have to.
191 return false;
194 void OmniboxView::ShowImeIfNeeded() {
197 bool OmniboxView::IsIndicatingQueryRefinement() const {
198 // The default implementation always returns false. Mobile ports can override
199 // this method and implement as needed.
200 return false;
203 void OmniboxView::OnMatchOpened(const AutocompleteMatch& match,
204 content::WebContents* web_contents) {
207 OmniboxView::OmniboxView(Profile* profile,
208 OmniboxEditController* controller,
209 CommandUpdater* command_updater)
210 : controller_(controller),
211 command_updater_(command_updater) {
212 // |profile| can be NULL in tests.
213 if (profile)
214 model_.reset(new OmniboxEditModel(this, controller, profile));
217 void OmniboxView::TextChanged() {
218 EmphasizeURLComponents();
219 if (model_.get())
220 model_->OnChanged();