Omnibox: Make Bookmarks Set Inline_Autocompletion

Make BookmarksProvider set inline_autocompletion and
allowed_to_be_default_match.

This has no effect unless bookmarks are made to score more highly (and
so they'll empirically end up as the top-scoring match).

Includes unit tests.

In the process, fix whether trailing slashes are omitted, for consistency
with other providers.  This will reduce jank if bookmarks are
ever inlining.  Tested interactively.

Likewise, fix whether http:// is omitted.  Tested interactively.

Also, refactor a common piece of code from ShortcutsProvider
to URLPrefix.

Likewise, make a protected function in HistoryProvider public
for use elsewhere.  Mark it static (because it can be).

Also, fix a PreventInlineAutocomplete bug in ShortcutsProvider.
Adds a test for this.  These tests fail before this change.

Also, fix a PreventInlineAutocomplete bug in BuiltinProvider.
Didn't bother adding a test for this because no tests examine
inline_autocompletion here and I didn't want to bother adding
some just for this minor change.  Tested it interactively.
These tests fail before this change.

BUG=

Review URL: https://codereview.chromium.org/229733004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263077 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/autocomplete/url_prefix.cc b/chrome/browser/autocomplete/url_prefix.cc
index b304c7c..1d680db 100644
--- a/chrome/browser/autocomplete/url_prefix.cc
+++ b/chrome/browser/autocomplete/url_prefix.cc
@@ -7,6 +7,27 @@
 #include "base/basictypes.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/autocomplete/autocomplete_input.h"
+
+namespace {
+
+// Like URLPrefix::BestURLPrefix() except also handles the prefix of
+// "www.".
+const URLPrefix* BestURLPrefixWithWWWCase(
+    const base::string16& text,
+    const base::string16& prefix_suffix) {
+  CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix,
+                         (base::ASCIIToUTF16("www."), 1));
+  const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix);
+  if ((best_prefix == NULL) ||
+      (best_prefix->num_components < www_prefix.num_components)) {
+    if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix))
+      best_prefix = &www_prefix;
+  }
+  return best_prefix;
+}
+
+}  // namespace
 
 URLPrefix::URLPrefix(const base::string16& prefix, size_t num_components)
     : prefix(prefix),
@@ -54,3 +75,36 @@
                             const base::string16& prefix_suffix) {
   return StartsWith(text, prefix.prefix + prefix_suffix, false);
 }
+
+// static
+void URLPrefix::ComputeMatchStartAndInlineAutocompleteOffset(
+    const AutocompleteInput& input,
+    const AutocompleteInput& fixed_up_input,
+    const bool allow_www_prefix_without_scheme,
+    const base::string16& text,
+    size_t* match_start,
+    size_t* inline_autocomplete_offset) {
+  const URLPrefix* best_prefix = allow_www_prefix_without_scheme ?
+      BestURLPrefixWithWWWCase(text, input.text()) :
+      BestURLPrefix(text, input.text());
+  const base::string16* matching_string = &input.text();
+  // If we failed to find a best_prefix initially, try again using a fixed-up
+  // version of the user input.  This is especially useful to get about: URLs
+  // to inline against chrome:// shortcuts.  (about: URLs are fixed up to the
+  // chrome:// scheme.)
+  if ((best_prefix == NULL) && !fixed_up_input.text().empty() &&
+      (fixed_up_input.text() != input.text())) {
+    best_prefix = allow_www_prefix_without_scheme ?
+        BestURLPrefixWithWWWCase(text, fixed_up_input.text()) :
+        BestURLPrefix(text, fixed_up_input.text());
+    matching_string = &fixed_up_input.text();
+  }
+  if (best_prefix != NULL) {
+    *match_start = best_prefix->prefix.length();
+    *inline_autocomplete_offset =
+        best_prefix->prefix.length() + matching_string->length();
+  } else {
+    *match_start = base::string16::npos;
+    *inline_autocomplete_offset = base::string16::npos;
+  }
+}