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

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,
Peter Kasting 2012/12/05 20:49:38 You should be able to get by without this by using
Bart N. 2012/12/06 21:43:32 In this particular case, I strongly disagree with
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 // Initial cursor normalization not yet adjusted for possible text trimming.
54 if (cursor_position >= text.length()) {
55 cursor_position_ = string16::npos;
56 }
36 // None of the providers care about leading white space so we always trim it. 57 // 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. 58 // Providers that care about trailing white space handle trimming themselves.
38 TrimWhitespace(text, TRIM_LEADING, &text_); 59 if ((TrimWhitespace(text, TRIM_LEADING, &text_) & TRIM_LEADING) != 0)
60 AdjustCursorPositionIfNecessary(text.length() - text_.length(),
61 &cursor_position_);
39 62
40 GURL canonicalized_url; 63 GURL canonicalized_url;
41 type_ = Parse(text_, desired_tld, &parts_, &scheme_, &canonicalized_url); 64 type_ = Parse(text_, desired_tld, &parts_, &scheme_, &canonicalized_url);
42 65
43 if (type_ == INVALID) 66 if (type_ == INVALID)
44 return; 67 return;
45 68
46 if (((type_ == UNKNOWN) || (type_ == REQUESTED_URL) || (type_ == URL)) && 69 if (((type_ == UNKNOWN) || (type_ == REQUESTED_URL) || (type_ == URL)) &&
47 canonicalized_url.is_valid() && 70 canonicalized_url.is_valid() &&
48 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || 71 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() ||
49 canonicalized_url.SchemeIsFileSystem() || 72 canonicalized_url.SchemeIsFileSystem() ||
50 !canonicalized_url.host().empty())) 73 !canonicalized_url.host().empty()))
51 canonicalized_url_ = canonicalized_url; 74 canonicalized_url_ = canonicalized_url;
52 75
53 RemoveForcedQueryStringIfNecessary(type_, &text_); 76 size_t chars_removed = RemoveForcedQueryStringIfNecessary(type_, &text_);
77 AdjustCursorPositionIfNecessary(chars_removed, &cursor_position_);
54 } 78 }
55 79
56 AutocompleteInput::~AutocompleteInput() { 80 AutocompleteInput::~AutocompleteInput() {
57 } 81 }
58 82
59 // static 83 // static
60 void AutocompleteInput::RemoveForcedQueryStringIfNecessary(Type type, 84 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary(Type type,
61 string16* text) { 85 string16* text) {
Peter Kasting 2012/12/05 20:49:38 Nit: Fix alignment
Bart N. 2012/12/06 21:43:32 Done.
62 if (type == FORCED_QUERY && !text->empty() && (*text)[0] == L'?') 86 if (type == FORCED_QUERY && !text->empty() && (*text)[0] == L'?') {
Peter Kasting 2012/12/05 20:49:38 Nit: Reverse conditional and early-return to elimi
Bart N. 2012/12/06 21:43:32 Done.
Bart N. 2012/12/06 21:43:32 Done.
63 text->erase(0, 1); 87 text->erase(0, 1);
88 return 1;
89 }
90 return 0;
64 } 91 }
65 92
66 // static 93 // static
67 std::string AutocompleteInput::TypeToString(Type type) { 94 std::string AutocompleteInput::TypeToString(Type type) {
68 switch (type) { 95 switch (type) {
69 case INVALID: return "invalid"; 96 case INVALID: return "invalid";
70 case UNKNOWN: return "unknown"; 97 case UNKNOWN: return "unknown";
71 case REQUESTED_URL: return "requested-url"; 98 case REQUESTED_URL: return "requested-url";
72 case URL: return "url"; 99 case URL: return "url";
73 case QUERY: return "query"; 100 case QUERY: return "query";
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 if (parts.path.is_nonempty()) 476 if (parts.path.is_nonempty())
450 ++num_nonhost_components; 477 ++num_nonhost_components;
451 if (parts.query.is_nonempty()) 478 if (parts.query.is_nonempty())
452 ++num_nonhost_components; 479 ++num_nonhost_components;
453 if (parts.ref.is_nonempty()) 480 if (parts.ref.is_nonempty())
454 ++num_nonhost_components; 481 ++num_nonhost_components;
455 return num_nonhost_components; 482 return num_nonhost_components;
456 } 483 }
457 484
458 void AutocompleteInput::UpdateText(const string16& text, 485 void AutocompleteInput::UpdateText(const string16& text,
486 size_t cursor_position,
459 const url_parse::Parsed& parts) { 487 const url_parse::Parsed& parts) {
460 text_ = text; 488 text_ = text;
489 cursor_position_ = cursor_position;
461 parts_ = parts; 490 parts_ = parts;
462 } 491 }
463 492
464 bool AutocompleteInput::Equals(const AutocompleteInput& other) const { 493 bool AutocompleteInput::Equals(const AutocompleteInput& other) const {
465 return (text_ == other.text_) && 494 return (text_ == other.text_) &&
495 (cursor_position_ == other.cursor_position_) &&
466 (type_ == other.type_) && 496 (type_ == other.type_) &&
467 (desired_tld_ == other.desired_tld_) && 497 (desired_tld_ == other.desired_tld_) &&
468 (scheme_ == other.scheme_) && 498 (scheme_ == other.scheme_) &&
469 (prevent_inline_autocomplete_ == other.prevent_inline_autocomplete_) && 499 (prevent_inline_autocomplete_ == other.prevent_inline_autocomplete_) &&
470 (prefer_keyword_ == other.prefer_keyword_) && 500 (prefer_keyword_ == other.prefer_keyword_) &&
471 (matches_requested_ == other.matches_requested_); 501 (matches_requested_ == other.matches_requested_);
472 } 502 }
473 503
474 void AutocompleteInput::Clear() { 504 void AutocompleteInput::Clear() {
475 text_.clear(); 505 text_.clear();
506 cursor_position_ = string16::npos;
476 type_ = INVALID; 507 type_ = INVALID;
477 parts_ = url_parse::Parsed(); 508 parts_ = url_parse::Parsed();
478 scheme_.clear(); 509 scheme_.clear();
479 desired_tld_.clear(); 510 desired_tld_.clear();
480 prevent_inline_autocomplete_ = false; 511 prevent_inline_autocomplete_ = false;
481 prefer_keyword_ = false; 512 prefer_keyword_ = false;
482 } 513 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698