Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view.cc
blobaea462d56b163251d259115c87cc51c391d9f5bc
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/ui/omnibox/omnibox_client.h"
14 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
15 #include "chrome/browser/ui/toolbar/toolbar_model.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/omnibox/browser/autocomplete_match.h"
18 #include "grit/components_scaled_resources.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/clipboard/clipboard.h"
21 #include "ui/base/l10n/l10n_util.h"
23 // static
24 base::string16 OmniboxView::StripJavascriptSchemas(const base::string16& text) {
25 const base::string16 kJsPrefix(
26 base::ASCIIToUTF16(url::kJavaScriptScheme) + base::ASCIIToUTF16(":"));
27 base::string16 out(text);
28 while (base::StartsWith(out, kJsPrefix,
29 base::CompareCase::INSENSITIVE_ASCII)) {
30 base::TrimWhitespace(out.substr(kJsPrefix.length()), base::TRIM_LEADING,
31 &out);
33 return out;
36 // static
37 base::string16 OmniboxView::SanitizeTextForPaste(const base::string16& text) {
38 // Check for non-newline whitespace; if found, collapse whitespace runs down
39 // to single spaces.
40 // TODO(shess): It may also make sense to ignore leading or
41 // trailing whitespace when making this determination.
42 for (size_t i = 0; i < text.size(); ++i) {
43 if (base::IsUnicodeWhitespace(text[i]) &&
44 text[i] != '\n' && text[i] != '\r') {
45 const base::string16 collapsed = base::CollapseWhitespace(text, false);
46 // If the user is pasting all-whitespace, paste a single space
47 // rather than nothing, since pasting nothing feels broken.
48 return collapsed.empty() ?
49 base::ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed);
53 // Otherwise, all whitespace is newlines; remove it entirely.
54 return StripJavascriptSchemas(base::CollapseWhitespace(text, true));
57 // static
58 base::string16 OmniboxView::GetClipboardText() {
59 // Try text format.
60 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
61 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
62 ui::CLIPBOARD_TYPE_COPY_PASTE)) {
63 base::string16 text;
64 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &text);
65 return SanitizeTextForPaste(text);
68 // Try bookmark format.
70 // It is tempting to try bookmark format first, but the URL we get out of a
71 // bookmark has been cannonicalized via GURL. This means if a user copies
72 // and pastes from the URL bar to itself, the text will get fixed up and
73 // cannonicalized, which is not what the user expects. By pasting in this
74 // order, we are sure to paste what the user copied.
75 if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(),
76 ui::CLIPBOARD_TYPE_COPY_PASTE)) {
77 std::string url_str;
78 clipboard->ReadBookmark(NULL, &url_str);
79 // pass resulting url string through GURL to normalize
80 GURL url(url_str);
81 if (url.is_valid())
82 return StripJavascriptSchemas(base::UTF8ToUTF16(url.spec()));
85 return base::string16();
88 OmniboxView::~OmniboxView() {
91 void OmniboxView::OpenMatch(const AutocompleteMatch& match,
92 WindowOpenDisposition disposition,
93 const GURL& alternate_nav_url,
94 const base::string16& pasted_text,
95 size_t selected_line) {
96 // Invalid URLs such as chrome://history can end up here.
97 if (!match.destination_url.is_valid() || !model_)
98 return;
99 model_->OpenMatch(
100 match, disposition, alternate_nav_url, pasted_text, selected_line);
101 OnMatchOpened(match);
104 bool OmniboxView::IsEditingOrEmpty() const {
105 return (model_.get() && model_->user_input_in_progress()) ||
106 (GetOmniboxTextLength() == 0);
109 int OmniboxView::GetIcon() const {
110 if (!IsEditingOrEmpty())
111 return controller_->GetToolbarModel()->GetIcon();
112 int id = AutocompleteMatch::TypeToIcon(model_.get() ?
113 model_->CurrentTextType() : AutocompleteMatchType::URL_WHAT_YOU_TYPED);
114 return (id == IDR_OMNIBOX_HTTP) ? IDR_LOCATION_BAR_HTTP : id;
117 void OmniboxView::SetUserText(const base::string16& text) {
118 SetUserText(text, text, true);
121 void OmniboxView::SetUserText(const base::string16& text,
122 const base::string16& display_text,
123 bool update_popup) {
124 if (model_.get())
125 model_->SetUserText(text);
126 SetWindowTextAndCaretPos(display_text, display_text.length(), update_popup,
127 true);
130 void OmniboxView::ShowURL() {
131 SetFocus();
132 controller_->GetToolbarModel()->set_url_replacement_enabled(false);
133 model_->UpdatePermanentText();
134 RevertWithoutResettingSearchTermReplacement();
135 SelectAll(true);
138 void OmniboxView::HideURL() {
139 controller_->GetToolbarModel()->set_url_replacement_enabled(true);
140 model_->UpdatePermanentText();
141 RevertWithoutResettingSearchTermReplacement();
144 void OmniboxView::RevertAll() {
145 controller_->GetToolbarModel()->set_url_replacement_enabled(true);
146 RevertWithoutResettingSearchTermReplacement();
149 void OmniboxView::RevertWithoutResettingSearchTermReplacement() {
150 CloseOmniboxPopup();
151 if (model_.get())
152 model_->Revert();
153 TextChanged();
156 void OmniboxView::CloseOmniboxPopup() {
157 if (model_.get())
158 model_->StopAutocomplete();
161 bool OmniboxView::IsImeShowingPopup() const {
162 // Default to claiming that the IME is not showing a popup, since hiding the
163 // omnibox dropdown is a bad user experience when we don't know for sure that
164 // we have to.
165 return false;
168 void OmniboxView::ShowImeIfNeeded() {
171 bool OmniboxView::IsIndicatingQueryRefinement() const {
172 // The default implementation always returns false. Mobile ports can override
173 // this method and implement as needed.
174 return false;
177 void OmniboxView::OnMatchOpened(const AutocompleteMatch& match) {
180 OmniboxView::OmniboxView(Profile* profile,
181 OmniboxEditController* controller,
182 scoped_ptr<OmniboxClient> client,
183 CommandUpdater* command_updater)
184 : controller_(controller), command_updater_(command_updater) {
185 // |profile| can be NULL in tests.
186 if (profile)
187 model_.reset(
188 new OmniboxEditModel(this, controller, client.Pass(), profile));
191 void OmniboxView::TextChanged() {
192 EmphasizeURLComponents();
193 if (model_.get())
194 model_->OnChanged();