Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_input.cc

Issue 11414303: Make Google Search autocomplete provider cursor aware. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/autocomplete/autocomplete_input.h" 5 #include "chrome/browser/autocomplete/autocomplete_input.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/external_protocol/external_protocol_handler.h" 9 #include "chrome/browser/external_protocol/external_protocol_handler.h"
10 #include "chrome/browser/net/url_fixer_upper.h" 10 #include "chrome/browser/net/url_fixer_upper.h"
11 #include "chrome/browser/profiles/profile_io_data.h" 11 #include "chrome/browser/profiles/profile_io_data.h"
12 #include "content/public/common/url_constants.h" 12 #include "content/public/common/url_constants.h"
13 #include "googleurl/src/url_canon_ip.h" 13 #include "googleurl/src/url_canon_ip.h"
14 #include "net/base/net_util.h" 14 #include "net/base/net_util.h"
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
16 16
17 namespace {
18
19 void AdjustCursorPositionIfNecessary(size_t num_leading_chars_removed,
20 size_t* cursor_position) {
21 if (*cursor_position == string16::npos)
22 return;
23 if (num_leading_chars_removed < *cursor_position)
24 *cursor_position -= num_leading_chars_removed;
25 else
26 *cursor_position = 0;
27 }
28
29 } // namespace
30
17 AutocompleteInput::AutocompleteInput() 31 AutocompleteInput::AutocompleteInput()
18 : type_(INVALID), 32 : cursor_position_(string16::npos),
19 prevent_inline_autocomplete_(false), 33 type_(INVALID),
20 prefer_keyword_(false), 34 prevent_inline_autocomplete_(false),
21 allow_exact_keyword_match_(true), 35 prefer_keyword_(false),
22 matches_requested_(ALL_MATCHES) { 36 allow_exact_keyword_match_(true),
37 matches_requested_(ALL_MATCHES) {
23 } 38 }
24 39
25 AutocompleteInput::AutocompleteInput(const string16& text, 40 AutocompleteInput::AutocompleteInput(const string16& text,
41 size_t cursor_position,
26 const string16& desired_tld, 42 const string16& desired_tld,
27 bool prevent_inline_autocomplete, 43 bool prevent_inline_autocomplete,
28 bool prefer_keyword, 44 bool prefer_keyword,
29 bool allow_exact_keyword_match, 45 bool allow_exact_keyword_match,
30 MatchesRequested matches_requested) 46 MatchesRequested matches_requested)
31 : desired_tld_(desired_tld), 47 : cursor_position_(cursor_position),
48 desired_tld_(desired_tld),
32 prevent_inline_autocomplete_(prevent_inline_autocomplete), 49 prevent_inline_autocomplete_(prevent_inline_autocomplete),
33 prefer_keyword_(prefer_keyword), 50 prefer_keyword_(prefer_keyword),
34 allow_exact_keyword_match_(allow_exact_keyword_match), 51 allow_exact_keyword_match_(allow_exact_keyword_match),
35 matches_requested_(matches_requested) { 52 matches_requested_(matches_requested) {
53 DCHECK(cursor_position <= text.length() || cursor_position == string16::npos);
36 // None of the providers care about leading white space so we always trim it. 54 // None of the providers care about leading white space so we always trim it.
37 // Providers that care about trailing white space handle trimming themselves. 55 // Providers that care about trailing white space handle trimming themselves.
38 TrimWhitespace(text, TRIM_LEADING, &text_); 56 if ((TrimWhitespace(text, TRIM_LEADING, &text_) & TRIM_LEADING) != 0)
57 AdjustCursorPositionIfNecessary(text.length() - text_.length(),
58 &cursor_position_);
39 59
40 GURL canonicalized_url; 60 GURL canonicalized_url;
41 type_ = Parse(text_, desired_tld, &parts_, &scheme_, &canonicalized_url); 61 type_ = Parse(text_, desired_tld, &parts_, &scheme_, &canonicalized_url);
42 62
43 if (type_ == INVALID) 63 if (type_ == INVALID)
44 return; 64 return;
45 65
46 if (((type_ == UNKNOWN) || (type_ == REQUESTED_URL) || (type_ == URL)) && 66 if (((type_ == UNKNOWN) || (type_ == REQUESTED_URL) || (type_ == URL)) &&
47 canonicalized_url.is_valid() && 67 canonicalized_url.is_valid() &&
48 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || 68 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() ||
49 canonicalized_url.SchemeIsFileSystem() || 69 canonicalized_url.SchemeIsFileSystem() ||
50 !canonicalized_url.host().empty())) 70 !canonicalized_url.host().empty()))
51 canonicalized_url_ = canonicalized_url; 71 canonicalized_url_ = canonicalized_url;
52 72
53 RemoveForcedQueryStringIfNecessary(type_, &text_); 73 size_t chars_removed = RemoveForcedQueryStringIfNecessary(type_, &text_);
74 AdjustCursorPositionIfNecessary(chars_removed, &cursor_position_);
54 } 75 }
55 76
56 AutocompleteInput::~AutocompleteInput() { 77 AutocompleteInput::~AutocompleteInput() {
57 } 78 }
58 79
59 // static 80 // static
60 void AutocompleteInput::RemoveForcedQueryStringIfNecessary(Type type, 81 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary(Type type,
61 string16* text) { 82 string16* text) {
62 if (type == FORCED_QUERY && !text->empty() && (*text)[0] == L'?') 83 if (type != FORCED_QUERY || text->empty() || (*text)[0] != L'?')
63 text->erase(0, 1); 84 return 0;
85 // Drop the leading '?'.
86 text->erase(0, 1);
87 return 1;
64 } 88 }
65 89
66 // static 90 // static
67 std::string AutocompleteInput::TypeToString(Type type) { 91 std::string AutocompleteInput::TypeToString(Type type) {
68 switch (type) { 92 switch (type) {
69 case INVALID: return "invalid"; 93 case INVALID: return "invalid";
70 case UNKNOWN: return "unknown"; 94 case UNKNOWN: return "unknown";
71 case REQUESTED_URL: return "requested-url"; 95 case REQUESTED_URL: return "requested-url";
72 case URL: return "url"; 96 case URL: return "url";
73 case QUERY: return "query"; 97 case QUERY: return "query";
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 if (parts.path.is_nonempty()) 473 if (parts.path.is_nonempty())
450 ++num_nonhost_components; 474 ++num_nonhost_components;
451 if (parts.query.is_nonempty()) 475 if (parts.query.is_nonempty())
452 ++num_nonhost_components; 476 ++num_nonhost_components;
453 if (parts.ref.is_nonempty()) 477 if (parts.ref.is_nonempty())
454 ++num_nonhost_components; 478 ++num_nonhost_components;
455 return num_nonhost_components; 479 return num_nonhost_components;
456 } 480 }
457 481
458 void AutocompleteInput::UpdateText(const string16& text, 482 void AutocompleteInput::UpdateText(const string16& text,
483 size_t cursor_position,
459 const url_parse::Parsed& parts) { 484 const url_parse::Parsed& parts) {
485 DCHECK(cursor_position <= text.length() || cursor_position == string16::npos);
460 text_ = text; 486 text_ = text;
487 cursor_position_ = cursor_position;
461 parts_ = parts; 488 parts_ = parts;
462 } 489 }
463 490
464 bool AutocompleteInput::Equals(const AutocompleteInput& other) const { 491 bool AutocompleteInput::Equals(const AutocompleteInput& other) const {
465 return (text_ == other.text_) && 492 return (text_ == other.text_) &&
493 (cursor_position_ == other.cursor_position_) &&
466 (type_ == other.type_) && 494 (type_ == other.type_) &&
467 (desired_tld_ == other.desired_tld_) && 495 (desired_tld_ == other.desired_tld_) &&
468 (scheme_ == other.scheme_) && 496 (scheme_ == other.scheme_) &&
469 (prevent_inline_autocomplete_ == other.prevent_inline_autocomplete_) && 497 (prevent_inline_autocomplete_ == other.prevent_inline_autocomplete_) &&
470 (prefer_keyword_ == other.prefer_keyword_) && 498 (prefer_keyword_ == other.prefer_keyword_) &&
471 (matches_requested_ == other.matches_requested_); 499 (matches_requested_ == other.matches_requested_);
472 } 500 }
473 501
474 void AutocompleteInput::Clear() { 502 void AutocompleteInput::Clear() {
475 text_.clear(); 503 text_.clear();
504 cursor_position_ = string16::npos;
476 type_ = INVALID; 505 type_ = INVALID;
477 parts_ = url_parse::Parsed(); 506 parts_ = url_parse::Parsed();
478 scheme_.clear(); 507 scheme_.clear();
479 desired_tld_.clear(); 508 desired_tld_.clear();
480 prevent_inline_autocomplete_ = false; 509 prevent_inline_autocomplete_ = false;
481 prefer_keyword_ = false; 510 prefer_keyword_ = false;
482 } 511 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698